@paypal/checkout-components 5.0.287-alpha.19 → 5.0.287

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/__sdk__.js CHANGED
@@ -62,6 +62,10 @@ module.exports = {
62
62
  entry: "./src/interface/wallet",
63
63
  globals,
64
64
  },
65
+ connect: {
66
+ entry: "./src/connect/interface",
67
+ globals,
68
+ },
65
69
  // @deprecated - renamed to payment-fields to be removed
66
70
  fields: {
67
71
  entry: "./src/interface/fields",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paypal/checkout-components",
3
- "version": "5.0.287-alpha.19",
3
+ "version": "5.0.287",
4
4
  "description": "PayPal Checkout components, for integrating checkout products.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -33,6 +33,7 @@
33
33
  "percy-screenshot": "npx playwright install && babel-node ./test/percy/server/createButtonConfigs.js && percy exec -- playwright test --config=./test/percy/playwright.config.js --reporter=dot --pass-with-no-tests",
34
34
  "typecheck": "npm run flow-typed && npm run flow",
35
35
  "version": "./scripts/version.sh",
36
+ "vitest": "vitest",
36
37
  "webpack": "babel-node $(npm bin)/webpack",
37
38
  "webpack-size": "npm run webpack -- --config webpack.config.size",
38
39
  "prepare": "husky install",
@@ -109,6 +110,7 @@
109
110
  "@krakenjs/zoid": "^10.3.1",
110
111
  "@paypal/common-components": "^1.0.35",
111
112
  "@paypal/funding-components": "^1.0.31",
113
+ "@paypal/connect-loader-component": "^1.1.0",
112
114
  "@paypal/sdk-client": "^4.0.176",
113
115
  "@paypal/sdk-constants": "^1.0.133",
114
116
  "@paypal/sdk-logos": "^2.2.6"
@@ -0,0 +1,79 @@
1
+ /* @flow */
2
+ import { loadAxo } from "@paypal/connect-loader-component";
3
+ import { stringifyError } from "@krakenjs/belter/src";
4
+ import {
5
+ getClientID,
6
+ getClientMetadataID,
7
+ getUserIDToken,
8
+ getLogger,
9
+ } from "@paypal/sdk-client/src";
10
+
11
+ import { sendCountMetric } from "./sendCountMetric";
12
+
13
+ // $FlowFixMe
14
+ export const getConnectComponent = async (merchantProps) => {
15
+ sendCountMetric({
16
+ name: "pp.app.paypal_sdk.connect.init.count",
17
+ dimensions: {},
18
+ });
19
+
20
+ const cmid = getClientMetadataID();
21
+ const clientID = getClientID();
22
+ const userIdToken = getUserIDToken();
23
+ const { metadata } = merchantProps;
24
+
25
+ let loadResult = {};
26
+ try {
27
+ loadResult = await loadAxo({
28
+ platform: "PPCP",
29
+ btSdkVersion: "3.97.3-connect-alpha.6.1",
30
+ minified: true,
31
+ metadata,
32
+ });
33
+ } catch (error) {
34
+ sendCountMetric({
35
+ name: "pp.app.paypal_sdk.connect.init.error.count",
36
+ event: "error",
37
+ dimensions: {
38
+ errorName: "connect_load_error",
39
+ },
40
+ });
41
+
42
+ getLogger().error("load_axo_error", { err: stringifyError(error) });
43
+
44
+ throw new Error(error);
45
+ }
46
+
47
+ try {
48
+ const connect = await window.braintree.connect.create({
49
+ ...loadResult.metadata, // returns a localeURL for assets
50
+ ...merchantProps, // AXO specific props
51
+ platformOptions: {
52
+ platform: "PPCP",
53
+ userIdToken,
54
+ clientID,
55
+ clientMetadataID: cmid,
56
+ },
57
+ });
58
+
59
+ sendCountMetric({
60
+ name: "pp.app.paypal_sdk.connect.init.success.count",
61
+ event: "success",
62
+ dimensions: {},
63
+ });
64
+
65
+ return connect;
66
+ } catch (error) {
67
+ sendCountMetric({
68
+ name: "pp.app.paypal_sdk.connect.init.error.count",
69
+ event: "error",
70
+ dimensions: {
71
+ errorName: "connect_init_error",
72
+ },
73
+ });
74
+
75
+ getLogger().error("init_axo_error", { err: stringifyError(error) });
76
+
77
+ throw new Error(error);
78
+ }
79
+ };
@@ -0,0 +1,89 @@
1
+ /* @flow */
2
+
3
+ import {
4
+ getClientID,
5
+ getClientMetadataID,
6
+ getUserIDToken,
7
+ } from "@paypal/sdk-client/src";
8
+ import { loadAxo } from "@paypal/connect-loader-component";
9
+ import { describe, expect, test, vi } from "vitest";
10
+
11
+ import { getConnectComponent } from "./component";
12
+ import { sendCountMetric } from "./sendCountMetric";
13
+
14
+ vi.mock("@paypal/sdk-client/src", () => {
15
+ return {
16
+ getClientID: vi.fn(() => "mock-client-id"),
17
+ getClientMetadataID: vi.fn(() => "mock-cmid"),
18
+ getUserIDToken: vi.fn(() => "mock-uid"),
19
+ getLogger: vi.fn(() => ({ metric: vi.fn(), error: vi.fn() })),
20
+ };
21
+ });
22
+
23
+ vi.mock("@paypal/connect-loader-component", () => {
24
+ return {
25
+ loadAxo: vi.fn(),
26
+ };
27
+ });
28
+
29
+ vi.mock("./sendCountMetric", () => {
30
+ return {
31
+ sendCountMetric: vi.fn(),
32
+ };
33
+ });
34
+
35
+ describe("getConnectComponent: returns ConnectComponent", () => {
36
+ const mockAxoMetadata = { someData: "data" };
37
+ const mockProps = { someProp: "value" };
38
+ beforeEach(() => {
39
+ vi.clearAllMocks();
40
+ window.braintree = {
41
+ connect: {
42
+ create: vi.fn(),
43
+ },
44
+ };
45
+
46
+ loadAxo.mockResolvedValue({ metadata: mockAxoMetadata });
47
+ });
48
+
49
+ test("loadAxo and window.braintree.connect.create are called with proper data", async () => {
50
+ await getConnectComponent(mockProps);
51
+
52
+ expect(getClientID).toHaveBeenCalled();
53
+ expect(getClientMetadataID).toHaveBeenCalled();
54
+ expect(getUserIDToken).toHaveBeenCalled();
55
+ expect(loadAxo).toHaveBeenCalled();
56
+
57
+ expect(window.braintree.connect.create).toHaveBeenCalledWith({
58
+ ...mockAxoMetadata,
59
+ ...mockProps,
60
+ platformOptions: {
61
+ platform: "PPCP",
62
+ clientID: "mock-client-id",
63
+ clientMetadataID: "mock-cmid",
64
+ userIdToken: "mock-uid",
65
+ },
66
+ });
67
+ expect(sendCountMetric).toBeCalledTimes(2);
68
+ });
69
+
70
+ test("loadAxo failure is handled", async () => {
71
+ const errorMessage = "Something went wrong";
72
+ loadAxo.mockRejectedValue(errorMessage);
73
+
74
+ await expect(() => getConnectComponent(mockProps)).rejects.toThrow(
75
+ errorMessage
76
+ );
77
+ expect(sendCountMetric).toHaveBeenCalledTimes(2);
78
+ });
79
+
80
+ test("connect create failure is handled", async () => {
81
+ const expectedError = "create failed";
82
+ window.braintree.connect.create.mockRejectedValue(expectedError);
83
+
84
+ await expect(() => getConnectComponent(mockProps)).rejects.toThrow(
85
+ expectedError
86
+ );
87
+ expect(sendCountMetric).toBeCalledTimes(2);
88
+ });
89
+ });
@@ -0,0 +1,16 @@
1
+ /* eslint-disable flowtype/no-weak-types */
2
+ /* @flow */
3
+ // flow-disable
4
+
5
+ import { getConnectComponent } from "./component";
6
+
7
+ type ConnectComponent = (merchantProps: any) => ConnectComponent;
8
+ // $FlowFixMe
9
+ export const Connect: ConnectComponent = async (
10
+ merchantProps: any
11
+ ): ConnectComponent => {
12
+ // $FlowFixMe
13
+ return await getConnectComponent(merchantProps);
14
+ };
15
+
16
+ /* eslint-enable flowtype/no-weak-types */
@@ -0,0 +1,19 @@
1
+ /* @flow */
2
+
3
+ import { describe, expect, vi } from "vitest";
4
+
5
+ import { getConnectComponent } from "./component";
6
+ import { Connect } from "./interface";
7
+
8
+ describe("interface.js", () => {
9
+ vi.mock("./component", () => {
10
+ return {
11
+ getConnectComponent: vi.fn(),
12
+ };
13
+ });
14
+ it("should call getConnectComponent with merchant props", async () => {
15
+ const merchantProps = { props: "someProps" };
16
+ await Connect(merchantProps);
17
+ expect(getConnectComponent).toBeCalledWith(merchantProps);
18
+ });
19
+ });
@@ -0,0 +1,25 @@
1
+ /* @flow */
2
+ import { getLogger } from "@paypal/sdk-client/src";
3
+
4
+ // TODO: This will be pulled in to a shared sdk-client util
5
+ export const sendCountMetric = ({
6
+ dimensions,
7
+ event = "unused",
8
+ name,
9
+ value = 1,
10
+ }: {|
11
+ event?: string,
12
+ name: string,
13
+ value?: number,
14
+ dimensions: {
15
+ [string]: mixed,
16
+ },
17
+ // $FlowIssue return type
18
+ |}) =>
19
+ getLogger().metric({
20
+ dimensions,
21
+ metricEventName: event,
22
+ metricNamespace: name,
23
+ metricValue: value,
24
+ metricType: "counter",
25
+ });
@@ -33,19 +33,11 @@ import {
33
33
  getModalComponent,
34
34
  type ModalComponent,
35
35
  } from "../zoid/modal/component";
36
- import {
37
- getHostedButtonsComponent,
38
- type HostedButtonsComponent,
39
- } from "../hosted-buttons";
40
36
 
41
37
  export const Buttons: LazyExport<ButtonsComponent> = {
42
38
  __get__: () => getButtonsComponent(),
43
39
  };
44
40
 
45
- export const HostedButtons: LazyExport<HostedButtonsComponent> = {
46
- __get__: () => getHostedButtonsComponent(),
47
- };
48
-
49
41
  export const Checkout: LazyProtectedExport<CheckoutComponent> = {
50
42
  __get__: () => protectedExport(getCheckoutComponent()),
51
43
  };
@@ -834,12 +834,6 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => {
834
834
  value: getExperimentation,
835
835
  },
836
836
 
837
- hostedButtonId: {
838
- type: "string",
839
- required: false,
840
- queryParam: true,
841
- },
842
-
843
837
  displayOnly: {
844
838
  type: "array",
845
839
  queryParam: true,
@@ -1,182 +0,0 @@
1
- /* @flow */
2
- import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
3
- import { request, noop } from "@krakenjs/belter/src";
4
- import { getSDKHost, getClientID } from "@paypal/sdk-client/src";
5
-
6
- import { getButtonsComponent } from "../zoid/buttons";
7
-
8
- type HostedButtonsProps = {|
9
- buttonType?: string,
10
- hostedButtonId: string,
11
- merchantId?: string,
12
- |};
13
-
14
- type HostedButtonsInstance = {|
15
- render: (string | HTMLElement) => void,
16
- |};
17
-
18
- type HostedButtonDetailsParams = (HostedButtonsProps) => ZalgoPromise<{|
19
- buttonType: string,
20
- merchantId: string,
21
- html: string,
22
- htmlScript: string,
23
- style: {|
24
- layout: string,
25
- shape: string,
26
- color: string,
27
- label: string,
28
- |},
29
- |}>;
30
-
31
- type ButtonVariables = $ReadOnlyArray<{|
32
- name: string,
33
- value: string,
34
- |}>;
35
-
36
- type CreateOrder = (data: {| paymentSource: string |}) => ZalgoPromise<string>;
37
-
38
- type OnApprove = (data: {| orderID: string |}) => ZalgoPromise<void>;
39
-
40
- export type HostedButtonsComponent =
41
- (HostedButtonsProps) => HostedButtonsInstance;
42
-
43
- const getHeaders = () => ({
44
- "PayPal-Entry-Point": "SDK",
45
- "Content-Type": "application/json",
46
- Authorization: `Basic ${btoa(getClientID())}`,
47
- });
48
-
49
- const nocodeApiUrl = `https://${getSDKHost()}/ncp/api`;
50
- const entryPoint = "SDK";
51
-
52
- const getButtonVariable = (variables: ButtonVariables, key: string): string =>
53
- variables.find((variable) => variable.name === key)?.value ?? "";
54
-
55
- export const getHostedButtonDetails: HostedButtonDetailsParams = ({
56
- hostedButtonId,
57
- }) => {
58
- return request({
59
- url: `${nocodeApiUrl}/form-fields/${hostedButtonId}`,
60
- headers: getHeaders(),
61
- }).then(({ body }) => {
62
- const button_details = body.button_details;
63
- return {
64
- buttonType: getButtonVariable(
65
- button_details.button_variables,
66
- "button_type"
67
- ),
68
- merchantId: getButtonVariable(
69
- button_details.button_variables,
70
- "business"
71
- ),
72
- style: {
73
- layout: getButtonVariable(button_details.button_variables, "layout"),
74
- shape: getButtonVariable(button_details.button_variables, "shape"),
75
- color: getButtonVariable(button_details.button_variables, "color"),
76
- label: getButtonVariable(
77
- button_details.button_variables,
78
- "button_text"
79
- ),
80
- },
81
- html: body.html,
82
- htmlScript: body.html_script,
83
- };
84
- });
85
- };
86
-
87
- export const getHostedButtonCreateOrder = ({
88
- buttonType,
89
- hostedButtonId,
90
- merchantId,
91
- }: HostedButtonsProps): CreateOrder => {
92
- return (data) => {
93
- const userInputs = window.__pp_form_fields?.getUserInputs?.() || {};
94
- return request({
95
- url: `${nocodeApiUrl}/create-order`,
96
- headers: getHeaders(),
97
- method: "POST",
98
- body: JSON.stringify({
99
- button_type: buttonType,
100
- hosted_button_id: hostedButtonId,
101
- merchant_id: merchantId,
102
- entry_point: entryPoint,
103
- funding_source: `PAY_WITH_${data.paymentSource.toUpperCase()}`,
104
- ...userInputs,
105
- }),
106
- }).then(({ body }) => body.order_id);
107
- };
108
- };
109
-
110
- export const getHostedButtonOnApprove = ({
111
- buttonType,
112
- hostedButtonId,
113
- merchantId,
114
- }: HostedButtonsProps): OnApprove => {
115
- return (data) => {
116
- return request({
117
- url: `${nocodeApiUrl}/capture-order`,
118
- headers: getHeaders(),
119
- method: "POST",
120
- body: JSON.stringify({
121
- button_type: buttonType,
122
- hosted_button_id: hostedButtonId,
123
- id: data.orderID,
124
- merchant_id: merchantId,
125
- entry_point: entryPoint,
126
- }),
127
- }).then(noop);
128
- };
129
- };
130
-
131
- const renderForm = (selector, html, htmlScript) => {
132
- const elm =
133
- typeof selector === "string" ? document.querySelector(selector) : selector;
134
- if (elm) {
135
- elm.innerHTML = html + htmlScript;
136
- const newScriptEl = document.createElement("script");
137
- const oldScriptEl = elm.querySelector("script");
138
- newScriptEl.innerHTML = oldScriptEl?.innerHTML ?? "";
139
- oldScriptEl?.parentNode?.replaceChild(newScriptEl, oldScriptEl);
140
- }
141
- };
142
-
143
- export const getHostedButtonsComponent = (): HostedButtonsComponent => {
144
- function HostedButtons({
145
- hostedButtonId,
146
- }: HostedButtonsProps): HostedButtonsInstance {
147
- const Buttons = getButtonsComponent();
148
- const render = (selector) => {
149
- getHostedButtonDetails({ hostedButtonId }).then(
150
- ({ buttonType, style, html, htmlScript, merchantId }) => {
151
- renderForm(selector, html, htmlScript);
152
-
153
- // $FlowFixMe
154
- Buttons({
155
- style,
156
- hostedButtonId,
157
- onInit(data, actions) {
158
- window.__pp_form_fields?.onInit?.(data, actions);
159
- },
160
- onClick(data, actions) {
161
- window.__pp_form_fields?.onClick?.(data, actions);
162
- },
163
- createOrder: getHostedButtonCreateOrder({
164
- buttonType,
165
- hostedButtonId,
166
- merchantId,
167
- }),
168
- onApprove: getHostedButtonOnApprove({
169
- buttonType,
170
- hostedButtonId,
171
- merchantId,
172
- }),
173
- }).render(selector);
174
- }
175
- );
176
- };
177
- return {
178
- render,
179
- };
180
- }
181
- return HostedButtons;
182
- };
@@ -1,146 +0,0 @@
1
- /* @flow */
2
-
3
- import { describe, test, expect, vi } from "vitest";
4
- import { request } from "@krakenjs/belter/src";
5
- import { ZalgoPromise } from "@krakenjs/zalgo-promise";
6
-
7
- import { getButtonsComponent } from "../zoid/buttons";
8
-
9
- import {
10
- getHostedButtonDetails,
11
- getHostedButtonCreateOrder,
12
- getHostedButtonOnApprove,
13
- getHostedButtonsComponent,
14
- } from ".";
15
-
16
- vi.mock("@krakenjs/belter/src", async () => {
17
- return {
18
- ...(await vi.importActual("@krakenjs/belter/src")),
19
- request: vi.fn(),
20
- };
21
- });
22
-
23
- vi.mock("@paypal/sdk-client/src", async () => {
24
- return {
25
- ...(await vi.importActual("@paypal/sdk-client/src")),
26
- getSDKHost: () => "example.com",
27
- getClientID: () => "client_id_123",
28
- };
29
- });
30
-
31
- vi.mock("../zoid/buttons", async () => {
32
- return {
33
- ...(await vi.importActual("../zoid/buttons")),
34
- getButtonsComponent: vi.fn(),
35
- };
36
- });
37
-
38
- const getHostedButtonDetailsResponse = {
39
- body: {
40
- button_details: {
41
- button_variables: [
42
- {
43
- name: "business",
44
- value: "M1234567890",
45
- },
46
- {
47
- name: "shape",
48
- value: "rect",
49
- },
50
- {
51
- name: "layout",
52
- value: "vertical",
53
- },
54
- {
55
- name: "color",
56
- value: "gold",
57
- },
58
- {
59
- name: "button_text",
60
- value: "paypal",
61
- },
62
- ],
63
- },
64
- },
65
- };
66
-
67
- describe("HostedButtons", () => {
68
- test("paypal.Buttons calls getHostedButtonDetails and invokes v5 of the SDK", () => {
69
- const Buttons = vi.fn(() => ({ render: vi.fn() }));
70
- // $FlowIssue
71
- getButtonsComponent.mockImplementationOnce(() => Buttons);
72
- const HostedButtons = getHostedButtonsComponent();
73
- // $FlowIssue
74
- request.mockImplementationOnce(() =>
75
- ZalgoPromise.resolve(getHostedButtonDetailsResponse)
76
- );
77
- HostedButtons({
78
- hostedButtonId: "B1234567890",
79
- }).render("#example");
80
- expect(Buttons).toHaveBeenCalled();
81
- expect.assertions(1);
82
- });
83
-
84
- test("getHostedButtonDetails", async () => {
85
- // $FlowIssue
86
- request.mockImplementationOnce(() =>
87
- ZalgoPromise.resolve(getHostedButtonDetailsResponse)
88
- );
89
- await getHostedButtonDetails({
90
- hostedButtonId: "B1234567890",
91
- }).then(({ merchantId, style }) => {
92
- expect(merchantId).toBe("M1234567890");
93
- expect(style).toEqual({
94
- layout: "vertical",
95
- shape: "rect",
96
- color: "gold",
97
- label: "paypal",
98
- });
99
- });
100
- expect.assertions(2);
101
- });
102
-
103
- test("getHostedButtonCreateOrder", async () => {
104
- const createOrder = getHostedButtonCreateOrder({
105
- hostedButtonId: "B1234567890",
106
- merchantId: "M1234567890",
107
- });
108
-
109
- // $FlowIssue
110
- request.mockImplementationOnce(() =>
111
- ZalgoPromise.resolve({
112
- body: {
113
- hosted_button_id: "B1234567890",
114
- merchant_id: "M1234567890",
115
- order_id: "EC-1234567890",
116
- status: "PAYER_ACTION_REQUIRED",
117
- },
118
- })
119
- );
120
- const orderID = await createOrder({ paymentSource: "paypal" });
121
- expect(orderID).toBe("EC-1234567890");
122
- expect.assertions(1);
123
- });
124
-
125
- test("getHostedButtonOnApprove", async () => {
126
- const onApprove = getHostedButtonOnApprove({
127
- hostedButtonId: "B1234567890",
128
- merchantId: "M1234567890",
129
- });
130
-
131
- // $FlowIssue
132
- request.mockImplementationOnce(() => ZalgoPromise.resolve({}));
133
- await onApprove({ orderID: "EC-1234567890" });
134
- expect(request).toHaveBeenCalledWith(
135
- expect.objectContaining({
136
- body: JSON.stringify({
137
- hosted_button_id: "B1234567890",
138
- id: "EC-1234567890",
139
- merchant_id: "M1234567890",
140
- entry_point: "SDK",
141
- }),
142
- })
143
- );
144
- expect.assertions(1);
145
- });
146
- });