@tonder.io/ionic-lite-sdk 0.0.36-beta.1 → 0.0.38-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 (79) hide show
  1. package/README.md +427 -116
  2. package/dist/classes/BaseInlineCheckout.d.ts +47 -0
  3. package/dist/classes/liteCheckout.d.ts +25 -29
  4. package/dist/data/businessApi.d.ts +2 -0
  5. package/dist/data/cardApi.d.ts +4 -0
  6. package/dist/data/checkoutApi.d.ts +5 -0
  7. package/dist/data/customerApi.d.ts +2 -0
  8. package/dist/data/openPayApi.d.ts +1 -0
  9. package/dist/data/paymentMethodApi.d.ts +5 -0
  10. package/dist/data/skyflowApi.d.ts +1 -0
  11. package/dist/helpers/skyflow.d.ts +3 -0
  12. package/dist/helpers/utils.d.ts +8 -4
  13. package/dist/helpers/validations.d.ts +6 -0
  14. package/dist/index.d.ts +3 -1
  15. package/dist/index.js +1 -1
  16. package/dist/shared/catalog/paymentMethodsCatalog.d.ts +1 -0
  17. package/dist/shared/constants/messages.d.ts +11 -0
  18. package/dist/shared/constants/paymentMethodAPM.d.ts +62 -0
  19. package/dist/shared/constants/tonderUrl.d.ts +7 -0
  20. package/dist/types/card.d.ts +30 -0
  21. package/dist/types/checkout.d.ts +109 -0
  22. package/dist/types/commons.d.ts +42 -0
  23. package/dist/types/customer.d.ts +22 -0
  24. package/dist/types/liteInlineCheckout.d.ts +151 -0
  25. package/dist/types/paymentMethod.d.ts +22 -0
  26. package/dist/types/requests.d.ts +12 -3
  27. package/dist/types/transaction.d.ts +101 -0
  28. package/package.json +4 -1
  29. package/src/classes/BaseInlineCheckout.ts +387 -0
  30. package/src/classes/errorResponse.ts +1 -1
  31. package/src/classes/liteCheckout.ts +372 -356
  32. package/src/data/businessApi.ts +18 -0
  33. package/src/data/cardApi.ts +87 -0
  34. package/src/data/checkoutApi.ts +84 -0
  35. package/src/data/customerApi.ts +31 -0
  36. package/src/data/openPayApi.ts +12 -0
  37. package/src/data/paymentMethodApi.ts +37 -0
  38. package/src/data/skyflowApi.ts +20 -0
  39. package/src/helpers/mercadopago.ts +14 -14
  40. package/src/helpers/skyflow.ts +91 -0
  41. package/src/helpers/utils.ts +66 -266
  42. package/src/helpers/validations.ts +55 -0
  43. package/src/index.ts +9 -1
  44. package/src/shared/catalog/paymentMethodsCatalog.ts +248 -0
  45. package/src/shared/constants/messages.ts +11 -0
  46. package/src/shared/constants/paymentMethodAPM.ts +63 -0
  47. package/src/shared/constants/tonderUrl.ts +8 -0
  48. package/src/types/card.ts +35 -0
  49. package/src/types/checkout.ts +124 -0
  50. package/src/types/commons.ts +114 -67
  51. package/src/types/customer.ts +22 -0
  52. package/src/types/liteInlineCheckout.ts +216 -0
  53. package/src/types/paymentMethod.ts +24 -0
  54. package/src/types/requests.ts +12 -3
  55. package/src/types/transaction.ts +101 -0
  56. package/src/types/validations.d.ts +11 -0
  57. package/tests/classes/liteCheckout.test.ts +5 -5
  58. package/tests/methods/createOrder.test.ts +3 -4
  59. package/tests/methods/createPayment.test.ts +2 -3
  60. package/tests/methods/customerRegister.test.ts +3 -4
  61. package/tests/methods/getBusiness.test.ts +4 -5
  62. package/tests/methods/getCustomerCards.test.ts +6 -13
  63. package/tests/methods/registerCustomerCard.test.ts +2 -2
  64. package/tests/methods/startCheckoutRouter.test.ts +2 -2
  65. package/tests/methods/startCheckoutRouterFull.test.ts +2 -2
  66. package/tests/utils/defaultMock.ts +3 -2
  67. package/tests/utils/mockClasses.ts +7 -4
  68. package/types/classes/liteCheckout.d.ts +29 -0
  69. package/types/classes/liteCheckout.js +225 -0
  70. package/types/classes/liteCheckout.js.map +1 -0
  71. package/types/helpers/utils.d.ts +3 -0
  72. package/types/helpers/utils.js +27 -0
  73. package/types/helpers/utils.js.map +1 -0
  74. package/types/index.d.ts +2 -0
  75. package/types/index.js +6 -0
  76. package/types/index.js.map +1 -0
  77. package/tests/methods/getOpenpayDeviceSessionID.test.ts +0 -95
  78. package/tests/methods/getSkyflowToken.test.ts +0 -155
  79. package/tests/methods/getVaultToken.test.ts +0 -107
