paysafe-sdk 1.0.0
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 +13 -0
- package/LICENSE +21 -0
- package/README.md +170 -0
- package/dist/index.cjs +1572 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1211 -0
- package/dist/index.d.ts +1211 -0
- package/dist/index.js +1548 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
Initial release.
|
|
6
|
+
|
|
7
|
+
- Payments API: Payment Handles, Payments, Settlements, Refunds, Payouts (standalone & original credits), Verifications.
|
|
8
|
+
- Customer Vault: profiles, saved payment handles, single-use customer tokens.
|
|
9
|
+
- Payment Scheduler: Plans, Subscriptions.
|
|
10
|
+
- Applications (merchant onboarding): create, update, submit, terms & conditions, document upload.
|
|
11
|
+
- Value Added Services: FX Rates, Customer Identity (KYC), Bank Account Validation, Interac Verification.
|
|
12
|
+
- Webhook HMAC-SHA256 verification and event parsing, portable across Node.js and Web Crypto runtimes.
|
|
13
|
+
- Zero runtime dependencies; dual ESM/CJS build with full type declarations.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanishka Naik
|
|
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
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# paysafe-sdk
|
|
2
|
+
|
|
3
|
+
Production-grade TypeScript SDK for the [Paysafe](https://www.paysafe.com) API.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/paysafe-sdk)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
## Coverage
|
|
9
|
+
|
|
10
|
+
| Bounded context | Resources |
|
|
11
|
+
|---|---|
|
|
12
|
+
| **Payments** | Payment Handles, Payments, Settlements, Refunds, Payouts (standalone & original credits), Verifications |
|
|
13
|
+
| **Customer Vault** | Customer profiles, saved payment handles, single-use customer tokens |
|
|
14
|
+
| **Payment Scheduler** | Plans, Subscriptions (create, update, cancel, suspend, reactivate) |
|
|
15
|
+
| **Applications** | Merchant onboarding: create, update, submit, terms & conditions, document upload |
|
|
16
|
+
| **Value Added Services** | FX Rates, Customer Identity (KYC), Bank Account Validation, Interac Verification (VerifiedMe) |
|
|
17
|
+
| **Webhooks** | HMAC-SHA256 signature verification (constant-time) and event parsing |
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install paysafe-sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Requires Node.js **>= 18**. Also runs on any runtime with `fetch` and either `node:crypto` or Web Crypto (`SubtleCrypto`) — browsers, Deno, Cloudflare Workers, and other edge runtimes.
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { PaysafeClient } from "paysafe-sdk";
|
|
31
|
+
|
|
32
|
+
const client = new PaysafeClient({
|
|
33
|
+
username: process.env.PAYSAFE_USERNAME!,
|
|
34
|
+
password: process.env.PAYSAFE_PASSWORD!,
|
|
35
|
+
environment: "test", // or "production"
|
|
36
|
+
accountId: process.env.PAYSAFE_ACCOUNT_ID!,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// 1. Tokenize a card into a Payment Handle.
|
|
40
|
+
const handle = await client.paymentHandles.create({
|
|
41
|
+
merchantRefNum: "order-1",
|
|
42
|
+
amount: 5000, // minor currency units — $50.00
|
|
43
|
+
currencyCode: "USD",
|
|
44
|
+
paymentType: "CARD",
|
|
45
|
+
transactionType: "PAYMENT",
|
|
46
|
+
card: {
|
|
47
|
+
cardNum: "4111111111111111",
|
|
48
|
+
cardExpiry: { month: 12, year: 2030 },
|
|
49
|
+
cvv: "123",
|
|
50
|
+
holderName: "Jane Doe",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// 2. Charge it.
|
|
55
|
+
const payment = await client.payments.create({
|
|
56
|
+
merchantRefNum: "order-1",
|
|
57
|
+
amount: 5000,
|
|
58
|
+
currencyCode: "USD",
|
|
59
|
+
paymentHandleToken: handle.paymentHandleToken,
|
|
60
|
+
settleWithAuth: true,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log(payment.status); // "COMPLETED"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Or build a client from `PAYSAFE_USERNAME` / `PAYSAFE_PASSWORD` / `PAYSAFE_ENVIRONMENT` / `PAYSAFE_ACCOUNT_ID` environment variables:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const client = PaysafeClient.fromEnv();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
See [`examples/`](./examples) for a full payment flow, a subscription signup flow, and a webhook receiver.
|
|
73
|
+
|
|
74
|
+
## Error handling
|
|
75
|
+
|
|
76
|
+
Every rejected promise from this SDK rejects with a `PaysafeError` — never a bare string or plain object:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { PaysafeError } from "paysafe-sdk";
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
await client.payments.create(req);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (err instanceof PaysafeError) {
|
|
85
|
+
console.error(err.kind); // "api_error" | "http_error" | "rate_limited" | "timeout" | ...
|
|
86
|
+
console.error(err.code); // Paysafe error code, e.g. "5068"
|
|
87
|
+
console.error(err.httpStatus); // e.g. 400
|
|
88
|
+
console.error(err.fieldErrors); // [{ field: "card.cardNum", error: "Invalid card number" }]
|
|
89
|
+
console.error(err.retryable); // whether the SDK's own retry policy would retry this
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Webhooks
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { WebhooksResource, webhookTopic, PaysafeError } from "paysafe-sdk";
|
|
98
|
+
|
|
99
|
+
const webhooks = new WebhooksResource();
|
|
100
|
+
|
|
101
|
+
// In your HTTP handler, using the *raw* request body:
|
|
102
|
+
try {
|
|
103
|
+
const event = await webhooks.verifyAndParse(rawBody, req.headers["signature"], hmacKey);
|
|
104
|
+
switch (webhookTopic(event)) {
|
|
105
|
+
case "payment_handle":
|
|
106
|
+
// ...
|
|
107
|
+
break;
|
|
108
|
+
// ...
|
|
109
|
+
}
|
|
110
|
+
res.status(200).send("OK");
|
|
111
|
+
} catch (err) {
|
|
112
|
+
if (err instanceof PaysafeError && err.kind === "webhook_signature_mismatch") {
|
|
113
|
+
res.status(401).send("Invalid signature");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Signature verification uses a constant-time comparison and works identically on Node.js (`node:crypto`) and Web Crypto runtimes (browsers, Deno, Cloudflare Workers).
|
|
119
|
+
|
|
120
|
+
## Configuration reference
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
new PaysafeClient({
|
|
124
|
+
username: string, // required
|
|
125
|
+
password: string, // required
|
|
126
|
+
environment?: "test" | "production", // default: "test"
|
|
127
|
+
accountId?: string, // default account ID for resources that need one
|
|
128
|
+
baseUrlOverride?: string, // override the base URL entirely (e.g. for a mock server in tests)
|
|
129
|
+
timeoutMs?: number, // per-request timeout, default 30_000
|
|
130
|
+
maxRetries?: number, // default 3
|
|
131
|
+
retryBaseDelayMs?: number, // exponential backoff base delay, default 500
|
|
132
|
+
rateLimit?: { limit: number; windowMs: number }, // local token bucket, default 100 req / 1000ms
|
|
133
|
+
fetch?: (url, init) => Promise<Response>, // custom fetch implementation
|
|
134
|
+
telemetryPrefix?: string, // metric/event name prefix, default "paysafe"
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Pass a `TelemetryHook` as the second constructor argument to observe every request:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const client = new PaysafeClient(options, {
|
|
142
|
+
onStart: (meta) => metrics.increment(`${meta.prefix}.${meta.api}.start`),
|
|
143
|
+
onStop: (meta) => metrics.timing(`${meta.prefix}.${meta.api}.duration`, meta.durationMs),
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Design principles
|
|
148
|
+
|
|
149
|
+
- **Zero runtime dependencies.** Built entirely on platform `fetch` and `crypto` APIs.
|
|
150
|
+
- **Dual ESM/CJS build** with full TypeScript declarations, via `tsup`.
|
|
151
|
+
- **Retry with exponential backoff + jitter** on transient failures (5xx, 429, timeouts, and a documented set of Paysafe API error codes).
|
|
152
|
+
- **Token-bucket rate limiting** per credential, enforced client-side before any network call.
|
|
153
|
+
- **Structured errors.** Every failure mode maps to a typed `PaysafeError.kind`.
|
|
154
|
+
- **Strict TypeScript**: `strict`, `noUncheckedIndexedAccess`, and friends are all on.
|
|
155
|
+
- Every resource method accepts an optional `AbortSignal` for cancellation, composed with the client's own timeout.
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
npm install
|
|
161
|
+
npm run build # tsup -> dist/ (ESM + CJS + .d.ts)
|
|
162
|
+
npm run test # vitest
|
|
163
|
+
npm run lint # eslint
|
|
164
|
+
npm run typecheck # tsc --noEmit
|
|
165
|
+
npm run format # prettier --write
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|