@tap-payments/apple-pay-button 0.0.52-development → 0.0.53-development

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,5 +7,5 @@ interface UseApplePayReturnProps {
7
7
  applePayRequestData?: ApplePayRequestData;
8
8
  applePayRequestConfiguration?: Omit<ApplePayDataToLunchSDKFromMerchantSide, 'applePayRequestData'>;
9
9
  }
10
- export declare const useApplePay: ({ environment, interface: interfaceObj, merchant, customer, acceptance, scope, debug, onError, onSuccess, onCancel, onReady, metaData, headers, operator, order, onOrderCreated }: UseApplePayProps) => UseApplePayReturnProps;
10
+ export declare const useApplePay: ({ environment, interface: interfaceObj, merchant, customer, acceptance, scope, debug, onError, onSuccess, onCancel, onReady, metaData, headers, operator, order, onOrderCreated, integration }: UseApplePayProps) => UseApplePayReturnProps;
11
11
  export {};
@@ -49,33 +49,16 @@ import { useEffect, useState } from 'react';
49
49
  import appService from '../api/app.service';
50
50
  import { ApplePayVersion, Scope } from '../constants';
51
51
  import { getAxiosGlobalHeaders, setAxiosGlobalHeaders } from '../api/httpClient';
52
- import { getApplePayPaymentMethod, getApplePayRequest, validateSupportedNetworks } from '../utils/config';
53
- import { getMerchantCapabilities } from '../utils/defaultValues';
52
+ import { prepareApplePayRequest } from '../utils/config';
53
+ import { useMerchantApplePay } from './useMerchantApplePay';
54
54
  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;
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, integration = _a.integration;
56
56
  var _b = useState(false), loading = _b[0], setLoading = _b[1];
57
57
  var _c = useState(), profileData = _c[0], setProfile = _c[1];
58
58
  var _d = useState(false), disabled = _d[0], setDisabled = _d[1];
59
59
  var _e = useState(), applePayRequestData = _e[0], setApplePayRequestData = _e[1];
60
60
  var _f = useState(), applePayRequestConfiguration = _f[0], setApplePayRequestConfiguration = _f[1];
61
- var prepareApplePayRequest = function (_a) {
62
- var payment_options = _a.payment_options, merchant = _a.merchant;
63
- var paymentMethod = getApplePayPaymentMethod(payment_options.payment_methods || []);
64
- if (debug)
65
- console.log('payment method: ', paymentMethod);
66
- var supportedNetworks = validateSupportedNetworks(paymentMethod.supported_card_brands, acceptance === null || acceptance === void 0 ? void 0 : acceptance.supportedSchemes);
67
- var request = getApplePayRequest({
68
- countryCode: merchant.country_code,
69
- order: order,
70
- customer: customer,
71
- supportedNetworks: supportedNetworks,
72
- merchantCapabilities: getMerchantCapabilities(acceptance === null || acceptance === void 0 ? void 0 : acceptance.supportedFundSource),
73
- name: merchant.name
74
- });
75
- if (debug)
76
- console.log('apple pay request data: ', request);
77
- setApplePayRequestData(request);
78
- };
61
+ useMerchantApplePay({ isIframeIntegration: integration === 'iframe', onError: onError, onSuccess: onSuccess, scope: scope });
79
62
  var initialize = function (metaData) { return __awaiter(void 0, void 0, void 0, function () {
80
63
  var merchantProfile, payment_options, headers_1, data, err_1;
81
64
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
@@ -142,7 +125,7 @@ export var useApplePay = function (_a) {
142
125
  if (debug)
143
126
  console.log('merchant configuration: ', data);
144
127
  setProfile(data);
145
- prepareApplePayRequest(data);
128
+ setApplePayRequestData(prepareApplePayRequest(__assign(__assign({}, data), { order: order, acceptance: acceptance, customer: customer })));
146
129
  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);
147
130
  setApplePayRequestConfiguration({
148
131
  BASE_URL: appService.getBaseUrl(),
@@ -237,47 +220,39 @@ export var useApplePay = function (_a) {
237
220
  console.info('onpaymentauthorized event', event);
238
221
  _b.label = 1;
239
222
  case 1:
240
- _b.trys.push([1, 9, 10, 11]);
223
+ _b.trys.push([1, 7, 8, 9]);
241
224
  _a = scope;
242
225
  switch (_a) {
243
226
  case Scope.APPLE_PAY_TOKEN: return [3, 2];
244
227
  case Scope.TAP_TOKEN: return [3, 3];
245
- case Scope.CHARGE: return [3, 5];
246
- case Scope.AUTHORIZE: return [3, 6];
247
228
  }
248
- return [3, 7];
229
+ return [3, 5];
249
230
  case 2:
250
231
  onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event.payment.token.paymentData);
251
232
  session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
252
- return [3, 8];
233
+ return [3, 6];
253
234
  case 3: return [4, appService.tapTokenization(event.payment.token.paymentData)];
254
235
  case 4:
255
236
  data = _b.sent();
256
237
  onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
257
238
  session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
258
- return [3, 8];
239
+ return [3, 6];
259
240
  case 5:
260
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
261
- return [3, 8];
262
- case 6:
263
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
264
- return [3, 8];
265
- case 7:
266
241
  console.error('We only support AppleToken and TapToken for now');
267
242
  console.info('Completing payment with status: STATUS_FAILURE');
268
243
  session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
269
- _b.label = 8;
270
- case 8: return [3, 11];
271
- case 9:
244
+ _b.label = 6;
245
+ case 6: return [3, 9];
246
+ case 7:
272
247
  error_2 = _b.sent();
273
248
  if (debug)
274
249
  console.error('error in onpaymentauthorized', error_2);
275
250
  session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
276
251
  throw error_2;
277
- case 10:
252
+ case 8:
278
253
  setLoading(false);
279
254
  return [7];
280
- case 11: return [2];
255
+ case 9: return [2];
281
256
  }
282
257
  });
283
258
  }); };
