@fat-zebra/sdk 1.0.6 → 1.2.2

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +162 -0
  3. package/dist/applepay/applepay.d.ts +20 -0
  4. package/dist/applepay/applepay.js +93 -0
  5. package/dist/applepay/clients/apple-pay-client.d.ts +24 -0
  6. package/dist/applepay/clients/apple-pay-client.js +29 -0
  7. package/dist/applepay/clients/paynow-client.d.ts +17 -0
  8. package/dist/applepay/clients/paynow-client.js +55 -0
  9. package/dist/applepay/index.d.ts +1 -0
  10. package/dist/applepay/index.js +1 -0
  11. package/dist/hpp/hpp.d.ts +61 -0
  12. package/dist/hpp/hpp.js +186 -0
  13. package/dist/hpp/index.d.ts +2 -0
  14. package/dist/hpp/index.js +1 -0
  15. package/dist/index.d.ts +5 -2
  16. package/dist/index.js +7 -1
  17. package/dist/main.d.ts +38 -0
  18. package/dist/main.js +148 -0
  19. package/dist/paypal/paypal-button.d.ts +31 -0
  20. package/dist/paypal/paypal-button.js +199 -0
  21. package/dist/paypal/paypal-checkout.d.ts +21 -0
  22. package/dist/paypal/paypal-checkout.js +100 -0
  23. package/dist/paypal/types.d.ts +188 -0
  24. package/dist/paypal/types.js +5 -0
  25. package/dist/paypal/validation.d.ts +4 -0
  26. package/dist/paypal/validation.js +65 -0
  27. package/dist/react/VerifyCard.d.ts +2 -4
  28. package/dist/react/VerifyCard.js +1 -2
  29. package/dist/react/url.d.ts +2 -11
  30. package/dist/react/useFatZebra.d.ts +2 -3
  31. package/dist/react/useFatZebra.js +7 -7
  32. package/dist/sca/cardinal.d.ts +1 -1
  33. package/dist/sca/cardinal.js +1 -1
  34. package/dist/sca/index.d.ts +8 -5
  35. package/dist/sca/index.js +26 -87
  36. package/dist/sca/scenarios/index.d.ts +3 -2
  37. package/dist/sca/types.d.ts +1 -1
  38. package/dist/sca/types.js +1 -1
  39. package/dist/shared/api-gateway-client.d.ts +7 -0
  40. package/dist/shared/api-gateway-client.js +37 -3
  41. package/dist/shared/env.d.ts +1 -1
  42. package/dist/shared/env.development.d.ts +5 -0
  43. package/dist/shared/env.development.js +5 -0
  44. package/dist/shared/env.js +1 -1
  45. package/dist/shared/event-manager.d.ts +1 -1
  46. package/dist/shared/event-manager.js +0 -1
  47. package/dist/shared/post-message-client.d.ts +1 -1
  48. package/dist/shared/post-message-client.js +2 -3
  49. package/dist/shared/types.d.ts +40 -33
  50. package/dist/shared/types.js +7 -1
  51. package/dist/validation/index.d.ts +3 -0
  52. package/dist/validation/index.js +3 -0
  53. package/dist/validation/schemas/customer.json +38 -0
  54. package/dist/validation/schemas/hpp-load-params.json +40 -0
  55. package/dist/validation/schemas/hpp-options.json +48 -0
  56. package/dist/validation/schemas/payment-intent.json +48 -0
  57. package/dist/validation/schemas/payment-method.json +83 -0
  58. package/dist/validation/schemas/verify-card-options.json +15 -0
  59. package/dist/validation/schemas/verify-card-params.json +24 -0
  60. package/dist/validation/validation-helper.d.ts +3 -0
  61. package/dist/validation/validation-helper.js +6 -0
  62. package/dist/validation/validators/hpp-load-params-validator.d.ts +3 -0
  63. package/dist/validation/validators/hpp-load-params-validator.js +14 -0
  64. package/dist/validation/validators/verify-card-params-validator.d.ts +3 -0
  65. package/dist/validation/validators/verify-card-params-validator.js +16 -0
  66. package/docker-compose.yml +16 -0
  67. package/package.json +44 -15
  68. package/tsconfig.json +8 -18
  69. package/tsconfig.package.json +25 -0
  70. package/index.js +0 -0
  71. package/jest.config.js +0 -16
  72. package/yarn-error.log +0 -2374
