@zendfi/sdk 0.1.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/README.md +389 -0
- package/dist/index.d.mts +611 -0
- package/dist/index.d.ts +611 -0
- package/dist/index.js +680 -0
- package/dist/index.mjs +640 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
# @zendfi/sdk
|
|
2
|
+
|
|
3
|
+
> Zero-config TypeScript SDK for ZendFi crypto payments
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@zendfi/sdk)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Zero Configuration** - Works out of the box, auto-detects environment
|
|
11
|
+
- **Type-Safe** - Full TypeScript support with complete type definitions
|
|
12
|
+
- **Auto-Retry** - Built-in retry logic with exponential backoff
|
|
13
|
+
- **Idempotency** - Automatic idempotency key generation
|
|
14
|
+
- **Environment Detection** - Automatically switches between test/production
|
|
15
|
+
- **Smart Defaults** - Sensible defaults for all options
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @zendfi/sdk
|
|
21
|
+
# or
|
|
22
|
+
yarn add @zendfi/sdk
|
|
23
|
+
# or
|
|
24
|
+
pnpm add @zendfi/sdk
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### 1. Set your API key
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# .env.local
|
|
33
|
+
ZENDFI_API_KEY=zfi_test_your_api_key_here
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Create a payment
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { zendfi } from '@zendfi/sdk';
|
|
40
|
+
|
|
41
|
+
const payment = await zendfi.createPayment({
|
|
42
|
+
amount: 50,
|
|
43
|
+
description: 'Premium subscription',
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
console.log(payment.checkout_url); // Send this to your customer
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
That's it! The SDK handles everything else automatically. 🎉
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
### Payments
|
|
54
|
+
|
|
55
|
+
#### Create a Payment
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const payment = await zendfi.createPayment({
|
|
59
|
+
amount: 99.99,
|
|
60
|
+
currency: 'USD', // Optional, defaults to 'USD'
|
|
61
|
+
token: 'USDC', // Optional, defaults to 'USDC'
|
|
62
|
+
description: 'Annual subscription',
|
|
63
|
+
customer_email: 'customer@example.com',
|
|
64
|
+
redirect_url: 'https://yourapp.com/success',
|
|
65
|
+
metadata: {
|
|
66
|
+
orderId: 'ORD-123',
|
|
67
|
+
customerId: 'CUST-456',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Redirect customer to checkout
|
|
72
|
+
window.location.href = payment.checkout_url;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### Get Payment Status
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
const payment = await zendfi.getPayment('payment_id');
|
|
79
|
+
|
|
80
|
+
console.log(payment.status); // 'pending' | 'confirmed' | 'failed' | 'expired'
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### List Payments
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const payments = await zendfi.listPayments({
|
|
87
|
+
page: 1,
|
|
88
|
+
limit: 10,
|
|
89
|
+
status: 'confirmed',
|
|
90
|
+
from_date: '2025-01-01',
|
|
91
|
+
to_date: '2025-12-31',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log(payments.data); // Array of payments
|
|
95
|
+
console.log(payments.pagination); // Pagination info
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Subscriptions
|
|
99
|
+
|
|
100
|
+
#### Create a Plan
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const plan = await zendfi.createSubscriptionPlan({
|
|
104
|
+
name: 'Pro Plan',
|
|
105
|
+
description: 'Access to all premium features',
|
|
106
|
+
amount: 29.99,
|
|
107
|
+
interval: 'monthly',
|
|
108
|
+
trial_days: 14,
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### Create a Subscription
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const subscription = await zendfi.createSubscription({
|
|
116
|
+
plan_id: plan.id,
|
|
117
|
+
customer_email: 'customer@example.com',
|
|
118
|
+
metadata: {
|
|
119
|
+
userId: 'user_123',
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### Cancel a Subscription
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
const canceled = await zendfi.cancelSubscription(subscription.id);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Webhooks
|
|
131
|
+
|
|
132
|
+
#### Verify Webhook Signature
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { zendfi } from '@zendfi/sdk';
|
|
136
|
+
|
|
137
|
+
// In your webhook handler (e.g., /api/webhooks/zendfi)
|
|
138
|
+
export async function POST(request: Request) {
|
|
139
|
+
const payload = await request.text();
|
|
140
|
+
const signature = request.headers.get('x-zendfi-signature');
|
|
141
|
+
|
|
142
|
+
const isValid = zendfi.verifyWebhook({
|
|
143
|
+
payload,
|
|
144
|
+
signature,
|
|
145
|
+
secret: process.env.ZENDFI_WEBHOOK_SECRET!,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (!isValid) {
|
|
149
|
+
return new Response('Invalid signature', { status: 401 });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const event = JSON.parse(payload);
|
|
153
|
+
|
|
154
|
+
switch (event.event) {
|
|
155
|
+
case 'payment.confirmed':
|
|
156
|
+
// Handle payment confirmation
|
|
157
|
+
break;
|
|
158
|
+
case 'subscription.activated':
|
|
159
|
+
// Handle subscription activation
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return new Response('OK', { status: 200 });
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Configuration
|
|
168
|
+
|
|
169
|
+
### Environment Variables
|
|
170
|
+
|
|
171
|
+
The SDK automatically detects and uses these environment variables:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
# API Key (required)
|
|
175
|
+
ZENDFI_API_KEY=zfi_test_...
|
|
176
|
+
|
|
177
|
+
# Or for Next.js
|
|
178
|
+
NEXT_PUBLIC_ZENDFI_API_KEY=zfi_test_...
|
|
179
|
+
|
|
180
|
+
# Or for Create React App
|
|
181
|
+
REACT_APP_ZENDFI_API_KEY=zfi_test_...
|
|
182
|
+
|
|
183
|
+
# Environment (optional, auto-detected)
|
|
184
|
+
ZENDFI_ENVIRONMENT=development # or staging, production
|
|
185
|
+
|
|
186
|
+
# Custom API URL (optional)
|
|
187
|
+
ZENDFI_API_URL=https://api.zendfi.tech
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Manual Configuration
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
import { ZendFiClient } from '@zendfi/sdk';
|
|
194
|
+
|
|
195
|
+
const client = new ZendFiClient({
|
|
196
|
+
apiKey: 'zfi_test_...',
|
|
197
|
+
environment: 'development',
|
|
198
|
+
timeout: 30000, // 30 seconds
|
|
199
|
+
retries: 3,
|
|
200
|
+
idempotencyEnabled: true,
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Advanced Features
|
|
205
|
+
|
|
206
|
+
### Auto Environment Detection
|
|
207
|
+
|
|
208
|
+
The SDK automatically detects your environment:
|
|
209
|
+
|
|
210
|
+
| Environment | Detected When |
|
|
211
|
+
| ----------- | -------------------------------------- |
|
|
212
|
+
| Development | `localhost`, `127.0.0.1`, `NODE_ENV=development` |
|
|
213
|
+
| Staging | `*.staging.*`, `*.vercel.app`, `NODE_ENV=staging` |
|
|
214
|
+
| Production | `NODE_ENV=production`, production domains |
|
|
215
|
+
|
|
216
|
+
### Automatic Retries
|
|
217
|
+
|
|
218
|
+
The SDK retries failed requests automatically:
|
|
219
|
+
|
|
220
|
+
- **Server errors (5xx)**: Retries up to 3 times with exponential backoff
|
|
221
|
+
- **Network errors**: Retries up to 3 times
|
|
222
|
+
- **Client errors (4xx)**: No retry (fix your request)
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// This will retry automatically on network errors
|
|
226
|
+
const payment = await zendfi.createPayment({ amount: 50 });
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Idempotency Keys
|
|
230
|
+
|
|
231
|
+
Prevent duplicate payments with automatic idempotency:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
// SDK automatically adds: Idempotency-Key: zfi_idem_1234567890_abc123
|
|
235
|
+
|
|
236
|
+
const payment = await zendfi.createPayment({
|
|
237
|
+
amount: 50,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Safe to retry - won't create duplicate payments
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## 🎯 TypeScript Support
|
|
244
|
+
|
|
245
|
+
Full type definitions included:
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import type {
|
|
249
|
+
Payment,
|
|
250
|
+
PaymentStatus,
|
|
251
|
+
Subscription,
|
|
252
|
+
SubscriptionPlan,
|
|
253
|
+
WebhookEvent,
|
|
254
|
+
} from '@zendfi/sdk';
|
|
255
|
+
|
|
256
|
+
const payment: Payment = await zendfi.createPayment({
|
|
257
|
+
amount: 50,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// IntelliSense for all fields
|
|
261
|
+
console.log(payment.id);
|
|
262
|
+
console.log(payment.status);
|
|
263
|
+
console.log(payment.checkout_url);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Error Handling
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
import { AuthenticationError, ValidationError, NetworkError } from '@zendfi/sdk';
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
const payment = await zendfi.createPayment({
|
|
273
|
+
amount: 50,
|
|
274
|
+
});
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error instanceof AuthenticationError) {
|
|
277
|
+
console.error('Invalid API key');
|
|
278
|
+
} else if (error instanceof ValidationError) {
|
|
279
|
+
console.error('Invalid request:', error.details);
|
|
280
|
+
} else if (error instanceof NetworkError) {
|
|
281
|
+
console.error('Network error, will retry automatically');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Framework Examples
|
|
287
|
+
|
|
288
|
+
### Next.js App Router
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
// app/api/checkout/route.ts
|
|
292
|
+
import { zendfi } from '@zendfi/sdk';
|
|
293
|
+
import { NextResponse } from 'next/server';
|
|
294
|
+
|
|
295
|
+
export async function POST(request: Request) {
|
|
296
|
+
const { amount } = await request.json();
|
|
297
|
+
|
|
298
|
+
const payment = await zendfi.createPayment({
|
|
299
|
+
amount,
|
|
300
|
+
redirect_url: `${process.env.NEXT_PUBLIC_URL}/success`,
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
return NextResponse.json({ url: payment.checkout_url });
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Express.js
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import express from 'express';
|
|
311
|
+
import { zendfi } from '@zendfi/sdk';
|
|
312
|
+
|
|
313
|
+
const app = express();
|
|
314
|
+
|
|
315
|
+
app.post('/api/checkout', async (req, res) => {
|
|
316
|
+
const { amount } = req.body;
|
|
317
|
+
|
|
318
|
+
const payment = await zendfi.createPayment({
|
|
319
|
+
amount,
|
|
320
|
+
redirect_url: 'https://yourapp.com/success',
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
res.json({ url: payment.checkout_url });
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### React
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
import { useState } from 'react';
|
|
331
|
+
import { zendfi } from '@zendfi/sdk';
|
|
332
|
+
|
|
333
|
+
function CheckoutButton() {
|
|
334
|
+
const [loading, setLoading] = useState(false);
|
|
335
|
+
|
|
336
|
+
const handleCheckout = async () => {
|
|
337
|
+
setLoading(true);
|
|
338
|
+
|
|
339
|
+
const payment = await zendfi.createPayment({
|
|
340
|
+
amount: 50,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
window.location.href = payment.checkout_url;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
return (
|
|
347
|
+
<button onClick={handleCheckout} disabled={loading}>
|
|
348
|
+
{loading ? 'Creating checkout...' : 'Pay with Crypto'}
|
|
349
|
+
</button>
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## API Reference
|
|
355
|
+
|
|
356
|
+
### Methods
|
|
357
|
+
|
|
358
|
+
| Method | Description |
|
|
359
|
+
| --------------------------- | -------------------------- |
|
|
360
|
+
| `createPayment()` | Create a new payment |
|
|
361
|
+
| `getPayment(id)` | Get payment by ID |
|
|
362
|
+
| `listPayments(options)` | List all payments |
|
|
363
|
+
| `createSubscriptionPlan()` | Create subscription plan |
|
|
364
|
+
| `getSubscriptionPlan(id)` | Get plan by ID |
|
|
365
|
+
| `createSubscription()` | Create a subscription |
|
|
366
|
+
| `getSubscription(id)` | Get subscription by ID |
|
|
367
|
+
| `cancelSubscription(id)` | Cancel a subscription |
|
|
368
|
+
| `verifyWebhook()` | Verify webhook signature |
|
|
369
|
+
|
|
370
|
+
See [full API documentation](https://docs.zendfi.tech/sdk) for detailed reference.
|
|
371
|
+
|
|
372
|
+
## Debugging
|
|
373
|
+
|
|
374
|
+
Enable debug logs:
|
|
375
|
+
|
|
376
|
+
```bash
|
|
377
|
+
DEBUG=zendfi:* node your-app.js
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Support
|
|
381
|
+
|
|
382
|
+
- [Documentation](https://docs.zendfi.tech)
|
|
383
|
+
- [API Reference](https://docs.zendfi.tech/api)
|
|
384
|
+
- [GitHub Issues](https://github.com/zendfi/zendfi-toolkit/issues)
|
|
385
|
+
- Email: dev@zendfi.tech
|
|
386
|
+
|
|
387
|
+
## License
|
|
388
|
+
|
|
389
|
+
MIT © ZendFi
|