@@ -300,7 +275,7 @@ export var useApplePay = function (_a) {
300
275
  return [2];
301
276
  }
302
277
  if (e.message) {
303
- onError([{ description: e.message, code: '400' }]);
278
+ onError([{ message: e.message, code: '400' }]);
304
279
  return [2];
305
280
  }
306
281
  onError(e);
@@ -308,8 +283,6 @@ export var useApplePay = function (_a) {
308
283
  return [2];
309
284
  });
310
285
  }); };
311
- if (debug)
312
- console.log('useApplePay return hooks', { loading: loading, disabled: disabled, applePayRequestData: applePayRequestData, applePayRequestConfiguration: applePayRequestConfiguration });
313
286
  return {
314
287
  loading: loading,
315
288
  onApplePayButtonClicked: onApplePayButtonClicked,
@@ -0,0 +1,9 @@
1
+ import { Scope } from '../constants';
2
+ interface ApplePayRequestData {
3
+ isIframeIntegration: boolean;
4
+ onError?: (error: any) => void;
5
+ onSuccess?: (data: Record<string, any>) => void;
6
+ scope: typeof Scope[keyof typeof Scope];
7
+ }
8
+ export declare const useMerchantApplePay: ({ isIframeIntegration, onError, onSuccess, scope }: ApplePayRequestData) => void;
9
+ export {};
@@ -0,0 +1,109 @@
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 (_) 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 { Scope } from '../constants';
39
+ import appService from '../api/app.service';
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, scope = _a.scope;
43
+ var onMessageReceived = function (_a) {
44
+ var message = _a.data;
45
+ return __awaiter(void 0, void 0, void 0, function () {
46
+ var _b, event, data, _c, response, e_1;
47
+ var _d;
48
+ return __generator(this, function (_e) {
49
+ switch (_e.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
+ _e.label = 1;
55
+ case 1:
56
+ _e.trys.push([1, 9, , 10]);
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, 6];
63
+ }
64
+ return [3, 7];
65
+ case 2:
66
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
67
+ return [3, 8];
68
+ case 3: return [4, appService.tapTokenization(data)];
69
+ case 4:
70
+ response = _e.sent();
71
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(response);
72
+ return [3, 8];
73
+ case 5: return [3, 8];
74
+ case 6: return [3, 8];
75
+ case 7:
76
+ console.error("We only support: ".concat(Object.values(Scope).join(', '), " for now"));
77
+ onError === null || onError === void 0 ? void 0 : onError({ message: "We only support: ".concat(Object.values(Scope).join(', '), " for now"), code: '400' });
78
+ return [3, 8];
79
+ case 8: return [3, 10];
80
+ case 9:
81
+ e_1 = _e.sent();
82
+ if (!onError)
83
+ return [2];
84
+ if (e_1.errors) {
85
+ onError(e_1.errors);
86
+ return [2];
87
+ }
88
+ if ((_d = e_1.response) === null || _d === void 0 ? void 0 : _d.data.errors) {
89
+ onError(e_1.response.data.errors);
90
+ return [2];
91
+ }
92
+ if (e_1.message) {
93
+ onError([{ message: e_1.message, code: '400' }]);
94
+ return [2];
95
+ }
96
+ onError(e_1);
97
+ return [3, 10];
98
+ case 10: return [2];
99
+ }
100
+ });
101
+ });
102
+ };
103
+ React.useEffect(function () {
104
+ if (!isIframeIntegration)
105
+ return;
106
+ window.addEventListener('message', onMessageReceived);
107
+ return function () { return window.removeEventListener('message', onMessageReceived); };
108
+ }, []);
109
+ };
@@ -1 +1 @@
1
- export declare function useScript(src: string, async?: boolean): "loading" | "ready" | "idle" | "error";
1
+ export declare function useScript(src: string, async?: boolean): "error" | "loading" | "ready" | "idle";
@@ -1,4 +1,4 @@
1
- import { Acceptance, ApplePayButtonProps, ApplePayRequestData, PaymentMethod } from '../@types';
1
+ import { Acceptance, ApplePayButtonProps, ApplePayRequestData, MetaData, PaymentMethod } from '../@types';
2
2
  export declare const validateSupportedNetworks: (supportedNetworksOptions: Acceptance['supportedSchemes'], merchantSupportedNetworks?: Acceptance['supportedSchemes']) => Acceptance['supportedSchemes'];
