@tap-payments/apple-pay-button 0.0.82-development → 0.0.83-development

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 (43) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +240 -240
  3. package/build/@types/ApplePayButtonProps.d.ts +129 -129
  4. package/build/@types/ApplePayButtonProps.js +1 -1
  5. package/build/@types/charge.d.ts +57 -57
  6. package/build/@types/charge.js +1 -1
  7. package/build/@types/checkoutProfile.d.ts +216 -195
  8. package/build/@types/checkoutProfile.js +1 -1
  9. package/build/@types/enums.d.ts +65 -65
  10. package/build/@types/enums.js +76 -76
  11. package/build/@types/index.d.ts +6 -6
  12. package/build/@types/index.js +6 -6
  13. package/build/@types/tapLocalisation.d.ts +193 -193
  14. package/build/@types/tapLocalisation.js +1 -1
  15. package/build/@types/tapTheme.d.ts +842 -842
  16. package/build/@types/tapTheme.js +1 -1
  17. package/build/api.d.ts +29 -29
  18. package/build/api.js +244 -244
  19. package/build/constants/index.d.ts +3 -3
  20. package/build/constants/index.js +3 -3
  21. package/build/features/ApplePayButton/ApplePayButton.d.ts +4 -4
  22. package/build/features/ApplePayButton/ApplePayButton.js +49 -49
  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 -93
  28. package/build/hooks/useApplePay.js +447 -447
  29. package/build/hooks/useMerchantApplePay.d.ts +20 -20
  30. package/build/hooks/useMerchantApplePay.js +176 -176
  31. package/build/index.d.ts +4 -4
  32. package/build/index.js +13 -13
  33. package/build/utils/config.d.ts +14 -14
  34. package/build/utils/config.js +65 -65
  35. package/build/utils/defaultValues.d.ts +2 -2
  36. package/build/utils/defaultValues.js +27 -27
  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 -4
  40. package/build/utils/index.js +4 -4
  41. package/build/utils/theme.d.ts +12 -12
  42. package/build/utils/theme.js +61 -61
  43. package/package.json +109 -109
@@ -1,447 +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 (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, _b, paymentData, paymentMethod, transactionIdentifier, 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;
259
- return __generator(this, function (_v) {
260
- switch (_v.label) {
261
- case 0:
262
- if (debug)
263
- console.info('onpaymentauthorized event', event);
264
- _v.label = 1;
265
- case 1:
266
- _v.trys.push([1, 16, 17, 18]);
267
- _a = scope;
268
- switch (_a) {
269
- case Scope.APPLE_PAY_TOKEN: return [3, 2];
270
- case Scope.TAP_TOKEN: return [3, 4];
271
- case Scope.AUTHORIZE: return [3, 7];
272
- case Scope.CHARGE: return [3, 7];
273
- }
274
- return [3, 14];
275
- case 2: return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event.payment.token))];
276
- case 3:
277
- _v.sent();
278
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
279
- return [3, 15];
280
- case 4:
281
- setLoading(true);
282
- _b = (_d = event.payment.token) !== null && _d !== void 0 ? _d : {}, paymentData = _b.paymentData, paymentMethod = _b.paymentMethod, transactionIdentifier = _b.transactionIdentifier;
283
- return [4, appService.tapTokenization(__assign(__assign({}, paymentData), { paymentMethod: paymentMethod, transactionIdentifier: transactionIdentifier }))];
284
- case 5:
285
- data = _v.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
- _v.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(__assign({}, event.payment.token.paymentData), event.payment.token), { paymentData: undefined }))];
295
- case 8:
296
- tokenId = (_v.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: order.amount,
301
- currency: 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: (_h = customer === null || customer === void 0 ? void 0 : customer.contact) === null || _h === void 0 ? void 0 : _h.email,
308
- first_name: ((_j = customer === null || customer === void 0 ? void 0 : customer.name) === null || _j === void 0 ? void 0 : _j.length) ? customer.name[0].first : '',
309
- last_name: ((_k = customer === null || customer === void 0 ? void 0 : customer.name) === null || _k === void 0 ? void 0 : _k.length) ? customer.name[0].last : '',
310
- phone: {
311
- country_code: (_o = (_m = (_l = customer === null || customer === void 0 ? void 0 : customer.contact) === null || _l === void 0 ? void 0 : _l.phone) === null || _m === void 0 ? void 0 : _m.countryCode) !== null && _o !== void 0 ? _o : '',
312
- number: (_r = (_q = (_p = customer === null || customer === void 0 ? void 0 : customer.contact) === null || _p === void 0 ? void 0 : _p.phone) === null || _q === void 0 ? void 0 : _q.number) !== null && _r !== void 0 ? _r : ''
313
- }
314
- },
315
- fee: 0,
316
- merchant: {
317
- id: (_t = (_s = profileData === null || profileData === void 0 ? void 0 : profileData.merchant) === null || _s === void 0 ? void 0 : _s.id) !== null && _t !== void 0 ? _t : ''
318
- },
319
- order: (_u = profileData === null || profileData === void 0 ? void 0 : profileData.payment_options) === null || _u === void 0 ? void 0 : _u.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 = _v.sent();
339
- return [3, 12];
340
- case 10: return [4, appService.createCharge(transactionRequest)];
341
- case 11:
342
- _c = _v.sent();
343
- _v.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
- _v.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
- _v.label = 15;
363
- case 15: return [3, 18];
364
- case 16:
365
- error_2 = _v.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
- };
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, _b, paymentData, paymentMethod, transactionIdentifier, 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 = scope;
268
+ switch (_a) {
269
+ case Scope.APPLE_PAY_TOKEN: return [3, 2];
270
+ case Scope.TAP_TOKEN: return [3, 4];
271
+ case Scope.AUTHORIZE: return [3, 7];
272
+ case Scope.CHARGE: return [3, 7];
273
+ }
274
+ return [3, 14];
275
+ case 2: return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event.payment.token))];
276
+ case 3:
277
+ _1.sent();
278
+ session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
279
+ return [3, 15];
280
+ case 4:
281
+ setLoading(true);
282
+ _b = (_d = event.payment.token) !== null && _d !== void 0 ? _d : {}, paymentData = _b.paymentData, paymentMethod = _b.paymentMethod, transactionIdentifier = _b.transactionIdentifier;
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(__assign({}, event.payment.token.paymentData), event.payment.token), { paymentData: undefined }))];
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
+ };