@tonder.io/ionic-lite-sdk 0.0.41-beta.1 → 0.0.42-beta.2

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