@drawbridge/drawbridge-stripe 0.0.3

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.
@@ -0,0 +1,23 @@
1
+ const lib = require( './lib' );
2
+
3
+ module.exports = ( apiKey ) => {
4
+
5
+ const stripe = require( 'stripe' )(
6
+ apiKey,
7
+ {
8
+ apiVersion : '2026-01-28.preview'
9
+ }
10
+ );
11
+
12
+ return {
13
+ billing : lib.billing({
14
+ apiKey,
15
+ stripe
16
+ }),
17
+ stripe,
18
+ subscription : lib.subscription({
19
+ stripe
20
+ })
21
+ }
22
+
23
+ };
@@ -0,0 +1,23 @@
1
+ const lib = require( './lib' );
2
+
3
+ module.exports = ( apiKey ) => {
4
+
5
+ const stripe = require( 'stripe' )(
6
+ apiKey,
7
+ {
8
+ apiVersion : '2026-01-28.preview'
9
+ }
10
+ );
11
+
12
+ return {
13
+ billing : lib.billing({
14
+ apiKey,
15
+ stripe
16
+ }),
17
+ stripe,
18
+ subscription : lib.subscription({
19
+ stripe
20
+ })
21
+ }
22
+
23
+ };
package/dist/index.js ADDED
@@ -0,0 +1,565 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
+ };
5
+
6
+ // lib/billing.js
7
+ var require_billing = __commonJS({
8
+ "lib/billing.js"(exports2, module2) {
9
+ var axios = require("axios");
10
+ var billing = "https://api.stripe.com/v2/billing";
11
+ module2.exports = ({
12
+ apiKey,
13
+ stripe
14
+ }) => {
15
+ const options = {
16
+ headers: {
17
+ "Authorization": "Bearer " + apiKey,
18
+ "Content-Type": "application/json",
19
+ "Stripe-Version": "2026-01-28.preview"
20
+ }
21
+ };
22
+ const subscribe = async ({
23
+ currency: currency2,
24
+ customer,
25
+ default_payment_method,
26
+ metadata = {}
27
+ }) => {
28
+ var _a, _b, _c, _d;
29
+ try {
30
+ const { data: intent } = await axios.post(
31
+ billing + "/intents",
32
+ {
33
+ currency: currency2,
34
+ cadence_data: {
35
+ billing_cycle: {
36
+ type: "month",
37
+ interval_count: 1,
38
+ month: {
39
+ day_of_month: 1
40
+ }
41
+ },
42
+ payer: {
43
+ billing_profile_data: {
44
+ customer,
45
+ default_payment_method
46
+ }
47
+ }
48
+ },
49
+ actions: [
50
+ {
51
+ type: "subscribe",
52
+ subscribe: {
53
+ type: "pricing_plan_subscription_details",
54
+ pricing_plan_subscription_details: {
55
+ component_configurations: [],
56
+ pricing_plan: process.env.STRIPE_PRICING_AI_PLAN_ID,
57
+ pricing_plan_version: process.env.STRIPE_PRICING_AI_PLAN_VERSION,
58
+ metadata
59
+ }
60
+ }
61
+ }
62
+ ]
63
+ },
64
+ options
65
+ );
66
+ const { data: reserve } = await axios.post(
67
+ billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
68
+ {},
69
+ options
70
+ );
71
+ const { data: commit } = await axios.post(
72
+ billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
73
+ {},
74
+ options
75
+ );
76
+ const { data: actions } = await axios.get(
77
+ billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
78
+ options
79
+ );
80
+ const replenish = await stripe.billing.alerts.create({
81
+ alert_type: "credit_balance_threshold",
82
+ credit_balance_threshold: {
83
+ filters: [
84
+ {
85
+ credit_grants: {
86
+ applicability_config: {
87
+ scope: {
88
+ price_type: "metered"
89
+ }
90
+ }
91
+ },
92
+ customer,
93
+ type: "customer"
94
+ }
95
+ ],
96
+ lte: {
97
+ balance_type: "monetary",
98
+ monetary: {
99
+ currency: currency2,
100
+ value: 500
101
+ // cents
102
+ }
103
+ }
104
+ },
105
+ title: customer + ".credit_balance_replenish"
106
+ });
107
+ const depleted = await stripe.billing.alerts.create({
108
+ alert_type: "credit_balance_threshold",
109
+ credit_balance_threshold: {
110
+ filters: [
111
+ {
112
+ credit_grants: {
113
+ applicability_config: {
114
+ scope: {
115
+ price_type: "metered"
116
+ }
117
+ }
118
+ },
119
+ customer,
120
+ type: "customer"
121
+ }
122
+ ],
123
+ lte: {
124
+ balance_type: "monetary",
125
+ monetary: {
126
+ currency: currency2,
127
+ value: 1
128
+ // cents
129
+ }
130
+ }
131
+ },
132
+ title: customer + ".credit_balance_depleted"
133
+ });
134
+ const stripePricingPlanSubscriptionId = (_d = (_c = (_b = (_a = actions == null ? void 0 : actions.data) == null ? void 0 : _a[0]) == null ? void 0 : _b.subscribe) == null ? void 0 : _c.pricing_plan_subscription_details) == null ? void 0 : _d.pricing_plan_subscription;
135
+ return {
136
+ stripeAlerts: {
137
+ ai: {
138
+ depleted: depleted == null ? void 0 : depleted.id,
139
+ replenish: replenish == null ? void 0 : replenish.id
140
+ }
141
+ },
142
+ stripePricingPlanSubscriptionId
143
+ };
144
+ } catch (error) {
145
+ throw error;
146
+ }
147
+ };
148
+ const unsubscribe = async (id) => {
149
+ try {
150
+ const { data } = await axios.post(
151
+ billing + "/intents",
152
+ {
153
+ currency,
154
+ actions: [
155
+ {
156
+ type: "deactivate",
157
+ deactivate: {
158
+ type: "pricing_plan_subscription_details",
159
+ pricing_plan_subscription_details: {
160
+ pricing_plan_subscription: id
161
+ }
162
+ }
163
+ }
164
+ ]
165
+ },
166
+ options
167
+ );
168
+ await axios.post(
169
+ billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
170
+ {},
171
+ options
172
+ );
173
+ const commit = await axios.post(
174
+ billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
175
+ {},
176
+ options
177
+ );
178
+ return commit;
179
+ } catch (error) {
180
+ throw error;
181
+ }
182
+ };
183
+ const usage = ({
184
+ customer,
185
+ metadata,
186
+ ...rest
187
+ }) => {
188
+ const event_name = "token-billing-tokens";
189
+ const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
190
+ const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
191
+ const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
192
+ if (input) {
193
+ stripe.billing.meterEvents.create({
194
+ event_name,
195
+ payload: {
196
+ model,
197
+ stripe_customer_id: customer,
198
+ token_type: "input",
199
+ value: input
200
+ }
201
+ });
202
+ }
203
+ ;
204
+ if (output) {
205
+ stripe.billing.meterEvents.create({
206
+ event_name,
207
+ payload: {
208
+ model,
209
+ stripe_customer_id: customer,
210
+ token_type: "output",
211
+ value: output
212
+ }
213
+ });
214
+ }
215
+ ;
216
+ };
217
+ return {
218
+ subscribe,
219
+ unsubscribe,
220
+ usage
221
+ };
222
+ };
223
+ }
224
+ });
225
+
226
+ // lib/subscription.js
227
+ var require_subscription = __commonJS({
228
+ "lib/subscription.js"(exports2, module2) {
229
+ module2.exports = ({
230
+ stripe
231
+ }) => {
232
+ const createMeter = async ({
233
+ metadata,
234
+ name
235
+ }) => {
236
+ const usage = (metadata == null ? void 0 : metadata.organization) + "_usage_" + name;
237
+ const meter = await stripe.billing.meters.create({
238
+ default_aggregation: {
239
+ formula: "sum"
240
+ },
241
+ display_name: usage,
242
+ event_name: usage
243
+ });
244
+ return {
245
+ meter: meter == null ? void 0 : meter.id,
246
+ usage
247
+ };
248
+ };
249
+ const createTier = async ({
250
+ currency: currency2,
251
+ interval,
252
+ limit,
253
+ metadata,
254
+ meter,
255
+ name,
256
+ overage
257
+ }) => {
258
+ const response = {};
259
+ const product = await stripe.products.create({
260
+ metadata,
261
+ name
262
+ });
263
+ response["product"] = product == null ? void 0 : product.id;
264
+ const price = await stripe.prices.create({
265
+ billing_scheme: "tiered",
266
+ currency: currency2,
267
+ metadata,
268
+ nickname: name,
269
+ product: product == null ? void 0 : product.id,
270
+ recurring: {
271
+ interval,
272
+ meter,
273
+ usage_type: "metered"
274
+ },
275
+ tax_behavior: "exclusive",
276
+ tiers: [
277
+ {
278
+ flat_amount: "0",
279
+ // included in the plan. Should not be a value.
280
+ up_to: limit
281
+ },
282
+ {
283
+ unit_amount_decimal: overage,
284
+ up_to: "inf"
285
+ }
286
+ ],
287
+ tiers_mode: "graduated"
288
+ });
289
+ response["price"] = price == null ? void 0 : price.id;
290
+ return response;
291
+ ;
292
+ };
293
+ const createItem = async ({
294
+ currency: currency2,
295
+ interval,
296
+ limit,
297
+ metadata,
298
+ name,
299
+ overage,
300
+ proration_behavior,
301
+ subscription
302
+ }) => {
303
+ const response = {};
304
+ const { meter, usage } = await createMeter({
305
+ metadata,
306
+ name
307
+ });
308
+ response["meter"] = meter;
309
+ response["usage"] = usage;
310
+ const { price, product } = await createTier({
311
+ currency: currency2,
312
+ interval,
313
+ limit,
314
+ metadata,
315
+ meter,
316
+ name,
317
+ overage
318
+ });
319
+ response["price"] = price;
320
+ response["product"] = product;
321
+ if (subscription) {
322
+ const { id } = await stripe.subscriptionItems.create({
323
+ metadata,
324
+ price,
325
+ proration_behavior,
326
+ subscription
327
+ });
328
+ response["id"] = id;
329
+ }
330
+ ;
331
+ return response;
332
+ };
333
+ return {
334
+ change: async ({
335
+ items,
336
+ metadata,
337
+ stripeSubscriptionId,
338
+ to
339
+ }) => {
340
+ var _a, _b, _c, _d;
341
+ try {
342
+ const props = {
343
+ metadata
344
+ };
345
+ const plan = await stripe.subscriptionItems.update(
346
+ (_a = items == null ? void 0 : items.plan) == null ? void 0 : _a.id,
347
+ {
348
+ ...props,
349
+ price: (_b = items == null ? void 0 : items.plan) == null ? void 0 : _b.price,
350
+ proration_behavior: "create_prorations"
351
+ }
352
+ );
353
+ let entries = (items == null ? void 0 : items.entries) || {};
354
+ if ((entries == null ? void 0 : entries.id) && (entries == null ? void 0 : entries.price)) {
355
+ ({ id: entries["id"] } = await stripe.subscriptionItems.update(
356
+ entries.id,
357
+ {
358
+ ...props,
359
+ price: entries.price,
360
+ proration_behavior: "none"
361
+ }
362
+ ));
363
+ } else {
364
+ entries = await createItem({
365
+ currency: to.currency,
366
+ interval: to.interval,
367
+ limit: to.limits.organization.entries,
368
+ metadata,
369
+ name: "entries",
370
+ overage: to.overages.entries,
371
+ proration_behavior: "none",
372
+ subscription: stripeSubscriptionId
373
+ });
374
+ }
375
+ await stripe.subscriptions.update(
376
+ stripeSubscriptionId,
377
+ {
378
+ metadata
379
+ }
380
+ );
381
+ return {
382
+ items: {
383
+ plan: {
384
+ id: plan.id,
385
+ price: (_c = plan == null ? void 0 : plan.price) == null ? void 0 : _c.id,
386
+ product: (_d = plan == null ? void 0 : plan.price) == null ? void 0 : _d.product
387
+ },
388
+ entries
389
+ }
390
+ };
391
+ } catch (error) {
392
+ throw error;
393
+ }
394
+ },
395
+ create: async ({
396
+ customer,
397
+ discounts = [],
398
+ metadata,
399
+ stripePaymentMethodId,
400
+ to
401
+ }) => {
402
+ var _a, _b, _c;
403
+ try {
404
+ const entries = await createItem({
405
+ currency: to.currency,
406
+ interval: to.interval,
407
+ limit: to.limits.organization.entries,
408
+ metadata,
409
+ name: "entries",
410
+ overage: to.overages.entries,
411
+ proration_behavior: "none"
412
+ });
413
+ const subscription = await stripe.subscriptions.create({
414
+ automatic_tax: {
415
+ enabled: true
416
+ },
417
+ customer,
418
+ description: "Organization subscription: " + metadata.organization,
419
+ default_payment_method: stripePaymentMethodId,
420
+ discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
421
+ items: [
422
+ {
423
+ price: entries.price
424
+ }
425
+ ],
426
+ metadata
427
+ });
428
+ const { product } = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
429
+ const plan = await stripe.subscriptionItems.create({
430
+ subscription: subscription == null ? void 0 : subscription.id,
431
+ price: to == null ? void 0 : to.stripePriceId,
432
+ quantity: 1,
433
+ proration_behavior: "always_invoice"
434
+ });
435
+ return {
436
+ discounts,
437
+ items: {
438
+ plan: {
439
+ id: plan.id,
440
+ price: to == null ? void 0 : to.stripePriceId,
441
+ product
442
+ },
443
+ entries: {
444
+ id: (_c = (_b = (_a = subscription == null ? void 0 : subscription.items) == null ? void 0 : _a.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.id,
445
+ ...entries
446
+ }
447
+ },
448
+ stripeCustomerId: customer,
449
+ stripeSubscriptionId: subscription.id
450
+ };
451
+ } catch (error) {
452
+ throw error;
453
+ }
454
+ },
455
+ update: async ({
456
+ current,
457
+ metadata,
458
+ stripeSubscriptionId,
459
+ to
460
+ }) => {
461
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
462
+ try {
463
+ const props = {
464
+ metadata
465
+ };
466
+ const plan = await stripe.subscriptionItems.update(
467
+ (_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.plan) == null ? void 0 : _b.id,
468
+ {
469
+ ...props,
470
+ price: to.stripePriceId,
471
+ proration_behavior: "always_invoice"
472
+ }
473
+ );
474
+ let entries;
475
+ if (((_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.entries) == null ? void 0 : _d.id) && ((_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.entries) == null ? void 0 : _f.meter)) {
476
+ entries = await createTier({
477
+ currency: to.currency,
478
+ interval: to.interval,
479
+ limit: to.limits.organization.entries,
480
+ metadata,
481
+ meter: current.items.entries.meter,
482
+ name: "entries",
483
+ overage: to.overages.entries
484
+ });
485
+ ({ id: entries["id"] } = await stripe.subscriptionItems.update(
486
+ current.items.entries.id,
487
+ {
488
+ ...props,
489
+ price: entries == null ? void 0 : entries.price,
490
+ proration_behavior: "none"
491
+ }
492
+ ));
493
+ } else {
494
+ entries = await createItem({
495
+ currency: to.currency,
496
+ interval: to.interval,
497
+ limit: to.limits.organization.entries,
498
+ metadata,
499
+ name: "entries",
500
+ overage: to.overages.entries,
501
+ proration_behavior: "none",
502
+ subscription: stripeSubscriptionId
503
+ });
504
+ }
505
+ ;
506
+ await stripe.subscriptions.update(
507
+ stripeSubscriptionId,
508
+ {
509
+ metadata
510
+ }
511
+ );
512
+ return {
513
+ items: {
514
+ plan: {
515
+ id: plan.id,
516
+ price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
517
+ product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
518
+ },
519
+ entries: {
520
+ ...((_i = current == null ? void 0 : current.items) == null ? void 0 : _i.entries) || {},
521
+ ...entries
522
+ }
523
+ }
524
+ };
525
+ } catch (error) {
526
+ throw error;
527
+ }
528
+ }
529
+ };
530
+ };
531
+ }
532
+ });
533
+
534
+ // lib/index.js
535
+ var require_lib = __commonJS({
536
+ "lib/index.js"(exports2, module2) {
537
+ var billing = require_billing();
538
+ var subscription = require_subscription();
539
+ module2.exports = {
540
+ billing,
541
+ subscription
542
+ };
543
+ }
544
+ });
545
+
546
+ // index.js
547
+ var lib = require_lib();
548
+ module.exports = (apiKey) => {
549
+ const stripe = require("stripe")(
550
+ apiKey,
551
+ {
552
+ apiVersion: "2026-01-28.preview"
553
+ }
554
+ );
555
+ return {
556
+ billing: lib.billing({
557
+ apiKey,
558
+ stripe
559
+ }),
560
+ stripe,
561
+ subscription: lib.subscription({
562
+ stripe
563
+ })
564
+ };
565
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,576 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
+ }) : x)(function(x) {
5
+ if (typeof require !== "undefined") return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+ var __commonJS = (cb, mod) => function __require2() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
11
+
12
+ // lib/billing.js
13
+ var require_billing = __commonJS({
14
+ "lib/billing.js"(exports, module) {
15
+ var axios = __require("axios");
16
+ var billing = "https://api.stripe.com/v2/billing";
17
+ module.exports = ({
18
+ apiKey,
19
+ stripe
20
+ }) => {
21
+ const options = {
22
+ headers: {
23
+ "Authorization": "Bearer " + apiKey,
24
+ "Content-Type": "application/json",
25
+ "Stripe-Version": "2026-01-28.preview"
26
+ }
27
+ };
28
+ const subscribe = async ({
29
+ currency: currency2,
30
+ customer,
31
+ default_payment_method,
32
+ metadata = {}
33
+ }) => {
34
+ var _a, _b, _c, _d;
35
+ try {
36
+ const { data: intent } = await axios.post(
37
+ billing + "/intents",
38
+ {
39
+ currency: currency2,
40
+ cadence_data: {
41
+ billing_cycle: {
42
+ type: "month",
43
+ interval_count: 1,
44
+ month: {
45
+ day_of_month: 1
46
+ }
47
+ },
48
+ payer: {
49
+ billing_profile_data: {
50
+ customer,
51
+ default_payment_method
52
+ }
53
+ }
54
+ },
55
+ actions: [
56
+ {
57
+ type: "subscribe",
58
+ subscribe: {
59
+ type: "pricing_plan_subscription_details",
60
+ pricing_plan_subscription_details: {
61
+ component_configurations: [],
62
+ pricing_plan: process.env.STRIPE_PRICING_AI_PLAN_ID,
63
+ pricing_plan_version: process.env.STRIPE_PRICING_AI_PLAN_VERSION,
64
+ metadata
65
+ }
66
+ }
67
+ }
68
+ ]
69
+ },
70
+ options
71
+ );
72
+ const { data: reserve } = await axios.post(
73
+ billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
74
+ {},
75
+ options
76
+ );
77
+ const { data: commit } = await axios.post(
78
+ billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
79
+ {},
80
+ options
81
+ );
82
+ const { data: actions } = await axios.get(
83
+ billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
84
+ options
85
+ );
86
+ const replenish = await stripe.billing.alerts.create({
87
+ alert_type: "credit_balance_threshold",
88
+ credit_balance_threshold: {
89
+ filters: [
90
+ {
91
+ credit_grants: {
92
+ applicability_config: {
93
+ scope: {
94
+ price_type: "metered"
95
+ }
96
+ }
97
+ },
98
+ customer,
99
+ type: "customer"
100
+ }
101
+ ],
102
+ lte: {
103
+ balance_type: "monetary",
104
+ monetary: {
105
+ currency: currency2,
106
+ value: 500
107
+ // cents
108
+ }
109
+ }
110
+ },
111
+ title: customer + ".credit_balance_replenish"
112
+ });
113
+ const depleted = await stripe.billing.alerts.create({
114
+ alert_type: "credit_balance_threshold",
115
+ credit_balance_threshold: {
116
+ filters: [
117
+ {
118
+ credit_grants: {
119
+ applicability_config: {
120
+ scope: {
121
+ price_type: "metered"
122
+ }
123
+ }
124
+ },
125
+ customer,
126
+ type: "customer"
127
+ }
128
+ ],
129
+ lte: {
130
+ balance_type: "monetary",
131
+ monetary: {
132
+ currency: currency2,
133
+ value: 1
134
+ // cents
135
+ }
136
+ }
137
+ },
138
+ title: customer + ".credit_balance_depleted"
139
+ });
140
+ const stripePricingPlanSubscriptionId = (_d = (_c = (_b = (_a = actions == null ? void 0 : actions.data) == null ? void 0 : _a[0]) == null ? void 0 : _b.subscribe) == null ? void 0 : _c.pricing_plan_subscription_details) == null ? void 0 : _d.pricing_plan_subscription;
141
+ return {
142
+ stripeAlerts: {
143
+ ai: {
144
+ depleted: depleted == null ? void 0 : depleted.id,
145
+ replenish: replenish == null ? void 0 : replenish.id
146
+ }
147
+ },
148
+ stripePricingPlanSubscriptionId
149
+ };
150
+ } catch (error) {
151
+ throw error;
152
+ }
153
+ };
154
+ const unsubscribe = async (id) => {
155
+ try {
156
+ const { data } = await axios.post(
157
+ billing + "/intents",
158
+ {
159
+ currency,
160
+ actions: [
161
+ {
162
+ type: "deactivate",
163
+ deactivate: {
164
+ type: "pricing_plan_subscription_details",
165
+ pricing_plan_subscription_details: {
166
+ pricing_plan_subscription: id
167
+ }
168
+ }
169
+ }
170
+ ]
171
+ },
172
+ options
173
+ );
174
+ await axios.post(
175
+ billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
176
+ {},
177
+ options
178
+ );
179
+ const commit = await axios.post(
180
+ billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
181
+ {},
182
+ options
183
+ );
184
+ return commit;
185
+ } catch (error) {
186
+ throw error;
187
+ }
188
+ };
189
+ const usage = ({
190
+ customer,
191
+ metadata,
192
+ ...rest
193
+ }) => {
194
+ const event_name = "token-billing-tokens";
195
+ const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
196
+ const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
197
+ const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
198
+ if (input) {
199
+ stripe.billing.meterEvents.create({
200
+ event_name,
201
+ payload: {
202
+ model,
203
+ stripe_customer_id: customer,
204
+ token_type: "input",
205
+ value: input
206
+ }
207
+ });
208
+ }
209
+ ;
210
+ if (output) {
211
+ stripe.billing.meterEvents.create({
212
+ event_name,
213
+ payload: {
214
+ model,
215
+ stripe_customer_id: customer,
216
+ token_type: "output",
217
+ value: output
218
+ }
219
+ });
220
+ }
221
+ ;
222
+ };
223
+ return {
224
+ subscribe,
225
+ unsubscribe,
226
+ usage
227
+ };
228
+ };
229
+ }
230
+ });
231
+
232
+ // lib/subscription.js
233
+ var require_subscription = __commonJS({
234
+ "lib/subscription.js"(exports, module) {
235
+ module.exports = ({
236
+ stripe
237
+ }) => {
238
+ const createMeter = async ({
239
+ metadata,
240
+ name
241
+ }) => {
242
+ const usage = (metadata == null ? void 0 : metadata.organization) + "_usage_" + name;
243
+ const meter = await stripe.billing.meters.create({
244
+ default_aggregation: {
245
+ formula: "sum"
246
+ },
247
+ display_name: usage,
248
+ event_name: usage
249
+ });
250
+ return {
251
+ meter: meter == null ? void 0 : meter.id,
252
+ usage
253
+ };
254
+ };
255
+ const createTier = async ({
256
+ currency: currency2,
257
+ interval,
258
+ limit,
259
+ metadata,
260
+ meter,
261
+ name,
262
+ overage
263
+ }) => {
264
+ const response = {};
265
+ const product = await stripe.products.create({
266
+ metadata,
267
+ name
268
+ });
269
+ response["product"] = product == null ? void 0 : product.id;
270
+ const price = await stripe.prices.create({
271
+ billing_scheme: "tiered",
272
+ currency: currency2,
273
+ metadata,
274
+ nickname: name,
275
+ product: product == null ? void 0 : product.id,
276
+ recurring: {
277
+ interval,
278
+ meter,
279
+ usage_type: "metered"
280
+ },
281
+ tax_behavior: "exclusive",
282
+ tiers: [
283
+ {
284
+ flat_amount: "0",
285
+ // included in the plan. Should not be a value.
286
+ up_to: limit
287
+ },
288
+ {
289
+ unit_amount_decimal: overage,
290
+ up_to: "inf"
291
+ }
292
+ ],
293
+ tiers_mode: "graduated"
294
+ });
295
+ response["price"] = price == null ? void 0 : price.id;
296
+ return response;
297
+ ;
298
+ };
299
+ const createItem = async ({
300
+ currency: currency2,
301
+ interval,
302
+ limit,
303
+ metadata,
304
+ name,
305
+ overage,
306
+ proration_behavior,
307
+ subscription
308
+ }) => {
309
+ const response = {};
310
+ const { meter, usage } = await createMeter({
311
+ metadata,
312
+ name
313
+ });
314
+ response["meter"] = meter;
315
+ response["usage"] = usage;
316
+ const { price, product } = await createTier({
317
+ currency: currency2,
318
+ interval,
319
+ limit,
320
+ metadata,
321
+ meter,
322
+ name,
323
+ overage
324
+ });
325
+ response["price"] = price;
326
+ response["product"] = product;
327
+ if (subscription) {
328
+ const { id } = await stripe.subscriptionItems.create({
329
+ metadata,
330
+ price,
331
+ proration_behavior,
332
+ subscription
333
+ });
334
+ response["id"] = id;
335
+ }
336
+ ;
337
+ return response;
338
+ };
339
+ return {
340
+ change: async ({
341
+ items,
342
+ metadata,
343
+ stripeSubscriptionId,
344
+ to
345
+ }) => {
346
+ var _a, _b, _c, _d;
347
+ try {
348
+ const props = {
349
+ metadata
350
+ };
351
+ const plan = await stripe.subscriptionItems.update(
352
+ (_a = items == null ? void 0 : items.plan) == null ? void 0 : _a.id,
353
+ {
354
+ ...props,
355
+ price: (_b = items == null ? void 0 : items.plan) == null ? void 0 : _b.price,
356
+ proration_behavior: "create_prorations"
357
+ }
358
+ );
359
+ let entries = (items == null ? void 0 : items.entries) || {};
360
+ if ((entries == null ? void 0 : entries.id) && (entries == null ? void 0 : entries.price)) {
361
+ ({ id: entries["id"] } = await stripe.subscriptionItems.update(
362
+ entries.id,
363
+ {
364
+ ...props,
365
+ price: entries.price,
366
+ proration_behavior: "none"
367
+ }
368
+ ));
369
+ } else {
370
+ entries = await createItem({
371
+ currency: to.currency,
372
+ interval: to.interval,
373
+ limit: to.limits.organization.entries,
374
+ metadata,
375
+ name: "entries",
376
+ overage: to.overages.entries,
377
+ proration_behavior: "none",
378
+ subscription: stripeSubscriptionId
379
+ });
380
+ }
381
+ await stripe.subscriptions.update(
382
+ stripeSubscriptionId,
383
+ {
384
+ metadata
385
+ }
386
+ );
387
+ return {
388
+ items: {
389
+ plan: {
390
+ id: plan.id,
391
+ price: (_c = plan == null ? void 0 : plan.price) == null ? void 0 : _c.id,
392
+ product: (_d = plan == null ? void 0 : plan.price) == null ? void 0 : _d.product
393
+ },
394
+ entries
395
+ }
396
+ };
397
+ } catch (error) {
398
+ throw error;
399
+ }
400
+ },
401
+ create: async ({
402
+ customer,
403
+ discounts = [],
404
+ metadata,
405
+ stripePaymentMethodId,
406
+ to
407
+ }) => {
408
+ var _a, _b, _c;
409
+ try {
410
+ const entries = await createItem({
411
+ currency: to.currency,
412
+ interval: to.interval,
413
+ limit: to.limits.organization.entries,
414
+ metadata,
415
+ name: "entries",
416
+ overage: to.overages.entries,
417
+ proration_behavior: "none"
418
+ });
419
+ const subscription = await stripe.subscriptions.create({
420
+ automatic_tax: {
421
+ enabled: true
422
+ },
423
+ customer,
424
+ description: "Organization subscription: " + metadata.organization,
425
+ default_payment_method: stripePaymentMethodId,
426
+ discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
427
+ items: [
428
+ {
429
+ price: entries.price
430
+ }
431
+ ],
432
+ metadata
433
+ });
434
+ const { product } = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
435
+ const plan = await stripe.subscriptionItems.create({
436
+ subscription: subscription == null ? void 0 : subscription.id,
437
+ price: to == null ? void 0 : to.stripePriceId,
438
+ quantity: 1,
439
+ proration_behavior: "always_invoice"
440
+ });
441
+ return {
442
+ discounts,
443
+ items: {
444
+ plan: {
445
+ id: plan.id,
446
+ price: to == null ? void 0 : to.stripePriceId,
447
+ product
448
+ },
449
+ entries: {
450
+ id: (_c = (_b = (_a = subscription == null ? void 0 : subscription.items) == null ? void 0 : _a.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.id,
451
+ ...entries
452
+ }
453
+ },
454
+ stripeCustomerId: customer,
455
+ stripeSubscriptionId: subscription.id
456
+ };
457
+ } catch (error) {
458
+ throw error;
459
+ }
460
+ },
461
+ update: async ({
462
+ current,
463
+ metadata,
464
+ stripeSubscriptionId,
465
+ to
466
+ }) => {
467
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
468
+ try {
469
+ const props = {
470
+ metadata
471
+ };
472
+ const plan = await stripe.subscriptionItems.update(
473
+ (_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.plan) == null ? void 0 : _b.id,
474
+ {
475
+ ...props,
476
+ price: to.stripePriceId,
477
+ proration_behavior: "always_invoice"
478
+ }
479
+ );
480
+ let entries;
481
+ if (((_d = (_c = current == null ? void 0 : current.items) == null ? void 0 : _c.entries) == null ? void 0 : _d.id) && ((_f = (_e = current == null ? void 0 : current.items) == null ? void 0 : _e.entries) == null ? void 0 : _f.meter)) {
482
+ entries = await createTier({
483
+ currency: to.currency,
484
+ interval: to.interval,
485
+ limit: to.limits.organization.entries,
486
+ metadata,
487
+ meter: current.items.entries.meter,
488
+ name: "entries",
489
+ overage: to.overages.entries
490
+ });
491
+ ({ id: entries["id"] } = await stripe.subscriptionItems.update(
492
+ current.items.entries.id,
493
+ {
494
+ ...props,
495
+ price: entries == null ? void 0 : entries.price,
496
+ proration_behavior: "none"
497
+ }
498
+ ));
499
+ } else {
500
+ entries = await createItem({
501
+ currency: to.currency,
502
+ interval: to.interval,
503
+ limit: to.limits.organization.entries,
504
+ metadata,
505
+ name: "entries",
506
+ overage: to.overages.entries,
507
+ proration_behavior: "none",
508
+ subscription: stripeSubscriptionId
509
+ });
510
+ }
511
+ ;
512
+ await stripe.subscriptions.update(
513
+ stripeSubscriptionId,
514
+ {
515
+ metadata
516
+ }
517
+ );
518
+ return {
519
+ items: {
520
+ plan: {
521
+ id: plan.id,
522
+ price: (_g = plan == null ? void 0 : plan.price) == null ? void 0 : _g.id,
523
+ product: (_h = plan == null ? void 0 : plan.price) == null ? void 0 : _h.product
524
+ },
525
+ entries: {
526
+ ...((_i = current == null ? void 0 : current.items) == null ? void 0 : _i.entries) || {},
527
+ ...entries
528
+ }
529
+ }
530
+ };
531
+ } catch (error) {
532
+ throw error;
533
+ }
534
+ }
535
+ };
536
+ };
537
+ }
538
+ });
539
+
540
+ // lib/index.js
541
+ var require_lib = __commonJS({
542
+ "lib/index.js"(exports, module) {
543
+ var billing = require_billing();
544
+ var subscription = require_subscription();
545
+ module.exports = {
546
+ billing,
547
+ subscription
548
+ };
549
+ }
550
+ });
551
+
552
+ // index.js
553
+ var require_index = __commonJS({
554
+ "index.js"(exports, module) {
555
+ var lib = require_lib();
556
+ module.exports = (apiKey) => {
557
+ const stripe = __require("stripe")(
558
+ apiKey,
559
+ {
560
+ apiVersion: "2026-01-28.preview"
561
+ }
562
+ );
563
+ return {
564
+ billing: lib.billing({
565
+ apiKey,
566
+ stripe
567
+ }),
568
+ stripe,
569
+ subscription: lib.subscription({
570
+ stripe
571
+ })
572
+ };
573
+ };
574
+ }
575
+ });
576
+ export default require_index();
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "dependencies": {
3
+ "axios": "^1.13.6",
4
+ "stripe": "^20.4.0",
5
+ "tsup": "^8.5.1",
6
+ "typescript": "^5.9.3"
7
+ },
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "license": "ISC",
18
+ "main": "dist/index.js",
19
+ "name": "@drawbridge/drawbridge-stripe",
20
+ "publishConfig": {
21
+ "registry": "https://registry.npmjs.org/"
22
+ },
23
+ "scripts": {
24
+ "build": "tsup ./index.js"
25
+ },
26
+ "types": "dist/index.d.ts",
27
+ "version": "0.0.3"
28
+ }