@imerchantsolutions/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/README.md +204 -0
- package/dist/index.d.mts +749 -0
- package/dist/index.d.ts +749 -0
- package/dist/index.js +745 -0
- package/dist/index.mjs +706 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# @imerchantsolutions/sdk
|
|
2
|
+
|
|
3
|
+
Official JavaScript/TypeScript SDK for the iMerchant Payments API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @imerchantsolutions/sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @imerchantsolutions/sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @imerchantsolutions/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { iMerchant } from '@imerchantsolutions/sdk';
|
|
19
|
+
|
|
20
|
+
const client = new iMerchant({
|
|
21
|
+
apiKey: 'sk_test_your_api_key'
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Create a payment
|
|
25
|
+
const payment = await client.payments.create({
|
|
26
|
+
amount: 1000, // $10.00 in cents
|
|
27
|
+
currency: 'usd',
|
|
28
|
+
reference: 'order_123',
|
|
29
|
+
description: 'Test payment'
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log('Payment ID:', payment.id);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Full TypeScript support** with comprehensive type definitions
|
|
38
|
+
- **Payments** - Create, capture, cancel, and list payments
|
|
39
|
+
- **Refunds** - Process full or partial refunds
|
|
40
|
+
- **Customers** - Manage customer profiles
|
|
41
|
+
- **Subscriptions** - Handle recurring billing
|
|
42
|
+
- **Webhooks** - Verify webhook signatures securely
|
|
43
|
+
|
|
44
|
+
## API Reference
|
|
45
|
+
|
|
46
|
+
### Payments
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Create a payment
|
|
50
|
+
const payment = await client.payments.create({
|
|
51
|
+
amount: 2000,
|
|
52
|
+
currency: 'usd',
|
|
53
|
+
reference: 'order_456',
|
|
54
|
+
customerId: 'cus_abc123',
|
|
55
|
+
returnUrl: 'https://yoursite.com/complete'
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Retrieve a payment
|
|
59
|
+
const payment = await client.payments.retrieve('pay_abc123');
|
|
60
|
+
|
|
61
|
+
// List payments
|
|
62
|
+
const { data, hasMore } = await client.payments.list({
|
|
63
|
+
status: 'captured',
|
|
64
|
+
limit: 10
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Capture an authorized payment
|
|
68
|
+
await client.payments.capture('pay_abc123');
|
|
69
|
+
|
|
70
|
+
// Cancel a payment
|
|
71
|
+
await client.payments.cancel('pay_abc123');
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Refunds
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// Create a full refund
|
|
78
|
+
const refund = await client.refunds.create({
|
|
79
|
+
paymentId: 'pay_abc123',
|
|
80
|
+
reason: 'Customer request'
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Create a partial refund
|
|
84
|
+
const refund = await client.refunds.create({
|
|
85
|
+
paymentId: 'pay_abc123',
|
|
86
|
+
amount: 500, // $5.00 in cents
|
|
87
|
+
reason: 'Partial return'
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// List refunds
|
|
91
|
+
const { data } = await client.refunds.list({
|
|
92
|
+
paymentId: 'pay_abc123'
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Customers
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// Create a customer
|
|
100
|
+
const customer = await client.customers.create({
|
|
101
|
+
email: 'john@example.com',
|
|
102
|
+
firstName: 'John',
|
|
103
|
+
lastName: 'Doe'
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Update a customer
|
|
107
|
+
const customer = await client.customers.update('cus_abc123', {
|
|
108
|
+
phone: '+1234567890'
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// List customers
|
|
112
|
+
const { data } = await client.customers.list({ limit: 20 });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Subscriptions
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// Create a subscription
|
|
119
|
+
const subscription = await client.subscriptions.create({
|
|
120
|
+
customerId: 'cus_abc123',
|
|
121
|
+
amount: 2999,
|
|
122
|
+
currency: 'usd',
|
|
123
|
+
interval: 'month',
|
|
124
|
+
trialDays: 14
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Cancel a subscription
|
|
128
|
+
await client.subscriptions.cancel('sub_abc123', {
|
|
129
|
+
atPeriodEnd: true
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Pause/Resume
|
|
133
|
+
await client.subscriptions.pause('sub_abc123');
|
|
134
|
+
await client.subscriptions.resume('sub_abc123');
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Webhooks
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { iMerchant } from '@imerchantsolutions/sdk';
|
|
141
|
+
|
|
142
|
+
// Express.js example
|
|
143
|
+
app.post('/webhooks', express.raw({ type: 'application/json' }), (req, res) => {
|
|
144
|
+
const signature = req.headers['imerchant-signature'];
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const event = iMerchant.webhooks.verify(
|
|
148
|
+
{ body: req.body.toString(), signature },
|
|
149
|
+
process.env.WEBHOOK_SECRET
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
switch (event.type) {
|
|
153
|
+
case 'payment.captured':
|
|
154
|
+
// Handle successful payment
|
|
155
|
+
break;
|
|
156
|
+
case 'payment.failed':
|
|
157
|
+
// Handle failed payment
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
res.json({ received: true });
|
|
162
|
+
} catch (err) {
|
|
163
|
+
res.status(400).send('Invalid signature');
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Error Handling
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import {
|
|
172
|
+
iMerchant,
|
|
173
|
+
APIError,
|
|
174
|
+
AuthenticationError,
|
|
175
|
+
ValidationError
|
|
176
|
+
} from '@imerchantsolutions/sdk';
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
await client.payments.create({ ... });
|
|
180
|
+
} catch (error) {
|
|
181
|
+
if (error instanceof AuthenticationError) {
|
|
182
|
+
console.error('Invalid API key');
|
|
183
|
+
} else if (error instanceof ValidationError) {
|
|
184
|
+
console.error('Validation errors:', error.errors);
|
|
185
|
+
} else if (error instanceof APIError) {
|
|
186
|
+
console.error(`API Error: ${error.code} - ${error.message}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Configuration
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
const client = new iMerchant({
|
|
195
|
+
apiKey: 'sk_test_...', // Required
|
|
196
|
+
baseUrl: 'https://...', // Optional: custom API URL
|
|
197
|
+
timeout: 30000, // Optional: request timeout in ms
|
|
198
|
+
apiVersion: 'v1' // Optional: API version
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT
|