@paypal/checkout-components
Advanced tools
Comparing version 5.0.292 to 5.0.293
{ | ||
"name": "@paypal/checkout-components", | ||
"version": "5.0.292", | ||
"version": "5.0.293", | ||
"description": "PayPal Checkout components, for integrating checkout products.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
/* @flow */ | ||
import { getPartnerAttributionID, getSessionID } from "@paypal/sdk-client/src"; | ||
import { request } from "@krakenjs/belter/src"; | ||
import { inlineMemoize, request } from "@krakenjs/belter/src"; | ||
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src"; | ||
@@ -55,1 +55,14 @@ | ||
} | ||
export function callMemoizedRestAPI({ | ||
accessToken, | ||
method, | ||
url, | ||
data, | ||
}: RestAPIParams): ZalgoPromise<Object> { | ||
return inlineMemoize( | ||
callMemoizedRestAPI, | ||
() => callRestAPI({ accessToken, method, url, data }), | ||
[accessToken, method, url, JSON.stringify(data)] | ||
); | ||
} |
@@ -5,5 +5,4 @@ /* @flow */ | ||
import { vi, describe, expect } from "vitest"; | ||
import { request } from "@krakenjs/belter/src"; | ||
import { callRestAPI } from "../api"; | ||
import { getShopperInsightsComponent } from "./component"; | ||
@@ -29,20 +28,19 @@ | ||
vi.mock("../api", async () => { | ||
const actual = await vi.importActual("../api"); | ||
vi.mock("@krakenjs/belter/src", async () => { | ||
const actual = await vi.importActual("@krakenjs/belter/src"); | ||
return { | ||
...actual, | ||
callRestAPI: vi.fn(() => | ||
request: vi.fn(() => | ||
ZalgoPromise.resolve({ | ||
eligible_methods: { | ||
paypal: { | ||
can_be_vaulted: true, | ||
eligible_in_paypal_network: true, | ||
recommended: true, | ||
recommended_priority: 1, | ||
status: 200, | ||
headers: {}, | ||
body: { | ||
eligible_methods: { | ||
paypal: { | ||
can_be_vaulted: false, | ||
eligible_in_paypal_network: true, | ||
recommended: true, | ||
recommended_priority: 1, | ||
}, | ||
}, | ||
venmo: { | ||
can_be_vaulted: true, | ||
eligible_in_paypal_network: true, | ||
recommended: false, | ||
}, | ||
}, | ||
@@ -72,3 +70,3 @@ }) | ||
expect(callRestAPI).toHaveBeenCalled(); | ||
expect(request).toHaveBeenCalled(); | ||
expect(recommendedPaymentMethods).toEqual({ | ||
@@ -81,8 +79,75 @@ isPayPalRecommended: true, | ||
test("should get recommended payment methods from memoized request for the exact same payload", async () => { | ||
const shopperInsightsComponent = getShopperInsightsComponent(); | ||
const payload = { | ||
customer: { | ||
email: "email-1.0@test.com", | ||
phone: { | ||
countryCode: "1", | ||
nationalNumber: "2345678901", | ||
}, | ||
}, | ||
}; | ||
const response1 = | ||
await shopperInsightsComponent.getRecommendedPaymentMethods(payload); | ||
expect(request).toHaveBeenCalled(); | ||
expect(request).toHaveBeenCalledTimes(1); | ||
const response2 = | ||
await shopperInsightsComponent.getRecommendedPaymentMethods(payload); | ||
expect(request).toHaveBeenCalled(); | ||
// This should not change as the payload is same | ||
expect(request).toHaveBeenCalledTimes(1); | ||
expect(response1).toEqual({ | ||
isPayPalRecommended: true, | ||
isVenmoRecommended: false, | ||
}); | ||
expect(response2).toEqual({ | ||
isPayPalRecommended: true, | ||
isVenmoRecommended: false, | ||
}); | ||
expect.assertions(6); | ||
}); | ||
test("should not get recommended payment methods from memoized request for a different payload", async () => { | ||
const shopperInsightsComponent = getShopperInsightsComponent(); | ||
const response1 = | ||
await shopperInsightsComponent.getRecommendedPaymentMethods({ | ||
customer: { | ||
email: "email-1.1@test.com", | ||
}, | ||
}); | ||
expect(request).toHaveBeenCalled(); | ||
expect(request).toHaveBeenCalledTimes(1); | ||
const response2 = | ||
await shopperInsightsComponent.getRecommendedPaymentMethods({ | ||
customer: { | ||
email: "email-1.2@test.com", | ||
}, | ||
}); | ||
expect(request).toHaveBeenCalled(); | ||
// This must change to 2 as the payload is different | ||
expect(request).toHaveBeenCalledTimes(2); | ||
expect(response1).toEqual({ | ||
isPayPalRecommended: true, | ||
isVenmoRecommended: false, | ||
}); | ||
expect(response2).toEqual({ | ||
isPayPalRecommended: true, | ||
isVenmoRecommended: false, | ||
}); | ||
expect.assertions(6); | ||
}); | ||
test("catch errors from the API", async () => { | ||
// $FlowFixMe | ||
callRestAPI.mockImplementationOnce(() => | ||
ZalgoPromise.reject({ | ||
name: "ERROR", | ||
message: "This is an API error", | ||
request.mockImplementationOnce(() => | ||
ZalgoPromise.resolve({ | ||
status: 400, | ||
headers: {}, | ||
body: { | ||
name: "ERROR", | ||
message: "This is an API error", | ||
}, | ||
}) | ||
@@ -99,8 +164,12 @@ ); | ||
countryCode: "1", | ||
nationalNumber: "2345678901", | ||
nationalNumber: "2345678905", | ||
}, | ||
}, | ||
}) | ||
).rejects.toThrow("This is an API error"); | ||
expect(callRestAPI).toHaveBeenCalled(); | ||
).rejects.toThrow( | ||
new Error( | ||
`https://api.paypal.com/v2/payments/find-eligible-methods returned status 400\n\n{"name":"ERROR","message":"This is an API error"}` | ||
) | ||
); | ||
expect(request).toHaveBeenCalled(); | ||
expect.assertions(2); | ||
@@ -113,6 +182,6 @@ }); | ||
customer: { | ||
email: "email@test.com", | ||
email: "email10@test.com", | ||
phone: { | ||
countryCode: "1", | ||
nationalNumber: "2345678901", | ||
nationalNumber: "2345678906", | ||
}, | ||
@@ -122,10 +191,10 @@ }, | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
customer: expect.objectContaining({ | ||
email: "email@test.com", | ||
email: "email10@test.com", | ||
phone: expect.objectContaining({ | ||
country_code: "1", | ||
national_number: "2345678901", | ||
national_number: "2345678906", | ||
}), | ||
@@ -142,11 +211,11 @@ }), | ||
customer: { | ||
email: "email@test.com", | ||
email: "email2@test.com", | ||
}, | ||
}); | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
customer: expect.objectContaining({ | ||
email: "email@test.com", | ||
email: "email2@test.com", | ||
}), | ||
@@ -162,3 +231,3 @@ }), | ||
customer: { | ||
email: "email@test.com", | ||
email: "email5@test.com", | ||
phone: { | ||
@@ -171,5 +240,5 @@ countryCode: "1", | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
customer: expect.objectContaining({ | ||
@@ -190,9 +259,9 @@ phone: expect.objectContaining({ | ||
customer: { | ||
email: "email@test.com", | ||
email: "email6@test.com", | ||
}, | ||
}); | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
purchase_units: expect.arrayContaining([ | ||
@@ -217,9 +286,9 @@ expect.objectContaining({ | ||
customer: { | ||
email: "email@test.com", | ||
email: "email7@test.com", | ||
}, | ||
}); | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
customer: expect.objectContaining({ | ||
@@ -242,9 +311,9 @@ country_code: "US", | ||
customer: { | ||
email: "email@test.com", | ||
email: "email9@test.com", | ||
}, | ||
}); | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
customer: expect.objectContaining({ | ||
@@ -266,5 +335,5 @@ country_code: "US", | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
customer: expect.not.objectContaining({ | ||
@@ -282,9 +351,9 @@ country_code: expect.anything(), | ||
customer: { | ||
email: "email@test.com", | ||
email: "email9@test.com", | ||
}, | ||
}); | ||
expect(callRestAPI).toHaveBeenCalledWith( | ||
expect(request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
json: expect.objectContaining({ | ||
preferences: expect.objectContaining({ | ||
@@ -291,0 +360,0 @@ include_account_details: true, |
/* @flow */ | ||
import type { LazyProtectedExport } from "../../types"; | ||
import { protectedExport } from "../../lib"; | ||
import type { LazyExport } from "../../types"; | ||
@@ -11,4 +10,4 @@ import { | ||
export const ShopperInsights: LazyProtectedExport<ShopperInsightsComponent> = { | ||
__get__: () => protectedExport(getShopperInsightsComponent()), | ||
export const ShopperInsights: LazyExport<ShopperInsightsComponent> = { | ||
__get__: () => getShopperInsightsComponent(), | ||
}; |
/* @flow */ | ||
import { loadAxo } from "@paypal/connect-loader-component"; | ||
import { stringifyError, getCurrentScriptUID } from "@krakenjs/belter/src"; | ||
import { stringifyError } from "@krakenjs/belter/src"; | ||
import { | ||
@@ -13,2 +13,3 @@ getClientID, | ||
getDebug, | ||
getSessionID, | ||
} from "@paypal/sdk-client/src"; | ||
@@ -19,5 +20,37 @@ import { FPTI_KEY } from "@paypal/sdk-constants"; | ||
const MIN_MAJOR_VERSION = 3; | ||
const MIN_MINOR_VERSION = 97; | ||
export const MIN_BT_VERSION = `${MIN_MAJOR_VERSION}.${MIN_MINOR_VERSION}.3-connect-alpha.6.1`; // Minimum for supporting AXO | ||
export function getSdkVersion(version: string | null): string { | ||
if (!version) { | ||
return MIN_BT_VERSION; | ||
} | ||
const versionSplit = version.split("."); | ||
const majorVersion = Number(versionSplit[0]); | ||
const minorVersion = Number(versionSplit[1]); | ||
if ( | ||
majorVersion < MIN_MAJOR_VERSION || | ||
(majorVersion === MIN_MAJOR_VERSION && minorVersion < MIN_MINOR_VERSION) | ||
) { | ||
sendCountMetric({ | ||
name: "pp.app.paypal_sdk.connect.init.error.count", | ||
event: "error", | ||
dimensions: { | ||
errorName: "braintree_version_not_supported_error", | ||
}, | ||
}); | ||
throw new Error( | ||
`The braintree version used does not support Connect. Please use version ${MIN_BT_VERSION} or above` | ||
); | ||
} | ||
return version; | ||
} | ||
// $FlowFixMe | ||
export const getConnectComponent = async (merchantProps = {}) => { | ||
const cmid = getClientMetadataID(); | ||
const cmid = getClientMetadataID() || getSessionID(); | ||
const clientID = getClientID(); | ||
@@ -30,3 +63,3 @@ const userIdToken = getUserIDToken(); | ||
env, | ||
clientMetadataID: cmid || getCurrentScriptUID(), | ||
clientMetadataID: cmid, | ||
cspNonce, | ||
@@ -49,3 +82,3 @@ appName: "ppcp-sdk-connect", | ||
platform: "PPCP", | ||
btSdkVersion: "3.97.3-connect-alpha.6.1", | ||
btSdkVersion: getSdkVersion(window?.braintree?.version ?? null), | ||
minified: !debugEnabled, | ||
@@ -85,2 +118,3 @@ metadata, | ||
clientMetadataId: cmid, | ||
env, | ||
}, | ||
@@ -87,0 +121,0 @@ }); |
@@ -11,3 +11,7 @@ /* @flow */ | ||
import { getConnectComponent } from "./component"; | ||
import { | ||
getConnectComponent, | ||
getSdkVersion, | ||
MIN_BT_VERSION, | ||
} from "./component"; | ||
import { sendCountMetric } from "./sendCountMetric"; | ||
@@ -76,2 +80,3 @@ | ||
fraudnet: expect.any(Function), | ||
env: "mock-env", | ||
}, | ||
@@ -112,1 +117,30 @@ }); | ||
}); | ||
describe("getSdkVersion", () => { | ||
test("returns minimum supported braintree version for AXO if input version is null", () => { | ||
const version = getSdkVersion(null); | ||
expect(version).toEqual(MIN_BT_VERSION); | ||
}); | ||
test("returns the version passed if it is supported for AXO", () => { | ||
const result1 = getSdkVersion("3.97.00"); | ||
const result2 = getSdkVersion("3.97.alpha-test"); | ||
const result3 = getSdkVersion("4.34.beta-test"); | ||
const result4 = getSdkVersion("4.34.47"); | ||
expect(result1).toEqual("3.97.00"); | ||
expect(result2).toEqual("3.97.alpha-test"); | ||
expect(result3).toEqual("4.34.beta-test"); | ||
expect(result4).toEqual("4.34.47"); | ||
}); | ||
test("throws error if the version passed is not supported for AXO and is not null", () => { | ||
const result1 = getSdkVersion("3.96.00"); | ||
const result2 = getSdkVersion("2.87.alpha-test"); | ||
const result3 = getSdkVersion("3.34.beta-test"); | ||
expect(result1).toThrowError(); | ||
expect(result2).toThrowError(); | ||
expect(result3).toThrowError(); | ||
}); | ||
}); |
@@ -722,2 +722,12 @@ /* @flow */ | ||
referrerDomain: { | ||
type: "string", | ||
required: false, | ||
value: () => { | ||
if (window.document.referrer) { | ||
return new URL(window.document.referrer).host || undefined; | ||
} | ||
}, | ||
}, | ||
userIDToken: { | ||
@@ -734,3 +744,8 @@ type: "string", | ||
required: false, | ||
default: getClientMetadataID, | ||
default: () => { | ||
const clientMetadataId = getClientMetadataID(); | ||
const sessionID = getSessionID(); | ||
return clientMetadataId || sessionID; | ||
}, | ||
queryParam: true, | ||
@@ -737,0 +752,0 @@ }, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1145131
14587