@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
|
@@ -1,7 +1,26 @@
|
|
|
1
|
+
import { sanitize3dsMarkup } from "../helpers/sanitize3dsMarkup"
|
|
2
|
+
import { isHttpsUrl } from "../helpers/safeUrl"
|
|
3
|
+
import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum"
|
|
4
|
+
import { buildPublicAppError } from "../shared/utils/appError"
|
|
5
|
+
import { ICoreTransactionResponse, PaymentEngine } from "../types/core"
|
|
6
|
+
|
|
7
|
+
type CoreResultBuilder = (transaction: ICoreTransactionResponse) => Record<string, any>
|
|
8
|
+
|
|
9
|
+
// A core payment that has not reached one of these is still open.
|
|
10
|
+
const OPEN_CORE_STATUSES = ["Pending", "Processing"]
|
|
11
|
+
|
|
12
|
+
type StoredVerification = {
|
|
13
|
+
url: string,
|
|
14
|
+
expires: number,
|
|
15
|
+
engine?: PaymentEngine
|
|
16
|
+
}
|
|
17
|
+
|
|
1
18
|
type ThreeDSHandlerContructor = {
|
|
2
19
|
payload?: any,
|
|
3
20
|
apiKey?: string,
|
|
4
21
|
baseUrl?: string,
|
|
22
|
+
coreBaseUrl?: string,
|
|
23
|
+
buildCoreResult?: CoreResultBuilder,
|
|
5
24
|
redirectOnComplete?: boolean,
|
|
6
25
|
tdsIframeId?: string,
|
|
7
26
|
tonderPayButtonId?: string,
|
|
@@ -12,6 +31,9 @@ export class ThreeDSHandler {
|
|
|
12
31
|
|
|
13
32
|
callBack?: (params: any) => any
|
|
14
33
|
baseUrl?: string
|
|
34
|
+
coreBaseUrl?: string
|
|
35
|
+
buildCoreResult: CoreResultBuilder
|
|
36
|
+
engine: PaymentEngine = "legacy"
|
|
15
37
|
apiKey?: string
|
|
16
38
|
payload?: any
|
|
17
39
|
localStorageKey: string = "verify_transaction_status_url"
|
|
@@ -23,12 +45,16 @@ export class ThreeDSHandler {
|
|
|
23
45
|
payload = null,
|
|
24
46
|
apiKey,
|
|
25
47
|
baseUrl,
|
|
48
|
+
coreBaseUrl,
|
|
49
|
+
buildCoreResult = (transaction) => transaction,
|
|
26
50
|
redirectOnComplete,
|
|
27
51
|
tdsIframeId,
|
|
28
52
|
tonderPayButtonId,
|
|
29
53
|
callBack
|
|
30
54
|
}: ThreeDSHandlerContructor) {
|
|
55
|
+
this.buildCoreResult = buildCoreResult
|
|
31
56
|
this.baseUrl = baseUrl,
|
|
57
|
+
this.coreBaseUrl = coreBaseUrl,
|
|
32
58
|
this.apiKey = apiKey,
|
|
33
59
|
this.payload = payload
|
|
34
60
|
this.tdsIframeId = tdsIframeId
|
|
@@ -63,13 +89,18 @@ export class ThreeDSHandler {
|
|
|
63
89
|
}
|
|
64
90
|
}
|
|
65
91
|
|
|
92
|
+
setEngine(engine: PaymentEngine) {
|
|
93
|
+
this.engine = engine
|
|
94
|
+
}
|
|
95
|
+
|
|
66
96
|
saveUrlWithExpiration(url: string) {
|
|
67
97
|
try {
|
|
68
98
|
const now = new Date()
|
|
69
|
-
const item = {
|
|
99
|
+
const item: StoredVerification = {
|
|
70
100
|
url: url,
|
|
71
101
|
// Expires after 20 minutes
|
|
72
|
-
expires: now.getTime() + 20 * 60 * 1000
|
|
102
|
+
expires: now.getTime() + 20 * 60 * 1000,
|
|
103
|
+
engine: this.engine
|
|
73
104
|
}
|
|
74
105
|
this.setStorageItem(item)
|
|
75
106
|
} catch (error) {
|
|
@@ -77,7 +108,7 @@ export class ThreeDSHandler {
|
|
|
77
108
|
}
|
|
78
109
|
}
|
|
79
110
|
|
|
80
|
-
|
|
111
|
+
getStoredVerification(): StoredVerification | null | undefined {
|
|
81
112
|
const status = this.getStorageItem();
|
|
82
113
|
if(status) {
|
|
83
114
|
const item = JSON.parse(status)
|
|
@@ -87,13 +118,23 @@ export class ThreeDSHandler {
|
|
|
87
118
|
this.removeVerifyTransactionUrl()
|
|
88
119
|
return null
|
|
89
120
|
} else {
|
|
90
|
-
return item
|
|
121
|
+
return item
|
|
91
122
|
}
|
|
92
123
|
} else {
|
|
93
124
|
return null
|
|
94
125
|
}
|
|
95
126
|
}
|
|
96
127
|
|
|
128
|
+
getUrlWithExpiration() {
|
|
129
|
+
const item = this.getStoredVerification()
|
|
130
|
+
return item === undefined ? undefined : item?.url ?? null
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// A state stored before the engine existed belongs to a legacy payment.
|
|
134
|
+
getStoredEngine(): PaymentEngine {
|
|
135
|
+
return this.getStoredVerification()?.engine === "core" ? "core" : "legacy"
|
|
136
|
+
}
|
|
137
|
+
|
|
97
138
|
removeVerifyTransactionUrl() {
|
|
98
139
|
return this.removeStorageItem()
|
|
99
140
|
}
|
|
@@ -112,7 +153,13 @@ export class ThreeDSHandler {
|
|
|
112
153
|
if (iframe) {
|
|
113
154
|
this.saveVerifyTransactionUrl()
|
|
114
155
|
const container = document.createElement('div')
|
|
115
|
-
|
|
156
|
+
// The 3DS Method step is device fingerprinting, not UI: the ACS form posts
|
|
157
|
+
// itself into its target frame and nothing here is ever meant to be seen. The
|
|
158
|
+
// hiding belongs to the SDK rather than to the remote markup that asks for it,
|
|
159
|
+
// so the sanitizer is free to drop the style attribute, and a fragment that
|
|
160
|
+
// positions itself over the card fields has nothing to render into.
|
|
161
|
+
container.style.display = 'none'
|
|
162
|
+
container.appendChild(sanitize3dsMarkup(iframe))
|
|
116
163
|
document.body.appendChild(container)
|
|
117
164
|
|
|
118
165
|
// Create and append the script tag manually
|
|
@@ -143,6 +190,23 @@ export class ThreeDSHandler {
|
|
|
143
190
|
redirectToChallenge() {
|
|
144
191
|
const url = this.getRedirectUrl()
|
|
145
192
|
if (url) {
|
|
193
|
+
// The challenge URL arrives in the remote payload and reaches two sinks below:
|
|
194
|
+
// window.location and an iframe src. A javascript: URL at either one executes in
|
|
195
|
+
// the merchant's own origin, next to the card fields.
|
|
196
|
+
//
|
|
197
|
+
// Rejection throws rather than skipping the navigation: the caller turns it into
|
|
198
|
+
// a rejected payment the merchant can surface and the shopper can retry, whereas
|
|
199
|
+
// doing nothing would strand the shopper mid-challenge with no signal that the
|
|
200
|
+
// flow had stopped. The error code is locked so it survives the caller's wrapping
|
|
201
|
+
// and stays distinguishable from an ordinary payment failure.
|
|
202
|
+
if (!isHttpsUrl(url)) {
|
|
203
|
+
throw buildPublicAppError({
|
|
204
|
+
errorCode: ErrorKeyEnum.THREEDS_REDIRECTION_ERROR,
|
|
205
|
+
statusCode: 400,
|
|
206
|
+
lockErrorCode: true,
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
146
210
|
this.saveVerifyTransactionUrl()
|
|
147
211
|
if(this.redirectOnComplete) {
|
|
148
212
|
window.location = url;
|
|
@@ -156,7 +220,7 @@ export class ThreeDSHandler {
|
|
|
156
220
|
const self = this;
|
|
157
221
|
|
|
158
222
|
const listenerHandler = async (event: any) => {
|
|
159
|
-
const checkStatus = (result: any) => result
|
|
223
|
+
const checkStatus = (result: any) => self.isVerificationFinal(result);
|
|
160
224
|
|
|
161
225
|
const executeAction = () => {
|
|
162
226
|
try {
|
|
@@ -200,11 +264,11 @@ export class ThreeDSHandler {
|
|
|
200
264
|
}
|
|
201
265
|
}
|
|
202
266
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
const url =
|
|
207
|
-
|
|
267
|
+
// The verify URL of a core payment is relative to the core, not to the monolith.
|
|
268
|
+
async fetchVerification(item: StoredVerification) {
|
|
269
|
+
const base = item.engine === "core" ? this.coreBaseUrl : this.baseUrl
|
|
270
|
+
const url = item.url.startsWith("https://") ? item.url : `${base}${item.url}`;
|
|
271
|
+
return await fetch(url, {
|
|
208
272
|
method: "GET",
|
|
209
273
|
headers: {
|
|
210
274
|
"Content-Type": "application/json",
|
|
@@ -212,17 +276,29 @@ export class ThreeDSHandler {
|
|
|
212
276
|
},
|
|
213
277
|
// body: JSON.stringify(data),
|
|
214
278
|
});
|
|
279
|
+
}
|
|
215
280
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
281
|
+
async requestTransactionStatus() {
|
|
282
|
+
const item = this.getStoredVerification()
|
|
283
|
+
// The state expires or is cleared by another tab while the iframe polls.
|
|
284
|
+
if (!item) return null;
|
|
285
|
+
let response: Response;
|
|
286
|
+
try {
|
|
287
|
+
response = await this.fetchVerification(item);
|
|
288
|
+
if (response.status === 200) return await response.json();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (this.isTransientCoreFailure(item)) return this.keepPolling();
|
|
291
|
+
throw error;
|
|
222
292
|
}
|
|
293
|
+
console.error('La verificación de la transacción falló.');
|
|
294
|
+
return this.isTransientCoreFailure(item, response.status) ? this.keepPolling() : null;
|
|
295
|
+
}
|
|
223
296
|
|
|
297
|
+
// A server error, a throttle or a dropped connection is not an answer: the poller asks again.
|
|
298
|
+
keepPolling() {
|
|
299
|
+
return { status: "Pending", transaction_status: "Pending" }
|
|
224
300
|
}
|
|
225
|
-
|
|
301
|
+
|
|
226
302
|
// Returns an object
|
|
227
303
|
// https://example.com/?name=John&age=30&city=NewYork
|
|
228
304
|
// { name: "John", age: "30", city: "NewYork" }
|
|
@@ -289,29 +365,57 @@ export class ThreeDSHandler {
|
|
|
289
365
|
}
|
|
290
366
|
}
|
|
291
367
|
|
|
368
|
+
// A core payment is open until the core says otherwise, whatever the rail answered meanwhile.
|
|
369
|
+
isCoreOpen(transaction: ICoreTransactionResponse) {
|
|
370
|
+
return OPEN_CORE_STATUSES.includes(transaction?.status)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// The core continues its own route, so there is nothing to resume here: the payment is
|
|
374
|
+
// read and answered like pay() answers it. An open payment keeps its state so a later
|
|
375
|
+
// call can get the outcome.
|
|
376
|
+
async handleCoreTransaction(response: Response) {
|
|
377
|
+
const transaction = await response.json();
|
|
378
|
+
if (!this.isCoreOpen(transaction)) this.removeVerifyTransactionUrl();
|
|
379
|
+
return this.buildCoreResult(transaction);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// The iframe poller stops on a final answer.
|
|
383
|
+
isVerificationFinal(result: any) {
|
|
384
|
+
return this.engine === "core"
|
|
385
|
+
? !this.isCoreOpen(result)
|
|
386
|
+
: result?.transaction_status !== "Pending"
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Only a refusal that will not change (the payment cannot be read) ends a core verification;
|
|
390
|
+
// a server error, a throttle or a dropped connection leaves it to be tried again.
|
|
391
|
+
isTransientCoreFailure(item: StoredVerification, status?: number) {
|
|
392
|
+
return item.engine === "core" && (status === undefined || status >= 500 || status === 429 || status === 408)
|
|
393
|
+
}
|
|
394
|
+
|
|
292
395
|
async verifyTransactionStatus() {
|
|
293
|
-
const
|
|
294
|
-
if (
|
|
295
|
-
const url = verifyUrl.startsWith("https://") ? verifyUrl : `${this.baseUrl}${verifyUrl}`;
|
|
396
|
+
const item = this.getStoredVerification();
|
|
397
|
+
if (item?.url) {
|
|
296
398
|
try {
|
|
297
|
-
const response = await
|
|
298
|
-
method: "GET",
|
|
299
|
-
headers: {
|
|
300
|
-
"Content-Type": "application/json",
|
|
301
|
-
Authorization: `Token ${this.apiKey}`,
|
|
302
|
-
},
|
|
303
|
-
// body: JSON.stringify(data),
|
|
304
|
-
});
|
|
399
|
+
const response = await this.fetchVerification(item);
|
|
305
400
|
|
|
306
401
|
if (response.status !== 200) {
|
|
307
402
|
console.error('La verificación de la transacción falló.');
|
|
308
|
-
this.removeVerifyTransactionUrl();
|
|
403
|
+
if (!this.isTransientCoreFailure(item, response.status)) this.removeVerifyTransactionUrl();
|
|
404
|
+
if (item.engine === "core") {
|
|
405
|
+
throw await buildPublicAppError({ response, errorCode: ErrorKeyEnum.FETCH_TRANSACTION_ERROR });
|
|
406
|
+
}
|
|
309
407
|
return response
|
|
310
408
|
}
|
|
311
409
|
|
|
410
|
+
if (item.engine === "core") {
|
|
411
|
+
return await this.handleCoreTransaction(response);
|
|
412
|
+
}
|
|
312
413
|
return await this.handleTransactionResponse(response);
|
|
313
414
|
} catch (error) {
|
|
314
415
|
console.error('Error al verificar la transacción:', error);
|
|
416
|
+
if (item.engine === "core") {
|
|
417
|
+
throw buildPublicAppError({ errorCode: ErrorKeyEnum.FETCH_TRANSACTION_ERROR }, error);
|
|
418
|
+
}
|
|
315
419
|
this.removeVerifyTransactionUrl();
|
|
316
420
|
}
|
|
317
421
|
} else {
|
|
@@ -9,8 +9,10 @@ import {
|
|
|
9
9
|
} from "../data/checkoutApi";
|
|
10
10
|
import { getOpenpayDeviceSessionID } from "../data/openPayApi";
|
|
11
11
|
import {
|
|
12
|
-
getBrowserInfo
|
|
12
|
+
getBrowserInfo,
|
|
13
|
+
isCardPaymentMethod
|
|
13
14
|
} from "../helpers/utils";
|
|
15
|
+
import { validateSecureToken } from "../helpers/validations";
|
|
14
16
|
import { registerOrFetchCustomer } from "../data/customerApi";
|
|
15
17
|
import { get } from "../helpers/get";
|
|
16
18
|
import {
|
|
@@ -36,11 +38,23 @@ import {ErrorKeyEnum} from "../shared/enum/ErrorKeyEnum";
|
|
|
36
38
|
import { IMPConfigRequest } from "../types/mercadoPago";
|
|
37
39
|
import { CardOnFile } from "../helpers/card_on_file";
|
|
38
40
|
import { SdkTelemetryClient } from "../helpers/SdkTelemetryClient";
|
|
39
|
-
import { getTelemetryEndpoint } from "../shared/constants/apiEndpoints";
|
|
41
|
+
import { getApiBaseUrl, getTelemetryEndpoint } from "../shared/constants/apiEndpoints";
|
|
40
42
|
import { SDK_INFO } from "../helpers/sdkInfo";
|
|
41
|
-
import { AppError, buildPublicAppError } from "../shared/utils/appError";
|
|
43
|
+
import { AppError, buildPublicAppError, buildPublicAppErrorFromAnswer } from "../shared/utils/appError";
|
|
44
|
+
import { createCorePayment, isOperationsDisabled, isOperationsDisabledAnswer } from "../data/coreApi";
|
|
45
|
+
import { buildCoreProcessBody, describeCoreRefusal, newRequestId, rebuildCheckoutResult } from "../helpers/corePayment";
|
|
46
|
+
type CheckoutRequest = {
|
|
47
|
+
card?: Record<string, any>;
|
|
48
|
+
payment_method?: string;
|
|
49
|
+
customer: Record<string, any>;
|
|
50
|
+
isSandbox?: boolean;
|
|
51
|
+
returnUrl?: string;
|
|
52
|
+
enable_card_on_file?: boolean;
|
|
53
|
+
};
|
|
54
|
+
|
|
42
55
|
export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOptions> {
|
|
43
56
|
baseUrl = "";
|
|
57
|
+
coreBaseUrl = "";
|
|
44
58
|
cartTotal: string | number = "0";
|
|
45
59
|
process3ds: ThreeDSHandler;
|
|
46
60
|
mode?: "production" | "sandbox" | "stage" | "development" | undefined;
|
|
@@ -86,6 +100,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
86
100
|
this.mode = mode;
|
|
87
101
|
this.customer = {} as ICustomer
|
|
88
102
|
this.baseUrl = baseUrlTonder || TONDER_URL_BY_MODE[this.mode] || TONDER_URL_BY_MODE["stage"];
|
|
103
|
+
this.coreBaseUrl = getApiBaseUrl(this.mode);
|
|
89
104
|
this.abortController = new AbortController();
|
|
90
105
|
this.requestId = this.generateRequestId();
|
|
91
106
|
this.customization = {
|
|
@@ -94,8 +109,11 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
94
109
|
}
|
|
95
110
|
|
|
96
111
|
this.process3ds = new ThreeDSHandler({
|
|
97
|
-
apiKey:
|
|
112
|
+
apiKey: this.apiKeyTonder,
|
|
98
113
|
baseUrl: this.baseUrl,
|
|
114
|
+
coreBaseUrl: this.coreBaseUrl,
|
|
115
|
+
buildCoreResult: (transaction) =>
|
|
116
|
+
rebuildCheckoutResult(200, transaction, { coreBaseUrl: this.coreBaseUrl }).body,
|
|
99
117
|
redirectOnComplete: this.customization.redirectOnComplete,
|
|
100
118
|
tdsIframeId: tdsIframeId,
|
|
101
119
|
tonderPayButtonId: tonderPayButtonId,
|
|
@@ -164,7 +182,16 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
164
182
|
}
|
|
165
183
|
|
|
166
184
|
async verify3dsTransaction(): Promise<ITransaction | IStartCheckoutResponse | void> {
|
|
185
|
+
const engine = this.process3ds.getStoredEngine();
|
|
167
186
|
const result3ds = await this.process3ds.verifyTransactionStatus();
|
|
187
|
+
if (engine === "core") {
|
|
188
|
+
// The core continues its own route after the challenge, so nothing is resumed. A still-open
|
|
189
|
+
// payment may carry the challenge the buyer just came back from: it is never followed again.
|
|
190
|
+
this.process3ds.setPayload(result3ds);
|
|
191
|
+
return result3ds;
|
|
192
|
+
}
|
|
193
|
+
// A challenge found while resuming is stored under the engine of this payment, not the last one used.
|
|
194
|
+
this.process3ds.setEngine("legacy");
|
|
168
195
|
const resultCheckout = await this.#resumeCheckout(result3ds);
|
|
169
196
|
this.process3ds.setPayload(resultCheckout);
|
|
170
197
|
return this._handle3dsRedirect(resultCheckout);
|
|
@@ -269,7 +296,90 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
269
296
|
return this.#customerData!;
|
|
270
297
|
}
|
|
271
298
|
|
|
272
|
-
|
|
299
|
+
// The engine is chosen here, once, and travels with the 3DS state: a payment started
|
|
300
|
+
// on one engine is finished by it, whatever the merchant's flag does meanwhile.
|
|
301
|
+
async _handleCheckout(request: CheckoutRequest) {
|
|
302
|
+
if (this.merchantData?.core_payments_enabled === true && this.#isCorePayment(request)) {
|
|
303
|
+
this.process3ds.setEngine("core");
|
|
304
|
+
try {
|
|
305
|
+
return await this.#coreCheckout(request);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (!isOperationsDisabled(error)) throw error;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
this.process3ds.setEngine("legacy");
|
|
311
|
+
return this.#legacyCheckout(request);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Card payments only (new, saved and Card on File: the core decides the latter by itself);
|
|
315
|
+
// alternative methods stay on the legacy flow.
|
|
316
|
+
#isCorePayment({ card, payment_method }: CheckoutRequest) {
|
|
317
|
+
return !!card && isCardPaymentMethod(payment_method);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async #coreCheckout({
|
|
321
|
+
card,
|
|
322
|
+
returnUrl: returnUrlData
|
|
323
|
+
}: CheckoutRequest) {
|
|
324
|
+
const { reference } = this.merchantData!;
|
|
325
|
+
let payment;
|
|
326
|
+
let coreError;
|
|
327
|
+
try {
|
|
328
|
+
payment = buildCoreProcessBody({
|
|
329
|
+
amount: Number(this.cartTotal),
|
|
330
|
+
currency: this.currency,
|
|
331
|
+
reference: this.order_reference ? this.order_reference : reference,
|
|
332
|
+
customer: this.customer,
|
|
333
|
+
card: card!,
|
|
334
|
+
metadata: this.metadata,
|
|
335
|
+
returnUrl: returnUrlData || this.returnUrl,
|
|
336
|
+
items: this.cartItems,
|
|
337
|
+
browserInfo: getBrowserInfo(),
|
|
338
|
+
});
|
|
339
|
+
const answer = await createCorePayment(
|
|
340
|
+
this.coreBaseUrl,
|
|
341
|
+
this.apiKeyTonder,
|
|
342
|
+
payment,
|
|
343
|
+
newRequestId(),
|
|
344
|
+
);
|
|
345
|
+
if (answer.status >= 400) coreError = describeCoreRefusal(answer.status, answer.body);
|
|
346
|
+
// Nothing was created: the raw answer is kept so the caller can recognise it and fall back.
|
|
347
|
+
if (isOperationsDisabledAnswer(answer)) {
|
|
348
|
+
throw buildPublicAppErrorFromAnswer({
|
|
349
|
+
errorCode: ErrorKeyEnum.START_CHECKOUT_ERROR,
|
|
350
|
+
status: answer.status,
|
|
351
|
+
body: answer.body,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
const result = rebuildCheckoutResult(
|
|
355
|
+
answer.status,
|
|
356
|
+
answer.body,
|
|
357
|
+
{ coreBaseUrl: this.coreBaseUrl },
|
|
358
|
+
answer.statusText,
|
|
359
|
+
);
|
|
360
|
+
if (result.outcome === "resolve") return result.body;
|
|
361
|
+
throw buildPublicAppErrorFromAnswer({
|
|
362
|
+
errorCode: ErrorKeyEnum.START_CHECKOUT_ERROR,
|
|
363
|
+
status: result.status,
|
|
364
|
+
statusText: result.statusText,
|
|
365
|
+
body: result.body,
|
|
366
|
+
});
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if (!isOperationsDisabled(error)) {
|
|
369
|
+
this.reportSdkError(error, {
|
|
370
|
+
feature: "payment",
|
|
371
|
+
metadata: {
|
|
372
|
+
step: "_handleCheckout",
|
|
373
|
+
payment,
|
|
374
|
+
core_error: coreError,
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
throw error;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async #legacyCheckout({
|
|
273
383
|
card,
|
|
274
384
|
payment_method,
|
|
275
385
|
customer,
|
|
@@ -277,14 +387,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
277
387
|
enable_card_on_file,
|
|
278
388
|
// TODO: DEPRECATED
|
|
279
389
|
returnUrl: returnUrlData
|
|
280
|
-
}: {
|
|
281
|
-
card?: Record<string, any>;
|
|
282
|
-
payment_method?: string;
|
|
283
|
-
customer: Record<string, any>;
|
|
284
|
-
isSandbox?: boolean;
|
|
285
|
-
returnUrl?: string;
|
|
286
|
-
enable_card_on_file?: boolean;
|
|
287
|
-
}) {
|
|
390
|
+
}: CheckoutRequest) {
|
|
288
391
|
const { openpay_keys, reference, business } = this.merchantData!;
|
|
289
392
|
const total = Number(this.cartTotal);
|
|
290
393
|
let orderItems;
|
|
@@ -423,7 +526,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
423
526
|
try {
|
|
424
527
|
if (!this.merchantData) {
|
|
425
528
|
this.merchantData = await fetchBusiness(
|
|
426
|
-
this.
|
|
529
|
+
this.coreBaseUrl,
|
|
427
530
|
this.apiKeyTonder,
|
|
428
531
|
this.abortController.signal,
|
|
429
532
|
);
|
|
@@ -436,7 +539,9 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
436
539
|
step: "_fetchMerchantData",
|
|
437
540
|
},
|
|
438
541
|
});
|
|
439
|
-
|
|
542
|
+
// Without its configuration the SDK cannot tell which engine charges, and it never falls
|
|
543
|
+
// back to another source: the caller gets a clear error and the next call asks again.
|
|
544
|
+
throw buildPublicAppError({ errorCode: ErrorKeyEnum.FETCH_BUSINESS_ERROR }, e);
|
|
440
545
|
}
|
|
441
546
|
}
|
|
442
547
|
|
|
@@ -504,6 +609,13 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
504
609
|
}
|
|
505
610
|
|
|
506
611
|
#setSecureToken(token: string) {
|
|
612
|
+
if (!validateSecureToken(token)) {
|
|
613
|
+
throw buildPublicAppError({
|
|
614
|
+
errorCode: ErrorKeyEnum.SECURE_TOKEN_INVALID,
|
|
615
|
+
statusCode: 400,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
|
|
507
619
|
this.secureToken = token;
|
|
508
620
|
}
|
|
509
621
|
|
|
@@ -543,7 +655,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
|
|
|
543
655
|
// setTimeout(() => {
|
|
544
656
|
// process3ds.verifyTransactionStatus();
|
|
545
657
|
// }, 10000);
|
|
546
|
-
this.process3ds.verifyTransactionStatus();
|
|
658
|
+
return this.process3ds.verifyTransactionStatus();
|
|
547
659
|
})
|
|
548
660
|
.catch((error) => {
|
|
549
661
|
console.log("Error loading iframe:", error);
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
getBrowserInfo,
|
|
7
7
|
getBusinessId,
|
|
8
8
|
getCardType,
|
|
9
|
+
isCardPaymentMethod,
|
|
9
10
|
} from "../helpers/utils";
|
|
10
11
|
import {getCustomerAPMs} from "../data/api";
|
|
11
12
|
import {BaseInlineCheckout} from "./BaseInlineCheckout";
|
|
@@ -458,7 +459,7 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
|
|
|
458
459
|
public async getBusiness(): Promise<GetBusinessResponse> {
|
|
459
460
|
try {
|
|
460
461
|
return await fetchBusiness(
|
|
461
|
-
this.
|
|
462
|
+
this.coreBaseUrl,
|
|
462
463
|
this.apiKeyTonder,
|
|
463
464
|
this.abortController.signal,
|
|
464
465
|
);
|
|
@@ -541,7 +542,7 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
|
|
|
541
542
|
|
|
542
543
|
async _checkout({
|
|
543
544
|
card,
|
|
544
|
-
payment_method,
|
|
545
|
+
payment_method: requestedPaymentMethod,
|
|
545
546
|
isSandbox,
|
|
546
547
|
// TODO: DEPRECATED
|
|
547
548
|
returnUrl: returnUrlData
|
|
@@ -552,6 +553,12 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
|
|
|
552
553
|
returnUrl?: string;
|
|
553
554
|
}) {
|
|
554
555
|
await this._fetchMerchantData();
|
|
556
|
+
// The core takes a card payment method in any case and collects the card as for no method;
|
|
557
|
+
// the legacy flow keeps sending the method exactly as the merchant gave it.
|
|
558
|
+
const payment_method =
|
|
559
|
+
this.merchantData?.core_payments_enabled === true && isCardPaymentMethod(requestedPaymentMethod)
|
|
560
|
+
? undefined
|
|
561
|
+
: requestedPaymentMethod;
|
|
555
562
|
const customer = await this._getCustomer(this.abortController.signal) as CustomerRegisterResponse;
|
|
556
563
|
const { business } = this.merchantData!;
|
|
557
564
|
const { auth_token, first_name = "", last_name = "", email = "" } = customer;
|
package/src/data/businessApi.ts
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
import {GetBusinessResponse} from "../types/responses";
|
|
1
|
+
import { GetBusinessResponse } from "../types/responses";
|
|
2
|
+
import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
|
|
3
|
+
import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
|
|
4
|
+
import { buildPublicAppError } from "../shared/utils/appError";
|
|
2
5
|
|
|
3
6
|
export async function fetchBusiness(
|
|
4
|
-
|
|
7
|
+
coreBaseUrl: string,
|
|
5
8
|
apiKey: string,
|
|
6
9
|
signal: AbortSignal,
|
|
7
10
|
): Promise<GetBusinessResponse> {
|
|
8
|
-
const
|
|
9
|
-
`${
|
|
11
|
+
const response = await fetch(
|
|
12
|
+
`${coreBaseUrl}${API_ENDPOINTS.CORE_BUSINESS_CONFIG}`,
|
|
10
13
|
{
|
|
11
14
|
headers: {
|
|
12
15
|
Authorization: `Token ${apiKey}`,
|
|
13
16
|
},
|
|
14
|
-
signal
|
|
17
|
+
signal,
|
|
15
18
|
},
|
|
16
19
|
);
|
|
17
|
-
return await
|
|
20
|
+
if (response.ok) return await response.json();
|
|
21
|
+
throw await buildPublicAppError({
|
|
22
|
+
response,
|
|
23
|
+
errorCode: ErrorKeyEnum.FETCH_BUSINESS_ERROR,
|
|
24
|
+
});
|
|
18
25
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
|
|
2
|
+
import { AppError } from "../shared/utils/appError";
|
|
3
|
+
import { buildCoreProcessBody } from "../helpers/corePayment";
|
|
4
|
+
|
|
5
|
+
export const SDK_APP_ORIGIN = "sdk/ionic";
|
|
6
|
+
|
|
7
|
+
export type CoreAnswer = { status: number; statusText?: string; body: any };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a card payment in the core and answers whatever the core answered, a refusal
|
|
11
|
+
* included: turning an answer into what the merchant sees is `rebuildCheckoutResult`'s job.
|
|
12
|
+
*/
|
|
13
|
+
export async function createCorePayment(
|
|
14
|
+
coreBaseUrl: string,
|
|
15
|
+
apiKey: string,
|
|
16
|
+
payment: ReturnType<typeof buildCoreProcessBody>,
|
|
17
|
+
requestId: string,
|
|
18
|
+
): Promise<CoreAnswer> {
|
|
19
|
+
const response = await fetch(`${coreBaseUrl}${API_ENDPOINTS.CORE_PROCESS}`, {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers: {
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
Authorization: `Token ${apiKey}`,
|
|
24
|
+
"X-App-Origin": SDK_APP_ORIGIN,
|
|
25
|
+
"X-Request-Id": requestId,
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify(payment),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const body = response.ok ? await response.json() : await response.json().catch(() => undefined);
|
|
31
|
+
return { status: response.status, statusText: response.statusText, body };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The kill switch refused the create: nothing was created, so the legacy flow may take over. */
|
|
35
|
+
export function isOperationsDisabled(error: unknown): boolean {
|
|
36
|
+
return (
|
|
37
|
+
error instanceof AppError &&
|
|
38
|
+
isOperationsDisabledAnswer({
|
|
39
|
+
status: error.statusCode,
|
|
40
|
+
body: (error.originalError as any)?.body,
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isOperationsDisabledAnswer(answer: Pick<CoreAnswer, "status" | "body">): boolean {
|
|
46
|
+
return answer.status === 503 && answer.body?.error?.code === "operations_disabled";
|
|
47
|
+
}
|