tonder-web-sdk 1.12.0-beta.1 → 1.12.0-beta.11
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/workspace.xml +10 -22
- package/README.md +558 -84
- package/package.json +1 -1
- package/src/classes/BaseInlineCheckout.js +35 -21
- package/src/classes/LiteInlineCheckout.js +147 -46
- package/src/classes/inlineCheckout.js +70 -47
- package/src/data/apmApi.js +1 -1
- package/src/data/cardApi.js +22 -12
- package/src/data/checkoutApi.js +17 -6
- package/src/data/customerApi.js +1 -1
- package/src/helpers/template.js +5 -4
- package/src/helpers/utils.js +18 -245
- package/src/index-dev.js +3 -2
- package/src/index.js +2 -0
- package/src/shared/catalog/paymentMethodsCatalog.js +247 -0
- package/src/shared/constants/messages.js +10 -0
- package/types/card.ts +33 -0
- package/types/checkout.ts +118 -0
- package/types/common.ts +26 -0
- package/types/customer.ts +11 -0
- package/types/index.d.ts +8 -76
- package/types/inlineCheckout.d.ts +65 -0
- package/types/liteInlineCheckout.d.ts +96 -0
- package/types/paymentMethod.ts +24 -0
- package/types/transaction.ts +101 -0
- package/v1/bundle.min.js +3 -3
package/README.md
CHANGED
|
@@ -2,18 +2,37 @@
|
|
|
2
2
|
|
|
3
3
|
Tonder SDK helps to integrate the services Tonder offers in your own website
|
|
4
4
|
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Installation](#installation)
|
|
8
|
+
2. [Usage](#usage)
|
|
9
|
+
- [InlineCheckout](#inlinecheckout)
|
|
10
|
+
- [LiteCheckout](#litecheckout)
|
|
11
|
+
3. [Configuration Options](#configuration-options)
|
|
12
|
+
4. [Styling InlineCheckout](#styling-inlinecheckout)
|
|
13
|
+
5. [Payment Data Structure](#payment-data-structure)
|
|
14
|
+
6. [API Reference](#api-reference)
|
|
15
|
+
7. [Examples](#examples)
|
|
16
|
+
8. [License](#license)
|
|
17
|
+
|
|
5
18
|
## Installation
|
|
6
19
|
|
|
7
|
-
You can install using NPM
|
|
20
|
+
You can install the Tonder SDK using NPM or by including it via a script tag.
|
|
21
|
+
|
|
22
|
+
### NPM Installation
|
|
23
|
+
|
|
8
24
|
```bash
|
|
9
25
|
npm i tonder-web-sdk
|
|
10
26
|
```
|
|
11
27
|
|
|
12
|
-
|
|
28
|
+
### Script Tag Installation
|
|
29
|
+
|
|
13
30
|
```html
|
|
14
31
|
<script src="https://zplit-prod.s3.amazonaws.com/v1/bundle.min.js"></script>
|
|
15
32
|
```
|
|
16
33
|
|
|
34
|
+
### Dependencies
|
|
35
|
+
|
|
17
36
|
Add dependencies to the root of the app (index.html)
|
|
18
37
|
```html
|
|
19
38
|
<script src="https://js.skyflow.com/v1/index.js"></script>
|
|
@@ -22,20 +41,125 @@ Add dependencies to the root of the app (index.html)
|
|
|
22
41
|
```
|
|
23
42
|
|
|
24
43
|
## Usage
|
|
25
|
-
|
|
44
|
+
|
|
45
|
+
### InlineCheckout
|
|
46
|
+
|
|
47
|
+
InlineCheckout provides a pre-built, customizable checkout interface.
|
|
48
|
+
|
|
49
|
+
#### HTML Setup
|
|
50
|
+
|
|
51
|
+
You need to have an element where the inline checkout will be mounted. This should be a DIV element with the ID "tonder-checkout":
|
|
52
|
+
|
|
26
53
|
```html
|
|
27
54
|
<div>
|
|
28
55
|
<h1>Checkout</h1>
|
|
29
|
-
|
|
30
|
-
<div id="tonder-checkout">
|
|
31
|
-
</div>
|
|
56
|
+
<div id="tonder-checkout"></div>
|
|
32
57
|
</div>
|
|
33
58
|
```
|
|
34
|
-
|
|
59
|
+
|
|
60
|
+
#### JavaScript Implementation
|
|
61
|
+
|
|
35
62
|
```javascript
|
|
36
63
|
import { InlineCheckout } from "tonder-web-sdk" // Not required if using script tag
|
|
37
64
|
```
|
|
38
65
|
|
|
66
|
+
```javascript
|
|
67
|
+
// if using script tag, it should be initialized like this
|
|
68
|
+
// new TonderSdk.InlineCheckout
|
|
69
|
+
const inlineCheckout = new InlineCheckout({
|
|
70
|
+
apiKey: "your-api-key",
|
|
71
|
+
returnUrl: "https://your-website.com/checkout",
|
|
72
|
+
styles: customStyles // Optional, see Styling section
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// The configureCheckout function allows you to set initial information,
|
|
76
|
+
// such as the customer's email, which is used to retrieve a list of saved cards.
|
|
77
|
+
inlineCheckout.configureCheckout({customer: {email: "example@email.com"}});
|
|
78
|
+
|
|
79
|
+
// Inject the checkout into the DOM
|
|
80
|
+
inlineCheckout.injectCheckout();
|
|
81
|
+
|
|
82
|
+
// To verify a 3ds transaction you can use the following method
|
|
83
|
+
// It should be called after the injectCheckout method
|
|
84
|
+
// The response status will be one of the following
|
|
85
|
+
// ['Declined', 'Cancelled', 'Failed', 'Success', 'Pending', 'Authorized']
|
|
86
|
+
|
|
87
|
+
inlineCheckout.verify3dsTransaction().then(response => {
|
|
88
|
+
console.log('Verify 3ds response', response)
|
|
89
|
+
})
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
// Process a payment
|
|
94
|
+
const response = await inlineCheckout.payment(checkoutData);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### LiteCheckout
|
|
98
|
+
|
|
99
|
+
LiteCheckout allows you to build a custom checkout interface using Tonder's core functionality.
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
import { LiteCheckout } from "tonder-web-sdk"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const liteCheckout = new LiteCheckout({
|
|
107
|
+
apiKey: "your-api-key", // Your api key getted from Tonder Dashboard
|
|
108
|
+
returnUrl: "http://your-website.com/checkout"
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// The configureCheckout function allows you to set initial information,
|
|
112
|
+
// such as the customer's email, which is used to retrieve a list of saved cards.
|
|
113
|
+
inlineCheckout.configureCheckout({customer: {email: "example@email.com"}});
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
// Initialize the checkout
|
|
117
|
+
await liteCheckout.injectCheckout();
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
// Retrieve customer's saved cards
|
|
122
|
+
const cards = await liteCheckout.getCustomerCards();
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
// Save a new card
|
|
127
|
+
const newCard = await liteCheckout.saveCustomerCard(cardData);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
// Remove a saved card
|
|
132
|
+
await liteCheckout.removeCustomerCard(cardId);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
// Get available payment methods
|
|
137
|
+
const paymentMethods = await liteCheckout.getCustomerPaymentMethods();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
// Process a payment
|
|
142
|
+
const paymentResponse = await liteCheckout.payment(paymentData);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
// Verify a 3DS transaction
|
|
147
|
+
const verificationResult = await liteCheckout.verify3dsTransaction();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Configuration Options
|
|
151
|
+
Both InlineCheckout and LiteCheckout accept the following configuration options:
|
|
152
|
+
|
|
153
|
+
| Property | Type | Description |
|
|
154
|
+
|:--------:|:----:|:-----------:|
|
|
155
|
+
| mode | string | Environment mode. Options: 'stage', 'production', 'sandbox'. Default: 'stage' |
|
|
156
|
+
| apiKey | string | Your API key from the Tonder Dashboard |
|
|
157
|
+
| returnUrl | string | URL where the checkout form is mounted (used for 3DS) |
|
|
158
|
+
| styles | object | (InlineCheckout only) Custom styles for the checkout interface |
|
|
159
|
+
|
|
160
|
+
## Styling InlineCheckout
|
|
161
|
+
|
|
162
|
+
You can customize the appearance of InlineCheckout by passing a `styles` object:
|
|
39
163
|
|
|
40
164
|
```javascript
|
|
41
165
|
const customStyles = {
|
|
@@ -86,132 +210,482 @@ const customStyles = {
|
|
|
86
210
|
},
|
|
87
211
|
},
|
|
88
212
|
labels: {
|
|
89
|
-
nameLabel: '
|
|
90
|
-
cardLabel: '
|
|
213
|
+
nameLabel: 'Card Holder Name',
|
|
214
|
+
cardLabel: 'Card Number',
|
|
91
215
|
cvvLabel: 'CVC/CVV',
|
|
92
|
-
expiryDateLabel: '
|
|
216
|
+
expiryDateLabel: 'Expiration Date',
|
|
93
217
|
},
|
|
94
218
|
placeholders: {
|
|
95
|
-
namePlaceholder: '
|
|
219
|
+
namePlaceholder: 'Name as it appears on the card',
|
|
96
220
|
cardPlaceholder: '1234 1234 1234 1234',
|
|
97
|
-
cvvPlaceholder: '3-4
|
|
221
|
+
cvvPlaceholder: '3-4 digits',
|
|
98
222
|
expiryMonthPlaceholder: 'MM',
|
|
99
|
-
expiryYearPlaceholder: '
|
|
223
|
+
expiryYearPlaceholder: 'YY'
|
|
100
224
|
}
|
|
101
225
|
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Payment Data Structure
|
|
229
|
+
|
|
230
|
+
When calling the `payment` method, use the following data structure:
|
|
231
|
+
|
|
232
|
+
### Field Descriptions
|
|
233
|
+
|
|
234
|
+
- **customer**: Object containing the customer's personal information to be registered in the transaction.
|
|
235
|
+
|
|
236
|
+
- **cart**: Object containing the total amount and an array of items to be registered in the Tonder order.
|
|
237
|
+
- **total**: The total amount of the transaction.
|
|
238
|
+
- **items**: An array of objects, each representing a product or service in the order.
|
|
239
|
+
- name: name of the product
|
|
240
|
+
- price_unit: valid float string with the price of the product
|
|
241
|
+
- quantity: valid integer string with the quantity of this product
|
|
242
|
+
|
|
243
|
+
- **currency**: String representing the currency code for the transaction (e.g., "MXN" for Mexican Peso).
|
|
244
|
+
|
|
245
|
+
- **metadata**: Object for including any additional information about the transaction. This can be used for internal references or tracking.
|
|
246
|
+
|
|
247
|
+
- **card**: (for LiteCheckout) Object containing card information. This is used differently depending on whether it's a new card or a saved card:
|
|
248
|
+
- For a new card: Include `card_number`, `cvv`, `expiration_month`, `expiration_year`, and `cardholder_name`.
|
|
249
|
+
- For a saved card: Include only the `skyflow_id` of the saved card.
|
|
250
|
+
- This is only used when not paying with a payment_method.
|
|
102
251
|
|
|
103
|
-
|
|
252
|
+
- **payment_method**: (for LiteCheckout) String indicating the alternative payment method to be used (e.g., "Spei"). This is only used when not paying with a card.
|
|
253
|
+
|
|
254
|
+
Note: The exact fields required may vary depending on whether you're using InlineCheckout or LiteCheckout, and the specific payment method being used.
|
|
255
|
+
|
|
256
|
+
```javascript
|
|
257
|
+
const paymentData = {
|
|
104
258
|
customer: {
|
|
105
|
-
firstName: "
|
|
106
|
-
lastName: "
|
|
107
|
-
country: "
|
|
108
|
-
address: "
|
|
109
|
-
city: "
|
|
110
|
-
state: "
|
|
111
|
-
postCode: "
|
|
112
|
-
email: "
|
|
113
|
-
phone: "
|
|
259
|
+
firstName: "John",
|
|
260
|
+
lastName: "Doe",
|
|
261
|
+
country: "USA",
|
|
262
|
+
address: "123 Main St",
|
|
263
|
+
city: "Anytown",
|
|
264
|
+
state: "CA",
|
|
265
|
+
postCode: "12345",
|
|
266
|
+
email: "john.doe@example.com",
|
|
267
|
+
phone: "1234567890",
|
|
114
268
|
},
|
|
115
|
-
currency: 'mxn',
|
|
116
269
|
cart: {
|
|
117
|
-
total:
|
|
270
|
+
total: "100.00",
|
|
118
271
|
items: [
|
|
119
272
|
{
|
|
120
|
-
description: "
|
|
273
|
+
description: "Product description",
|
|
121
274
|
quantity: 1,
|
|
122
|
-
price_unit:
|
|
123
|
-
discount: 0,
|
|
124
|
-
taxes: 0,
|
|
125
|
-
product_reference:
|
|
126
|
-
name: "
|
|
127
|
-
amount_total:
|
|
275
|
+
price_unit: "100.00",
|
|
276
|
+
discount: "0.00",
|
|
277
|
+
taxes: "0.00",
|
|
278
|
+
product_reference: "PROD123",
|
|
279
|
+
name: "Product Name",
|
|
280
|
+
amount_total: "100.00",
|
|
128
281
|
},
|
|
129
282
|
]
|
|
130
283
|
},
|
|
284
|
+
currency: "MXN",
|
|
131
285
|
metadata: {
|
|
132
|
-
order_id:
|
|
286
|
+
order_id: "ORDER123"
|
|
133
287
|
},
|
|
288
|
+
// For Lite checkout
|
|
134
289
|
card: {
|
|
135
|
-
|
|
136
|
-
|
|
290
|
+
// For a new card:
|
|
291
|
+
card_number: "4111111111111111",
|
|
292
|
+
cvv: "123",
|
|
293
|
+
expiration_month: "12",
|
|
294
|
+
expiration_year: "25",
|
|
295
|
+
cardholder_name: "John Doe",
|
|
296
|
+
// Or for a saved card:
|
|
297
|
+
skyflow_id: "saved-card-id",
|
|
298
|
+
},
|
|
299
|
+
// For Lite checkout
|
|
300
|
+
payment_method: "Spei",
|
|
137
301
|
};
|
|
302
|
+
```
|
|
138
303
|
|
|
139
|
-
|
|
140
|
-
|
|
304
|
+
## API Reference
|
|
305
|
+
|
|
306
|
+
### InlineCheckout Methods
|
|
307
|
+
|
|
308
|
+
- `configureCheckout(data)`: Set initial checkout data
|
|
309
|
+
- `injectCheckout()`: Inject the checkout interface into the DOM
|
|
310
|
+
- `payment(data)`: Process a payment
|
|
311
|
+
- `verify3dsTransaction()`: Verify a 3DS transaction
|
|
312
|
+
|
|
313
|
+
### LiteCheckout Methods
|
|
314
|
+
|
|
315
|
+
- `configureCheckout(data)`: Set initial checkout data
|
|
316
|
+
- `injectCheckout()`: Initialize the checkout
|
|
317
|
+
- `getCustomerCards()`: Retrieve saved cards
|
|
318
|
+
- `saveCustomerCard(cardData)`: Save a new card
|
|
319
|
+
- `removeCustomerCard(cardId)`: Remove a saved card
|
|
320
|
+
- `getCustomerPaymentMethods()`: Get available payment methods
|
|
321
|
+
- `payment(data)`: Process a payment
|
|
322
|
+
- `verify3dsTransaction()`: Verify a 3DS transaction
|
|
323
|
+
|
|
324
|
+
## Examples
|
|
325
|
+
Here are examples of how to implement Tonder SDK in various JavaScript frameworks:
|
|
326
|
+
|
|
327
|
+
### Vanilla JavaScript
|
|
328
|
+
|
|
329
|
+
#### HTML Setup
|
|
330
|
+
|
|
331
|
+
```html
|
|
332
|
+
<!DOCTYPE html>
|
|
333
|
+
<html lang="en">
|
|
334
|
+
<head>
|
|
335
|
+
<meta charset="UTF-8">
|
|
336
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
337
|
+
<title>Tonder Checkout Example</title>
|
|
338
|
+
<script src="https://js.skyflow.com/v1/index.js"></script>
|
|
339
|
+
<script src="https://openpay.s3.amazonaws.com/openpay.v1.min.js"></script>
|
|
340
|
+
<script src="https://openpay.s3.amazonaws.com/openpay-data.v1.min.js"></script>
|
|
341
|
+
<!-- Only if not use npm package -->
|
|
342
|
+
<script src="https://zplit-prod.s3.amazonaws.com/v1/bundle.min.js"></script>
|
|
343
|
+
</head>
|
|
344
|
+
<body>
|
|
345
|
+
<div id="tonder-checkout"></div>
|
|
346
|
+
<button id="pay-button">Pay Now</button>
|
|
347
|
+
|
|
348
|
+
<script src="your-script.js"></script>
|
|
349
|
+
</body>
|
|
350
|
+
</html>
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
#### InlineCheckout Example (your-script.js)
|
|
354
|
+
|
|
355
|
+
```javascript
|
|
356
|
+
import { InlineCheckout } from "tonder-web-sdk"
|
|
357
|
+
|
|
358
|
+
const apiKey = "your-api-key";
|
|
359
|
+
const returnUrl = "http://your-website.com/checkout";
|
|
141
360
|
|
|
142
|
-
// if using script tag, it should be initialized like this
|
|
143
|
-
// new TonderSdk.InlineCheckout
|
|
144
361
|
const inlineCheckout = new InlineCheckout({
|
|
362
|
+
mode: 'development',
|
|
145
363
|
apiKey,
|
|
146
364
|
returnUrl,
|
|
147
|
-
styles: customStyles
|
|
365
|
+
styles: customStyles // Define your custom styles here
|
|
148
366
|
});
|
|
149
367
|
|
|
150
|
-
// The configureCheckout function allows you to set initial information,
|
|
151
|
-
// such as the customer's email, which is used to retrieve a list of saved cards.
|
|
152
368
|
inlineCheckout.configureCheckout({customer: {email: "example@email.com"}});
|
|
153
|
-
|
|
154
|
-
|
|
155
369
|
inlineCheckout.injectCheckout();
|
|
156
370
|
|
|
157
|
-
// To verify a 3ds transaction you can use the following method
|
|
158
|
-
// It should be called after the injectCheckout method
|
|
159
|
-
// The response status will be one of the following
|
|
160
|
-
// ['Declined', 'Cancelled', 'Failed', 'Success', 'Pending', 'Authorized']
|
|
161
|
-
|
|
162
371
|
inlineCheckout.verify3dsTransaction().then(response => {
|
|
163
|
-
console.log('Verify 3ds response', response)
|
|
164
|
-
})
|
|
372
|
+
console.log('Verify 3ds response', response);
|
|
373
|
+
});
|
|
165
374
|
|
|
166
|
-
|
|
375
|
+
document.getElementById('pay-button').addEventListener('click', async function () {
|
|
376
|
+
try {
|
|
377
|
+
const response = await inlineCheckout.payment(checkoutData);
|
|
378
|
+
console.log("Payment response:", response);
|
|
379
|
+
alert('Payment successful');
|
|
380
|
+
} catch (error) {
|
|
381
|
+
console.error("Payment error:", error.details);
|
|
382
|
+
alert('Payment failed');
|
|
383
|
+
}
|
|
384
|
+
});
|
|
167
385
|
```
|
|
168
386
|
|
|
169
|
-
|
|
387
|
+
#### LiteCheckout Example (your-script.js)
|
|
388
|
+
|
|
170
389
|
```javascript
|
|
390
|
+
import { LiteInlineCheckout } from "tonder-web-sdk"
|
|
391
|
+
|
|
392
|
+
const apiKey = "your-api-key";
|
|
393
|
+
const returnUrl = "http://your-website.com/checkout";
|
|
394
|
+
|
|
395
|
+
const liteCheckout = new LiteInlineCheckout({
|
|
396
|
+
mode: 'development',
|
|
397
|
+
apiKey,
|
|
398
|
+
returnUrl
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
liteCheckout.configureCheckout({customer: {email: "example@email.com"}});
|
|
402
|
+
liteCheckout.injectCheckout();
|
|
403
|
+
|
|
404
|
+
liteCheckout.verify3dsTransaction().then(response => {
|
|
405
|
+
console.log('Verify 3ds response', response);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
document.getElementById('lite-payment-form').addEventListener('submit', async function(event) {
|
|
409
|
+
event.preventDefault();
|
|
410
|
+
const cardData = {
|
|
411
|
+
card_number: document.getElementById('card-number').value,
|
|
412
|
+
cardholder_name: document.getElementById('card-name').value,
|
|
413
|
+
expiration_month: document.getElementById('month').value,
|
|
414
|
+
expiration_year: document.getElementById('year').value,
|
|
415
|
+
cvv: document.getElementById('cvv').value
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
try {
|
|
419
|
+
const paymentData = { ...checkoutData, card: cardData };
|
|
420
|
+
const response = await liteCheckout.payment(paymentData);
|
|
421
|
+
console.log("Payment response:", response);
|
|
422
|
+
alert('Payment successful');
|
|
423
|
+
} catch (error) {
|
|
424
|
+
console.error("Payment error:", error);
|
|
425
|
+
alert('Payment failed');
|
|
426
|
+
}
|
|
427
|
+
});
|
|
171
428
|
```
|
|
172
429
|
|
|
173
|
-
|
|
174
|
-
| Property | Type | Description |
|
|
175
|
-
|:---------------:|:-------------:|:---------------------------------------------------:|
|
|
176
|
-
| mode | string | 'stage' 'production' 'sandbox', default 'stage' |
|
|
177
|
-
| apiKey | string | You can take this from you Tonder Dashboard |
|
|
178
|
-
| backgroundColor | string | Hex color #000000 |
|
|
179
|
-
| returnUrl | string | url where the checkout form is mounted (3ds) |
|
|
180
|
-
| backgroundColor | string | |
|
|
430
|
+
### React or Nextjs
|
|
181
431
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
432
|
+
For React or Nextjs applications, we recommend using a context provider to manage the Tonder instance across your application.
|
|
433
|
+
|
|
434
|
+
First, create a TonderProvider:
|
|
435
|
+
|
|
436
|
+
```jsx
|
|
437
|
+
// TonderProvider.jsx
|
|
438
|
+
'use client' // only for Nextjs
|
|
439
|
+
import React, { createContext, useContext, useState, useEffect } from 'react';
|
|
440
|
+
import { InlineCheckout } from "tonder-web-sdk";
|
|
441
|
+
|
|
442
|
+
const TonderContext = createContext();
|
|
443
|
+
|
|
444
|
+
export const useTonder = () => useContext(TonderContext);
|
|
445
|
+
|
|
446
|
+
export const TonderProvider = ({ children }) => {
|
|
447
|
+
const [tonderInstance, setTonderInstance] = useState(null);
|
|
448
|
+
|
|
449
|
+
useEffect(() => {
|
|
450
|
+
const init = async () => {
|
|
451
|
+
try {
|
|
452
|
+
const inlineCheckout = new InlineCheckout({
|
|
453
|
+
mode: 'development',
|
|
454
|
+
apiKey: 'your-api-key',
|
|
455
|
+
returnUrl: 'http://your-website.com/checkout',
|
|
456
|
+
});
|
|
457
|
+
setTonderInstance(inlineCheckout);
|
|
458
|
+
} catch (error) {
|
|
459
|
+
console.error("Error initializing Tonder:", error);
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
init();
|
|
464
|
+
}, []);
|
|
465
|
+
|
|
466
|
+
return (
|
|
467
|
+
<TonderContext.Provider value={tonderInstance}>
|
|
468
|
+
{children}
|
|
469
|
+
</TonderContext.Provider>
|
|
470
|
+
);
|
|
471
|
+
};
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
Then, create a TonderCheckout component:
|
|
475
|
+
|
|
476
|
+
```jsx
|
|
477
|
+
// TonderCheckout.jsx
|
|
478
|
+
'use client' // only for Nextjs
|
|
479
|
+
import React, { useEffect, useState } from 'react';
|
|
480
|
+
import { useTonder } from './TonderProvider';
|
|
481
|
+
|
|
482
|
+
const TonderCheckout = () => {
|
|
483
|
+
const tonder = useTonder();
|
|
484
|
+
const [loading, setLoading] = useState(false);
|
|
485
|
+
|
|
486
|
+
useEffect(() => {
|
|
487
|
+
if (!tonder) return;
|
|
488
|
+
|
|
489
|
+
tonder.configureCheckout({customer: {email: 'example@email.com'}});
|
|
490
|
+
tonder.injectCheckout();
|
|
491
|
+
|
|
492
|
+
tonder.verify3dsTransaction().then(response => {
|
|
493
|
+
console.log('Verify 3ds response', response);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
return () => {
|
|
497
|
+
if (tonder.removeCheckout) {
|
|
498
|
+
tonder.removeCheckout();
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
}, [tonder]);
|
|
502
|
+
|
|
503
|
+
const handlePayment = async () => {
|
|
504
|
+
if (!tonder) return;
|
|
505
|
+
setLoading(true);
|
|
506
|
+
try {
|
|
507
|
+
const response = await tonder.payment(paymentData);
|
|
508
|
+
console.log('Payment successful:', response);
|
|
509
|
+
alert('Payment successful');
|
|
510
|
+
} catch (error) {
|
|
511
|
+
console.error('Payment failed:', error);
|
|
512
|
+
alert('Payment failed');
|
|
513
|
+
} finally {
|
|
514
|
+
setLoading(false);
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
return (
|
|
519
|
+
<div>
|
|
520
|
+
<div id="tonder-checkout"></div>
|
|
521
|
+
<button onClick={handlePayment} disabled={loading}>
|
|
522
|
+
{loading ? 'Processing...' : 'Pay Now'}
|
|
523
|
+
</button>
|
|
524
|
+
</div>
|
|
525
|
+
);
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
export default TonderCheckout;
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
Finally, use it in your checkout component:
|
|
532
|
+
|
|
533
|
+
```jsx
|
|
534
|
+
'use client';
|
|
535
|
+
import { useEffect, useState } from 'react';
|
|
536
|
+
import { useTonder, TonderProvider } from './TonderProvider';
|
|
537
|
+
import Script from 'next/script';
|
|
538
|
+
|
|
539
|
+
export default function TonderCheckout() {
|
|
540
|
+
return (
|
|
541
|
+
<div id="checkout">
|
|
542
|
+
{/*Provider*/}
|
|
543
|
+
<TonderProvider>
|
|
544
|
+
<CheckoutContent />
|
|
545
|
+
</TonderProvider>
|
|
546
|
+
</div>
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function CheckoutContent() {
|
|
551
|
+
const tonder = useTonder();
|
|
552
|
+
const [loading, setLoading] = useState(false);
|
|
553
|
+
|
|
554
|
+
useEffect(() => {
|
|
555
|
+
if (!tonder) return;
|
|
556
|
+
tonder.configureCheckout({customer: {email: 'example@email.com'}});
|
|
557
|
+
tonder.injectCheckout();
|
|
558
|
+
tonder.verify3dsTransaction().then(response => {
|
|
559
|
+
console.log('Verify 3ds response', response);
|
|
560
|
+
});
|
|
561
|
+
return () => {
|
|
562
|
+
if (tonder.removeCheckout) {
|
|
563
|
+
tonder.removeCheckout();
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}, [tonder]);
|
|
567
|
+
|
|
568
|
+
const handlePayment = async () => {
|
|
569
|
+
if (!tonder) return;
|
|
570
|
+
setLoading(true);
|
|
571
|
+
try {
|
|
572
|
+
const response = await tonder.payment(paymentData);
|
|
573
|
+
console.log(response);
|
|
574
|
+
alert('Payment successful');
|
|
575
|
+
} catch (error) {
|
|
576
|
+
console.error(error);
|
|
577
|
+
alert('Payment failed');
|
|
578
|
+
} finally {
|
|
579
|
+
setLoading(false);
|
|
191
580
|
}
|
|
192
|
-
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
return (
|
|
584
|
+
<div>
|
|
585
|
+
<div id="tonder-checkout"></div>
|
|
586
|
+
<button onClick={handlePayment} disabled={loading}>
|
|
587
|
+
{loading ? 'Processing...' : 'Pay'}
|
|
588
|
+
</button>
|
|
589
|
+
</div>
|
|
590
|
+
);
|
|
591
|
+
}
|
|
193
592
|
```
|
|
194
|
-
### shippingCost
|
|
195
|
-
It is a valid float string, that will be the shipping cost.
|
|
196
593
|
|
|
197
|
-
###
|
|
198
|
-
|
|
594
|
+
### Angular
|
|
595
|
+
|
|
596
|
+
For Angular, we recommend using a service to manage the Tonder instance:
|
|
597
|
+
|
|
598
|
+
```typescript
|
|
599
|
+
// tonder.service.ts
|
|
600
|
+
import { Injectable } from '@angular/core';
|
|
601
|
+
import { InlineCheckout } from 'tonder-web-sdk';
|
|
199
602
|
|
|
200
|
-
|
|
201
|
-
|
|
603
|
+
@Injectable({
|
|
604
|
+
providedIn: 'root'
|
|
605
|
+
})
|
|
606
|
+
export class TonderService {
|
|
607
|
+
private inlineCheckout: InlineCheckout;
|
|
608
|
+
|
|
609
|
+
constructor() {
|
|
610
|
+
this.inlineCheckout = new InlineCheckout({
|
|
611
|
+
apiKey: 'your-api-key',
|
|
612
|
+
returnUrl: 'http://your-website.com/checkout',
|
|
613
|
+
mode: 'development'
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
getInlineCheckout(): InlineCheckout {
|
|
618
|
+
return this.inlineCheckout;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// checkout.component.ts
|
|
623
|
+
import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
624
|
+
import { TonderService } from './tonder.service';
|
|
625
|
+
|
|
626
|
+
@Component({
|
|
627
|
+
selector: 'app-tonder-checkout',
|
|
628
|
+
template: `
|
|
629
|
+
<div id="tonder-checkout"></div>
|
|
630
|
+
<button (click)="pay()" [disabled]="loading">
|
|
631
|
+
{{ loading ? 'Processing...' : 'Pay' }}
|
|
632
|
+
</button>
|
|
633
|
+
`
|
|
634
|
+
})
|
|
635
|
+
export class TonderCheckoutComponent implements OnInit, OnDestroy {
|
|
636
|
+
loading = false;
|
|
637
|
+
|
|
638
|
+
constructor(private tonderService: TonderService) {}
|
|
639
|
+
|
|
640
|
+
ngOnInit() {
|
|
641
|
+
const tonder = this.tonderService.getInlineCheckout();
|
|
642
|
+
tonder.configureCheckout({ customer: { email: 'example@email.com' } });
|
|
643
|
+
tonder.injectCheckout();
|
|
644
|
+
tonder.verify3dsTransaction().then(response => {
|
|
645
|
+
console.log('Verify 3ds response', response);
|
|
646
|
+
});
|
|
647
|
+
}
|
|
202
648
|
|
|
203
|
-
|
|
204
|
-
|
|
649
|
+
ngOnDestroy() {
|
|
650
|
+
const tonder = this.tonderService.getInlineCheckout();
|
|
651
|
+
if (tonder.removeCheckout) {
|
|
652
|
+
tonder.removeCheckout();
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
async pay() {
|
|
657
|
+
this.loading = true;
|
|
658
|
+
try {
|
|
659
|
+
const tonder = this.tonderService.getInlineCheckout();
|
|
660
|
+
const response = await tonder.payment(paymentData);
|
|
661
|
+
console.log('Payment successful:', response);
|
|
662
|
+
alert('Payment successful');
|
|
663
|
+
} catch (error) {
|
|
664
|
+
console.error('Payment failed:', error);
|
|
665
|
+
alert('Payment failed');
|
|
666
|
+
} finally {
|
|
667
|
+
this.loading = false;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
```
|
|
205
672
|
|
|
206
|
-
|
|
207
|
-
An array of items to be registered in the Tonder order.
|
|
673
|
+
## Notes
|
|
208
674
|
|
|
209
|
-
###
|
|
210
|
-
|
|
675
|
+
### General
|
|
676
|
+
- Replace `'your-api-key'`, `'http://your-website.com/checkout'`, `customStyles`, and `paymentData` with your actual values.
|
|
677
|
+
- The `paymentData` should be defined according to your specific requirements.
|
|
211
678
|
|
|
679
|
+
### Script Dependencies
|
|
680
|
+
For all implementations, ensure you include the necessary scripts:
|
|
212
681
|
```html
|
|
213
|
-
<
|
|
682
|
+
<script src="https://js.skyflow.com/v1/index.js"></script>
|
|
683
|
+
<script src="https://openpay.s3.amazonaws.com/openpay.v1.min.js"></script>
|
|
684
|
+
<script src="https://openpay.s3.amazonaws.com/openpay-data.v1.min.js"></script>
|
|
685
|
+
<!-- Only if not use npm package -->
|
|
686
|
+
<script src="https://zplit-prod.s3.amazonaws.com/v1/bundle.min.js"></script>
|
|
214
687
|
```
|
|
688
|
+
|
|
215
689
|
## License
|
|
216
690
|
|
|
217
691
|
[MIT](https://choosealicense.com/licenses/mit/)
|