chargebee 3.21.0 → 3.21.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/CHANGELOG.md +7 -0
- package/README.md +70 -16
- package/cjs/createChargebee.js +3 -0
- package/cjs/environment.js +1 -1
- package/esm/createChargebee.js +3 -0
- package/esm/environment.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -20,6 +20,16 @@ If you're upgrading from an older version of [`chargebee-typescript`](https://ww
|
|
|
20
20
|
|
|
21
21
|
Node.js 18 or higher.
|
|
22
22
|
|
|
23
|
+
## Runtime Support
|
|
24
|
+
|
|
25
|
+
This SDK supports multiple JavaScript runtimes:
|
|
26
|
+
|
|
27
|
+
- **Node.js** 18+
|
|
28
|
+
- **Deno**
|
|
29
|
+
- **Bun**
|
|
30
|
+
- **Cloudflare Workers**
|
|
31
|
+
- **Edge Runtimes** (e.g., Vercel Edge, Netlify Edge Functions)
|
|
32
|
+
|
|
23
33
|
## Installation
|
|
24
34
|
|
|
25
35
|
Install the library with npm:
|
|
@@ -37,6 +47,16 @@ With yarn:
|
|
|
37
47
|
yarn add chargebee
|
|
38
48
|
```
|
|
39
49
|
|
|
50
|
+
With bun:
|
|
51
|
+
```sh
|
|
52
|
+
bun add chargebee
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
With deno:
|
|
56
|
+
```sh
|
|
57
|
+
deno add npm:chargebee
|
|
58
|
+
```
|
|
59
|
+
|
|
40
60
|
## Usage
|
|
41
61
|
|
|
42
62
|
The package needs to be configured with your site's API key, which is available under Configure Chargebee Section. Refer [here](https://www.chargebee.com/docs/2.0/api_keys.html) for more details.
|
|
@@ -63,6 +83,34 @@ const chargebee = new Chargebee({
|
|
|
63
83
|
});
|
|
64
84
|
```
|
|
65
85
|
|
|
86
|
+
### Using Deno:
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import Chargebee from "npm:chargebee";
|
|
90
|
+
|
|
91
|
+
const chargebee = new Chargebee({
|
|
92
|
+
site: "{{site}}",
|
|
93
|
+
apiKey: "{{api-key}}",
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const response = await chargebee.customer.list();
|
|
97
|
+
console.log(response);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Using Bun:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import Chargebee from "chargebee";
|
|
104
|
+
|
|
105
|
+
const chargebee = new Chargebee({
|
|
106
|
+
site: "{{site}}",
|
|
107
|
+
apiKey: "{{api-key}}",
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const response = await chargebee.customer.list();
|
|
111
|
+
console.log(response);
|
|
112
|
+
```
|
|
113
|
+
|
|
66
114
|
### Using Async / Await
|
|
67
115
|
|
|
68
116
|
```typescript
|
|
@@ -159,6 +207,7 @@ The simplest way to handle webhooks is using the `webhooks` property on your ini
|
|
|
159
207
|
```typescript
|
|
160
208
|
import express from 'express';
|
|
161
209
|
import Chargebee, {
|
|
210
|
+
WebhookEventType,
|
|
162
211
|
AuthenticationError,
|
|
163
212
|
PayloadValidationError,
|
|
164
213
|
PayloadParseError,
|
|
@@ -173,7 +222,7 @@ const app = express();
|
|
|
173
222
|
app.use(express.json());
|
|
174
223
|
|
|
175
224
|
// ⚠️ Register listeners once at startup, not inside request handlers
|
|
176
|
-
chargebee.webhooks.on(
|
|
225
|
+
chargebee.webhooks.on(WebhookEventType.SubscriptionCreated, async ({ event, response }) => {
|
|
177
226
|
console.log(`Subscription created: ${event.id}`);
|
|
178
227
|
const subscription = event.content.subscription;
|
|
179
228
|
console.log(`Customer: ${subscription.customer_id}`);
|
|
@@ -217,6 +266,7 @@ For more control or multiple webhook endpoints, use `chargebee.webhooks.createHa
|
|
|
217
266
|
```typescript
|
|
218
267
|
import express, { Request, Response } from 'express';
|
|
219
268
|
import Chargebee, {
|
|
269
|
+
WebhookEventType,
|
|
220
270
|
basicAuthValidator,
|
|
221
271
|
AuthenticationError,
|
|
222
272
|
PayloadValidationError,
|
|
@@ -240,7 +290,7 @@ handler.requestValidator = basicAuthValidator((username, password) => {
|
|
|
240
290
|
});
|
|
241
291
|
|
|
242
292
|
// ⚠️ Register event listeners once at startup, not inside request handlers
|
|
243
|
-
handler.on(
|
|
293
|
+
handler.on(WebhookEventType.SubscriptionCreated, async ({ event, response }) => {
|
|
244
294
|
console.log(`Subscription created: ${event.id}`);
|
|
245
295
|
const subscription = event.content.subscription;
|
|
246
296
|
console.log(`Customer: ${subscription.customer_id}`);
|
|
@@ -248,7 +298,7 @@ handler.on('subscription_created', async ({ event, response }) => {
|
|
|
248
298
|
response?.status(200).send('OK');
|
|
249
299
|
});
|
|
250
300
|
|
|
251
|
-
handler.on(
|
|
301
|
+
handler.on(WebhookEventType.PaymentSucceeded, async ({ event, response }) => {
|
|
252
302
|
console.log(`Payment succeeded: ${event.id}`);
|
|
253
303
|
const transaction = event.content.transaction;
|
|
254
304
|
const customer = event.content.customer;
|
|
@@ -285,7 +335,7 @@ For more control, you can parse webhook events manually:
|
|
|
285
335
|
|
|
286
336
|
```typescript
|
|
287
337
|
import express from 'express';
|
|
288
|
-
import Chargebee, { type WebhookEvent } from 'chargebee';
|
|
338
|
+
import Chargebee, { type WebhookEvent, WebhookEventType } from 'chargebee';
|
|
289
339
|
|
|
290
340
|
const app = express();
|
|
291
341
|
app.use(express.json());
|
|
@@ -293,23 +343,27 @@ app.use(express.json());
|
|
|
293
343
|
app.post('/chargebee/webhooks', async (req, res) => {
|
|
294
344
|
try {
|
|
295
345
|
const event = req.body as WebhookEvent;
|
|
296
|
-
|
|
346
|
+
|
|
297
347
|
switch (event.event_type) {
|
|
298
|
-
case
|
|
299
|
-
//
|
|
300
|
-
const
|
|
348
|
+
case WebhookEventType.SubscriptionCreated: {
|
|
349
|
+
// Cast to specific event type for proper content typing
|
|
350
|
+
const typedEvent = event as WebhookEvent<WebhookEventType.SubscriptionCreated>;
|
|
351
|
+
const subscription = typedEvent.content.subscription;
|
|
301
352
|
console.log('Subscription created:', subscription.id);
|
|
302
353
|
break;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
case WebhookEventType.PaymentSucceeded: {
|
|
357
|
+
const typedEvent = event as WebhookEvent<WebhookEventType.PaymentSucceeded>;
|
|
358
|
+
const transaction = typedEvent.content.transaction;
|
|
306
359
|
console.log('Payment succeeded:', transaction.amount);
|
|
307
360
|
break;
|
|
308
|
-
|
|
361
|
+
}
|
|
362
|
+
|
|
309
363
|
default:
|
|
310
364
|
console.log('Unhandled event type:', event.event_type);
|
|
311
365
|
}
|
|
312
|
-
|
|
366
|
+
|
|
313
367
|
res.status(200).send('OK');
|
|
314
368
|
} catch (err) {
|
|
315
369
|
console.error('Error processing webhook:', err);
|
|
@@ -327,7 +381,7 @@ app.listen(8080);
|
|
|
327
381
|
**Respond with 200** to acknowledge receipt:
|
|
328
382
|
|
|
329
383
|
```typescript
|
|
330
|
-
handler.on(
|
|
384
|
+
handler.on(WebhookEventType.SubscriptionCreated, async ({ event, response }) => {
|
|
331
385
|
await provisionAccess(event.content.subscription);
|
|
332
386
|
response?.status(200).json({ received: true });
|
|
333
387
|
});
|
|
@@ -336,7 +390,7 @@ handler.on('subscription_created', async ({ event, response }) => {
|
|
|
336
390
|
**Respond with 5xx** so Chargebee retries on failure:
|
|
337
391
|
|
|
338
392
|
```typescript
|
|
339
|
-
handler.on(
|
|
393
|
+
handler.on(WebhookEventType.PaymentSucceeded, async ({ event, response }) => {
|
|
340
394
|
try {
|
|
341
395
|
await recordPayment(event.content.transaction);
|
|
342
396
|
response?.status(200).send('OK');
|
|
@@ -349,7 +403,7 @@ handler.on('payment_succeeded', async ({ event, response }) => {
|
|
|
349
403
|
**Access request context** (headers, middleware data):
|
|
350
404
|
|
|
351
405
|
```typescript
|
|
352
|
-
handler.on(
|
|
406
|
+
handler.on(WebhookEventType.CustomerCreated, async ({ event, request, response }) => {
|
|
353
407
|
const tenantId = (request as any)?.tenant?.id;
|
|
354
408
|
await createCustomerForTenant(tenantId, event.content.customer);
|
|
355
409
|
response?.status(200).send('OK');
|
package/cjs/createChargebee.js
CHANGED
|
@@ -18,6 +18,9 @@ const CreateChargebee = (httpClient) => {
|
|
|
18
18
|
this._endpoints = api_endpoints_js_1.Endpoints;
|
|
19
19
|
// Initialize webhooks handler with auto-configured Basic Auth (if env vars are set)
|
|
20
20
|
const handler = (0, handler_js_1.createDefaultHandler)();
|
|
21
|
+
this.__clientIdentifier = (serviceName) => {
|
|
22
|
+
(0, util_js_1.extend)(true, this._env, { userAgentSuffix: serviceName });
|
|
23
|
+
};
|
|
21
24
|
// Create webhooks namespace with handler methods + createHandler factory
|
|
22
25
|
this.webhooks = Object.assign(handler, {
|
|
23
26
|
createHandler(options) {
|
package/cjs/environment.js
CHANGED
|
@@ -11,7 +11,7 @@ exports.Environment = {
|
|
|
11
11
|
hostSuffix: '.chargebee.com',
|
|
12
12
|
apiPath: '/api/v2',
|
|
13
13
|
timeout: DEFAULT_TIME_OUT,
|
|
14
|
-
clientVersion: 'v3.21.
|
|
14
|
+
clientVersion: 'v3.21.1',
|
|
15
15
|
port: DEFAULT_PORT,
|
|
16
16
|
timemachineWaitInMillis: DEFAULT_TIME_MACHINE_WAIT,
|
|
17
17
|
exportWaitInMillis: DEFAULT_EXPORT_WAIT,
|
package/esm/createChargebee.js
CHANGED
|
@@ -15,6 +15,9 @@ export const CreateChargebee = (httpClient) => {
|
|
|
15
15
|
this._endpoints = Endpoints;
|
|
16
16
|
// Initialize webhooks handler with auto-configured Basic Auth (if env vars are set)
|
|
17
17
|
const handler = createDefaultHandler();
|
|
18
|
+
this.__clientIdentifier = (serviceName) => {
|
|
19
|
+
extend(true, this._env, { userAgentSuffix: serviceName });
|
|
20
|
+
};
|
|
18
21
|
// Create webhooks namespace with handler methods + createHandler factory
|
|
19
22
|
this.webhooks = Object.assign(handler, {
|
|
20
23
|
createHandler(options) {
|
package/esm/environment.js
CHANGED
|
@@ -8,7 +8,7 @@ export const Environment = {
|
|
|
8
8
|
hostSuffix: '.chargebee.com',
|
|
9
9
|
apiPath: '/api/v2',
|
|
10
10
|
timeout: DEFAULT_TIME_OUT,
|
|
11
|
-
clientVersion: 'v3.21.
|
|
11
|
+
clientVersion: 'v3.21.1',
|
|
12
12
|
port: DEFAULT_PORT,
|
|
13
13
|
timemachineWaitInMillis: DEFAULT_TIME_MACHINE_WAIT,
|
|
14
14
|
exportWaitInMillis: DEFAULT_EXPORT_WAIT,
|