@tap-payments/apple-pay-button 0.0.2 → 0.0.3-staging

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 (51) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +240 -194
  3. package/build/@types/ApplePayButtonProps.d.ts +129 -0
  4. package/build/@types/ApplePayButtonProps.js +1 -0
  5. package/build/@types/charge.d.ts +57 -0
  6. package/build/@types/charge.js +1 -0
  7. package/build/@types/checkoutProfile.d.ts +216 -0
  8. package/build/@types/checkoutProfile.js +1 -0
  9. package/build/@types/enums.d.ts +65 -0
  10. package/build/@types/enums.js +76 -0
  11. package/build/@types/index.d.ts +6 -82
  12. package/build/@types/index.js +6 -1
  13. package/build/@types/tapLocalisation.d.ts +193 -0
  14. package/build/@types/tapLocalisation.js +1 -0
  15. package/build/@types/tapTheme.d.ts +842 -0
  16. package/build/@types/tapTheme.js +1 -0
  17. package/build/api.d.ts +29 -0
  18. package/build/{api/app.service.js → api.js} +244 -253
  19. package/build/constants/index.d.ts +3 -51
  20. package/build/constants/index.js +3 -51
  21. package/build/features/ApplePayButton/ApplePayButton.d.ts +4 -6
  22. package/build/features/ApplePayButton/ApplePayButton.js +49 -80
  23. package/build/features/ApplePayButton/index.d.ts +3 -3
  24. package/build/features/ApplePayButton/index.js +2 -2
  25. package/build/hooks/index.d.ts +1 -1
  26. package/build/hooks/index.js +1 -1
  27. package/build/hooks/useApplePay.d.ts +93 -9
  28. package/build/hooks/useApplePay.js +447 -243
  29. package/build/hooks/useMerchantApplePay.d.ts +20 -0
  30. package/build/hooks/useMerchantApplePay.js +188 -0
  31. package/build/index.d.ts +4 -4
  32. package/build/index.js +13 -10
  33. package/build/utils/config.d.ts +14 -11
  34. package/build/utils/config.js +65 -72
  35. package/build/utils/defaultValues.d.ts +2 -0
  36. package/build/utils/defaultValues.js +27 -0
  37. package/build/utils/html.d.ts +1 -1
  38. package/build/utils/html.js +9 -9
  39. package/build/utils/index.d.ts +4 -1
  40. package/build/utils/index.js +4 -1
  41. package/build/utils/theme.d.ts +12 -0
  42. package/build/utils/theme.js +61 -0
  43. package/package.json +109 -104
  44. package/build/api/app.service.d.ts +0 -23
  45. package/build/api/base.d.ts +0 -9
  46. package/build/api/base.js +0 -45
  47. package/build/api/httpClient.d.ts +0 -8
  48. package/build/api/httpClient.js +0 -33
  49. package/build/features/ApplePayButton/ApplePayButton.css +0 -21
  50. package/build/hooks/useScript.d.ts +0 -1
  51. package/build/hooks/useScript.js +0 -39
