@tonder.io/ionic-lite-sdk 0.0.31-beta → 0.0.33-beta
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.
- package/.gitlab-ci.yml +28 -28
- package/README.md +202 -592
- package/dist/classes/3dsHandler.d.ts +36 -0
- package/dist/classes/liteCheckout.d.ts +13 -4
- package/dist/index.js +1 -1
- package/dist/types/requests.d.ts +3 -0
- package/dist/types/responses.d.ts +1 -0
- package/jest.config.ts +14 -14
- package/package.json +38 -38
- package/rollup.config.js +16 -16
- package/src/classes/3dsHandler.ts +254 -0
- package/src/classes/errorResponse.ts +16 -16
- package/src/classes/liteCheckout.ts +535 -462
- package/src/helpers/utils.ts +12 -12
- package/src/index.ts +4 -4
- package/src/types/commons.ts +62 -62
- package/src/types/requests.ts +93 -89
- package/src/types/responses.ts +188 -187
- package/src/types/skyflow.ts +17 -17
- package/tests/classes/liteCheckout.test.ts +57 -57
- package/tests/methods/createOrder.test.ts +142 -142
- package/tests/methods/createPayment.test.ts +122 -122
- package/tests/methods/customerRegister.test.ts +119 -119
- package/tests/methods/getBusiness.test.ts +115 -115
- package/tests/methods/getCustomerCards.test.ts +117 -117
- package/tests/methods/getOpenpayDeviceSessionID.test.ts +94 -94
- package/tests/methods/getSkyflowToken.test.ts +154 -154
- package/tests/methods/getVaultToken.test.ts +106 -106
- package/tests/methods/registerCustomerCard.test.ts +117 -117
- package/tests/methods/startCheckoutRouter.test.ts +119 -119
- package/tests/methods/startCheckoutRouterFull.test.ts +138 -138
- package/tests/utils/defaultMock.ts +20 -20
- package/tests/utils/mockClasses.ts +651 -649
- package/tsconfig.json +18 -18
|
@@ -1,462 +1,535 @@
|
|
|
1
|
-
import Skyflow from "skyflow-js";
|
|
2
|
-
import CollectContainer from "skyflow-js/types/core/external/collect/collect-container";
|
|
3
|
-
import CollectElement from "skyflow-js/types/core/external/collect/collect-element";
|
|
4
|
-
import { Business } from "../types/commons";
|
|
5
|
-
import { CreateOrderRequest, CreatePaymentRequest, RegisterCustomerCardRequest, StartCheckoutRequest, TokensRequest, StartCheckoutFullRequest } from "../types/requests";
|
|
6
|
-
import { GetBusinessResponse, CustomerRegisterResponse, CreateOrderResponse, CreatePaymentResponse, StartCheckoutResponse, GetVaultTokenResponse, IErrorResponse, GetCustomerCardsResponse, RegisterCustomerCardResponse } from "../types/responses";
|
|
7
|
-
import { ErrorResponse } from "./errorResponse";
|
|
8
|
-
import { getBrowserInfo } from "../helpers/utils";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
1
|
+
import Skyflow from "skyflow-js";
|
|
2
|
+
import CollectContainer from "skyflow-js/types/core/external/collect/collect-container";
|
|
3
|
+
import CollectElement from "skyflow-js/types/core/external/collect/collect-element";
|
|
4
|
+
import { Business } from "../types/commons";
|
|
5
|
+
import { CreateOrderRequest, CreatePaymentRequest, RegisterCustomerCardRequest, StartCheckoutRequest, TokensRequest, StartCheckoutFullRequest, StartCheckoutIdRequest } from "../types/requests";
|
|
6
|
+
import { GetBusinessResponse, CustomerRegisterResponse, CreateOrderResponse, CreatePaymentResponse, StartCheckoutResponse, GetVaultTokenResponse, IErrorResponse, GetCustomerCardsResponse, RegisterCustomerCardResponse } from "../types/responses";
|
|
7
|
+
import { ErrorResponse } from "./errorResponse";
|
|
8
|
+
import { getBrowserInfo } from "../helpers/utils";
|
|
9
|
+
import { ThreeDSHandler } from "./3dsHandler";
|
|
10
|
+
|
|
11
|
+
declare global {
|
|
12
|
+
interface Window {
|
|
13
|
+
OpenPay: any;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type LiteCheckoutConstructor = {
|
|
18
|
+
signal: AbortSignal;
|
|
19
|
+
baseUrlTonder: string;
|
|
20
|
+
apiKeyTonder: string;
|
|
21
|
+
successUrl?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export class LiteCheckout implements LiteCheckoutConstructor {
|
|
25
|
+
signal: AbortSignal;
|
|
26
|
+
baseUrlTonder: string;
|
|
27
|
+
apiKeyTonder: string;
|
|
28
|
+
process3ds: ThreeDSHandler;
|
|
29
|
+
successUrl?: string
|
|
30
|
+
|
|
31
|
+
constructor({
|
|
32
|
+
signal,
|
|
33
|
+
baseUrlTonder,
|
|
34
|
+
apiKeyTonder,
|
|
35
|
+
successUrl,
|
|
36
|
+
}: LiteCheckoutConstructor) {
|
|
37
|
+
this.baseUrlTonder = baseUrlTonder;
|
|
38
|
+
this.signal = signal;
|
|
39
|
+
this.apiKeyTonder = apiKeyTonder;
|
|
40
|
+
this.successUrl = successUrl;
|
|
41
|
+
|
|
42
|
+
this.process3ds = new ThreeDSHandler({
|
|
43
|
+
apiKey: this.apiKeyTonder,
|
|
44
|
+
baseUrl: this.baseUrlTonder,
|
|
45
|
+
successUrl: successUrl
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async getOpenpayDeviceSessionID(
|
|
50
|
+
merchant_id: string,
|
|
51
|
+
public_key: string,
|
|
52
|
+
is_sandbox: boolean
|
|
53
|
+
): Promise<string | ErrorResponse> {
|
|
54
|
+
try {
|
|
55
|
+
let openpay = await window.OpenPay;
|
|
56
|
+
openpay.setId(merchant_id);
|
|
57
|
+
openpay.setApiKey(public_key);
|
|
58
|
+
openpay.setSandboxMode(is_sandbox);
|
|
59
|
+
return await openpay.deviceData.setup({
|
|
60
|
+
signal: this.signal,
|
|
61
|
+
}) as string;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async getBusiness(): Promise<GetBusinessResponse | ErrorResponse> {
|
|
68
|
+
try {
|
|
69
|
+
const getBusiness = await fetch(
|
|
70
|
+
`${this.baseUrlTonder}/api/v1/payments/business/${this.apiKeyTonder}`,
|
|
71
|
+
{
|
|
72
|
+
headers: {
|
|
73
|
+
Authorization: `Token ${this.apiKeyTonder}`,
|
|
74
|
+
},
|
|
75
|
+
signal: this.signal,
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (getBusiness.ok) return (await getBusiness.json()) as Business;
|
|
80
|
+
|
|
81
|
+
throw await this.buildErrorResponse(getBusiness);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async verify3dsTransaction () {
|
|
88
|
+
const result3ds = await this.process3ds.verifyTransactionStatus()
|
|
89
|
+
const resultCheckout = await this.resumeCheckout(result3ds)
|
|
90
|
+
this.process3ds.setPayload(resultCheckout)
|
|
91
|
+
if (resultCheckout && 'is_route_finished' in resultCheckout && 'provider' in resultCheckout && resultCheckout.provider === 'tonder') {
|
|
92
|
+
return resultCheckout
|
|
93
|
+
}
|
|
94
|
+
return this.handle3dsRedirect(resultCheckout)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async resumeCheckout(response: any) {
|
|
98
|
+
if (["Failed", "Declined", "Cancelled"].includes(response?.status)) {
|
|
99
|
+
const routerItems = {
|
|
100
|
+
// TODO: Replace this with reponse.checkout_id
|
|
101
|
+
checkout_id: this.process3ds.getCurrentCheckoutId(),
|
|
102
|
+
};
|
|
103
|
+
const routerResponse = await this.handleCheckoutRouter(
|
|
104
|
+
routerItems
|
|
105
|
+
);
|
|
106
|
+
return routerResponse
|
|
107
|
+
}
|
|
108
|
+
return response
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async handle3dsRedirect(response: ErrorResponse | StartCheckoutResponse | false | undefined) {
|
|
112
|
+
const iframe = response && 'next_action' in response ? response?.next_action?.iframe_resources?.iframe:null
|
|
113
|
+
|
|
114
|
+
if (iframe) {
|
|
115
|
+
this.process3ds.loadIframe()!.then(() => {
|
|
116
|
+
//TODO: Check if this will be necessary on the frontend side
|
|
117
|
+
// after some the tests in production, since the 3DS process
|
|
118
|
+
// doesn't works properly on the sandbox environment
|
|
119
|
+
// setTimeout(() => {
|
|
120
|
+
// process3ds.verifyTransactionStatus();
|
|
121
|
+
// }, 10000);
|
|
122
|
+
this.process3ds.verifyTransactionStatus();
|
|
123
|
+
}).catch((error: any) => {
|
|
124
|
+
console.log('Error loading iframe:', error)
|
|
125
|
+
})
|
|
126
|
+
} else {
|
|
127
|
+
const redirectUrl = this.process3ds.getRedirectUrl()
|
|
128
|
+
if (redirectUrl) {
|
|
129
|
+
this.process3ds.redirectToChallenge()
|
|
130
|
+
} else {
|
|
131
|
+
return response;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async customerRegister(email: string): Promise<CustomerRegisterResponse | ErrorResponse> {
|
|
137
|
+
try {
|
|
138
|
+
const url = `${this.baseUrlTonder}/api/v1/customer/`;
|
|
139
|
+
const data = { email: email };
|
|
140
|
+
const response = await fetch(url, {
|
|
141
|
+
method: "POST",
|
|
142
|
+
headers: {
|
|
143
|
+
"Content-Type": "application/json",
|
|
144
|
+
Authorization: `Token ${this.apiKeyTonder}`,
|
|
145
|
+
},
|
|
146
|
+
signal: this.signal,
|
|
147
|
+
body: JSON.stringify(data),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (response.ok) return await response.json() as CustomerRegisterResponse;
|
|
151
|
+
throw await this.buildErrorResponse(response);
|
|
152
|
+
} catch (e) {
|
|
153
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async createOrder(orderItems: CreateOrderRequest): Promise<CreateOrderResponse | ErrorResponse> {
|
|
158
|
+
try {
|
|
159
|
+
const url = `${this.baseUrlTonder}/api/v1/orders/`;
|
|
160
|
+
const data = orderItems;
|
|
161
|
+
const response = await fetch(url, {
|
|
162
|
+
method: "POST",
|
|
163
|
+
headers: {
|
|
164
|
+
"Content-Type": "application/json",
|
|
165
|
+
Authorization: `Token ${this.apiKeyTonder}`,
|
|
166
|
+
},
|
|
167
|
+
body: JSON.stringify(data),
|
|
168
|
+
});
|
|
169
|
+
if (response.ok) return await response.json() as CreateOrderResponse;
|
|
170
|
+
throw await this.buildErrorResponse(response);
|
|
171
|
+
} catch (e) {
|
|
172
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async createPayment(paymentItems: CreatePaymentRequest): Promise<CreatePaymentResponse | ErrorResponse> {
|
|
177
|
+
try {
|
|
178
|
+
const url = `${this.baseUrlTonder}/api/v1/business/${paymentItems.business_pk}/payments/`;
|
|
179
|
+
const data = paymentItems;
|
|
180
|
+
const response = await fetch(url, {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: {
|
|
183
|
+
"Content-Type": "application/json",
|
|
184
|
+
Authorization: `Token ${this.apiKeyTonder}`,
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify(data),
|
|
187
|
+
});
|
|
188
|
+
if (response.ok) return await response.json() as CreatePaymentResponse;
|
|
189
|
+
throw await this.buildErrorResponse(response);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async handleCheckoutRouter(routerData: StartCheckoutRequest | StartCheckoutIdRequest){
|
|
195
|
+
try {
|
|
196
|
+
const url = `${this.baseUrlTonder}/api/v1/checkout-router/`;
|
|
197
|
+
const data = routerData;
|
|
198
|
+
const response = await fetch(url, {
|
|
199
|
+
method: "POST",
|
|
200
|
+
headers: {
|
|
201
|
+
"Content-Type": "application/json",
|
|
202
|
+
Authorization: `Token ${this.apiKeyTonder}`,
|
|
203
|
+
},
|
|
204
|
+
body: JSON.stringify(data),
|
|
205
|
+
});
|
|
206
|
+
if (response.ok) return await response.json() as StartCheckoutResponse;
|
|
207
|
+
throw await this.buildErrorResponse(response);
|
|
208
|
+
} catch (e) {
|
|
209
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async startCheckoutRouter(routerData: StartCheckoutRequest | StartCheckoutIdRequest): Promise<StartCheckoutResponse | ErrorResponse | undefined> {
|
|
214
|
+
const checkoutResult = await this.handleCheckoutRouter(routerData);
|
|
215
|
+
const payload = await this.init3DSRedirect(checkoutResult)
|
|
216
|
+
if(payload)
|
|
217
|
+
return checkoutResult;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async startCheckoutRouterFull(routerFullData: StartCheckoutFullRequest): Promise<StartCheckoutResponse | ErrorResponse | undefined> {
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
|
|
224
|
+
const {
|
|
225
|
+
order,
|
|
226
|
+
total,
|
|
227
|
+
customer,
|
|
228
|
+
skyflowTokens,
|
|
229
|
+
return_url,
|
|
230
|
+
isSandbox,
|
|
231
|
+
metadata,
|
|
232
|
+
currency
|
|
233
|
+
} = routerFullData;
|
|
234
|
+
|
|
235
|
+
const merchantResult = await this.getBusiness();
|
|
236
|
+
|
|
237
|
+
const customerResult : CustomerRegisterResponse | ErrorResponse = await this.customerRegister(customer.email);
|
|
238
|
+
|
|
239
|
+
if(customerResult && "auth_token" in customerResult && merchantResult && "reference" in merchantResult) {
|
|
240
|
+
|
|
241
|
+
const orderData: CreateOrderRequest = {
|
|
242
|
+
business: this.apiKeyTonder,
|
|
243
|
+
client: customerResult.auth_token,
|
|
244
|
+
billing_address_id: null,
|
|
245
|
+
shipping_address_id: null,
|
|
246
|
+
amount: total,
|
|
247
|
+
reference: merchantResult.reference,
|
|
248
|
+
is_oneclick: true,
|
|
249
|
+
items: order.items,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const orderResult = await this.createOrder(orderData);
|
|
253
|
+
|
|
254
|
+
const now = new Date();
|
|
255
|
+
|
|
256
|
+
const dateString = now.toISOString();
|
|
257
|
+
|
|
258
|
+
if("id" in orderResult && "id" in customerResult && "business" in merchantResult) {
|
|
259
|
+
|
|
260
|
+
const paymentItems: CreatePaymentRequest = {
|
|
261
|
+
business_pk: merchantResult.business.pk,
|
|
262
|
+
amount: total,
|
|
263
|
+
date: dateString,
|
|
264
|
+
order_id: orderResult.id,
|
|
265
|
+
client_id: customerResult.id
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const paymentResult = await this.createPayment(
|
|
269
|
+
paymentItems
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
let deviceSessionIdTonder: any;
|
|
273
|
+
|
|
274
|
+
const { openpay_keys, business } = merchantResult
|
|
275
|
+
|
|
276
|
+
if (openpay_keys.merchant_id && openpay_keys.public_key) {
|
|
277
|
+
deviceSessionIdTonder = await this.getOpenpayDeviceSessionID(
|
|
278
|
+
openpay_keys.merchant_id,
|
|
279
|
+
openpay_keys.public_key,
|
|
280
|
+
isSandbox
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const routerItems: StartCheckoutRequest = {
|
|
285
|
+
card: skyflowTokens,
|
|
286
|
+
name: customer.name,
|
|
287
|
+
last_name: customer.lastname,
|
|
288
|
+
email_client: customer.email,
|
|
289
|
+
phone_number: customer.phone,
|
|
290
|
+
return_url: return_url,
|
|
291
|
+
id_product: "no_id",
|
|
292
|
+
quantity_product: 1,
|
|
293
|
+
id_ship: "0",
|
|
294
|
+
instance_id_ship: "0",
|
|
295
|
+
amount: total,
|
|
296
|
+
title_ship: "shipping",
|
|
297
|
+
description: "transaction",
|
|
298
|
+
device_session_id: deviceSessionIdTonder ? deviceSessionIdTonder : null,
|
|
299
|
+
token_id: "",
|
|
300
|
+
order_id: ("id" in orderResult) && orderResult.id,
|
|
301
|
+
business_id: business.pk,
|
|
302
|
+
payment_id: ("pk" in paymentResult) && paymentResult.pk,
|
|
303
|
+
source: 'sdk',
|
|
304
|
+
metadata: metadata,
|
|
305
|
+
browser_info: getBrowserInfo(),
|
|
306
|
+
currency: currency
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const checkoutResult = await this.handleCheckoutRouter(routerItems);
|
|
310
|
+
const payload = await this.init3DSRedirect(checkoutResult)
|
|
311
|
+
if(payload)
|
|
312
|
+
return checkoutResult;
|
|
313
|
+
} else {
|
|
314
|
+
|
|
315
|
+
throw new ErrorResponse({
|
|
316
|
+
code: "500",
|
|
317
|
+
body: orderResult as any,
|
|
318
|
+
name: "Keys error",
|
|
319
|
+
message: "Order response errors"
|
|
320
|
+
} as IErrorResponse)
|
|
321
|
+
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
} else {
|
|
325
|
+
|
|
326
|
+
throw new ErrorResponse({
|
|
327
|
+
code: "500",
|
|
328
|
+
body: merchantResult as any,
|
|
329
|
+
name: "Keys error",
|
|
330
|
+
message: "Merchant or customer reposne errors"
|
|
331
|
+
} as IErrorResponse)
|
|
332
|
+
|
|
333
|
+
}
|
|
334
|
+
} catch (e) {
|
|
335
|
+
|
|
336
|
+
throw this.buildErrorResponseFromCatch(e);
|
|
337
|
+
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async init3DSRedirect(checkoutResult: ErrorResponse | StartCheckoutResponse){
|
|
342
|
+
this.process3ds.setPayload(checkoutResult)
|
|
343
|
+
this.process3ds.saveCheckoutId(checkoutResult && 'checkout_id' in checkoutResult ? checkoutResult.checkout_id:"")
|
|
344
|
+
return await this.handle3dsRedirect(checkoutResult)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async getSkyflowTokens({
|
|
348
|
+
vault_id,
|
|
349
|
+
vault_url,
|
|
350
|
+
data,
|
|
351
|
+
}: TokensRequest): Promise<any | ErrorResponse> {
|
|
352
|
+
const skyflow = Skyflow.init({
|
|
353
|
+
vaultID: vault_id,
|
|
354
|
+
vaultURL: vault_url,
|
|
355
|
+
getBearerToken: async () => await this.getVaultToken(),
|
|
356
|
+
options: {
|
|
357
|
+
logLevel: Skyflow.LogLevel.ERROR,
|
|
358
|
+
env: Skyflow.Env.DEV,
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const collectContainer: CollectContainer = skyflow.container(
|
|
363
|
+
Skyflow.ContainerType.COLLECT
|
|
364
|
+
) as CollectContainer;
|
|
365
|
+
|
|
366
|
+
const fieldPromises = await this.getFieldsPromise(data, collectContainer);
|
|
367
|
+
|
|
368
|
+
const result = await Promise.all(fieldPromises);
|
|
369
|
+
|
|
370
|
+
const mountFail = result.some((item: boolean) => !item);
|
|
371
|
+
|
|
372
|
+
if (mountFail) {
|
|
373
|
+
throw this.buildErrorResponseFromCatch(Error("Ocurrió un error al montar los campos de la tarjeta"));
|
|
374
|
+
} else {
|
|
375
|
+
try {
|
|
376
|
+
const collectResponseSkyflowTonder = await collectContainer.collect() as any;
|
|
377
|
+
if (collectResponseSkyflowTonder) return collectResponseSkyflowTonder["records"][0]["fields"];
|
|
378
|
+
throw this.buildErrorResponseFromCatch(Error("Por favor, verifica todos los campos de tu tarjeta"))
|
|
379
|
+
} catch (error) {
|
|
380
|
+
throw this.buildErrorResponseFromCatch(error);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async getVaultToken(): Promise<string> {
|
|
386
|
+
try {
|
|
387
|
+
const response = await fetch(`${this.baseUrlTonder}/api/v1/vault-token/`, {
|
|
388
|
+
method: "GET",
|
|
389
|
+
headers: {
|
|
390
|
+
Authorization: `Token ${this.apiKeyTonder}`,
|
|
391
|
+
},
|
|
392
|
+
signal: this.signal,
|
|
393
|
+
});
|
|
394
|
+
if (response.ok) return (await response.json() as GetVaultTokenResponse)?.token;
|
|
395
|
+
throw new Error(`HTTPCODE: ${response.status}`)
|
|
396
|
+
} catch (e) {
|
|
397
|
+
throw new Error(`Failed to retrieve bearer token; ${typeof e == "string" ? e : (e as Error).message}`)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async getFieldsPromise(data: any, collectContainer: CollectContainer): Promise<Promise<boolean>[]> {
|
|
402
|
+
const fields = await this.getFields(data, collectContainer);
|
|
403
|
+
if (!fields) return [];
|
|
404
|
+
|
|
405
|
+
return fields.map((field: { element: CollectElement, key: string }) => {
|
|
406
|
+
return new Promise((resolve) => {
|
|
407
|
+
const div = document.createElement("div");
|
|
408
|
+
div.hidden = true;
|
|
409
|
+
div.id = `id-${field.key}`;
|
|
410
|
+
document.querySelector(`body`)?.appendChild(div);
|
|
411
|
+
setTimeout(() => {
|
|
412
|
+
field.element.mount(`#id-${field.key}`);
|
|
413
|
+
setInterval(() => {
|
|
414
|
+
if (field.element.isMounted()) {
|
|
415
|
+
const value = data[field.key];
|
|
416
|
+
field.element.update({ value: value });
|
|
417
|
+
return resolve(field.element.isMounted());
|
|
418
|
+
}
|
|
419
|
+
}, 120);
|
|
420
|
+
}, 120);
|
|
421
|
+
});
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async registerCustomerCard(customerToken: string, data: RegisterCustomerCardRequest): Promise<RegisterCustomerCardResponse | ErrorResponse> {
|
|
426
|
+
try {
|
|
427
|
+
const response = await fetch(`${this.baseUrlTonder}/api/v1/cards/`, {
|
|
428
|
+
method: 'POST',
|
|
429
|
+
headers: {
|
|
430
|
+
'Authorization': `Token ${customerToken}`,
|
|
431
|
+
'Content-Type': 'application/json'
|
|
432
|
+
},
|
|
433
|
+
signal: this.signal,
|
|
434
|
+
body: JSON.stringify(data)
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
if (response.ok) return await response.json() as RegisterCustomerCardResponse;
|
|
438
|
+
throw await this.buildErrorResponse(response);
|
|
439
|
+
} catch (error) {
|
|
440
|
+
throw this.buildErrorResponseFromCatch(error);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
async getCustomerCards(customerToken: string, query: string = ""): Promise<GetCustomerCardsResponse | ErrorResponse> {
|
|
445
|
+
try {
|
|
446
|
+
const response = await fetch(`${this.baseUrlTonder}/api/v1/cards/${query}`, {
|
|
447
|
+
method: 'GET',
|
|
448
|
+
headers: {
|
|
449
|
+
'Authorization': `Token ${customerToken}`,
|
|
450
|
+
'Content-Type': 'application/json'
|
|
451
|
+
},
|
|
452
|
+
signal: this.signal,
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
if (response.ok) return await response.json() as GetCustomerCardsResponse;
|
|
456
|
+
throw await this.buildErrorResponse(response);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
throw this.buildErrorResponseFromCatch(error);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async deleteCustomerCard(customerToken: string, skyflowId: string = ""): Promise<Boolean | ErrorResponse> {
|
|
463
|
+
try {
|
|
464
|
+
const response = await fetch(`${this.baseUrlTonder}/api/v1/cards/${skyflowId}`, {
|
|
465
|
+
method: 'DELETE',
|
|
466
|
+
headers: {
|
|
467
|
+
'Authorization': `Token ${customerToken}`,
|
|
468
|
+
'Content-Type': 'application/json'
|
|
469
|
+
},
|
|
470
|
+
signal: this.signal,
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
if (response.ok) return true;
|
|
474
|
+
throw await this.buildErrorResponse(response);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
throw this.buildErrorResponseFromCatch(error);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
private buildErrorResponseFromCatch(e: any): ErrorResponse {
|
|
481
|
+
|
|
482
|
+
const error = new ErrorResponse({
|
|
483
|
+
code: e?.status ? e.status : e.code,
|
|
484
|
+
body: e?.body,
|
|
485
|
+
name: e ? typeof e == "string" ? "catch" : (e as Error).name : "Error",
|
|
486
|
+
message: e ? (typeof e == "string" ? e : (e as Error).message) : "Error",
|
|
487
|
+
stack: typeof e == "string" ? undefined : (e as Error).stack,
|
|
488
|
+
})
|
|
489
|
+
|
|
490
|
+
return error;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private async buildErrorResponse(
|
|
494
|
+
response: Response,
|
|
495
|
+
stack: string | undefined = undefined
|
|
496
|
+
): Promise<ErrorResponse> {
|
|
497
|
+
|
|
498
|
+
let body, status, message = "Error";
|
|
499
|
+
|
|
500
|
+
if(response && "json" in response) {
|
|
501
|
+
body = await response?.json();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if(response && "status" in response) {
|
|
505
|
+
status = response.status.toString();
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
if(response && "text" in response) {
|
|
509
|
+
message = await response.text();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const error = new ErrorResponse({
|
|
513
|
+
code: status,
|
|
514
|
+
body: body,
|
|
515
|
+
name: status,
|
|
516
|
+
message: message,
|
|
517
|
+
stack,
|
|
518
|
+
} as IErrorResponse)
|
|
519
|
+
|
|
520
|
+
return error;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
private async getFields(data: any, collectContainer: CollectContainer): Promise<{ element: CollectElement, key: string }[]> {
|
|
524
|
+
return await Promise.all(
|
|
525
|
+
Object.keys(data).map(async (key) => {
|
|
526
|
+
const cardHolderNameElement = await collectContainer.create({
|
|
527
|
+
table: "cards",
|
|
528
|
+
column: key,
|
|
529
|
+
type: Skyflow.ElementType.INPUT_FIELD,
|
|
530
|
+
});
|
|
531
|
+
return { element: cardHolderNameElement, key: key };
|
|
532
|
+
})
|
|
533
|
+
)
|
|
534
|
+
}
|
|
535
|
+
}
|