@paypal/checkout-components 5.0.287 → 5.0.288-alpha.21

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.287",
3
+ "version": "5.0.288-alpha.21",
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.0",
113
+ "@paypal/connect-loader-component": "1.1.0-beta.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,195 @@
1
+ /* @flow */
2
+ import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
3
+ import { request, noop } from "@krakenjs/belter/src";
4
+ import { getSDKHost, getClientID, getEnv } 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
+ merchantId: string,
26
+ html: string,
27
+ htmlScript: string,
28
+ style: {|
29
+ layout: string,
30
+ shape: string,
31
+ color: string,
32
+ label: string,
33
+ |},
34
+ |}>;
35
+
36
+ type ButtonVariables = $ReadOnlyArray<{|
37
+ name: string,
38
+ value: string,
39
+ |}>;
40
+
41
+ type CreateOrder = (data: {| paymentSource: string |}) => ZalgoPromise<string>;
42
+
43
+ type OnApprove = (data: {| orderID: string |}) => ZalgoPromise<void>;
44
+
45
+ export type HostedButtonsComponent =
46
+ (HostedButtonsComponentProps) => HostedButtonsInstance;
47
+
48
+ const entryPoint = "SDK";
49
+ const baseUrl = `https://${getSDKHost()}`;
50
+
51
+ const getHeaders = () => ({
52
+ Authorization: `Basic ${btoa(getClientID())}`,
53
+ "Content-Type": "application/json",
54
+ "PayPal-Entry-Point": entryPoint,
55
+ });
56
+
57
+ const getButtonVariable = (variables: ButtonVariables, key: string): string =>
58
+ variables.find((variable) => variable.name === key)?.value ?? "";
59
+
60
+ const getFundingSource = (paymentSource) => {
61
+ if (paymentSource === "credit") {
62
+ return `PAY_WITH_PAYPAL_CREDIT`;
63
+ }
64
+ return `PAY_WITH_${paymentSource.toUpperCase()}`;
65
+ };
66
+
67
+ export const getHostedButtonDetails: HostedButtonDetailsParams = ({
68
+ hostedButtonId,
69
+ }) => {
70
+ return request({
71
+ url: `${baseUrl}/ncp/api/form-fields/${hostedButtonId}`,
72
+ headers: getHeaders(),
73
+ }).then(({ body }) => {
74
+ const variables = body.button_details.button_variables;
75
+ return {
76
+ buttonType: getButtonVariable(variables, "button_type"),
77
+ merchantId: getButtonVariable(variables, "business"),
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
+ export const getHostedButtonCreateOrder = ({
91
+ buttonType,
92
+ hostedButtonId,
93
+ merchantId,
94
+ }: GetCallbackProps): CreateOrder => {
95
+ return (data) => {
96
+ const userInputs = window.__pp_form_fields?.getUserInputs?.() || {};
97
+ return request({
98
+ url: `${
99
+ getEnv() === "local" ? baseUrl : baseUrl.replace("www", "api")
100
+ }/v1/ncp/orders`,
101
+ headers: getHeaders(),
102
+ method: "POST",
103
+ body: JSON.stringify({
104
+ button_type: buttonType,
105
+ entry_point: entryPoint,
106
+ funding_source: getFundingSource(data.paymentSource),
107
+ hosted_button_id: hostedButtonId,
108
+ merchant_id: merchantId,
109
+ ...userInputs,
110
+ }),
111
+ }).then(({ body }) => body.order_id);
112
+ };
113
+ };
114
+
115
+ export const getHostedButtonOnApprove = ({
116
+ buttonType,
117
+ hostedButtonId,
118
+ merchantId,
119
+ }: GetCallbackProps): OnApprove => {
120
+ return (data) => {
121
+ return request({
122
+ url: `${baseUrl}/v1/ncp/capture-order`,
123
+ headers: getHeaders(),
124
+ method: "POST",
125
+ body: JSON.stringify({
126
+ button_type: buttonType,
127
+ entry_point: entryPoint,
128
+ hosted_button_id: hostedButtonId,
129
+ id: data.orderID,
130
+ merchant_id: merchantId,
131
+ }),
132
+ }).then(noop);
133
+ };
134
+ };
135
+
136
+ /**
137
+ * Attaches form fields (html) to the given selector, and
138
+ * initializes window.__pp_form_fields (htmlScript).
139
+ */
140
+ const renderForm = ({ html, htmlScript, selector }) => {
141
+ const elm =
142
+ typeof selector === "string" ? document.querySelector(selector) : selector;
143
+ if (elm) {
144
+ elm.innerHTML = html + htmlScript;
145
+ const newScriptEl = document.createElement("script");
146
+ const oldScriptEl = elm.querySelector("script");
147
+ newScriptEl.innerHTML = oldScriptEl?.innerHTML ?? "";
148
+ oldScriptEl?.parentNode?.replaceChild(newScriptEl, oldScriptEl);
149
+ }
150
+ };
151
+
152
+ export const getHostedButtonsComponent = (): HostedButtonsComponent => {
153
+ function HostedButtons({
154
+ hostedButtonId,
155
+ }: HostedButtonsComponentProps): HostedButtonsInstance {
156
+ const Buttons = getButtonsComponent();
157
+ const render = (selector) => {
158
+ getHostedButtonDetails({ hostedButtonId }).then(
159
+ ({ buttonType, html, htmlScript, merchantId, style }) => {
160
+ renderForm({ html, htmlScript, selector });
161
+
162
+ // $FlowFixMe
163
+ Buttons({
164
+ style,
165
+ hostedButtonId,
166
+ onInit(data, actions) {
167
+ // disable the button, listen for input changes,
168
+ // and enable the button when the form is valid
169
+ // using actions.disable() and actions.enable()
170
+ window.__pp_form_fields?.onInit?.(data, actions);
171
+ },
172
+ onClick(data, actions) {
173
+ // render form errors, if present
174
+ window.__pp_form_fields?.onClick?.(data, actions);
175
+ },
176
+ createOrder: getHostedButtonCreateOrder({
177
+ buttonType,
178
+ hostedButtonId,
179
+ merchantId,
180
+ }),
181
+ onApprove: getHostedButtonOnApprove({
182
+ buttonType,
183
+ hostedButtonId,
184
+ merchantId,
185
+ }),
186
+ }).render(selector);
187
+ }
188
+ );
189
+ };
190
+ return {
191
+ render,
192
+ };
193
+ }
194
+ return HostedButtons;
195
+ };
@@ -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
+ };
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
+ buttonType: "FIXED_PRICE",
106
+ hostedButtonId: "B1234567890",
107
+ merchantId: "M1234567890",
108
+ });
109
+
110
+ // $FlowIssue
111
+ request.mockImplementationOnce(() =>
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.mockImplementationOnce(() => 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
+ });
@@ -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,