package/dist/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- import { Payment, PaymentIntent, VerificationConfig, Handlers, PublicEvent } from "./shared/types";
2
- export { type Payment, type PaymentIntent, type VerificationConfig, type Handlers, PublicEvent, };
1
+ import "ts-polyfill";
2
+ import "custom-event-polyfill";
3
+ import { type PaymentIntent, type Payment, PublicEvent, type PaymentConfig, type Handlers } from "./shared/types";
4
+ export type { PaymentIntent, Payment, PaymentConfig, Handlers };
5
+ export { PublicEvent };
package/dist/index.js CHANGED
@@ -1,2 +1,8 @@
1
+ import "ts-polyfill";
2
+ import "custom-event-polyfill";
3
+ import { FatZebra } from "./main";
1
4
  import { PublicEvent } from "./shared/types";
2
- export { PublicEvent, };
5
+ export { PublicEvent };
6
+ if (typeof window !== "undefined") {
7
+ window.FatZebra = FatZebra;
8
+ }
package/dist/main.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { Customer, PaymentIntent, PaymentMethod, PublicEvent } from './shared/types';
2
+ import { HppLoadParams } from './hpp';
3
+ import { PayPalConfig } from './paypal/types';
4
+ import { ChallengeWindowSize } from './sca/types';
5
+ export interface FZConfig {
6
+ username: string;
7
+ test?: boolean;
8
+ }
9
+ interface Card {
10
+ number: string;
11
+ holder: string;
12
+ expiryMonth: string;
13
+ expiryYear: string;
14
+ cvv: string;
15
+ }
16
+ interface VerifyCardParams {
17
+ customer: Customer;
18
+ paymentIntent: PaymentIntent;
19
+ paymentMethod: PaymentMethod;
20
+ options?: VerifyCardOptions;
21
+ }
22
+ interface VerifyCardOptions {
23
+ challengeWindowSize?: ChallengeWindowSize;
24
+ }
25
+ export default class FatZebra {
26
+ private sca;
27
+ private fzConfig;
28
+ private gatewayClient;
29
+ constructor(config: FZConfig);
30
+ tokenizeCard(card: Card, headless: HTMLIFrameElement): void;
31
+ cardDidTokenize(headless: HTMLIFrameElement, callback: (data: any) => void): void;
32
+ verifyCard(params: VerifyCardParams): Promise<void>;
33
+ renderPaymentsPage(params: HppLoadParams): void;
34
+ checkout(): void;
35
+ setupPayPal(config: PayPalConfig): void;
36
+ on(event: PublicEvent, callback: (e: any) => void): void;
37
+ }
38
+ export { type HppLoadParams, FatZebra, };
package/dist/main.js ADDED
@@ -0,0 +1,148 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import Sca from './sca';
11
+ import { PostMessageClient, } from './shared/post-message-client';
12
+ import { BridgeEvent, PaymentMethodType, PublicEvent, } from './shared/types';
13
+ import { LocalStorageAccessTokenKey } from './shared/constants';
14
+ import { emit, on } from './shared/event-manager';
15
+ import * as bridge from './shared/bridge-client';
16
+ import { validateHppLoadParams, validateVerifyCardParams, toHumanizedErrors, } from './validation';
17
+ import GatewayClient from './shared/api-gateway-client';
18
+ import { Hpp } from './hpp';
19
+ import { PayPalCheckout } from './paypal/paypal-checkout';
20
+ export default class FatZebra {
21
+ constructor(config) {
22
+ this.fzConfig = config;
23
+ window.MerchantUsername = config.username;
24
+ this.gatewayClient = new GatewayClient({
25
+ accessToken: window.localStorage.getItem(LocalStorageAccessTokenKey),
26
+ username: config.username,
27
+ });
28
+ this.sca = new Sca({
29
+ gatewayClient: this.gatewayClient,
30
+ });
31
+ this.sca.loadScript();
32
+ }
33
+ tokenizeCard(card, headless) {
34
+ const channel = 'sca';
35
+ headless.onload = () => {
36
+ const message = {
37
+ channel,
38
+ subject: BridgeEvent.TOKENIZE_CARD_REQUEST,
39
+ data: {
40
+ access_token: window.localStorage.getItem(LocalStorageAccessTokenKey),
41
+ card_holder: card.holder,
42
+ card_number: card.number,
43
+ card_expiry: `${card.expiryMonth}/${card.expiryYear}`,
44
+ cvv: card.cvv,
45
+ }
46
+ };
47
+ headless.contentWindow.postMessage(message, '*');
48
+ };
49
+ }
50
+ cardDidTokenize(headless, callback) {
51
+ const channel = 'sca';
52
+ const postMessageClient = new PostMessageClient({
53
+ channel,
54
+ target: headless
55
+ });
56
+ const handlers = {};
57
+ handlers[BridgeEvent.TOKENIZE_CARD_RESPONSE] = (data) => {
58
+ callback(data);
59
+ };
60
+ postMessageClient.setEventListeners(handlers);
61
+ }
62
+ verifyCard(params) {
63
+ var _a;
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ const valid = validateVerifyCardParams(params);
66
+ if (!valid) {
67
+ emit(PublicEvent.VALIDATION_ERROR, {
68
+ errors: toHumanizedErrors(validateVerifyCardParams.errors),
69
+ data: null
70
+ });
71
+ return;
72
+ }
73
+ switch (params.paymentMethod.type) {
74
+ case PaymentMethodType.CARD:
75
+ const headless = bridge.load(process.env.PAYNOW_BRIDGE_URL);
76
+ const card = params.paymentMethod.data;
77
+ this.cardDidTokenize(headless, (data) => __awaiter(this, void 0, void 0, function* () {
78
+ var _b;
79
+ const bin = card.number.substr(0, 6);
80
+ this.sca.run({
81
+ cardToken: data.token,
82
+ customer: params.customer,
83
+ paymentIntent: params.paymentIntent,
84
+ bin,
85
+ challengeWindowSize: (_b = params.options) === null || _b === void 0 ? void 0 : _b.challengeWindowSize,
86
+ });
87
+ }));
88
+ this.tokenizeCard(card, headless);
89
+ break;
90
+ case PaymentMethodType.CARD_ON_FILE:
91
+ const cardToken = params.paymentMethod.data.token;
92
+ const bin = (yield this.gatewayClient.getCard({ card_token: cardToken })).data.bin;
93
+ this.sca.run({
94
+ cardToken,
95
+ customer: params.customer,
96
+ paymentIntent: params.paymentIntent,
97
+ bin,
98
+ challengeWindowSize: (_a = params.options) === null || _a === void 0 ? void 0 : _a.challengeWindowSize,
99
+ });
100
+ break;
101
+ }
102
+ });
103
+ }
104
+ renderPaymentsPage(params) {
105
+ const valid = validateHppLoadParams(params);
106
+ if (!valid) {
107
+ emit(PublicEvent.VALIDATION_ERROR, {
108
+ errors: toHumanizedErrors(validateHppLoadParams.errors),
109
+ data: null
110
+ });
111
+ return;
112
+ }
113
+ window.HPP = new Hpp({
114
+ version: params.version,
115
+ paymentIntent: params.paymentIntent,
116
+ customer: params.customer,
117
+ username: this.fzConfig.username,
118
+ sca: this.sca,
119
+ test: this.fzConfig.test,
120
+ });
121
+ window.HPP.load(params);
122
+ }
123
+ checkout() {
124
+ window.HPP.purchase();
125
+ }
126
+ setupPayPal(config) {
127
+ const paypalCheckout = new PayPalCheckout({
128
+ currency: config.currency,
129
+ payment: config.payment,
130
+ containerId: config.containerId,
131
+ style: config.style
132
+ });
133
+ paypalCheckout.load()
134
+ .then(function () {
135
+ paypalCheckout.render();
136
+ })
137
+ .catch(function (error) {
138
+ emit(PublicEvent.PAYPAL_ERROR, {
139
+ errors: [error].flat(),
140
+ data: null
141
+ });
142
+ });
143
+ }
144
+ on(event, callback) {
145
+ on(event, callback);
146
+ }
147
+ }
148
+ export { FatZebra, };
@@ -0,0 +1,31 @@
1
+ import { PayPalPaymentMethod } from './types';
2
+ import { PaymentIntent } from '../shared/types';
3
+ interface PayPal {
4
+ render(containerId: string): void;
5
+ }
6
+ interface PayPalButtonStyle {
7
+ [key: string]: any;
8
+ }
9
+ export default class PayPalButton {
10
+ private validationErrorRaised;
11
+ private _paypalButtons;
12
+ private paymentMethod;
13
+ private paymentIntent;
14
+ private buttonStyle;
15
+ private billingAgreement;
16
+ private client;
17
+ private actions;
18
+ constructor(paymentMethod?: PayPalPaymentMethod, paymentIntent?: PaymentIntent, billingAgreement?: boolean, buttonStyle?: PayPalButtonStyle);
19
+ get paypalButtons(): any;
20
+ get paypalActions(): any;
21
+ render(containerId: string): void;
22
+ getContainer(containerId: string): any;
23
+ private createButton;
24
+ private createOrder;
25
+ private approveOrder;
26
+ private createBillingAgreement;
27
+ private approveBillingAgreement;
28
+ private formatError;
29
+ }
30
+ export { PayPalButton };
31
+ export type { PayPal, PayPalButtonStyle, };
@@ -0,0 +1,199 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { emit } from '../shared/event-manager';
11
+ import { PayPalIntent } from './types';
12
+ import GatewayClient from '../shared/api-gateway-client';
13
+ import { PublicEvent } from '../shared/types';
14
+ import * as util from '../shared/util';
15
+ import { LocalStorageAccessTokenKey } from '../shared/constants';
16
+ const defaultStyle = {
17
+ layout: 'horizontal',
18
+ color: 'gold',
19
+ shape: 'pill',
20
+ label: 'paypal',
21
+ };
22
+ export default class PayPalButton {
23
+ constructor(paymentMethod, paymentIntent, billingAgreement, buttonStyle) {
24
+ this.paymentMethod = paymentMethod;
25
+ this.paymentIntent = paymentIntent;
26
+ this.billingAgreement = billingAgreement;
27
+ this.buttonStyle = buttonStyle;
28
+ this.client = new GatewayClient({ accessToken: window.localStorage.getItem(LocalStorageAccessTokenKey),
29
+ username: window.MerchantUsername,
30
+ });
31
+ this.createButton();
32
+ }
33
+ get paypalButtons() {
34
+ return this._paypalButtons;
35
+ }
36
+ get paypalActions() {
37
+ return this.actions;
38
+ }
39
+ render(containerId) {
40
+ let container = this.getContainer(containerId);
41
+ if (typeof (container) != 'undefined' && container != null) {
42
+ this._paypalButtons.render('#' + container.id);
43
+ }
44
+ else {
45
+ emit(PublicEvent.PAYPAL_ERROR, {
46
+ errors: [`Container with id #${containerId} does not exist.`],
47
+ data: null
48
+ });
49
+ }
50
+ }
51
+ getContainer(containerId) {
52
+ return document.getElementById(containerId);
53
+ }
54
+ createButton() {
55
+ let button = ({});
56
+ button.style = (this.buttonStyle ? this.buttonStyle : defaultStyle);
57
+ if (this.billingAgreement === true) {
58
+ button.createBillingAgreement = () => __awaiter(this, void 0, void 0, function* () {
59
+ return this.createBillingAgreement();
60
+ });
61
+ button.onApprove = (data, actions) => __awaiter(this, void 0, void 0, function* () {
62
+ emit(PublicEvent.PAYPAL_PROCESSESING, { data: {}, message: 'The request is being processed.' });
63
+ return this.approveBillingAgreement(data);
64
+ });
65
+ button.onCancel = (data, actions) => {
66
+ emit(PublicEvent.PAYPAL_CANCEL, { data: { id: data.orderID }, message: 'PayPal Billing Agreement cancelled.' });
67
+ };
68
+ }
69
+ else {
70
+ button.createOrder = () => __awaiter(this, void 0, void 0, function* () {
71
+ return this.createOrder();
72
+ });
73
+ button.onApprove = (data, actions) => __awaiter(this, void 0, void 0, function* () {
74
+ emit(PublicEvent.PAYPAL_PROCESSESING, { data: {}, message: 'The request is being processed.' });
75
+ return this.approveOrder(data);
76
+ });
77
+ button.onCancel = (data, actions) => {
78
+ emit(PublicEvent.PAYPAL_CANCEL, { data: { id: data.orderID }, message: 'PayPal Order cancelled.' });
79
+ };
80
+ }
81
+ button.onError = (error) => {
82
+ // This flag is used to prevent emitting general error from PayPal
83
+ // if an error has already been raised and emitted.
84
+ if (this.validationErrorRaised === false) {
85
+ emit(PublicEvent.PAYPAL_ERROR, {
86
+ errors: ['PayPal Checkout failed.'],
87
+ data: null
88
+ });
89
+ }
90
+ };
91
+ this._paypalButtons = window.paypal.Buttons(button);
92
+ }
93
+ createOrder() {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ try {
96
+ this.validationErrorRaised = false;
97
+ let payload = util.toObjectWithSnakeCaseKeys(this.paymentMethod);
98
+ payload.payment_intent = this.paymentIntent;
99
+ const response = (yield this.client.createPayPalOrder({
100
+ order: payload
101
+ })).data;
102
+ return response.id;
103
+ }
104
+ catch (error) {
105
+ this.validationErrorRaised = true;
106
+ emit(PublicEvent.PAYPAL_ERROR, {
107
+ errors: this.formatError(error),
108
+ data: null
109
+ });
110
+ return null;
111
+ }
112
+ });
113
+ }
114
+ approveOrder(data) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ try {
117
+ this.validationErrorRaised = false;
118
+ let response = undefined;
119
+ if (this.paymentMethod.intent.toUpperCase() === PayPalIntent.CAPTURE) {
120
+ response = (yield this.client.capturePayPalOrder({
121
+ id: data.orderID
122
+ })).data;
123
+ }
124
+ else if (this.paymentMethod.intent.toUpperCase() === PayPalIntent.AUTHORIZE) {
125
+ response = (yield this.client.authorizePayPalOrder({
126
+ id: data.orderID
127
+ })).data;
128
+ }
129
+ emit(PublicEvent.PAYPAL_SUCCESS, {
130
+ data: util.toObjectWithCamelCaseKeys(response),
131
+ message: 'PayPal order approved.'
132
+ });
133
+ return;
134
+ }
135
+ catch (error) {
136
+ this.validationErrorRaised = true;
137
+ emit(PublicEvent.PAYPAL_ERROR, {
138
+ errors: this.formatError(error),
139
+ data: null
140
+ });
141
+ return;
142
+ }
143
+ });
144
+ }
145
+ createBillingAgreement() {
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ try {
148
+ this.validationErrorRaised = false;
149
+ let payload = util.toObjectWithSnakeCaseKeys(this.paymentMethod);
150
+ delete payload.billing_agreement;
151
+ let response = (yield this.client.createPayPalBillingAgreement({
152
+ billing: payload
153
+ })).data;
154
+ return response.token_id;
155
+ }
156
+ catch (error) {
157
+ this.validationErrorRaised = true;
158
+ emit(PublicEvent.PAYPAL_ERROR, {
159
+ errors: this.formatError(error),
160
+ data: null
161
+ });
162
+ return;
163
+ }
164
+ });
165
+ }
166
+ approveBillingAgreement(data) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ try {
169
+ this.validationErrorRaised = false;
170
+ let response = (yield this.client.approvePayPalBillingAgreement({
171
+ id: data.billingToken
172
+ })).data;
173
+ emit(PublicEvent.PAYPAL_SUCCESS, {
174
+ data: util.toObjectWithCamelCaseKeys(response),
175
+ message: 'PayPal Billing Agreement created.'
176
+ });
177
+ return;
178
+ }
179
+ catch (error) {
180
+ this.validationErrorRaised = true;
181
+ emit(PublicEvent.PAYPAL_ERROR, {
182
+ errors: this.formatError(error),
183
+ data: null
184
+ });
185
+ return;
186
+ }
187
+ });
188
+ }
189
+ formatError(error) {
190
+ var _a, _b;
191
+ var message = [(_b = (_a = error === null || error === void 0 ? void 0 : error.request) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.errors].flat();
192
+ if (typeof message[0] === 'undefined')
193
+ message = [error === null || error === void 0 ? void 0 : error.message].flat();
194
+ if (typeof message[0] === 'undefined')
195
+ message = ['Unknown error'];
196
+ return message;
197
+ }
198
+ }
199
+ export { PayPalButton };
@@ -0,0 +1,21 @@
1
+ import { PayPalButton } from './paypal-button';
2
+ import { PayPalConfig } from './types';
3
+ export default class PayPalCheckout {
4
+ private paypalButton;
5
+ private validationErrors;
6
+ private intent;
7
+ private currency;
8
+ private payment;
9
+ private billingAgreement;
10
+ private containerId;
11
+ private buttonStyle;
12
+ constructor(config: PayPalConfig);
13
+ get paypal(): PayPalButton;
14
+ set paypal(paypal: PayPalButton);
15
+ load(): Promise<any>;
16
+ render(): void;
17
+ private validatePayPalSetup;
18
+ private validatePayPalRequest;
19
+ private errors;
20
+ }
21
+ export { PayPalCheckout };
@@ -0,0 +1,100 @@
1
+ // This is the implementation of PayPal Checkout
2
+ //
3
+ // https://developer.paypal.com/docs/checkout/
4
+ import { emit } from '../shared/event-manager';
5
+ import { PaymentMethodType, PublicEvent } from '../shared/types';
6
+ import { PayPalButton, } from './paypal-button';
7
+ import { validatePayPalSetup, validatePayPalRequest, } from './validation';
8
+ export default class PayPalCheckout {
9
+ constructor(config) {
10
+ var _a, _b, _c, _d, _e;
11
+ if (!this.validatePayPalSetup(config)) {
12
+ emit(PublicEvent.PAYPAL_ERROR, {
13
+ errors: this.errors(),
14
+ data: null
15
+ });
16
+ return;
17
+ }
18
+ this.billingAgreement = (((_b = (_a = config === null || config === void 0 ? void 0 : config.payment) === null || _a === void 0 ? void 0 : _a.paymentMethod) === null || _b === void 0 ? void 0 : _b.type) === PaymentMethodType.PAYPAL_BILLING);
19
+ this.payment = config === null || config === void 0 ? void 0 : config.payment;
20
+ this.intent = (_e = (_d = (_c = config === null || config === void 0 ? void 0 : config.payment) === null || _c === void 0 ? void 0 : _c.paymentMethod) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.intent;
21
+ this.currency = config === null || config === void 0 ? void 0 : config.currency;
22
+ this.containerId = config === null || config === void 0 ? void 0 : config.containerId;
23
+ this.buttonStyle = config === null || config === void 0 ? void 0 : config.style;
24
+ if (!this.validatePayPalRequest()) {
25
+ emit(PublicEvent.PAYPAL_ERROR, {
26
+ errors: this.errors(),
27
+ data: null
28
+ });
29
+ return;
30
+ }
31
+ }
32
+ get paypal() {
33
+ return this.paypalButton;
34
+ }
35
+ set paypal(paypal) {
36
+ this.paypalButton = paypal;
37
+ }
38
+ load() {
39
+ if (this.errors()) {
40
+ return;
41
+ }
42
+ if (this.intent && !this.billingAgreement) {
43
+ let data = this.payment.paymentMethod.data;
44
+ data.intent = this.intent.toUpperCase();
45
+ }
46
+ let self = this;
47
+ return new Promise((resolve, reject) => {
48
+ const script = document.createElement('script');
49
+ script.type = 'text/javascript';
50
+ let source = process.env.PAYPAL_SDK_URL + '&client-id=' + process.env.PAYPAL_CLIENT_ID + '&debug=' + process.env.PAYPAL_DEBUG;
51
+ if (this.currency)
52
+ source += '&currency=' + this.currency;
53
+ if (this.intent && !this.billingAgreement)
54
+ source += '&intent=' + this.intent;
55
+ if (this.billingAgreement === true)
56
+ source += '&vault=true';
57
+ script.setAttribute('data-partner-attribution-id', process.env.PAYPAL_PARTNER_ID);
58
+ script.src = source;
59
+ script.async = true;
60
+ document.body.appendChild(script);
61
+ script.onload = () => {
62
+ self.paypalButton = new PayPalButton(self.payment.paymentMethod.data, self.payment.paymentIntent, self.billingAgreement, self.buttonStyle);
63
+ resolve(null);
64
+ };
65
+ script.onerror = () => {
66
+ reject(['Loading PayPal failed']);
67
+ };
68
+ });
69
+ }
70
+ render() {
71
+ if (this.errors()) {
72
+ return;
73
+ }
74
+ this.paypalButton.render(this.containerId);
75
+ }
76
+ validatePayPalSetup(config) {
77
+ const validations = validatePayPalSetup(config);
78
+ this.validationErrors = null;
79
+ if (!validations.valid) {
80
+ this.validationErrors = validations.errors;
81
+ return false;
82
+ }
83
+ return true;
84
+ }
85
+ validatePayPalRequest() {
86
+ const validations = validatePayPalRequest(this.payment, this.billingAgreement);
87
+ this.validationErrors = null;
88
+ if (!validations.valid) {
89
+ this.validationErrors = validations.errors;
90
+ return false;
91
+ }
92
+ return true;
93
+ }
94
+ errors() {
95
+ if (this.validationErrors !== null)
96
+ return this.validationErrors;
97
+ return null;
98
+ }
99
+ }
100
+ export { PayPalCheckout };