@tonder.io/ionic-lite-sdk 0.0.35-beta.7 → 0.0.36-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/.gitlab-ci.yml +28 -28
- package/README.md +221 -202
- package/dist/classes/errorResponse.d.ts +1 -1
- package/dist/classes/liteCheckout.d.ts +32 -56
- package/dist/data/api.d.ts +1 -1
- package/dist/helpers/utils.d.ts +4 -8
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -1
- package/dist/types/commons.d.ts +0 -37
- package/dist/types/requests.d.ts +3 -12
- package/dist/types/responses.d.ts +3 -0
- package/jest.config.ts +14 -14
- package/package.json +38 -38
- package/rollup.config.js +16 -16
- package/src/classes/3dsHandler.ts +237 -237
- package/src/classes/errorResponse.ts +16 -16
- package/src/classes/liteCheckout.ts +575 -598
- package/src/data/api.ts +20 -20
- package/src/helpers/constants.ts +63 -63
- package/src/helpers/mercadopago.ts +15 -15
- package/src/helpers/utils.ts +320 -120
- package/src/index.ts +4 -10
- package/src/types/commons.ts +83 -125
- package/src/types/requests.ts +105 -114
- package/src/types/responses.ts +193 -189
- 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 +119 -113
- package/tests/methods/getOpenpayDeviceSessionID.test.ts +95 -0
- package/tests/methods/getSkyflowToken.test.ts +155 -0
- package/tests/methods/getVaultToken.test.ts +107 -0
- 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 -21
- package/tests/utils/mockClasses.ts +656 -659
- package/tsconfig.json +18 -18
- package/.idea/aws.xml +0 -17
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/prettier.xml +0 -6
- package/.idea/vcs.xml +0 -6
- package/.idea/workspace.xml +0 -132
- package/dist/classes/BaseInlineCheckout.d.ts +0 -45
- package/dist/data/businessApi.d.ts +0 -2
- package/dist/data/cardApi.d.ts +0 -4
- package/dist/data/checkoutApi.d.ts +0 -4
- package/dist/data/customerApi.d.ts +0 -2
- package/dist/data/openPayApi.d.ts +0 -1
- package/dist/data/paymentMethodApi.d.ts +0 -5
- package/dist/data/skyflowApi.d.ts +0 -1
- package/dist/helpers/skyflow.d.ts +0 -3
- package/dist/helpers/validations.d.ts +0 -6
- package/dist/shared/catalog/paymentMethodsCatalog.d.ts +0 -1
- package/dist/shared/constants/messages.d.ts +0 -11
- package/dist/shared/constants/paymentMethodAPM.d.ts +0 -62
- package/dist/shared/constants/tonderUrl.d.ts +0 -7
- package/dist/types/card.d.ts +0 -29
- package/dist/types/checkout.d.ts +0 -103
- package/dist/types/customer.d.ts +0 -12
- package/dist/types/paymentMethod.d.ts +0 -22
- package/src/classes/BaseInlineCheckout.ts +0 -356
- package/src/data/businessApi.ts +0 -18
- package/src/data/cardApi.ts +0 -89
- package/src/data/checkoutApi.ts +0 -87
- package/src/data/customerApi.ts +0 -31
- package/src/data/openPayApi.ts +0 -12
- package/src/data/paymentMethodApi.ts +0 -37
- package/src/data/skyflowApi.ts +0 -20
- package/src/helpers/skyflow.ts +0 -91
- package/src/helpers/validations.ts +0 -55
- package/src/shared/catalog/paymentMethodsCatalog.ts +0 -248
- package/src/shared/constants/messages.ts +0 -11
- package/src/shared/constants/paymentMethodAPM.ts +0 -63
- package/src/shared/constants/tonderUrl.ts +0 -8
- package/src/types/card.ts +0 -34
- package/src/types/checkout.ts +0 -118
- package/src/types/customer.ts +0 -12
- package/src/types/index.d.ts +0 -10
- package/src/types/liteInlineCheckout.d.ts +0 -191
- package/src/types/paymentMethod.ts +0 -24
- package/src/types/validations.d.ts +0 -11
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
import { ThreeDSHandler } from "./3dsHandler";
|
|
2
|
-
import { ErrorResponse } from "./errorResponse";
|
|
3
|
-
import { fetchBusiness } from "../data/businessApi";
|
|
4
|
-
import { injectMercadoPagoSecurity } from "../helpers/mercadopago";
|
|
5
|
-
import { TONDER_URL_BY_MODE } from "../shared/constants/tonderUrl";
|
|
6
|
-
import {
|
|
7
|
-
createOrder,
|
|
8
|
-
createPayment,
|
|
9
|
-
startCheckoutRouter,
|
|
10
|
-
} from "../data/checkoutApi";
|
|
11
|
-
import { getOpenpayDeviceSessionID } from "../data/openPayApi";
|
|
12
|
-
import { getBrowserInfo } from "../helpers/utils";
|
|
13
|
-
import { registerOrFetchCustomer } from "../data/customerApi";
|
|
14
|
-
import {
|
|
15
|
-
Business,
|
|
16
|
-
ICustomer,
|
|
17
|
-
IItem,
|
|
18
|
-
IInlineCheckoutBaseOptions,
|
|
19
|
-
IConfigureCheckout,
|
|
20
|
-
IProcessPaymentRequest,
|
|
21
|
-
IStartCheckoutResponse,
|
|
22
|
-
StartCheckoutResponse,
|
|
23
|
-
ICardFields,
|
|
24
|
-
} from "../types";
|
|
25
|
-
export class BaseInlineCheckout {
|
|
26
|
-
baseUrl = "";
|
|
27
|
-
cartTotal = "0";
|
|
28
|
-
process3ds: ThreeDSHandler;
|
|
29
|
-
mode?: "production" | "sandbox" | "stage" | "development" | undefined;
|
|
30
|
-
apiKeyTonder: string;
|
|
31
|
-
returnUrl: string;
|
|
32
|
-
callBack?: ((response: any) => void) | undefined;
|
|
33
|
-
merchantData?: Business;
|
|
34
|
-
abortController: AbortController;
|
|
35
|
-
|
|
36
|
-
firstName?: string;
|
|
37
|
-
lastName?: string;
|
|
38
|
-
country?: string;
|
|
39
|
-
address?: string;
|
|
40
|
-
city?: string;
|
|
41
|
-
state?: string;
|
|
42
|
-
postCode?: string;
|
|
43
|
-
email?: string;
|
|
44
|
-
phone?: string;
|
|
45
|
-
customer?: ICustomer;
|
|
46
|
-
|
|
47
|
-
cartItems?: IItem[];
|
|
48
|
-
metadata = {};
|
|
49
|
-
card? = {};
|
|
50
|
-
currency?: string = "";
|
|
51
|
-
|
|
52
|
-
#customerData?: Record<string, any>;
|
|
53
|
-
|
|
54
|
-
constructor({
|
|
55
|
-
mode = "stage",
|
|
56
|
-
apiKey,
|
|
57
|
-
apiKeyTonder,
|
|
58
|
-
returnUrl,
|
|
59
|
-
callBack = () => {},
|
|
60
|
-
}: IInlineCheckoutBaseOptions) {
|
|
61
|
-
this.apiKeyTonder = apiKey || apiKeyTonder || "";
|
|
62
|
-
this.returnUrl = returnUrl;
|
|
63
|
-
this.callBack = callBack;
|
|
64
|
-
this.mode = mode;
|
|
65
|
-
this.baseUrl = TONDER_URL_BY_MODE[this.mode] || TONDER_URL_BY_MODE["stage"];
|
|
66
|
-
this.abortController = new AbortController();
|
|
67
|
-
this.process3ds = new ThreeDSHandler({
|
|
68
|
-
apiKey: apiKey,
|
|
69
|
-
baseUrl: this.baseUrl,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
configureCheckout(data: IConfigureCheckout) {
|
|
74
|
-
if ("customer" in data) this.#handleCustomer(data["customer"]);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async verify3dsTransaction() {
|
|
78
|
-
const result3ds = await this.process3ds.verifyTransactionStatus();
|
|
79
|
-
const resultCheckout = await this.#resumeCheckout(result3ds);
|
|
80
|
-
this.process3ds.setPayload(resultCheckout);
|
|
81
|
-
return this._handle3dsRedirect(resultCheckout);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
payment(data: IProcessPaymentRequest): Promise<IStartCheckoutResponse> {
|
|
85
|
-
return new Promise(async (resolve, reject) => {
|
|
86
|
-
try {
|
|
87
|
-
this.#handleCustomer(data.customer);
|
|
88
|
-
this._setCartTotal(data.cart?.total);
|
|
89
|
-
this.#setCartItems(data.cart?.items);
|
|
90
|
-
this.#handleMetadata(data);
|
|
91
|
-
this.#handleCurrency(data);
|
|
92
|
-
this.#handleCard(data);
|
|
93
|
-
const response = await this._checkout(data);
|
|
94
|
-
this.process3ds.setPayload(response);
|
|
95
|
-
if (this.callBack) this.callBack!(response);
|
|
96
|
-
const payload = await this._handle3dsRedirect(response);
|
|
97
|
-
if (payload) {
|
|
98
|
-
resolve(response);
|
|
99
|
-
}
|
|
100
|
-
} catch (error) {
|
|
101
|
-
reject(error);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async _initializeCheckout() {
|
|
107
|
-
const business_response = await this._fetchMerchantData();
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
!!business_response &&
|
|
111
|
-
!!business_response.mercado_pago &&
|
|
112
|
-
business_response.mercado_pago.active
|
|
113
|
-
) {
|
|
114
|
-
injectMercadoPagoSecurity();
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async _checkout(data: any): Promise<any> {
|
|
119
|
-
throw new Error(
|
|
120
|
-
"The #checkout method should be implement in child classes.",
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
_setCartTotal(total: string | number) {
|
|
125
|
-
throw new Error(
|
|
126
|
-
"The #setCartTotal method should be implement in child classes.",
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async _getCustomer(signal: AbortSignal | null = null) {
|
|
131
|
-
if (!!this.#customerData) return this.#customerData!;
|
|
132
|
-
|
|
133
|
-
this.#customerData = await registerOrFetchCustomer(
|
|
134
|
-
this.baseUrl,
|
|
135
|
-
this.apiKeyTonder,
|
|
136
|
-
this.customer!,
|
|
137
|
-
signal,
|
|
138
|
-
);
|
|
139
|
-
return this.#customerData!;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async _handleCheckout({
|
|
143
|
-
card,
|
|
144
|
-
payment_method,
|
|
145
|
-
customer,
|
|
146
|
-
isSandbox,
|
|
147
|
-
}: {
|
|
148
|
-
card?: string;
|
|
149
|
-
payment_method?: string;
|
|
150
|
-
customer: Record<string, any>;
|
|
151
|
-
isSandbox?: boolean;
|
|
152
|
-
}) {
|
|
153
|
-
const { openpay_keys, reference, business } = this.merchantData!;
|
|
154
|
-
const total = Number(this.cartTotal);
|
|
155
|
-
try {
|
|
156
|
-
let deviceSessionIdTonder;
|
|
157
|
-
if (
|
|
158
|
-
!deviceSessionIdTonder &&
|
|
159
|
-
openpay_keys.merchant_id &&
|
|
160
|
-
openpay_keys.public_key
|
|
161
|
-
) {
|
|
162
|
-
deviceSessionIdTonder = await getOpenpayDeviceSessionID(
|
|
163
|
-
openpay_keys.merchant_id,
|
|
164
|
-
openpay_keys.public_key,
|
|
165
|
-
isSandbox,
|
|
166
|
-
this.abortController.signal,
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const { id, auth_token } = customer;
|
|
171
|
-
|
|
172
|
-
const orderItems = {
|
|
173
|
-
business: this.apiKeyTonder,
|
|
174
|
-
client: auth_token,
|
|
175
|
-
billing_address_id: null,
|
|
176
|
-
shipping_address_id: null,
|
|
177
|
-
amount: total,
|
|
178
|
-
status: "A",
|
|
179
|
-
reference: reference,
|
|
180
|
-
is_oneclick: true,
|
|
181
|
-
items: this.cartItems!,
|
|
182
|
-
};
|
|
183
|
-
const jsonResponseOrder = await createOrder(
|
|
184
|
-
this.baseUrl,
|
|
185
|
-
this.apiKeyTonder,
|
|
186
|
-
orderItems,
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
// Create payment
|
|
190
|
-
const now = new Date();
|
|
191
|
-
const dateString = now.toISOString();
|
|
192
|
-
|
|
193
|
-
const paymentItems = {
|
|
194
|
-
business_pk: business.pk,
|
|
195
|
-
client_id: id,
|
|
196
|
-
amount: total,
|
|
197
|
-
date: dateString,
|
|
198
|
-
order_id: jsonResponseOrder.id,
|
|
199
|
-
};
|
|
200
|
-
const jsonResponsePayment = await createPayment(
|
|
201
|
-
this.baseUrl,
|
|
202
|
-
this.apiKeyTonder,
|
|
203
|
-
paymentItems,
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
// Checkout router
|
|
207
|
-
const routerItems = {
|
|
208
|
-
name: this.firstName || "",
|
|
209
|
-
last_name: this.lastName || "",
|
|
210
|
-
email_client: this.email,
|
|
211
|
-
phone_number: this.phone,
|
|
212
|
-
return_url: this.returnUrl,
|
|
213
|
-
id_product: "no_id",
|
|
214
|
-
quantity_product: 1,
|
|
215
|
-
id_ship: "0",
|
|
216
|
-
instance_id_ship: "0",
|
|
217
|
-
amount: total,
|
|
218
|
-
title_ship: "shipping",
|
|
219
|
-
description: "transaction",
|
|
220
|
-
device_session_id: deviceSessionIdTonder ? deviceSessionIdTonder : null,
|
|
221
|
-
token_id: "",
|
|
222
|
-
order_id: jsonResponseOrder.id,
|
|
223
|
-
business_id: business.pk,
|
|
224
|
-
payment_id: jsonResponsePayment.pk,
|
|
225
|
-
source: "sdk",
|
|
226
|
-
metadata: this.metadata,
|
|
227
|
-
browser_info: getBrowserInfo(),
|
|
228
|
-
currency: this.currency!,
|
|
229
|
-
...(!!payment_method ? { payment_method } : { card }),
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
const jsonResponseRouter = await startCheckoutRouter(
|
|
233
|
-
this.baseUrl,
|
|
234
|
-
this.apiKeyTonder,
|
|
235
|
-
routerItems,
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
if (jsonResponseRouter) {
|
|
239
|
-
return jsonResponseRouter;
|
|
240
|
-
} else {
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
} catch (error) {
|
|
244
|
-
console.log(error);
|
|
245
|
-
throw error;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
async _fetchMerchantData() {
|
|
250
|
-
try {
|
|
251
|
-
if (!this.merchantData) {
|
|
252
|
-
this.merchantData = await fetchBusiness(
|
|
253
|
-
this.baseUrl,
|
|
254
|
-
this.apiKeyTonder,
|
|
255
|
-
this.abortController.signal,
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
return this.merchantData;
|
|
259
|
-
} catch (e) {
|
|
260
|
-
return this.merchantData;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
#handleCustomer(customer: ICustomer) {
|
|
265
|
-
if (!customer) return;
|
|
266
|
-
|
|
267
|
-
this.firstName = customer?.firstName;
|
|
268
|
-
this.lastName = customer?.lastName;
|
|
269
|
-
this.country = customer?.country;
|
|
270
|
-
this.address = customer?.street;
|
|
271
|
-
this.city = customer?.city;
|
|
272
|
-
this.state = customer?.state;
|
|
273
|
-
this.postCode = customer?.postCode;
|
|
274
|
-
this.email = customer?.email;
|
|
275
|
-
this.phone = customer?.phone;
|
|
276
|
-
this.customer = customer;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
#setCartItems(items: IItem[]) {
|
|
280
|
-
this.cartItems = items;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
#handleMetadata(data: { metadata?: any }) {
|
|
284
|
-
this.metadata = data?.metadata;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
#handleCurrency(data: { currency?: string }) {
|
|
288
|
-
this.currency = data?.currency;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
#handleCard(data: { card?: ICardFields | string }) {
|
|
292
|
-
this.card = data?.card;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// TODO: Make private after remove deprecated functions of liteCheckout
|
|
296
|
-
async _handle3dsRedirect(
|
|
297
|
-
response: ErrorResponse | StartCheckoutResponse | false | undefined,
|
|
298
|
-
) {
|
|
299
|
-
const iframe =
|
|
300
|
-
response && "next_action" in response
|
|
301
|
-
? response?.next_action?.iframe_resources?.iframe
|
|
302
|
-
: null;
|
|
303
|
-
|
|
304
|
-
if (iframe) {
|
|
305
|
-
this.process3ds
|
|
306
|
-
.loadIframe()!
|
|
307
|
-
.then(() => {
|
|
308
|
-
//TODO: Check if this will be necessary on the frontend side
|
|
309
|
-
// after some the tests in production, since the 3DS process
|
|
310
|
-
// doesn't works properly on the sandbox environment
|
|
311
|
-
// setTimeout(() => {
|
|
312
|
-
// process3ds.verifyTransactionStatus();
|
|
313
|
-
// }, 10000);
|
|
314
|
-
this.process3ds.verifyTransactionStatus();
|
|
315
|
-
})
|
|
316
|
-
.catch((error) => {
|
|
317
|
-
console.log("Error loading iframe:", error);
|
|
318
|
-
});
|
|
319
|
-
} else {
|
|
320
|
-
const redirectUrl = this.process3ds.getRedirectUrl();
|
|
321
|
-
if (redirectUrl) {
|
|
322
|
-
this.process3ds.redirectToChallenge();
|
|
323
|
-
} else {
|
|
324
|
-
return response;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
async #resumeCheckout(response: any) {
|
|
330
|
-
// Stop the routing process if the transaction is either hard declined or successful
|
|
331
|
-
if (response?.decline?.error_type === "Hard") {
|
|
332
|
-
return response;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
if (["Success", "Authorized"].includes(response?.transaction_status)) {
|
|
336
|
-
return response;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
if (response) {
|
|
340
|
-
const routerItems = {
|
|
341
|
-
checkout_id: response.checkout?.id,
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
try {
|
|
345
|
-
return await startCheckoutRouter(
|
|
346
|
-
this.baseUrl,
|
|
347
|
-
this.apiKeyTonder,
|
|
348
|
-
routerItems,
|
|
349
|
-
);
|
|
350
|
-
} catch (error) {
|
|
351
|
-
// throw error
|
|
352
|
-
}
|
|
353
|
-
return response;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
package/src/data/businessApi.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { GetBusinessResponse } from "../types";
|
|
2
|
-
|
|
3
|
-
export async function fetchBusiness(
|
|
4
|
-
baseUrl: string,
|
|
5
|
-
apiKey: string,
|
|
6
|
-
signal: AbortSignal,
|
|
7
|
-
): Promise<GetBusinessResponse> {
|
|
8
|
-
const getBusiness = await fetch(
|
|
9
|
-
`${baseUrl}/api/v1/payments/business/${apiKey}`,
|
|
10
|
-
{
|
|
11
|
-
headers: {
|
|
12
|
-
Authorization: `Token ${apiKey}`,
|
|
13
|
-
},
|
|
14
|
-
signal: signal,
|
|
15
|
-
},
|
|
16
|
-
);
|
|
17
|
-
return await getBusiness.json();
|
|
18
|
-
}
|
package/src/data/cardApi.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildErrorResponse,
|
|
3
|
-
buildErrorResponseFromCatch,
|
|
4
|
-
} from "../helpers/utils";
|
|
5
|
-
import {
|
|
6
|
-
ICustomerCardsResponse,
|
|
7
|
-
ISaveCardResponse,
|
|
8
|
-
ISaveCardSkyflowRequest,
|
|
9
|
-
} from "../types";
|
|
10
|
-
import { MESSAGES } from "../shared/constants/messages";
|
|
11
|
-
|
|
12
|
-
export async function fetchCustomerCards(
|
|
13
|
-
baseUrl: string,
|
|
14
|
-
customerToken: string,
|
|
15
|
-
businessId: string | number,
|
|
16
|
-
signal = null,
|
|
17
|
-
): Promise<ICustomerCardsResponse> {
|
|
18
|
-
try {
|
|
19
|
-
const url = `${baseUrl}/api/v1/business/${businessId}/cards/`;
|
|
20
|
-
const response = await fetch(url, {
|
|
21
|
-
method: "GET",
|
|
22
|
-
headers: {
|
|
23
|
-
Authorization: `Token ${customerToken}`,
|
|
24
|
-
"Content-Type": "application/json",
|
|
25
|
-
},
|
|
26
|
-
signal,
|
|
27
|
-
});
|
|
28
|
-
if (response.ok) return await response.json();
|
|
29
|
-
const res_json = await response.json();
|
|
30
|
-
|
|
31
|
-
throw await buildErrorResponse(response, res_json);
|
|
32
|
-
} catch (error) {
|
|
33
|
-
throw buildErrorResponseFromCatch(error);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export async function saveCustomerCard(
|
|
38
|
-
baseUrl: string,
|
|
39
|
-
customerToken: string,
|
|
40
|
-
businessId: string | number,
|
|
41
|
-
data: ISaveCardSkyflowRequest,
|
|
42
|
-
): Promise<ISaveCardResponse> {
|
|
43
|
-
try {
|
|
44
|
-
const url = `${baseUrl}/api/v1/business/${businessId}/cards/`;
|
|
45
|
-
const response = await fetch(url, {
|
|
46
|
-
method: "POST",
|
|
47
|
-
headers: {
|
|
48
|
-
Authorization: `Token ${customerToken}`,
|
|
49
|
-
"Content-Type": "application/json",
|
|
50
|
-
},
|
|
51
|
-
body: JSON.stringify(data),
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
if (response.ok) return await response.json();
|
|
55
|
-
|
|
56
|
-
const res_json = await response.json();
|
|
57
|
-
|
|
58
|
-
throw await buildErrorResponse(response, res_json);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw buildErrorResponseFromCatch(error);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export async function removeCustomerCard(
|
|
65
|
-
baseUrl: string,
|
|
66
|
-
customerToken: string,
|
|
67
|
-
skyflowId = "",
|
|
68
|
-
businessId: string | number,
|
|
69
|
-
): Promise<string> {
|
|
70
|
-
try {
|
|
71
|
-
const url = `${baseUrl}/api/v1/business/${businessId}/cards/${skyflowId}`;
|
|
72
|
-
|
|
73
|
-
const response = await fetch(url, {
|
|
74
|
-
method: "DELETE",
|
|
75
|
-
headers: {
|
|
76
|
-
Authorization: `Token ${customerToken}`,
|
|
77
|
-
"Content-Type": "application/json",
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (response.status === 204) return MESSAGES.cardSaved;
|
|
82
|
-
if (response.ok && "json" in response) return await response.json();
|
|
83
|
-
const res_json = await response.json();
|
|
84
|
-
|
|
85
|
-
throw await buildErrorResponse(response, res_json);
|
|
86
|
-
} catch (error) {
|
|
87
|
-
throw buildErrorResponseFromCatch(error);
|
|
88
|
-
}
|
|
89
|
-
}
|
package/src/data/checkoutApi.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
declare const MP_DEVICE_SESSION_ID: string | undefined;
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
CreateOrderRequest,
|
|
5
|
-
CreatePaymentRequest,
|
|
6
|
-
IStartCheckoutIdRequest,
|
|
7
|
-
IStartCheckoutRequest,
|
|
8
|
-
} from "../types";
|
|
9
|
-
|
|
10
|
-
export async function createOrder(
|
|
11
|
-
baseUrl: string,
|
|
12
|
-
apiKey: string,
|
|
13
|
-
orderItems: CreateOrderRequest,
|
|
14
|
-
) {
|
|
15
|
-
const url = `${baseUrl}/api/v1/orders/`;
|
|
16
|
-
const data = orderItems;
|
|
17
|
-
const response = await fetch(url, {
|
|
18
|
-
method: "POST",
|
|
19
|
-
headers: {
|
|
20
|
-
"Content-Type": "application/json",
|
|
21
|
-
Authorization: `Token ${apiKey}`,
|
|
22
|
-
},
|
|
23
|
-
body: JSON.stringify(data),
|
|
24
|
-
});
|
|
25
|
-
if (response.status === 201) {
|
|
26
|
-
return await response.json();
|
|
27
|
-
} else {
|
|
28
|
-
throw new Error(`Error: ${response.statusText}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export async function createPayment(
|
|
33
|
-
baseUrl: string,
|
|
34
|
-
apiKey: string,
|
|
35
|
-
paymentItems: CreatePaymentRequest,
|
|
36
|
-
) {
|
|
37
|
-
const url = `${baseUrl}/api/v1/business/${paymentItems.business_pk}/payments/`;
|
|
38
|
-
const data = paymentItems;
|
|
39
|
-
const response = await fetch(url, {
|
|
40
|
-
method: "POST",
|
|
41
|
-
headers: {
|
|
42
|
-
"Content-Type": "application/json",
|
|
43
|
-
Authorization: `Token ${apiKey}`,
|
|
44
|
-
},
|
|
45
|
-
body: JSON.stringify(data),
|
|
46
|
-
});
|
|
47
|
-
if (response.status >= 200 && response.status <= 299) {
|
|
48
|
-
return await response.json();
|
|
49
|
-
} else {
|
|
50
|
-
throw new Error(`Error: ${response.statusText}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export async function startCheckoutRouter(
|
|
55
|
-
baseUrl: string,
|
|
56
|
-
apiKey: string,
|
|
57
|
-
routerItems: IStartCheckoutRequest | IStartCheckoutIdRequest,
|
|
58
|
-
) {
|
|
59
|
-
try {
|
|
60
|
-
const url = `${baseUrl}/api/v1/checkout-router/`;
|
|
61
|
-
const data = routerItems;
|
|
62
|
-
const response = await fetch(url, {
|
|
63
|
-
method: "POST",
|
|
64
|
-
headers: {
|
|
65
|
-
"Content-Type": "application/json",
|
|
66
|
-
Authorization: `Token ${apiKey}`,
|
|
67
|
-
},
|
|
68
|
-
body: JSON.stringify({
|
|
69
|
-
...data,
|
|
70
|
-
...(typeof MP_DEVICE_SESSION_ID !== "undefined"
|
|
71
|
-
? { mp_device_session_id: MP_DEVICE_SESSION_ID }
|
|
72
|
-
: {}),
|
|
73
|
-
}),
|
|
74
|
-
});
|
|
75
|
-
if (response.status >= 200 && response.status <= 299) {
|
|
76
|
-
return await response.json();
|
|
77
|
-
} else {
|
|
78
|
-
const errorResponse = await response.json();
|
|
79
|
-
const error = new Error("Failed to start checkout router");
|
|
80
|
-
// @ts-ignore
|
|
81
|
-
error.details = errorResponse;
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
} catch (error) {
|
|
85
|
-
throw error;
|
|
86
|
-
}
|
|
87
|
-
}
|
package/src/data/customerApi.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { CustomerRegisterResponse } from "../types";
|
|
2
|
-
|
|
3
|
-
export async function registerOrFetchCustomer(
|
|
4
|
-
baseUrl: string,
|
|
5
|
-
apiKey: string,
|
|
6
|
-
customer: Record<string, any>,
|
|
7
|
-
signal: AbortSignal | null = null,
|
|
8
|
-
): Promise<CustomerRegisterResponse> {
|
|
9
|
-
const url = `${baseUrl}/api/v1/customer/`;
|
|
10
|
-
const data = {
|
|
11
|
-
email: customer.email,
|
|
12
|
-
first_name: customer?.firstName,
|
|
13
|
-
last_name: customer?.lastName,
|
|
14
|
-
phone: customer?.phone,
|
|
15
|
-
};
|
|
16
|
-
const response = await fetch(url, {
|
|
17
|
-
method: "POST",
|
|
18
|
-
headers: {
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
Authorization: `Token ${apiKey}`,
|
|
21
|
-
},
|
|
22
|
-
signal: signal,
|
|
23
|
-
body: JSON.stringify(data),
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (response.status === 201) {
|
|
27
|
-
return await response.json();
|
|
28
|
-
} else {
|
|
29
|
-
throw new Error(`Error: ${response.statusText}`);
|
|
30
|
-
}
|
|
31
|
-
}
|
package/src/data/openPayApi.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export async function getOpenpayDeviceSessionID(
|
|
2
|
-
merchant_id: string,
|
|
3
|
-
public_key: string,
|
|
4
|
-
isSandbox: boolean = true,
|
|
5
|
-
signal: AbortSignal | null = null,
|
|
6
|
-
): Promise<string> {
|
|
7
|
-
let openpay = await window.OpenPay;
|
|
8
|
-
openpay.setId(merchant_id);
|
|
9
|
-
openpay.setApiKey(public_key);
|
|
10
|
-
openpay.setSandboxMode(isSandbox);
|
|
11
|
-
return await openpay.deviceData.setup({ signal });
|
|
12
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildErrorResponse,
|
|
3
|
-
buildErrorResponseFromCatch,
|
|
4
|
-
} from "../helpers/utils";
|
|
5
|
-
import { IPaymentMethodResponse } from "../types";
|
|
6
|
-
|
|
7
|
-
export async function fetchCustomerAPMs(
|
|
8
|
-
baseUrl: string,
|
|
9
|
-
apiKey: string,
|
|
10
|
-
params = {
|
|
11
|
-
status: "active",
|
|
12
|
-
pagesize: "10000",
|
|
13
|
-
},
|
|
14
|
-
signal = null,
|
|
15
|
-
): Promise<IPaymentMethodResponse> {
|
|
16
|
-
try {
|
|
17
|
-
const queryString = new URLSearchParams(params).toString();
|
|
18
|
-
|
|
19
|
-
const response = await fetch(
|
|
20
|
-
`${baseUrl}/api/v1/payment_methods?${queryString}`,
|
|
21
|
-
{
|
|
22
|
-
method: "GET",
|
|
23
|
-
headers: {
|
|
24
|
-
Authorization: `Token ${apiKey}`,
|
|
25
|
-
"Content-Type": "application/json",
|
|
26
|
-
},
|
|
27
|
-
signal,
|
|
28
|
-
},
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
if (response.ok) return await response.json();
|
|
32
|
-
const res_json = await response.json();
|
|
33
|
-
throw await buildErrorResponse(response, res_json);
|
|
34
|
-
} catch (error) {
|
|
35
|
-
throw buildErrorResponseFromCatch(error);
|
|
36
|
-
}
|
|
37
|
-
}
|
package/src/data/skyflowApi.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export async function getVaultToken(
|
|
2
|
-
baseUrl: string,
|
|
3
|
-
apiKey: string,
|
|
4
|
-
signal = null,
|
|
5
|
-
) {
|
|
6
|
-
const response = await fetch(`${baseUrl}/api/v1/vault-token/`, {
|
|
7
|
-
method: "GET",
|
|
8
|
-
headers: {
|
|
9
|
-
Authorization: `Token ${apiKey}`,
|
|
10
|
-
},
|
|
11
|
-
signal: signal,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
if (response.ok) {
|
|
15
|
-
const responseBody = await response.json();
|
|
16
|
-
return responseBody.token;
|
|
17
|
-
} else {
|
|
18
|
-
throw new Error("Failed to retrieve bearer token");
|
|
19
|
-
}
|
|
20
|
-
}
|