Skip to content

Commit

Permalink
is member check for shopper insights
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrunson committed May 6, 2024
1 parent 4a46749 commit 2add11e
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 13 deletions.
87 changes: 77 additions & 10 deletions src/shopper-insights/shopperSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { type LoggerType } from "@krakenjs/beaver-logger/src";
import { stringifyError } from "@krakenjs/belter/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";

import {
ELIGIBLE_PAYMENT_METHODS,
FPTI_TRANSITION,
SHOPPER_INSIGHTS_METRIC_NAME,
} from "../constants/api";
import { ELIGIBLE_PAYMENT_METHODS, FPTI_TRANSITION } from "../constants/api";
import { ValidationError } from "../lib";

export const shopperInsightsMetricNamespace = "shopper_insights.count";
export const recommendedPaymentsMetricNamespace =
"shopper_insights.recommended_payments.count";
export const isMemberMetricNamespace = "shopper_insights.is_member.count";

export type MerchantPayloadData = {|
email?: string,
phone?: {|
Expand Down Expand Up @@ -185,7 +186,7 @@ const parseSdkConfig = ({
|}): SdkConfig => {
if (!sdkConfig.sdkToken) {
logger.metricCounter({
namespace: SHOPPER_INSIGHTS_METRIC_NAME,
namespace: shopperInsightsMetricNamespace,
event: "error",
dimensions: {
errorType: "merchant_configuration_validation_error",
Expand All @@ -200,7 +201,7 @@ const parseSdkConfig = ({

if (!sdkConfig.pageType) {
logger.metricCounter({
namespace: SHOPPER_INSIGHTS_METRIC_NAME,
namespace: shopperInsightsMetricNamespace,
event: "error",
dimensions: {
errorType: "merchant_configuration_validation_error",
Expand All @@ -215,7 +216,7 @@ const parseSdkConfig = ({

if (sdkConfig.userIDToken) {
logger.metricCounter({
namespace: SHOPPER_INSIGHTS_METRIC_NAME,
namespace: shopperInsightsMetricNamespace,
event: "error",
dimensions: {
errorType: "merchant_configuration_validation_error",
Expand Down Expand Up @@ -269,6 +270,72 @@ export class ShopperSession {
this.sessionState = sessionState;
}

async isEligibleInPaypalNetwork(merchantPayload: MerchantPayloadData): Promise<boolean> {
const startTime = Date.now();
const data = parseMerchantPayload({
merchantPayload,
sdkConfig: this.sdkConfig,
});
try {
const body = await this.request<
RecommendedPaymentMethodsRequestData,
RecommendedPaymentMethodsResponse
>({
method: "POST",
url: `${this.sdkConfig.paypalApiDomain}/${ELIGIBLE_PAYMENT_METHODS}`,
data,
accessToken: this.sdkConfig.sdkToken,
});

this.sessionState.set("shopperInsights", {
shopperInsightsIsMemberUsed: true,
});

const eligibleMethods = body?.eligible_methods ?? {};
const eligibleInPaypalNetwork = Object.keys(eligibleMethods).some(
(paymentMethod) =>
eligibleMethods[paymentMethod]?.eligible_in_paypal_network
);

this.logger.track({
[FPTI_KEY.TRANSITION]: FPTI_TRANSITION.SHOPPER_INSIGHTS_API_SUCCESS,
[FPTI_KEY.EVENT_NAME]: FPTI_TRANSITION.SHOPPER_INSIGHTS_API_SUCCESS,
[FPTI_KEY.RESPONSE_DURATION]: (Date.now() - startTime).toString(),
shopper_insights_is_member: eligibleInPaypalNetwork,
});

this.logger.metricCounter({
namespace: isMemberMetricNamespace,
event: "success",
dimensions: {
eligibleInPaypalNetwork,
},
});

return eligibleInPaypalNetwork;
} catch (error) {
this.logger.metricCounter({
namespace: isMemberMetricNamespace,
event: "error",
dimensions: {
errorType: "api_error",
},
});

this.logger.track({
[FPTI_KEY.TRANSITION]: FPTI_TRANSITION.SHOPPER_INSIGHTS_API_ERROR,
[FPTI_KEY.EVENT_NAME]: FPTI_TRANSITION.SHOPPER_INSIGHTS_API_ERROR,
[FPTI_KEY.RESPONSE_DURATION]: (Date.now() - startTime).toString(),
});

this.logger.error("shopper_insights_api_error", {
err: stringifyError(error),
});

throw error;
}
}

async getRecommendedPaymentMethods(
merchantPayload: MerchantPayloadData
): Promise<RecommendedPaymentMethods> {
Expand Down Expand Up @@ -314,7 +381,7 @@ export class ShopperSession {
});

this.logger.metricCounter({
namespace: SHOPPER_INSIGHTS_METRIC_NAME,
namespace: recommendedPaymentsMetricNamespace,
event: "success",
dimensions: {
isPayPalRecommended: String(isPayPalRecommended),
Expand All @@ -325,7 +392,7 @@ export class ShopperSession {
return { isPayPalRecommended, isVenmoRecommended };
} catch (error) {
this.logger.metricCounter({
namespace: SHOPPER_INSIGHTS_METRIC_NAME,
namespace: recommendedPaymentsMetricNamespace,
event: "error",
dimensions: {
errorType: "api_error",
Expand Down
97 changes: 94 additions & 3 deletions src/shopper-insights/shopperSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,105 @@ const createShopperSession = ({
// $FlowIssue
logger,
sessionState,
// $FlowIssue
request,
});

describe("shopper insights component - getRecommendedPaymentMethods()", () => {
afterEach(() => {
vi.clearAllMocks();
afterEach(() => {
vi.clearAllMocks();
});

describe("shopper insights component - isEligibleInPaypalNetwork()", () => {
test("should get is member using the shopper insights API", async () => {
const shopperSession = createShopperSession();
const recommendedPaymentMethods = await shopperSession.isEligibleInPaypalNetwork({
email: "email@test.com",
phone: {
countryCode: "1",
nationalNumber: "2345678901",
},
});

expect.assertions(1);
expect(recommendedPaymentMethods).toEqual(true);
});

test("should return isEligibleInPaypalNetwork true as long as one payment method is true", async () => {
const shopperSession = createShopperSession({
request: () =>
Promise.resolve({

Check failure on line 84 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

Unexpected use of 'Promise'

Check failure on line 84 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

"Promise" is not defined

Check failure on line 84 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

Promise.resolve() is not supported in Safari 5, IE 9, Chrome 27
eligible_methods: {
venmo: {
eligible_in_paypal_network: false,
},
paypal: {
eligible_in_paypal_network: true,
},
},
}),
});

const recommendedPaymentMethods = await shopperSession.isEligibleInPaypalNetwork({
email: "email@test.com",
phone: {
countryCode: "1",
nationalNumber: "2345678901",
},
});

expect.assertions(1);
expect(recommendedPaymentMethods).toEqual(true);
});

test("should return isEligibleInPaypalNetwork false if all payment methods are false", async () => {
const shopperSession = createShopperSession({
request: () =>
Promise.resolve({

Check failure on line 111 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

Unexpected use of 'Promise'

Check failure on line 111 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

"Promise" is not defined

Check failure on line 111 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

Promise.resolve() is not supported in Safari 5, IE 9, Chrome 27
eligible_methods: {
venmo: {
eligible_in_paypal_network: false,
},
paypal: {
eligible_in_paypal_network: false,
},
},
}),
});

const recommendedPaymentMethods = await shopperSession.isEligibleInPaypalNetwork({
email: "email@test.com",
phone: {
countryCode: "1",
nationalNumber: "2345678901",
},
});

expect.assertions(1);
expect(recommendedPaymentMethods).toEqual(false);
});

test("should return isEligibleInPaypalNetwork false if no eligible payment methods", async () => {
const shopperSession = createShopperSession({
request: () =>
Promise.resolve({

Check failure on line 138 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

Unexpected use of 'Promise'

Check failure on line 138 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

"Promise" is not defined

Check failure on line 138 in src/shopper-insights/shopperSession.test.js

View workflow job for this annotation

GitHub Actions / main

Promise.resolve() is not supported in Safari 5, IE 9, Chrome 27
eligible_methods: {},
}),
});

const recommendedPaymentMethods = await shopperSession.isEligibleInPaypalNetwork({
email: "email@test.com",
phone: {
countryCode: "1",
nationalNumber: "2345678901",
},
});

expect.assertions(1);
expect(recommendedPaymentMethods).toEqual(false);
});
});

describe("shopper insights component - getRecommendedPaymentMethods()", () => {
test("should get recommended payment methods using the shopper insights API", async () => {
const shopperSession = createShopperSession();
const recommendedPaymentMethods =
Expand Down

0 comments on commit 2add11e

Please sign in to comment.