@paypal/checkout-components 5.0.288 → 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paypal/checkout-components",
|
|
3
|
-
"version": "5.0.
|
|
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": {
|
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
"@krakenjs/zoid": "^10.3.1",
|
|
111
111
|
"@paypal/common-components": "^1.0.35",
|
|
112
112
|
"@paypal/funding-components": "^1.0.31",
|
|
113
|
-
"@paypal/connect-loader-component": "1.1.
|
|
113
|
+
"@paypal/connect-loader-component": "1.1.1",
|
|
114
114
|
"@paypal/sdk-client": "^4.0.176",
|
|
115
115
|
"@paypal/sdk-constants": "^1.0.133",
|
|
116
116
|
"@paypal/sdk-logos": "^2.2.6"
|
|
@@ -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,
|