@@ -0,0 +1,387 @@
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
+ secureToken: string,
276
+ businessId: string | number,
277
+ skyflowTokens: ISaveCardSkyflowRequest,
278
+ ): Promise<ISaveCardResponse> {
279
+ return await saveCustomerCard(
280
+ this.baseUrl,
281
+ secureToken,
282
+ authToken,
283
+ businessId,
284
+ skyflowTokens,
285
+ );
286
+ }
287
+
288
+ async _removeCustomerCard(
289
+ authToken: string,
290
+ businessId: string | number,
291
+ skyflowId: string,
292
+ ): Promise<string> {
293
+ return await removeCustomerCard(
294
+ this.baseUrl,
295
+ authToken,
296
+ skyflowId,
297
+ businessId,
298
+ );
299
+ }
300
+ async _fetchCustomerPaymentMethods(): Promise<IPaymentMethodResponse> {
301
+ return await fetchCustomerPaymentMethods(this.baseUrl, this.apiKeyTonder);
302
+ }
303
+
304
+ #handleCustomer(customer: ICustomer | { email: string }) {
305
+ if (!customer) return;
306
+
307
+ this.customer = customer;
308
+ }
309
+
310
+ #setCartItems(items: IItem[]) {
311
+ this.cartItems = items;
312
+ }
313
+
314
+ #handleMetadata(data: { metadata?: any }) {
315
+ this.metadata = data?.metadata;
316
+ }
317
+
318
+ #handleCurrency(data: { currency?: string }) {
319
+ this.currency = data?.currency;
320
+ }
321
+
322
+ #handleCard(data: { card?: ICardFields | string }) {
323
+ this.card = data?.card;
324
+ }
325
+
326
+ // TODO: Make private after remove deprecated functions of liteCheckout
327
+ async _handle3dsRedirect(
328
+ response: ITransaction | IStartCheckoutResponse | void,
329
+ ) {
330
+ const iframe =
331
+ response && "next_action" in response
332
+ ? response?.next_action?.iframe_resources?.iframe
333
+ : null;
334
+
335
+ if (iframe) {
336
+ this.process3ds
337
+ .loadIframe()!
338
+ .then(() => {
339
+ //TODO: Check if this will be necessary on the frontend side
340
+ // after some the tests in production, since the 3DS process
341
+ // doesn't works properly on the sandbox environment
342
+ // setTimeout(() => {
343
+ // process3ds.verifyTransactionStatus();
344
+ // }, 10000);
345
+ this.process3ds.verifyTransactionStatus();
346
+ })
347
+ .catch((error) => {
348
+ console.log("Error loading iframe:", error);
349
+ });
350
+ } else {
351
+ const redirectUrl = this.process3ds.getRedirectUrl();
352
+ if (redirectUrl) {
353
+ this.process3ds.redirectToChallenge();
354
+ } else {
355
+ return response;
356
+ }
357
+ }
358
+ }
359
+
360
+ async #resumeCheckout(response: any) {
361
+ // Stop the routing process if the transaction is either hard declined or successful
362
+ if (response?.decline?.error_type === "Hard") {
363
+ return response;
364
+ }
365
+
366
+ if (["Success", "Authorized"].includes(response?.transaction_status)) {
367
+ return response;
368
+ }
369
+
370
+ if (response) {
371
+ const routerItems = {
372
+ checkout_id: response.checkout?.id,
373
+ };
374
+
375
+ try {
376
+ return await startCheckoutRouter(
377
+ this.baseUrl,
378
+ this.apiKeyTonder,
379
+ routerItems,
380
+ );
381
+ } catch (error) {
382
+ // throw error
383
+ }
384
+ return response;
385
+ }
386
+ }
387
+ }
@@ -1,4 +1,4 @@
1
- import { IErrorResponse } from "../types/responses";
1
+ import {IErrorResponse} from "../types/responses";
2
2
 
3
3
  export class ErrorResponse implements IErrorResponse {
4
4
  code?: string | undefined;