@blocklet/payment-js 1.18.6 → 1.18.7
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 +181 -44
- package/lib/index.d.ts +1 -11
- package/lib/resources/customer.d.ts +1 -8
- package/lib/resources/customer.js +9 -5
- package/lib/resources/subscription.d.ts +0 -3
- package/lib/resources/subscription.js +4 -4
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# PaymentKit Node.js SDK
|
|
2
2
|
|
|
3
|
-
A Node.js SDK for the PaymentKit API
|
|
3
|
+
A Node.js SDK for the PaymentKit API. This package allows you to manage resources in PaymentKit including customers, subscriptions, products, prices, payments, checkout sessions, usage records, and webhooks.
|
|
4
|
+
|
|
5
|
+
## Related Links
|
|
6
|
+
|
|
7
|
+
- [Payment Kit Documentation](https://www.arcblock.io/docs/arcblock-payment-kit/en/start-payment-js) - Official documentation with detailed guides and API references
|
|
4
8
|
|
|
5
9
|
## Installation
|
|
6
10
|
|
|
@@ -8,32 +12,113 @@ A Node.js SDK for the PaymentKit API, can be used to manage resources in Payment
|
|
|
8
12
|
npm install @blocklet/payment-js
|
|
9
13
|
```
|
|
10
14
|
|
|
11
|
-
##
|
|
15
|
+
## Getting Started
|
|
16
|
+
|
|
17
|
+
### Configuration
|
|
18
|
+
|
|
19
|
+
Configure the SDK with your desired environment and API key:
|
|
12
20
|
|
|
13
21
|
```js
|
|
14
22
|
import payment from '@blocklet/payment-js';
|
|
15
23
|
|
|
16
|
-
//
|
|
17
|
-
payment.environments.setTestMode(true);
|
|
18
|
-
payment.environments.
|
|
24
|
+
// Set environment mode
|
|
25
|
+
payment.environments.setTestMode(true); // Use test environment
|
|
26
|
+
// payment.environments.setLiveMode(true); // Use live environment
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Environment Variables
|
|
31
|
+
|
|
32
|
+
The SDK supports the following environment variables for configuration:
|
|
33
|
+
|
|
34
|
+
- `PAYMENT_LIVE_MODE`: Set to 'true' for live mode, 'false' for test mode
|
|
35
|
+
- `PAYMENT_TEST_MODE`: Set to 'true' to enable test mode
|
|
36
|
+
|
|
37
|
+
## Usage Examples
|
|
38
|
+
|
|
39
|
+
### Listing Subscriptions
|
|
19
40
|
|
|
20
|
-
|
|
41
|
+
```js
|
|
21
42
|
const subscriptions = await payment.subscriptions.list({
|
|
22
|
-
order: 'updated_at:ASC',
|
|
23
|
-
activeFirst: true,
|
|
43
|
+
order: 'updated_at:ASC', // Sort by update time
|
|
44
|
+
activeFirst: true, // Show active first
|
|
24
45
|
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Creating a Checkout Session
|
|
25
49
|
|
|
26
|
-
|
|
50
|
+
```js
|
|
27
51
|
const session = await payment.checkout.sessions.create({
|
|
28
52
|
success_url: 'https://example.com/success',
|
|
29
53
|
cancel_url: 'https://example.com/cancel',
|
|
30
54
|
mode: 'payment',
|
|
31
55
|
line_items: [
|
|
32
56
|
{ price_id: 'price_xxx', quantity: 1 }
|
|
57
|
+
],
|
|
58
|
+
subscription_data: {
|
|
59
|
+
service_actions: [
|
|
60
|
+
{
|
|
61
|
+
type: 'notification',
|
|
62
|
+
text: { zh: '查看文档', en: 'View Documentation' },
|
|
63
|
+
link: 'https://docs.example.com',
|
|
64
|
+
triggerEvents: ['customer.subscription.started']
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
expires_at: 1729243800
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Managing Products and Prices
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
// Create product
|
|
76
|
+
const product = await payment.products.create({
|
|
77
|
+
name: 'Test Product',
|
|
78
|
+
description: 'Product description',
|
|
79
|
+
type: 'service'
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Create price with EVM support
|
|
83
|
+
const price = await payment.prices.create({
|
|
84
|
+
product_id: product.id,
|
|
85
|
+
type: 'recurring',
|
|
86
|
+
unit_amount: '0.001',
|
|
87
|
+
currency_id: 'pc_xxx',
|
|
88
|
+
recurring: {
|
|
89
|
+
interval: 'month',
|
|
90
|
+
interval_count: 1,
|
|
91
|
+
usage_type: 'licensed'
|
|
92
|
+
},
|
|
93
|
+
quantity_available: 10,
|
|
94
|
+
quantity_limit_per_checkout: 2,
|
|
95
|
+
currency_options: [
|
|
96
|
+
{
|
|
97
|
+
currency_id: 'pc_xxx',
|
|
98
|
+
unit_amount: '0.001'
|
|
99
|
+
}
|
|
33
100
|
]
|
|
34
101
|
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Managing Subscriptions
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
// get subscription
|
|
108
|
+
await payment.subscriptions.retrieve('sub_xxx');
|
|
109
|
+
|
|
110
|
+
// Report usage
|
|
111
|
+
await payment.subscriptionItems.createUsageRecord({
|
|
112
|
+
subscription_item_id: 'si_xxx',
|
|
113
|
+
quantity: 1,
|
|
114
|
+
action: 'increment',
|
|
115
|
+
timestamp: Math.floor(Date.now() / 1000)
|
|
116
|
+
});
|
|
117
|
+
```
|
|
35
118
|
|
|
36
|
-
|
|
119
|
+
### Handling Refunds
|
|
120
|
+
|
|
121
|
+
```js
|
|
37
122
|
const refund = await payment.paymentIntents.refund('pi_xxx', {
|
|
38
123
|
amount: '0.001',
|
|
39
124
|
reason: 'requested_by_customer',
|
|
@@ -41,61 +126,117 @@ const refund = await payment.paymentIntents.refund('pi_xxx', {
|
|
|
41
126
|
});
|
|
42
127
|
```
|
|
43
128
|
|
|
44
|
-
|
|
129
|
+
### Setting up Webhooks
|
|
45
130
|
|
|
46
|
-
|
|
131
|
+
```js
|
|
132
|
+
const webhook = await payment.webhookEndpoints.create({
|
|
133
|
+
url: 'https://example.com/webhook',
|
|
134
|
+
enabled_events: [
|
|
135
|
+
'checkout.session.completed',
|
|
136
|
+
'checkout.session.nft_minted',
|
|
137
|
+
'customer.subscription.created',
|
|
138
|
+
'customer.subscription.deleted',
|
|
139
|
+
'customer.subscription.paused',
|
|
140
|
+
'customer.subscription.updated',
|
|
141
|
+
'customer.subscription.started',
|
|
142
|
+
'customer.subscription.renewed',
|
|
143
|
+
'payment_intent.created',
|
|
144
|
+
'payment_intent.succeeded'
|
|
145
|
+
]
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## API Resources
|
|
47
150
|
|
|
48
151
|
### Customers
|
|
49
|
-
- `payment.customers.create()`
|
|
50
152
|
- `payment.customers.retrieve(id)`
|
|
51
153
|
- `payment.customers.update(id, data)`
|
|
52
154
|
- `payment.customers.list(params)`
|
|
53
155
|
- `payment.customers.search(params)`
|
|
54
156
|
- `payment.customers.del(id)`
|
|
157
|
+
- `payment.customers.me()`
|
|
55
158
|
|
|
56
159
|
### Subscriptions
|
|
57
|
-
- `payment.subscriptions.create()`
|
|
58
160
|
- `payment.subscriptions.retrieve(id)`
|
|
59
161
|
- `payment.subscriptions.update(id, data)`
|
|
60
162
|
- `payment.subscriptions.list(params)`
|
|
163
|
+
- `payment.subscriptions.search(params)`
|
|
61
164
|
- `payment.subscriptions.cancel(id, options)`
|
|
62
165
|
- `payment.subscriptions.recover(id)`
|
|
63
166
|
- `payment.subscriptions.pause(id)`
|
|
64
167
|
- `payment.subscriptions.resume(id)`
|
|
168
|
+
- `payment.subscriptions.del(id)`
|
|
65
169
|
|
|
66
170
|
### Products & Prices
|
|
67
|
-
- `payment.products
|
|
68
|
-
- `payment.
|
|
171
|
+
- `payment.products.create(data)`
|
|
172
|
+
- `payment.products.retrieve(id)`
|
|
173
|
+
- `payment.products.update(id, data)`
|
|
174
|
+
- `payment.products.list(params)`
|
|
175
|
+
- `payment.products.search(params)`
|
|
176
|
+
- `payment.products.archive(id)`
|
|
177
|
+
- `payment.products.del(id)`
|
|
178
|
+
- `payment.prices.create(data)`
|
|
179
|
+
- `payment.prices.retrieve(id)`
|
|
180
|
+
- `payment.prices.update(id, data)`
|
|
181
|
+
- `payment.prices.list(params)`
|
|
182
|
+
- `payment.prices.search(params)`
|
|
183
|
+
- `payment.prices.archive(id)`
|
|
184
|
+
- `payment.prices.del(id)`
|
|
185
|
+
- `payment.prices.inventory(id, data)`
|
|
69
186
|
|
|
70
|
-
### Payments
|
|
71
|
-
- `payment.paymentIntents
|
|
72
|
-
- `payment.
|
|
73
|
-
- `payment.
|
|
187
|
+
### Payments & Refunds
|
|
188
|
+
- `payment.paymentIntents.create(data)`
|
|
189
|
+
- `payment.paymentIntents.retrieve(id)`
|
|
190
|
+
- `payment.paymentIntents.update(id, data)`
|
|
191
|
+
- `payment.paymentIntents.list(params)`
|
|
192
|
+
- `payment.paymentIntents.search(params)`
|
|
193
|
+
- `payment.paymentIntents.refund(id, data)`
|
|
194
|
+
- `payment.refunds.create(data)`
|
|
195
|
+
- `payment.refunds.retrieve(id)`
|
|
196
|
+
- `payment.refunds.list(params)`
|
|
197
|
+
- `payment.refunds.search(params)`
|
|
198
|
+
|
|
199
|
+
### Payment Links
|
|
200
|
+
- `payment.paymentLinks.create(data)`
|
|
201
|
+
- `payment.paymentLinks.retrieve(id)`
|
|
202
|
+
- `payment.paymentLinks.update(id, data)`
|
|
203
|
+
- `payment.paymentLinks.archive(id)`
|
|
204
|
+
- `payment.paymentLinks.list(params)`
|
|
74
205
|
|
|
75
206
|
### Checkout
|
|
76
|
-
- `payment.checkout.sessions
|
|
77
|
-
- `payment.
|
|
207
|
+
- `payment.checkout.sessions.create(data)`
|
|
208
|
+
- `payment.checkout.sessions.retrieve(id)`
|
|
209
|
+
- `payment.checkout.sessions.update(id, data)`
|
|
210
|
+
- `payment.checkout.sessions.expire(id)`
|
|
211
|
+
- `payment.checkout.sessions.list(params)`
|
|
78
212
|
|
|
79
213
|
### Usage & Metering
|
|
80
|
-
- `payment.subscriptionItems.
|
|
81
|
-
- `payment.subscriptionItems.
|
|
214
|
+
- `payment.subscriptionItems.create(data)`
|
|
215
|
+
- `payment.subscriptionItems.retrieve(id)`
|
|
216
|
+
- `payment.subscriptionItems.update(id, data)`
|
|
217
|
+
- `payment.subscriptionItems.list(params)`
|
|
218
|
+
- `payment.subscriptionItems.del(id)`
|
|
219
|
+
- `payment.subscriptionItems.createUsageRecord(data)`
|
|
220
|
+
- `payment.subscriptionItems.listUsageRecordSummaries(id)`
|
|
221
|
+
- `payment.subscriptionItems.listUsageRecords(params)`
|
|
82
222
|
|
|
83
223
|
### Webhooks
|
|
84
|
-
- `payment.webhookEndpoints
|
|
85
|
-
|
|
86
|
-
|
|
224
|
+
- `payment.webhookEndpoints.create(data)`
|
|
225
|
+
- `payment.webhookEndpoints.retrieve(id)`
|
|
226
|
+
- `payment.webhookEndpoints.update(id, data)`
|
|
227
|
+
- `payment.webhookEndpoints.list(params)`
|
|
228
|
+
- `payment.webhookEndpoints.del(id)`
|
|
87
229
|
|
|
88
|
-
|
|
230
|
+
## Subscription Status
|
|
89
231
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
payment
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
```
|
|
232
|
+
A subscription can have the following statuses:
|
|
233
|
+
- `active`: The subscription is in good standing
|
|
234
|
+
- `canceled`: The subscription has been canceled
|
|
235
|
+
- `incomplete`: Initial payment attempt failed
|
|
236
|
+
- `incomplete_expired`: Initial payment failed and retry period expired
|
|
237
|
+
- `past_due`: Latest payment failed
|
|
238
|
+
- `trialing`: In trial period
|
|
239
|
+
- `paused`: Temporarily paused
|
|
99
240
|
|
|
100
241
|
## Error Handling
|
|
101
242
|
|
|
@@ -103,18 +244,14 @@ payment.environments.setEndpoint('https://api.example.com');
|
|
|
103
244
|
try {
|
|
104
245
|
const subscription = await payment.subscriptions.retrieve('sub_xxx');
|
|
105
246
|
} catch (error) {
|
|
106
|
-
|
|
107
|
-
console.error('Subscription not found');
|
|
108
|
-
} else {
|
|
109
|
-
console.error('An error occurred:', error.message);
|
|
110
|
-
}
|
|
247
|
+
console.error('An error occurred:', error.message);
|
|
111
248
|
}
|
|
112
249
|
```
|
|
113
250
|
|
|
114
251
|
## TypeScript Support
|
|
115
252
|
|
|
116
|
-
The SDK is written in TypeScript and
|
|
253
|
+
The SDK is written in TypeScript and includes full type definitions for all resources and methods.
|
|
117
254
|
|
|
118
255
|
## License
|
|
119
256
|
|
|
120
|
-
|
|
257
|
+
Apache-2.0
|
package/lib/index.d.ts
CHANGED
|
@@ -31,11 +31,6 @@ declare const _default: {
|
|
|
31
31
|
};
|
|
32
32
|
};
|
|
33
33
|
customers: {
|
|
34
|
-
create: (data: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Customer, {
|
|
35
|
-
omit: never;
|
|
36
|
-
}>>, params?: never) => Promise<import("sequelize").InferAttributes<import("@blocklet/payment-types").Customer, {
|
|
37
|
-
omit: never;
|
|
38
|
-
}>>;
|
|
39
34
|
retrieve: (_params: string, data?: never) => Promise<import("@blocklet/payment-types").TCustomerExpanded>;
|
|
40
35
|
update: (_params: string, data?: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Customer, {
|
|
41
36
|
omit: never;
|
|
@@ -46,9 +41,7 @@ declare const _default: {
|
|
|
46
41
|
did?: string;
|
|
47
42
|
}>, data?: never) => Promise<import("@blocklet/payment-types").Paginated<import("@blocklet/payment-types").TCustomerExpanded>>;
|
|
48
43
|
search: (_params: any, data?: never) => Promise<import("@blocklet/payment-types").Paginated<import("@blocklet/payment-types").TCustomerExpanded>>;
|
|
49
|
-
|
|
50
|
-
omit: never;
|
|
51
|
-
}>>;
|
|
44
|
+
me: (_params: never, data?: never) => Promise<import("@blocklet/payment-types").TCustomerExpanded>;
|
|
52
45
|
};
|
|
53
46
|
products: {
|
|
54
47
|
create: (data: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Product, {
|
|
@@ -175,9 +168,6 @@ declare const _default: {
|
|
|
175
168
|
}) => Promise<import("@blocklet/payment-types").TPriceExpanded>;
|
|
176
169
|
};
|
|
177
170
|
subscriptions: {
|
|
178
|
-
create: (data: Partial<import("@blocklet/payment-types").TSubscriptionExpanded>, params?: never) => Promise<import("sequelize").InferAttributes<import("@blocklet/payment-types").Subscription, {
|
|
179
|
-
omit: never;
|
|
180
|
-
}>>;
|
|
181
171
|
retrieve: (_params: string, data?: never) => Promise<import("@blocklet/payment-types").TSubscriptionExpanded>;
|
|
182
172
|
update: (_params: string, data?: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Subscription, {
|
|
183
173
|
omit: never;
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import type { Paginated, Pagination, TCustomerExpanded } from '@blocklet/payment-types';
|
|
2
2
|
declare const _default: {
|
|
3
|
-
create: (data: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Customer, {
|
|
4
|
-
omit: never;
|
|
5
|
-
}>>, params?: never) => Promise<import("sequelize").InferAttributes<import("@blocklet/payment-types").Customer, {
|
|
6
|
-
omit: never;
|
|
7
|
-
}>>;
|
|
8
3
|
retrieve: (_params: string, data?: never) => Promise<TCustomerExpanded>;
|
|
9
4
|
update: (_params: string, data?: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Customer, {
|
|
10
5
|
omit: never;
|
|
@@ -15,8 +10,6 @@ declare const _default: {
|
|
|
15
10
|
did?: string;
|
|
16
11
|
}>, data?: never) => Promise<Paginated<TCustomerExpanded>>;
|
|
17
12
|
search: (_params: any, data?: never) => Promise<Paginated<TCustomerExpanded>>;
|
|
18
|
-
|
|
19
|
-
omit: never;
|
|
20
|
-
}>>;
|
|
13
|
+
me: (_params: never, data?: never) => Promise<TCustomerExpanded>;
|
|
21
14
|
};
|
|
22
15
|
export default _default;
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const resource_1 = require("../resource");
|
|
4
4
|
exports.default = {
|
|
5
|
-
create:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}),
|
|
5
|
+
// create: createResourceCreateMethod<TCustomer, Partial<TCustomer>, never>({
|
|
6
|
+
// method: 'POST',
|
|
7
|
+
// path: '/api/customers',
|
|
8
|
+
// }),
|
|
9
9
|
// get customer by id or did
|
|
10
10
|
retrieve: (0, resource_1.createResourceMethod)({ method: 'GET', path: '/api/customers/{id}' }),
|
|
11
11
|
update: (0, resource_1.createResourceMethod)({
|
|
@@ -20,5 +20,9 @@ exports.default = {
|
|
|
20
20
|
method: 'GET',
|
|
21
21
|
path: '/api/customers/search',
|
|
22
22
|
}),
|
|
23
|
-
|
|
23
|
+
me: (0, resource_1.createResourceMethod)({
|
|
24
|
+
method: 'GET',
|
|
25
|
+
path: '/api/customers/me',
|
|
26
|
+
}),
|
|
27
|
+
// del: createResourceMethod<TCustomer, never, string>({ method: 'DELETE', path: '/api/customers/{id}' }),
|
|
24
28
|
};
|
|
@@ -13,9 +13,6 @@ type CancelBodyType = {
|
|
|
13
13
|
cancel_from?: 'customer' | 'admin';
|
|
14
14
|
};
|
|
15
15
|
declare const _default: {
|
|
16
|
-
create: (data: Partial<TSubscriptionExpanded>, params?: never) => Promise<import("sequelize").InferAttributes<import("@blocklet/payment-types").Subscription, {
|
|
17
|
-
omit: never;
|
|
18
|
-
}>>;
|
|
19
16
|
retrieve: (_params: string, data?: never) => Promise<TSubscriptionExpanded>;
|
|
20
17
|
update: (_params: string, data?: Partial<import("sequelize").InferAttributes<import("@blocklet/payment-types").Subscription, {
|
|
21
18
|
omit: never;
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const resource_1 = require("../resource");
|
|
4
4
|
exports.default = {
|
|
5
|
-
create:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}),
|
|
5
|
+
// create: createResourceCreateMethod<TSubscription, Partial<TSubscriptionExpanded>, never>({
|
|
6
|
+
// method: 'POST',
|
|
7
|
+
// path: '/api/subscriptions',
|
|
8
|
+
// }),
|
|
9
9
|
retrieve: (0, resource_1.createResourceMethod)({
|
|
10
10
|
method: 'GET',
|
|
11
11
|
path: '/api/subscriptions/{id}',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/payment-js",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.7",
|
|
4
4
|
"description": "Node.js client for Payment Kit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"types",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"url": "https://github.com/blocklet/payment-kit/issues"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@blocklet/payment-types": "1.18.
|
|
39
|
+
"@blocklet/payment-types": "1.18.7",
|
|
40
40
|
"@blocklet/sdk": "^1.16.38"
|
|
41
41
|
},
|
|
42
42
|
"importSort": {
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"type-fest": "^4.23.0",
|
|
64
64
|
"typescript": "^5.5.4"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "b75168ab5e2cd939571e6e26543374c8d0dab963"
|
|
67
67
|
}
|