@tonder.io/ionic-lite-sdk 0.0.35-beta.8 → 0.0.37-beta.1

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 (83) hide show
  1. package/.gitlab-ci.yml +28 -28
  2. package/README.md +532 -202
  3. package/dist/classes/BaseInlineCheckout.d.ts +21 -19
  4. package/dist/classes/errorResponse.d.ts +1 -1
  5. package/dist/classes/liteCheckout.d.ts +15 -43
  6. package/dist/data/api.d.ts +1 -1
  7. package/dist/data/businessApi.d.ts +1 -1
  8. package/dist/data/cardApi.d.ts +1 -1
  9. package/dist/data/checkoutApi.d.ts +2 -1
  10. package/dist/data/customerApi.d.ts +1 -1
  11. package/dist/data/paymentMethodApi.d.ts +2 -2
  12. package/dist/helpers/skyflow.d.ts +1 -1
  13. package/dist/index.d.ts +2 -1
  14. package/dist/index.js +1 -1
  15. package/dist/types/card.d.ts +1 -0
  16. package/dist/types/checkout.d.ts +7 -1
  17. package/dist/types/commons.d.ts +8 -3
  18. package/dist/types/customer.d.ts +10 -0
  19. package/{src → dist}/types/liteInlineCheckout.d.ts +33 -73
  20. package/dist/types/responses.d.ts +3 -0
  21. package/jest.config.ts +14 -14
  22. package/package.json +41 -38
  23. package/rollup.config.js +16 -16
  24. package/src/classes/3dsHandler.ts +237 -237
  25. package/src/classes/BaseInlineCheckout.ts +385 -356
  26. package/src/classes/errorResponse.ts +16 -16
  27. package/src/classes/liteCheckout.ts +588 -598
  28. package/src/data/api.ts +20 -20
  29. package/src/data/businessApi.ts +18 -18
  30. package/src/data/cardApi.ts +85 -89
  31. package/src/data/checkoutApi.ts +84 -87
  32. package/src/data/customerApi.ts +31 -31
  33. package/src/data/openPayApi.ts +12 -12
  34. package/src/data/paymentMethodApi.ts +37 -37
  35. package/src/data/skyflowApi.ts +20 -20
  36. package/src/helpers/constants.ts +63 -63
  37. package/src/helpers/mercadopago.ts +15 -15
  38. package/src/helpers/skyflow.ts +91 -91
  39. package/src/helpers/utils.ts +120 -120
  40. package/src/helpers/validations.ts +55 -55
  41. package/src/index.ts +12 -10
  42. package/src/shared/catalog/paymentMethodsCatalog.ts +247 -247
  43. package/src/shared/constants/messages.ts +10 -10
  44. package/src/shared/constants/paymentMethodAPM.ts +63 -63
  45. package/src/shared/constants/tonderUrl.ts +8 -8
  46. package/src/types/card.ts +35 -34
  47. package/src/types/checkout.ts +123 -117
  48. package/src/types/commons.ts +130 -125
  49. package/src/types/customer.ts +22 -12
  50. package/src/types/liteInlineCheckout.ts +216 -0
  51. package/src/types/paymentMethod.ts +23 -23
  52. package/src/types/requests.ts +114 -114
  53. package/src/types/responses.ts +193 -189
  54. package/src/types/skyflow.ts +17 -17
  55. package/src/types/transaction.ts +101 -101
  56. package/src/types/validations.d.ts +11 -11
  57. package/tests/classes/liteCheckout.test.ts +57 -57
  58. package/tests/methods/createOrder.test.ts +141 -142
  59. package/tests/methods/createPayment.test.ts +121 -122
  60. package/tests/methods/customerRegister.test.ts +118 -119
  61. package/tests/methods/getBusiness.test.ts +114 -115
  62. package/tests/methods/getCustomerCards.test.ts +112 -113
  63. package/tests/methods/registerCustomerCard.test.ts +117 -117
  64. package/tests/methods/startCheckoutRouter.test.ts +119 -119
  65. package/tests/methods/startCheckoutRouterFull.test.ts +138 -138
  66. package/tests/utils/defaultMock.ts +21 -21
  67. package/tests/utils/mockClasses.ts +659 -659
  68. package/tsconfig.json +18 -18
  69. package/types/classes/liteCheckout.d.ts +29 -0
  70. package/types/classes/liteCheckout.js +225 -0
  71. package/types/classes/liteCheckout.js.map +1 -0
  72. package/types/helpers/utils.d.ts +3 -0
  73. package/types/helpers/utils.js +27 -0
  74. package/types/helpers/utils.js.map +1 -0
  75. package/types/index.d.ts +2 -0
  76. package/types/index.js +6 -0
  77. package/types/index.js.map +1 -0
  78. package/.idea/aws.xml +0 -17
  79. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  80. package/.idea/prettier.xml +0 -6
  81. package/.idea/vcs.xml +0 -6
  82. package/.idea/workspace.xml +0 -133
  83. package/src/types/index.d.ts +0 -10
