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