@tap-payments/apple-pay-button 0.0.6-test → 0.0.7-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 (67) hide show
  1. package/README.md +204 -81
  2. package/build/@types/ApplePayButtonProps.d.ts +56 -0
  3. package/build/@types/charge.d.ts +51 -0
  4. package/build/@types/charge.js +1 -0
  5. package/build/@types/checkoutProfile.d.ts +216 -0
  6. package/build/@types/checkoutProfile.js +1 -0
  7. package/build/@types/enums.d.ts +44 -0
  8. package/build/@types/enums.js +51 -0
  9. package/build/@types/index.d.ts +6 -0
  10. package/build/@types/index.js +6 -0
  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 +31 -0
  16. package/build/api.js +236 -0
  17. package/build/constants/index.d.ts +4 -0
  18. package/build/constants/index.js +4 -0
  19. package/build/features/ApplePayButton/ApplePayButton.d.ts +4 -0
  20. package/build/features/ApplePayButton/ApplePayButton.js +48 -0
  21. package/build/{src/features → features}/ApplePayButton/index.d.ts +2 -2
  22. package/build/hooks/index.d.ts +1 -0
  23. package/build/hooks/index.js +1 -0
  24. package/build/hooks/useApplePay.d.ts +84 -0
  25. package/build/hooks/useApplePay.js +396 -0
  26. package/build/hooks/useMerchantApplePay.d.ts +22 -0
  27. package/build/hooks/useMerchantApplePay.js +190 -0
  28. package/build/index.d.ts +5 -0
  29. package/build/index.js +14 -0
  30. package/build/utils/config.d.ts +14 -0
  31. package/build/utils/config.js +59 -0
  32. package/build/utils/defaultValues.d.ts +2 -0
  33. package/build/utils/defaultValues.js +27 -0
  34. package/build/utils/index.d.ts +3 -0
  35. package/build/utils/index.js +3 -0
  36. package/build/utils/theme.d.ts +13 -0
  37. package/build/utils/theme.js +62 -0
  38. package/package.json +109 -102
  39. package/build/features/ApplePayButton/ApplePayButton.css +0 -51
  40. package/build/package.json +0 -102
  41. package/build/src/@types/index.d.ts +0 -73
  42. package/build/src/api/app.service.d.ts +0 -11
  43. package/build/src/api/app.service.js +0 -217
  44. package/build/src/api/base.d.ts +0 -9
  45. package/build/src/api/base.js +0 -45
  46. package/build/src/api/httpClient.d.ts +0 -2
  47. package/build/src/api/httpClient.js +0 -16
  48. package/build/src/constants/index.d.ts +0 -26
  49. package/build/src/constants/index.js +0 -26
  50. package/build/src/features/ApplePayButton/ApplePayButton.d.ts +0 -8
  51. package/build/src/features/ApplePayButton/ApplePayButton.js +0 -43
  52. package/build/src/hooks/index.d.ts +0 -1
  53. package/build/src/hooks/index.js +0 -1
  54. package/build/src/hooks/useApplePay.d.ts +0 -9
  55. package/build/src/hooks/useApplePay.js +0 -206
  56. package/build/src/hooks/useScript.d.ts +0 -1
  57. package/build/src/hooks/useScript.js +0 -39
  58. package/build/src/index.d.ts +0 -4
  59. package/build/src/index.js +0 -11
  60. package/build/src/utils/config.d.ts +0 -9
  61. package/build/src/utils/config.js +0 -54
  62. package/build/src/utils/html.d.ts +0 -3
  63. package/build/src/utils/html.js +0 -20
  64. package/build/src/utils/index.d.ts +0 -1
  65. package/build/src/utils/index.js +0 -1
  66. /package/build/{src/@types/index.js → @types/ApplePayButtonProps.js} +0 -0
  67. /package/build/{src/features → features}/ApplePayButton/index.js +0 -0
