@paypal/checkout-components 5.0.287-alpha.11 → 5.0.287-alpha.19
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/package.json +1 -1
- package/src/hosted-buttons/index.js +68 -18
- package/src/hosted-buttons/index.test.js +29 -25
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
|
|
3
3
|
import { request, noop } from "@krakenjs/belter/src";
|
|
4
|
-
import { getSDKHost } from "@paypal/sdk-client/src";
|
|
4
|
+
import { getSDKHost, getClientID } from "@paypal/sdk-client/src";
|
|
5
5
|
|
|
6
6
|
import { getButtonsComponent } from "../zoid/buttons";
|
|
7
7
|
|
|
8
8
|
type HostedButtonsProps = {|
|
|
9
|
+
buttonType?: string,
|
|
9
10
|
hostedButtonId: string,
|
|
10
11
|
merchantId?: string,
|
|
11
12
|
|};
|
|
@@ -15,7 +16,10 @@ type HostedButtonsInstance = {|
|
|
|
15
16
|
|};
|
|
16
17
|
|
|
17
18
|
type HostedButtonDetailsParams = (HostedButtonsProps) => ZalgoPromise<{|
|
|
19
|
+
buttonType: string,
|
|
18
20
|
merchantId: string,
|
|
21
|
+
html: string,
|
|
22
|
+
htmlScript: string,
|
|
19
23
|
style: {|
|
|
20
24
|
layout: string,
|
|
21
25
|
shape: string,
|
|
@@ -29,19 +33,21 @@ type ButtonVariables = $ReadOnlyArray<{|
|
|
|
29
33
|
value: string,
|
|
30
34
|
|}>;
|
|
31
35
|
|
|
32
|
-
type CreateOrder = () => ZalgoPromise<string>;
|
|
36
|
+
type CreateOrder = (data: {| paymentSource: string |}) => ZalgoPromise<string>;
|
|
33
37
|
|
|
34
38
|
type OnApprove = (data: {| orderID: string |}) => ZalgoPromise<void>;
|
|
35
39
|
|
|
36
40
|
export type HostedButtonsComponent =
|
|
37
41
|
(HostedButtonsProps) => HostedButtonsInstance;
|
|
38
42
|
|
|
39
|
-
const
|
|
43
|
+
const getHeaders = () => ({
|
|
40
44
|
"PayPal-Entry-Point": "SDK",
|
|
41
45
|
"Content-Type": "application/json",
|
|
42
|
-
}
|
|
46
|
+
Authorization: `Basic ${btoa(getClientID())}`,
|
|
47
|
+
});
|
|
43
48
|
|
|
44
|
-
const
|
|
49
|
+
const nocodeApiUrl = `https://${getSDKHost()}/ncp/api`;
|
|
50
|
+
const entryPoint = "SDK";
|
|
45
51
|
|
|
46
52
|
const getButtonVariable = (variables: ButtonVariables, key: string): string =>
|
|
47
53
|
variables.find((variable) => variable.name === key)?.value ?? "";
|
|
@@ -50,56 +56,90 @@ export const getHostedButtonDetails: HostedButtonDetailsParams = ({
|
|
|
50
56
|
hostedButtonId,
|
|
51
57
|
}) => {
|
|
52
58
|
return request({
|
|
53
|
-
url: `${
|
|
54
|
-
headers,
|
|
59
|
+
url: `${nocodeApiUrl}/form-fields/${hostedButtonId}`,
|
|
60
|
+
headers: getHeaders(),
|
|
55
61
|
}).then(({ body }) => {
|
|
62
|
+
const button_details = body.button_details;
|
|
56
63
|
return {
|
|
57
|
-
|
|
64
|
+
buttonType: getButtonVariable(
|
|
65
|
+
button_details.button_variables,
|
|
66
|
+
"button_type"
|
|
67
|
+
),
|
|
68
|
+
merchantId: getButtonVariable(
|
|
69
|
+
button_details.button_variables,
|
|
70
|
+
"business"
|
|
71
|
+
),
|
|
58
72
|
style: {
|
|
59
|
-
layout: getButtonVariable(
|
|
60
|
-
shape: getButtonVariable(
|
|
61
|
-
color: getButtonVariable(
|
|
62
|
-
label: getButtonVariable(
|
|
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
|
+
),
|
|
63
80
|
},
|
|
81
|
+
html: body.html,
|
|
82
|
+
htmlScript: body.html_script,
|
|
64
83
|
};
|
|
65
84
|
});
|
|
66
85
|
};
|
|
67
86
|
|
|
68
87
|
export const getHostedButtonCreateOrder = ({
|
|
88
|
+
buttonType,
|
|
69
89
|
hostedButtonId,
|
|
70
90
|
merchantId,
|
|
71
91
|
}: HostedButtonsProps): CreateOrder => {
|
|
72
|
-
return () => {
|
|
92
|
+
return (data) => {
|
|
93
|
+
const userInputs = window.__pp_form_fields?.getUserInputs?.() || {};
|
|
73
94
|
return request({
|
|
74
|
-
url: `${
|
|
75
|
-
headers,
|
|
95
|
+
url: `${nocodeApiUrl}/create-order`,
|
|
96
|
+
headers: getHeaders(),
|
|
76
97
|
method: "POST",
|
|
77
98
|
body: JSON.stringify({
|
|
99
|
+
button_type: buttonType,
|
|
78
100
|
hosted_button_id: hostedButtonId,
|
|
79
101
|
merchant_id: merchantId,
|
|
102
|
+
entry_point: entryPoint,
|
|
103
|
+
funding_source: `PAY_WITH_${data.paymentSource.toUpperCase()}`,
|
|
104
|
+
...userInputs,
|
|
80
105
|
}),
|
|
81
106
|
}).then(({ body }) => body.order_id);
|
|
82
107
|
};
|
|
83
108
|
};
|
|
84
109
|
|
|
85
110
|
export const getHostedButtonOnApprove = ({
|
|
111
|
+
buttonType,
|
|
86
112
|
hostedButtonId,
|
|
87
113
|
merchantId,
|
|
88
114
|
}: HostedButtonsProps): OnApprove => {
|
|
89
115
|
return (data) => {
|
|
90
116
|
return request({
|
|
91
|
-
url: `${
|
|
92
|
-
headers,
|
|
117
|
+
url: `${nocodeApiUrl}/capture-order`,
|
|
118
|
+
headers: getHeaders(),
|
|
93
119
|
method: "POST",
|
|
94
120
|
body: JSON.stringify({
|
|
121
|
+
button_type: buttonType,
|
|
95
122
|
hosted_button_id: hostedButtonId,
|
|
96
123
|
id: data.orderID,
|
|
97
124
|
merchant_id: merchantId,
|
|
125
|
+
entry_point: entryPoint,
|
|
98
126
|
}),
|
|
99
127
|
}).then(noop);
|
|
100
128
|
};
|
|
101
129
|
};
|
|
102
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
|
+
|
|
103
143
|
export const getHostedButtonsComponent = (): HostedButtonsComponent => {
|
|
104
144
|
function HostedButtons({
|
|
105
145
|
hostedButtonId,
|
|
@@ -107,16 +147,26 @@ export const getHostedButtonsComponent = (): HostedButtonsComponent => {
|
|
|
107
147
|
const Buttons = getButtonsComponent();
|
|
108
148
|
const render = (selector) => {
|
|
109
149
|
getHostedButtonDetails({ hostedButtonId }).then(
|
|
110
|
-
({
|
|
150
|
+
({ buttonType, style, html, htmlScript, merchantId }) => {
|
|
151
|
+
renderForm(selector, html, htmlScript);
|
|
152
|
+
|
|
111
153
|
// $FlowFixMe
|
|
112
154
|
Buttons({
|
|
113
155
|
style,
|
|
114
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
|
+
},
|
|
115
163
|
createOrder: getHostedButtonCreateOrder({
|
|
164
|
+
buttonType,
|
|
116
165
|
hostedButtonId,
|
|
117
166
|
merchantId,
|
|
118
167
|
}),
|
|
119
168
|
onApprove: getHostedButtonOnApprove({
|
|
169
|
+
buttonType,
|
|
120
170
|
hostedButtonId,
|
|
121
171
|
merchantId,
|
|
122
172
|
}),
|
|
@@ -24,6 +24,7 @@ vi.mock("@paypal/sdk-client/src", async () => {
|
|
|
24
24
|
return {
|
|
25
25
|
...(await vi.importActual("@paypal/sdk-client/src")),
|
|
26
26
|
getSDKHost: () => "example.com",
|
|
27
|
+
getClientID: () => "client_id_123",
|
|
27
28
|
};
|
|
28
29
|
});
|
|
29
30
|
|
|
@@ -36,34 +37,36 @@ vi.mock("../zoid/buttons", async () => {
|
|
|
36
37
|
|
|
37
38
|
const getHostedButtonDetailsResponse = {
|
|
38
39
|
body: {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
+
},
|
|
61
64
|
},
|
|
62
65
|
};
|
|
63
66
|
|
|
64
67
|
describe("HostedButtons", () => {
|
|
65
68
|
test("paypal.Buttons calls getHostedButtonDetails and invokes v5 of the SDK", () => {
|
|
66
|
-
const Buttons = vi.fn();
|
|
69
|
+
const Buttons = vi.fn(() => ({ render: vi.fn() }));
|
|
67
70
|
// $FlowIssue
|
|
68
71
|
getButtonsComponent.mockImplementationOnce(() => Buttons);
|
|
69
72
|
const HostedButtons = getHostedButtonsComponent();
|
|
@@ -73,7 +76,7 @@ describe("HostedButtons", () => {
|
|
|
73
76
|
);
|
|
74
77
|
HostedButtons({
|
|
75
78
|
hostedButtonId: "B1234567890",
|
|
76
|
-
}).render("");
|
|
79
|
+
}).render("#example");
|
|
77
80
|
expect(Buttons).toHaveBeenCalled();
|
|
78
81
|
expect.assertions(1);
|
|
79
82
|
});
|
|
@@ -114,7 +117,7 @@ describe("HostedButtons", () => {
|
|
|
114
117
|
},
|
|
115
118
|
})
|
|
116
119
|
);
|
|
117
|
-
const orderID = await createOrder();
|
|
120
|
+
const orderID = await createOrder({ paymentSource: "paypal" });
|
|
118
121
|
expect(orderID).toBe("EC-1234567890");
|
|
119
122
|
expect.assertions(1);
|
|
120
123
|
});
|
|
@@ -134,6 +137,7 @@ describe("HostedButtons", () => {
|
|
|
134
137
|
hosted_button_id: "B1234567890",
|
|
135
138
|
id: "EC-1234567890",
|
|
136
139
|
merchant_id: "M1234567890",
|
|
140
|
+
entry_point: "SDK",
|
|
137
141
|
}),
|
|
138
142
|
})
|
|
139
143
|
);
|