@tonder.io/ionic-lite-sdk 1.0.1 → 1.0.2-beta.TEC-1207.53fb846
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/README.md +38 -3
- package/dist/classes/3dsHandler.d.ts +26 -2
- package/dist/classes/BaseInlineCheckout.d.ts +12 -9
- package/dist/classes/liteCheckout.d.ts +1 -1
- package/dist/data/businessApi.d.ts +1 -1
- package/dist/data/coreApi.d.ts +15 -0
- package/dist/helpers/corePayment.d.ts +60 -0
- package/dist/helpers/safeUrl.d.ts +15 -0
- package/dist/helpers/sanitize3dsMarkup.d.ts +18 -0
- package/dist/helpers/utils.d.ts +1 -0
- package/dist/helpers/validations.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/shared/constants/apiEndpoints.d.ts +2 -0
- package/dist/shared/utils/appError.d.ts +9 -0
- package/dist/types/commons.d.ts +5 -0
- package/dist/types/core.d.ts +28 -0
- package/jest.config.ts +11 -1
- package/package.json +1 -1
- package/src/classes/3dsHandler.ts +134 -30
- package/src/classes/BaseInlineCheckout.ts +128 -16
- package/src/classes/liteCheckout.ts +9 -2
- package/src/data/businessApi.ts +13 -6
- package/src/data/coreApi.ts +47 -0
- package/src/helpers/corePayment.ts +211 -0
- package/src/helpers/safeUrl.ts +26 -0
- package/src/helpers/sanitize3dsMarkup.ts +133 -0
- package/src/helpers/utils.ts +4 -0
- package/src/helpers/validations.ts +9 -0
- package/src/shared/constants/apiEndpoints.ts +2 -0
- package/src/shared/constants/messages.ts +4 -4
- package/src/shared/utils/appError.ts +28 -11
- package/src/types/commons.ts +5 -0
- package/src/types/core.ts +30 -0
- package/tests/coreBusinessConfig.test.ts +100 -0
- package/tests/corePayment.test.ts +393 -0
- package/tests/corePaymentCheckout.test.ts +668 -0
- package/tests/coreStartupFailure.test.ts +70 -0
- package/tests/redirectToChallenge.test.ts +149 -0
- package/tests/sanitize3dsMarkup.test.ts +248 -0
- package/tests/secureTokenBoundary.test.ts +81 -0
- package/tests/threeDsApiKey.test.ts +41 -0
- package/tests/threeDsCore.test.ts +444 -0
- package/tests/threeDsPolling.test.ts +45 -0
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
import { LiteCheckout } from "../src/classes/liteCheckout";
|
|
2
|
+
import { getBrowserInfo } from "../src/helpers/utils";
|
|
3
|
+
import { ErrorKeyEnum } from "../src/shared/enum/ErrorKeyEnum";
|
|
4
|
+
|
|
5
|
+
const API_KEY = "merchant-api-key";
|
|
6
|
+
const LEGACY = "https://stage.tonder.io";
|
|
7
|
+
const CORE = "https://api-stage.tonder.io";
|
|
8
|
+
const PUBLIC_ID = "0192b8a0-0000-7000-8000-000000000001";
|
|
9
|
+
|
|
10
|
+
const ITEMS = [
|
|
11
|
+
{
|
|
12
|
+
name: "Shirt",
|
|
13
|
+
description: "Blue",
|
|
14
|
+
quantity: 1,
|
|
15
|
+
price_unit: 100,
|
|
16
|
+
discount: 0,
|
|
17
|
+
taxes: 0,
|
|
18
|
+
product_reference: "sku-1",
|
|
19
|
+
amount_total: 100,
|
|
20
|
+
},
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const CUSTOMER = { firstName: "Ana", lastName: "Lopez", email: "ana@example.com", phone: "555" };
|
|
24
|
+
|
|
25
|
+
function config(flag: boolean | undefined) {
|
|
26
|
+
return {
|
|
27
|
+
business: { pk: 7, name: "Shop" },
|
|
28
|
+
openpay_keys: { merchant_id: "", public_key: "" },
|
|
29
|
+
mercado_pago: { active: false },
|
|
30
|
+
vault_id: "vault",
|
|
31
|
+
vault_url: "https://vault.example.com",
|
|
32
|
+
reference: "TNDR-1",
|
|
33
|
+
...(flag === undefined ? {} : { core_payments_enabled: flag }),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const CORE_SUCCESS = {
|
|
38
|
+
id: PUBLIC_ID,
|
|
39
|
+
status: "Success",
|
|
40
|
+
amount: 100,
|
|
41
|
+
currency: "MXN",
|
|
42
|
+
provider: "tonder",
|
|
43
|
+
status_code: 201,
|
|
44
|
+
payment_id: 55,
|
|
45
|
+
transaction_id: "9",
|
|
46
|
+
transaction_status: "Success",
|
|
47
|
+
psp_response: { id: "psp-1" },
|
|
48
|
+
routing: { checkout_id: "chk-1", is_route_finished: true },
|
|
49
|
+
next_action: { verify_transaction_status_url: `/transactions/v1/${PUBLIC_ID}` },
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const LEGACY_ROUTER_BODY = {
|
|
53
|
+
status: "200",
|
|
54
|
+
message: "Success",
|
|
55
|
+
checkout_id: "legacy-chk",
|
|
56
|
+
transaction_status: "Success",
|
|
57
|
+
transaction_id: 3,
|
|
58
|
+
payment_id: 12,
|
|
59
|
+
provider: "tonder",
|
|
60
|
+
next_action: {},
|
|
61
|
+
psp_response: {},
|
|
62
|
+
actions: [],
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
type Reply = { status: number; body: unknown };
|
|
66
|
+
|
|
67
|
+
function reply({ status, body }: Reply) {
|
|
68
|
+
return {
|
|
69
|
+
ok: status >= 200 && status < 300,
|
|
70
|
+
status,
|
|
71
|
+
json: async () => body,
|
|
72
|
+
clone() {
|
|
73
|
+
return this;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type Routes = {
|
|
79
|
+
flag?: boolean;
|
|
80
|
+
process?: Reply;
|
|
81
|
+
router?: Reply;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
function installFetch({ flag, process, router }: Routes): jest.Mock {
|
|
85
|
+
const mock = jest.fn(async (url: string) => {
|
|
86
|
+
if (url === `${CORE}/transactions/v1/business/config`) {
|
|
87
|
+
return reply({ status: 200, body: config(flag) });
|
|
88
|
+
}
|
|
89
|
+
if (url === `${CORE}/transactions/v1/process`) {
|
|
90
|
+
return reply(process ?? { status: 201, body: CORE_SUCCESS });
|
|
91
|
+
}
|
|
92
|
+
if (url === `${LEGACY}/api/v1/customer/`) {
|
|
93
|
+
return reply({ status: 200, body: { id: 1, auth_token: "cust-token" } });
|
|
94
|
+
}
|
|
95
|
+
if (url === `${LEGACY}/api/v1/orders/`) return reply({ status: 201, body: { id: 21 } });
|
|
96
|
+
if (url === `${LEGACY}/api/v1/business/7/payments/`) {
|
|
97
|
+
return reply({ status: 201, body: { pk: 31 } });
|
|
98
|
+
}
|
|
99
|
+
if (url === `${LEGACY}/api/v1/checkout-router/`) {
|
|
100
|
+
return reply(router ?? { status: 201, body: LEGACY_ROUTER_BODY });
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`unexpected request to ${url}`);
|
|
103
|
+
});
|
|
104
|
+
(global as any).fetch = mock;
|
|
105
|
+
return mock;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function callsTo(mock: jest.Mock, url: string) {
|
|
109
|
+
return mock.mock.calls.filter(([called]) => called === url);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function paymentData(overrides: Record<string, unknown> = {}) {
|
|
113
|
+
return {
|
|
114
|
+
customer: CUSTOMER,
|
|
115
|
+
cart: { total: 100, items: ITEMS },
|
|
116
|
+
currency: "MXN",
|
|
117
|
+
metadata: { order_id: "o-1" },
|
|
118
|
+
card: "sf-saved",
|
|
119
|
+
...overrides,
|
|
120
|
+
} as any;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function buildCheckout(callBack = jest.fn()) {
|
|
124
|
+
const checkout = new LiteCheckout({
|
|
125
|
+
mode: "stage",
|
|
126
|
+
apiKey: API_KEY,
|
|
127
|
+
returnUrl: "https://shop.example.com/done",
|
|
128
|
+
callBack,
|
|
129
|
+
});
|
|
130
|
+
return { checkout, callBack };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
describe("card payment with the flag ON", () => {
|
|
134
|
+
it("charges through the core with today's key and no legacy order, payment or router", async () => {
|
|
135
|
+
const mock = installFetch({ flag: true });
|
|
136
|
+
const { checkout } = buildCheckout();
|
|
137
|
+
|
|
138
|
+
await checkout.injectCheckout();
|
|
139
|
+
await checkout.payment(paymentData());
|
|
140
|
+
|
|
141
|
+
const [url, init] = callsTo(mock, `${CORE}/transactions/v1/process`)[0];
|
|
142
|
+
expect(url).toBe(`${CORE}/transactions/v1/process`);
|
|
143
|
+
expect(init.method).toBe("POST");
|
|
144
|
+
expect(init.headers).toMatchObject({
|
|
145
|
+
Authorization: `Token ${API_KEY}`,
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
"X-App-Origin": "sdk/ionic",
|
|
148
|
+
});
|
|
149
|
+
expect(init.headers["X-Request-Id"]).toEqual(expect.any(String));
|
|
150
|
+
expect(JSON.parse(init.body)).toEqual({
|
|
151
|
+
operation_type: "payment",
|
|
152
|
+
amount: 100,
|
|
153
|
+
currency: "MXN",
|
|
154
|
+
client_reference: "TNDR-1",
|
|
155
|
+
customer: { name: "Ana Lopez", email: "ana@example.com", phone: "555" },
|
|
156
|
+
payment_method: { type: "CARD", token: "sf-saved" },
|
|
157
|
+
metadata: { order_id: "o-1" },
|
|
158
|
+
return_url: "https://shop.example.com/done",
|
|
159
|
+
items: ITEMS,
|
|
160
|
+
browser_info: getBrowserInfo(),
|
|
161
|
+
});
|
|
162
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/orders/`)).toHaveLength(0);
|
|
163
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/business/7/payments/`)).toHaveLength(0);
|
|
164
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("sends the merchant's order reference as the client reference", async () => {
|
|
168
|
+
const mock = installFetch({ flag: true });
|
|
169
|
+
const { checkout } = buildCheckout();
|
|
170
|
+
|
|
171
|
+
await checkout.payment(paymentData({ order_reference: "ORDER-77" }));
|
|
172
|
+
|
|
173
|
+
const body = JSON.parse(callsTo(mock, `${CORE}/transactions/v1/process`)[0][1].body);
|
|
174
|
+
expect(body.client_reference).toBe("ORDER-77");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("sends the vault tokens of a new card", async () => {
|
|
178
|
+
const mock = installFetch({ flag: true });
|
|
179
|
+
const { checkout } = buildCheckout();
|
|
180
|
+
const tokens = {
|
|
181
|
+
card_number: "tok-number",
|
|
182
|
+
cvv: "tok-cvv",
|
|
183
|
+
expiration_month: "tok-month",
|
|
184
|
+
expiration_year: "tok-year",
|
|
185
|
+
cardholder_name: "tok-name",
|
|
186
|
+
skyflow_id: "sf-new",
|
|
187
|
+
};
|
|
188
|
+
(checkout as any).mountedElementsByContext.set("create", {
|
|
189
|
+
elements: [],
|
|
190
|
+
container: {
|
|
191
|
+
container: { collect: async () => ({ records: [{ fields: tokens }] }) },
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
await checkout.payment(paymentData({ card: undefined }));
|
|
196
|
+
|
|
197
|
+
const body = JSON.parse(callsTo(mock, `${CORE}/transactions/v1/process`)[0][1].body);
|
|
198
|
+
expect(body.payment_method).toEqual({
|
|
199
|
+
type: "CARD",
|
|
200
|
+
card_number: "tok-number",
|
|
201
|
+
cvv: "tok-cvv",
|
|
202
|
+
expiration_month: "tok-month",
|
|
203
|
+
expiration_year: "tok-year",
|
|
204
|
+
cardholder_name: "tok-name",
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("resolves payment() and the callback with the rebuilt checkout-router body", async () => {
|
|
209
|
+
installFetch({ flag: true });
|
|
210
|
+
const { checkout, callBack } = buildCheckout();
|
|
211
|
+
|
|
212
|
+
const result = await checkout.payment(paymentData());
|
|
213
|
+
|
|
214
|
+
expect(result).toMatchObject({
|
|
215
|
+
status: "200",
|
|
216
|
+
message: "Success",
|
|
217
|
+
checkout_id: "chk-1",
|
|
218
|
+
transaction_status: "Success",
|
|
219
|
+
transaction_id: 9,
|
|
220
|
+
payment_id: 55,
|
|
221
|
+
provider: "tonder",
|
|
222
|
+
});
|
|
223
|
+
expect(callBack).toHaveBeenCalledTimes(1);
|
|
224
|
+
expect(callBack).toHaveBeenCalledWith(result);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("uses a new request id for every payment", async () => {
|
|
228
|
+
const mock = installFetch({ flag: true });
|
|
229
|
+
const { checkout } = buildCheckout();
|
|
230
|
+
|
|
231
|
+
await checkout.payment(paymentData());
|
|
232
|
+
await checkout.payment(paymentData());
|
|
233
|
+
|
|
234
|
+
const ids = callsTo(mock, `${CORE}/transactions/v1/process`).map(
|
|
235
|
+
([, init]) => init.headers["X-Request-Id"],
|
|
236
|
+
);
|
|
237
|
+
expect(ids).toHaveLength(2);
|
|
238
|
+
expect(ids[0]).not.toBe(ids[1]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("rejects a declined payment with the body the router used to answer", async () => {
|
|
242
|
+
installFetch({
|
|
243
|
+
flag: true,
|
|
244
|
+
process: {
|
|
245
|
+
status: 200,
|
|
246
|
+
body: {
|
|
247
|
+
...CORE_SUCCESS,
|
|
248
|
+
status: "Declined",
|
|
249
|
+
status_code: 200,
|
|
250
|
+
transaction_status: "Declined",
|
|
251
|
+
decline_code: "05",
|
|
252
|
+
decline_reason: "Do not honor",
|
|
253
|
+
next_action: undefined,
|
|
254
|
+
routing: {
|
|
255
|
+
checkout_id: "chk-1",
|
|
256
|
+
is_route_finished: true,
|
|
257
|
+
decline_type: "Soft",
|
|
258
|
+
rejected_psp_responses: [{ id: "r1" }],
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
const { checkout, callBack } = buildCheckout();
|
|
264
|
+
|
|
265
|
+
const failure = await checkout.payment(paymentData()).catch((e) => e);
|
|
266
|
+
|
|
267
|
+
expect(failure.code).toBe(ErrorKeyEnum.PAYMENT_PROCESS_ERROR);
|
|
268
|
+
expect(failure.statusCode).toBe(404);
|
|
269
|
+
expect(failure.originalError).toEqual({
|
|
270
|
+
status: 404,
|
|
271
|
+
body: expect.objectContaining({
|
|
272
|
+
status: "404",
|
|
273
|
+
message: "Do not honor",
|
|
274
|
+
transaction_status: "Declined",
|
|
275
|
+
decline_code: "05",
|
|
276
|
+
psp_response: [{ id: "r1" }],
|
|
277
|
+
}),
|
|
278
|
+
});
|
|
279
|
+
expect(callBack).not.toHaveBeenCalled();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("rejects the core's normalized 402 like a route that ended with no transaction", async () => {
|
|
283
|
+
installFetch({
|
|
284
|
+
flag: true,
|
|
285
|
+
process: {
|
|
286
|
+
status: 402,
|
|
287
|
+
body: { error: { code: "payment_declined", message: "x", type: "business_error" } },
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
const { checkout } = buildCheckout();
|
|
291
|
+
|
|
292
|
+
const failure = await checkout.payment(paymentData()).catch((e) => e);
|
|
293
|
+
|
|
294
|
+
expect(failure.code).toBe(ErrorKeyEnum.PAYMENT_PROCESS_ERROR);
|
|
295
|
+
expect(failure.statusCode).toBe(404);
|
|
296
|
+
expect(failure.originalError.body).toEqual({
|
|
297
|
+
status: "404",
|
|
298
|
+
message: "Error",
|
|
299
|
+
psp_response: [],
|
|
300
|
+
is_route_finished: true,
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("rejects any other core error as a failed checkout, without a second try", async () => {
|
|
305
|
+
const mock = installFetch({
|
|
306
|
+
flag: true,
|
|
307
|
+
process: {
|
|
308
|
+
status: 503,
|
|
309
|
+
body: { error: { code: "service_unavailable", message: "x", type: "service_error" } },
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
const { checkout } = buildCheckout();
|
|
313
|
+
|
|
314
|
+
const failure = await checkout.payment(paymentData()).catch((e) => e);
|
|
315
|
+
|
|
316
|
+
expect(failure.code).toBe(ErrorKeyEnum.PAYMENT_PROCESS_ERROR);
|
|
317
|
+
expect(failure.statusCode).toBe(503);
|
|
318
|
+
expect(callsTo(mock, `${CORE}/transactions/v1/process`)).toHaveLength(1);
|
|
319
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it.each([
|
|
323
|
+
[400, { error: { code: "invalid_request", message: "bad body", type: "validation_error" }, request_id: "r" }],
|
|
324
|
+
[409, { error: { code: "conflict", message: "busy", type: "conflict" } }],
|
|
325
|
+
[429, { error: { code: "rate_limited", message: "slow down", type: "service_error" } }],
|
|
326
|
+
[503, { error: { code: "retry_later", message: "later", type: "service_error" } }],
|
|
327
|
+
[502, undefined],
|
|
328
|
+
])("rejects a %s with the checkout-router error shape, not the raw answer", async (status, body) => {
|
|
329
|
+
installFetch({ flag: true, process: { status, body } });
|
|
330
|
+
const { checkout } = buildCheckout();
|
|
331
|
+
|
|
332
|
+
const failure = await checkout.payment(paymentData()).catch((e) => e);
|
|
333
|
+
|
|
334
|
+
expect(failure.code).toBe(ErrorKeyEnum.PAYMENT_PROCESS_ERROR);
|
|
335
|
+
expect(failure.statusCode).toBe(status);
|
|
336
|
+
expect(failure.originalError.status).toBe(status);
|
|
337
|
+
expect(failure.originalError.body).not.toHaveProperty("error");
|
|
338
|
+
expect(failure.originalError.body).toEqual({
|
|
339
|
+
status: String(status),
|
|
340
|
+
message: (body as any)?.error?.message ?? "Error",
|
|
341
|
+
psp_response: [],
|
|
342
|
+
is_route_finished: false,
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("keeps the status text the legacy refusal carries", async () => {
|
|
347
|
+
const mock = installFetch({ flag: true });
|
|
348
|
+
const base = mock.getMockImplementation()!;
|
|
349
|
+
mock.mockImplementation(async (url: string, init?: any) =>
|
|
350
|
+
url === `${CORE}/transactions/v1/process`
|
|
351
|
+
? { ...reply({ status: 400, body: { error: { code: "invalid_request", message: "bad" } } }), statusText: "Bad Request" }
|
|
352
|
+
: base(url, init),
|
|
353
|
+
);
|
|
354
|
+
const { checkout } = buildCheckout();
|
|
355
|
+
|
|
356
|
+
const failure = await checkout.payment(paymentData()).catch((e) => e);
|
|
357
|
+
|
|
358
|
+
expect(failure.originalError.statusText).toBe("Bad Request");
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("carries the core's error code, type and request id in telemetry only", async () => {
|
|
362
|
+
installFetch({
|
|
363
|
+
flag: true,
|
|
364
|
+
process: {
|
|
365
|
+
status: 409,
|
|
366
|
+
body: { error: { code: "conflict", message: "busy", type: "conflict" }, request_id: "req-9" },
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
const { checkout } = buildCheckout();
|
|
370
|
+
const report = jest.spyOn(checkout as any, "reportSdkError").mockImplementation(() => {});
|
|
371
|
+
|
|
372
|
+
const failure = await checkout.payment(paymentData()).catch((e) => e);
|
|
373
|
+
|
|
374
|
+
expect(report).toHaveBeenCalledWith(
|
|
375
|
+
expect.anything(),
|
|
376
|
+
expect.objectContaining({
|
|
377
|
+
metadata: expect.objectContaining({
|
|
378
|
+
core_error: { status: 409, code: "conflict", type: "conflict", request_id: "req-9" },
|
|
379
|
+
}),
|
|
380
|
+
}),
|
|
381
|
+
);
|
|
382
|
+
const merchantSees = JSON.stringify([failure.originalError, failure.details]);
|
|
383
|
+
for (const leaked of ["conflict", "req-9"]) expect(merchantSees).not.toContain(leaked);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("retries once on the legacy flow when the kill switch refused the create", async () => {
|
|
387
|
+
const mock = installFetch({
|
|
388
|
+
flag: true,
|
|
389
|
+
process: {
|
|
390
|
+
status: 503,
|
|
391
|
+
body: { error: { code: "operations_disabled", message: "x", type: "service_error" } },
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
const { checkout } = buildCheckout();
|
|
395
|
+
|
|
396
|
+
const result = await checkout.payment(paymentData());
|
|
397
|
+
|
|
398
|
+
expect(result).toEqual(LEGACY_ROUTER_BODY);
|
|
399
|
+
expect(callsTo(mock, `${CORE}/transactions/v1/process`)).toHaveLength(1);
|
|
400
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/orders/`)).toHaveLength(1);
|
|
401
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(1);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("sends the customer's identification as the legacy SDK did", async () => {
|
|
405
|
+
const mock = installFetch({ flag: true });
|
|
406
|
+
const { checkout } = buildCheckout();
|
|
407
|
+
const identification = { type: "RFC", number: "XAXX010101000" };
|
|
408
|
+
|
|
409
|
+
await checkout.payment(paymentData({ customer: { ...CUSTOMER, identification } }));
|
|
410
|
+
|
|
411
|
+
const body = JSON.parse(callsTo(mock, `${CORE}/transactions/v1/process`)[0][1].body);
|
|
412
|
+
expect(body.customer.identification).toEqual(identification);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("charges a Card on File payment through the core, with no flag in the request", async () => {
|
|
416
|
+
const mock = installFetch({ flag: true });
|
|
417
|
+
const { checkout } = buildCheckout();
|
|
418
|
+
await checkout.injectCheckout();
|
|
419
|
+
checkout.configureCheckout({ customer: CUSTOMER, cart: { total: 100, items: ITEMS } } as any);
|
|
420
|
+
|
|
421
|
+
await checkout._handleCheckout({
|
|
422
|
+
card: { skyflow_id: "sf-saved" },
|
|
423
|
+
customer: { id: 1, auth_token: "t" },
|
|
424
|
+
enable_card_on_file: true,
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
const body = JSON.parse(callsTo(mock, `${CORE}/transactions/v1/process`)[0][1].body);
|
|
428
|
+
expect(body.payment_method).toEqual({ type: "CARD", token: "sf-saved" });
|
|
429
|
+
expect(JSON.stringify(body)).not.toContain("enable_card_on_file");
|
|
430
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it.each([
|
|
434
|
+
["500 with the kill switch code", 500, "operations_disabled"],
|
|
435
|
+
["502", 502, "service_unavailable"],
|
|
436
|
+
["504", 504, "service_unavailable"],
|
|
437
|
+
["503 with another code", 503, "retry_later"],
|
|
438
|
+
])("never creates a second payment on legacy after a %s", async (_label, status, code) => {
|
|
439
|
+
const mock = installFetch({
|
|
440
|
+
flag: true,
|
|
441
|
+
process: { status, body: { error: { code, message: "x", type: "service_error" } } },
|
|
442
|
+
});
|
|
443
|
+
const { checkout } = buildCheckout();
|
|
444
|
+
|
|
445
|
+
await expect(checkout.payment(paymentData())).rejects.toMatchObject({ statusCode: status });
|
|
446
|
+
|
|
447
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/orders/`)).toHaveLength(0);
|
|
448
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("never creates a second payment on legacy after a network error", async () => {
|
|
452
|
+
const mock = installFetch({ flag: true });
|
|
453
|
+
const base = mock.getMockImplementation()!;
|
|
454
|
+
mock.mockImplementation(async (url: string, init?: any) => {
|
|
455
|
+
if (url === `${CORE}/transactions/v1/process`) throw new TypeError("Failed to fetch");
|
|
456
|
+
return base(url, init);
|
|
457
|
+
});
|
|
458
|
+
const { checkout } = buildCheckout();
|
|
459
|
+
|
|
460
|
+
await expect(checkout.payment(paymentData())).rejects.toBeDefined();
|
|
461
|
+
|
|
462
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/orders/`)).toHaveLength(0);
|
|
463
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it.each(["CARD", "Card", "card"])(
|
|
467
|
+
"charges a payment() with the '%s' payment method through the core",
|
|
468
|
+
async (type) => {
|
|
469
|
+
const mock = installFetch({ flag: true });
|
|
470
|
+
const { checkout } = buildCheckout();
|
|
471
|
+
|
|
472
|
+
await checkout.payment(paymentData({ payment_method: type }));
|
|
473
|
+
|
|
474
|
+
const body = JSON.parse(callsTo(mock, `${CORE}/transactions/v1/process`)[0][1].body);
|
|
475
|
+
expect(body.payment_method).toEqual({ type: "CARD", token: "sf-saved" });
|
|
476
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
477
|
+
},
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
it("keeps an alternative payment method on the legacy flow", async () => {
|
|
481
|
+
const mock = installFetch({ flag: true });
|
|
482
|
+
const { checkout } = buildCheckout();
|
|
483
|
+
|
|
484
|
+
await checkout.payment(paymentData({ card: undefined, payment_method: "oxxopay" }));
|
|
485
|
+
|
|
486
|
+
expect(callsTo(mock, `${CORE}/transactions/v1/process`)).toHaveLength(0);
|
|
487
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(1);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it("keeps the engine of a payment with the 3DS state it stores", async () => {
|
|
491
|
+
installFetch({
|
|
492
|
+
flag: true,
|
|
493
|
+
process: {
|
|
494
|
+
status: 201,
|
|
495
|
+
body: {
|
|
496
|
+
...CORE_SUCCESS,
|
|
497
|
+
status: "Pending",
|
|
498
|
+
transaction_status: "Pending",
|
|
499
|
+
routing: { checkout_id: "chk-1", is_route_finished: false },
|
|
500
|
+
next_action: {
|
|
501
|
+
redirect_to_url: {
|
|
502
|
+
url: "https://payflow.example.com/process?token=t#session=s",
|
|
503
|
+
return_url: "https://shop.example.com/done",
|
|
504
|
+
verify_transaction_status_url: `/transactions/v1/${PUBLIC_ID}`,
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
},
|
|
508
|
+
},
|
|
509
|
+
});
|
|
510
|
+
const { checkout } = buildCheckout();
|
|
511
|
+
const realLocation = Object.getOwnPropertyDescriptor(window, "location")!;
|
|
512
|
+
Object.defineProperty(window, "location", {
|
|
513
|
+
configurable: true,
|
|
514
|
+
writable: true,
|
|
515
|
+
value: { href: "https://localhost/" },
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
try {
|
|
519
|
+
void checkout.payment(paymentData());
|
|
520
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
521
|
+
|
|
522
|
+
expect(JSON.parse(localStorage.getItem("verify_transaction_status_url")!)).toMatchObject({
|
|
523
|
+
url: `/transactions/v1/${PUBLIC_ID}`,
|
|
524
|
+
engine: "core",
|
|
525
|
+
});
|
|
526
|
+
expect((window.location as any).href ?? window.location).toBe(
|
|
527
|
+
"https://payflow.example.com/process?token=t#session=s",
|
|
528
|
+
);
|
|
529
|
+
} finally {
|
|
530
|
+
Object.defineProperty(window, "location", realLocation);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
describe("card payment with the flag OFF", () => {
|
|
536
|
+
beforeEach(() => localStorage.clear());
|
|
537
|
+
|
|
538
|
+
it.each(["CARD", "card"])(
|
|
539
|
+
"sends a '%s' payment method to checkout-router exactly as the merchant gave it",
|
|
540
|
+
async (type) => {
|
|
541
|
+
const mock = installFetch({ flag: false });
|
|
542
|
+
const { checkout } = buildCheckout();
|
|
543
|
+
|
|
544
|
+
await checkout.payment(paymentData({ card: undefined, payment_method: type }));
|
|
545
|
+
|
|
546
|
+
const body = JSON.parse(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)[0][1].body);
|
|
547
|
+
expect(body.payment_method).toBe(type);
|
|
548
|
+
expect(body).not.toHaveProperty("card");
|
|
549
|
+
expect(callsTo(mock, `${CORE}/transactions/v1/process`)).toHaveLength(0);
|
|
550
|
+
},
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
it.each([
|
|
554
|
+
["false", false],
|
|
555
|
+
["missing", undefined],
|
|
556
|
+
])("keeps today's legacy flow when the flag is %s", async (_label, flag) => {
|
|
557
|
+
const mock = installFetch({ flag });
|
|
558
|
+
const { checkout, callBack } = buildCheckout();
|
|
559
|
+
|
|
560
|
+
const result = await checkout.payment(paymentData());
|
|
561
|
+
|
|
562
|
+
expect(result).toEqual(LEGACY_ROUTER_BODY);
|
|
563
|
+
expect(callBack).toHaveBeenCalledWith(LEGACY_ROUTER_BODY);
|
|
564
|
+
expect(callsTo(mock, `${CORE}/transactions/v1/process`)).toHaveLength(0);
|
|
565
|
+
const order = mock.mock.calls
|
|
566
|
+
.map(([url]) => url as string)
|
|
567
|
+
.filter((url) => url.startsWith(`${LEGACY}/api/v1/`) && !url.endsWith("/customer/"));
|
|
568
|
+
expect(order).toEqual([
|
|
569
|
+
`${LEGACY}/api/v1/orders/`,
|
|
570
|
+
`${LEGACY}/api/v1/business/7/payments/`,
|
|
571
|
+
`${LEGACY}/api/v1/checkout-router/`,
|
|
572
|
+
]);
|
|
573
|
+
const routerBody = JSON.parse(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)[0][1].body);
|
|
574
|
+
expect(routerBody).toMatchObject({
|
|
575
|
+
order_id: 21,
|
|
576
|
+
payment_id: 31,
|
|
577
|
+
business_id: 7,
|
|
578
|
+
source: "sdk",
|
|
579
|
+
card: { skyflow_id: "sf-saved" },
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
describe("verify3dsTransaction of a core payment", () => {
|
|
585
|
+
const STORAGE_KEY = "verify_transaction_status_url";
|
|
586
|
+
const VERIFY_PATH = `/transactions/v1/${PUBLIC_ID}`;
|
|
587
|
+
|
|
588
|
+
function storeCoreState() {
|
|
589
|
+
localStorage.setItem(
|
|
590
|
+
STORAGE_KEY,
|
|
591
|
+
JSON.stringify({ url: VERIFY_PATH, engine: "core", expires: Date.now() + 60_000 }),
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
async function startedCheckout(getBody: Reply) {
|
|
596
|
+
const mock = installFetch({ flag: true });
|
|
597
|
+
const base = mock.getMockImplementation()!;
|
|
598
|
+
mock.mockImplementation(async (url: string, init?: any) =>
|
|
599
|
+
url === `${CORE}${VERIFY_PATH}` ? reply(getBody) : base(url, init),
|
|
600
|
+
);
|
|
601
|
+
const { checkout } = buildCheckout();
|
|
602
|
+
await checkout.injectCheckout();
|
|
603
|
+
return { mock, checkout };
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
beforeEach(() => localStorage.clear());
|
|
607
|
+
|
|
608
|
+
it("reads the core once, resumes nothing and answers the rebuilt checkout-router body", async () => {
|
|
609
|
+
storeCoreState();
|
|
610
|
+
const { mock, checkout } = await startedCheckout({
|
|
611
|
+
status: 200,
|
|
612
|
+
body: {
|
|
613
|
+
...CORE_SUCCESS,
|
|
614
|
+
status: "Declined",
|
|
615
|
+
transaction_status: "Declined",
|
|
616
|
+
decline_code: "05",
|
|
617
|
+
decline_reason: "Do not honor",
|
|
618
|
+
next_action: undefined,
|
|
619
|
+
routing: {
|
|
620
|
+
checkout_id: "chk-1",
|
|
621
|
+
is_route_finished: true,
|
|
622
|
+
decline_type: "Soft",
|
|
623
|
+
rejected_psp_responses: [{ id: "r1" }, { id: "r2" }],
|
|
624
|
+
},
|
|
625
|
+
},
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
const result = await checkout.verify3dsTransaction();
|
|
629
|
+
|
|
630
|
+
expect(callsTo(mock, `${CORE}${VERIFY_PATH}`)).toHaveLength(1);
|
|
631
|
+
expect(callsTo(mock, `${LEGACY}/api/v1/checkout-router/`)).toHaveLength(0);
|
|
632
|
+
expect(result).toMatchObject({
|
|
633
|
+
status: "404",
|
|
634
|
+
message: "Do not honor",
|
|
635
|
+
checkout_id: "chk-1",
|
|
636
|
+
is_route_finished: true,
|
|
637
|
+
transaction_status: "Declined",
|
|
638
|
+
decline_code: "05",
|
|
639
|
+
psp_response: [{ id: "r1" }, { id: "r2" }],
|
|
640
|
+
});
|
|
641
|
+
expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it("answers an approved payment with the same body payment() resolves with", async () => {
|
|
645
|
+
storeCoreState();
|
|
646
|
+
const { checkout } = await startedCheckout({ status: 200, body: CORE_SUCCESS });
|
|
647
|
+
|
|
648
|
+
const verified = await checkout.verify3dsTransaction();
|
|
649
|
+
|
|
650
|
+
const { checkout: other } = buildCheckout();
|
|
651
|
+
installFetch({ flag: true });
|
|
652
|
+
const paid = await other.payment(paymentData());
|
|
653
|
+
expect(verified).toEqual(paid);
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("keeps the 3DS state while the core payment is not final", async () => {
|
|
657
|
+
storeCoreState();
|
|
658
|
+
const { checkout } = await startedCheckout({
|
|
659
|
+
status: 200,
|
|
660
|
+
body: { ...CORE_SUCCESS, status: "Pending", transaction_status: "Pending" },
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
const result = await checkout.verify3dsTransaction();
|
|
664
|
+
|
|
665
|
+
expect(result).toMatchObject({ transaction_status: "Pending" });
|
|
666
|
+
expect(JSON.parse(localStorage.getItem(STORAGE_KEY)!)).toMatchObject({ engine: "core" });
|
|
667
|
+
});
|
|
668
|
+
});
|