@drawbridge/drawbridge-stripe 0.1.25 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -9,9 +9,6 @@ const stripe = require( 'stripe' )(
9
9
  );
10
10
 
11
11
  module.exports = {
12
- billing : lib.billing({
13
- stripe
14
- }),
15
12
  fx : lib.fx({
16
13
  request
17
14
  }),
package/dist/index.d.ts CHANGED
@@ -9,9 +9,6 @@ const stripe = require( 'stripe' )(
9
9
  );
10
10
 
11
11
  module.exports = {
12
- billing : lib.billing({
13
- stripe
14
- }),
15
12
  fx : lib.fx({
16
13
  request
17
14
  }),
package/dist/index.js CHANGED
@@ -3,293 +3,6 @@ var __commonJS = (cb, mod) => function __require() {
3
3
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
4
  };
5
5
 
6
- // lib/billing.js
7
- var require_billing = __commonJS({
8
- "lib/billing.js"(exports2, module2) {
9
- var { request: request2 } = require("@drawbridge/drawbridge-utils/http");
10
- var billing = "https://api.stripe.com/v2/billing";
11
- module2.exports = ({
12
- stripe: stripe2
13
- }) => {
14
- const headers = {
15
- "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
16
- "Stripe-Version": "2026-01-28.preview"
17
- };
18
- const withIdem = (base, suffix) => {
19
- if (!base) return headers;
20
- return {
21
- ...headers,
22
- "Idempotency-Key": base + ":" + suffix
23
- };
24
- };
25
- const idem = (base, suffix) => {
26
- if (!base) return void 0;
27
- return {
28
- idempotencyKey: base + ":" + suffix
29
- };
30
- };
31
- const billableItems = async () => {
32
- var _a;
33
- const components = await request2({
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) => request2({
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
- });
54
- }
55
- ;
56
- }
57
- ;
58
- }
59
- ;
60
- return items;
61
- };
62
- const subscribe = async ({
63
- currency,
64
- customer,
65
- default_payment_method = null,
66
- idempotencyKey,
67
- metadata = {}
68
- }) => {
69
- var _a, _b, _c, _d;
70
- const payer = {
71
- billing_profile_data: {
72
- customer
73
- }
74
- };
75
- if (Boolean(default_payment_method)) {
76
- payer.billing_profile_data.default_payment_method = default_payment_method;
77
- }
78
- ;
79
- const intent = await request2({
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
89
- },
90
- payer
91
- },
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
102
- }
103
- }
104
- }
105
- ]
106
- }
107
- });
108
- await request2({
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 request2({
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 request2({
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
131
- }
132
- }
133
- },
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
149
- }
150
- }
151
- },
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
- }
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 request2({
182
- url: billing + "/pricing_plan_subscriptions/" + stripePricingPlanSubscriptionId,
183
- headers
184
- });
185
- if (subscription == null ? void 0 : subscription.billing_cadence) {
186
- const cadence = await request2({
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;
192
- }
193
- ;
194
- }
195
- ;
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
- };
208
- };
209
- const unsubscribe = async ({
210
- currency,
211
- id,
212
- idempotencyKey
213
- }) => {
214
- const data = await request2({
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
227
- }
228
- }
229
- }
230
- ]
231
- }
232
- });
233
- await request2({
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 request2({
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;
246
- };
247
- const usage = ({
248
- customer,
249
- metadata,
250
- ...rest
251
- }) => {
252
- const event_name = "token-billing-tokens";
253
- const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
254
- const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
255
- const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
256
- const sendMeterEvent = (token_type, value) => {
257
- stripe2.billing.meterEvents.create({
258
- event_name,
259
- payload: {
260
- model,
261
- stripe_customer_id: customer,
262
- token_type,
263
- value
264
- }
265
- }).catch((error) => {
266
- console.error("[billing.usage] meterEvents.create failed", {
267
- customer,
268
- token_type,
269
- value,
270
- error: error == null ? void 0 : error.message
271
- });
272
- });
273
- };
274
- if (input) {
275
- sendMeterEvent("input", input);
276
- }
277
- ;
278
- if (output) {
279
- sendMeterEvent("output", output);
280
- }
281
- ;
282
- };
283
- return {
284
- billableItems,
285
- subscribe,
286
- unsubscribe,
287
- usage
288
- };
289
- };
290
- }
291
- });
292
-
293
6
  // lib/fx.js
