@drawbridge/drawbridge-stripe 0.1.18 → 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.
- package/dist/index.js +217 -243
- package/dist/index.mjs +217 -243
- package/package.json +2 -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
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
19
|
+
if (!base) return headers;
|
|
23
20
|
return {
|
|
24
|
-
...
|
|
25
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
for (const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
)
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
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
|
|
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
|
|
313
|
-
|
|
314
|
-
|
|
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;
|
|
@@ -686,55 +672,43 @@ var require_subscription = __commonJS({
|
|
|
686
672
|
idempotencyKey,
|
|
687
673
|
stripeSubscriptionId
|
|
688
674
|
}) => {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
body,
|
|
698
|
-
{
|
|
699
|
-
...options,
|
|
700
|
-
...idempotencyKey && {
|
|
701
|
-
headers: {
|
|
702
|
-
...options.headers,
|
|
703
|
-
"Idempotency-Key": idempotencyKey
|
|
704
|
-
}
|
|
705
|
-
}
|
|
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
|
|
706
683
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
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
|
+
});
|
|
712
692
|
},
|
|
713
693
|
resume: async ({
|
|
714
694
|
idempotencyKey,
|
|
715
695
|
stripeSubscriptionId
|
|
716
696
|
}) => {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
...options,
|
|
726
|
-
...idempotencyKey && {
|
|
727
|
-
headers: {
|
|
728
|
-
...options.headers,
|
|
729
|
-
"Idempotency-Key": idempotencyKey
|
|
730
|
-
}
|
|
731
|
-
}
|
|
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
|
|
732
705
|
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
706
|
+
},
|
|
707
|
+
body: {
|
|
708
|
+
"billing_cycle_anchor": "unchanged",
|
|
709
|
+
"proration_behavior": "create_prorations"
|
|
710
|
+
}
|
|
711
|
+
});
|
|
738
712
|
},
|
|
739
713
|
update: async ({
|
|
740
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
|
|
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
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
25
|
+
if (!base) return headers;
|
|
29
26
|
return {
|
|
30
|
-
...
|
|
31
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
for (const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
)
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
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
|
|
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
|
|
319
|
-
|
|
320
|
-
|
|
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;
|
|
@@ -692,55 +678,43 @@ var require_subscription = __commonJS({
|
|
|
692
678
|
idempotencyKey,
|
|
693
679
|
stripeSubscriptionId
|
|
694
680
|
}) => {
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
body,
|
|
704
|
-
{
|
|
705
|
-
...options,
|
|
706
|
-
...idempotencyKey && {
|
|
707
|
-
headers: {
|
|
708
|
-
...options.headers,
|
|
709
|
-
"Idempotency-Key": idempotencyKey
|
|
710
|
-
}
|
|
711
|
-
}
|
|
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
|
|
712
689
|
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
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
|
+
});
|
|
718
698
|
},
|
|
719
699
|
resume: async ({
|
|
720
700
|
idempotencyKey,
|
|
721
701
|
stripeSubscriptionId
|
|
722
702
|
}) => {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
...options,
|
|
732
|
-
...idempotencyKey && {
|
|
733
|
-
headers: {
|
|
734
|
-
...options.headers,
|
|
735
|
-
"Idempotency-Key": idempotencyKey
|
|
736
|
-
}
|
|
737
|
-
}
|
|
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
|
|
738
711
|
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
712
|
+
},
|
|
713
|
+
body: {
|
|
714
|
+
"billing_cycle_anchor": "unchanged",
|
|
715
|
+
"proration_behavior": "create_prorations"
|
|
716
|
+
}
|
|
717
|
+
});
|
|
744
718
|
},
|
|
745
719
|
update: async ({
|
|
746
720
|
current,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
|
-
"
|
|
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"
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"build": "tsup ./index.js && npm publish"
|
|
26
26
|
},
|
|
27
27
|
"types": "dist/index.d.ts",
|
|
28
|
-
"version": "0.1.
|
|
28
|
+
"version": "0.1.19"
|
|
29
29
|
}
|