@caspay/sdk 1.0.3 → 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 -55
- 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 +461 -63
- package/dist/index.js +461 -63
- 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,97 +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
|
-
**Note:** API endpoint is fixed to `https://caspay.link/api` for all requests.
|
|
84
|
-
|
|
85
131
|
### Get API Keys
|
|
86
132
|
|
|
87
133
|
1. Sign up at [caspay.link](https://caspay.link)
|
|
88
|
-
2.
|
|
134
|
+
2. Create and go the merchant page → API Keys
|
|
89
135
|
3. Generate a new API key
|
|
90
136
|
|
|
91
137
|
## 📚 API Reference
|
|
92
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
|
+
|
|
93
188
|
### Payments
|
|
94
189
|
|
|
95
|
-
#### `caspay.payments.
|
|
190
|
+
#### `caspay.payments.makePayment(params)` - Full Management
|
|
96
191
|
|
|
97
|
-
|
|
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
|
|
226
|
+
|
|
227
|
+
Record a payment that was already processed by merchant.
|
|
98
228
|
|
|
99
229
|
**Parameters:**
|
|
100
230
|
|
|
101
231
|
```typescript
|
|
102
232
|
{
|
|
103
233
|
senderAddress: string; // Sender's Casper wallet address
|
|
234
|
+
transactionHash?: string; // Casper transaction hash (optional)
|
|
104
235
|
productId?: string; // Product ID (for one-time payments)
|
|
105
236
|
subscriptionPlanId?: string; // Subscription plan ID (for recurring)
|
|
106
237
|
amount: number; // Payment amount
|
|
107
238
|
currency?: string; // Currency code (default: USD)
|
|
108
|
-
transactionHash?: string; // Optional: Casper transaction hash
|
|
109
239
|
}
|
|
110
240
|
```
|
|
111
241
|
|
|
@@ -134,18 +264,78 @@ Create a new payment record.
|
|
|
134
264
|
**Example:**
|
|
135
265
|
|
|
136
266
|
```typescript
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
267
|
+
const result = await caspay.payments.recordPayment({
|
|
268
|
+
senderAddress: '0145ab...',
|
|
269
|
+
transactionHash: 'abc123...',
|
|
140
270
|
productId: 'prod_abc123',
|
|
141
|
-
amount:
|
|
271
|
+
amount: 10.5,
|
|
272
|
+
currency: 'CSPR'
|
|
142
273
|
});
|
|
274
|
+
```
|
|
143
275
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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'
|
|
149
339
|
});
|
|
150
340
|
```
|
|
151
341
|
|
|
@@ -155,7 +345,7 @@ All SDK methods throw structured errors:
|
|
|
155
345
|
|
|
156
346
|
```typescript
|
|
157
347
|
try {
|
|
158
|
-
const payment = await caspay.payments.
|
|
348
|
+
const payment = await caspay.payments.makePayment({...});
|
|
159
349
|
} catch (error) {
|
|
160
350
|
console.error('Error:', error.error); // Error message
|
|
161
351
|
console.error('Code:', error.code); // Error code
|
|
@@ -167,9 +357,29 @@ try {
|
|
|
167
357
|
|
|
168
358
|
- `INVALID_PARAMS` - Missing or invalid parameters
|
|
169
359
|
- `INVALID_API_KEY` - Invalid API key
|
|
170
|
-
- `
|
|
171
|
-
- `
|
|
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
|
|
172
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
|
+
```
|
|
173
383
|
|
|
174
384
|
## 🌐 Environment Support
|
|
175
385
|
|
|
@@ -177,36 +387,48 @@ try {
|
|
|
177
387
|
|-------------|--------------|--------|
|
|
178
388
|
| React/Next.js | `npm install @caspay/sdk` | `import CasPay from '@caspay/sdk'` |
|
|
179
389
|
| Node.js | `npm install @caspay/sdk` | `const CasPay = require('@caspay/sdk')` |
|
|
180
|
-
|
|
|
181
|
-
| HTML | CDN script tag | `window.CasPay` |
|
|
182
|
-
|
|
183
|
-
## 🔒 Security
|
|
390
|
+
| PHP/Vanilla JS | CDN script tag | `window.CasPay` |
|
|
184
391
|
|
|
185
|
-
- **Never expose your API key** in client-side code in production
|
|
186
|
-
- Use environment variables for sensitive data
|
|
187
|
-
- API keys should be stored server-side
|
|
188
|
-
- Use test keys (`cp_test_...`) for development
|
|
189
392
|
|
|
190
393
|
## 📦 TypeScript Support
|
|
191
394
|
|
|
192
395
|
Full TypeScript support with type definitions included:
|
|
193
396
|
|
|
194
397
|
```typescript
|
|
195
|
-
import CasPay, {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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 = {
|
|
199
409
|
productId: 'prod_abc',
|
|
200
|
-
amount:
|
|
410
|
+
amount: 10.5
|
|
201
411
|
};
|
|
202
412
|
|
|
203
|
-
const
|
|
413
|
+
const result: MakePaymentResult = await caspay.payments.makePayment(params);
|
|
204
414
|
```
|
|
205
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
|
+
|
|
206
427
|
## 🔗 Links
|
|
207
428
|
|
|
208
429
|
- **Documentation:** [docs.caspay.link](https://docs.caspay.link)
|
|
209
430
|
- **Dashboard:** [caspay.link](https://caspay.link)
|
|
431
|
+
- **Demo:** [demo.caspay.link](https://caspay.link/demo.html)
|
|
210
432
|
- **NPM:** [@caspay/sdk](https://www.npmjs.com/package/@caspay/sdk)
|
|
211
433
|
- **GitHub:** [dmrdvn/caspay-sdk](https://github.com/dmrdvn/caspay-sdk)
|
|
212
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;
|