@drawbridge/drawbridge-stripe 0.1.18 → 0.1.20

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 +315 -256
  2. package/dist/index.mjs +315 -256
  3. package/package.json +4 -3
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;
@@ -484,14 +470,14 @@ var require_subscription = __commonJS({
484
470
  var _a, _b, _c, _d, _e, _f, _g, _h;
485
471
  try {
486
472
  if (direction === "downgrade") {
487
- const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
488
- const currentPlanItem = live.items.data.find(
473
+ const live2 = await stripe.subscriptions.retrieve(stripeSubscriptionId);
474
+ const currentPlanItem = live2.items.data.find(
489
475
  (item) => {
490
476
  var _a2, _b2;
491
477
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
492
478
  }
493
479
  );
494
- const currentActionsItem = live.items.data.find(
480
+ const currentActionsItem = live2.items.data.find(
495
481
  (item) => {
496
482
  var _a2, _b2;
497
483
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
@@ -564,8 +550,21 @@ var require_subscription = __commonJS({
564
550
  const props = {
565
551
  metadata
566
552
  };
553
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
554
+ const livePlanItem = live.items.data.find(
555
+ (item) => {
556
+ var _a2, _b2;
557
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
558
+ }
559
+ );
560
+ const liveActionsItem = live.items.data.find(
561
+ (item) => {
562
+ var _a2, _b2;
563
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
564
+ }
565
+ );
567
566
  const plan = await stripe.subscriptionItems.update(
568
- (_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id,
567
+ (livePlanItem == null ? void 0 : livePlanItem.id) || ((_e = items == null ? void 0 : items.plan) == null ? void 0 : _e.id),
569
568
  {
570
569
  ...props,
571
570
  price: (_f = items == null ? void 0 : items.plan) == null ? void 0 : _f.price,
@@ -574,9 +573,10 @@ var require_subscription = __commonJS({
574
573
  idem(idempotencyKey, "plan.update")
575
574
  );
576
575
  let actions = (items == null ? void 0 : items.actions) || {};
577
- if ((actions == null ? void 0 : actions.id) && (actions == null ? void 0 : actions.price)) {
576
+ const actionsItemId = (liveActionsItem == null ? void 0 : liveActionsItem.id) || (actions == null ? void 0 : actions.id);
577
+ if (actionsItemId && (actions == null ? void 0 : actions.price)) {
578
578
  ({ id: actions["id"] } = await stripe.subscriptionItems.update(
579
- actions.id,
579
+ actionsItemId,
580
580
  {
581
581
  ...props,
582
582
  price: actions.price,
@@ -692,55 +692,43 @@ var require_subscription = __commonJS({
692
692
  idempotencyKey,
693
693
  stripeSubscriptionId
694
694
  }) => {
695
- try {
696
- const body = new URLSearchParams();
697
- body.append("type", "subscription");
698
- body.append("bill_for[unused_time_from][type]", "now");
699
- body.append("bill_for[outstanding_usage_through][type]", "now");
700
- body.append("invoicing_behavior", "pending_invoice_item");
701
- const { data } = await axios.post(
702
- subscriptions + "/" + stripeSubscriptionId + "/pause",
703
- body,
704
- {
705
- ...options,
706
- ...idempotencyKey && {
707
- headers: {
708
- ...options.headers,
709
- "Idempotency-Key": idempotencyKey
710
- }
711
- }
695
+ return await request({
696
+ method: "POST",
697
+ url: subscriptions + "/" + stripeSubscriptionId + "/pause",
698
+ type: "form",
699
+ headers: {
700
+ ...headers,
701
+ ...idempotencyKey && {
702
+ "Idempotency-Key": idempotencyKey
712
703
  }
713
- );
714
- return data;
715
- } catch (error) {
716
- throw error;
717
- }
704
+ },
705
+ body: {
706
+ "type": "subscription",
707
+ "bill_for[unused_time_from][type]": "now",
708
+ "bill_for[outstanding_usage_through][type]": "now",
709
+ "invoicing_behavior": "pending_invoice_item"
710
+ }
711
+ });
718
712
  },
719
713
  resume: async ({
720
714
  idempotencyKey,
721
715
  stripeSubscriptionId
722
716
  }) => {
723
- try {
724
- const body = new URLSearchParams();
725
- body.append("billing_cycle_anchor", "unchanged");
726
- body.append("proration_behavior", "create_prorations");
727
- const { data } = await axios.post(
728
- subscriptions + "/" + stripeSubscriptionId + "/resume",
729
- body,
730
- {
731
- ...options,
732
- ...idempotencyKey && {
733
- headers: {
734
- ...options.headers,
735
- "Idempotency-Key": idempotencyKey
736
- }
737
- }
717
+ return await request({
718
+ method: "POST",
719
+ url: subscriptions + "/" + stripeSubscriptionId + "/resume",
720
+ type: "form",
721
+ headers: {
722
+ ...headers,
723
+ ...idempotencyKey && {
724
+ "Idempotency-Key": idempotencyKey
738
725
  }
739
- );
740
- return data;
741
- } catch (error) {
742
- throw error;
743
- }
726
+ },
727
+ body: {
728
+ "billing_cycle_anchor": "unchanged",
729
+ "proration_behavior": "create_prorations"
730
+ }
731
+ });
744
732
  },
745
733
  update: async ({
746
734
  current,
@@ -753,7 +741,7 @@ var require_subscription = __commonJS({
753
741
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
754
742
  try {
755
743
  if (direction === "downgrade") {
756
- const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
744
+ const live2 = await stripe.subscriptions.retrieve(stripeSubscriptionId);
757
745
  let actions2;
758
746
  if ((_b = (_a = current == null ? void 0 : current.items) == null ? void 0 : _a.actions) == null ? void 0 : _b.meter) {
759
747
  actions2 = await createTier({
@@ -786,14 +774,14 @@ var require_subscription = __commonJS({
786
774
  }
787
775
  ;
788
776
  const product = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
789
- const livePlanItem = live.items.data.find(
777
+ const livePlanItem2 = live2.items.data.find(
790
778
  (item) => {
791
779
  var _a2, _b2;
792
780
  return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
793
781
  }
794
782
  );
795
- const currentPeriodStart = livePlanItem == null ? void 0 : livePlanItem.current_period_start;
796
- const currentPeriodEnd = livePlanItem == null ? void 0 : livePlanItem.current_period_end;
783
+ const currentPeriodStart = livePlanItem2 == null ? void 0 : livePlanItem2.current_period_start;
784
+ const currentPeriodEnd = livePlanItem2 == null ? void 0 : livePlanItem2.current_period_end;
797
785
  const phases = [
798
786
  {
799
787
  items: [
@@ -865,8 +853,22 @@ var require_subscription = __commonJS({
865
853
  const props = {
866
854
  metadata
867
855
  };
856
+ const live = await stripe.subscriptions.retrieve(stripeSubscriptionId);
857
+ const livePlanItem = live.items.data.find(
858
+ (item) => {
859
+ var _a2, _b2;
860
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) !== "metered";
861
+ }
862
+ );
863
+ const liveActionsItem = live.items.data.find(
864
+ (item) => {
865
+ var _a2, _b2;
866
+ return ((_b2 = (_a2 = item == null ? void 0 : item.price) == null ? void 0 : _a2.recurring) == null ? void 0 : _b2.usage_type) === "metered";
867
+ }
868
+ );
869
+ const liveActionsItemId = (liveActionsItem == null ? void 0 : liveActionsItem.id) || ((_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.actions) == null ? void 0 : _h.id);
868
870
  const plan = await stripe.subscriptionItems.update(
869
- (_h = (_g = current == null ? void 0 : current.items) == null ? void 0 : _g.plan) == null ? void 0 : _h.id,
871
+ (livePlanItem == null ? void 0 : livePlanItem.id) || ((_j = (_i = current == null ? void 0 : current.items) == null ? void 0 : _i.plan) == null ? void 0 : _j.id),
870
872
  {
871
873
  ...props,
872
874
  price: to.stripePriceId,
@@ -875,7 +877,7 @@ var require_subscription = __commonJS({
875
877
  idem(idempotencyKey, "plan.update")
876
878
  );
877
879
  let actions;
878
- if (((_j = (_i = current == null ? void 0 : current.items) == null ? void 0 : _i.actions) == null ? void 0 : _j.id) && ((_l = (_k = current == null ? void 0 : current.items) == null ? void 0 : _k.actions) == null ? void 0 : _l.meter)) {
880
+ if (liveActionsItemId && ((_l = (_k = current == null ? void 0 : current.items) == null ? void 0 : _k.actions) == null ? void 0 : _l.meter)) {
879
881
  actions = await createTier({
880
882
  currency: to.currency,
881
883
  idempotencyKey,
@@ -887,7 +889,7 @@ var require_subscription = __commonJS({
887
889
  overage: to.overages.actions
888
890
  });
889
891
  ({ id: actions["id"] } = await stripe.subscriptionItems.update(
890
- current.items.actions.id,
892
+ liveActionsItemId,
891
893
  {
892
894
  ...props,
893
895
  price: actions == null ? void 0 : actions.price,
@@ -932,6 +934,63 @@ var require_subscription = __commonJS({
932
934
  } catch (error) {
933
935
  throw error;
934
936
  }
937
+ },
938
+ // Pure. Given a subscription document, return the Stripe artifacts that
939
+ // are safe to deactivate at org-delete — the terminal point, the only
940
+ // place deactivation is ever safe. Meters are per-org (actions/ai/
941
+ // storage) and the actions price/product are created fresh per
942
+ // subscription, so all are reclaimable. Never the plan price/product
943
+ // (shared STRIPE_PRICE_* catalog) and never the ai/storage price/product
944
+ // (they only carry a meter, and the ai price is a shared env catalog
945
+ // price). `items` is an untyped, possibly-partial object — optional-chain
946
+ // everything and drop entries without an id.
947
+ deactivatableItems: (subscription) => {
948
+ var _a, _b, _c, _d, _e;
949
+ const items = (subscription == null ? void 0 : subscription.items) || {};
950
+ return [
951
+ { type: "meter", id: (_a = items == null ? void 0 : items.actions) == null ? void 0 : _a.meter },
952
+ { type: "price", id: (_b = items == null ? void 0 : items.actions) == null ? void 0 : _b.price },
953
+ { type: "product", id: (_c = items == null ? void 0 : items.actions) == null ? void 0 : _c.product },
954
+ { type: "meter", id: (_d = items == null ? void 0 : items.ai) == null ? void 0 : _d.meter },
955
+ { type: "meter", id: (_e = items == null ? void 0 : items.storage) == null ? void 0 : _e.meter }
956
+ ].filter((entry) => entry.id);
957
+ },
958
+ // Consume a deactivatableItems list. Each artifact is deactivated with an
959
+ // artifact-id idempotency key (`<type>.deactivate:<id>`) so re-running a
960
+ // delete is a safe no-op. Runs concurrently via Promise.allSettled;
961
+ // rejections are logged through the injected logger and never rethrown,
962
+ // so a single Stripe failure cannot block org-doc deletion. The logger is
963
+ // injected because drawbridge-stripe stays a pure SDK wrapper — telemetry
964
+ // remains the single Sentry owner.
965
+ deactivate: async ({
966
+ items = [],
967
+ logger = { info() {
968
+ }, warn() {
969
+ }, error() {
970
+ } }
971
+ }) => {
972
+ const deactivateOne = ({ type, id }) => {
973
+ const options = idem(type + ".deactivate", id);
974
+ if (type === "meter") {
975
+ return stripe.billing.meters.deactivate(id, {}, options);
976
+ }
977
+ ;
978
+ if (type === "price") {
979
+ return stripe.prices.update(id, { active: false }, options);
980
+ }
981
+ ;
982
+ if (type === "product") {
983
+ return stripe.products.update(id, { active: false }, options);
984
+ }
985
+ ;
986
+ };
987
+ return Promise.allSettled(
988
+ items.map((item) => Promise.resolve(deactivateOne(item)).catch((error) => {
989
+ logger.error(
990
+ "Stripe deactivate failed for " + (item == null ? void 0 : item.type) + " " + (item == null ? void 0 : item.id) + ": " + (error == null ? void 0 : error.message)
991
+ );
992
+ }))
993
+ );
935
994
  }
936
995
  };
937
996
  };