@drawbridge/drawbridge-stripe 0.1.17 → 0.1.19

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.
Files changed (3) hide show
  1. package/dist/index.js +245 -243
  2. package/dist/index.mjs +245 -243
  3. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -6,26 +6,20 @@ var __commonJS = (cb, mod) => function __require() {
6
6
  // lib/billing.js
7
7
  var require_billing = __commonJS({
8
8
  "lib/billing.js"(exports2, module2) {
9
- var axios = require("axios");
9
+ var { request } = require("@drawbridge/drawbridge-utils/http");
10
10
  var billing = "https://api.stripe.com/v2/billing";
11
11
  module2.exports = ({
12
12
  stripe: stripe2
13
13
  }) => {
14
- const options = {
15
- headers: {
16
- "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
17
- "Content-Type": "application/json",
18
- "Stripe-Version": "2026-01-28.preview"
19
- }
14
+ const headers = {
15
+ "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
16
+ "Stripe-Version": "2026-01-28.preview"
20
17
  };
21
18
  const withIdem = (base, suffix) => {
22
- if (!base) return options;
19
+ if (!base) return headers;
23
20
  return {
24
- ...options,
25
- headers: {
26
- ...options.headers,
27
- "Idempotency-Key": base + ":" + suffix
28
- }
21
+ ...headers,
22
+ "Idempotency-Key": base + ":" + suffix
29
23
  };
30
24
  };
31
25
  const idem = (base, suffix) => {
@@ -36,38 +30,34 @@ var require_billing = __commonJS({
36
30
  };
37
31
  const billableItems = async () => {
38
32
  var _a;
39
- try {
40
- const { data: components } = await axios.get(
41
- billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
42
- options
43
- );
44
- const rateCardIds = ((components == null ? void 0 : components.data) || []).map((plan) => {
45
- var _a2;
46
- return (_a2 = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _a2.id;
47
- }).filter(Boolean);
48
- const rateResults = await Promise.all(
49
- rateCardIds.map((id) => axios.get(
50
- billing + "/rate_cards/" + id + "/rates",
51
- options
52
- ))
53
- );
54
- const items = [];
55
- for (const { data: rates } of rateResults) {
56
- for (const element of (rates == null ? void 0 : rates.data) || []) {
57
- if ((_a = element == null ? void 0 : element.metered_item) == null ? void 0 : _a.id) {
58
- items.push({
59
- id: element.metered_item.id
60
- });
61
- }
62
- ;
33
+ const components = await request({
34
+ url: billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
35
+ headers
36
+ });
37
+ const rateCardIds = ((components == null ? void 0 : components.data) || []).map((plan) => {
38
+ var _a2;
39
+ return (_a2 = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _a2.id;
40
+ }).filter(Boolean);
41
+ const rateResults = await Promise.all(
42
+ rateCardIds.map((id) => request({
43
+ url: billing + "/rate_cards/" + id + "/rates",
44
+ headers
45
+ }))
46
+ );
47
+ const items = [];
48
+ for (const rates of rateResults) {
49
+ for (const element of (rates == null ? void 0 : rates.data) || []) {
50
+ if ((_a = element == null ? void 0 : element.metered_item) == null ? void 0 : _a.id) {
51
+ items.push({
52
+ id: element.metered_item.id
53
+ });
63
54
  }
64
55
  ;
65
56
  }
66
57
  ;
67
- return items;
68
- } catch (error) {
69
- throw error;
70
58
  }
59
+ ;
60
+ return items;
71
61
  };
72
62
  const subscribe = async ({
73
63
  currency,
@@ -77,183 +67,182 @@ var require_billing = __commonJS({
77
67
  metadata = {}
78
68
  }) => {
79
69
  var _a, _b, _c, _d;
80
- try {
81
- const payer = {
82
- billing_profile_data: {
83
- customer
84
- }
85
- };
86
- if (Boolean(default_payment_method)) {
87
- payer.billing_profile_data.default_payment_method = default_payment_method;
70
+ const payer = {
71
+ billing_profile_data: {
72
+ customer
88
73
  }
89
- ;
90
- const { data: intent } = await axios.post(
91
- billing + "/intents",
92
- {
93
- currency,
94
- cadence_data: {
95
- billing_cycle: {
96
- type: process.env.STRIPE_SUBSCRIPTION_INTERVAL,
97
- interval_count: 1
98
- },
99
- payer
74
+ };
75
+ if (Boolean(default_payment_method)) {
76
+ payer.billing_profile_data.default_payment_method = default_payment_method;
77
+ }
78
+ ;
79
+ const intent = await request({
80
+ method: "POST",
81
+ url: billing + "/intents",
82
+ headers: withIdem(idempotencyKey, "intent.create"),
83
+ body: {
84
+ currency,
85
+ cadence_data: {
86
+ billing_cycle: {
87
+ type: process.env.STRIPE_SUBSCRIPTION_INTERVAL,
88
+ interval_count: 1
100
89
  },
101
- actions: [
102
- {
103
- type: "subscribe",
104
- subscribe: {
105
- type: "pricing_plan_subscription_details",
106
- pricing_plan_subscription_details: {
107
- component_configurations: [],
108
- pricing_plan: process.env.STRIPE_PRICING_AI_PLAN_ID,
109
- pricing_plan_version: process.env.STRIPE_PRICING_AI_PLAN_VERSION,
110
- metadata
111
- }
112
- }
113
- }
114
- ]
90
+ payer
115
91
  },
116
- withIdem(idempotencyKey, "intent.create")
117
- );
118
- const { data: reserve } = await axios.post(
119
- billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
120
- {},
121
- withIdem(idempotencyKey, "intent.reserve")
122
- );
123
- const { data: commit } = await axios.post(
124
- billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
125
- {},
126
- withIdem(idempotencyKey, "intent.commit")
127
- );
128
- const { data: actions } = await axios.get(
129
- billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
130
- options
131
- );
132
- const billable_items = await billableItems();
133
- const filters = [
134
- {
135
- credit_grants: {
136
- applicability_config: {
137
- scope: {
138
- billable_items
92
+ actions: [
93
+ {
94
+ type: "subscribe",
95
+ subscribe: {
96
+ type: "pricing_plan_subscription_details",
97
+ pricing_plan_subscription_details: {
98
+ component_configurations: [],
99
+ pricing_plan: process.env.STRIPE_PRICING_AI_PLAN_ID,
100
+ pricing_plan_version: process.env.STRIPE_PRICING_AI_PLAN_VERSION,
101
+ metadata
139
102
  }
140
103
  }
141
- },
142
- customer,
143
- type: "customer"
144
- }
145
- ];
146
- const replenish = await stripe2.billing.alerts.create(
147
- {
148
- alert_type: "credit_balance_threshold",
149
- credit_balance_threshold: {
150
- filters,
151
- lte: {
152
- balance_type: "monetary",
153
- monetary: {
154
- currency,
155
- value: 200
156
- // cents
157
- }
104
+ }
105
+ ]
106
+ }
107
+ });
108
+ await request({
109
+ method: "POST",
110
+ url: billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
111
+ headers: withIdem(idempotencyKey, "intent.reserve"),
112
+ body: {}
113
+ });
114
+ await request({
115
+ method: "POST",
116
+ url: billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
117
+ headers: withIdem(idempotencyKey, "intent.commit"),
118
+ body: {}
119
+ });
120
+ const actions = await request({
121
+ url: billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
122
+ headers
123
+ });
124
+ const billable_items = await billableItems();
125
+ const filters = [
126
+ {
127
+ credit_grants: {
128
+ applicability_config: {
129
+ scope: {
130
+ billable_items
158
131
  }
159
- },
160
- title: customer + ".credit_balance_replenish"
132
+ }
161
133
  },
162
- idem(idempotencyKey, "alert.replenish")
163
- );
164
- const depleted = await stripe2.billing.alerts.create(
165
- {
166
- alert_type: "credit_balance_threshold",
167
- credit_balance_threshold: {
168
- filters,
169
- lte: {
170
- balance_type: "monetary",
171
- monetary: {
172
- currency,
173
- value: 1
174
- // cents
175
- }
134
+ customer,
135
+ type: "customer"
136
+ }
137
+ ];
138
+ const replenish = await stripe2.billing.alerts.create(
139
+ {
140
+ alert_type: "credit_balance_threshold",
141
+ credit_balance_threshold: {
142
+ filters,
143
+ lte: {
144
+ balance_type: "monetary",
145
+ monetary: {
146
+ currency,
147
+ value: 200
148
+ // cents
176
149
  }
177
- },
178
- title: customer + ".credit_balance_depleted"
150
+ }
179
151
  },
180
- idem(idempotencyKey, "alert.depleted")
181
- );
182
- 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;
183
- if (!stripePricingPlanSubscriptionId) {
184
- throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
185
- }
186
- ;
187
- let stripePricingPlanSubscriptionNextBillingDate;
188
- if (!default_payment_method) {
189
- const { data: subscription } = await axios.get(
190
- billing + "/pricing_plan_subscriptions/" + stripePricingPlanSubscriptionId,
191
- options
192
- );
193
- if (subscription == null ? void 0 : subscription.billing_cadence) {
194
- const { data: cadence } = await axios.get(
195
- billing + "/cadences/" + subscription.billing_cadence,
196
- options
197
- );
198
- if (cadence == null ? void 0 : cadence.next_billing_date) {
199
- stripePricingPlanSubscriptionNextBillingDate = cadence.next_billing_date;
152
+ title: customer + ".credit_balance_replenish"
153
+ },
154
+ idem(idempotencyKey, "alert.replenish")
155
+ );
156
+ const depleted = await stripe2.billing.alerts.create(
157
+ {
158
+ alert_type: "credit_balance_threshold",
159
+ credit_balance_threshold: {
160
+ filters,
161
+ lte: {
162
+ balance_type: "monetary",
163
+ monetary: {
164
+ currency,
165
+ value: 1
166
+ // cents
167
+ }
200
168
  }
169
+ },
170
+ title: customer + ".credit_balance_depleted"
171
+ },
172
+ idem(idempotencyKey, "alert.depleted")
173
+ );
174
+ 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;
175
+ if (!stripePricingPlanSubscriptionId) {
176
+ throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
177
+ }
178
+ ;
179
+ let stripePricingPlanSubscriptionNextBillingDate;
180
+ if (!default_payment_method) {
181
+ const subscription = await request({
182
+ url: billing + "/pricing_plan_subscriptions/" + stripePricingPlanSubscriptionId,
183
+ headers
184
+ });
185
+ if (subscription == null ? void 0 : subscription.billing_cadence) {
186
+ const cadence = await request({
187
+ url: billing + "/cadences/" + subscription.billing_cadence,
188
+ headers
189
+ });
190
+ if (cadence == null ? void 0 : cadence.next_billing_date) {
191
+ stripePricingPlanSubscriptionNextBillingDate = cadence.next_billing_date;
201
192
  }
202
193
  ;
203
194
  }
204
195
  ;
205
- return {
206
- stripeAlerts: {
207
- ai: {
208
- depleted: depleted == null ? void 0 : depleted.id,
209
- replenish: replenish == null ? void 0 : replenish.id
210
- }
211
- },
212
- stripePricingPlanSubscriptionId,
213
- stripePricingPlanSubscriptionNextBillingDate
214
- };
215
- } catch (error) {
216
- throw error;
217
196
  }
197
+ ;
198
+ return {
199
+ stripeAlerts: {
200
+ ai: {
201
+ depleted: depleted == null ? void 0 : depleted.id,
202
+ replenish: replenish == null ? void 0 : replenish.id
203
+ }
204
+ },
205
+ stripePricingPlanSubscriptionId,
206
+ stripePricingPlanSubscriptionNextBillingDate
207
+ };
218
208
  };
219
209
  const unsubscribe = async ({
220
210
  currency,
221
211
  id,
222
212
  idempotencyKey
223
213
  }) => {
224
- try {
225
- const { data } = await axios.post(
226
- billing + "/intents",
227
- {
228
- currency,
229
- actions: [
230
- {
231
- type: "deactivate",
232
- deactivate: {
233
- type: "pricing_plan_subscription_details",
234
- pricing_plan_subscription_details: {
235
- pricing_plan_subscription: id
236
- }
214
+ const data = await request({
215
+ method: "POST",
216
+ url: billing + "/intents",
217
+ headers: withIdem(idempotencyKey, "intent.create"),
218
+ body: {
219
+ currency,
220
+ actions: [
221
+ {
222
+ type: "deactivate",
223
+ deactivate: {
224
+ type: "pricing_plan_subscription_details",
225
+ pricing_plan_subscription_details: {
226
+ pricing_plan_subscription: id
237
227
  }
238
228
  }
239
- ]
240
- },
241
- withIdem(idempotencyKey, "intent.create")
242
- );
243
- await axios.post(
244
- billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
245
- {},
246
- withIdem(idempotencyKey, "intent.reserve")
247
- );
248
- const commit = await axios.post(
249
- billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
250
- {},
251
- withIdem(idempotencyKey, "intent.commit")
252
- );
253
- return commit;
254
- } catch (error) {
255
- throw error;
256
- }
229
+ }
230
+ ]
231
+ }
232
+ });
233
+ await request({
234
+ method: "POST",
235
+ url: billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
236
+ headers: withIdem(idempotencyKey, "intent.reserve"),
237
+ body: {}
238
+ });
239
+ const commit = await request({
240
+ method: "POST",
241
+ url: billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
242
+ headers: withIdem(idempotencyKey, "intent.commit"),
243
+ body: {}
244
+ });
245
+ return commit;
257
246
  };
258
247
  const usage = ({
259
248
  customer,
@@ -304,17 +293,14 @@ var require_billing = __commonJS({
304
293
  // lib/subscription.js
305
294
  var require_subscription = __commonJS({
306
295
  "lib/subscription.js"(exports2, module2) {
307
- var axios = require("axios");
296
+ var { request } = require("@drawbridge/drawbridge-utils/http");
308
297
  var subscriptions = "https://api.stripe.com/v1/subscriptions";
309
298
  module2.exports = ({
310
299
  stripe: stripe2
311
300
  }) => {
312
- const options = {
313
- headers: {
314
- "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
315
- "Content-Type": "application/x-www-form-urlencoded",
316
- "Stripe-Version": "2026-03-25.preview"
317
- }
301
+ const headers = {
302
+ "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
303
+ "Stripe-Version": "2026-03-25.preview"
318
304
  };
319
305
  const idem = (base, suffix) => {
320
306
  if (!base) return void 0;
@@ -324,10 +310,34 @@ var require_subscription = __commonJS({
324
310
  };
325
311
  const createMeter = async ({
326
312
  idempotencyKey,
313
+ meter: existing,
327
314
  metadata,
328
315
  name
329
316
  }) => {
330
317
  const usage = (metadata == null ? void 0 : metadata.organization) + "_usage_" + name;
318
+ if (existing) {
319
+ const meter2 = await stripe2.billing.meters.retrieve(existing).catch(() => null);
320
+ if ((meter2 == null ? void 0 : meter2.status) === "active" && (meter2 == null ? void 0 : meter2.event_name) === usage) {
321
+ return {
322
+ meter: meter2.id,
323
+ usage
324
+ };
325
+ }
326
+ ;
327
+ }
328
+ ;
329
+ for await (const meter2 of stripe2.billing.meters.list({
330
+ status: "active"
331
+ })) {
332
+ if ((meter2 == null ? void 0 : meter2.event_name) === usage) {
333
+ return {
334
+ meter: meter2.id,
335
+ usage
336
+ };
337
+ }
338
+ ;
339
+ }
340
+ ;
331
341
  const meter = await stripe2.billing.meters.create(
332
342
  {
333
343
  default_aggregation: {
@@ -399,6 +409,7 @@ var require_subscription = __commonJS({
399
409
  idempotencyKey,
400
410
  interval,
401
411
  limit,
412
+ meter: existing,
402
413
  metadata,
403
414
  name,
404
415
  overage,
@@ -408,6 +419,7 @@ var require_subscription = __commonJS({
408
419
  const response = {};
409
420
  const { meter, usage } = await createMeter({
410
421
  idempotencyKey,
422
+ meter: existing,
411
423
  metadata,
412
424
  name
413
425
  });
@@ -590,6 +602,7 @@ var require_subscription = __commonJS({
590
602
  customer,
591
603
  discounts = [],
592
604
  idempotencyKey,
605
+ meter,
593
606
  metadata,
594
607
  stripePaymentMethodId,
595
608
  to
@@ -601,6 +614,7 @@ var require_subscription = __commonJS({
601
614
  idempotencyKey,
602
615
  interval: to.interval,
603
616
  limit: to.limits.organization.actions,
617
+ meter,
604
618
  metadata,
605
619
  name: "actions",
606
620
  overage: to.overages.actions,
@@ -658,55 +672,43 @@ var require_subscription = __commonJS({
658
672
  idempotencyKey,
659
673
  stripeSubscriptionId
660
674
  }) => {
661
- try {
662
- const body = new URLSearchParams();
663
- body.append("type", "subscription");
664
- body.append("bill_for[unused_time_from][type]", "now");
665
- body.append("bill_for[outstanding_usage_through][type]", "now");
666
- body.append("invoicing_behavior", "pending_invoice_item");
667
- const { data } = await axios.post(
668
- subscriptions + "/" + stripeSubscriptionId + "/pause",
669
- body,
670
- {
671
- ...options,
672
- ...idempotencyKey && {
673
- headers: {
674
- ...options.headers,
675
- "Idempotency-Key": idempotencyKey
676
- }
677
- }
675
+ return await request({
676
+ method: "POST",
677
+ url: subscriptions + "/" + stripeSubscriptionId + "/pause",
678
+ type: "form",
679
+ headers: {
680
+ ...headers,
681
+ ...idempotencyKey && {
682
+ "Idempotency-Key": idempotencyKey
678
683
  }
679
- );
680
- return data;
681
- } catch (error) {
682
- throw error;
683
- }
684
+ },
685
+ body: {
686
+ "type": "subscription",
687
+ "bill_for[unused_time_from][type]": "now",
688
+ "bill_for[outstanding_usage_through][type]": "now",
689
+ "invoicing_behavior": "pending_invoice_item"
690
+ }
691
+ });
684
692
  },
685
693
  resume: async ({
686
694
  idempotencyKey,
687
695
  stripeSubscriptionId
688
696
  }) => {
689
- try {
690
- const body = new URLSearchParams();
691
- body.append("billing_cycle_anchor", "unchanged");
692
- body.append("proration_behavior", "create_prorations");
693
- const { data } = await axios.post(
694
- subscriptions + "/" + stripeSubscriptionId + "/resume",
695
- body,
696
- {
697
- ...options,
698
- ...idempotencyKey && {
699
- headers: {
700
- ...options.headers,
701
- "Idempotency-Key": idempotencyKey
702
- }
703
- }
697
+ return await request({
698
+ method: "POST",
699
+ url: subscriptions + "/" + stripeSubscriptionId + "/resume",
700
+ type: "form",
701
+ headers: {
702
+ ...headers,
703
+ ...idempotencyKey && {
704
+ "Idempotency-Key": idempotencyKey
704
705
  }
705
- );
706
- return data;
707
- } catch (error) {
708
- throw error;
709
- }
706
+ },
707
+ body: {
708
+ "billing_cycle_anchor": "unchanged",
709
+ "proration_behavior": "create_prorations"
710
+ }
711
+ });
710
712
  },
711
713
  update: async ({
712
714
  current,
package/dist/index.mjs CHANGED
@@ -12,26 +12,20 @@ var __commonJS = (cb, mod) => function __require2() {
12
12
  // lib/billing.js
13
13
  var require_billing = __commonJS({
14
14
  "lib/billing.js"(exports, module) {
15
- var axios = __require("axios");
15
+ var { request } = __require("@drawbridge/drawbridge-utils/http");
16
16
  var billing = "https://api.stripe.com/v2/billing";
17
17
  module.exports = ({
18
18
  stripe
19
19
  }) => {
20
- const options = {
21
- headers: {
22
- "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
23
- "Content-Type": "application/json",
24
- "Stripe-Version": "2026-01-28.preview"
25
- }
20
+ const headers = {
21
+ "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
22
+ "Stripe-Version": "2026-01-28.preview"
26
23
  };
27
24
  const withIdem = (base, suffix) => {
28
- if (!base) return options;
25
+ if (!base) return headers;
29
26
  return {
30
- ...options,
31
- headers: {
32
- ...options.headers,
33
- "Idempotency-Key": base + ":" + suffix
34
- }
27
+ ...headers,
28
+ "Idempotency-Key": base + ":" + suffix
35
29
  };
36
30
  };
37
31
  const idem = (base, suffix) => {
@@ -42,38 +36,34 @@ var require_billing = __commonJS({
42
36
  };
43
37
  const billableItems = async () => {
44
38
  var _a;
45
- try {
46
- const { data: components } = await axios.get(
47
- billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
48
- options
49
- );
50
- const rateCardIds = ((components == null ? void 0 : components.data) || []).map((plan) => {
51
- var _a2;
52
- return (_a2 = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _a2.id;
53
- }).filter(Boolean);
54
- const rateResults = await Promise.all(
55
- rateCardIds.map((id) => axios.get(
56
- billing + "/rate_cards/" + id + "/rates",
57
- options
58
- ))
59
- );
60
- const items = [];
61
- for (const { data: rates } of rateResults) {
62
- for (const element of (rates == null ? void 0 : rates.data) || []) {
63
- if ((_a = element == null ? void 0 : element.metered_item) == null ? void 0 : _a.id) {
64
- items.push({
65
- id: element.metered_item.id
66
- });
67
- }
68
- ;
39
+ const components = await request({
40
+ url: billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
41
+ headers
42
+ });
43
+ const rateCardIds = ((components == null ? void 0 : components.data) || []).map((plan) => {
44
+ var _a2;
45
+ return (_a2 = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _a2.id;
46
+ }).filter(Boolean);
47
+ const rateResults = await Promise.all(
48
+ rateCardIds.map((id) => request({
49
+ url: billing + "/rate_cards/" + id + "/rates",
50
+ headers
51
+ }))
52
+ );
53
+ const items = [];
54
+ for (const rates of rateResults) {
55
+ for (const element of (rates == null ? void 0 : rates.data) || []) {
56
+ if ((_a = element == null ? void 0 : element.metered_item) == null ? void 0 : _a.id) {
57
+ items.push({
58
+ id: element.metered_item.id
59
+ });
69
60
  }
70
61
  ;
71
62
  }
72
63
  ;
73
- return items;
74
- } catch (error) {
75
- throw error;
76
64
  }
65
+ ;
66
+ return items;
77
67
  };
78
68
  const subscribe = async ({
79
69
  currency,
@@ -83,183 +73,182 @@ var require_billing = __commonJS({
83
73
  metadata = {}
84
74
  }) => {
85
75
  var _a, _b, _c, _d;
86
- try {
87
- const payer = {
88
- billing_profile_data: {
89
- customer
90
- }
91
- };
92
- if (Boolean(default_payment_method)) {
93
- payer.billing_profile_data.default_payment_method = default_payment_method;
76
+ const payer = {
77
+ billing_profile_data: {
78
+ customer
94
79
  }
95
- ;
96
- const { data: intent } = await axios.post(
97
- billing + "/intents",
98
- {
99
- currency,
100
- cadence_data: {
101
- billing_cycle: {
102
- type: process.env.STRIPE_SUBSCRIPTION_INTERVAL,
103
- interval_count: 1
104
- },
105
- payer
80
+ };
81
+ if (Boolean(default_payment_method)) {
82
+ payer.billing_profile_data.default_payment_method = default_payment_method;
83
+ }
84
+ ;
85
+ const intent = await request({
86
+ method: "POST",
87
+ url: billing + "/intents",
88
+ headers: withIdem(idempotencyKey, "intent.create"),
89
+ body: {
90
+ currency,
91
+ cadence_data: {
92
+ billing_cycle: {
93
+ type: process.env.STRIPE_SUBSCRIPTION_INTERVAL,
94
+ interval_count: 1
106
95
  },
107
- actions: [
108
- {
109
- type: "subscribe",
110
- subscribe: {
111
- type: "pricing_plan_subscription_details",
112
- pricing_plan_subscription_details: {
113
- component_configurations: [],
114
- pricing_plan: process.env.STRIPE_PRICING_AI_PLAN_ID,
115
- pricing_plan_version: process.env.STRIPE_PRICING_AI_PLAN_VERSION,
116
- metadata
117
- }
118
- }
119
- }
120
- ]
96
+ payer
121
97
  },
122
- withIdem(idempotencyKey, "intent.create")
123
- );
124
- const { data: reserve } = await axios.post(
125
- billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
126
- {},
127
- withIdem(idempotencyKey, "intent.reserve")
128
- );
129
- const { data: commit } = await axios.post(
130
- billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
131
- {},
132
- withIdem(idempotencyKey, "intent.commit")
133
- );
134
- const { data: actions } = await axios.get(
135
- billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
136
- options
137
- );
138
- const billable_items = await billableItems();
139
- const filters = [
140
- {
141
- credit_grants: {
142
- applicability_config: {
143
- scope: {
144
- billable_items
98
+ actions: [
99
+ {
100
+ type: "subscribe",
101
+ subscribe: {
102
+ type: "pricing_plan_subscription_details",
103
+ pricing_plan_subscription_details: {
104
+ component_configurations: [],
105
+ pricing_plan: process.env.STRIPE_PRICING_AI_PLAN_ID,
106
+ pricing_plan_version: process.env.STRIPE_PRICING_AI_PLAN_VERSION,
107
+ metadata
145
108
  }
146
109
  }
147
- },
148
- customer,
149
- type: "customer"
150
- }
151
- ];
152
- const replenish = await stripe.billing.alerts.create(
153
- {
154
- alert_type: "credit_balance_threshold",
155
- credit_balance_threshold: {
156
- filters,
157
- lte: {
158
- balance_type: "monetary",
159
- monetary: {
160
- currency,
161
- value: 200
162
- // cents
163
- }
110
+ }
111
+ ]
112
+ }
113
+ });
114
+ await request({
115
+ method: "POST",
116
+ url: billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
117
+ headers: withIdem(idempotencyKey, "intent.reserve"),
118
+ body: {}
119
+ });
120
+ await request({
121
+ method: "POST",
122
+ url: billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
123
+ headers: withIdem(idempotencyKey, "intent.commit"),
124
+ body: {}
125
+ });
126
+ const actions = await request({
127
+ url: billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
128
+ headers
129
+ });
130
+ const billable_items = await billableItems();
131
+ const filters = [
132
+ {
133
+ credit_grants: {
134
+ applicability_config: {
135
+ scope: {
136
+ billable_items
164
137
  }
165
- },
166
- title: customer + ".credit_balance_replenish"
138
+ }
167
139
  },
168
- idem(idempotencyKey, "alert.replenish")
169
- );
170
- const depleted = await stripe.billing.alerts.create(
171
- {
172
- alert_type: "credit_balance_threshold",
173
- credit_balance_threshold: {
174
- filters,
175
- lte: {
176
- balance_type: "monetary",
177
- monetary: {
178
- currency,
179
- value: 1
180
- // cents
181
- }
140
+ customer,
141
+ type: "customer"
142
+ }
143
+ ];
144
+ const replenish = await stripe.billing.alerts.create(
145
+ {
146
+ alert_type: "credit_balance_threshold",
147
+ credit_balance_threshold: {
148
+ filters,
149
+ lte: {
150
+ balance_type: "monetary",
151
+ monetary: {
152
+ currency,
153
+ value: 200
154
+ // cents
182
155
  }
183
- },
184
- title: customer + ".credit_balance_depleted"
156
+ }
185
157
  },
186
- idem(idempotencyKey, "alert.depleted")
187
- );
188
- 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;
189
- if (!stripePricingPlanSubscriptionId) {
190
- throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
191
- }
192
- ;
193
- let stripePricingPlanSubscriptionNextBillingDate;
194
- if (!default_payment_method) {
195
- const { data: subscription } = await axios.get(
196
- billing + "/pricing_plan_subscriptions/" + stripePricingPlanSubscriptionId,
197
- options
198
- );
199
- if (subscription == null ? void 0 : subscription.billing_cadence) {
200
- const { data: cadence } = await axios.get(
201
- billing + "/cadences/" + subscription.billing_cadence,
202
- options
203
- );
204
- if (cadence == null ? void 0 : cadence.next_billing_date) {
205
- stripePricingPlanSubscriptionNextBillingDate = cadence.next_billing_date;
158
+ title: customer + ".credit_balance_replenish"
159
+ },
160
+ idem(idempotencyKey, "alert.replenish")
161
+ );
162
+ const depleted = await stripe.billing.alerts.create(
163
+ {
164
+ alert_type: "credit_balance_threshold",
165
+ credit_balance_threshold: {
166
+ filters,
167
+ lte: {
168
+ balance_type: "monetary",
169
+ monetary: {
170
+ currency,
171
+ value: 1
172
+ // cents
173
+ }
206
174
  }
175
+ },
176
+ title: customer + ".credit_balance_depleted"
177
+ },
178
+ idem(idempotencyKey, "alert.depleted")
179
+ );
180
+ 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;
181
+ if (!stripePricingPlanSubscriptionId) {
182
+ throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
183
+ }
184
+ ;
185
+ let stripePricingPlanSubscriptionNextBillingDate;
186
+ if (!default_payment_method) {
187
+ const subscription = await request({
188
+ url: billing + "/pricing_plan_subscriptions/" + stripePricingPlanSubscriptionId,
189
+ headers
190
+ });
191
+ if (subscription == null ? void 0 : subscription.billing_cadence) {
192
+ const cadence = await request({
193
+ url: billing + "/cadences/" + subscription.billing_cadence,
194
+ headers
195
+ });
196
+ if (cadence == null ? void 0 : cadence.next_billing_date) {
197
+ stripePricingPlanSubscriptionNextBillingDate = cadence.next_billing_date;
207
198
  }
208
199
  ;
209
200
  }
210
201
  ;
211
- return {
212
- stripeAlerts: {
213
- ai: {
214
- depleted: depleted == null ? void 0 : depleted.id,
215
- replenish: replenish == null ? void 0 : replenish.id
216
- }
217
- },
218
- stripePricingPlanSubscriptionId,
219
- stripePricingPlanSubscriptionNextBillingDate
220
- };
221
- } catch (error) {
222
- throw error;
223
202
  }
203
+ ;
204
+ return {
205
+ stripeAlerts: {
206
+ ai: {
207
+ depleted: depleted == null ? void 0 : depleted.id,
208
+ replenish: replenish == null ? void 0 : replenish.id
209
+ }
210
+ },
211
+ stripePricingPlanSubscriptionId,
212
+ stripePricingPlanSubscriptionNextBillingDate
213
+ };
224
214
  };
225
215
  const unsubscribe = async ({
226
216
  currency,
227
217
  id,
228
218
  idempotencyKey
229
219
  }) => {
230
- try {
231
- const { data } = await axios.post(
232
- billing + "/intents",
233
- {
234
- currency,
235
- actions: [
236
- {
237
- type: "deactivate",
238
- deactivate: {
239
- type: "pricing_plan_subscription_details",
240
- pricing_plan_subscription_details: {
241
- pricing_plan_subscription: id
242
- }
220
+ const data = await request({
221
+ method: "POST",
222
+ url: billing + "/intents",
223
+ headers: withIdem(idempotencyKey, "intent.create"),
224
+ body: {
225
+ currency,
226
+ actions: [
227
+ {
228
+ type: "deactivate",
229
+ deactivate: {
230
+ type: "pricing_plan_subscription_details",
231
+ pricing_plan_subscription_details: {
232
+ pricing_plan_subscription: id
243
233
  }
244
234
  }
245
- ]
246
- },
247
- withIdem(idempotencyKey, "intent.create")
248
- );
249
- await axios.post(
250
- billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
251
- {},
252
- withIdem(idempotencyKey, "intent.reserve")
253
- );
254
- const commit = await axios.post(
255
- billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
256
- {},
257
- withIdem(idempotencyKey, "intent.commit")
258
- );
259
- return commit;
260
- } catch (error) {
261
- throw error;
262
- }
235
+ }
236
+ ]
237
+ }
238
+ });
239
+ await request({
240
+ method: "POST",
241
+ url: billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
242
+ headers: withIdem(idempotencyKey, "intent.reserve"),
243
+ body: {}
244
+ });
245
+ const commit = await request({
246
+ method: "POST",
247
+ url: billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
248
+ headers: withIdem(idempotencyKey, "intent.commit"),
249
+ body: {}
250
+ });
251
+ return commit;
263
252
  };
264
253
  const usage = ({
265
254
  customer,
@@ -310,17 +299,14 @@ var require_billing = __commonJS({
310
299
  // lib/subscription.js
311
300
  var require_subscription = __commonJS({
312
301
  "lib/subscription.js"(exports, module) {
313
- var axios = __require("axios");
302
+ var { request } = __require("@drawbridge/drawbridge-utils/http");
314
303
  var subscriptions = "https://api.stripe.com/v1/subscriptions";
315
304
  module.exports = ({
316
305
  stripe
317
306
  }) => {
318
- const options = {
319
- headers: {
320
- "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
321
- "Content-Type": "application/x-www-form-urlencoded",
322
- "Stripe-Version": "2026-03-25.preview"
323
- }
307
+ const headers = {
308
+ "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
309
+ "Stripe-Version": "2026-03-25.preview"
324
310
  };
325
311
  const idem = (base, suffix) => {
326
312
  if (!base) return void 0;
@@ -330,10 +316,34 @@ var require_subscription = __commonJS({
330
316
  };
331
317
  const createMeter = async ({
332
318
  idempotencyKey,
319
+ meter: existing,
333
320
  metadata,
334
321
  name
335
322
  }) => {
336
323
  const usage = (metadata == null ? void 0 : metadata.organization) + "_usage_" + name;
324
+ if (existing) {
325
+ const meter2 = await stripe.billing.meters.retrieve(existing).catch(() => null);
326
+ if ((meter2 == null ? void 0 : meter2.status) === "active" && (meter2 == null ? void 0 : meter2.event_name) === usage) {
327
+ return {
328
+ meter: meter2.id,
329
+ usage
330
+ };
331
+ }
332
+ ;
333
+ }
334
+ ;
335
+ for await (const meter2 of stripe.billing.meters.list({
336
+ status: "active"
337
+ })) {
338
+ if ((meter2 == null ? void 0 : meter2.event_name) === usage) {
339
+ return {
340
+ meter: meter2.id,
341
+ usage
342
+ };
343
+ }
344
+ ;
345
+ }
346
+ ;
337
347
  const meter = await stripe.billing.meters.create(
338
348
  {
339
349
  default_aggregation: {
@@ -405,6 +415,7 @@ var require_subscription = __commonJS({
405
415
  idempotencyKey,
406
416
  interval,
407
417
  limit,
418
+ meter: existing,
408
419
  metadata,
409
420
  name,
410
421
  overage,
@@ -414,6 +425,7 @@ var require_subscription = __commonJS({
414
425
  const response = {};
415
426
  const { meter, usage } = await createMeter({
416
427
  idempotencyKey,
428
+ meter: existing,
417
429
  metadata,
418
430
  name
419
431
  });
@@ -596,6 +608,7 @@ var require_subscription = __commonJS({
596
608
  customer,
597
609
  discounts = [],
598
610
  idempotencyKey,
611
+ meter,
599
612
  metadata,
600
613
  stripePaymentMethodId,
601
614
  to
@@ -607,6 +620,7 @@ var require_subscription = __commonJS({
607
620
  idempotencyKey,
608
621
  interval: to.interval,
609
622
  limit: to.limits.organization.actions,
623
+ meter,
610
624
  metadata,
611
625
  name: "actions",
612
626
  overage: to.overages.actions,
@@ -664,55 +678,43 @@ var require_subscription = __commonJS({
664
678
  idempotencyKey,
665
679
  stripeSubscriptionId
666
680
  }) => {
667
- try {
668
- const body = new URLSearchParams();
669
- body.append("type", "subscription");
670
- body.append("bill_for[unused_time_from][type]", "now");
671
- body.append("bill_for[outstanding_usage_through][type]", "now");
672
- body.append("invoicing_behavior", "pending_invoice_item");
673
- const { data } = await axios.post(
674
- subscriptions + "/" + stripeSubscriptionId + "/pause",
675
- body,
676
- {
677
- ...options,
678
- ...idempotencyKey && {
679
- headers: {
680
- ...options.headers,
681
- "Idempotency-Key": idempotencyKey
682
- }
683
- }
681
+ return await request({
682
+ method: "POST",
683
+ url: subscriptions + "/" + stripeSubscriptionId + "/pause",
684
+ type: "form",
685
+ headers: {
686
+ ...headers,
687
+ ...idempotencyKey && {
688
+ "Idempotency-Key": idempotencyKey
684
689
  }
685
- );
686
- return data;
687
- } catch (error) {
688
- throw error;
689
- }
690
+ },
691
+ body: {
692
+ "type": "subscription",
693
+ "bill_for[unused_time_from][type]": "now",
694
+ "bill_for[outstanding_usage_through][type]": "now",
695
+ "invoicing_behavior": "pending_invoice_item"
696
+ }
697
+ });
690
698
  },
691
699
  resume: async ({
692
700
  idempotencyKey,
693
701
  stripeSubscriptionId
694
702
  }) => {
695
- try {
696
- const body = new URLSearchParams();
697
- body.append("billing_cycle_anchor", "unchanged");
698
- body.append("proration_behavior", "create_prorations");
699
- const { data } = await axios.post(
700
- subscriptions + "/" + stripeSubscriptionId + "/resume",
701
- body,
702
- {
703
- ...options,
704
- ...idempotencyKey && {
705
- headers: {
706
- ...options.headers,
707
- "Idempotency-Key": idempotencyKey
708
- }
709
- }
703
+ return await request({
704
+ method: "POST",
705
+ url: subscriptions + "/" + stripeSubscriptionId + "/resume",
706
+ type: "form",
707
+ headers: {
708
+ ...headers,
709
+ ...idempotencyKey && {
710
+ "Idempotency-Key": idempotencyKey
710
711
  }
711
- );
712
- return data;
713
- } catch (error) {
714
- throw error;
715
- }
712
+ },
713
+ body: {
714
+ "billing_cycle_anchor": "unchanged",
715
+ "proration_behavior": "create_prorations"
716
+ }
717
+ });
716
718
  },
717
719
  update: async ({
718
720
  current,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "dependencies": {
3
- "axios": "1.16.0",
3
+ "@drawbridge/drawbridge-utils": "0.0.25",
4
4
  "stripe": "20.4.0",
5
5
  "tsup": "8.5.1",
6
6
  "typescript": "5.9.3"
@@ -21,8 +21,9 @@
21
21
  "access": "public"
22
22
  },
23
23
  "scripts": {
24
+ "sync": ". \"$HOME/.nvm/nvm.sh\" && nvm use && npm prune && npm install",
24
25
  "build": "tsup ./index.js && npm publish"
25
26
  },
26
27
  "types": "dist/index.d.ts",
27
- "version": "0.1.17"
28
+ "version": "0.1.19"
28
29
  }