@openaisdk/billing-sdk-node 1.3.0 → 1.11.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.
@@ -2,10 +2,11 @@ import { MegaBilling } from '../src/index.js';
2
2
 
3
3
  const apiKey = process.env.BILLING_API_KEY;
4
4
  const appUrl = process.env.APP_URL ?? 'http://localhost:3000';
5
- const workspaceId = process.env.WORKSPACE_ID ?? 'ws_quickstart';
5
+ const workspaceId = process.env.WORKSPACE_ID ?? `ws_quickstart_${Date.now()}`;
6
6
  const workspaceEmail = process.env.WORKSPACE_EMAIL ?? 'owner@example.com';
7
7
  const workspaceName = process.env.WORKSPACE_NAME ?? 'Quickstart Workspace';
8
- const priceId = process.env.BILLING_PRICE_ID ?? 'price_pro_monthly';
8
+ const priceId = process.env.BILLING_PRICE_ID;
9
+ const upgradePriceId = process.env.BILLING_UPGRADE_PRICE_ID;
9
10
 
10
11
  if (!apiKey) {
11
12
  throw new Error('Set BILLING_API_KEY before running the quickstart');
@@ -15,6 +16,25 @@ const billing = new MegaBilling({
15
16
  apiKey,
16
17
  });
17
18
 
19
+ const products = await billing.products.list({ active: true, limit: 20 });
20
+ const firstProduct = products.data[0];
21
+ if (!firstProduct) {
22
+ throw new Error('No public products in project — seed catalog first');
23
+ }
24
+
25
+ const prices = await billing.prices.list({
26
+ product: firstProduct.id,
27
+ active: true,
28
+ limit: 20,
29
+ });
30
+ const resolvedPriceId = priceId ?? prices.data[0]?.id;
31
+ if (!resolvedPriceId) {
32
+ throw new Error('No active price for product — set BILLING_PRICE_ID or seed prices');
33
+ }
34
+
35
+ const features = await billing.features.list({ limit: 20 });
36
+ const productFeatures = await billing.products.features(firstProduct.id, { limit: 20 });
37
+
18
38
  const customer = await billing.customers.create(
19
39
  {
20
40
  externalType: 'workspace',
@@ -30,29 +50,52 @@ const customer = await billing.customers.create(
30
50
  const checkout = await billing.checkout.sessions.create(
31
51
  {
32
52
  customer: customer.id,
33
- price: priceId,
53
+ price: resolvedPriceId,
34
54
  successUrl: `${appUrl}/billing/success`,
35
55
  cancelUrl: `${appUrl}/billing`,
36
56
  },
37
57
  {
38
- idempotencyKey: `checkout:${workspaceId}:${priceId}:v1`,
58
+ idempotencyKey: `checkout:${workspaceId}:${resolvedPriceId}:v1`,
39
59
  }
40
60
  );
41
61
 
42
62
  const access = await billing.access.retrieve(customer.id);
43
63
 
64
+ let changePreview = null;
65
+ let change = null;
66
+ if (upgradePriceId && checkout.subscription) {
67
+ changePreview = await billing.subscriptions.previewChange(checkout.subscription, {
68
+ price: upgradePriceId,
69
+ effectivePolicy: 'IMMEDIATE',
70
+ });
71
+ change = await billing.subscriptions.change(
72
+ checkout.subscription,
73
+ {
74
+ preview: changePreview.id,
75
+ expectedTotalMinor: changePreview.dueNowMinor,
76
+ successUrl: `${appUrl}/billing/success`,
77
+ cancelUrl: `${appUrl}/billing`,
78
+ },
79
+ { idempotencyKey: `change:${checkout.subscription}:v1` }
80
+ );
81
+ }
82
+
44
83
  console.log(
45
84
  JSON.stringify(
46
85
  {
86
+ productId: firstProduct.id,
87
+ priceId: resolvedPriceId,
88
+ featureCount: features.data.length,
89
+ productFeatureCount: productFeatures.data.length,
47
90
  customerId: customer.id,
48
91
  customerLivemode: customer.livemode,
49
- customerRequestId: customer.requestId,
50
92
  confirmationUrl: checkout.confirmationUrl,
51
- checkoutLivemode: checkout.livemode,
52
- checkoutRequestId: checkout.requestId,
93
+ subscriptionId: checkout.subscription,
53
94
  accessStatus: access.status,
54
95
  accessLivemode: access.livemode,
55
- accessRequestId: access.requestId,
96
+ changePreviewId: changePreview?.id ?? null,
97
+ changeId: change?.id ?? null,
98
+ changeConfirmationUrl: change?.confirmationUrl ?? null,
56
99
  },
57
100
  null,
58
101
  2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openaisdk/billing-sdk-node",
3
- "version": "1.3.0",
3
+ "version": "1.11.0",
4
4
  "description": "Handwritten public Node SDK for Mega-Billing Wave 1 API",
5
5
  "license": "MIT",
6
6
  "author": "Anatoliy Tukov <openaisdk@gmail.com>",
@@ -25,6 +25,9 @@
25
25
  "type": "module",
26
26
  "main": "dist/index.js",
27
27
  "types": "dist/index.d.ts",
28
+ "bin": {
29
+ "billing-sdk-webhook-forward": "./bin/billing-sdk-webhook-forward.mjs"
30
+ },
28
31
  "exports": {
29
32
  ".": {
30
33
  "types": "./dist/index.d.ts",
@@ -34,6 +37,7 @@
34
37
  "sideEffects": false,
35
38
  "files": [
36
39
  "dist",
40
+ "bin",
37
41
  "README.md",
38
42
  "examples/quickstart.ts"
39
43
  ],
@@ -49,6 +53,7 @@
49
53
  "lint": "eslint .",
50
54
  "test": "pnpm run build && node --test ./test/**/*.test.mjs",
51
55
  "quickstart": "tsx ./examples/quickstart.ts",
56
+ "webhook-forward": "node ./bin/billing-sdk-webhook-forward.mjs",
52
57
  "prepack": "pnpm run test"
53
58
  },
54
59
  "devDependencies": {