@tap-payments/apple-pay-button 0.0.38-development → 0.0.38-test

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