3
3
  export declare const getApplePayRequest: ({ order, customer, supportedNetworks, merchantCapabilities, countryCode, name }: Pick<ApplePayButtonProps, "order" | "customer"> & {
4
4
  supportedNetworks: Acceptance['supportedSchemes'];
@@ -9,3 +9,4 @@ export declare const getApplePayRequest: ({ order, customer, supportedNetworks,
9
9
  export declare const getApplePayPaymentMethod: (paymentMethods: Array<PaymentMethod>) => PaymentMethod;
10
10
  export declare const rsaSetup: (key: string) => (string: string) => string;
11
11
  export declare function generateApplicationHeader(obj: Record<string, string | any>): string;
12
+ export declare const prepareApplePayRequest: ({ payment_options, merchant, order, customer, acceptance }: Pick<MetaData, 'merchant' | 'payment_options'> & Pick<ApplePayButtonProps, 'order' | 'customer' | 'acceptance'>) => ApplePayRequestData;
@@ -1,4 +1,5 @@
1
1
  import { JSEncrypt } from 'jsencrypt';
2
+ import { getMerchantCapabilities } from './defaultValues';
2
3
  export var validateSupportedNetworks = function (supportedNetworksOptions, merchantSupportedNetworks) {
3
4
  var toLowerCase = function (item) { return item.toLowerCase(); };
4
5
  if (!supportedNetworksOptions)
@@ -62,3 +63,17 @@ export function generateApplicationHeader(obj) {
62
63
  .map(function (key) { return "".concat(key, "=").concat(obj[key]); })
63
64
  .join('|');
64
65
  }
66
+ export var prepareApplePayRequest = function (_a) {
67
+ var payment_options = _a.payment_options, merchant = _a.merchant, order = _a.order, customer = _a.customer, acceptance = _a.acceptance;
68
+ var paymentMethod = getApplePayPaymentMethod(payment_options.payment_methods || []);
69
+ var supportedNetworks = validateSupportedNetworks(paymentMethod.supported_card_brands, acceptance === null || acceptance === void 0 ? void 0 : acceptance.supportedSchemes);
70
+ var request = getApplePayRequest({
71
+ countryCode: merchant.country_code,
72
+ order: order,
73
+ customer: customer,
74
+ supportedNetworks: supportedNetworks,
75
+ merchantCapabilities: getMerchantCapabilities(acceptance === null || acceptance === void 0 ? void 0 : acceptance.supportedFundSource),
76
+ name: merchant.name
77
+ });
78
+ return request;
79
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tap-payments/apple-pay-button",
3
- "version": "0.0.52-development",
3
+ "version": "0.0.53-development",
4
4
  "description": "Apple Pay Button React Component",
5
5
  "main": "build/index.js",
6
6
  "module": "build/index.js",