@@ -1,217 +0,0 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = function (d, b) {
3
- extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
- return extendStatics(d, b);
7
- };
8
- return function (d, b) {
9
- if (typeof b !== "function" && b !== null)
10
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
- extendStatics(d, b);
12
- function __() { this.constructor = d; }
13
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
- };
15
- })();
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
- var __generator = (this && this.__generator) || function (thisArg, body) {
26
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
- function verb(n) { return function (v) { return step([n, v]); }; }
29
- function step(op) {
30
- if (f) throw new TypeError("Generator is already executing.");
31
- while (_) try {
32
- 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;
33
- if (y = 0, t) op = [op[0] & 2, t.value];
34
- switch (op[0]) {
35
- case 0: case 1: t = op; break;
36
- case 4: _.label++; return { value: op[1], done: false };
37
- case 5: _.label++; y = op[1]; op = [0]; continue;
38
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
- default:
40
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
- if (t[2]) _.ops.pop();
45
- _.trys.pop(); continue;
46
- }
47
- op = body.call(thisArg, _);
48
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
- }
51
- };
52
- import BaseService from './base';
53
- import httpClient from './httpClient';
54
- var APPService = (function (_super) {
55
- __extends(APPService, _super);
56
- function APPService() {
57
- return _super.call(this, httpClient) || this;
58
- }
59
- Object.defineProperty(APPService.prototype, "init", {
60
- enumerable: false,
61
- configurable: true,
62
- writable: true,
63
- value: function (publicKey, merchantId) {
64
- return __awaiter(this, void 0, void 0, function () {
65
- var body, data;
66
- return __generator(this, function (_a) {
67
- switch (_a.label) {
68
- case 0:
69
- body = {
70
- headers: { authorization: "Bearer ".concat(publicKey) },
71
- reqBody: { public_key: publicKey, merchant_id: merchantId }
72
- };
73
- return [4, this.post('/init', body)];
74
- case 1:
75
- data = (_a.sent()).data;
76
- if (data.errors)
77
- throw data;
78
- return [2, {
79
- merchant: data.merchant,
80
- payment_options: data.payment_options
81
- }];
82
- }
83
- });
84
- });
85
- }
86
- });
87
- Object.defineProperty(APPService.prototype, "appleSession", {
88
- enumerable: false,
89
- configurable: true,
90
- writable: true,
91
- value: function (merchant, validationURL, merchantRegisteredDomain) {
92
- return __awaiter(this, void 0, void 0, function () {
93
- var body, data;
94
- return __generator(this, function (_a) {
95
- switch (_a.label) {
96
- case 0:
97
- body = {
98
- validationUrl: validationURL,
99
- origin: merchantRegisteredDomain,
100
- merchantIdentifier: 'merchant.tap.gosell',
101
- merchantId: merchant.id,
102
- merchantName: merchant.name,
103
- session_token: merchant.session_token
104
- };
105
- return [4, this.post('/session', body)];
106
- case 1:
107
- data = (_a.sent()).data;
108
- if (data.errors)
109
- throw data;
110
- return [2, data];
111
- }
112
- });
113
- });
114
- }
115
- });
116
- Object.defineProperty(APPService.prototype, "tapTokenization", {
117
- enumerable: false,
118
- configurable: true,
119
- writable: true,
120
- value: function (applePaymentData, merchant) {
121
- return __awaiter(this, void 0, void 0, function () {
122
- var body, data;
123
- return __generator(this, function (_a) {
124
- switch (_a.label) {
125
- case 0:
126
- body = {
127
- method: 'POST',
128
- path: '/v2/token',
129
- headers: {
130
- session_token: merchant.session_token
131
- },
132
- reqBody: {
133
- type: 'applepay',
134
- token_data: applePaymentData
135
- }
136
- };
137
- return [4, this.post('/api', body)];
138
- case 1:
139
- data = (_a.sent()).data;
140
- if (data.errors)
141
- throw data;
142
- return [2, data];
143
- }
144
- });
145
- });
146
- }
147
- });
148
- Object.defineProperty(APPService.prototype, "createCharge", {
149
- enumerable: false,
150
- configurable: true,
151
- writable: true,
152
- value: function (publicKey, merchant, request, cardToken) {
153
- var body = {
154
- method: 'POST',
155
- path: '/v2/charge',
156
- headers: {
157
- session_token: merchant.session_token
158
- },
159
- reqBody: {
160
- id: null,
161
- amount: request.total.amount,
162
- currency: request.currencyCode,
163
- public_key: publicKey,
164
- product: 'GOSELL',
165
- threeDSecure: true,
166
- save_card: false,
167
- fee: 0,
168
- statement_descriptor: 'Sample',
169
- description: 'Test Description',
170
- metadata: {},
171
- reference: {
172
- transaction: 'txn_0001',
173
- order: 'ord_0001'
174
- },
175
- receipt: {
176
- email: false,
177
- sms: true
178
- },
179
- customer: {
180
- id: null,
181
- first_name: 'Hala',
182
- middle_name: null,
183
- last_name: 'Q.',
184
- email: 'email@email.com',
185
- phone: {
186
- country_code: '965',
187
- number: '00000000'
188
- }
189
- },
190
- source: {
191
- id: cardToken
192
- },
193
- hashstring: null,
194
- post: {
195
- url: null
196
- },
197
- redirect: {
198
- url: 'https://jslib.gosell.io/demo?token=63cce645332aa501e6cf768f&mode=popup'
199
- },
200
- ipaddress: null,
201
- selected_currency: request.currencyCode,
202
- selected_amount: request.total.amount,
203
- merchant: {
204
- id: merchant.id
205
- },
206
- order: {
207
- id: 'ord_06CO2023731xt7V22SC0X487'
208
- }
209
- }
210
- };
211
- return this.post('/api', body);
212
- }
213
- });
214
- return APPService;
215
- }(BaseService));
216
- var appService = new APPService();
217
- export default appService;
@@ -1,9 +0,0 @@
1
- import { AxiosInstance, AxiosRequestConfig } from 'axios';
2
- export default class BaseService {
3
- httpClient: AxiosInstance;
4
- constructor(httpClient: AxiosInstance);
5
- get(url: string, config?: AxiosRequestConfig<any>): Promise<import("axios").AxiosResponse<any, any>>;
6
- post(url: string, data?: any, config?: AxiosRequestConfig<any>): Promise<import("axios").AxiosResponse<any, any>>;
7
- put(url: string, data?: any, config?: AxiosRequestConfig<any>): Promise<import("axios").AxiosResponse<any, any>>;
8
- delete(url: string, config?: AxiosRequestConfig<any>): Promise<import("axios").AxiosResponse<any, any>>;
9
- }
@@ -1,45 +0,0 @@
1
- var BaseService = (function () {
2
- function BaseService(httpClient) {
3
- Object.defineProperty(this, "httpClient", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- this.httpClient = httpClient;
10
- }
11
- Object.defineProperty(BaseService.prototype, "get", {
12
- enumerable: false,
13
- configurable: true,
14
- writable: true,
15
- value: function (url, config) {
16
- return this.httpClient.get(url, config);
17
- }
18
- });
19
- Object.defineProperty(BaseService.prototype, "post", {
20
- enumerable: false,
21
- configurable: true,
22
- writable: true,
23
- value: function (url, data, config) {
24
- return this.httpClient.post(url, data, config);
25
- }
26
- });
27
- Object.defineProperty(BaseService.prototype, "put", {
28
- enumerable: false,
29
- configurable: true,
30
- writable: true,
31
- value: function (url, data, config) {
32
- return this.httpClient.put(url, data, config);
33
- }
34
- });
35
- Object.defineProperty(BaseService.prototype, "delete", {
36
- enumerable: false,
37
- configurable: true,
38
- writable: true,
39
- value: function (url, config) {
40
- return this.httpClient.delete(url, config);
41
- }
42
- });
43
- return BaseService;
44
- }());
45
- export default BaseService;
@@ -1,2 +0,0 @@
1
- declare const httpClient: import("axios").AxiosInstance;
2
- export default httpClient;
@@ -1,16 +0,0 @@
1
- import axios from 'axios';
2
- var MW_BASE_URL = 'https://checkout.payments.tap.company/api';
3
- var httpClient = axios.create({
4
- baseURL: MW_BASE_URL,
5
- headers: {
6
- 'Content-Type': 'application/json',
7
- Accept: 'application/json'
8
- }
9
- });
10
- httpClient.interceptors.request.use(function (config) {
11
- return config;
12
- });
13
- httpClient.interceptors.response.use(function (response) {
14
- return response;
15
- });
16
- export default httpClient;
@@ -1,26 +0,0 @@
1
- export declare const Scope: {
2
- readonly AppleToken: "AppleToken";
3
- readonly TapToken: "TapToken";
4
- };
5
- export declare const ButtonStyle: {
6
- readonly Black: "black";
7
- readonly White: "white";
8
- readonly WhiteOutline: "whiteOutline";
9
- };
10
- export declare const MerchantCapabilities: {
11
- readonly Supports3DS: "supports3DS";
12
- readonly SupportsCredit: "supportsCredit";
13
- readonly SupportsDebit: "supportsDebit";
14
- };
15
- export declare const SupportedNetworks: {
16
- readonly Amex: "amex";
17
- readonly Mada: "mada";
18
- readonly MasterCard: "masterCard";
19
- readonly Visa: "visa";
20
- readonly ChinaUnionPay: "chinaUnionPay";
21
- readonly Discover: "discover";
22
- readonly Electron: "electron";
23
- readonly Jcb: "jcb";
24
- readonly Maestro: "maestro";
25
- };
26
- export declare const ApplePayVersion = 5;
@@ -1,26 +0,0 @@
1
- export var Scope = {
2
- AppleToken: 'AppleToken',
3
- TapToken: 'TapToken'
4
- };
5
- export var ButtonStyle = {
6
- Black: 'black',
7
- White: 'white',
8
- WhiteOutline: 'whiteOutline'
9
- };
10
- export var MerchantCapabilities = {
11
- Supports3DS: 'supports3DS',
12
- SupportsCredit: 'supportsCredit',
13
- SupportsDebit: 'supportsDebit'
14
- };
15
- export var SupportedNetworks = {
16
- Amex: 'amex',
17
- Mada: 'mada',
18
- MasterCard: 'masterCard',
19
- Visa: 'visa',
20
- ChinaUnionPay: 'chinaUnionPay',
21
- Discover: 'discover',
22
- Electron: 'electron',
23
- Jcb: 'jcb',
24
- Maestro: 'maestro'
25
- };
26
- export var ApplePayVersion = 5;
@@ -1,8 +0,0 @@
1
- /// <reference types="react" />
2
- import { ApplePayButtonProps } from '../../@types';
3
- import './ApplePayButton.css';
4
- export type { ApplePayButtonProps };
5
- export declare function ApplePayButton(props: ApplePayButtonProps): JSX.Element;
6
- export declare function renderApplePayButton(config: ApplePayButtonProps, elementId: string): {
7
- unmount: () => void;
8
- };
@@ -1,43 +0,0 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- import { jsx as _jsx } from "react/jsx-runtime";
13
- import React from 'react';
14
- import { createRoot } from 'react-dom/client';
15
- import { createElementAndInject } from '../../utils';
16
- import { useApplePay } from '../../hooks/useApplePay';
17
- import { ButtonStyle, Scope } from '../../constants';
18
- import './ApplePayButton.css';
19
- var ApplePay = React.memo(function (_a) {
20
- var publicKey = _a.publicKey, merchant = _a.merchant, transaction = _a.transaction, billingContact = _a.billingContact, supportedNetworks = _a.supportedNetworks, onCancel = _a.onCancel, onError = _a.onError, onSuccess = _a.onSuccess, _b = _a.scope, scope = _b === void 0 ? Scope.TapToken : _b, _c = _a.buttonStyle, buttonStyle = _c === void 0 ? ButtonStyle.WhiteOutline : _c;
21
- var _d = useApplePay({
22
- publicKey: publicKey,
23
- merchant: merchant,
24
- transaction: transaction,
25
- billingContact: billingContact,
26
- supportedNetworks: supportedNetworks,
27
- onCancel: onCancel,
28
- onError: onError,
29
- onSuccess: onSuccess,
30
- scope: scope
31
- }), loading = _d.loading, onApplePayButtonClicked = _d.onApplePayButtonClicked, disabled = _d.disabled;
32
- return (_jsx("button", __assign({ className: "button-applepay-tap ".concat(buttonStyle), onClick: onApplePayButtonClicked, disabled: loading || disabled }, { children: _jsx("div", __assign({ className: 'img-container' }, { children: _jsx("img", { loading: 'eager', src: 'https://back-end.b-cdn.net/payment_methods/apple_pay.svg' }) })) })));
33
- });
34
- export function ApplePayButton(props) {
35
- return _jsx(ApplePay, __assign({}, props));
36
- }
37
- export function renderApplePayButton(config, elementId) {
38
- var el = createElementAndInject(elementId);
39
- var root = createRoot(el);
40
- root.render(_jsx(ApplePay, __assign({}, config)));
41
- var unmount = function () { return root.unmount(); };
42
- return { unmount: unmount };
43
- }
@@ -1 +0,0 @@
1
- export * from './useScript';
@@ -1 +0,0 @@
1
- export * from './useScript';
@@ -1,9 +0,0 @@
1
- import { ApplePayButtonProps } from '../@types';
2
- declare type UseApplePayProps = Omit<ApplePayButtonProps, 'buttonStyle'>;
3
- interface UseApplePayReturnProps {
4
- loading: boolean;
5
- onApplePayButtonClicked: () => Promise<void>;
6
- disabled: boolean;
7
- }
8
- export declare const useApplePay: ({ publicKey, merchant, transaction, billingContact, onCancel, onError, onSuccess, scope, supportedNetworks }: UseApplePayProps) => UseApplePayReturnProps;
9
- export {};
@@ -1,206 +0,0 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
- return new (P || (P = Promise))(function (resolve, reject) {
15
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
- step((generator = generator.apply(thisArg, _arguments || [])).next());
19
- });
20
- };
21
- var __generator = (this && this.__generator) || function (thisArg, body) {
22
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
- function verb(n) { return function (v) { return step([n, v]); }; }
25
- function step(op) {
26
- if (f) throw new TypeError("Generator is already executing.");
27
- while (_) try {
28
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
- if (y = 0, t) op = [op[0] & 2, t.value];
30
- switch (op[0]) {
31
- case 0: case 1: t = op; break;
32
- case 4: _.label++; return { value: op[1], done: false };
33
- case 5: _.label++; y = op[1]; op = [0]; continue;
34
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
- default:
36
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
- if (t[2]) _.ops.pop();
41
- _.trys.pop(); continue;
42
- }
43
- op = body.call(thisArg, _);
44
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
- }
47
- };
48
- 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';
52
- 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);
69
- });
70
- }, [merchant, onError, publicKey]);
71
- useEffect(function () {
72
- getMerchantData();
73
- }, [getMerchantData]);
74
- var onApplePayButtonClicked = function () { return __awaiter(void 0, void 0, void 0, function () {
75
- var ApplePaySession, paymentMethod, currency, cardBrands, request, session_1;
76
- var _a;
77
- return __generator(this, function (_b) {
78
- ApplePaySession = window.ApplePaySession;
79
- if (!ApplePaySession) {
80
- setDisabled(true);
81
- console.error('ApplePaySession is not available, please check if you are using Safari browser');
82
- return [2];
83
- }
84
- if (!profileData) {
85
- setDisabled(true);
86
- console.error('The profile data is not available, please check networks and try again');
87
- return [2];
88
- }
89
- try {
90
- paymentMethod = getApplePayPaymentMethod(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
- name: profileData.merchant.name
99
- });
100
- console.info("Creating ApplePaySession with version: ".concat(ApplePayVersion, " and request: ").concat(JSON.stringify(request)));
101
- session_1 = new ApplePaySession(ApplePayVersion, request);
102
- session_1.onvalidatemerchant = function (event) { return __awaiter(void 0, void 0, void 0, function () {
103
- var merchantSession, error_1;
104
- return __generator(this, function (_a) {
105
- switch (_a.label) {
106
- case 0:
107
- console.info('Creating merchant session and validating merchant session');
108
- console.info('onvalidatemerchant event', event);
109
- setLoading(true);
110
- _a.label = 1;
111
- case 1:
112
- _a.trys.push([1, 3, , 4]);
113
- console.info("creating merchant session for merchantData: ".concat(JSON.stringify(profileData.merchant), " and validationURL: ").concat(event.validationURL, " and merchant.domain: ").concat(merchant.domain));
114
- return [4, appService.appleSession(profileData.merchant, event.validationURL, merchant.domain)];
115
- case 2:
116
- merchantSession = (_a.sent()).body;
117
- console.info('merchantSession', merchantSession);
118
- session_1.completeMerchantValidation(merchantSession);
119
- console.info('merchantSession completed successfully!');
120
- return [3, 4];
121
- case 3:
122
- error_1 = _a.sent();
123
- console.error('error in onvalidatemerchant', error_1);
124
- session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
125
- throw error_1;
126
- case 4:
127
- setLoading(false);
128
- return [2];
129
- }
130
- });
131
- }); };
132
- session_1.onpaymentauthorized = function (event) { return __awaiter(void 0, void 0, void 0, function () {
133
- var _a, data, error_2;
134
- return __generator(this, function (_b) {
135
- switch (_b.label) {
136
- case 0:
137
- setLoading(true);
138
- console.info('onpaymentauthorized event', event);
139
- _b.label = 1;
140
- case 1:
141
- _b.trys.push([1, 9, , 10]);
142
- _a = scope;
143
- switch (_a) {
144
- case 'AppleToken': return [3, 2];
145
- case 'TapToken': return [3, 4];
146
- }
147
- return [3, 7];
148
- case 2: return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event.payment.token.paymentData))];
149
- case 3:
150
- _b.sent();
151
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
152
- return [3, 8];
153
- case 4: return [4, appService.tapTokenization(event.payment.token.paymentData, profileData.merchant)];
154
- case 5:
155
- data = _b.sent();
156
- return [4, (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data))];
157
- case 6:
158
- _b.sent();
159
- session_1.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
160
- return [3, 8];
161
- case 7:
162
- console.error('We only support AppleToken and TapToken for now');
163
- console.info('Completing payment with status: STATUS_FAILURE');
164
- session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
165
- _b.label = 8;
166
- case 8: return [3, 10];
167
- case 9:
168
- error_2 = _b.sent();
169
- console.error('error in onpaymentauthorized', error_2);
170
- session_1.completePayment({ status: ApplePaySession.STATUS_FAILURE });
171
- throw error_2;
172
- case 10:
173
- setLoading(false);
174
- return [2];
175
- }
176
- });
177
- }); };
178
- session_1.oncancel = function (event) {
179
- console.info('oncancel event', event);
180
- if (onCancel)
181
- onCancel();
182
- };
183
- session_1.begin();
184
- }
185
- catch (e) {
186
- if (!onError)
187
- return [2];
188
- if (e.errors) {
189
- onError(e.errors);
190
- return [2];
191
- }
192
- if ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data.errors) {
193
- onError(e.response.data.errors);
194
- return [2];
195
- }
196
- if (e.message) {
197
- onError([{ description: e.message, code: '400' }]);
198
- return [2];
199
- }
200
- onError(e);
201
- }
202
- return [2];
203
- });
204
- }); };
205
- return { loading: loading, onApplePayButtonClicked: onApplePayButtonClicked, disabled: !profileData || disabled };
206
- };
@@ -1 +0,0 @@
1
- export declare function useScript(src: string, async?: boolean): "loading" | "ready" | "idle" | "error";
@@ -1,39 +0,0 @@
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
- }