Skip to content

Commit

Permalink
Shopper session (#195)
Browse files Browse the repository at this point in the history
* sdk_settings -> sdk_data_attributes

* get shopper session id helper
  • Loading branch information
wsbrunson authored Apr 17, 2024
1 parent c0c92a9 commit 0b51536
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@krakenjs/belter/src";
import {
COUNTRY,
SDK_SETTINGS,
SDK_DATA_ATTRIBUTES,
SDK_QUERY_KEYS,
INTENT,
COMMIT,
Expand Down Expand Up @@ -71,7 +71,7 @@ export const getSDKAttributes: GetSDKAttributes = memoize(() => {
});

export function getSDKAttribute<T: string | void>(
name: $Values<typeof SDK_SETTINGS>,
name: $Values<typeof SDK_DATA_ATTRIBUTES>,
def: T
): T {
// $FlowFixMe
Expand Down Expand Up @@ -134,19 +134,21 @@ export function getMerchantID(): $ReadOnlyArray<string> {

if (merchantIDString === "*") {
// get multiple merchant ids or emails from data-merchant-id
const merchantIDAttribute = getSDKAttribute(SDK_SETTINGS.MERCHANT_ID);
const merchantIDAttribute = getSDKAttribute(
SDK_DATA_ATTRIBUTES.MERCHANT_ID
);

if (!merchantIDAttribute) {
throw new Error(
`Must pass ${SDK_SETTINGS.MERCHANT_ID} when ${SDK_QUERY_KEYS.MERCHANT_ID}=* passed in url`
`Must pass ${SDK_DATA_ATTRIBUTES.MERCHANT_ID} when ${SDK_QUERY_KEYS.MERCHANT_ID}=* passed in url`
);
}

const merchantID = merchantIDAttribute.split(",");

if (merchantID.length <= 1) {
throw new Error(
`Must pass multiple merchant ids to ${SDK_SETTINGS.MERCHANT_ID}. If passing a single id, pass ${SDK_QUERY_KEYS.MERCHANT_ID}=XYZ in url`
`Must pass multiple merchant ids to ${SDK_DATA_ATTRIBUTES.MERCHANT_ID}. If passing a single id, pass ${SDK_QUERY_KEYS.MERCHANT_ID}=XYZ in url`
);
}

Expand All @@ -157,7 +159,7 @@ export function getMerchantID(): $ReadOnlyArray<string> {

if (hasDuplicate) {
throw new Error(
`Duplicates ${SDK_SETTINGS.MERCHANT_ID}. Must pass unique merchant ids to ${SDK_SETTINGS.MERCHANT_ID}.`
`Duplicates ${SDK_DATA_ATTRIBUTES.MERCHANT_ID}. Must pass unique merchant ids to ${SDK_DATA_ATTRIBUTES.MERCHANT_ID}.`
);
}
return merchantID;
Expand Down Expand Up @@ -220,15 +222,17 @@ export function getBuyerCountry(): ?$Values<typeof COUNTRY> {
}

export function getNamespace(): string {
return getSDKAttribute(SDK_SETTINGS.NAMESPACE) || getDefaultNamespace();
return (
getSDKAttribute(SDK_DATA_ATTRIBUTES.NAMESPACE) || getDefaultNamespace()
);
}

export function getClientToken(): ?string {
return getSDKAttribute(SDK_SETTINGS.CLIENT_TOKEN);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.CLIENT_TOKEN);
}

export function getAmount(): ?string {
const amount = getSDKAttribute(SDK_SETTINGS.AMOUNT);
const amount = getSDKAttribute(SDK_DATA_ATTRIBUTES.AMOUNT);
if (amount && !amount.match(/^\d+\.\d\d$/)) {
throw new Error(`Invalid amount: ${amount}`);
}
Expand All @@ -244,15 +248,15 @@ export function getClientAccessToken(): ?string {
}

export function getPartnerAttributionID(): ?string {
return getSDKAttribute(SDK_SETTINGS.PARTNER_ATTRIBUTION_ID);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.PARTNER_ATTRIBUTION_ID);
}

export function getMerchantRequestedPopupsDisabled(): boolean {
return getSDKAttribute(SDK_SETTINGS.POPUPS_DISABLED) === "true";
return getSDKAttribute(SDK_DATA_ATTRIBUTES.POPUPS_DISABLED) === "true";
}

export function getPageType(): ?string {
const pageType = getSDKAttribute(SDK_SETTINGS.PAGE_TYPE, "");
const pageType = getSDKAttribute(SDK_DATA_ATTRIBUTES.PAGE_TYPE, "");
const validPageType =
values(PAGE_TYPES).indexOf(pageType.toLowerCase()) !== -1;

Expand Down Expand Up @@ -307,45 +311,45 @@ export function getLocale(): LocaleType {
}

export function getCSPNonce(): string {
return getSDKAttribute(SDK_SETTINGS.CSP_NONCE) || "";
return getSDKAttribute(SDK_DATA_ATTRIBUTES.CSP_NONCE) || "";
}

export function getEnableThreeDomainSecure(): boolean {
return getSDKAttributes().hasOwnProperty(SDK_SETTINGS.ENABLE_3DS);
return getSDKAttributes().hasOwnProperty(SDK_DATA_ATTRIBUTES.ENABLE_3DS);
}

export function getSDKIntegrationSource(): ?string {
return getSDKAttribute(SDK_SETTINGS.SDK_INTEGRATION_SOURCE);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.SDK_INTEGRATION_SOURCE);
}

export function getJsSdkLibrary(): ?string {
return getSDKAttribute(SDK_SETTINGS.JS_SDK_LIBRARY);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.JS_SDK_LIBRARY);
}

export function getUserExperienceFlow(): ?string {
return getSDKAttribute(SDK_SETTINGS.USER_EXPERIENCE_FLOW);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.USER_EXPERIENCE_FLOW);
}

export function getUserIDToken(): ?string {
if (
getSDKAttribute(SDK_SETTINGS.SDK_TOKEN) &&
!getSDKAttribute(SDK_SETTINGS.USER_ID_TOKEN)
getSDKAttribute(SDK_DATA_ATTRIBUTES.SDK_TOKEN) &&
!getSDKAttribute(SDK_DATA_ATTRIBUTES.USER_ID_TOKEN)
) {
return getSDKAttribute(SDK_SETTINGS.SDK_TOKEN);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.SDK_TOKEN);
}

return getSDKAttribute(SDK_SETTINGS.USER_ID_TOKEN);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.USER_ID_TOKEN);
}

export function getSDKToken(): ?string {
if (
getSDKAttribute(SDK_SETTINGS.SDK_TOKEN) &&
getSDKAttribute(SDK_SETTINGS.USER_ID_TOKEN)
getSDKAttribute(SDK_DATA_ATTRIBUTES.SDK_TOKEN) &&
getSDKAttribute(SDK_DATA_ATTRIBUTES.USER_ID_TOKEN)
) {
throw new Error("Do not pass SDK token and ID token");
}

return getSDKAttribute(SDK_SETTINGS.SDK_TOKEN);
return getSDKAttribute(SDK_DATA_ATTRIBUTES.SDK_TOKEN);
}

type decodedCustomerId = (string) => string;
Expand All @@ -363,10 +367,14 @@ export const decodeCustomerIdFromToken: decodedCustomerId = memoize((token) => {
});

export function getCustomerId(): string {
const sdkToken = getSDKAttribute(SDK_SETTINGS.SDK_TOKEN) || "";
const sdkToken = getSDKAttribute(SDK_DATA_ATTRIBUTES.SDK_TOKEN) || "";
return decodeCustomerIdFromToken(sdkToken);
}

export function getShopperSessionId(): string {
return getSDKAttribute(SDK_DATA_ATTRIBUTES.SHOPPER_SESSION_ID) || "";
}

/* v8 ignore next 3 */
export function isChildWindow(): boolean {
return Boolean(window.xprops);
Expand Down

0 comments on commit 0b51536

Please sign in to comment.