@tonder.io/ionic-lite-sdk 0.0.34-beta.3 → 0.0.35-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.
- package/.idea/aws.xml +17 -0
- package/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/.idea/vcs.xml +6 -0
- package/.idea/workspace.xml +72 -17
- package/dist/classes/BaseInlineCheckout.d.ts +48 -0
- package/dist/classes/liteCheckout.d.ts +61 -29
- package/dist/data/businessApi.d.ts +2 -0
- package/dist/data/cardApi.d.ts +4 -0
- package/dist/data/checkoutApi.d.ts +5 -0
- package/dist/data/customerApi.d.ts +2 -0
- package/dist/data/openPayApi.d.ts +1 -0
- package/dist/data/paymentMethodApi.d.ts +5 -0
- package/dist/data/skyflowApi.d.ts +1 -0
- package/dist/helpers/skyflow.d.ts +3 -0
- package/dist/helpers/utils.d.ts +8 -4
- package/dist/helpers/validations.d.ts +6 -0
- package/dist/index.js +1 -1
- package/dist/shared/catalog/paymentMethodsCatalog.d.ts +1 -0
- package/dist/shared/constants/messages.d.ts +11 -0
- package/dist/shared/constants/paymentMethodAPM.d.ts +62 -0
- package/dist/shared/constants/tonderUrl.d.ts +7 -0
- package/dist/types/card.d.ts +29 -0
- package/dist/types/checkout.d.ts +103 -0
- package/dist/types/commons.d.ts +32 -0
- package/dist/types/customer.d.ts +12 -0
- package/dist/types/paymentMethod.d.ts +22 -0
- package/dist/types/requests.d.ts +12 -3
- package/package.json +1 -1
- package/src/classes/BaseInlineCheckout.ts +318 -0
- package/src/classes/liteCheckout.ts +273 -280
- package/src/data/businessApi.ts +14 -0
- package/src/data/cardApi.ts +82 -0
- package/src/data/checkoutApi.ts +71 -0
- package/src/data/customerApi.ts +31 -0
- package/src/data/openPayApi.ts +7 -0
- package/src/data/paymentMethodApi.ts +34 -0
- package/src/data/skyflowApi.ts +16 -0
- package/src/helpers/skyflow.ts +85 -0
- package/src/helpers/utils.ts +31 -243
- package/src/helpers/validations.ts +55 -0
- package/src/shared/catalog/paymentMethodsCatalog.ts +248 -0
- package/src/shared/constants/messages.ts +11 -0
- package/src/shared/constants/paymentMethodAPM.ts +63 -0
- package/src/shared/constants/tonderUrl.ts +8 -0
- package/src/types/card.ts +34 -0
- package/src/types/checkout.ts +118 -0
- package/src/types/commons.ts +37 -0
- package/src/types/customer.ts +12 -0
- package/src/types/index.d.ts +10 -0
- package/src/types/liteInlineCheckout.d.ts +191 -0
- package/src/types/paymentMethod.ts +24 -0
- package/src/types/requests.ts +12 -3
- package/src/types/validations.d.ts +11 -0
- package/tests/classes/liteCheckout.test.ts +3 -3
- package/tests/methods/getBusiness.test.ts +2 -2
- package/tests/methods/getCustomerCards.test.ts +4 -8
- package/tests/utils/defaultMock.ts +3 -2
- package/tests/utils/mockClasses.ts +7 -4
- package/tests/methods/getOpenpayDeviceSessionID.test.ts +0 -95
- package/tests/methods/getSkyflowToken.test.ts +0 -155
- package/tests/methods/getVaultToken.test.ts +0 -107
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
declare const PAYMENT_METHOD_APM: Readonly<{
|
|
2
|
+
SORIANA: "SORIANA";
|
|
3
|
+
OXXO: "OXXO";
|
|
4
|
+
SPEI: "SPEI";
|
|
5
|
+
CODI: "CODI";
|
|
6
|
+
MERCADOPAGO: "MERCADOPAGO";
|
|
7
|
+
PAYPAL: "PAYPAL";
|
|
8
|
+
COMERCIALMEXICANA: "COMERCIALMEXICANA";
|
|
9
|
+
BANCOMER: "BANCOMER";
|
|
10
|
+
WALMART: "WALMART";
|
|
11
|
+
BODEGA: "BODEGA";
|
|
12
|
+
SAMSCLUB: "SAMSCLUB";
|
|
13
|
+
SUPERAMA: "SUPERAMA";
|
|
14
|
+
CALIMAX: "CALIMAX";
|
|
15
|
+
EXTRA: "EXTRA";
|
|
16
|
+
CIRCULOK: "CIRCULOK";
|
|
17
|
+
SEVEN11: "7ELEVEN";
|
|
18
|
+
TELECOMM: "TELECOMM";
|
|
19
|
+
BANORTE: "BANORTE";
|
|
20
|
+
BENAVIDES: "BENAVIDES";
|
|
21
|
+
DELAHORRO: "DELAHORRO";
|
|
22
|
+
ELASTURIANO: "ELASTURIANO";
|
|
23
|
+
WALDOS: "WALDOS";
|
|
24
|
+
ALSUPER: "ALSUPER";
|
|
25
|
+
KIOSKO: "KIOSKO";
|
|
26
|
+
STAMARIA: "STAMARIA";
|
|
27
|
+
LAMASBARATA: "LAMASBARATA";
|
|
28
|
+
FARMROMA: "FARMROMA";
|
|
29
|
+
FARMUNION: "FARMUNION";
|
|
30
|
+
FARMATODO: "FARMATODO";
|
|
31
|
+
SFDEASIS: "SFDEASIS";
|
|
32
|
+
FARM911: "FARM911";
|
|
33
|
+
FARMECONOMICAS: "FARMECONOMICAS";
|
|
34
|
+
FARMMEDICITY: "FARMMEDICITY";
|
|
35
|
+
RIANXEIRA: "RIANXEIRA";
|
|
36
|
+
WESTERNUNION: "WESTERNUNION";
|
|
37
|
+
ZONAPAGO: "ZONAPAGO";
|
|
38
|
+
CAJALOSANDES: "CAJALOSANDES";
|
|
39
|
+
CAJAPAITA: "CAJAPAITA";
|
|
40
|
+
CAJASANTA: "CAJASANTA";
|
|
41
|
+
CAJASULLANA: "CAJASULLANA";
|
|
42
|
+
CAJATRUJILLO: "CAJATRUJILLO";
|
|
43
|
+
EDPYME: "EDPYME";
|
|
44
|
+
KASNET: "KASNET";
|
|
45
|
+
NORANDINO: "NORANDINO";
|
|
46
|
+
QAPAQ: "QAPAQ";
|
|
47
|
+
RAIZ: "RAIZ";
|
|
48
|
+
PAYSER: "PAYSER";
|
|
49
|
+
WUNION: "WUNION";
|
|
50
|
+
BANCOCONTINENTAL: "BANCOCONTINENTAL";
|
|
51
|
+
GMONEY: "GMONEY";
|
|
52
|
+
GOPAY: "GOPAY";
|
|
53
|
+
WU: "WU";
|
|
54
|
+
PUNTOSHEY: "PUNTOSHEY";
|
|
55
|
+
AMPM: "AMPM";
|
|
56
|
+
JUMBOMARKET: "JUMBOMARKET";
|
|
57
|
+
SMELPUEBLO: "SMELPUEBLO";
|
|
58
|
+
BAM: "BAM";
|
|
59
|
+
REFACIL: "REFACIL";
|
|
60
|
+
ACYVALORES: "ACYVALORES";
|
|
61
|
+
}>;
|
|
62
|
+
export { PAYMENT_METHOD_APM };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface ICard {
|
|
2
|
+
fields: ICardSkyflowFields;
|
|
3
|
+
icon?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ICardSkyflowFields {
|
|
6
|
+
card_number: string;
|
|
7
|
+
expiration_month: string;
|
|
8
|
+
expiration_year: string;
|
|
9
|
+
skyflow_id: string;
|
|
10
|
+
card_scheme: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ICustomerCardsResponse {
|
|
13
|
+
user_id: number;
|
|
14
|
+
cards: ICard[];
|
|
15
|
+
}
|
|
16
|
+
export interface ISaveCardResponse {
|
|
17
|
+
skyflow_id: string;
|
|
18
|
+
user_id: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ISaveCardSkyflowRequest {
|
|
21
|
+
skyflow_id: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ISaveCardRequest {
|
|
24
|
+
card_number: string;
|
|
25
|
+
cvv: string;
|
|
26
|
+
expiration_month: string;
|
|
27
|
+
expiration_year: string;
|
|
28
|
+
cardholder_name: string;
|
|
29
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ICustomer } from "./customer";
|
|
2
|
+
export interface IStartCheckoutRequestBase {
|
|
3
|
+
name: any;
|
|
4
|
+
last_name: string;
|
|
5
|
+
email_client: any;
|
|
6
|
+
phone_number: any;
|
|
7
|
+
return_url?: string;
|
|
8
|
+
id_product: string;
|
|
9
|
+
quantity_product: number;
|
|
10
|
+
id_ship: string;
|
|
11
|
+
instance_id_ship: string;
|
|
12
|
+
amount: any;
|
|
13
|
+
title_ship: string;
|
|
14
|
+
description: string;
|
|
15
|
+
device_session_id: any;
|
|
16
|
+
token_id: string;
|
|
17
|
+
order_id: any;
|
|
18
|
+
business_id: any;
|
|
19
|
+
payment_id: any;
|
|
20
|
+
source: string;
|
|
21
|
+
browser_info?: any;
|
|
22
|
+
metadata: any;
|
|
23
|
+
currency: string;
|
|
24
|
+
}
|
|
25
|
+
export type IStartCheckoutRequestWithCard = IStartCheckoutRequestBase & {
|
|
26
|
+
card: any;
|
|
27
|
+
payment_method?: never;
|
|
28
|
+
};
|
|
29
|
+
export type IStartCheckoutRequestWithPaymentMethod = IStartCheckoutRequestBase & {
|
|
30
|
+
card?: never;
|
|
31
|
+
payment_method: string;
|
|
32
|
+
};
|
|
33
|
+
export type IStartCheckoutRequest = IStartCheckoutRequestWithCard | IStartCheckoutRequestWithPaymentMethod;
|
|
34
|
+
export interface IStartCheckoutIdRequest {
|
|
35
|
+
checkout_id: string;
|
|
36
|
+
}
|
|
37
|
+
export interface IStartCheckoutErrorResponse {
|
|
38
|
+
status: string;
|
|
39
|
+
message: string;
|
|
40
|
+
psp_response: [
|
|
41
|
+
{
|
|
42
|
+
status: number;
|
|
43
|
+
response: Object;
|
|
44
|
+
}
|
|
45
|
+
];
|
|
46
|
+
checkout_id: string;
|
|
47
|
+
is_route_finished: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface IStartCheckoutResponse {
|
|
50
|
+
status: string;
|
|
51
|
+
message: string;
|
|
52
|
+
psp_response: Record<string, any>;
|
|
53
|
+
checkout_id: string;
|
|
54
|
+
is_route_finished: Boolean;
|
|
55
|
+
transaction_status: string;
|
|
56
|
+
transaction_id: number;
|
|
57
|
+
payment_id: number;
|
|
58
|
+
provider: string;
|
|
59
|
+
next_action: {
|
|
60
|
+
redirect_to_url: {
|
|
61
|
+
url: string;
|
|
62
|
+
return_url: string;
|
|
63
|
+
verify_transaction_status_url: string;
|
|
64
|
+
};
|
|
65
|
+
iframe_resources?: {
|
|
66
|
+
iframe: string;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
actions: IStartCheckoutActionResponse[];
|
|
70
|
+
}
|
|
71
|
+
export interface IStartCheckoutActionResponse {
|
|
72
|
+
name: string;
|
|
73
|
+
url: string;
|
|
74
|
+
method: string;
|
|
75
|
+
}
|
|
76
|
+
export interface IItem {
|
|
77
|
+
description: string;
|
|
78
|
+
quantity: number;
|
|
79
|
+
price_unit: number;
|
|
80
|
+
discount: number;
|
|
81
|
+
taxes: number;
|
|
82
|
+
product_reference: string | number;
|
|
83
|
+
name: string;
|
|
84
|
+
amount_total: number;
|
|
85
|
+
}
|
|
86
|
+
export interface IProcessPaymentRequest {
|
|
87
|
+
customer: ICustomer;
|
|
88
|
+
cart: {
|
|
89
|
+
total: string | number;
|
|
90
|
+
items: IItem[];
|
|
91
|
+
};
|
|
92
|
+
metadata?: Record<string, any>;
|
|
93
|
+
currency?: string;
|
|
94
|
+
payment_method?: string;
|
|
95
|
+
card?: ICardFields | string;
|
|
96
|
+
}
|
|
97
|
+
export interface ICardFields {
|
|
98
|
+
card_number: string;
|
|
99
|
+
cvv: string;
|
|
100
|
+
expiration_month: string;
|
|
101
|
+
expiration_year: string;
|
|
102
|
+
cardholder_name: string;
|
|
103
|
+
}
|
package/dist/types/commons.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ICustomer } from "./customer";
|
|
1
2
|
export type Business = {
|
|
2
3
|
business: {
|
|
3
4
|
pk: number;
|
|
@@ -76,3 +77,34 @@ export type APM = {
|
|
|
76
77
|
icon: string;
|
|
77
78
|
label: string;
|
|
78
79
|
};
|
|
80
|
+
export interface IConfigureCheckout {
|
|
81
|
+
customer: ICustomer;
|
|
82
|
+
}
|
|
83
|
+
export interface IInlineCheckoutBaseOptions {
|
|
84
|
+
mode?: "production" | "sandbox" | "stage" | "development";
|
|
85
|
+
/**
|
|
86
|
+
* @deprecated This property is deprecated and will be removed in a future release.
|
|
87
|
+
* `baseUrlTonder` is no longer required.
|
|
88
|
+
*/
|
|
89
|
+
baseUrlTonder?: string;
|
|
90
|
+
apiKey: string;
|
|
91
|
+
/**
|
|
92
|
+
* @deprecated This property is deprecated and will be removed in a future release.
|
|
93
|
+
* Use `apiKey` instead, as `apiKeyTonder` is no longer required.
|
|
94
|
+
*/
|
|
95
|
+
apiKeyTonder?: string;
|
|
96
|
+
returnUrl: string;
|
|
97
|
+
callBack?: (response: any) => void;
|
|
98
|
+
}
|
|
99
|
+
export interface IApiError {
|
|
100
|
+
code: string;
|
|
101
|
+
body: Record<string, string> | string;
|
|
102
|
+
name: string;
|
|
103
|
+
message: string;
|
|
104
|
+
}
|
|
105
|
+
export interface IPublicError {
|
|
106
|
+
status: string;
|
|
107
|
+
code: number;
|
|
108
|
+
message: string;
|
|
109
|
+
detail: Record<string, any> | string;
|
|
110
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface IPaymentMethodResponse {
|
|
2
|
+
count: number;
|
|
3
|
+
next: string | null;
|
|
4
|
+
previous: string | null;
|
|
5
|
+
results: ITonderPaymentMethod[];
|
|
6
|
+
}
|
|
7
|
+
export interface ITonderPaymentMethod {
|
|
8
|
+
pk: string;
|
|
9
|
+
payment_method: string;
|
|
10
|
+
priority: number;
|
|
11
|
+
category: string;
|
|
12
|
+
unavailable_countries: string[];
|
|
13
|
+
status: string;
|
|
14
|
+
}
|
|
15
|
+
export interface IPaymentMethod {
|
|
16
|
+
id: string;
|
|
17
|
+
payment_method: string;
|
|
18
|
+
priority: number;
|
|
19
|
+
category: string;
|
|
20
|
+
icon: string;
|
|
21
|
+
label: string;
|
|
22
|
+
}
|
package/dist/types/requests.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { OrderItem } from "./commons";
|
|
2
1
|
import { SkyflowRecord } from "./skyflow";
|
|
2
|
+
import { IItem } from "./checkout";
|
|
3
3
|
export interface CreateOrderRequest {
|
|
4
4
|
business: string;
|
|
5
5
|
client: string;
|
|
@@ -9,7 +9,7 @@ export interface CreateOrderRequest {
|
|
|
9
9
|
status?: string;
|
|
10
10
|
reference: string | number;
|
|
11
11
|
is_oneclick: boolean;
|
|
12
|
-
items:
|
|
12
|
+
items: IItem[];
|
|
13
13
|
}
|
|
14
14
|
export type CreatePaymentRequest = {
|
|
15
15
|
business_pk?: string | number;
|
|
@@ -61,6 +61,15 @@ export interface VaultRequest extends SkyflowRecord {
|
|
|
61
61
|
export type RegisterCustomerCardRequest = {
|
|
62
62
|
skyflow_id: string;
|
|
63
63
|
};
|
|
64
|
+
export type TokensSkyflowRequest = {
|
|
65
|
+
baseUrl: string;
|
|
66
|
+
apiKey: string;
|
|
67
|
+
vault_id: string;
|
|
68
|
+
vault_url: string;
|
|
69
|
+
data: {
|
|
70
|
+
[key: string]: any;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
64
73
|
export type TokensRequest = {
|
|
65
74
|
vault_id: string;
|
|
66
75
|
vault_url: string;
|
|
@@ -70,7 +79,7 @@ export type TokensRequest = {
|
|
|
70
79
|
};
|
|
71
80
|
export type StartCheckoutFullRequest = {
|
|
72
81
|
order: {
|
|
73
|
-
items:
|
|
82
|
+
items: IItem[];
|
|
74
83
|
};
|
|
75
84
|
total: number;
|
|
76
85
|
customer: {
|
package/package.json
CHANGED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import {ThreeDSHandler} from "./3dsHandler";
|
|
2
|
+
import {Business, IConfigureCheckout, IInlineCheckoutBaseOptions} from "../types/commons";
|
|
3
|
+
import {ErrorResponse} from "./errorResponse";
|
|
4
|
+
import {fetchBusiness} from "../data/businessApi";
|
|
5
|
+
import {injectMercadoPagoSecurity} from "../helpers/mercadopago";
|
|
6
|
+
import {TONDER_URL_BY_MODE} from "../shared/constants/tonderUrl";
|
|
7
|
+
import {ICardFields, IItem, IProcessPaymentRequest, IStartCheckoutResponse} from "../types/checkout";
|
|
8
|
+
import {StartCheckoutResponse} from "../types/responses";
|
|
9
|
+
import {createOrder, createPayment, startCheckoutRouter} from "../data/checkoutApi";
|
|
10
|
+
import {getOpenpayDeviceSessionID} from "../data/openPayApi";
|
|
11
|
+
import {getBrowserInfo} from "../helpers/utils";
|
|
12
|
+
import {registerOrFetchCustomer} from "../data/customerApi";
|
|
13
|
+
import {ICustomer} from "../types/customer";
|
|
14
|
+
|
|
15
|
+
export class BaseInlineCheckout {
|
|
16
|
+
baseUrl = "";
|
|
17
|
+
cartTotal = "0";
|
|
18
|
+
process3ds: ThreeDSHandler;
|
|
19
|
+
mode?: "production" | "sandbox" | "stage" | "development" | undefined;
|
|
20
|
+
apiKeyTonder: string;
|
|
21
|
+
returnUrl: string;
|
|
22
|
+
callBack?: ((response: any) => void) | undefined;
|
|
23
|
+
merchantData?: Business;
|
|
24
|
+
abortController: AbortController;
|
|
25
|
+
|
|
26
|
+
firstName?: string
|
|
27
|
+
lastName?: string
|
|
28
|
+
country?: string
|
|
29
|
+
address?: string
|
|
30
|
+
city?: string
|
|
31
|
+
state?: string
|
|
32
|
+
postCode?: string
|
|
33
|
+
email?: string
|
|
34
|
+
phone?: string
|
|
35
|
+
customer?: ICustomer
|
|
36
|
+
|
|
37
|
+
cartItems?: IItem[]
|
|
38
|
+
metadata = {}
|
|
39
|
+
card? = {}
|
|
40
|
+
currency?: string = ""
|
|
41
|
+
|
|
42
|
+
#customerData?: Record<string, any>;
|
|
43
|
+
|
|
44
|
+
constructor({ mode = "stage", apiKey, apiKeyTonder, returnUrl, callBack = () => {} }: IInlineCheckoutBaseOptions) {
|
|
45
|
+
this.apiKeyTonder = apiKey || apiKeyTonder || '';
|
|
46
|
+
this.returnUrl = returnUrl;
|
|
47
|
+
this.callBack = callBack;
|
|
48
|
+
this.mode = mode;
|
|
49
|
+
this.baseUrl = TONDER_URL_BY_MODE[this.mode] || TONDER_URL_BY_MODE["stage"];
|
|
50
|
+
this.abortController = new AbortController();
|
|
51
|
+
this.process3ds = new ThreeDSHandler({
|
|
52
|
+
apiKey: apiKey,
|
|
53
|
+
baseUrl: this.baseUrl,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
configureCheckout(data: IConfigureCheckout) {
|
|
59
|
+
if ("customer" in data) this.#handleCustomer(data["customer"]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async verify3dsTransaction () {
|
|
63
|
+
const result3ds = await this.process3ds.verifyTransactionStatus()
|
|
64
|
+
const resultCheckout = await this.#resumeCheckout(result3ds)
|
|
65
|
+
this.process3ds.setPayload(resultCheckout)
|
|
66
|
+
return this._handle3dsRedirect(resultCheckout)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
payment(data: IProcessPaymentRequest): Promise<IStartCheckoutResponse> {
|
|
70
|
+
return new Promise(async (resolve, reject) => {
|
|
71
|
+
try {
|
|
72
|
+
this.#handleCustomer(data.customer);
|
|
73
|
+
this._setCartTotal(data.cart?.total);
|
|
74
|
+
this.#setCartItems(data.cart?.items);
|
|
75
|
+
this.#handleMetadata(data);
|
|
76
|
+
this.#handleCurrency(data);
|
|
77
|
+
this.#handleCard(data);
|
|
78
|
+
const response = await this._checkout(data);
|
|
79
|
+
this.process3ds.setPayload(response);
|
|
80
|
+
if(this.callBack)
|
|
81
|
+
this.callBack!(response);
|
|
82
|
+
const payload = await this._handle3dsRedirect(response);
|
|
83
|
+
if (payload) {
|
|
84
|
+
resolve(response);
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
reject(error);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _initializeCheckout() {
|
|
93
|
+
const business_response = await this._fetchMerchantData();
|
|
94
|
+
|
|
95
|
+
if (!!business_response && !!business_response.mercado_pago && business_response.mercado_pago.active) {
|
|
96
|
+
injectMercadoPagoSecurity();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async _checkout(data: any): Promise<any> {
|
|
101
|
+
throw new Error(
|
|
102
|
+
"The #checkout method should be implement in child classes.",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
_setCartTotal(total: string | number) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"The #setCartTotal method should be implement in child classes.",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async _getCustomer(signal: AbortSignal | null = null) {
|
|
113
|
+
if (!!this.#customerData) return this.#customerData!;
|
|
114
|
+
|
|
115
|
+
this.#customerData = await registerOrFetchCustomer(
|
|
116
|
+
this.baseUrl,
|
|
117
|
+
this.apiKeyTonder,
|
|
118
|
+
this.customer!,
|
|
119
|
+
signal
|
|
120
|
+
);
|
|
121
|
+
return this.#customerData!;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async _handleCheckout({ card, payment_method, customer, isSandbox }: {card?: string; payment_method?: string; customer: Record<string, any>; isSandbox?: boolean}) {
|
|
125
|
+
const { openpay_keys, reference, business } = this.merchantData!;
|
|
126
|
+
const total = Number(this.cartTotal);
|
|
127
|
+
try {
|
|
128
|
+
let deviceSessionIdTonder;
|
|
129
|
+
if (
|
|
130
|
+
!deviceSessionIdTonder &&
|
|
131
|
+
openpay_keys.merchant_id &&
|
|
132
|
+
openpay_keys.public_key
|
|
133
|
+
) {
|
|
134
|
+
deviceSessionIdTonder = await getOpenpayDeviceSessionID(
|
|
135
|
+
openpay_keys.merchant_id,
|
|
136
|
+
openpay_keys.public_key,
|
|
137
|
+
isSandbox,
|
|
138
|
+
this.abortController.signal,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const { id, auth_token } = customer;
|
|
143
|
+
|
|
144
|
+
const orderItems = {
|
|
145
|
+
business: this.apiKeyTonder,
|
|
146
|
+
client: auth_token,
|
|
147
|
+
billing_address_id: null,
|
|
148
|
+
shipping_address_id: null,
|
|
149
|
+
amount: total,
|
|
150
|
+
status: "A",
|
|
151
|
+
reference: reference,
|
|
152
|
+
is_oneclick: true,
|
|
153
|
+
items: this.cartItems!,
|
|
154
|
+
};
|
|
155
|
+
const jsonResponseOrder = await createOrder(
|
|
156
|
+
this.baseUrl,
|
|
157
|
+
this.apiKeyTonder,
|
|
158
|
+
orderItems,
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// Create payment
|
|
162
|
+
const now = new Date();
|
|
163
|
+
const dateString = now.toISOString();
|
|
164
|
+
|
|
165
|
+
const paymentItems = {
|
|
166
|
+
business_pk: business.pk,
|
|
167
|
+
client_id: id,
|
|
168
|
+
amount: total,
|
|
169
|
+
date: dateString,
|
|
170
|
+
order_id: jsonResponseOrder.id,
|
|
171
|
+
};
|
|
172
|
+
const jsonResponsePayment = await createPayment(
|
|
173
|
+
this.baseUrl,
|
|
174
|
+
this.apiKeyTonder,
|
|
175
|
+
paymentItems,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// Checkout router
|
|
179
|
+
const routerItems = {
|
|
180
|
+
name: this.firstName || "",
|
|
181
|
+
last_name: this.lastName || "",
|
|
182
|
+
email_client: this.email,
|
|
183
|
+
phone_number: this.phone,
|
|
184
|
+
return_url: this.returnUrl,
|
|
185
|
+
id_product: "no_id",
|
|
186
|
+
quantity_product: 1,
|
|
187
|
+
id_ship: "0",
|
|
188
|
+
instance_id_ship: "0",
|
|
189
|
+
amount: total,
|
|
190
|
+
title_ship: "shipping",
|
|
191
|
+
description: "transaction",
|
|
192
|
+
device_session_id: deviceSessionIdTonder ? deviceSessionIdTonder : null,
|
|
193
|
+
token_id: "",
|
|
194
|
+
order_id: jsonResponseOrder.id,
|
|
195
|
+
business_id: business.pk,
|
|
196
|
+
payment_id: jsonResponsePayment.pk,
|
|
197
|
+
source: "sdk",
|
|
198
|
+
metadata: this.metadata,
|
|
199
|
+
browser_info: getBrowserInfo(),
|
|
200
|
+
currency: this.currency!,
|
|
201
|
+
...(!!payment_method ? { payment_method } : { card }),
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const jsonResponseRouter = await startCheckoutRouter(
|
|
205
|
+
this.baseUrl,
|
|
206
|
+
this.apiKeyTonder,
|
|
207
|
+
routerItems,
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
if (jsonResponseRouter) {
|
|
211
|
+
return jsonResponseRouter;
|
|
212
|
+
} else {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.log(error);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
async _fetchMerchantData() {
|
|
223
|
+
try {
|
|
224
|
+
if (!this.merchantData){
|
|
225
|
+
this.merchantData = await fetchBusiness(this.baseUrl, this.apiKeyTonder, this.abortController.signal);
|
|
226
|
+
}
|
|
227
|
+
return this.merchantData
|
|
228
|
+
}catch(e){
|
|
229
|
+
return this.merchantData
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
#handleCustomer(customer: ICustomer) {
|
|
234
|
+
if (!customer) return;
|
|
235
|
+
|
|
236
|
+
this.firstName = customer?.firstName;
|
|
237
|
+
this.lastName = customer?.lastName;
|
|
238
|
+
this.country = customer?.country;
|
|
239
|
+
this.address = customer?.street;
|
|
240
|
+
this.city = customer?.city;
|
|
241
|
+
this.state = customer?.state;
|
|
242
|
+
this.postCode = customer?.postCode;
|
|
243
|
+
this.email = customer?.email;
|
|
244
|
+
this.phone = customer?.phone;
|
|
245
|
+
this.customer = customer;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
#setCartItems(items: IItem[]) {
|
|
249
|
+
this.cartItems = items;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
#handleMetadata(data: { metadata?: any }) {
|
|
253
|
+
this.metadata = data?.metadata;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
#handleCurrency(data: { currency?: string }) {
|
|
257
|
+
this.currency = data?.currency;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
#handleCard(data: { card?: ICardFields | string }) {
|
|
261
|
+
this.card = data?.card;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// TODO: Make private after remove deprecated functions of liteCheckout
|
|
265
|
+
async _handle3dsRedirect(response: ErrorResponse | StartCheckoutResponse | false | undefined) {
|
|
266
|
+
const iframe = response && 'next_action' in response ? response?.next_action?.iframe_resources?.iframe:null
|
|
267
|
+
|
|
268
|
+
if (iframe) {
|
|
269
|
+
this.process3ds.loadIframe()!.then(() => {
|
|
270
|
+
//TODO: Check if this will be necessary on the frontend side
|
|
271
|
+
// after some the tests in production, since the 3DS process
|
|
272
|
+
// doesn't works properly on the sandbox environment
|
|
273
|
+
// setTimeout(() => {
|
|
274
|
+
// process3ds.verifyTransactionStatus();
|
|
275
|
+
// }, 10000);
|
|
276
|
+
this.process3ds.verifyTransactionStatus();
|
|
277
|
+
}).catch((error) => {
|
|
278
|
+
console.log('Error loading iframe:', error)
|
|
279
|
+
})
|
|
280
|
+
} else {
|
|
281
|
+
const redirectUrl = this.process3ds.getRedirectUrl()
|
|
282
|
+
if (redirectUrl) {
|
|
283
|
+
this.process3ds.redirectToChallenge()
|
|
284
|
+
} else {
|
|
285
|
+
return response;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async #resumeCheckout(response: any) {
|
|
291
|
+
// Stop the routing process if the transaction is either hard declined or successful
|
|
292
|
+
if (response?.decline?.error_type === "Hard") {
|
|
293
|
+
return response
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (["Success", "Authorized"].includes(response?.transaction_status)) {
|
|
297
|
+
return response;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (response) {
|
|
301
|
+
const routerItems = {
|
|
302
|
+
checkout_id: response.checkout?.id,
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
try{
|
|
306
|
+
return await startCheckoutRouter(
|
|
307
|
+
this.baseUrl,
|
|
308
|
+
this.apiKeyTonder,
|
|
309
|
+
routerItems
|
|
310
|
+
)
|
|
311
|
+
}catch (error){
|
|
312
|
+
// throw error
|
|
313
|
+
}
|
|
314
|
+
return response
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
}
|