@@ -1,243 +1,447 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
- return new (P || (P = Promise))(function (resolve, reject) {
15
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
- step((generator = generator.apply(thisArg, _arguments || [])).next());
19
- });
20
- };
21
- var __generator = (this && this.__generator) || function (thisArg, body) {
22
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
- function verb(n) { return function (v) { return step([n, v]); }; }
25
- function step(op) {
26
- if (f) throw new TypeError("Generator is already executing.");
27
- while (_) try {
28
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
- if (y = 0, t) op = [op[0] & 2, t.value];
30
- switch (op[0]) {
31
- case 0: case 1: t = op; break;
32
- case 4: _.label++; return { value: op[1], done: false };
33
- case 5: _.label++; y = op[1]; op = [0]; continue;
34
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
- default:
36
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
- if (t[2]) _.ops.pop();
41
- _.trys.pop(); continue;
42
- }
43
- op = body.call(thisArg, _);
44
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
- }
47
- };
48
- import { useCallback, useEffect, useState } from 'react';
49
- import appService from '../api/app.service';
50
- import { ApplePayVersion } from '../constants';
51
- import { setAxiosGlobalHeaders } from '../api/httpClient';
52
- import { getApplePayPaymentMethod, getApplePayRequest, validateCurrency, validateSupportedNetworks } from '../utils/config';
53
- export var useApplePay = function (_a) {
54
- var publicKey = _a.publicKey, merchant = _a.merchant, transaction = _a.transaction, billingContact = _a.billingContact, onCancel = _a.onCancel, onError = _a.onError, onSuccess = _a.onSuccess, scope = _a.scope, supportedNetworks = _a.supportedNetworks, metaData = _a.metaData, debug = _a.debug, merchantIdentifier = _a.merchantIdentifier, environment = _a.environment, locale = _a.locale, onReady = _a.onReady;
55
- var _b = useState(false), loading = _b[0], setLoading = _b[1];
56
- var _c = useState(null), profileData = _c[0], setProfile = _c[1];
57
- var _d = useState(false), disabled = _d[0], setDisabled = _d[1];
58
- var initialize = useCallback(function (metaData) { return __awaiter(void 0, void 0, void 0, function () {
59
- var merchantProfile, payment_options, headers, data, err_1;
60
- return __generator(this, function (_a) {
61
- switch (_a.label) {
62
- case 0:
63
- setLoading(true);
64
- appService.setEnv(environment);
65
- appService.setBaseUrl();
66
- _a.label = 1;
67
- case 1:
68
- _a.trys.push([1, 4, 5, 6]);
69
- if (metaData) {
70
- merchantProfile = metaData.merchant, payment_options = metaData.payment_options, headers = metaData.headers;
71
- setAxiosGlobalHeaders(__assign({}, headers));
72
- setProfile({ merchant: merchantProfile, payment_options: payment_options });
73
- return [2];
74
- }
75
- return [4, appService.setBrowserHeaders({ locale: locale, domain: merchant.domain, pk: publicKey })];
76
- case 2:
77
- _a.sent();
78
- return [4, appService.checkoutProfile()];
79
- case 3:
80
- data = _a.sent();
81
- if (debug)
82
- console.log('merchant configuration: ', data);
83
- setProfile(data);
84
- return [3, 6];
85
- case 4:
86
- err_1 = _a.sent();
87
- onError && onError(err_1.errors || err_1);
88
- return [3, 6];
89
- case 5:
90
- setLoading(false);
91
- onReady && onReady();
92
- return [7];
93
- case 6: return [2];
94
- }
95
- });
96
- }); }, [environment, locale, merchant.domain, publicKey, debug, onError]);
97
- useEffect(function () {
98
- initialize(metaData);
99
- }, [initialize, metaData]);
100
- var onApplePayButtonClicked = function () { return __awaiter(void 0, void 0, void 0, function () {
101
- var ApplePaySession, paymentMethod, currency, cardBrands, request, session_1;
102
- var _a;
103
- return __generator(this, function (_b) {
104
- ApplePaySession = window.ApplePaySession;
105
- if (!ApplePaySession) {
106
- setDisabled(true);
107
- console.error('ApplePaySession is not available, please check if you are using Safari browser');
108
- return [2];
109
- }
110
- if (!profileData) {
111
- setDisabled(true);
112
- console.error('The profile data is not available, please check networks and try again');
113
- return [2];
114
- }
115
- try {
116
- paymentMethod = getApplePayPaymentMethod(profileData.payment_options.payment_methods || []);
117
- currency = validateCurrency(transaction.currency, paymentMethod.supported_currencies);
118
- cardBrands = validateSupportedNetworks(paymentMethod.supported_card_brands, supportedNetworks);
119
- request = getApplePayRequest({
120
- countryCode: profileData.merchant.country_code,
121
- transaction: __assign(__assign({}, transaction), { currency: currency }),
122
- billingContact: billingContact,
123
- supportedNetworks: cardBrands,
124
- name: profileData.merchant.name
125
- });
126
- if (debug) {
127
- console.info("Creating ApplePaySession with version: ".concat(ApplePayVersion, " and request: ").concat(JSON.stringify(request)));
128
- }
129
- session_1 = new ApplePaySession(ApplePayVersion, request);
130
- session_1.onvalidatemerchant = function (event) { return __awaiter(void 0, void 0, void 0, function () {
131
- var merchantSession, error_1;
132
- return __generator(this, function (_a) {
133
- switch (_a.label) {
134
- case 0:
135
- if (debug) {
136
- console.info('Creating merchant session and validating merchant session');
137
- console.info('onvalidatemerchant event', event);
138
- }
139
- setLoading(true);
140
- _a.label = 1;
141
- case 1:
142
- _a.trys.push([1, 3, , 4]);
143
- if (debug) {
144
- console.info("creating merchant session for merchantData: ".concat(JSON.stringify(profileData.merchant), " and validationURL: ").concat(event.validationURL, " and merchant.domain: ").concat(merchant.domain));
145
- }
146
- return [4, appService.appleSession(profileData.merchant, event.validationURL, merchant.domain, merchantIdentifier)];
147
- case 2:
148
- merchantSession = _a.sent();
149
- if (debug)
150
- console.info('merchantSession', merchantSession);
151
- session_1.completeMerchantValidation(merchantSession);
152
- console.info('merchantSession completed successfully!');
153
- return [3, 4];
154
- case 3:
155
- error_1 = _a.sent();
156
- if (debug)
157
- console.error('error in onvalidatemerchant', error_1);
158
- session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
159
- throw error_1;
160
- case 4:
161
- setLoading(false);
162
- return [2];
163
- }
164
- });
165
- }); };
166
- session_1.onpaymentauthorized = function (event) { return __awaiter(void 0, void 0, void 0, function () {
167
- var _a, data, error_2;
168
- return __generator(this, function (_b) {
169
- switch (_b.label) {
170
- case 0:
171
- setLoading(true);
172
- if (debug)
173
- console.info('onpaymentauthorized event', event);
174
- _b.label = 1;
175
- case 1:
176
- _b.trys.push([1, 9, 10, 11]);
177
- _a = scope;
178
- switch (_a) {
179
- case 'AppleToken': return [3, 2];
180
- case 'TapToken': return [3, 4];
181
- }
182
- return [3, 7];
183
- case 2: return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event.payment.token.paymentData))];
184
- case 3:
185
- _b.sent();
186
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
187
- return [3, 8];
188
- case 4: return [4, appService.tapTokenization(event.payment.token.paymentData)];
189
- case 5:
190
- data = _b.sent();
191
- return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data))];
192
- case 6:
193
- _b.sent();
194
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
195
- return [3, 8];
196
- case 7:
197
- console.error('We only support AppleToken and TapToken for now');
198
- console.info('Completing payment with status: STATUS_FAILURE');
199
- session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
200
- _b.label = 8;
201
- case 8: return [3, 11];
202
- case 9:
203
- error_2 = _b.sent();
204
- if (debug)
205
- console.error('error in onpaymentauthorized', error_2);
206
- session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
207
- throw error_2;
208
- case 10:
209
- setLoading(false);
210
- return [7];
211
- case 11: return [2];
212
- }
213
- });
214
- }); };
215
- session_1.oncancel = function (event) {
216
- console.info('oncancel event', event);
217
- if (onCancel)
218
- onCancel();
219
- };
220
- session_1.begin();
221
- }
222
- catch (e) {
223
- if (!onError)
224
- return [2];
225
- if (e.errors) {
226
- onError(e.errors);
227
- return [2];
228
- }
229
- if ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data.errors) {
230
- onError(e.response.data.errors);
231
- return [2];
232
- }
233
- if (e.message) {
234
- onError([{ description: e.message, code: '400' }]);
235
- return [2];
236
- }
237
- onError(e);
238
- }
239
- return [2];
240
- });
241
- }); };
242
- return { loading: loading, onApplePayButtonClicked: onApplePayButtonClicked, disabled: !profileData || disabled };
243
- };
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ var __rest = (this && this.__rest) || function (s, e) {
49
+ var t = {};
50
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
51
+ t[p] = s[p];
52
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
53
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
54
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
55
+ t[p[i]] = s[p[i]];
56
+ }
57
+ return t;
58
+ };
59
+ import { useCallback, useEffect, useMemo, useState } from 'react';
60
+ import { ApplePayVersion, MW_BASE_URL } from '../constants';
61
+ import { prepareApplePayRequest, shallRenderApplePay } from '../utils/config';
62
+ import { Scope } from '../@types';
63
+ import { useMerchantApplePay } from './useMerchantApplePay';
64
+ import { getApplePayPaymentMethod, getDefaultThemeMode } from '../utils';
65
+ import appService, { getAxiosGlobalHeaders, setAxiosGlobalHeaders } from '../api';
66
+ import { ChargeCode, mappingInterface } from '@tap-payments/acceptance-sdk';
67
+ export var useApplePay = function (_a) {
68
+ var _b;
69
+ var buttonProps = _a.buttonProps;
70
+ var _c = useState(), applePayRequestData = _c[0], setApplePayRequestData = _c[1];
71
+ var _d = useState(), applePayRequestConfiguration = _d[0], setApplePayRequestConfiguration = _d[1];
72
+ var _e = useState(), profileData = _e[0], setProfile = _e[1];
73
+ var _f = useState(false), disabled = _f[0], setDisabled = _f[1];
74
+ var _g = useState(false), initialLoading = _g[0], setInitialLoading = _g[1];
75
+ var _h = useState(false), error = _h[0], setError = _h[1];
76
+ var _j = useState(false), success = _j[0], setSuccess = _j[1];
77
+ var _k = useState(false), loading = _k[0], setLoading = _k[1];
78
+ var _l = useState(false), isRounded = _l[0], setIsRounded = _l[1];
79
+ var mappedProps = __assign(__assign({}, buttonProps), { interface: mappingInterface(buttonProps.interface) });
80
+ var interfaceObj = mappedProps.interface, merchant = mappedProps.merchant, customer = mappedProps.customer, acceptance = mappedProps.acceptance, scope = mappedProps.scope, debug = mappedProps.debug, onError = mappedProps.onError, onSuccess = mappedProps.onSuccess, onCancel = mappedProps.onCancel, onReady = mappedProps.onReady, metaData = mappedProps.metaData, headers = mappedProps.headers, operator = mappedProps.operator, order = mappedProps.order, onOrderCreated = mappedProps.onOrderCreated, transaction = mappedProps.transaction, post = mappedProps.post, redirect = mappedProps.redirect;
81
+ var isIframeIntegration = buttonProps.integration === 'iframe';
82
+ useMerchantApplePay({
83
+ isIframeIntegration: isIframeIntegration,
84
+ onError: onError,
85
+ onSuccess: onSuccess,
86
+ scope: scope,
87
+ profileData: profileData,
88
+ merchant: merchant,
89
+ order: order,
90
+ customer: customer,
91
+ transaction: transaction,
92
+ operator: operator,
93
+ post: post,
94
+ redirect: redirect,
95
+ setError: setError,
96
+ setSuccess: setSuccess,
97
+ setLoading: setLoading
98
+ });
99
+ var theme = getDefaultThemeMode((_b = mappedProps.interface) === null || _b === void 0 ? void 0 : _b.theme);
100
+ var applePayPaymentMethodOptions = useMemo(function () {
101
+ if (!profileData)
102
+ return null;
103
+ return getApplePayPaymentMethod(profileData.payment_options.payment_methods || []);
104
+ }, [profileData]);
105
+ var initialize = function (metaData) { return __awaiter(void 0, void 0, void 0, function () {
106
+ var headers_1, rest, applePayPreparedData_1, data, applePayPreparedData, err_1;
107
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
108
+ return __generator(this, function (_o) {
109
+ switch (_o.label) {
110
+ case 0:
111
+ setInitialLoading(true);
112
+ _o.label = 1;
113
+ case 1:
114
+ _o.trys.push([1, 7, , 8]);
115
+ if (!shallRenderApplePay()) {
116
+ throw new Error('The device/customer is not eligible to use ApplePay');
117
+ }
118
+ if (metaData) {
119
+ headers_1 = metaData.headers, rest = __rest(metaData, ["headers"]);
120
+ setAxiosGlobalHeaders(__assign({}, headers_1));
121
+ setProfile(rest);
122
+ applePayPreparedData_1 = prepareApplePayRequest(__assign(__assign({}, rest), { order: order, acceptance: acceptance, customer: customer }));
123
+ setApplePayRequestData(applePayPreparedData_1.applePaySessionRequest);
124
+ setInitialLoading(false);
125
+ return [2];
126
+ }
127
+ if (!headers) return [3, 3];
128
+ return [4, appService.setHttpHeaders(__assign(__assign({}, headers), { authorization: operator.publicKey }))];
129
+ case 2:
130
+ _o.sent();
131
+ return [3, 5];
132
+ case 3: return [4, appService.setBrowserHeaders({
133
+ locale: interfaceObj === null || interfaceObj === void 0 ? void 0 : interfaceObj.locale,
134
+ domain: window.location.origin,
135
+ pk: operator.publicKey
136
+ })];
137
+ case 4:
138
+ _o.sent();
139
+ _o.label = 5;
140
+ case 5: return [4, appService.checkoutProfile({
141
+ supported_payment_methods: ['APPLE_PAY'],
142
+ supported_currencies: [order.currency],
143
+ transaction_mode: scope === Scope.AUTHORIZE ? 'AUTHORIZE_CAPTURE' : 'PURCHASE',
144
+ currency: order.currency,
145
+ merchant_id: merchant === null || merchant === void 0 ? void 0 : merchant.id,
146
+ total_amount: Number(order.amount),
147
+ order: __assign(__assign({ id: order === null || order === void 0 ? void 0 : order.id, amount: Number(order.amount), currency: order.currency, description: order === null || order === void 0 ? void 0 : order.description, metadata: order === null || order === void 0 ? void 0 : order.metadata }, (customer && {
148
+ customer: {
149
+ id: customer === null || customer === void 0 ? void 0 : customer.id,
150
+ email: (_a = customer.contact) === null || _a === void 0 ? void 0 : _a.email,
151
+ first_name: ((_b = customer.name) === null || _b === void 0 ? void 0 : _b.length) ? customer.name[0].first : '',
152
+ last_name: ((_c = customer.name) === null || _c === void 0 ? void 0 : _c.length) ? customer.name[0].last : '',
153
+ phone: {
154
+ country_code: (_f = (_e = (_d = customer.contact) === null || _d === void 0 ? void 0 : _d.phone) === null || _e === void 0 ? void 0 : _e.countryCode) !== null && _f !== void 0 ? _f : '',
155
+ number: (_j = (_h = (_g = customer.contact) === null || _g === void 0 ? void 0 : _g.phone) === null || _h === void 0 ? void 0 : _h.number) !== null && _j !== void 0 ? _j : ''
156
+ }
157
+ }
158
+ })), { merchant: {
159
+ id: merchant === null || merchant === void 0 ? void 0 : merchant.id
160
+ }, items: [
161
+ {
162
+ amount: Number(order.amount),
163
+ currency: order.currency,
164
+ name: "apple pay sdk button order item",
165
+ quantity: 1,
166
+ description: "apple pay sdk button order item description"
167
+ }
168
+ ] })
169
+ })];
170
+ case 6:
171
+ data = _o.sent();
172
+ if (debug)
173
+ console.log('merchant configuration: ', data);
174
+ setProfile(data);
175
+ applePayPreparedData = prepareApplePayRequest(__assign(__assign({}, data), { order: order, acceptance: acceptance, customer: customer }));
176
+ setApplePayRequestData(applePayPreparedData.applePaySessionRequest);
177
+ onOrderCreated === null || onOrderCreated === void 0 ? void 0 : onOrderCreated((_l = (_k = data.payment_options) === null || _k === void 0 ? void 0 : _k.order) === null || _l === void 0 ? void 0 : _l.id);
178
+ setApplePayRequestConfiguration({
179
+ BASE_URL: MW_BASE_URL,
180
+ headers: getAxiosGlobalHeaders(),
181
+ merchant: __assign(__assign({}, data.merchant), { id: merchant.id || ((_m = data.merchant) === null || _m === void 0 ? void 0 : _m.id) })
182
+ });
183
+ setInitialLoading(false);
184
+ onReady === null || onReady === void 0 ? void 0 : onReady();
185
+ return [3, 8];
186
+ case 7:
187
+ err_1 = _o.sent();
188
+ onError === null || onError === void 0 ? void 0 : onError(err_1.errors || err_1);
189
+ console.log('error in initialize', err_1);
190
+ return [3, 8];
191
+ case 8: return [2];
192
+ }
193
+ });
194
+ }); };
195
+ var onApplePayButtonClicked = function () { return __awaiter(void 0, void 0, void 0, function () {
196
+ var ApplePaySession, session_1;
197
+ var _a;
198
+ return __generator(this, function (_b) {
199
+ ApplePaySession = window.ApplePaySession;
200
+ if (!ApplePaySession) {
201
+ setDisabled(true);
202
+ console.error('ApplePaySession is not available, please check if you are using Safari browser');
203
+ return [2];
204
+ }
205
+ if (!profileData) {
206
+ setDisabled(true);
207
+ console.error('The profile data is not available, please check the network and try again');
208
+ return [2];
209
+ }
210
+ if (!applePayRequestData) {
211
+ setDisabled(true);
212
+ console.error("We can't find the apple pay request data, please check the network and try again");
213
+ return [2];
214
+ }
215
+ try {
216
+ if (debug) {
217
+ console.info("Creating ApplePaySession with version: ".concat(ApplePayVersion, " and request: ").concat(JSON.stringify(applePayRequestData)));
218
+ }
219
+ session_1 = new ApplePaySession(ApplePayVersion, applePayRequestData);
220
+ session_1.onvalidatemerchant = function (event) { return __awaiter(void 0, void 0, void 0, function () {
221
+ var merchantSession, error_1;
222
+ return __generator(this, function (_a) {
223
+ switch (_a.label) {
224
+ case 0:
225
+ if (debug) {
226
+ console.info('Creating merchant session and validating merchant session');
227
+ console.info('onvalidatemerchant event', event);
228
+ }
229
+ setDisabled(true);
230
+ _a.label = 1;
231
+ case 1:
232
+ _a.trys.push([1, 3, , 4]);
233
+ if (debug) {
234
+ console.info("creating merchant session for merchantData: ".concat(JSON.stringify(profileData.merchant), " and validationURL: ").concat(event.validationURL, " and merchant.domain: ").concat(window.location.hostname));
235
+ }
236
+ return [4, appService.appleSession(__assign(__assign({}, profileData.merchant), { id: merchant.id || profileData.merchant.id }), event.validationURL, window.location.hostname, merchant.identifier || merchant.id || profileData.merchant.id)];
237
+ case 2:
238
+ merchantSession = _a.sent();
239
+ if (debug)
240
+ console.info('merchantSession', merchantSession);
241
+ session_1.completeMerchantValidation(merchantSession);
242
+ console.info('merchantSession completed successfully!');
243
+ return [3, 4];
244
+ case 3:
245
+ error_1 = _a.sent();
246
+ if (debug)
247
+ console.error('error in onvalidatemerchant', error_1);
248
+ session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
249
+ throw error_1;
250
+ case 4:
251
+ setDisabled(false);
252
+ return [2];
253
+ }
254
+ });
255
+ }); };
256
+ session_1.onpaymentauthorized = function (event) { return __awaiter(void 0, void 0, void 0, function () {
257
+ var _a, paymentData, paymentMethod, transactionIdentifier, _b, data, tokenId, supportCurrency, transactionRequest, transactionResponse, _c, successState, error_2;
258
+ var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
259
+ return __generator(this, function (_1) {
260
+ switch (_1.label) {
261
+ case 0:
262
+ if (debug)
263
+ console.info('onpaymentauthorized event', event);
264
+ _1.label = 1;
265
+ case 1:
266
+ _1.trys.push([1, 16, 17, 18]);
267
+ _a = (_d = event.payment.token) !== null && _d !== void 0 ? _d : {}, paymentData = _a.paymentData, paymentMethod = _a.paymentMethod, transactionIdentifier = _a.transactionIdentifier;
268
+ _b = scope;
269
+ switch (_b) {
270
+ case Scope.APPLE_PAY_TOKEN: return [3, 2];
271
+ case Scope.TAP_TOKEN: return [3, 4];
272
+ case Scope.AUTHORIZE: return [3, 7];
273
+ case Scope.CHARGE: return [3, 7];
274
+ }
275
+ return [3, 14];
276
+ case 2: return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event.payment.token))];
277
+ case 3:
278
+ _1.sent();
279
+ session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
280
+ return [3, 15];
281
+ case 4:
282
+ setLoading(true);
283
+ return [4, appService.tapTokenization(__assign(__assign({}, paymentData), { paymentMethod: paymentMethod, transactionIdentifier: transactionIdentifier }))];
284
+ case 5:
285
+ data = _1.sent();
286
+ setSuccess === null || setSuccess === void 0 ? void 0 : setSuccess(true);
287
+ return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data))];
288
+ case 6:
289
+ _1.sent();
290
+ session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
291
+ return [3, 15];
292
+ case 7:
293
+ setLoading(true);
294
+ return [4, appService.tapTokenization(__assign(__assign({}, paymentData), { paymentMethod: paymentMethod, transactionIdentifier: transactionIdentifier }))];
295
+ case 8:
296
+ tokenId = (_1.sent()).id;
297
+ supportCurrency = ((_f = (_e = profileData === null || profileData === void 0 ? void 0 : profileData.payment_options) === null || _e === void 0 ? void 0 : _e.supported_currencies) !== null && _f !== void 0 ? _f : [])[0];
298
+ transactionRequest = {
299
+ auto: (_g = transaction === null || transaction === void 0 ? void 0 : transaction.authorize) === null || _g === void 0 ? void 0 : _g.auto,
300
+ amount: (_k = (_j = (_h = profileData === null || profileData === void 0 ? void 0 : profileData.payment_options) === null || _h === void 0 ? void 0 : _h.order) === null || _j === void 0 ? void 0 : _j.amount) !== null && _k !== void 0 ? _k : order.amount,
301
+ currency: (_o = (_m = (_l = profileData === null || profileData === void 0 ? void 0 : profileData.payment_options) === null || _l === void 0 ? void 0 : _l.order) === null || _m === void 0 ? void 0 : _m.currency) !== null && _o !== void 0 ? _o : order.currency,
302
+ selected_amount: supportCurrency === null || supportCurrency === void 0 ? void 0 : supportCurrency.amount,
303
+ selected_currency: supportCurrency === null || supportCurrency === void 0 ? void 0 : supportCurrency.currency,
304
+ customer: (customer === null || customer === void 0 ? void 0 : customer.id)
305
+ ? { id: customer === null || customer === void 0 ? void 0 : customer.id }
306
+ : {
307
+ email: (_p = customer === null || customer === void 0 ? void 0 : customer.contact) === null || _p === void 0 ? void 0 : _p.email,
308
+ first_name: ((_q = customer === null || customer === void 0 ? void 0 : customer.name) === null || _q === void 0 ? void 0 : _q.length) ? customer.name[0].first : '',
309
+ last_name: ((_r = customer === null || customer === void 0 ? void 0 : customer.name) === null || _r === void 0 ? void 0 : _r.length) ? customer.name[0].last : '',
310
+ phone: {
311
+ country_code: (_u = (_t = (_s = customer === null || customer === void 0 ? void 0 : customer.contact) === null || _s === void 0 ? void 0 : _s.phone) === null || _t === void 0 ? void 0 : _t.countryCode) !== null && _u !== void 0 ? _u : '',
312
+ number: (_x = (_w = (_v = customer === null || customer === void 0 ? void 0 : customer.contact) === null || _v === void 0 ? void 0 : _v.phone) === null || _w === void 0 ? void 0 : _w.number) !== null && _x !== void 0 ? _x : ''
313
+ }
314
+ },
315
+ fee: 0,
316
+ merchant: {
317
+ id: (_z = (_y = profileData === null || profileData === void 0 ? void 0 : profileData.merchant) === null || _y === void 0 ? void 0 : _y.id) !== null && _z !== void 0 ? _z : ''
318
+ },
319
+ order: (_0 = profileData === null || profileData === void 0 ? void 0 : profileData.payment_options) === null || _0 === void 0 ? void 0 : _0.order,
320
+ product: 'Apple-Pay-Button',
321
+ save_card: false,
322
+ source: { id: tokenId !== null && tokenId !== void 0 ? tokenId : '' },
323
+ threeDSecure: transaction === null || transaction === void 0 ? void 0 : transaction.authentication,
324
+ hashstring: operator.hashstring,
325
+ redirect: redirect,
326
+ metadata: transaction === null || transaction === void 0 ? void 0 : transaction.metadata,
327
+ post: post,
328
+ payment_agreement: transaction === null || transaction === void 0 ? void 0 : transaction.paymentAgreement,
329
+ destinations: transaction === null || transaction === void 0 ? void 0 : transaction.destinations,
330
+ reference: {
331
+ order: order === null || order === void 0 ? void 0 : order.reference,
332
+ transaction: transaction === null || transaction === void 0 ? void 0 : transaction.reference
333
+ }
334
+ };
335
+ if (!(scope === Scope.AUTHORIZE)) return [3, 10];
336
+ return [4, appService.createAuthorize(transactionRequest)];
337
+ case 9:
338
+ _c = _1.sent();
339
+ return [3, 12];
340
+ case 10: return [4, appService.createCharge(transactionRequest)];
341
+ case 11:
342
+ _c = _1.sent();
343
+ _1.label = 12;
344
+ case 12:
345
+ transactionResponse = _c;
346
+ successState = scope === Scope.AUTHORIZE ? ChargeCode.AUTHORIZED : ChargeCode.SUCCESS;
347
+ if ((transactionResponse === null || transactionResponse === void 0 ? void 0 : transactionResponse.response.code) === successState) {
348
+ setSuccess === null || setSuccess === void 0 ? void 0 : setSuccess(true);
349
+ }
350
+ else {
351
+ setError === null || setError === void 0 ? void 0 : setError(true);
352
+ }
353
+ return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(transactionResponse))];
354
+ case 13:
355
+ _1.sent();
356
+ session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
357
+ return [3, 15];
358
+ case 14:
359
+ console.error('We only support AppleToken and TapToken for now');
360
+ console.info('Completing payment with status: STATUS_FAILURE');
361
+ session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
362
+ _1.label = 15;
363
+ case 15: return [3, 18];
364
+ case 16:
365
+ error_2 = _1.sent();
366
+ if (debug)
367
+ console.error('error in onpaymentauthorized', error_2);
368
+ setError(true);
369
+ session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
370
+ throw error_2;
371
+ case 17:
372
+ setLoading(false);
373
+ return [7];
374
+ case 18: return [2];
375
+ }
376
+ });
377
+ }); };
378
+ session_1.oncancel = function (event) {
379
+ console.info('oncancel event', event);
380
+ if (onCancel)
381
+ onCancel();
382
+ };
383
+ session_1.begin();
384
+ }
385
+ catch (e) {
386
+ if (!onError)
387
+ return [2];
388
+ if (e.errors) {
389
+ onError(e.errors);
390
+ return [2];
391
+ }
392
+ if ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data.errors) {
393
+ onError(e.response.data.errors);
394
+ return [2];
395
+ }
396
+ if (e.message) {
397
+ onError([{ message: e.message, code: '400' }]);
398
+ return [2];
399
+ }
400
+ onError(e);
401
+ }
402
+ return [2];
403
+ });
404
+ }); };
405
+ var handleClick = useCallback(function () {
406
+ var _a, _b;
407
+ if (isIframeIntegration) {
408
+ (_a = buttonProps.onClick) === null || _a === void 0 ? void 0 : _a.call(buttonProps, __assign(__assign({}, applePayRequestConfiguration), { applePayRequestData: applePayRequestData }));
409
+ return;
410
+ }
411
+ onApplePayButtonClicked();
412
+ (_b = buttonProps.onClick) === null || _b === void 0 ? void 0 : _b.call(buttonProps);
413
+ }, [applePayRequestConfiguration, applePayRequestData, buttonProps, isIframeIntegration, onApplePayButtonClicked]);
414
+ useEffect(function () {
415
+ initialize(metaData);
416
+ }, []);
417
+ useEffect(function () {
418
+ setIsRounded(error || success || loading);
419
+ }, [error, success, loading]);
420
+ useEffect(function () {
421
+ if (!error)
422
+ return;
423
+ var timer = setTimeout(function () {
424
+ setError(false);
425
+ setLoading(false);
426
+ setIsRounded(false);
427
+ }, 2000);
428
+ return function () { return clearTimeout(timer); };
429
+ }, [error, loading]);
430
+ return {
431
+ onClick: handleClick,
432
+ loading: loading,
433
+ isIframeIntegration: isIframeIntegration,
434
+ onApplePayButtonClicked: onApplePayButtonClicked,
435
+ disabled: !profileData || disabled || !applePayRequestData,
436
+ applePayRequestData: applePayRequestData,
437
+ applePayRequestConfiguration: applePayRequestConfiguration,
438
+ mappedProps: mappedProps,
439
+ theme: theme,
440
+ initialLoading: initialLoading,
441
+ isRounded: isRounded,
442
+ success: success,
443
+ error: error,
444
+ applePayPaymentMethodOptions: applePayPaymentMethodOptions,
445
+ profileData: profileData
446
+ };
447
+ };