294
7
  var require_fx = __commonJS({
295
8
  "lib/fx.js"(exports2, module2) {
@@ -1186,11 +899,9 @@ var require_subscription = __commonJS({
1186
899
  // lib/index.js
1187
900
  var require_lib = __commonJS({
1188
901
  "lib/index.js"(exports2, module2) {
1189
- var billing = require_billing();
1190
902
  var fx = require_fx();
1191
903
  var subscription = require_subscription();
1192
904
  module2.exports = {
1193
- billing,
1194
905
  fx,
1195
906
  subscription
1196
907
  };
@@ -1207,9 +918,6 @@ var stripe = require("stripe")(
1207
918
  }
1208
919
  );
1209
920
  module.exports = {
1210
- billing: lib.billing({
1211
- stripe
1212
- }),
1213
921
  fx: lib.fx({
1214
922
  request
1215
923
  }),
package/dist/index.mjs CHANGED
@@ -9,293 +9,6 @@ var __commonJS = (cb, mod) => function __require2() {
9
9
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
10
  };
11
11
 
12
- // lib/billing.js
13
- var require_billing = __commonJS({
14
- "lib/billing.js"(exports, module) {
15
- var { request } = __require("@drawbridge/drawbridge-utils/http");
16
- var billing = "https://api.stripe.com/v2/billing";
17
- module.exports = ({
18
- stripe
19
- }) => {
20
- const headers = {
21
- "Authorization": "Bearer " + process.env.STRIPE_API_KEY,
22
- "Stripe-Version": "2026-01-28.preview"
23
- };
24
- const withIdem = (base, suffix) => {
25
- if (!base) return headers;
26
- return {
27
- ...headers,
28
- "Idempotency-Key": base + ":" + suffix
29
- };
30
- };
31
- const idem = (base, suffix) => {
32
- if (!base) return void 0;
33
- return {
34
- idempotencyKey: base + ":" + suffix
35
- };
36
- };
37
- const billableItems = async () => {
38
- var _a;
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
- });
60
- }
61
- ;
62
- }
63
- ;
64
- }
65
- ;
66
- return items;
67
- };
68
- const subscribe = async ({
69
- currency,
70
- customer,
71
- default_payment_method = null,
72
- idempotencyKey,
73
- metadata = {}
74
- }) => {
75
- var _a, _b, _c, _d;
76
- const payer = {
77
- billing_profile_data: {
78
- customer
79
- }
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
95
- },
96
- payer
97
- },
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
108
- }
109
- }
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
137
- }
138
- }
139
- },
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
155
- }
156
- }
157
- },
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
- }
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;
198
- }
199
- ;
200
- }
201
- ;
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
- };
214
- };
215
- const unsubscribe = async ({
216
- currency,
217
- id,
218
- idempotencyKey
219
- }) => {
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
233
- }
234
- }
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;
252
- };
253
- const usage = ({
254
- customer,
255
- metadata,
256
- ...rest
257
- }) => {
258
- const event_name = "token-billing-tokens";
259
- const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
260
- const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
261
- const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
262
- const sendMeterEvent = (token_type, value) => {
263
- stripe.billing.meterEvents.create({
264
- event_name,
265
- payload: {
266
- model,
267
- stripe_customer_id: customer,
268
- token_type,
269
- value
270
- }
271
- }).catch((error) => {
272
- console.error("[billing.usage] meterEvents.create failed", {
273
- customer,
274
- token_type,
275
- value,
276
- error: error == null ? void 0 : error.message
277
- });
278
- });
279
- };
280
- if (input) {
281
- sendMeterEvent("input", input);
282
- }
283
- ;
284
- if (output) {
285
- sendMeterEvent("output", output);
286
- }
287
- ;
288
- };
289
- return {
290
- billableItems,
291
- subscribe,
292
- unsubscribe,
293
- usage
294
- };
295
- };
296
- }
297
- });
298
-
299
12
  // lib/fx.js
300
13
  var require_fx = __commonJS({
301
14
  "lib/fx.js"(exports, module) {
@@ -1192,11 +905,9 @@ var require_subscription = __commonJS({
1192
905
  // lib/index.js
1193
906
  var require_lib = __commonJS({
1194
907
  "lib/index.js"(exports, module) {
1195
- var billing = require_billing();
1196
908
  var fx = require_fx();
1197
909
  var subscription = require_subscription();
1198
910
  module.exports = {
1199
- billing,
1200
911
  fx,
1201
912
  subscription
1202
913
  };
@@ -1215,9 +926,6 @@ var require_index = __commonJS({
1215
926
  }
1216
927
  );
1217
928
  module.exports = {
1218
- billing: lib.billing({
1219
- stripe
1220
- }),
1221
929
  fx: lib.fx({
1222
930
  request
1223
931
  }),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "@drawbridge/drawbridge-agents": "0.0.10",
4
- "@drawbridge/drawbridge-utils": "0.0.49",
4
+ "@drawbridge/drawbridge-utils": "0.0.50",
5
5
  "stripe": "20.4.0",
6
6
  "tsup": "8.5.1",
7
7
  "typescript": "5.9.3"
@@ -26,5 +26,5 @@
26
26
  "build": "tsup ./index.js && npm publish"
27
27
  },
28
28
  "types": "dist/index.d.ts",
29
- "version": "0.1.25"
29
+ "version": "0.1.27"
30
30
  }