@@ -1,356 +1,385 @@
1
- import { ThreeDSHandler } from "./3dsHandler";
2
- import { ErrorResponse } from "./errorResponse";
3
- import { fetchBusiness } from "../data/businessApi";
4
- import { injectMercadoPagoSecurity } from "../helpers/mercadopago";
5
- import { TONDER_URL_BY_MODE } from "../shared/constants/tonderUrl";
6
- import {
7
- createOrder,
8
- createPayment,
9
- startCheckoutRouter,
10
- } from "../data/checkoutApi";
11
- import { getOpenpayDeviceSessionID } from "../data/openPayApi";
12
- import { getBrowserInfo } from "../helpers/utils";
13
- import { registerOrFetchCustomer } from "../data/customerApi";
14
- import {
15
- Business,
16
- ICustomer,
17
- IItem,
18
- IInlineCheckoutBaseOptions,
19
- IConfigureCheckout,
20
- IProcessPaymentRequest,
21
- IStartCheckoutResponse,
22
- StartCheckoutResponse,
23
- ICardFields,
24
- } from "../types";
25
- export class BaseInlineCheckout {
26
- baseUrl = "";
27
- cartTotal = "0";
28
- process3ds: ThreeDSHandler;
29
- mode?: "production" | "sandbox" | "stage" | "development" | undefined;
30
- apiKeyTonder: string;
31
- returnUrl: string;
32
- callBack?: ((response: any) => void) | undefined;
33
- merchantData?: Business;
34
- abortController: AbortController;
35
-
36
- firstName?: string;
37
- lastName?: string;
38
- country?: string;
39
- address?: string;
40
- city?: string;
41
- state?: string;
42
- postCode?: string;
43
- email?: string;
44
- phone?: string;
45
- customer?: ICustomer;
46
-
47
- cartItems?: IItem[];
48
- metadata = {};
49
- card? = {};
50
- currency?: string = "";
51
-
52
- #customerData?: Record<string, any>;
53
-
54
- constructor({
55
- mode = "stage",
56
- apiKey,
57
- apiKeyTonder,
58
- returnUrl,
59
- callBack = () => {},
60
- }: IInlineCheckoutBaseOptions) {
61
- this.apiKeyTonder = apiKey || apiKeyTonder || "";
62
- this.returnUrl = returnUrl;
63
- this.callBack = callBack;
64
- this.mode = mode;
65
- this.baseUrl = TONDER_URL_BY_MODE[this.mode] || TONDER_URL_BY_MODE["stage"];
66
- this.abortController = new AbortController();
67
- this.process3ds = new ThreeDSHandler({
68
- apiKey: apiKey,
69
- baseUrl: this.baseUrl,
70
- });
71
- }
72
-
73
- configureCheckout(data: IConfigureCheckout) {
74
- if ("customer" in data) this.#handleCustomer(data["customer"]);
75
- }
76
-
77
- async verify3dsTransaction() {
78
- const result3ds = await this.process3ds.verifyTransactionStatus();
79
- const resultCheckout = await this.#resumeCheckout(result3ds);
80
- this.process3ds.setPayload(resultCheckout);
81
- return this._handle3dsRedirect(resultCheckout);
82
- }
83
-
84
- payment(data: IProcessPaymentRequest): Promise<IStartCheckoutResponse> {
85
- return new Promise(async (resolve, reject) => {
86
- try {
87
- this.#handleCustomer(data.customer);
88
- this._setCartTotal(data.cart?.total);
89
- this.#setCartItems(data.cart?.items);
90
- this.#handleMetadata(data);
91
- this.#handleCurrency(data);
92
- this.#handleCard(data);
93
- const response = await this._checkout(data);
94
- this.process3ds.setPayload(response);
95
- if (this.callBack) this.callBack!(response);
96
- const payload = await this._handle3dsRedirect(response);
97
- if (payload) {
98
- resolve(response);
99
- }
100
- } catch (error) {
101
- reject(error);
102
- }
103
- });
104
- }
105
-
106
- async _initializeCheckout() {
107
- const business_response = await this._fetchMerchantData();
108
-
109
- if (
110
- !!business_response &&
111
- !!business_response.mercado_pago &&
112
- business_response.mercado_pago.active
113
- ) {
114
- injectMercadoPagoSecurity();
115
- }
116
- }
117
-
118
- async _checkout(data: any): Promise<any> {
119
- throw new Error(
120
- "The #checkout method should be implement in child classes.",
121
- );
122
- }
123
-
124
- _setCartTotal(total: string | number) {
125
- throw new Error(
126
- "The #setCartTotal method should be implement in child classes.",
127
- );
128
- }
129
-
130
- async _getCustomer(signal: AbortSignal | null = null) {
131
- if (!!this.#customerData) return this.#customerData!;
132
-
133
- this.#customerData = await registerOrFetchCustomer(
134
- this.baseUrl,
135
- this.apiKeyTonder,
136
- this.customer!,
137
- signal,
138
- );
139
- return this.#customerData!;
140
- }
141
-
142
- async _handleCheckout({
143
- card,
144
- payment_method,
145
- customer,
146
- isSandbox,
147
- }: {
148
- card?: string;
149
- payment_method?: string;
150
- customer: Record<string, any>;
151
- isSandbox?: boolean;
152
- }) {
153
- const { openpay_keys, reference, business } = this.merchantData!;
154
- const total = Number(this.cartTotal);
155
- try {
156
- let deviceSessionIdTonder;
157
- if (
158
- !deviceSessionIdTonder &&
159
- openpay_keys.merchant_id &&
160
- openpay_keys.public_key
161
- ) {
162
- deviceSessionIdTonder = await getOpenpayDeviceSessionID(
163
- openpay_keys.merchant_id,
164
- openpay_keys.public_key,
165
- isSandbox,
166
- this.abortController.signal,
167
- );
168
- }
169
-
170
- const { id, auth_token } = customer;
171
-
172
- const orderItems = {
173
- business: this.apiKeyTonder,
174
- client: auth_token,
175
- billing_address_id: null,
176
- shipping_address_id: null,
177
- amount: total,
178
- status: "A",
179
- reference: reference,
180
- is_oneclick: true,
181
- items: this.cartItems!,
182
- };
183
- const jsonResponseOrder = await createOrder(
184
- this.baseUrl,
185
- this.apiKeyTonder,
186
- orderItems,
187
- );
188
-
189
- // Create payment
190
- const now = new Date();
191
- const dateString = now.toISOString();
192
-
193
- const paymentItems = {
194
- business_pk: business.pk,
195
- client_id: id,
196
- amount: total,
197
- date: dateString,
198
- order_id: jsonResponseOrder.id,
199
- };
200
- const jsonResponsePayment = await createPayment(
201
- this.baseUrl,
202
- this.apiKeyTonder,
203
- paymentItems,
204
- );
205
-
206
- // Checkout router
207
- const routerItems = {
208
- name: this.firstName || "",
209
- last_name: this.lastName || "",
210
- email_client: this.email,
211
- phone_number: this.phone,
212
- return_url: this.returnUrl,
213
- id_product: "no_id",
214
- quantity_product: 1,
215
- id_ship: "0",
216
- instance_id_ship: "0",
217
- amount: total,
218
- title_ship: "shipping",
219
- description: "transaction",
220
- device_session_id: deviceSessionIdTonder ? deviceSessionIdTonder : null,
221
- token_id: "",
222
- order_id: jsonResponseOrder.id,
223
- business_id: business.pk,
224
- payment_id: jsonResponsePayment.pk,
225
- source: "sdk",
226
- metadata: this.metadata,
227
- browser_info: getBrowserInfo(),
228
- currency: this.currency!,
229
- ...(!!payment_method ? { payment_method } : { card }),
230
- };
231
-
232
- const jsonResponseRouter = await startCheckoutRouter(
233
- this.baseUrl,
234
- this.apiKeyTonder,
235
- routerItems,
236
- );
237
-
238
- if (jsonResponseRouter) {
239
- return jsonResponseRouter;
240
- } else {
241
- return false;
242
- }
243
- } catch (error) {
244
- console.log(error);
245
- throw error;
246
- }
247
- }
248
-
249
- async _fetchMerchantData() {
250
- try {
251
- if (!this.merchantData) {
252
- this.merchantData = await fetchBusiness(
253
- this.baseUrl,
254
- this.apiKeyTonder,
255
- this.abortController.signal,
256
- );
257
- }
258
- return this.merchantData;
259
- } catch (e) {
260
- return this.merchantData;
261
- }
262
- }
263
-
264
- #handleCustomer(customer: ICustomer) {
265
- if (!customer) return;
266
-
267
- this.firstName = customer?.firstName;
268
- this.lastName = customer?.lastName;
269
- this.country = customer?.country;
270
- this.address = customer?.street;
271
- this.city = customer?.city;
272
- this.state = customer?.state;
273
- this.postCode = customer?.postCode;
274
- this.email = customer?.email;
275
- this.phone = customer?.phone;
276
- this.customer = customer;
277
- }
278
-
279
- #setCartItems(items: IItem[]) {
280
- this.cartItems = items;
281
- }
282
-
283
- #handleMetadata(data: { metadata?: any }) {
284
- this.metadata = data?.metadata;
285
- }
286
-
287
- #handleCurrency(data: { currency?: string }) {
288
- this.currency = data?.currency;
289
- }
290
-
291
- #handleCard(data: { card?: ICardFields | string }) {
292
- this.card = data?.card;
293
- }
294
-
295
- // TODO: Make private after remove deprecated functions of liteCheckout
296
- async _handle3dsRedirect(
297
- response: ErrorResponse | StartCheckoutResponse | false | undefined,
298
- ) {
299
- const iframe =
300
- response && "next_action" in response
301
- ? response?.next_action?.iframe_resources?.iframe
302
- : null;
303
-
304
- if (iframe) {
305
- this.process3ds
306
- .loadIframe()!
307
- .then(() => {
308
- //TODO: Check if this will be necessary on the frontend side
309
- // after some the tests in production, since the 3DS process
310
- // doesn't works properly on the sandbox environment
311
- // setTimeout(() => {
312
- // process3ds.verifyTransactionStatus();
313
- // }, 10000);
314
- this.process3ds.verifyTransactionStatus();
315
- })
316
- .catch((error) => {
317
- console.log("Error loading iframe:", error);
318
- });
319
- } else {
320
- const redirectUrl = this.process3ds.getRedirectUrl();
321
- if (redirectUrl) {
322
- this.process3ds.redirectToChallenge();
323
- } else {
324
- return response;
325
- }
326
- }
327
- }
328
-
329
- async #resumeCheckout(response: any) {
330
- // Stop the routing process if the transaction is either hard declined or successful
331
- if (response?.decline?.error_type === "Hard") {
332
- return response;
333
- }
334
-
335
- if (["Success", "Authorized"].includes(response?.transaction_status)) {
336
- return response;
337
- }
338
-
339
- if (response) {
340
- const routerItems = {
341
- checkout_id: response.checkout?.id,
342
- };
343
-
344
- try {
345
- return await startCheckoutRouter(
346
- this.baseUrl,
347
- this.apiKeyTonder,
348
- routerItems,
349
- );
350
- } catch (error) {
351
- // throw error
352
- }
353
- return response;
354
- }
355
- }
356
- }
1
+ import { ThreeDSHandler } from "./3dsHandler";
2
+ import { ErrorResponse } from "./errorResponse";
3
+ import { fetchBusiness } from "../data/businessApi";
4
+ import { injectMercadoPagoSecurity } from "../helpers/mercadopago";
5
+ import { TONDER_URL_BY_MODE } from "../shared/constants/tonderUrl";
6
+ import {
7
+ createOrder,
8
+ createPayment,
9
+ startCheckoutRouter,
10
+ } from "../data/checkoutApi";
11
+ import { getOpenpayDeviceSessionID } from "../data/openPayApi";
12
+ import { getBrowserInfo } from "../helpers/utils";
13
+ import { registerOrFetchCustomer } from "../data/customerApi";
14
+ import { get } from "lodash";
15
+ import {
16
+ fetchCustomerCards,
17
+ removeCustomerCard,
18
+ saveCustomerCard,
19
+ } from "../data/cardApi";
20
+ import { fetchCustomerPaymentMethods } from "../data/paymentMethodApi";
21
+ import {Business, IConfigureCheckout, IInlineCheckoutBaseOptions} from "../types/commons";
22
+ import {ICustomer} from "../types/customer";
23
+ import {ICardFields, IItem, IProcessPaymentRequest, IStartCheckoutResponse} from "../types/checkout";
24
+ import {ICustomerCardsResponse, ISaveCardResponse, ISaveCardSkyflowRequest} from "../types/card";
25
+ import {IPaymentMethodResponse} from "../types/paymentMethod";
26
+ import {ITransaction} from "../types/transaction";
27
+ export class BaseInlineCheckout {
28
+ baseUrl = "";
29
+ cartTotal: string | number = "0";
30
+ process3ds: ThreeDSHandler;
31
+ mode?: "production" | "sandbox" | "stage" | "development" | undefined;
32
+ apiKeyTonder: string;
33
+ returnUrl?: string;
34
+ callBack?: ((response: IStartCheckoutResponse | Record<string, any>) => void) | undefined;
35
+ merchantData?: Business;
36
+ abortController: AbortController;
37
+
38
+ customer?: ICustomer | { email: string };
39
+
40
+ cartItems?: IItem[];
41
+ metadata = {};
42
+ card? = {};
43
+ currency?: string = "";
44
+
45
+ #customerData?: Record<string, any>;
46
+
47
+ constructor({
48
+ mode = "stage",
49
+ apiKey,
50
+ apiKeyTonder,
51
+ returnUrl,
52
+ callBack = () => {},
53
+ baseUrlTonder
54
+ }: IInlineCheckoutBaseOptions) {
55
+ this.apiKeyTonder = apiKeyTonder || apiKey || "";
56
+ this.returnUrl = returnUrl;
57
+ this.callBack = callBack;
58
+ this.mode = mode;
59
+ this.customer = {} as ICustomer
60
+ this.baseUrl = baseUrlTonder || TONDER_URL_BY_MODE[this.mode] || TONDER_URL_BY_MODE["stage"];
61
+ this.abortController = new AbortController();
62
+ this.process3ds = new ThreeDSHandler({
63
+ apiKey: apiKey,
64
+ baseUrl: this.baseUrl,
65
+ });
66
+ }
67
+
68
+ configureCheckout(data: IConfigureCheckout) {
69
+ if ("customer" in data) this.#handleCustomer(data["customer"]);
70
+ }
71
+
72
+ async verify3dsTransaction(): Promise<ITransaction | IStartCheckoutResponse | void> {
73
+ const result3ds = await this.process3ds.verifyTransactionStatus();
74
+ const resultCheckout = await this.#resumeCheckout(result3ds);
75
+ this.process3ds.setPayload(resultCheckout);
76
+ return this._handle3dsRedirect(resultCheckout);
77
+ }
78
+
79
+ payment(data: IProcessPaymentRequest): Promise<IStartCheckoutResponse> {
80
+ return new Promise(async (resolve, reject) => {
81
+ try {
82
+ this.#handleCustomer(data.customer);
83
+ this._setCartTotal(data.cart?.total);
84
+ this.#setCartItems(data.cart?.items);
85
+ this.#handleMetadata(data);
86
+ this.#handleCurrency(data);
87
+ this.#handleCard(data);
88
+ const response = await this._checkout(data);
89
+ this.process3ds.setPayload(response);
90
+ if (this.callBack) this.callBack!(response);
91
+ const payload = await this._handle3dsRedirect(response);
92
+ if (payload) {
93
+ resolve(response);
94
+ }
95
+ } catch (error) {
96
+ reject(error);
97
+ }
98
+ });
99
+ }
100
+
101
+ async _initializeCheckout() {
102
+ const business_response = await this._fetchMerchantData();
103
+
104
+ if (
105
+ !!business_response &&
106
+ !!business_response.mercado_pago &&
107
+ business_response.mercado_pago.active
108
+ ) {
109
+ injectMercadoPagoSecurity();
110
+ }
111
+ }
112
+
113
+ async _checkout(data: any): Promise<any> {
114
+ throw new Error(
115
+ "The #checkout method should be implement in child classes.",
116
+ );
117
+ }
118
+
119
+ _setCartTotal(total: string | number) {
120
+ throw new Error(
121
+ "The #setCartTotal method should be implement in child classes.",
122
+ );
123
+ }
124
+
125
+ async _getCustomer(signal: AbortSignal | null = null) {
126
+ if (!!this.#customerData) return this.#customerData!;
127
+
128
+ this.#customerData = await registerOrFetchCustomer(
129
+ this.baseUrl,
130
+ this.apiKeyTonder,
131
+ this.customer!,
132
+ signal,
133
+ );
134
+ return this.#customerData!;
135
+ }
136
+
137
+ async _handleCheckout({
138
+ card,
139
+ payment_method,
140
+ customer,
141
+ isSandbox,
142
+ // TODO: DEPRECATED
143
+ returnUrl: returnUrlData
144
+ }: {
145
+ card?: string;
146
+ payment_method?: string;
147
+ customer: Record<string, any>;
148
+ isSandbox?: boolean;
149
+ returnUrl?: string;
150
+ }) {
151
+ const { openpay_keys, reference, business } = this.merchantData!;
152
+ const total = Number(this.cartTotal);
153
+ try {
154
+ let deviceSessionIdTonder;
155
+ if (
156
+ !deviceSessionIdTonder &&
157
+ openpay_keys.merchant_id &&
158
+ openpay_keys.public_key
159
+ ) {
160
+ deviceSessionIdTonder = await getOpenpayDeviceSessionID(
161
+ openpay_keys.merchant_id,
162
+ openpay_keys.public_key,
163
+ isSandbox,
164
+ this.abortController.signal,
165
+ );
166
+ }
167
+
168
+ const { id, auth_token } = customer;
169
+
170
+ const orderItems = {
171
+ business: this.apiKeyTonder,
172
+ client: auth_token,
173
+ billing_address_id: null,
174
+ shipping_address_id: null,
175
+ amount: total,
176
+ status: "A",
177
+ reference: reference,
178
+ is_oneclick: true,
179
+ items: this.cartItems!,
180
+ };
181
+ const jsonResponseOrder = await createOrder(
182
+ this.baseUrl,
183
+ this.apiKeyTonder,
184
+ orderItems,
185
+ );
186
+
187
+ // Create payment
188
+ const now = new Date();
189
+ const dateString = now.toISOString();
190
+
191
+ const paymentItems = {
192
+ business_pk: business.pk,
193
+ client_id: id,
194
+ amount: total,
195
+ date: dateString,
196
+ order_id: jsonResponseOrder.id,
197
+ };
198
+ const jsonResponsePayment = await createPayment(
199
+ this.baseUrl,
200
+ this.apiKeyTonder,
201
+ paymentItems,
202
+ );
203
+
204
+ // Checkout router
205
+ const routerItems = {
206
+ name: get(this.customer, "firstName", get(this.customer, "name", "")),
207
+ last_name: get(
208
+ this.customer,
209
+ "lastName",
210
+ get(this.customer, "lastname", ""),
211
+ ),
212
+ email_client: get(this.customer, "email", ""),
213
+ phone_number: get(this.customer, "phone", ""),
214
+ return_url: returnUrlData || this.returnUrl,
215
+ id_product: "no_id",
216
+ quantity_product: 1,
217
+ id_ship: "0",
218
+ instance_id_ship: "0",
219
+ amount: total,
220
+ title_ship: "shipping",
221
+ description: "transaction",
222
+ device_session_id: deviceSessionIdTonder ? deviceSessionIdTonder : null,
223
+ token_id: "",
224
+ order_id: jsonResponseOrder.id,
225
+ business_id: business.pk,
226
+ payment_id: jsonResponsePayment.pk,
227
+ source: "sdk",
228
+ metadata: this.metadata,
229
+ browser_info: getBrowserInfo(),
230
+ currency: this.currency!,
231
+ ...(!!payment_method ? { payment_method } : { card }),
232
+ };
233
+
234
+ const jsonResponseRouter = await startCheckoutRouter(
235
+ this.baseUrl,
236
+ this.apiKeyTonder,
237
+ routerItems,
238
+ );
239
+
240
+ if (jsonResponseRouter) {
241
+ return jsonResponseRouter;
242
+ } else {
243
+ return false;
244
+ }
245
+ } catch (error) {
246
+ console.log(error);
247
+ throw error;
248
+ }
249
+ }
250
+
251
+ async _fetchMerchantData() {
252
+ try {
253
+ if (!this.merchantData) {
254
+ this.merchantData = await fetchBusiness(
255
+ this.baseUrl,
256
+ this.apiKeyTonder,
257
+ this.abortController.signal,
258
+ );
259
+ }
260
+ return this.merchantData;
261
+ } catch (e) {
262
+ return this.merchantData;
263
+ }
264
+ }
265
+
266
+ async _getCustomerCards(
267
+ authToken: string,
268
+ businessId: string | number,
269
+ ): Promise<ICustomerCardsResponse> {
270
+ return await fetchCustomerCards(this.baseUrl, authToken, businessId);
271
+ }
272
+
273
+ async _saveCustomerCard(
274
+ authToken: string,
275
+ businessId: string | number,
276
+ skyflowTokens: ISaveCardSkyflowRequest,
277
+ ): Promise<ISaveCardResponse> {
278
+ return await saveCustomerCard(
279
+ this.baseUrl,
280
+ authToken,
281
+ businessId,
282
+ skyflowTokens,
283
+ );
284
+ }
285
+
286
+ async _removeCustomerCard(
287
+ authToken: string,
288
+ businessId: string | number,
289
+ skyflowId: string,
290
+ ): Promise<string> {
291
+ return await removeCustomerCard(
292
+ this.baseUrl,
293
+ authToken,
294
+ skyflowId,
295
+ businessId,
296
+ );
297
+ }
298
+ async _fetchCustomerPaymentMethods(): Promise<IPaymentMethodResponse> {
299
+ return await fetchCustomerPaymentMethods(this.baseUrl, this.apiKeyTonder);
300
+ }
301
+
302
+ #handleCustomer(customer: ICustomer | { email: string }) {
303
+ if (!customer) return;
304
+
305
+ this.customer = customer;
306
+ }
307
+
308
+ #setCartItems(items: IItem[]) {
309
+ this.cartItems = items;
310
+ }
311
+
312
+ #handleMetadata(data: { metadata?: any }) {
313
+ this.metadata = data?.metadata;
314
+ }
315
+
316
+ #handleCurrency(data: { currency?: string }) {
317
+ this.currency = data?.currency;
318
+ }
319
+
320
+ #handleCard(data: { card?: ICardFields | string }) {
321
+ this.card = data?.card;
322
+ }
323
+
324
+ // TODO: Make private after remove deprecated functions of liteCheckout
325
+ async _handle3dsRedirect(
326
+ response: ITransaction | IStartCheckoutResponse | void,
327
+ ) {
328
+ const iframe =
329
+ response && "next_action" in response
330
+ ? response?.next_action?.iframe_resources?.iframe
331
+ : null;
332
+
333
+ if (iframe) {
334
+ this.process3ds
335
+ .loadIframe()!
336
+ .then(() => {
337
+ //TODO: Check if this will be necessary on the frontend side
338
+ // after some the tests in production, since the 3DS process
339
+ // doesn't works properly on the sandbox environment
340
+ // setTimeout(() => {
341
+ // process3ds.verifyTransactionStatus();
342
+ // }, 10000);
343
+ this.process3ds.verifyTransactionStatus();
344
+ })
345
+ .catch((error) => {
346
+ console.log("Error loading iframe:", error);
347
+ });
348
+ } else {
349
+ const redirectUrl = this.process3ds.getRedirectUrl();
350
+ if (redirectUrl) {
351
+ this.process3ds.redirectToChallenge();
352
+ } else {
353
+ return response;
354
+ }
355
+ }
356
+ }
357
+
358
+ async #resumeCheckout(response: any) {
359
+ // Stop the routing process if the transaction is either hard declined or successful
360
+ if (response?.decline?.error_type === "Hard") {
361
+ return response;
362
+ }
363
+
364
+ if (["Success", "Authorized"].includes(response?.transaction_status)) {
365
+ return response;
366
+ }
367
+
368
+ if (response) {
369
+ const routerItems = {
370
+ checkout_id: response.checkout?.id,
371
+ };
372
+
373
+ try {
374
+ return await startCheckoutRouter(
375
+ this.baseUrl,
376
+ this.apiKeyTonder,
377
+ routerItems,
378
+ );
379
+ } catch (error) {
380
+ // throw error
381
+ }
382
+ return response;
383
+ }
384
+ }
385
+ }