@caspay/sdk 1.0.4 → 1.1.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 +277 -53
- package/dist/browser.d.ts +0 -15
- package/dist/caspay.min.js +2 -1
- package/dist/core/client.d.ts +1 -5
- package/dist/core/transfer.d.ts +8 -0
- package/dist/index.d.ts +7 -28
- package/dist/index.esm.js +460 -62
- package/dist/index.js +460 -62
- package/dist/resources/payments.d.ts +9 -23
- package/dist/resources/subscriptions.d.ts +7 -0
- package/dist/resources/wallet.d.ts +21 -0
- package/dist/types/index.d.ts +57 -31
- package/dist/version.d.ts +1 -0
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -5,7 +5,17 @@ Official JavaScript/TypeScript SDK for CasPay - Accept crypto payments with Casp
|
|
|
5
5
|
[](https://www.npmjs.com/package/@caspay/sdk)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
|
-
## 🚀
|
|
8
|
+
## 🚀 Features
|
|
9
|
+
|
|
10
|
+
- **Two Integration Modes:**
|
|
11
|
+
- **Full Management**: CasPay handles wallet connection, transfer & recording
|
|
12
|
+
- **Tracking Only**: Merchant handles payment, CasPay tracks analytics
|
|
13
|
+
- **Casper Wallet Integration**: Seamless wallet connection and payments
|
|
14
|
+
- **Subscription Management**: Recurring payments with automatic tracking
|
|
15
|
+
- **TypeScript Support**: Full type definitions included
|
|
16
|
+
- **Framework Agnostic**: Works with React, Next.js, Vue, Vanilla JS, PHP
|
|
17
|
+
|
|
18
|
+
## 📦 Installation
|
|
9
19
|
|
|
10
20
|
### NPM / Yarn (React, Next.js, Node.js)
|
|
11
21
|
|
|
@@ -15,95 +25,217 @@ npm install @caspay/sdk
|
|
|
15
25
|
yarn add @caspay/sdk
|
|
16
26
|
```
|
|
17
27
|
|
|
18
|
-
### CDN (
|
|
28
|
+
### CDN (PHP, HTML)
|
|
19
29
|
|
|
20
30
|
```html
|
|
21
|
-
<script src="https://cdn.jsdelivr.net/npm/@caspay/sdk@1.0
|
|
31
|
+
<script src="https://cdn.jsdelivr.net/npm/@caspay/sdk@1.1.0/dist/caspay.min.js"></script>
|
|
22
32
|
```
|
|
23
33
|
|
|
24
|
-
|
|
34
|
+
> **Note**: Users only need to install **Casper Wallet browser extension**. No additional dependencies required.
|
|
35
|
+
|
|
36
|
+
## 📚 Quick Start
|
|
37
|
+
|
|
38
|
+
### Mode 1: Full Management (CasPay Handles Everything)
|
|
25
39
|
|
|
26
|
-
|
|
40
|
+
CasPay SDK manages wallet connection, blockchain transfer, and payment recording.
|
|
41
|
+
|
|
42
|
+
#### React / Next.js
|
|
27
43
|
|
|
28
44
|
```typescript
|
|
29
45
|
import CasPay from '@caspay/sdk';
|
|
30
46
|
|
|
31
47
|
const caspay = new CasPay({
|
|
32
48
|
apiKey: 'cp_live_...',
|
|
33
|
-
merchantId: 'MERCH_...'
|
|
49
|
+
merchantId: 'MERCH_...',
|
|
50
|
+
walletAddress: '01ab...', // Your wallet to receive payments
|
|
51
|
+
network: 'testnet'
|
|
34
52
|
});
|
|
35
53
|
|
|
36
|
-
//
|
|
37
|
-
const
|
|
38
|
-
senderAddress: '0x123...',
|
|
54
|
+
// One-time payment
|
|
55
|
+
const result = await caspay.payments.makePayment({
|
|
39
56
|
productId: 'prod_abc123',
|
|
40
|
-
amount:
|
|
41
|
-
currency: 'USD'
|
|
57
|
+
amount: 10.5, // CSPR
|
|
42
58
|
});
|
|
43
59
|
|
|
44
|
-
|
|
60
|
+
// Subscription payment
|
|
61
|
+
const subResult = await caspay.payments.makePayment({
|
|
62
|
+
subscriptionPlanId: 'plan_xyz789',
|
|
63
|
+
amount: 29.99
|
|
64
|
+
});
|
|
45
65
|
```
|
|
46
66
|
|
|
47
|
-
|
|
67
|
+
#### PHP / Vanilla JS
|
|
48
68
|
|
|
49
69
|
```html
|
|
50
|
-
<script src="https://cdn.jsdelivr.net/npm/@caspay/sdk@1.0
|
|
70
|
+
<script src="https://cdn.jsdelivr.net/npm/@caspay/sdk@1.1.0/dist/caspay.min.js"></script>
|
|
51
71
|
|
|
52
|
-
<button id="payBtn">Pay
|
|
72
|
+
<button id="payBtn">Pay 10 CSPR</button>
|
|
53
73
|
|
|
54
74
|
<script>
|
|
55
75
|
const caspay = new CasPay({
|
|
56
76
|
apiKey: 'cp_live_...',
|
|
57
|
-
merchantId: 'MERCH_...'
|
|
77
|
+
merchantId: 'MERCH_...',
|
|
78
|
+
walletAddress: '01ab...',
|
|
79
|
+
network: 'testnet'
|
|
58
80
|
});
|
|
59
81
|
|
|
60
82
|
document.getElementById('payBtn').onclick = async () => {
|
|
61
|
-
const
|
|
62
|
-
senderAddress: '0x123...',
|
|
83
|
+
const result = await caspay.payments.makePayment({
|
|
63
84
|
productId: 'prod_abc',
|
|
64
|
-
amount:
|
|
85
|
+
amount: 10
|
|
65
86
|
});
|
|
66
|
-
|
|
67
|
-
alert('Payment successful!');
|
|
68
87
|
};
|
|
69
88
|
</script>
|
|
70
89
|
```
|
|
71
90
|
|
|
91
|
+
### Mode 2: Tracking Only (Merchant Handles Payment)
|
|
92
|
+
|
|
93
|
+
Merchant integrates Casper Wallet separately, makes the payment, then records it with CasPay for analytics.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// After merchant processes the payment with their own wallet integration:
|
|
97
|
+
|
|
98
|
+
const result = await caspay.payments.recordPayment({
|
|
99
|
+
senderAddress: '0145ab...', // Customer's wallet address
|
|
100
|
+
transactionHash: 'abc123...', // Casper blockchain tx hash
|
|
101
|
+
productId: 'prod_abc123',
|
|
102
|
+
amount: 10.5,
|
|
103
|
+
currency: 'CSPR'
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
// For subscriptions:
|
|
108
|
+
const subResult = await caspay.payments.recordSubscription({
|
|
109
|
+
senderAddress: '0145ab...',
|
|
110
|
+
transactionHash: 'xyz789...',
|
|
111
|
+
subscriptionPlanId: 'plan_monthly',
|
|
112
|
+
amount: 29.99,
|
|
113
|
+
currency: 'CSPR'
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
72
117
|
## 🔧 Configuration
|
|
73
118
|
|
|
74
119
|
### Constructor Options
|
|
75
120
|
|
|
76
121
|
```typescript
|
|
77
122
|
const caspay = new CasPay({
|
|
78
|
-
apiKey: string;
|
|
79
|
-
merchantId: string;
|
|
123
|
+
apiKey: string; // Required: Your CasPay API key
|
|
124
|
+
merchantId: string; // Required: Your merchant ID
|
|
125
|
+
walletAddress: string; // Required: Merchant wallet to receive payments
|
|
126
|
+
network?: 'mainnet' | 'testnet'; // Optional: Default is testnet
|
|
127
|
+
baseUrl?: string; // Optional: API base URL (for development)
|
|
80
128
|
});
|
|
81
129
|
```
|
|
82
130
|
|
|
83
131
|
### Get API Keys
|
|
84
132
|
|
|
85
133
|
1. Sign up at [caspay.link](https://caspay.link)
|
|
86
|
-
2.
|
|
134
|
+
2. Create and go the merchant page → API Keys
|
|
87
135
|
3. Generate a new API key
|
|
88
136
|
|
|
89
137
|
## 📚 API Reference
|
|
90
138
|
|
|
139
|
+
### Wallet
|
|
140
|
+
|
|
141
|
+
#### `caspay.wallet.connect()`
|
|
142
|
+
|
|
143
|
+
Connect to Casper Wallet extension.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
const address = await caspay.wallet.connect();
|
|
147
|
+
console.log('Connected:', address);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### `caspay.wallet.disconnect()`
|
|
151
|
+
|
|
152
|
+
Disconnect from wallet.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
await caspay.wallet.disconnect();
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### `caspay.wallet.getAddress()`
|
|
159
|
+
|
|
160
|
+
Get current connected wallet address.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
const address = await caspay.wallet.getAddress();
|
|
164
|
+
console.log('Address:', address);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
#### `caspay.wallet.getInfo()`
|
|
168
|
+
|
|
169
|
+
Get cached wallet connection info (synchronous).
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
const info = caspay.wallet.getInfo();
|
|
173
|
+
console.log('Connected:', info.isConnected);
|
|
174
|
+
console.log('Address:', info.address);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### `caspay.wallet.getState()`
|
|
178
|
+
|
|
179
|
+
Get complete wallet state (asynchronous).
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const state = await caspay.wallet.getState();
|
|
183
|
+
console.log('Connected:', state.connected);
|
|
184
|
+
console.log('Address:', state.address);
|
|
185
|
+
console.log('Locked:', state.locked);
|
|
186
|
+
```
|
|
187
|
+
|
|
91
188
|
### Payments
|
|
92
189
|
|
|
93
|
-
#### `caspay.payments.
|
|
190
|
+
#### `caspay.payments.makePayment(params)` - Full Management
|
|
191
|
+
|
|
192
|
+
Make a payment with full wallet & transfer management.
|
|
193
|
+
|
|
194
|
+
**Parameters:**
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
{
|
|
198
|
+
productId?: string; // Product ID (for one-time payments)
|
|
199
|
+
subscriptionPlanId?: string; // Subscription plan ID (for recurring)
|
|
200
|
+
amount: number; // Payment amount in CSPR
|
|
201
|
+
currency?: string; // Currency code (default: CSPR)
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Returns:**
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
{
|
|
209
|
+
success: boolean;
|
|
210
|
+
transactionHash: string;
|
|
211
|
+
payment?: PaymentResponse; // If successfully recorded
|
|
212
|
+
error?: string; // If payment failed
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**Example:**
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const result = await caspay.payments.makePayment({
|
|
220
|
+
productId: 'prod_abc123',
|
|
221
|
+
amount: 10.5
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### `caspay.payments.recordPayment(params)` - Tracking Only
|
|
94
226
|
|
|
95
|
-
|
|
227
|
+
Record a payment that was already processed by merchant.
|
|
96
228
|
|
|
97
229
|
**Parameters:**
|
|
98
230
|
|
|
99
231
|
```typescript
|
|
100
232
|
{
|
|
101
233
|
senderAddress: string; // Sender's Casper wallet address
|
|
234
|
+
transactionHash?: string; // Casper transaction hash (optional)
|
|
102
235
|
productId?: string; // Product ID (for one-time payments)
|
|
103
236
|
subscriptionPlanId?: string; // Subscription plan ID (for recurring)
|
|
104
237
|
amount: number; // Payment amount
|
|
105
238
|
currency?: string; // Currency code (default: USD)
|
|
106
|
-
transactionHash?: string; // Optional: Casper transaction hash
|
|
107
239
|
}
|
|
108
240
|
```
|
|
109
241
|
|
|
@@ -132,18 +264,78 @@ Create a new payment record.
|
|
|
132
264
|
**Example:**
|
|
133
265
|
|
|
134
266
|
```typescript
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
267
|
+
const result = await caspay.payments.recordPayment({
|
|
268
|
+
senderAddress: '0145ab...',
|
|
269
|
+
transactionHash: 'abc123...',
|
|
138
270
|
productId: 'prod_abc123',
|
|
139
|
-
amount:
|
|
271
|
+
amount: 10.5,
|
|
272
|
+
currency: 'CSPR'
|
|
140
273
|
});
|
|
274
|
+
```
|
|
141
275
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
276
|
+
#### `caspay.payments.recordSubscription(params)` - Tracking Only
|
|
277
|
+
|
|
278
|
+
Record a subscription payment (alias for recordPayment with subscriptionPlanId).
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
const result = await caspay.payments.recordSubscription({
|
|
282
|
+
senderAddress: '0145ab...',
|
|
283
|
+
transactionHash: 'xyz789...',
|
|
284
|
+
subscriptionPlanId: 'plan_monthly',
|
|
285
|
+
amount: 29.99,
|
|
286
|
+
currency: 'CSPR'
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Subscriptions
|
|
291
|
+
|
|
292
|
+
#### `caspay.subscriptions.checkStatus(params)`
|
|
293
|
+
|
|
294
|
+
Check subscription status for a subscriber.
|
|
295
|
+
|
|
296
|
+
**Parameters:**
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
{
|
|
300
|
+
subscriberAddress: string; // Subscriber's wallet address
|
|
301
|
+
planId?: string; // Optional: Filter by specific plan
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
**Returns:**
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
{
|
|
309
|
+
success: boolean;
|
|
310
|
+
active: boolean;
|
|
311
|
+
subscriptions?: Array<{
|
|
312
|
+
id: string;
|
|
313
|
+
plan_id: string;
|
|
314
|
+
subscriber_address: string;
|
|
315
|
+
status: string;
|
|
316
|
+
current_period_start: string;
|
|
317
|
+
current_period_end: string;
|
|
318
|
+
created_at: string;
|
|
319
|
+
}>;
|
|
320
|
+
message?: string;
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
**Example:**
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
// Check all subscriptions for a subscriber
|
|
328
|
+
const status = await caspay.subscriptions.checkStatus({
|
|
329
|
+
subscriberAddress: '0145ab...'
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
console.log('Active:', status.active);
|
|
333
|
+
console.log('Subscriptions:', status.subscriptions);
|
|
334
|
+
|
|
335
|
+
// Check specific plan
|
|
336
|
+
const planStatus = await caspay.subscriptions.checkStatus({
|
|
337
|
+
subscriberAddress: '0145ab...',
|
|
338
|
+
planId: 'plan_monthly'
|
|
147
339
|
});
|
|
148
340
|
```
|
|
149
341
|
|
|
@@ -153,7 +345,7 @@ All SDK methods throw structured errors:
|
|
|
153
345
|
|
|
154
346
|
```typescript
|
|
155
347
|
try {
|
|
156
|
-
const payment = await caspay.payments.
|
|
348
|
+
const payment = await caspay.payments.makePayment({...});
|
|
157
349
|
} catch (error) {
|
|
158
350
|
console.error('Error:', error.error); // Error message
|
|
159
351
|
console.error('Code:', error.code); // Error code
|
|
@@ -165,9 +357,29 @@ try {
|
|
|
165
357
|
|
|
166
358
|
- `INVALID_PARAMS` - Missing or invalid parameters
|
|
167
359
|
- `INVALID_API_KEY` - Invalid API key
|
|
168
|
-
- `
|
|
169
|
-
- `
|
|
360
|
+
- `WALLET_NOT_FOUND` - Casper Wallet extension not installed
|
|
361
|
+
- `WALLET_LOCKED` - Wallet is locked
|
|
362
|
+
- `CONNECTION_REJECTED` - User rejected wallet connection
|
|
363
|
+
- `TRANSFER_REJECTED` - User rejected transaction
|
|
170
364
|
- `NETWORK_ERROR` - Network connection error
|
|
365
|
+
- `VERIFICATION_FAILED` - Transaction verification failed
|
|
366
|
+
|
|
367
|
+
### Wallet Errors
|
|
368
|
+
|
|
369
|
+
```typescript
|
|
370
|
+
try {
|
|
371
|
+
await caspay.wallet.connect();
|
|
372
|
+
} catch (error) {
|
|
373
|
+
if (error.code === 'WALLET_NOT_FOUND') {
|
|
374
|
+
// Prompt user to install Casper Wallet
|
|
375
|
+
window.open(error.installUrl, '_blank');
|
|
376
|
+
} else if (error.code === 'WALLET_LOCKED') {
|
|
377
|
+
alert('Please unlock your Casper Wallet');
|
|
378
|
+
} else if (error.code === 'CONNECTION_REJECTED') {
|
|
379
|
+
alert('Connection rejected by user');
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
```
|
|
171
383
|
|
|
172
384
|
## 🌐 Environment Support
|
|
173
385
|
|
|
@@ -175,36 +387,48 @@ try {
|
|
|
175
387
|
|-------------|--------------|--------|
|
|
176
388
|
| React/Next.js | `npm install @caspay/sdk` | `import CasPay from '@caspay/sdk'` |
|
|
177
389
|
| Node.js | `npm install @caspay/sdk` | `const CasPay = require('@caspay/sdk')` |
|
|
178
|
-
|
|
|
179
|
-
| HTML | CDN script tag | `window.CasPay` |
|
|
180
|
-
|
|
181
|
-
## 🔒 Security
|
|
390
|
+
| PHP/Vanilla JS | CDN script tag | `window.CasPay` |
|
|
182
391
|
|
|
183
|
-
- **Never expose your API key** in client-side code in production
|
|
184
|
-
- Use environment variables for sensitive data
|
|
185
|
-
- API keys should be stored server-side
|
|
186
|
-
- Use test keys (`cp_test_...`) for development
|
|
187
392
|
|
|
188
393
|
## 📦 TypeScript Support
|
|
189
394
|
|
|
190
395
|
Full TypeScript support with type definitions included:
|
|
191
396
|
|
|
192
397
|
```typescript
|
|
193
|
-
import CasPay, {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
398
|
+
import CasPay, {
|
|
399
|
+
MakePaymentParams,
|
|
400
|
+
MakePaymentResult,
|
|
401
|
+
PaymentCreateParams,
|
|
402
|
+
PaymentResponse,
|
|
403
|
+
SubscriptionCheckParams,
|
|
404
|
+
SubscriptionCheckResponse,
|
|
405
|
+
WalletState
|
|
406
|
+
} from '@caspay/sdk';
|
|
407
|
+
|
|
408
|
+
const params: MakePaymentParams = {
|
|
197
409
|
productId: 'prod_abc',
|
|
198
|
-
amount:
|
|
410
|
+
amount: 10.5
|
|
199
411
|
};
|
|
200
412
|
|
|
201
|
-
const
|
|
413
|
+
const result: MakePaymentResult = await caspay.payments.makePayment(params);
|
|
202
414
|
```
|
|
203
415
|
|
|
416
|
+
## 🎯 Integration Modes Comparison
|
|
417
|
+
|
|
418
|
+
| Feature | Full Management | Tracking Only |
|
|
419
|
+
|---------|----------------|---------------|
|
|
420
|
+
| Wallet Integration | ✅ CasPay SDK | ❌ Merchant implements |
|
|
421
|
+
| Blockchain Transfer | ✅ CasPay SDK | ❌ Merchant handles |
|
|
422
|
+
| Payment Recording | ✅ Automatic | ✅ Manual via SDK |
|
|
423
|
+
| Analytics Dashboard | ✅ Yes | ✅ Yes |
|
|
424
|
+
| Subscription Tracking | ✅ Yes | ✅ Yes |
|
|
425
|
+
| **Best For** | Simple integration | Custom wallet UX |
|
|
426
|
+
|
|
204
427
|
## 🔗 Links
|
|
205
428
|
|
|
206
429
|
- **Documentation:** [docs.caspay.link](https://docs.caspay.link)
|
|
207
430
|
- **Dashboard:** [caspay.link](https://caspay.link)
|
|
431
|
+
- **Demo:** [demo.caspay.link](https://caspay.link/demo.html)
|
|
208
432
|
- **NPM:** [@caspay/sdk](https://www.npmjs.com/package/@caspay/sdk)
|
|
209
433
|
- **GitHub:** [dmrdvn/caspay-sdk](https://github.com/dmrdvn/caspay-sdk)
|
|
210
434
|
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,17 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser bundle entry point
|
|
3
|
-
* Exposes CasPay SDK to window.CasPay for script tag usage
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```html
|
|
7
|
-
* <script src="https://cdn.jsdelivr.net/npm/@caspay/sdk/dist/caspay.min.js"></script>
|
|
8
|
-
* <script>
|
|
9
|
-
* const caspay = new CasPay({
|
|
10
|
-
* apiKey: 'cp_live_...',
|
|
11
|
-
* merchantId: 'MERCH_...'
|
|
12
|
-
* });
|
|
13
|
-
* </script>
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
1
|
import CasPay from './index';
|
|
17
2
|
export default CasPay;
|