@paypal/checkout-components 5.0.289-alpha.0 → 5.0.289-alpha.43
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 +1 -1
- package/package.json +3 -2
- package/src/connect/component.jsx +50 -19
- package/src/connect/component.test.js +37 -22
- package/src/connect/interface.js +3 -3
- package/src/connect/sendCountMetric.js +25 -0
- package/src/hosted-buttons/index.js +216 -0
- package/src/hosted-buttons/index.test.js +149 -0
- package/src/interface/button.js +8 -0
- package/src/zoid/buttons/component.jsx +6 -0
package/__sdk__.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paypal/checkout-components",
|
|
3
|
-
"version": "5.0.289-alpha.
|
|
3
|
+
"version": "5.0.289-alpha.43",
|
|
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,7 +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",
|
|
112
|
-
"@paypal/connect-loader-component": "
|
|
113
|
+
"@paypal/connect-loader-component": "1.1.1",
|
|
113
114
|
"@paypal/sdk-client": "^4.0.176",
|
|
114
115
|
"@paypal/sdk-constants": "^1.0.133",
|
|
115
116
|
"@paypal/sdk-logos": "^2.2.6"
|
|
@@ -1,48 +1,79 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
import { loadAxo } from "@paypal/connect-loader-component";
|
|
3
|
+
import { stringifyError } from "@krakenjs/belter/src";
|
|
3
4
|
import {
|
|
4
5
|
getClientID,
|
|
5
6
|
getClientMetadataID,
|
|
6
7
|
getUserIDToken,
|
|
8
|
+
getLogger,
|
|
7
9
|
} from "@paypal/sdk-client/src";
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// TODO: What's the expected structure/approach for this interface. It's not a zoid
|
|
12
|
-
// scenario, so what do we return?
|
|
13
|
-
// -> Looks like it returns a function that accepts the props
|
|
14
|
-
// How do we define the input of merchant params here?
|
|
11
|
+
import { sendCountMetric } from "./sendCountMetric";
|
|
12
|
+
|
|
15
13
|
// $FlowFixMe
|
|
16
14
|
export const getConnectComponent = async (merchantProps) => {
|
|
15
|
+
sendCountMetric({
|
|
16
|
+
name: "pp.app.paypal_sdk.connect.init.count",
|
|
17
|
+
dimensions: {},
|
|
18
|
+
});
|
|
19
|
+
|
|
17
20
|
const cmid = getClientMetadataID();
|
|
18
21
|
const clientID = getClientID();
|
|
19
22
|
const userIdToken = getUserIDToken();
|
|
20
23
|
const { metadata } = merchantProps;
|
|
21
|
-
|
|
24
|
+
|
|
25
|
+
let loadResult = {};
|
|
22
26
|
try {
|
|
23
|
-
|
|
27
|
+
loadResult = await loadAxo({
|
|
24
28
|
platform: "PPCP",
|
|
25
29
|
btSdkVersion: "3.97.3-connect-alpha.6.1",
|
|
26
|
-
minified:
|
|
30
|
+
minified: true,
|
|
27
31
|
metadata,
|
|
28
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
|
+
}
|
|
29
46
|
|
|
30
|
-
|
|
31
|
-
|
|
47
|
+
try {
|
|
48
|
+
const connect = await window.braintree.connect.create({
|
|
32
49
|
...loadResult.metadata, // returns a localeURL for assets
|
|
33
50
|
...merchantProps, // AXO specific props
|
|
34
51
|
platformOptions: {
|
|
35
52
|
platform: "PPCP",
|
|
36
|
-
userIdToken,
|
|
37
|
-
clientID,
|
|
38
|
-
clientMetadataID: cmid,
|
|
39
|
-
fraudnet: () => {
|
|
40
|
-
return "";
|
|
41
|
-
}, // Pattern TBD
|
|
53
|
+
userIdToken,
|
|
54
|
+
clientID,
|
|
55
|
+
clientMetadataID: cmid,
|
|
42
56
|
},
|
|
43
57
|
});
|
|
58
|
+
|
|
59
|
+
sendCountMetric({
|
|
60
|
+
name: "pp.app.paypal_sdk.connect.init.success.count",
|
|
61
|
+
event: "success",
|
|
62
|
+
dimensions: {},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return connect;
|
|
44
66
|
} catch (error) {
|
|
45
|
-
|
|
46
|
-
|
|
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);
|
|
47
78
|
}
|
|
48
79
|
};
|
|
@@ -9,36 +9,41 @@ import { loadAxo } from "@paypal/connect-loader-component";
|
|
|
9
9
|
import { describe, expect, test, vi } from "vitest";
|
|
10
10
|
|
|
11
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
|
+
});
|
|
12
34
|
|
|
13
35
|
describe("getConnectComponent: returns ConnectComponent", () => {
|
|
14
36
|
const mockAxoMetadata = { someData: "data" };
|
|
15
37
|
const mockProps = { someProp: "value" };
|
|
16
38
|
beforeEach(() => {
|
|
39
|
+
vi.clearAllMocks();
|
|
17
40
|
window.braintree = {
|
|
18
41
|
connect: {
|
|
19
42
|
create: vi.fn(),
|
|
20
43
|
},
|
|
21
44
|
};
|
|
22
45
|
|
|
23
|
-
vi.mock("@paypal/sdk-client/src", () => {
|
|
24
|
-
return {
|
|
25
|
-
getClientID: vi.fn(() => "mock-client-id"),
|
|
26
|
-
getClientMetadataID: vi.fn(() => "mock-cmid"),
|
|
27
|
-
getUserIDToken: vi.fn(() => "mock-uid"),
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
vi.mock("@paypal/connect-loader-component", () => {
|
|
32
|
-
return {
|
|
33
|
-
loadAxo: vi.fn(),
|
|
34
|
-
};
|
|
35
|
-
});
|
|
36
|
-
|
|
37
46
|
loadAxo.mockResolvedValue({ metadata: mockAxoMetadata });
|
|
38
|
-
|
|
39
|
-
// getClientID.mockReturnValue("mock-client-id");
|
|
40
|
-
// getClientMetadataID.mockReturnValue("mock-cmid");
|
|
41
|
-
// getUserIDToken.mockReturnValue("mock-uid");
|
|
42
47
|
});
|
|
43
48
|
|
|
44
49
|
test("loadAxo and window.braintree.connect.create are called with proper data", async () => {
|
|
@@ -57,18 +62,28 @@ describe("getConnectComponent: returns ConnectComponent", () => {
|
|
|
57
62
|
clientID: "mock-client-id",
|
|
58
63
|
clientMetadataID: "mock-cmid",
|
|
59
64
|
userIdToken: "mock-uid",
|
|
60
|
-
fraudnet: expect.any(Function),
|
|
61
65
|
},
|
|
62
66
|
});
|
|
67
|
+
expect(sendCountMetric).toBeCalledTimes(2);
|
|
63
68
|
});
|
|
64
69
|
|
|
65
70
|
test("loadAxo failure is handled", async () => {
|
|
66
71
|
const errorMessage = "Something went wrong";
|
|
67
72
|
loadAxo.mockRejectedValue(errorMessage);
|
|
68
73
|
|
|
69
|
-
|
|
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);
|
|
70
83
|
|
|
71
|
-
expect(
|
|
72
|
-
|
|
84
|
+
await expect(() => getConnectComponent(mockProps)).rejects.toThrow(
|
|
85
|
+
expectedError
|
|
86
|
+
);
|
|
87
|
+
expect(sendCountMetric).toBeCalledTimes(2);
|
|
73
88
|
});
|
|
74
89
|
});
|
package/src/connect/interface.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
/* @flow */
|
|
3
3
|
// flow-disable
|
|
4
4
|
|
|
5
|
-
import { getConnectComponent
|
|
5
|
+
import { getConnectComponent } from "./component";
|
|
6
6
|
|
|
7
|
-
type
|
|
7
|
+
type ConnectComponent = (merchantProps: any) => ConnectComponent;
|
|
8
8
|
// $FlowFixMe
|
|
9
|
-
export const Connect:
|
|
9
|
+
export const Connect: ConnectComponent = async (
|
|
10
10
|
merchantProps: any
|
|
11
11
|
): ConnectComponent => {
|
|
12
12
|
// $FlowFixMe
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
|
|
3
|
+
import { request, noop, memoize } from "@krakenjs/belter/src";
|
|
4
|
+
import { getSDKHost, getClientID, getMerchantID } from "@paypal/sdk-client/src";
|
|
5
|
+
|
|
6
|
+
import { getButtonsComponent } from "../zoid/buttons";
|
|
7
|
+
|
|
8
|
+
type HostedButtonsComponentProps = {|
|
|
9
|
+
hostedButtonId: string,
|
|
10
|
+
|};
|
|
11
|
+
|
|
12
|
+
type GetCallbackProps = {|
|
|
13
|
+
buttonType: string,
|
|
14
|
+
hostedButtonId: string,
|
|
15
|
+
merchantId: string,
|
|
16
|
+
|};
|
|
17
|
+
|
|
18
|
+
type HostedButtonsInstance = {|
|
|
19
|
+
render: (string | HTMLElement) => void,
|
|
20
|
+
|};
|
|
21
|
+
|
|
22
|
+
type HostedButtonDetailsParams =
|
|
23
|
+
(HostedButtonsComponentProps) => ZalgoPromise<{|
|
|
24
|
+
buttonType: string,
|
|
25
|
+
html: string,
|
|
26
|
+
htmlScript: string,
|
|
27
|
+
style: {|
|
|
28
|
+
layout: string,
|
|
29
|
+
shape: string,
|
|
30
|
+
color: string,
|
|
31
|
+
label: string,
|
|
32
|
+
|},
|
|
33
|
+
|}>;
|
|
34
|
+
|
|
35
|
+
type ButtonVariables = $ReadOnlyArray<{|
|
|
36
|
+
name: string,
|
|
37
|
+
value: string,
|
|
38
|
+
|}>;
|
|
39
|
+
|
|
40
|
+
type CreateOrder = (data: {| paymentSource: string |}) => ZalgoPromise<string>;
|
|
41
|
+
|
|
42
|
+
type OnApprove = (data: {| orderID: string |}) => ZalgoPromise<void>;
|
|
43
|
+
|
|
44
|
+
type CreateAccessToken = (clientID: string) => ZalgoPromise<string>;
|
|
45
|
+
|
|
46
|
+
export type HostedButtonsComponent =
|
|
47
|
+
(HostedButtonsComponentProps) => HostedButtonsInstance;
|
|
48
|
+
|
|
49
|
+
const entryPoint = "SDK";
|
|
50
|
+
const baseUrl = `https://${getSDKHost()}`;
|
|
51
|
+
|
|
52
|
+
const getHeaders = (accessToken?: string) => ({
|
|
53
|
+
...(accessToken && { Authorization: `Bearer ${accessToken}` }),
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
"PayPal-Entry-Point": entryPoint,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const getButtonVariable = (variables: ButtonVariables, key: string): string =>
|
|
59
|
+
variables?.find((variable) => variable.name === key)?.value ?? "";
|
|
60
|
+
|
|
61
|
+
const getFundingSource = (paymentSource) => {
|
|
62
|
+
if (paymentSource === "credit") {
|
|
63
|
+
return `PAY_WITH_PAYPAL_CREDIT`;
|
|
64
|
+
}
|
|
65
|
+
return `PAY_WITH_${paymentSource.toUpperCase()}`;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const getHostedButtonDetails: HostedButtonDetailsParams = ({
|
|
69
|
+
hostedButtonId,
|
|
70
|
+
}) => {
|
|
71
|
+
return request({
|
|
72
|
+
url: `${baseUrl}/ncp/api/form-fields/${hostedButtonId}`,
|
|
73
|
+
headers: getHeaders(),
|
|
74
|
+
}).then(({ body }) => {
|
|
75
|
+
const variables = body.button_details.button_variables;
|
|
76
|
+
return {
|
|
77
|
+
buttonType: getButtonVariable(variables, "button_type"),
|
|
78
|
+
style: {
|
|
79
|
+
layout: getButtonVariable(variables, "layout"),
|
|
80
|
+
shape: getButtonVariable(variables, "shape"),
|
|
81
|
+
color: getButtonVariable(variables, "color"),
|
|
82
|
+
label: getButtonVariable(variables, "button_text"),
|
|
83
|
+
},
|
|
84
|
+
html: body.html,
|
|
85
|
+
htmlScript: body.html_script,
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const createAccessToken = memoize<CreateAccessToken>((clientId) => {
|
|
91
|
+
return request({
|
|
92
|
+
url: `${baseUrl.replace("www", "api")}/v1/oauth2/token`,
|
|
93
|
+
method: "POST",
|
|
94
|
+
body: "grant_type=client_credentials",
|
|
95
|
+
headers: {
|
|
96
|
+
Authorization: `Basic ${btoa(clientId)}`,
|
|
97
|
+
"Content-Type": "application/json",
|
|
98
|
+
},
|
|
99
|
+
}).then((response) => response.body.access_token);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export const getHostedButtonCreateOrder = ({
|
|
103
|
+
buttonType,
|
|
104
|
+
hostedButtonId,
|
|
105
|
+
merchantId,
|
|
106
|
+
}: GetCallbackProps): CreateOrder => {
|
|
107
|
+
return (data) => {
|
|
108
|
+
const userInputs = window.__pp_form_fields?.getUserInputs?.() || {};
|
|
109
|
+
return createAccessToken(getClientID()).then((accessToken) => {
|
|
110
|
+
return request({
|
|
111
|
+
url: `${baseUrl.replace("www", "api")}/v1/ncp/orders`,
|
|
112
|
+
headers: getHeaders(accessToken),
|
|
113
|
+
method: "POST",
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
button_type: buttonType,
|
|
116
|
+
entry_point: entryPoint,
|
|
117
|
+
funding_source: getFundingSource(data.paymentSource),
|
|
118
|
+
hosted_button_id: hostedButtonId,
|
|
119
|
+
merchant_id: merchantId,
|
|
120
|
+
...userInputs,
|
|
121
|
+
}),
|
|
122
|
+
}).then(({ body }) => body.order_id);
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export const getHostedButtonOnApprove = ({
|
|
128
|
+
buttonType,
|
|
129
|
+
hostedButtonId,
|
|
130
|
+
merchantId,
|
|
131
|
+
}: GetCallbackProps): OnApprove => {
|
|
132
|
+
return (data) => {
|
|
133
|
+
return createAccessToken(getClientID()).then((accessToken) => {
|
|
134
|
+
return request({
|
|
135
|
+
url: `${baseUrl.replace("www", "api")}/v1/ncp/orders/${
|
|
136
|
+
data.orderID
|
|
137
|
+
}/capture`,
|
|
138
|
+
headers: getHeaders(accessToken),
|
|
139
|
+
method: "POST",
|
|
140
|
+
body: JSON.stringify({
|
|
141
|
+
button_type: buttonType,
|
|
142
|
+
entry_point: entryPoint,
|
|
143
|
+
hosted_button_id: hostedButtonId,
|
|
144
|
+
merchant_id: merchantId,
|
|
145
|
+
id: data.orderID,
|
|
146
|
+
}),
|
|
147
|
+
}).then(noop);
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Attaches form fields (html) to the given selector, and
|
|
154
|
+
* initializes window.__pp_form_fields (htmlScript).
|
|
155
|
+
*/
|
|
156
|
+
const renderForm = ({ html, htmlScript, selector }) => {
|
|
157
|
+
const elm =
|
|
158
|
+
typeof selector === "string" ? document.querySelector(selector) : selector;
|
|
159
|
+
if (elm) {
|
|
160
|
+
elm.innerHTML = html + htmlScript;
|
|
161
|
+
const newScriptEl = document.createElement("script");
|
|
162
|
+
const oldScriptEl = elm.querySelector("script");
|
|
163
|
+
newScriptEl.innerHTML = oldScriptEl?.innerHTML ?? "";
|
|
164
|
+
oldScriptEl?.parentNode?.replaceChild(newScriptEl, oldScriptEl);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export const getHostedButtonsComponent = (): HostedButtonsComponent => {
|
|
169
|
+
function HostedButtons({
|
|
170
|
+
hostedButtonId,
|
|
171
|
+
}: HostedButtonsComponentProps): HostedButtonsInstance {
|
|
172
|
+
const Buttons = getButtonsComponent();
|
|
173
|
+
const render = (selector) => {
|
|
174
|
+
// The SDK supports mutiple merchant IDs, but hosted buttons only
|
|
175
|
+
// have one merchant id as a query parameter to the SDK script.
|
|
176
|
+
// https://github.com/paypal/paypal-sdk-client/blob/c58e35f8f7adbab76523eb25b9c10543449d2d29/src/script.js#L144
|
|
177
|
+
const merchantId = getMerchantID()[0];
|
|
178
|
+
|
|
179
|
+
getHostedButtonDetails({ hostedButtonId }).then(
|
|
180
|
+
({ buttonType, html, htmlScript, style }) => {
|
|
181
|
+
renderForm({ html, htmlScript, selector });
|
|
182
|
+
|
|
183
|
+
// $FlowFixMe
|
|
184
|
+
Buttons({
|
|
185
|
+
style,
|
|
186
|
+
hostedButtonId,
|
|
187
|
+
onInit(data, actions) {
|
|
188
|
+
// disable the button, listen for input changes,
|
|
189
|
+
// and enable the button when the form is valid
|
|
190
|
+
// using actions.disable() and actions.enable()
|
|
191
|
+
window.__pp_form_fields?.onInit?.(data, actions);
|
|
192
|
+
},
|
|
193
|
+
onClick(data, actions) {
|
|
194
|
+
// render form errors, if present
|
|
195
|
+
window.__pp_form_fields?.onClick?.(data, actions);
|
|
196
|
+
},
|
|
197
|
+
createOrder: getHostedButtonCreateOrder({
|
|
198
|
+
buttonType,
|
|
199
|
+
hostedButtonId,
|
|
200
|
+
merchantId,
|
|
201
|
+
}),
|
|
202
|
+
onApprove: getHostedButtonOnApprove({
|
|
203
|
+
buttonType,
|
|
204
|
+
hostedButtonId,
|
|
205
|
+
merchantId,
|
|
206
|
+
}),
|
|
207
|
+
}).render(selector);
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
return {
|
|
212
|
+
render,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
return HostedButtons;
|
|
216
|
+
};
|
|
@@ -0,0 +1,149 @@
|
|
|
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
|
+
getMerchantID: () => ["merchant_id_123"],
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
vi.mock("../zoid/buttons", async () => {
|
|
33
|
+
return {
|
|
34
|
+
...(await vi.importActual("../zoid/buttons")),
|
|
35
|
+
getButtonsComponent: vi.fn(),
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const getHostedButtonDetailsResponse = {
|
|
40
|
+
body: {
|
|
41
|
+
button_details: {
|
|
42
|
+
button_variables: [
|
|
43
|
+
{
|
|
44
|
+
name: "business",
|
|
45
|
+
value: "M1234567890",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "shape",
|
|
49
|
+
value: "rect",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "layout",
|
|
53
|
+
value: "vertical",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: "color",
|
|
57
|
+
value: "gold",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: "button_text",
|
|
61
|
+
value: "paypal",
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
describe("HostedButtons", () => {
|
|
69
|
+
test("paypal.Buttons calls getHostedButtonDetails and invokes v5 of the SDK", () => {
|
|
70
|
+
const Buttons = vi.fn(() => ({ render: vi.fn() }));
|
|
71
|
+
// $FlowIssue
|
|
72
|
+
getButtonsComponent.mockImplementationOnce(() => Buttons);
|
|
73
|
+
const HostedButtons = getHostedButtonsComponent();
|
|
74
|
+
// $FlowIssue
|
|
75
|
+
request.mockImplementationOnce(() =>
|
|
76
|
+
ZalgoPromise.resolve(getHostedButtonDetailsResponse)
|
|
77
|
+
);
|
|
78
|
+
HostedButtons({
|
|
79
|
+
hostedButtonId: "B1234567890",
|
|
80
|
+
}).render("#example");
|
|
81
|
+
expect(Buttons).toHaveBeenCalled();
|
|
82
|
+
expect.assertions(1);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("getHostedButtonDetails", async () => {
|
|
86
|
+
// $FlowIssue
|
|
87
|
+
request.mockImplementationOnce(() =>
|
|
88
|
+
ZalgoPromise.resolve(getHostedButtonDetailsResponse)
|
|
89
|
+
);
|
|
90
|
+
await getHostedButtonDetails({
|
|
91
|
+
hostedButtonId: "B1234567890",
|
|
92
|
+
}).then(({ style }) => {
|
|
93
|
+
expect(style).toEqual({
|
|
94
|
+
layout: "vertical",
|
|
95
|
+
shape: "rect",
|
|
96
|
+
color: "gold",
|
|
97
|
+
label: "paypal",
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
expect.assertions(1);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("getHostedButtonCreateOrder", async () => {
|
|
104
|
+
const createOrder = getHostedButtonCreateOrder({
|
|
105
|
+
buttonType: "FIXED_PRICE",
|
|
106
|
+
hostedButtonId: "B1234567890",
|
|
107
|
+
merchantId: "M1234567890",
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// $FlowIssue
|
|
111
|
+
request.mockImplementation(() =>
|
|
112
|
+
ZalgoPromise.resolve({
|
|
113
|
+
body: {
|
|
114
|
+
hosted_button_id: "B1234567890",
|
|
115
|
+
merchant_id: "M1234567890",
|
|
116
|
+
order_id: "EC-1234567890",
|
|
117
|
+
status: "PAYER_ACTION_REQUIRED",
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
const orderID = await createOrder({ paymentSource: "paypal" });
|
|
122
|
+
expect(orderID).toBe("EC-1234567890");
|
|
123
|
+
expect.assertions(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("getHostedButtonOnApprove", async () => {
|
|
127
|
+
const onApprove = getHostedButtonOnApprove({
|
|
128
|
+
buttonType: "FIXED_PRICE",
|
|
129
|
+
hostedButtonId: "B1234567890",
|
|
130
|
+
merchantId: "M1234567890",
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// $FlowIssue
|
|
134
|
+
request.mockImplementation(() => ZalgoPromise.resolve({}));
|
|
135
|
+
await onApprove({ orderID: "EC-1234567890" });
|
|
136
|
+
expect(request).toHaveBeenCalledWith(
|
|
137
|
+
expect.objectContaining({
|
|
138
|
+
body: JSON.stringify({
|
|
139
|
+
button_type: "FIXED_PRICE",
|
|
140
|
+
entry_point: "SDK",
|
|
141
|
+
hosted_button_id: "B1234567890",
|
|
142
|
+
id: "EC-1234567890",
|
|
143
|
+
merchant_id: "M1234567890",
|
|
144
|
+
}),
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
expect.assertions(1);
|
|
148
|
+
});
|
|
149
|
+
});
|
package/src/interface/button.js
CHANGED
|
@@ -33,11 +33,19 @@ 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";
|
|
36
40
|
|
|
37
41
|
export const Buttons: LazyExport<ButtonsComponent> = {
|
|
38
42
|
__get__: () => getButtonsComponent(),
|
|
39
43
|
};
|
|
40
44
|
|
|
45
|
+
export const HostedButtons: LazyExport<HostedButtonsComponent> = {
|
|
46
|
+
__get__: () => getHostedButtonsComponent(),
|
|
47
|
+
};
|
|
48
|
+
|
|
41
49
|
export const Checkout: LazyProtectedExport<CheckoutComponent> = {
|
|
42
50
|
__get__: () => protectedExport(getCheckoutComponent()),
|
|
43
51
|
};
|
|
@@ -834,6 +834,12 @@ 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
|
+
|
|
837
843
|
displayOnly: {
|
|
838
844
|
type: "array",
|
|
839
845
|
queryParam: true,
|