@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
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type FileInput } from '../core/files.js';
|
|
2
|
+
import type { RequestOptions, Transport } from '../core/http.js';
|
|
3
|
+
import type { WithMeta } from '../core/meta.js';
|
|
4
|
+
import type { BankSlip, QrCode, TrueWalletSlip, V1Me } from '../models.js';
|
|
5
|
+
/** Options of the v1 verify calls. */
|
|
6
|
+
export interface V1VerifyOptions extends RequestOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Reject a slip this application verified before with `DuplicateSlipError`, whose `slip`
|
|
9
|
+
* is the original slip. v1 does not charge for a duplicate.
|
|
10
|
+
*/
|
|
11
|
+
checkDuplicate?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Shared fields of every QR request. */
|
|
14
|
+
interface QrBase {
|
|
15
|
+
/** Amount in baht. Leave out for a QR the payer fills in. */
|
|
16
|
+
amount?: number;
|
|
17
|
+
}
|
|
18
|
+
/** PromptPay QR: exactly one of `msisdn`, `natId`, `eWalletId`, `billerId`. */
|
|
19
|
+
export interface PromptPayQr extends QrBase {
|
|
20
|
+
type: 'PROMPTPAY';
|
|
21
|
+
msisdn?: string;
|
|
22
|
+
natId?: string;
|
|
23
|
+
eWalletId?: string;
|
|
24
|
+
/** Biller id (bill payment). `ref1` and `ref2` are only allowed with it. */
|
|
25
|
+
billerId?: string;
|
|
26
|
+
/** Reference 1, with `billerId` only. Optional: leave it out for a QR without Ref1. */
|
|
27
|
+
ref1?: string;
|
|
28
|
+
/** Reference 2, with `billerId` and `ref1` only. */
|
|
29
|
+
ref2?: string;
|
|
30
|
+
}
|
|
31
|
+
/** Bill payment QR, the older name for PromptPay with `billerId`. */
|
|
32
|
+
export interface BillPaymentQr extends QrBase {
|
|
33
|
+
type: 'BILL_PAYMENT';
|
|
34
|
+
billerId: string;
|
|
35
|
+
/** Reference 1. Optional: leave it out for a QR without Ref1. */
|
|
36
|
+
ref1?: string;
|
|
37
|
+
/** Reference 2, only together with `ref1`. */
|
|
38
|
+
ref2?: string;
|
|
39
|
+
}
|
|
40
|
+
/** K SHOP or Mae Manee merchant QR. */
|
|
41
|
+
export interface MerchantQr extends QrBase {
|
|
42
|
+
type: 'KSHOP' | 'MAE_MANEE';
|
|
43
|
+
/**
|
|
44
|
+
* The shop id. K SHOP: `KB` and 12 digits (the API accepts any case and removes a leading
|
|
45
|
+
* `KPS004`). Mae Manee: 15 digits.
|
|
46
|
+
*/
|
|
47
|
+
ref1: string;
|
|
48
|
+
}
|
|
49
|
+
/** Thung Ngern merchant QR. */
|
|
50
|
+
export interface TungNgernQr extends QrBase {
|
|
51
|
+
type: 'TUNGNGERN';
|
|
52
|
+
/** Merchant code, 1 to 20 letters or digits. */
|
|
53
|
+
ref1: string;
|
|
54
|
+
/**
|
|
55
|
+
* The English shop name shown in the Tung Ngern app. The API keeps A-Z and 0-9, uppercased
|
|
56
|
+
* and cut to 20 characters, and rejects a name with none of them (a Thai-only name).
|
|
57
|
+
*/
|
|
58
|
+
merchantName: string;
|
|
59
|
+
}
|
|
60
|
+
/** Body of `v1.generateQr`. The API checks the field combinations. */
|
|
61
|
+
export type QrRequest = PromptPayQr | BillPaymentQr | MerchantQr | TungNgernQr;
|
|
62
|
+
/** The QR types `v1.generateQr` accepts. */
|
|
63
|
+
export type QrType = QrRequest['type'];
|
|
64
|
+
/** A QR code from `generateQr`, with a helper that decodes the PNG. */
|
|
65
|
+
export type QrCodeResult = WithMeta<QrCode> & {
|
|
66
|
+
/** The PNG image as bytes. */
|
|
67
|
+
imageBytes(): Uint8Array;
|
|
68
|
+
};
|
|
69
|
+
/** The v1 API (`client.v1`). Verify calls are billed; `generateQr` is free. */
|
|
70
|
+
export declare class V1 {
|
|
71
|
+
#private;
|
|
72
|
+
constructor(transport: Transport);
|
|
73
|
+
/** Quota and credit of the application. */
|
|
74
|
+
getMe(options?: RequestOptions): Promise<WithMeta<V1Me>>;
|
|
75
|
+
/** Verifies a bank slip from its QR payload (`GET /verify`). */
|
|
76
|
+
verifyBankByPayload(payload: string, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
77
|
+
/** Verifies a bank slip image (multipart field `file`). */
|
|
78
|
+
verifyBankByImage(file: FileInput, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
79
|
+
/** Verifies a base64 bank slip image. Sent as the JSON field `image`. */
|
|
80
|
+
verifyBankByBase64(base64: string, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
81
|
+
/** Verifies a bank slip image the API downloads from a public URL. */
|
|
82
|
+
verifyBankByUrl(url: string, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
83
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
84
|
+
verifyTruemoneyByImage(file: FileInput, options?: V1VerifyOptions): Promise<WithMeta<TrueWalletSlip>>;
|
|
85
|
+
/** Generates a payment QR image. No quota is used. */
|
|
86
|
+
generateQr(request: QrRequest, options?: RequestOptions): Promise<QrCodeResult>;
|
|
87
|
+
}
|
|
88
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { call, imageForm, requestOptions, requireString } from '../core/call.js';
|
|
2
|
+
import { InputError } from '../core/errors.js';
|
|
3
|
+
import { prepareFile } from '../core/files.js';
|
|
4
|
+
function decodeBase64(text) {
|
|
5
|
+
const binary = atob(text);
|
|
6
|
+
const out = new Uint8Array(binary.length);
|
|
7
|
+
for (let i = 0; i < binary.length; i++)
|
|
8
|
+
out[i] = binary.charCodeAt(i);
|
|
9
|
+
return out;
|
|
10
|
+
}
|
|
11
|
+
function boolString(value) {
|
|
12
|
+
return value ? 'true' : 'false';
|
|
13
|
+
}
|
|
14
|
+
/** The v1 API (`client.v1`). Verify calls are billed; `generateQr` is free. */
|
|
15
|
+
export class V1 {
|
|
16
|
+
#t;
|
|
17
|
+
constructor(transport) {
|
|
18
|
+
this.#t = transport;
|
|
19
|
+
}
|
|
20
|
+
/** Quota and credit of the application. */
|
|
21
|
+
async getMe(options) {
|
|
22
|
+
return call(this.#t, 'V1Me', {
|
|
23
|
+
...requestOptions(options),
|
|
24
|
+
api: 'v1',
|
|
25
|
+
method: 'GET',
|
|
26
|
+
path: '/me',
|
|
27
|
+
retry: 'safe',
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** Verifies a bank slip from its QR payload (`GET /verify`). */
|
|
31
|
+
async verifyBankByPayload(payload, options) {
|
|
32
|
+
requireString(payload, 'payload');
|
|
33
|
+
return call(this.#t, 'BankSlip', {
|
|
34
|
+
...requestOptions(options),
|
|
35
|
+
api: 'v1',
|
|
36
|
+
method: 'GET',
|
|
37
|
+
path: '/verify',
|
|
38
|
+
query: {
|
|
39
|
+
payload,
|
|
40
|
+
checkDuplicate: options?.checkDuplicate === undefined
|
|
41
|
+
? undefined
|
|
42
|
+
: boolString(options.checkDuplicate),
|
|
43
|
+
},
|
|
44
|
+
retry: 'billed',
|
|
45
|
+
duplicateSlip: 'BankSlip',
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** Verifies a bank slip image (multipart field `file`). */
|
|
49
|
+
async verifyBankByImage(file, options) {
|
|
50
|
+
const prepared = await prepareFile(file);
|
|
51
|
+
const fields = {};
|
|
52
|
+
if (options?.checkDuplicate !== undefined) {
|
|
53
|
+
fields.checkDuplicate = boolString(options.checkDuplicate);
|
|
54
|
+
}
|
|
55
|
+
return call(this.#t, 'BankSlip', {
|
|
56
|
+
...requestOptions(options),
|
|
57
|
+
api: 'v1',
|
|
58
|
+
method: 'POST',
|
|
59
|
+
path: '/verify',
|
|
60
|
+
form: imageForm('file', prepared, fields),
|
|
61
|
+
retry: 'billed',
|
|
62
|
+
duplicateSlip: 'BankSlip',
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Verifies a base64 bank slip image. Sent as the JSON field `image`. */
|
|
66
|
+
async verifyBankByBase64(base64, options) {
|
|
67
|
+
requireString(base64, 'base64');
|
|
68
|
+
const json = { image: base64 };
|
|
69
|
+
if (options?.checkDuplicate !== undefined)
|
|
70
|
+
json.checkDuplicate = options.checkDuplicate;
|
|
71
|
+
return call(this.#t, 'BankSlip', {
|
|
72
|
+
...requestOptions(options),
|
|
73
|
+
api: 'v1',
|
|
74
|
+
method: 'POST',
|
|
75
|
+
path: '/verify',
|
|
76
|
+
json,
|
|
77
|
+
retry: 'billed',
|
|
78
|
+
duplicateSlip: 'BankSlip',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** Verifies a bank slip image the API downloads from a public URL. */
|
|
82
|
+
async verifyBankByUrl(url, options) {
|
|
83
|
+
requireString(url, 'url');
|
|
84
|
+
const json = { url };
|
|
85
|
+
if (options?.checkDuplicate !== undefined)
|
|
86
|
+
json.checkDuplicate = options.checkDuplicate;
|
|
87
|
+
return call(this.#t, 'BankSlip', {
|
|
88
|
+
...requestOptions(options),
|
|
89
|
+
api: 'v1',
|
|
90
|
+
method: 'POST',
|
|
91
|
+
path: '/verify',
|
|
92
|
+
json,
|
|
93
|
+
retry: 'billed',
|
|
94
|
+
duplicateSlip: 'BankSlip',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
98
|
+
async verifyTruemoneyByImage(file, options) {
|
|
99
|
+
const prepared = await prepareFile(file);
|
|
100
|
+
const fields = {};
|
|
101
|
+
if (options?.checkDuplicate !== undefined) {
|
|
102
|
+
fields.checkDuplicate = boolString(options.checkDuplicate);
|
|
103
|
+
}
|
|
104
|
+
return call(this.#t, 'TrueWalletSlip', {
|
|
105
|
+
...requestOptions(options),
|
|
106
|
+
api: 'v1',
|
|
107
|
+
method: 'POST',
|
|
108
|
+
path: '/verify/truewallet',
|
|
109
|
+
form: imageForm('file', prepared, fields),
|
|
110
|
+
retry: 'billed',
|
|
111
|
+
duplicateSlip: 'TrueWalletSlip',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** Generates a payment QR image. No quota is used. */
|
|
115
|
+
async generateQr(request, options) {
|
|
116
|
+
if (!request || typeof request !== 'object') {
|
|
117
|
+
throw new InputError('INVALID_ARGUMENT', 'request must be an object with a type');
|
|
118
|
+
}
|
|
119
|
+
const result = await call(this.#t, 'QrCode', {
|
|
120
|
+
...requestOptions(options),
|
|
121
|
+
api: 'v1',
|
|
122
|
+
method: 'POST',
|
|
123
|
+
path: '/qr/generate',
|
|
124
|
+
json: request,
|
|
125
|
+
retry: 'safe',
|
|
126
|
+
});
|
|
127
|
+
Object.defineProperty(result, 'imageBytes', {
|
|
128
|
+
value: () => decodeBase64(result.image),
|
|
129
|
+
enumerable: false,
|
|
130
|
+
});
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type FileInput } from '../core/files.js';
|
|
2
|
+
import type { RequestOptions, Transport } from '../core/http.js';
|
|
3
|
+
import type { WithMeta } from '../core/meta.js';
|
|
4
|
+
import type { BankSlipResult, TrueWalletResult } from '../models.js';
|
|
5
|
+
/** Options of every v2 verify call. Options you leave out are not sent. */
|
|
6
|
+
export interface VerifyOptions {
|
|
7
|
+
/** Your own note, 1 to 255 characters. Echoed back as `remark`. */
|
|
8
|
+
remark?: string;
|
|
9
|
+
/** Match the receiver against your registered bank accounts. Adds `matchedAccount`. */
|
|
10
|
+
matchAccount?: boolean;
|
|
11
|
+
/** Expected amount. Adds `amountInOrder` and `isAmountMatched`. */
|
|
12
|
+
matchAmount?: number;
|
|
13
|
+
/** Check whether this branch verified the slip before. Adds `isDuplicate`. */
|
|
14
|
+
checkDuplicate?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export type VerifyCallOptions = VerifyOptions & RequestOptions;
|
|
17
|
+
export declare function verifyFields(options: VerifyOptions | undefined): VerifyOptions;
|
|
18
|
+
/**
|
|
19
|
+
* v2 slip verification (`client.verify`). Every call here is billed: it can spend quota, so
|
|
20
|
+
* the SDK only retries it when the request never reached the API (or on 429).
|
|
21
|
+
*/
|
|
22
|
+
export declare class Verify {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(transport: Transport);
|
|
25
|
+
/** Verifies a bank slip from its QR payload string. */
|
|
26
|
+
bankByPayload(payload: string, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
27
|
+
/** Verifies a bank slip image (JPEG, PNG, GIF or WebP, up to 4 MB). */
|
|
28
|
+
bankByImage(file: FileInput, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
29
|
+
/** Verifies a base64 bank slip image. A `data:image/...;base64,` prefix is fine. */
|
|
30
|
+
bankByBase64(base64: string, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
31
|
+
/** Verifies a bank slip image the API downloads from a public http(s) URL. */
|
|
32
|
+
bankByUrl(url: string, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
33
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
34
|
+
truewalletByImage(file: FileInput, options?: VerifyCallOptions): Promise<WithMeta<TrueWalletResult>>;
|
|
35
|
+
/** Verifies a base64 TrueMoney Wallet slip image. */
|
|
36
|
+
truewalletByBase64(base64: string, options?: VerifyCallOptions): Promise<WithMeta<TrueWalletResult>>;
|
|
37
|
+
/** Verifies a TrueMoney Wallet slip image from a public URL (up to 255 characters). */
|
|
38
|
+
truewalletByUrl(url: string, options?: VerifyCallOptions): Promise<WithMeta<TrueWalletResult>>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { call, defined, imageForm, requestOptions, requireString } from '../core/call.js';
|
|
2
|
+
import { prepareFile } from '../core/files.js';
|
|
3
|
+
const OPTION_KEYS = ['remark', 'matchAccount', 'matchAmount', 'checkDuplicate'];
|
|
4
|
+
export function verifyFields(options) {
|
|
5
|
+
return defined(options, OPTION_KEYS);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* v2 slip verification (`client.verify`). Every call here is billed: it can spend quota, so
|
|
9
|
+
* the SDK only retries it when the request never reached the API (or on 429).
|
|
10
|
+
*/
|
|
11
|
+
export class Verify {
|
|
12
|
+
#t;
|
|
13
|
+
constructor(transport) {
|
|
14
|
+
this.#t = transport;
|
|
15
|
+
}
|
|
16
|
+
/** Verifies a bank slip from its QR payload string. */
|
|
17
|
+
async bankByPayload(payload, options) {
|
|
18
|
+
requireString(payload, 'payload');
|
|
19
|
+
return call(this.#t, 'BankSlipResult', {
|
|
20
|
+
...requestOptions(options),
|
|
21
|
+
api: 'v2',
|
|
22
|
+
method: 'POST',
|
|
23
|
+
path: '/verify/bank',
|
|
24
|
+
json: { payload, ...verifyFields(options) },
|
|
25
|
+
retry: 'billed',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** Verifies a bank slip image (JPEG, PNG, GIF or WebP, up to 4 MB). */
|
|
29
|
+
async bankByImage(file, options) {
|
|
30
|
+
const prepared = await prepareFile(file);
|
|
31
|
+
return call(this.#t, 'BankSlipResult', {
|
|
32
|
+
...requestOptions(options),
|
|
33
|
+
api: 'v2',
|
|
34
|
+
method: 'POST',
|
|
35
|
+
path: '/verify/bank',
|
|
36
|
+
form: imageForm('image', prepared, verifyFields(options)),
|
|
37
|
+
retry: 'billed',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/** Verifies a base64 bank slip image. A `data:image/...;base64,` prefix is fine. */
|
|
41
|
+
async bankByBase64(base64, options) {
|
|
42
|
+
requireString(base64, 'base64');
|
|
43
|
+
return call(this.#t, 'BankSlipResult', {
|
|
44
|
+
...requestOptions(options),
|
|
45
|
+
api: 'v2',
|
|
46
|
+
method: 'POST',
|
|
47
|
+
path: '/verify/bank',
|
|
48
|
+
json: { base64, ...verifyFields(options) },
|
|
49
|
+
retry: 'billed',
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** Verifies a bank slip image the API downloads from a public http(s) URL. */
|
|
53
|
+
async bankByUrl(url, options) {
|
|
54
|
+
requireString(url, 'url');
|
|
55
|
+
return call(this.#t, 'BankSlipResult', {
|
|
56
|
+
...requestOptions(options),
|
|
57
|
+
api: 'v2',
|
|
58
|
+
method: 'POST',
|
|
59
|
+
path: '/verify/bank',
|
|
60
|
+
json: { url, ...verifyFields(options) },
|
|
61
|
+
retry: 'billed',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
65
|
+
async truewalletByImage(file, options) {
|
|
66
|
+
const prepared = await prepareFile(file);
|
|
67
|
+
return call(this.#t, 'TrueWalletResult', {
|
|
68
|
+
...requestOptions(options),
|
|
69
|
+
api: 'v2',
|
|
70
|
+
method: 'POST',
|
|
71
|
+
path: '/verify/truewallet',
|
|
72
|
+
form: imageForm('image', prepared, verifyFields(options)),
|
|
73
|
+
retry: 'billed',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Verifies a base64 TrueMoney Wallet slip image. */
|
|
77
|
+
async truewalletByBase64(base64, options) {
|
|
78
|
+
requireString(base64, 'base64');
|
|
79
|
+
return call(this.#t, 'TrueWalletResult', {
|
|
80
|
+
...requestOptions(options),
|
|
81
|
+
api: 'v2',
|
|
82
|
+
method: 'POST',
|
|
83
|
+
path: '/verify/truewallet',
|
|
84
|
+
json: { base64, ...verifyFields(options) },
|
|
85
|
+
retry: 'billed',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** Verifies a TrueMoney Wallet slip image from a public URL (up to 255 characters). */
|
|
89
|
+
async truewalletByUrl(url, options) {
|
|
90
|
+
requireString(url, 'url');
|
|
91
|
+
return call(this.#t, 'TrueWalletResult', {
|
|
92
|
+
...requestOptions(options),
|
|
93
|
+
api: 'v2',
|
|
94
|
+
method: 'POST',
|
|
95
|
+
path: '/verify/truewallet',
|
|
96
|
+
json: { url, ...verifyFields(options) },
|
|
97
|
+
retry: 'billed',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type WebhookEvent } from './models.js';
|
|
2
|
+
/** Name of the header that carries the webhook signature. */
|
|
3
|
+
export declare const WEBHOOK_SIGNATURE_HEADER: string;
|
|
4
|
+
/** The webhook body exactly as received: a string or the raw bytes. */
|
|
5
|
+
export type WebhookBody = string | Uint8Array | ArrayBuffer;
|
|
6
|
+
/** A header value as web and Node frameworks hand it over. */
|
|
7
|
+
export type SignatureHeader = string | readonly string[] | null | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Checks a webhook signature: `sha256=` plus the lower-case hex HMAC-SHA256 of the raw body
|
|
10
|
+
* under your webhook secret, compared in constant time. Pass the body exactly as received,
|
|
11
|
+
* before any JSON parsing. A missing header gives false.
|
|
12
|
+
*
|
|
13
|
+
* @param secret Your branch's webhook secret. Defaults to the webhook secret env variable.
|
|
14
|
+
*/
|
|
15
|
+
export declare function verifyWebhookSignature(rawBody: WebhookBody, signatureHeader: SignatureHeader, secret?: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Parses a webhook body into a {@link WebhookEvent} without checking the signature. Throws
|
|
18
|
+
* {@link InputError} when the body is not a JSON object with string `jobId`, `status` and
|
|
19
|
+
* `timestamp` fields.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseWebhookEvent(rawBody: WebhookBody): WebhookEvent;
|
|
22
|
+
/**
|
|
23
|
+
* Verifies the signature, then parses the body. Throws {@link WebhookSignatureError} when the
|
|
24
|
+
* signature is missing or wrong, and {@link InputError} when a signed body is not a webhook event.
|
|
25
|
+
*/
|
|
26
|
+
export declare function constructWebhookEvent(rawBody: WebhookBody, signatureHeader: SignatureHeader, secret?: string): WebhookEvent;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { InputError, WebhookSignatureError } from './core/errors.js';
|
|
2
|
+
import { readEnv } from './core/runtime.js';
|
|
3
|
+
import { settings } from './core/settings.js';
|
|
4
|
+
import { hmacSha256, toHex } from './core/sha256.js';
|
|
5
|
+
import { parseModel } from './models.js';
|
|
6
|
+
/** Name of the header that carries the webhook signature. */
|
|
7
|
+
export const WEBHOOK_SIGNATURE_HEADER = settings.webhook.signatureHeader;
|
|
8
|
+
const PREFIX = settings.webhook.signaturePrefix;
|
|
9
|
+
const SIGNATURE = /^[0-9a-f]{64}$/;
|
|
10
|
+
function toBytes(body) {
|
|
11
|
+
if (typeof body === 'string')
|
|
12
|
+
return new TextEncoder().encode(body);
|
|
13
|
+
if (body instanceof ArrayBuffer)
|
|
14
|
+
return new Uint8Array(body);
|
|
15
|
+
if (body instanceof Uint8Array)
|
|
16
|
+
return body;
|
|
17
|
+
throw new InputError('INVALID_ARGUMENT', 'webhook body must be a string or bytes');
|
|
18
|
+
}
|
|
19
|
+
function secretOrEnv(secret) {
|
|
20
|
+
const value = secret ?? readEnv(settings.env.webhookSecret);
|
|
21
|
+
if (!value) {
|
|
22
|
+
throw new InputError('INVALID_ARGUMENT', `no webhook secret: pass one or set ${settings.env.webhookSecret}`);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function equalConstantTime(a, b) {
|
|
27
|
+
if (a.length !== b.length)
|
|
28
|
+
return false;
|
|
29
|
+
let diff = 0;
|
|
30
|
+
for (let i = 0; i < a.length; i++)
|
|
31
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
32
|
+
return diff === 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Checks a webhook signature: `sha256=` plus the lower-case hex HMAC-SHA256 of the raw body
|
|
36
|
+
* under your webhook secret, compared in constant time. Pass the body exactly as received,
|
|
37
|
+
* before any JSON parsing. A missing header gives false.
|
|
38
|
+
*
|
|
39
|
+
* @param secret Your branch's webhook secret. Defaults to the webhook secret env variable.
|
|
40
|
+
*/
|
|
41
|
+
export function verifyWebhookSignature(rawBody, signatureHeader, secret) {
|
|
42
|
+
const key = secretOrEnv(secret);
|
|
43
|
+
const header = Array.isArray(signatureHeader) ? signatureHeader[0] : signatureHeader;
|
|
44
|
+
if (typeof header !== 'string' || !header.startsWith(PREFIX))
|
|
45
|
+
return false;
|
|
46
|
+
const given = header.slice(PREFIX.length);
|
|
47
|
+
if (!SIGNATURE.test(given))
|
|
48
|
+
return false;
|
|
49
|
+
const expected = toHex(hmacSha256(new TextEncoder().encode(key), toBytes(rawBody)));
|
|
50
|
+
return equalConstantTime(given, expected);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Parses a webhook body into a {@link WebhookEvent} without checking the signature. Throws
|
|
54
|
+
* {@link InputError} when the body is not a JSON object with string `jobId`, `status` and
|
|
55
|
+
* `timestamp` fields.
|
|
56
|
+
*/
|
|
57
|
+
export function parseWebhookEvent(rawBody) {
|
|
58
|
+
const text = typeof rawBody === 'string' ? rawBody : new TextDecoder().decode(toBytes(rawBody));
|
|
59
|
+
let value;
|
|
60
|
+
try {
|
|
61
|
+
value = JSON.parse(text);
|
|
62
|
+
}
|
|
63
|
+
catch (cause) {
|
|
64
|
+
throw new InputError('INVALID_ARGUMENT', 'webhook body is not JSON', { cause });
|
|
65
|
+
}
|
|
66
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
67
|
+
throw new InputError('INVALID_ARGUMENT', 'webhook body is not a JSON object');
|
|
68
|
+
}
|
|
69
|
+
const fields = value;
|
|
70
|
+
if (typeof fields.jobId !== 'string' ||
|
|
71
|
+
typeof fields.status !== 'string' ||
|
|
72
|
+
typeof fields.timestamp !== 'string') {
|
|
73
|
+
throw new InputError('INVALID_ARGUMENT', 'webhook body must contain jobId, status and timestamp');
|
|
74
|
+
}
|
|
75
|
+
return parseModel('WebhookEvent', value);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Verifies the signature, then parses the body. Throws {@link WebhookSignatureError} when the
|
|
79
|
+
* signature is missing or wrong, and {@link InputError} when a signed body is not a webhook event.
|
|
80
|
+
*/
|
|
81
|
+
export function constructWebhookEvent(rawBody, signatureHeader, secret) {
|
|
82
|
+
if (!verifyWebhookSignature(rawBody, signatureHeader, secret)) {
|
|
83
|
+
throw new WebhookSignatureError(`webhook signature in ${WEBHOOK_SIGNATURE_HEADER} is missing or does not match`);
|
|
84
|
+
}
|
|
85
|
+
return parseWebhookEvent(rawBody);
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,81 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@easyslip/sdk",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Official EasySlip SDK for JavaScript and TypeScript: verify Thai bank and TrueMoney Wallet slips, generate payment QR codes and check webhook signatures.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"easyslip",
|
|
7
|
+
"slip",
|
|
8
|
+
"slip-verification",
|
|
9
|
+
"promptpay",
|
|
10
|
+
"truemoney",
|
|
11
|
+
"thai-bank",
|
|
12
|
+
"qr-code",
|
|
13
|
+
"payment",
|
|
14
|
+
"sdk",
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/EASYSLIP-CO-LTD/sdk-js#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/EASYSLIP-CO-LTD/sdk-js/issues"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/EASYSLIP-CO-LTD/sdk-js.git"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "EasySlip Co., Ltd.",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "./dist/cjs/index.js",
|
|
29
|
+
"module": "./dist/esm/index.js",
|
|
30
|
+
"types": "./dist/cjs/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/esm/index.d.ts",
|
|
35
|
+
"default": "./dist/esm/index.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/cjs/index.d.ts",
|
|
39
|
+
"default": "./dist/cjs/index.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"./package.json": "./package.json"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"README.th.md",
|
|
47
|
+
"CHANGELOG.md"
|
|
48
|
+
],
|
|
49
|
+
"sideEffects": false,
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=22"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"provenance": true
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
|
|
59
|
+
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
|
60
|
+
"build:esm": "tsc -p tsconfig.esm.json && node -e \"fs.writeFileSync('dist/esm/package.json', JSON.stringify({ type: 'module' }))\"",
|
|
61
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && node -e \"fs.writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))\"",
|
|
62
|
+
"lint": "biome ci .",
|
|
63
|
+
"format": "biome format --write .",
|
|
64
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:coverage": "vitest run --coverage",
|
|
67
|
+
"test:integration": "vitest run --config vitest.integration.config.ts",
|
|
68
|
+
"check:package": "npm pack --dry-run && publint run --strict && attw --pack .",
|
|
69
|
+
"prepack": "npm run build"
|
|
70
|
+
},
|
|
71
|
+
"devDependencies": {
|
|
72
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
73
|
+
"@biomejs/biome": "2.6.4",
|
|
74
|
+
"@types/node": "26.6.6",
|
|
75
|
+
"@vitest/coverage-v8": "5.0.5",
|
|
76
|
+
"publint": "0.3.25",
|
|
77
|
+
"typescript": "7.0.2",
|
|
78
|
+
"vite": "8.3.5",
|
|
79
|
+
"vitest": "5.0.5"
|
|
80
|
+
}
|
|
81
|
+
}
|