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