@easyslip/sdk 0.0.0-stage → 1.0.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +595 -2
- package/README.th.md +610 -0
- package/dist/cjs/client.d.ts +59 -0
- package/dist/cjs/client.js +150 -0
- package/dist/cjs/core/call.d.ts +18 -0
- package/dist/cjs/core/call.js +60 -0
- package/dist/cjs/core/error-codes.d.ts +8 -0
- package/dist/cjs/core/error-codes.js +87 -0
- package/dist/cjs/core/errors.d.ts +162 -0
- package/dist/cjs/core/errors.js +210 -0
- package/dist/cjs/core/files.d.ts +20 -0
- package/dist/cjs/core/files.js +131 -0
- package/dist/cjs/core/http.d.ts +65 -0
- package/dist/cjs/core/http.js +242 -0
- package/dist/cjs/core/meta.d.ts +33 -0
- package/dist/cjs/core/meta.js +52 -0
- package/dist/cjs/core/redact.d.ts +43 -0
- package/dist/cjs/core/redact.js +208 -0
- package/dist/cjs/core/runtime.d.ts +16 -0
- package/dist/cjs/core/runtime.js +86 -0
- package/dist/cjs/core/settings.d.ts +19 -0
- package/dist/cjs/core/settings.js +22 -0
- package/dist/cjs/core/sha256.d.ts +6 -0
- package/dist/cjs/core/sha256.js +101 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +55 -0
- package/dist/cjs/models.d.ts +398 -0
- package/dist/cjs/models.js +234 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/resources/bank-accounts.d.ts +63 -0
- package/dist/cjs/resources/bank-accounts.js +126 -0
- package/dist/cjs/resources/banks.d.ts +10 -0
- package/dist/cjs/resources/banks.js +22 -0
- package/dist/cjs/resources/jobs.d.ts +64 -0
- package/dist/cjs/resources/jobs.js +177 -0
- package/dist/cjs/resources/v1.d.ts +88 -0
- package/dist/cjs/resources/v1.js +137 -0
- package/dist/cjs/resources/verify.d.ts +39 -0
- package/dist/cjs/resources/verify.js +105 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/webhooks.d.ts +26 -0
- package/dist/cjs/webhooks.js +92 -0
- package/dist/esm/client.d.ts +59 -0
- package/dist/esm/client.js +146 -0
- package/dist/esm/core/call.d.ts +18 -0
- package/dist/esm/core/call.js +52 -0
- package/dist/esm/core/error-codes.d.ts +8 -0
- package/dist/esm/core/error-codes.js +84 -0
- package/dist/esm/core/errors.d.ts +162 -0
- package/dist/esm/core/errors.js +184 -0
- package/dist/esm/core/files.d.ts +20 -0
- package/dist/esm/core/files.js +126 -0
- package/dist/esm/core/http.d.ts +65 -0
- package/dist/esm/core/http.js +235 -0
- package/dist/esm/core/meta.d.ts +33 -0
- package/dist/esm/core/meta.js +45 -0
- package/dist/esm/core/redact.d.ts +43 -0
- package/dist/esm/core/redact.js +198 -0
- package/dist/esm/core/runtime.d.ts +16 -0
- package/dist/esm/core/runtime.js +47 -0
- package/dist/esm/core/settings.d.ts +19 -0
- package/dist/esm/core/settings.js +19 -0
- package/dist/esm/core/sha256.d.ts +6 -0
- package/dist/esm/core/sha256.js +96 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/models.d.ts +398 -0
- package/dist/esm/models.js +228 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/resources/bank-accounts.d.ts +63 -0
- package/dist/esm/resources/bank-accounts.js +122 -0
- package/dist/esm/resources/banks.d.ts +10 -0
- package/dist/esm/resources/banks.js +18 -0
- package/dist/esm/resources/jobs.d.ts +64 -0
- package/dist/esm/resources/jobs.js +173 -0
- package/dist/esm/resources/v1.d.ts +88 -0
- package/dist/esm/resources/v1.js +133 -0
- package/dist/esm/resources/verify.d.ts +39 -0
- package/dist/esm/resources/verify.js +100 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/webhooks.d.ts +26 -0
- package/dist/esm/webhooks.js +86 -0
- package/package.json +80 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [1.0.1](https://github.com/EASYSLIP-CO-LTD/sdk-js/compare/v1.0.0...v1.0.1) (2026-11-05)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* publish to npm with provenance ([37ce44e](https://github.com/EASYSLIP-CO-LTD/sdk-js/commit/37ce44e88e862243081e3d2ecd4bc204c9ac65f3))
|
|
9
|
+
|
|
10
|
+
## 1.0.0 (2026-10-30)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* first release of the EasySlip JavaScript and TypeScript SDK ([aadc034](https://github.com/EASYSLIP-CO-LTD/sdk-js/commit/aadc03445e2ff0192812f6eb66eb4f4fc90b8540))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 EasySlip Co., Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,596 @@
|
|
|
1
|
-
#
|
|
1
|
+
# EasySlip SDK for JavaScript and TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/EASYSLIP-CO-LTD/sdk-js/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
[EasySlip](https://document.easyslip.com) checks Thai bank transfer slips and TrueMoney Wallet receipts with the bank, so you know a payment is real before you credit an order. With this SDK, your Node.js, Bun or Deno server can verify a slip, confirm that it paid your account the right amount and was not used before, and receive the results of background checks through signed webhooks.
|
|
7
|
+
|
|
8
|
+
[อ่านภาษาไทย](README.th.md)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Verify a bank transfer slip from its image, the text in its QR code, a public image URL or a base64 string.
|
|
13
|
+
- Confirm in the same request that the slip paid one of your bank accounts, for the amount you expect, and was not used before.
|
|
14
|
+
- Verify TrueMoney Wallet receipts with the same checks.
|
|
15
|
+
- Send up to 100 slips at a time for background checking and get each result on your webhook.
|
|
16
|
+
- Manage the bank accounts you receive payments to.
|
|
17
|
+
- Create PromptPay and bill payment QR codes for your customers to pay with.
|
|
18
|
+
- Handle each kind of failure with its own error class. Automatic retries never charge you twice for a slip.
|
|
19
|
+
- Use it from TypeScript with full types, or from JavaScript as an ES module or CommonJS. The package has no dependencies.
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
- Node.js 22, 24 or 26. Bun 1.4, Deno 2.9 and edge runtimes with `fetch`, such as Cloudflare Workers, work too.
|
|
24
|
+
- An EasySlip API key. Sign in to the [EasySlip developer portal](https://developer.easyslip.com) and copy the API key of your application.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
npm install @easyslip/sdk
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`pnpm add @easyslip/sdk`, `yarn add @easyslip/sdk` and `bun add @easyslip/sdk` work as well. On Deno, import from `npm:@easyslip/sdk`.
|
|
33
|
+
|
|
34
|
+
## Quick start
|
|
35
|
+
|
|
36
|
+
This program checks the slip a customer sent for an order of 1,500 baht. It confirms that the slip paid one of your registered bank accounts, that the amount is right and that the slip was not used before, then prints the payment.
|
|
37
|
+
|
|
38
|
+
Save it as `verify.mjs` and put the slip image next to it as `slip.jpg`:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { EasySlip, SlipNotFoundError } from '@easyslip/sdk'
|
|
42
|
+
|
|
43
|
+
const easyslip = new EasySlip() // reads your key from EASYSLIP_API_KEY
|
|
44
|
+
const orderTotal = 1500 // baht
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const result = await easyslip.verify.bankByImage('./slip.jpg', {
|
|
48
|
+
matchAccount: true, // did the slip pay one of your bank accounts?
|
|
49
|
+
matchAmount: orderTotal, // is the amount right?
|
|
50
|
+
checkDuplicate: true, // was this slip used before?
|
|
51
|
+
})
|
|
52
|
+
const slip = result.rawSlip
|
|
53
|
+
|
|
54
|
+
if (result.isDuplicate) {
|
|
55
|
+
console.log('This slip was already used. Do not credit the order again.')
|
|
56
|
+
} else if (!result.isAmountMatched) {
|
|
57
|
+
console.log(`The slip pays ${result.amountInSlip} baht, but the order is ${orderTotal} baht.`)
|
|
58
|
+
} else if (result.matchedAccount === null) {
|
|
59
|
+
console.log('The slip does not pay any of your bank accounts.')
|
|
60
|
+
} else {
|
|
61
|
+
console.log('Payment confirmed')
|
|
62
|
+
console.log(`Amount: ${slip.amount.amount} THB`)
|
|
63
|
+
console.log(`Date: ${slip.date}`)
|
|
64
|
+
console.log(`From: ${slip.sender.account.name.th} (${slip.sender.bank?.short})`)
|
|
65
|
+
console.log(`To: ${slip.receiver.account.name.th} (${slip.receiver.bank?.short})`)
|
|
66
|
+
console.log(`Reference: ${slip.transRef}`)
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error instanceof SlipNotFoundError) {
|
|
70
|
+
console.log('The bank has no record of this transfer. The slip may be fake or edited.')
|
|
71
|
+
} else {
|
|
72
|
+
throw error
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Run it with your API key:
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
EASYSLIP_API_KEY="your-api-key" node verify.mjs
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
For a valid slip it prints:
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
Payment confirmed
|
|
87
|
+
Amount: 1500 THB
|
|
88
|
+
Date: 2024-01-15T14:30:00+07:00
|
|
89
|
+
From: นาย ผู้โอน ทดสอบ (KBANK)
|
|
90
|
+
To: บริษัท ตัวอย่าง จำกัด (SCB)
|
|
91
|
+
Reference: 68370160657749I376388B35
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`matchAccount` compares the receiver with the bank accounts you registered, so add your receiving accounts first, in the developer portal or with [`bankAccounts.create`](#receiving-bank-accounts). Each verification uses one unit of your quota.
|
|
95
|
+
|
|
96
|
+
Next steps:
|
|
97
|
+
|
|
98
|
+
- [Verify slips you receive in another form](#verify-a-bank-slip), such as QR code text or an image URL.
|
|
99
|
+
- [See every field of the result](#the-result-object).
|
|
100
|
+
- [Handle errors](#error-handling) and see what the SDK retries for you.
|
|
101
|
+
- [Receive background results on a webhook](#webhooks).
|
|
102
|
+
|
|
103
|
+
## Usage
|
|
104
|
+
|
|
105
|
+
Each example creates its own client so that you can copy it on its own. In your application, create one client and reuse it.
|
|
106
|
+
|
|
107
|
+
### Verify a bank slip
|
|
108
|
+
|
|
109
|
+
Pick the method that matches what your customer gives you. All four return the same [result](#the-result-object) and take the same [options](#check-amount-receiving-account-and-duplicates).
|
|
110
|
+
|
|
111
|
+
| What you have | Method |
|
|
112
|
+
| --- | --- |
|
|
113
|
+
| An image file, an upload or a stream | `verify.bankByImage(image)` |
|
|
114
|
+
| The text in the slip's QR code, read by a QR scanner | `verify.bankByPayload(payload)` |
|
|
115
|
+
| A public https URL of the slip image | `verify.bankByUrl(url)` |
|
|
116
|
+
| The image as a base64 string, with or without a `data:` prefix | `verify.bankByBase64(base64)` |
|
|
117
|
+
|
|
118
|
+
`bankByImage` accepts a file path, a `Uint8Array` or `Buffer`, a `Blob` or `File` (for example from `request.formData()`), and web or Node.js streams. The image must be a JPEG, PNG, GIF or WebP file of at most 4 MB. A string is always read as a file path, so pass a user's upload as a `File` or as bytes, never as a string.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
122
|
+
|
|
123
|
+
const easyslip = new EasySlip()
|
|
124
|
+
|
|
125
|
+
const result = await easyslip.verify.bankByUrl('https://example.com/slips/1001.jpg')
|
|
126
|
+
console.log(result.rawSlip.transRef, result.amountInSlip)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Check amount, receiving account and duplicates
|
|
130
|
+
|
|
131
|
+
Turn on the checks you need with these options. Each one adds fields to the result, and an option you leave out adds nothing. When a check fails, the API still returns the slip, and your code decides what to do.
|
|
132
|
+
|
|
133
|
+
| Option | What it checks | Result fields | When the check fails |
|
|
134
|
+
| --- | --- | --- | --- |
|
|
135
|
+
| `matchAmount: 1500` | The slip amount equals the amount you expect, in baht. | `amountInOrder`, `isAmountMatched` | `isAmountMatched` is `false`. |
|
|
136
|
+
| `matchAccount: true` | The receiver is one of your registered bank accounts. | `matchedAccount` | `matchedAccount` is `null`. |
|
|
137
|
+
| `checkDuplicate: true` | The slip was verified before. | `isDuplicate` | `isDuplicate` is `true`. |
|
|
138
|
+
| `remark: 'Order 1001'` | Nothing. It saves your note of 1 to 255 characters with the verification. | `remark` | It never fails. |
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
142
|
+
|
|
143
|
+
const easyslip = new EasySlip()
|
|
144
|
+
|
|
145
|
+
const result = await easyslip.verify.bankByImage('./slip.jpg', {
|
|
146
|
+
matchAmount: 1500,
|
|
147
|
+
matchAccount: true,
|
|
148
|
+
checkDuplicate: true,
|
|
149
|
+
remark: 'Order 1001',
|
|
150
|
+
})
|
|
151
|
+
const accepted =
|
|
152
|
+
result.isAmountMatched === true && result.matchedAccount !== null && result.isDuplicate === false
|
|
153
|
+
console.log(accepted ? 'accept' : 'reject', result.remark)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
If the same branch verified the slip before, the API returns the saved result and uses no quota. If another branch verified it, the API checks it with the bank again and uses quota. `isDuplicate` is `true` in both cases.
|
|
157
|
+
|
|
158
|
+
### TrueMoney Wallet receipts
|
|
159
|
+
|
|
160
|
+
TrueMoney Wallet receipts work the same way and take the same options. Their result has a [shorter slip](#truemoney-wallet-receipt).
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
164
|
+
|
|
165
|
+
const easyslip = new EasySlip()
|
|
166
|
+
|
|
167
|
+
const result = await easyslip.verify.truewalletByImage('./wallet-receipt.png', {
|
|
168
|
+
matchAmount: 500,
|
|
169
|
+
checkDuplicate: true,
|
|
170
|
+
})
|
|
171
|
+
console.log(result.rawSlip.transactionId, result.rawSlip.amount, result.rawSlip.receiver.name)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`verify.truewalletByUrl` takes an image URL and `verify.truewalletByBase64` takes base64.
|
|
175
|
+
|
|
176
|
+
### Account info and remaining quota
|
|
177
|
+
|
|
178
|
+
`getInfo` returns your application, branch and quota. `quota.remaining` is `null` when your plan has no limit.
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
182
|
+
|
|
183
|
+
const easyslip = new EasySlip()
|
|
184
|
+
|
|
185
|
+
const info = await easyslip.getInfo()
|
|
186
|
+
const { used, remaining } = info.application.quota
|
|
187
|
+
console.log(`${info.application.name}, branch ${info.branch.name}`)
|
|
188
|
+
console.log(`Used ${used}, remaining ${remaining ?? 'unlimited'}`)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Receiving bank accounts
|
|
192
|
+
|
|
193
|
+
These are the accounts that `matchAccount` compares the receiver with. An account belongs to your service, and a branch only matches against the accounts linked to it. `bankAccounts.create` adds an account and links it to the branch of your API key.
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
197
|
+
|
|
198
|
+
const easyslip = new EasySlip()
|
|
199
|
+
|
|
200
|
+
const account = await easyslip.bankAccounts.create({
|
|
201
|
+
bankCode: '004', // bank codes come from easyslip.banks.list()
|
|
202
|
+
bankNumber: '1234567890',
|
|
203
|
+
nameTh: 'บริษัท ตัวอย่าง จำกัด',
|
|
204
|
+
nameEn: 'EXAMPLE CO., LTD.',
|
|
205
|
+
type: 'JURISTIC',
|
|
206
|
+
})
|
|
207
|
+
await easyslip.bankAccounts.update(account.id, { nameEn: 'EXAMPLE COMPANY LIMITED' })
|
|
208
|
+
|
|
209
|
+
// One page of the accounts linked to this branch
|
|
210
|
+
const page = await easyslip.bankAccounts.list({ page: 1, limit: 20 })
|
|
211
|
+
console.log(`${page.total} accounts linked to this branch`)
|
|
212
|
+
|
|
213
|
+
// Every account in the service. The SDK fetches the pages for you.
|
|
214
|
+
for await (const item of easyslip.bankAccounts.iterateAll()) {
|
|
215
|
+
console.log(item.id, item.bankNumber, item.nameEn)
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
| Method | What it does |
|
|
220
|
+
| --- | --- |
|
|
221
|
+
| `bankAccounts.create(params)` | Adds an account to the service and links it to this branch. |
|
|
222
|
+
| `bankAccounts.get(id)` | Returns one account. |
|
|
223
|
+
| `bankAccounts.update(id, params)` | Changes only the fields you pass. `extraVerify: null` clears that field. |
|
|
224
|
+
| `bankAccounts.link(id)` | Links an account of the service to this branch. Linking twice is fine. |
|
|
225
|
+
| `bankAccounts.unlink(id)` | Removes the link to this branch. The account stays in the service. |
|
|
226
|
+
| `bankAccounts.list(params)` | Returns one page of the accounts linked to this branch. `page` starts at 1, and `limit` is 1 to 100 (default 20). |
|
|
227
|
+
| `bankAccounts.listAll(params)` | Returns one page of every account in the service, with the same paging. |
|
|
228
|
+
| `bankAccounts.iterate(params)` | Goes through every account linked to this branch, 100 per page. |
|
|
229
|
+
| `bankAccounts.iterateAll(params)` | Goes through every account in the service, 100 per page. |
|
|
230
|
+
|
|
231
|
+
`type` is `NATURAL` for a personal account or `JURISTIC` for a company account, and the `AccountType` type accepts only those two values. The four list methods can filter by `type` and by `bankCode`. Adding an account that already exists throws `ConflictError`.
|
|
232
|
+
|
|
233
|
+
### Verify many slips in the background
|
|
234
|
+
|
|
235
|
+
A background job checks a slip on the EasySlip side and posts the result to your [webhook](#webhooks). Use jobs when you have many slips or do not want to wait for the bank. Pass an https `callbackUrl`, unless your branch already has a default webhook URL.
|
|
236
|
+
|
|
237
|
+
This example reads the QR code text of several slips from `payloads.txt`, one per line, and submits them as one batch:
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import { readFile } from 'node:fs/promises'
|
|
241
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
242
|
+
|
|
243
|
+
const easyslip = new EasySlip()
|
|
244
|
+
const payloads = (await readFile('payloads.txt', 'utf8')).split('\n').filter(Boolean)
|
|
245
|
+
|
|
246
|
+
const batch = await easyslip.jobs.submitBankBatch(
|
|
247
|
+
payloads.map((payload) => ({ payload, checkDuplicate: true })),
|
|
248
|
+
{ callbackUrl: 'https://example.com/webhooks/easyslip' },
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
// Your webhook receives every result. You can also wait for a job here:
|
|
252
|
+
for (const { jobId } of batch.jobs) {
|
|
253
|
+
const job = await easyslip.jobs.waitForJob(jobId)
|
|
254
|
+
console.log(jobId, job.status, job.result?.status, job.result?.data?.amountInSlip)
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
| Method | What it does |
|
|
259
|
+
| --- | --- |
|
|
260
|
+
| `jobs.submitBank(payload, options)` | Submits one slip, with the same options as `verify` plus `callbackUrl`. |
|
|
261
|
+
| `jobs.submitBankBatch(slips, { callbackUrl })` | Submits 1 to 100 slips in one request. Each slip has its own options. |
|
|
262
|
+
| `jobs.submitBankMany(slips, { callbackUrl })` | Submits any number of slips in batches of 100, one batch after another. |
|
|
263
|
+
| `jobs.get(jobId)` | Returns the job, with its result once it has finished. |
|
|
264
|
+
| `jobs.waitForJob(jobId, options)` | Checks the job every `interval` milliseconds (default 2000) until it is `done` or `failed`, for at most `timeout` milliseconds (default 600000, which is 10 minutes). |
|
|
265
|
+
|
|
266
|
+
### Generate a payment QR code
|
|
267
|
+
|
|
268
|
+
The API creates PromptPay and bill payment QR codes that your customers scan to pay you. Creating a QR code uses no quota. `amount` is in baht, from 0.01 to 9,999,999,999.99 with at most two decimals. Leave it out to let the payer type the amount.
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
import { writeFile } from 'node:fs/promises'
|
|
272
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
273
|
+
|
|
274
|
+
const easyslip = new EasySlip()
|
|
275
|
+
|
|
276
|
+
// PromptPay to a phone number, for 250 baht
|
|
277
|
+
const promptPay = await easyslip.v1.generateQr({ type: 'PROMPTPAY', msisdn: '0812345678', amount: 250 })
|
|
278
|
+
await writeFile('promptpay.png', promptPay.imageBytes())
|
|
279
|
+
|
|
280
|
+
// Bill payment to your biller ID, with your invoice number as reference 1
|
|
281
|
+
const bill = await easyslip.v1.generateQr({
|
|
282
|
+
type: 'PROMPTPAY',
|
|
283
|
+
billerId: '010555512345601',
|
|
284
|
+
ref1: 'INV1001',
|
|
285
|
+
amount: 1500,
|
|
286
|
+
})
|
|
287
|
+
await writeFile('bill.png', bill.imageBytes())
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
| `type` | Required fields | Optional fields |
|
|
291
|
+
| --- | --- | --- |
|
|
292
|
+
| `PROMPTPAY` | One of `msisdn` (phone number), `natId` (national ID or tax ID), `eWalletId` or `billerId` | `amount`, and `ref1` and `ref2` with `billerId` |
|
|
293
|
+
| `KSHOP` | `ref1`, the shop ID: `KB` followed by 12 digits | `amount` |
|
|
294
|
+
| `MAE_MANEE` | `ref1`, the 15-digit shop ID | `amount` |
|
|
295
|
+
| `TUNGNGERN` | `ref1`, the merchant code, and `merchantName`, the shop name in English | `amount` |
|
|
296
|
+
|
|
297
|
+
The older `BILL_PAYMENT` type still works. With the same `billerId` it creates the same QR code as `PROMPTPAY`. The `QrType` type lists the five types the API accepts.
|
|
298
|
+
|
|
299
|
+
### Legacy API v1
|
|
300
|
+
|
|
301
|
+
Use `easyslip.v1` only if your integration already uses API v1. New code should use the v2 methods above. The v1 methods return the slip itself, and with `checkDuplicate: true` a slip that was verified before throws `DuplicateSlipError`.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { DuplicateSlipError, EasySlip } from '@easyslip/sdk'
|
|
305
|
+
|
|
306
|
+
const easyslip = new EasySlip()
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
const slip = await easyslip.v1.verifyBankByImage('./slip.jpg', { checkDuplicate: true })
|
|
310
|
+
console.log(slip.transRef, slip.amount.amount)
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (!(error instanceof DuplicateSlipError)) throw error
|
|
313
|
+
console.log('This slip was already used. Transfer date:', error.slip?.date)
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
API v1 also has `verifyBankByPayload`, `verifyBankByUrl`, `verifyBankByBase64`, `verifyTruemoneyByImage` and `getMe`.
|
|
318
|
+
|
|
319
|
+
### Promises, timeouts and cancelling
|
|
320
|
+
|
|
321
|
+
Every method returns a promise, so you can check several slips at once. Pass `signal` to cancel a call or to give it a deadline. Keep the number of calls in flight small, because the API limits how many requests you can send.
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
325
|
+
|
|
326
|
+
const easyslip = new EasySlip()
|
|
327
|
+
const files = ['./slip-1001.jpg', './slip-1002.jpg', './slip-1003.jpg']
|
|
328
|
+
|
|
329
|
+
const outcomes = await Promise.allSettled(
|
|
330
|
+
files.map((file) =>
|
|
331
|
+
easyslip.verify.bankByImage(file, { checkDuplicate: true, signal: AbortSignal.timeout(60_000) }),
|
|
332
|
+
),
|
|
333
|
+
)
|
|
334
|
+
outcomes.forEach((outcome, index) => {
|
|
335
|
+
if (outcome.status === 'fulfilled') console.log(files[index], outcome.value.rawSlip.transRef)
|
|
336
|
+
else console.log(files[index], 'failed:', outcome.reason)
|
|
337
|
+
})
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## The result object
|
|
341
|
+
|
|
342
|
+
A v2 verification returns your check results at the top level and the slip as the bank reports it in `rawSlip`. The [API reference](https://document.easyslip.com/en/v2/verify/bank/) describes the full model.
|
|
343
|
+
|
|
344
|
+
### Bank slip
|
|
345
|
+
|
|
346
|
+
| Field | Type | Meaning | Example |
|
|
347
|
+
| --- | --- | --- | --- |
|
|
348
|
+
| `amountInSlip` | `number` | The amount transferred, in baht. | `1500` |
|
|
349
|
+
| `isAmountMatched` | `boolean` | Whether the amount equals `matchAmount`. Only with `matchAmount`. | `true` |
|
|
350
|
+
| `amountInOrder` | `number` | The `matchAmount` you sent. Only with `matchAmount`. | `1500` |
|
|
351
|
+
| `matchedAccount` | `object` or `null` | Your registered account that the slip paid, or `null`. Only with `matchAccount`. | `{ bankNumber: '123-4-56789-0', ... }` |
|
|
352
|
+
| `isDuplicate` | `boolean` | Whether the slip was verified before. Only with `checkDuplicate`. | `false` |
|
|
353
|
+
| `remark` | `string` | Your `remark`. Only when you sent one. | `'Order 1001'` |
|
|
354
|
+
| `rawSlip.transRef` | `string` | The bank's reference for the transfer. Each transfer has its own, so you can store it to catch reuse. | `'68370160657749I376388B35'` |
|
|
355
|
+
| `rawSlip.date` | `string` | When the transfer happened, in ISO 8601 with the Thai time offset. | `'2024-01-15T14:30:00+07:00'` |
|
|
356
|
+
| `rawSlip.amount.amount` | `number` | The amount, in baht. | `1500` |
|
|
357
|
+
| `rawSlip.fee` | `number` | The transfer fee, when the bank reports one. | `0` |
|
|
358
|
+
| `rawSlip.sender.bank` | `object` | The sender's bank: `id` (the bank code), `name` and `short`. | `{ id: '004', name: 'กสิกรไทย', short: 'KBANK' }` |
|
|
359
|
+
| `rawSlip.sender.account.name` | `object` | The sender's name in Thai (`th`) and English (`en`), when the bank shows it. | `{ th: 'นาย ผู้โอน ทดสอบ', en: 'MR. SENDER TEST' }` |
|
|
360
|
+
| `rawSlip.sender.account.bank` | `object` | The sender's account number, partly hidden by the bank. | `{ type: 'BANKAC', account: '123-4-xxxxx-5' }` |
|
|
361
|
+
| `rawSlip.receiver.bank` | `object` | The receiver's bank, in the same form as the sender's. | `{ id: '014', name: 'ไทยพาณิชย์', short: 'SCB' }` |
|
|
362
|
+
| `rawSlip.receiver.account.name` | `object` | The receiver's name. | `{ th: 'บริษัท ตัวอย่าง จำกัด' }` |
|
|
363
|
+
| `rawSlip.receiver.account.bank` | `object` | The receiver's account number, partly hidden. | `{ type: 'BANKAC', account: 'xxx-x-x5678-x' }` |
|
|
364
|
+
| `rawSlip.receiver.account.proxy` | `object` | The PromptPay ID the money was sent to, when the transfer used one. | `{ type: 'MSISDN', account: '08xxxxxxxx89' }` |
|
|
365
|
+
| `rawSlip.ref1`, `rawSlip.ref2`, `rawSlip.ref3` | `string` | Bill payment references. They are empty for ordinary transfers. | `''` |
|
|
366
|
+
|
|
367
|
+
### TrueMoney Wallet receipt
|
|
368
|
+
|
|
369
|
+
The top-level fields are the same as for a bank slip. `rawSlip` holds:
|
|
370
|
+
|
|
371
|
+
| Field | Type | Meaning | Example |
|
|
372
|
+
| --- | --- | --- | --- |
|
|
373
|
+
| `rawSlip.transactionId` | `string` | The TrueMoney transaction ID. | `'50012345678901'` |
|
|
374
|
+
| `rawSlip.date` | `string` | When the payment happened, in ISO 8601. | `'2026-10-26T14:30:00+07:00'` |
|
|
375
|
+
| `rawSlip.amount` | `number` | The amount, in baht. | `500` |
|
|
376
|
+
| `rawSlip.sender.name` | `string` | The payer's name. | `'นาย ผู้โอน ทดสอบ'` |
|
|
377
|
+
| `rawSlip.receiver.name` | `string` | The receiver's name. | `'นาย รับเงิน ทดสอบ'` |
|
|
378
|
+
| `rawSlip.receiver.phone` | `string` | The receiver's phone number, partly hidden. | `'08x-xxx-4567'` |
|
|
379
|
+
|
|
380
|
+
Every result also has a `meta` property with the HTTP status, the `requestId`, the rate limit counters and the response headers. It is not part of the data, so `JSON.stringify(result)` leaves it out.
|
|
381
|
+
|
|
382
|
+
## Configuration
|
|
383
|
+
|
|
384
|
+
Pass options to `new EasySlip({ ... })`. An option you leave out comes from its environment variable, or else from its default.
|
|
385
|
+
|
|
386
|
+
| Option | Environment variable | Default | What it does |
|
|
387
|
+
| --- | --- | --- | --- |
|
|
388
|
+
| `apiKey` | `EASYSLIP_API_KEY` | None | Your API key. Without a key, `new EasySlip()` throws `InputError`. |
|
|
389
|
+
| No option | `EASYSLIP_API_KEY_FILE` | None | The path of a file that holds your API key, such as a Docker or Kubernetes secret. The SDK uses it when there is no `apiKey` and no `EASYSLIP_API_KEY`. |
|
|
390
|
+
| `timeout` | No variable | `30000` | How long one attempt may take, in milliseconds. |
|
|
391
|
+
| `maxRetries` | No variable | `2` | How many times the SDK retries a failed request. `0` turns retries off. |
|
|
392
|
+
| `fetch` | No variable | The global `fetch` | The function the SDK sends requests with. |
|
|
393
|
+
| `v2BaseUrl` | `EASYSLIP_V2_BASE_URL` | `https://api.easyslip.com/v2` | The address of API v2. |
|
|
394
|
+
| `v1BaseUrl` | `EASYSLIP_V1_BASE_URL` | `https://api.easyslip.com/v1` | The address of API v1. |
|
|
395
|
+
| No option | `EASYSLIP_WEBHOOK_SECRET` | None | Your webhook secret. The [webhook functions](#webhooks) use it when you do not pass a secret. |
|
|
396
|
+
|
|
397
|
+
Every method also takes `timeout`, `maxRetries` and `signal` for that call alone. Verification methods take them together with the verification options, for example `verify.bankByImage(file, { checkDuplicate: true, timeout: 10_000 })`.
|
|
398
|
+
|
|
399
|
+
### Custom HTTP client
|
|
400
|
+
|
|
401
|
+
Pass your own `fetch` to log requests, send them through a proxy or record them in tests. It receives the URL and the request options, and it returns a `Response`.
|
|
402
|
+
|
|
403
|
+
```ts
|
|
404
|
+
import { EasySlip } from '@easyslip/sdk'
|
|
405
|
+
|
|
406
|
+
const easyslip = new EasySlip({
|
|
407
|
+
fetch: async (url, init) => {
|
|
408
|
+
const started = Date.now()
|
|
409
|
+
const response = await fetch(url, init)
|
|
410
|
+
console.log(init.method, url, response.status, `${Date.now() - started} ms`)
|
|
411
|
+
return response
|
|
412
|
+
},
|
|
413
|
+
})
|
|
414
|
+
console.log((await easyslip.getInfo()).application.name)
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
The client opens no connections of its own, so you do not need to close it. If your `fetch` uses its own connection pool, such as an undici `Agent`, close that pool when your application shuts down.
|
|
418
|
+
|
|
419
|
+
## Error handling
|
|
420
|
+
|
|
421
|
+
Every error the SDK throws is an `EasySlipError`. Errors that come from the API are `ApiError`s, and they carry `status`, `code` (such as `SLIP_NOT_FOUND`), `message`, `requestId` and `retryAfter`. Some classes are subclasses of others: `QuotaExceededError` is a `PermissionDeniedError`; `InvalidImageError`, `InvalidUrlError` and `DuplicateSlipError` are `InvalidRequestError`s; `SlipNotFoundError` and `SlipPendingError` are `NotFoundError`s; `ServiceUnavailableError` is a `ServerError`; and `RequestTimeoutError` is a `NetworkError`.
|
|
422
|
+
|
|
423
|
+
| Error class | HTTP status | When it happens | What to do |
|
|
424
|
+
| --- | --- | --- | --- |
|
|
425
|
+
| `AuthenticationError` | 401 | The API key is missing, wrong or revoked. | Check the key you pass or `EASYSLIP_API_KEY`. Create a new key in the developer portal if you need one. |
|
|
426
|
+
| `PermissionDeniedError` | 403 | The key is valid but may not make this request, for example because the branch is turned off, the plan has expired or your server's IP address is not on the allowed list. | Read `error.code`, then fix that setting in the developer portal. |
|
|
427
|
+
| `QuotaExceededError` | 403 | Your quota is used up. This request was not charged. | Top up or change your plan, then send the request again. |
|
|
428
|
+
| `InvalidRequestError` | 400 | The API rejected a value in the request, such as the QR code text or an option. | Fix the value that `error.message` names. Sending the same request again fails the same way. |
|
|
429
|
+
| `InvalidImageError` | 400 | The image has no readable slip QR code, is not a supported type or is too large. | Ask the customer for a clear JPEG, PNG, GIF or WebP image under 4 MB. |
|
|
430
|
+
| `InvalidUrlError` | 400 | The image URL or `callbackUrl` points to a private address, does not use http or https, or cannot be reached. | Use a public https URL. |
|
|
431
|
+
| `DuplicateSlipError` | 400 | Only in API v1 with `checkDuplicate: true`: the slip was verified before. `error.slip` holds the first result. | Treat the payment as already received and do not credit it again. |
|
|
432
|
+
| `NotFoundError` | 404 | The bank account, job or other item you asked for does not exist. | Check the ID you passed. |
|
|
433
|
+
| `SlipNotFoundError` | 404 | The bank has no transfer that matches this slip. | Do not accept the payment. Ask the customer for the original slip. |
|
|
434
|
+
| `SlipPendingError` | 404 | The bank has not published the transfer yet. This happens with very recent Bangkok Bank slips. | Verify the slip again after a minute. |
|
|
435
|
+
| `ConflictError` | 409 | The bank account you are adding already exists. | Use the existing account. `bankAccounts.listAll()` finds it. |
|
|
436
|
+
| `RateLimitError` | 429 | You sent too many requests. `error.retryAfter` says how many seconds to wait. | Send fewer requests at the same time. The SDK has already retried this one. |
|
|
437
|
+
| `ServerError` | 500, 502, 504; other 5xx except 503 | Something failed on the EasySlip side. | Try again later. If it keeps happening, contact support with `error.requestId`. |
|
|
438
|
+
| `ServiceUnavailableError` | 503 | The service is briefly unavailable, for example while quotas renew. | Try again in a few minutes. |
|
|
439
|
+
| `ApiError` | Another 4xx status | The API answered with an error status that has no class of its own. | Log `status`, `code` and `requestId`, and contact support if it continues. |
|
|
440
|
+
| `NetworkError` | Network | No response arrived, because of a DNS, connection or TLS failure. | Check your network or proxy, then try again. |
|
|
441
|
+
| `RequestTimeoutError` | Network | No response arrived within `timeout`. | Try again, or raise `timeout`. A verification may already have been counted. |
|
|
442
|
+
| `InputError` | Before sending | The SDK found a problem before sending, such as a missing API key, empty QR code text, an image over 4 MB or a file that is not an image. `error.code` names the problem. | Fix the value you passed. |
|
|
443
|
+
| `WebhookSignatureError` | Before sending | A webhook has no signature, or its signature does not match. | Reject the webhook with status 400. |
|
|
444
|
+
| `JobWaitTimeoutError` | Before sending | `waitForJob` reached its `timeout`. `error.lastJob` is the last state it saw. | Wait longer, or rely on the webhook. |
|
|
445
|
+
| `BatchSubmitError` | Before sending | `submitBankMany` stopped part way. `error.submitted` lists the batches that were sent, and `error.failedChunk` is the batch that failed. | Submit the failed batch and the ones after it again. Batches that were already sent keep running. |
|
|
446
|
+
|
|
447
|
+
This example checks an uploaded slip at checkout and turns each failure into a message for the customer:
|
|
448
|
+
|
|
449
|
+
```ts
|
|
450
|
+
import { readFile } from 'node:fs/promises'
|
|
451
|
+
import {
|
|
452
|
+
ApiError,
|
|
453
|
+
EasySlip,
|
|
454
|
+
InvalidImageError,
|
|
455
|
+
QuotaExceededError,
|
|
456
|
+
RateLimitError,
|
|
457
|
+
SlipNotFoundError,
|
|
458
|
+
SlipPendingError,
|
|
459
|
+
} from '@easyslip/sdk'
|
|
460
|
+
|
|
461
|
+
const easyslip = new EasySlip()
|
|
462
|
+
|
|
463
|
+
async function checkPayment(upload: File, orderTotal: number): Promise<string> {
|
|
464
|
+
try {
|
|
465
|
+
const result = await easyslip.verify.bankByImage(upload, {
|
|
466
|
+
matchAmount: orderTotal,
|
|
467
|
+
matchAccount: true,
|
|
468
|
+
checkDuplicate: true,
|
|
469
|
+
})
|
|
470
|
+
if (result.isDuplicate) return 'This slip has already been used.'
|
|
471
|
+
if (!result.isAmountMatched) return `This slip is for ${result.amountInSlip} baht.`
|
|
472
|
+
if (result.matchedAccount === null) return 'This slip was not paid to our account.'
|
|
473
|
+
return 'Payment received.'
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if (error instanceof SlipNotFoundError) return 'We could not find this transfer at the bank.'
|
|
476
|
+
if (error instanceof SlipPendingError) return 'The bank is still processing this transfer. Try again in a minute.'
|
|
477
|
+
if (error instanceof InvalidImageError) return 'Please upload a clear photo or screenshot of the slip.'
|
|
478
|
+
if (error instanceof QuotaExceededError || error instanceof RateLimitError) {
|
|
479
|
+
console.error('EasySlip limit reached', error.code, error.requestId)
|
|
480
|
+
return 'We cannot check slips right now. Please try again shortly.'
|
|
481
|
+
}
|
|
482
|
+
if (error instanceof ApiError) {
|
|
483
|
+
console.error('EasySlip error', error.status, error.code, error.requestId)
|
|
484
|
+
return 'We could not check the slip. Please try again.'
|
|
485
|
+
}
|
|
486
|
+
throw error
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const upload = new File([await readFile('./slip.jpg')], 'slip.jpg', { type: 'image/jpeg' })
|
|
491
|
+
console.log(await checkPayment(upload, 1500))
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
### What the SDK retries
|
|
495
|
+
|
|
496
|
+
The SDK retries a failed request up to `maxRetries` times (2 by default). It waits about half a second before the first retry and doubles the wait each time, up to 8 seconds. When the API sends `Retry-After`, the SDK waits that long instead, but if the wait is longer than 60 seconds it throws the error at once.
|
|
497
|
+
|
|
498
|
+
| Request | Retried when | Never retried when |
|
|
499
|
+
| --- | --- | --- |
|
|
500
|
+
| Reading data and linking accounts: `getInfo`, `banks.list`, `bankAccounts.list`, `listAll`, `get` and `link`, `jobs.get`, `v1.getMe`, `v1.generateQr` | The network fails, the call times out, or the API answers 429, 500, 502, 503 or 504. | The API answers 400, 401, 403, 404 or 409. |
|
|
501
|
+
| Verifications and job submissions, which use quota | The API answers 429, or the connection failed before the request left your server. | The request was sent and then timed out or got a 5xx answer. |
|
|
502
|
+
| Changing bank accounts: `create`, `update` and `unlink` | The API answers 429, or the connection failed before the request left your server. | The request was sent and then timed out or got a 5xx answer. |
|
|
503
|
+
|
|
504
|
+
A verification that reached the API may have been counted even if no answer came back. Retrying it could charge you twice, so the SDK leaves that decision to you.
|
|
505
|
+
|
|
506
|
+
## Webhooks
|
|
507
|
+
|
|
508
|
+
When a background job finishes, EasySlip sends a POST request to your `callbackUrl`. If your branch has a webhook secret, the request has an `X-EasySlip-Signature` header. Check the signature against the raw body before you parse or trust anything in it.
|
|
509
|
+
|
|
510
|
+
This server uses only Node.js:
|
|
511
|
+
|
|
512
|
+
```ts
|
|
513
|
+
import { createServer } from 'node:http'
|
|
514
|
+
import {
|
|
515
|
+
constructWebhookEvent,
|
|
516
|
+
InputError,
|
|
517
|
+
WEBHOOK_SIGNATURE_HEADER,
|
|
518
|
+
WebhookSignatureError,
|
|
519
|
+
} from '@easyslip/sdk'
|
|
520
|
+
|
|
521
|
+
if (!process.env.EASYSLIP_WEBHOOK_SECRET) throw new Error('Set EASYSLIP_WEBHOOK_SECRET')
|
|
522
|
+
|
|
523
|
+
createServer(async (request, response) => {
|
|
524
|
+
try {
|
|
525
|
+
const chunks: Buffer[] = []
|
|
526
|
+
for await (const chunk of request) chunks.push(chunk as Buffer)
|
|
527
|
+
const rawBody = Buffer.concat(chunks)
|
|
528
|
+
const signature = request.headers[WEBHOOK_SIGNATURE_HEADER.toLowerCase()]
|
|
529
|
+
|
|
530
|
+
// Uses the secret in EASYSLIP_WEBHOOK_SECRET
|
|
531
|
+
const event = constructWebhookEvent(rawBody, signature)
|
|
532
|
+
if (event.status === 'success') {
|
|
533
|
+
console.log('Verified', event.jobId, event.data?.rawSlip.transRef, event.data?.amountInSlip)
|
|
534
|
+
} else {
|
|
535
|
+
console.log('Not verified', event.jobId, event.status, event.error?.code)
|
|
536
|
+
}
|
|
537
|
+
response.writeHead(200).end('ok')
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (error instanceof WebhookSignatureError || error instanceof InputError) {
|
|
540
|
+
// The signature is missing or wrong, or the signed body is not a webhook event
|
|
541
|
+
response.writeHead(400).end('invalid webhook')
|
|
542
|
+
} else {
|
|
543
|
+
console.error('Webhook failed', error)
|
|
544
|
+
response.writeHead(500).end('error')
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}).listen(3000)
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
The server answers 400 when the signature is missing or wrong and when a signed body is not a webhook event, and 500 when anything else fails. EasySlip sends the event again after a non-2xx answer.
|
|
551
|
+
|
|
552
|
+
In Express, add `express.raw({ type: 'application/json' })` to the webhook route so that `req.body` is the raw body as a `Buffer`, then pass `req.body` and `req.get(WEBHOOK_SIGNATURE_HEADER)` to `constructWebhookEvent`. Do not use `express.json()` on that route. Parsing the JSON and turning it back into text changes the bytes, and the signature then no longer matches.
|
|
553
|
+
|
|
554
|
+
| Function | What it does |
|
|
555
|
+
| --- | --- |
|
|
556
|
+
| `constructWebhookEvent(rawBody, header, secret?)` | Checks the signature, then returns the parsed event. It throws `WebhookSignatureError` when the signature is missing or wrong, and `InputError` when the body is not a JSON object with `jobId`, `status` and `timestamp`. |
|
|
557
|
+
| `verifyWebhookSignature(rawBody, header, secret?)` | Returns `true` when the signature matches and `false` when it does not. |
|
|
558
|
+
| `parseWebhookEvent(rawBody)` | Parses the event without checking the signature, and throws `InputError` for a body that is not an event. Use it only after you have checked the signature yourself. |
|
|
559
|
+
|
|
560
|
+
`secret` defaults to `EASYSLIP_WEBHOOK_SECRET`. Answer with a 2xx status within 5 seconds. EasySlip may send the same event more than once, so use `jobId` to skip events you have already handled.
|
|
561
|
+
|
|
562
|
+
## Security and privacy
|
|
563
|
+
|
|
564
|
+
- Keep your API key on your server. Never put it in a web page, a mobile app or a public repository, because anyone who has the key can use your quota.
|
|
565
|
+
- Load the key from `EASYSLIP_API_KEY` or a secret file instead of writing it in your code.
|
|
566
|
+
- The SDK never puts your API key in error messages, so you can log its errors.
|
|
567
|
+
- Slips show names, bank accounts and amounts, which are personal data. Store only what you need, protect it like your other customer data and follow the Thai Personal Data Protection Act (PDPA).
|
|
568
|
+
- Check the signature of every webhook before you act on it.
|
|
569
|
+
|
|
570
|
+
## Versioning and support policy
|
|
571
|
+
|
|
572
|
+
- Releases follow [semantic versioning](https://semver.org). Minor and patch releases do not break existing code. A breaking change comes in a new major version with upgrade notes in the changelog.
|
|
573
|
+
- The SDK supports the Node.js versions that the Node.js project still maintains. After a Node.js version reaches its end of life, a later release may stop supporting it, and the changelog says so.
|
|
574
|
+
- [CHANGELOG.md](CHANGELOG.md) and the [releases page](https://github.com/EASYSLIP-CO-LTD/sdk-js/releases) list every release and its changes.
|
|
575
|
+
|
|
576
|
+
## Examples and documentation
|
|
577
|
+
|
|
578
|
+
- [`examples`](examples) has one runnable program for every method. Clone this repository, run `npm install` and `npm run build`, then run an example, for example `EASYSLIP_API_KEY="your-api-key" node examples/verify-bank-image.ts ./slip.jpg`. If you leave out its arguments, an example prints how to call it.
|
|
579
|
+
- The [SDK guide](https://document.easyslip.com/en/sdk/javascript) covers this SDK in the EasySlip documentation.
|
|
580
|
+
- The [API reference](https://document.easyslip.com) describes every endpoint, field and [error code](https://document.easyslip.com/en/reference/error-codes).
|
|
581
|
+
|
|
582
|
+
## Support
|
|
583
|
+
|
|
584
|
+
For account, quota or billing questions, use the [contact page](https://developer.easyslip.com/contact) and include the request id if available. Report bugs and suggest features in [issues](https://github.com/EASYSLIP-CO-LTD/sdk-js/issues). Never post API keys or real slip images.
|
|
585
|
+
|
|
586
|
+
## Contributing
|
|
587
|
+
|
|
588
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for setup, checks and pull requests. Maintainers follow [RELEASING.md](RELEASING.md).
|
|
589
|
+
|
|
590
|
+
## Security reports
|
|
591
|
+
|
|
592
|
+
Report vulnerabilities privately as described in [SECURITY.md](SECURITY.md), or email [hello@easyslip.com](mailto:hello@easyslip.com). Never report vulnerabilities in public issues.
|
|
593
|
+
|
|
594
|
+
## License
|
|
595
|
+
|
|
596
|
+
[MIT](LICENSE). Copyright (c) 2026 EasySlip Co., Ltd.
|