@drawbridge/drawbridge-stripe 0.1.13 → 0.1.15
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 +140 -86
- package/dist/index.mjs +140 -86
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -18,28 +18,46 @@ var require_billing = __commonJS({
|
|
|
18
18
|
"Stripe-Version": "2026-01-28.preview"
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
|
+
const withIdem = (base, suffix) => {
|
|
22
|
+
if (!base) return options;
|
|
23
|
+
return {
|
|
24
|
+
...options,
|
|
25
|
+
headers: {
|
|
26
|
+
...options.headers,
|
|
27
|
+
"Idempotency-Key": base + ":" + suffix
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const idem = (base, suffix) => {
|
|
32
|
+
if (!base) return void 0;
|
|
33
|
+
return {
|
|
34
|
+
idempotencyKey: base + ":" + suffix
|
|
35
|
+
};
|
|
36
|
+
};
|
|
21
37
|
const billableItems = async () => {
|
|
22
|
-
var _a
|
|
38
|
+
var _a;
|
|
23
39
|
try {
|
|
24
|
-
const items = [];
|
|
25
40
|
const { data: components } = await axios.get(
|
|
26
41
|
billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
|
|
27
42
|
options
|
|
28
43
|
);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
});
|
|
43
61
|
}
|
|
44
62
|
;
|
|
45
63
|
}
|
|
@@ -55,6 +73,7 @@ var require_billing = __commonJS({
|
|
|
55
73
|
currency,
|
|
56
74
|
customer,
|
|
57
75
|
default_payment_method = null,
|
|
76
|
+
idempotencyKey,
|
|
58
77
|
metadata = {}
|
|
59
78
|
}) => {
|
|
60
79
|
var _a, _b, _c, _d;
|
|
@@ -94,17 +113,17 @@ var require_billing = __commonJS({
|
|
|
94
113
|
}
|
|
95
114
|
]
|
|
96
115
|
},
|
|
97
|
-
|
|
116
|
+
withIdem(idempotencyKey, "intent.create")
|
|
98
117
|
);
|
|
99
118
|
const { data: reserve } = await axios.post(
|
|
100
119
|
billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
|
|
101
120
|
{},
|
|
102
|
-
|
|
121
|
+
withIdem(idempotencyKey, "intent.reserve")
|
|
103
122
|
);
|
|
104
123
|
const { data: commit } = await axios.post(
|
|
105
124
|
billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
|
|
106
125
|
{},
|
|
107
|
-
|
|
126
|
+
withIdem(idempotencyKey, "intent.commit")
|
|
108
127
|
);
|
|
109
128
|
const { data: actions } = await axios.get(
|
|
110
129
|
billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
|
|
@@ -124,36 +143,42 @@ var require_billing = __commonJS({
|
|
|
124
143
|
type: "customer"
|
|
125
144
|
}
|
|
126
145
|
];
|
|
127
|
-
const replenish = await stripe2.billing.alerts.create(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
}
|
|
137
158
|
}
|
|
138
|
-
}
|
|
159
|
+
},
|
|
160
|
+
title: customer + ".credit_balance_replenish"
|
|
139
161
|
},
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const depleted = await stripe2.billing.alerts.create(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
}
|
|
152
176
|
}
|
|
153
|
-
}
|
|
177
|
+
},
|
|
178
|
+
title: customer + ".credit_balance_depleted"
|
|
154
179
|
},
|
|
155
|
-
|
|
156
|
-
|
|
180
|
+
idem(idempotencyKey, "alert.depleted")
|
|
181
|
+
);
|
|
157
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;
|
|
158
183
|
if (!stripePricingPlanSubscriptionId) {
|
|
159
184
|
throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
|
|
@@ -193,7 +218,8 @@ var require_billing = __commonJS({
|
|
|
193
218
|
};
|
|
194
219
|
const unsubscribe = async ({
|
|
195
220
|
currency,
|
|
196
|
-
id
|
|
221
|
+
id,
|
|
222
|
+
idempotencyKey
|
|
197
223
|
}) => {
|
|
198
224
|
try {
|
|
199
225
|
const { data } = await axios.post(
|
|
@@ -212,17 +238,17 @@ var require_billing = __commonJS({
|
|
|
212
238
|
}
|
|
213
239
|
]
|
|
214
240
|
},
|
|
215
|
-
|
|
241
|
+
withIdem(idempotencyKey, "intent.create")
|
|
216
242
|
);
|
|
217
243
|
await axios.post(
|
|
218
244
|
billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
|
|
219
245
|
{},
|
|
220
|
-
|
|
246
|
+
withIdem(idempotencyKey, "intent.reserve")
|
|
221
247
|
);
|
|
222
248
|
const commit = await axios.post(
|
|
223
249
|
billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
|
|
224
250
|
{},
|
|
225
|
-
|
|
251
|
+
withIdem(idempotencyKey, "intent.commit")
|
|
226
252
|
);
|
|
227
253
|
return commit;
|
|
228
254
|
} catch (error) {
|
|
@@ -238,28 +264,30 @@ var require_billing = __commonJS({
|
|
|
238
264
|
const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
|
|
239
265
|
const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
|
|
240
266
|
const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
|
|
241
|
-
|
|
267
|
+
const sendMeterEvent = (token_type, value) => {
|
|
242
268
|
stripe2.billing.meterEvents.create({
|
|
243
269
|
event_name,
|
|
244
270
|
payload: {
|
|
245
271
|
model,
|
|
246
272
|
stripe_customer_id: customer,
|
|
247
|
-
token_type
|
|
248
|
-
value
|
|
273
|
+
token_type,
|
|
274
|
+
value
|
|
249
275
|
}
|
|
276
|
+
}).catch((error) => {
|
|
277
|
+
console.error("[billing.usage] meterEvents.create failed", {
|
|
278
|
+
customer,
|
|
279
|
+
token_type,
|
|
280
|
+
value,
|
|
281
|
+
error: error == null ? void 0 : error.message
|
|
282
|
+
});
|
|
250
283
|
});
|
|
284
|
+
};
|
|
285
|
+
if (input) {
|
|
286
|
+
sendMeterEvent("input", input);
|
|
251
287
|
}
|
|
252
288
|
;
|
|
253
289
|
if (output) {
|
|
254
|
-
|
|
255
|
-
event_name,
|
|
256
|
-
payload: {
|
|
257
|
-
model,
|
|
258
|
-
stripe_customer_id: customer,
|
|
259
|
-
token_type: "output",
|
|
260
|
-
value: output
|
|
261
|
-
}
|
|
262
|
-
});
|
|
290
|
+
sendMeterEvent("output", output);
|
|
263
291
|
}
|
|
264
292
|
;
|
|
265
293
|
};
|
|
@@ -562,6 +590,7 @@ var require_subscription = __commonJS({
|
|
|
562
590
|
billingCycleAnchor,
|
|
563
591
|
customer,
|
|
564
592
|
discounts = [],
|
|
593
|
+
idempotencyKey,
|
|
565
594
|
metadata,
|
|
566
595
|
stripePaymentMethodId,
|
|
567
596
|
to
|
|
@@ -570,6 +599,7 @@ var require_subscription = __commonJS({
|
|
|
570
599
|
try {
|
|
571
600
|
const actions = await createItem({
|
|
572
601
|
currency: to.currency,
|
|
602
|
+
idempotencyKey,
|
|
573
603
|
interval: to.interval,
|
|
574
604
|
limit: to.limits.organization.actions,
|
|
575
605
|
metadata,
|
|
@@ -577,31 +607,37 @@ var require_subscription = __commonJS({
|
|
|
577
607
|
overage: to.overages.actions,
|
|
578
608
|
proration_behavior: "none"
|
|
579
609
|
});
|
|
580
|
-
const subscription = await stripe2.subscriptions.create(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
610
|
+
const subscription = await stripe2.subscriptions.create(
|
|
611
|
+
{
|
|
612
|
+
automatic_tax: {
|
|
613
|
+
enabled: true
|
|
614
|
+
},
|
|
615
|
+
customer,
|
|
616
|
+
description: "Organization subscription: " + metadata.organization,
|
|
617
|
+
default_payment_method: stripePaymentMethodId,
|
|
618
|
+
discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
|
|
619
|
+
items: [
|
|
620
|
+
{
|
|
621
|
+
price: actions.price
|
|
622
|
+
}
|
|
623
|
+
],
|
|
624
|
+
metadata,
|
|
625
|
+
...billingCycleAnchor && {
|
|
626
|
+
billing_cycle_anchor: billingCycleAnchor
|
|
591
627
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
billing_cycle_anchor: billingCycleAnchor
|
|
596
|
-
}
|
|
597
|
-
});
|
|
628
|
+
},
|
|
629
|
+
idem(idempotencyKey, "subscription.create")
|
|
630
|
+
);
|
|
598
631
|
const { product } = await stripe2.prices.retrieve(to == null ? void 0 : to.stripePriceId);
|
|
599
|
-
const plan = await stripe2.subscriptionItems.create(
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
632
|
+
const plan = await stripe2.subscriptionItems.create(
|
|
633
|
+
{
|
|
634
|
+
subscription: subscription == null ? void 0 : subscription.id,
|
|
635
|
+
price: to == null ? void 0 : to.stripePriceId,
|
|
636
|
+
quantity: 1,
|
|
637
|
+
proration_behavior: "always_invoice"
|
|
638
|
+
},
|
|
639
|
+
idem(idempotencyKey, "plan.create")
|
|
640
|
+
);
|
|
605
641
|
return {
|
|
606
642
|
discounts,
|
|
607
643
|
items: {
|
|
@@ -623,6 +659,7 @@ var require_subscription = __commonJS({
|
|
|
623
659
|
}
|
|
624
660
|
},
|
|
625
661
|
pause: async ({
|
|
662
|
+
idempotencyKey,
|
|
626
663
|
stripeSubscriptionId
|
|
627
664
|
}) => {
|
|
628
665
|
try {
|
|
@@ -634,7 +671,15 @@ var require_subscription = __commonJS({
|
|
|
634
671
|
const { data } = await axios.post(
|
|
635
672
|
subscriptions + "/" + stripeSubscriptionId + "/pause",
|
|
636
673
|
body,
|
|
637
|
-
|
|
674
|
+
{
|
|
675
|
+
...options,
|
|
676
|
+
...idempotencyKey && {
|
|
677
|
+
headers: {
|
|
678
|
+
...options.headers,
|
|
679
|
+
"Idempotency-Key": idempotencyKey
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
638
683
|
);
|
|
639
684
|
return data;
|
|
640
685
|
} catch (error) {
|
|
@@ -642,6 +687,7 @@ var require_subscription = __commonJS({
|
|
|
642
687
|
}
|
|
643
688
|
},
|
|
644
689
|
resume: async ({
|
|
690
|
+
idempotencyKey,
|
|
645
691
|
stripeSubscriptionId
|
|
646
692
|
}) => {
|
|
647
693
|
try {
|
|
@@ -651,7 +697,15 @@ var require_subscription = __commonJS({
|
|
|
651
697
|
const { data } = await axios.post(
|
|
652
698
|
subscriptions + "/" + stripeSubscriptionId + "/resume",
|
|
653
699
|
body,
|
|
654
|
-
|
|
700
|
+
{
|
|
701
|
+
...options,
|
|
702
|
+
...idempotencyKey && {
|
|
703
|
+
headers: {
|
|
704
|
+
...options.headers,
|
|
705
|
+
"Idempotency-Key": idempotencyKey
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
655
709
|
);
|
|
656
710
|
return data;
|
|
657
711
|
} catch (error) {
|
package/dist/index.mjs
CHANGED
|
@@ -24,28 +24,46 @@ var require_billing = __commonJS({
|
|
|
24
24
|
"Stripe-Version": "2026-01-28.preview"
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
|
+
const withIdem = (base, suffix) => {
|
|
28
|
+
if (!base) return options;
|
|
29
|
+
return {
|
|
30
|
+
...options,
|
|
31
|
+
headers: {
|
|
32
|
+
...options.headers,
|
|
33
|
+
"Idempotency-Key": base + ":" + suffix
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const idem = (base, suffix) => {
|
|
38
|
+
if (!base) return void 0;
|
|
39
|
+
return {
|
|
40
|
+
idempotencyKey: base + ":" + suffix
|
|
41
|
+
};
|
|
42
|
+
};
|
|
27
43
|
const billableItems = async () => {
|
|
28
|
-
var _a
|
|
44
|
+
var _a;
|
|
29
45
|
try {
|
|
30
|
-
const items = [];
|
|
31
46
|
const { data: components } = await axios.get(
|
|
32
47
|
billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
|
|
33
48
|
options
|
|
34
49
|
);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
+
});
|
|
49
67
|
}
|
|
50
68
|
;
|
|
51
69
|
}
|
|
@@ -61,6 +79,7 @@ var require_billing = __commonJS({
|
|
|
61
79
|
currency,
|
|
62
80
|
customer,
|
|
63
81
|
default_payment_method = null,
|
|
82
|
+
idempotencyKey,
|
|
64
83
|
metadata = {}
|
|
65
84
|
}) => {
|
|
66
85
|
var _a, _b, _c, _d;
|
|
@@ -100,17 +119,17 @@ var require_billing = __commonJS({
|
|
|
100
119
|
}
|
|
101
120
|
]
|
|
102
121
|
},
|
|
103
|
-
|
|
122
|
+
withIdem(idempotencyKey, "intent.create")
|
|
104
123
|
);
|
|
105
124
|
const { data: reserve } = await axios.post(
|
|
106
125
|
billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
|
|
107
126
|
{},
|
|
108
|
-
|
|
127
|
+
withIdem(idempotencyKey, "intent.reserve")
|
|
109
128
|
);
|
|
110
129
|
const { data: commit } = await axios.post(
|
|
111
130
|
billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
|
|
112
131
|
{},
|
|
113
|
-
|
|
132
|
+
withIdem(idempotencyKey, "intent.commit")
|
|
114
133
|
);
|
|
115
134
|
const { data: actions } = await axios.get(
|
|
116
135
|
billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
|
|
@@ -130,36 +149,42 @@ var require_billing = __commonJS({
|
|
|
130
149
|
type: "customer"
|
|
131
150
|
}
|
|
132
151
|
];
|
|
133
|
-
const replenish = await stripe.billing.alerts.create(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
+
}
|
|
143
164
|
}
|
|
144
|
-
}
|
|
165
|
+
},
|
|
166
|
+
title: customer + ".credit_balance_replenish"
|
|
145
167
|
},
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const depleted = await stripe.billing.alerts.create(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
+
}
|
|
158
182
|
}
|
|
159
|
-
}
|
|
183
|
+
},
|
|
184
|
+
title: customer + ".credit_balance_depleted"
|
|
160
185
|
},
|
|
161
|
-
|
|
162
|
-
|
|
186
|
+
idem(idempotencyKey, "alert.depleted")
|
|
187
|
+
);
|
|
163
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;
|
|
164
189
|
if (!stripePricingPlanSubscriptionId) {
|
|
165
190
|
throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
|
|
@@ -199,7 +224,8 @@ var require_billing = __commonJS({
|
|
|
199
224
|
};
|
|
200
225
|
const unsubscribe = async ({
|
|
201
226
|
currency,
|
|
202
|
-
id
|
|
227
|
+
id,
|
|
228
|
+
idempotencyKey
|
|
203
229
|
}) => {
|
|
204
230
|
try {
|
|
205
231
|
const { data } = await axios.post(
|
|
@@ -218,17 +244,17 @@ var require_billing = __commonJS({
|
|
|
218
244
|
}
|
|
219
245
|
]
|
|
220
246
|
},
|
|
221
|
-
|
|
247
|
+
withIdem(idempotencyKey, "intent.create")
|
|
222
248
|
);
|
|
223
249
|
await axios.post(
|
|
224
250
|
billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
|
|
225
251
|
{},
|
|
226
|
-
|
|
252
|
+
withIdem(idempotencyKey, "intent.reserve")
|
|
227
253
|
);
|
|
228
254
|
const commit = await axios.post(
|
|
229
255
|
billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
|
|
230
256
|
{},
|
|
231
|
-
|
|
257
|
+
withIdem(idempotencyKey, "intent.commit")
|
|
232
258
|
);
|
|
233
259
|
return commit;
|
|
234
260
|
} catch (error) {
|
|
@@ -244,28 +270,30 @@ var require_billing = __commonJS({
|
|
|
244
270
|
const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
|
|
245
271
|
const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
|
|
246
272
|
const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
|
|
247
|
-
|
|
273
|
+
const sendMeterEvent = (token_type, value) => {
|
|
248
274
|
stripe.billing.meterEvents.create({
|
|
249
275
|
event_name,
|
|
250
276
|
payload: {
|
|
251
277
|
model,
|
|
252
278
|
stripe_customer_id: customer,
|
|
253
|
-
token_type
|
|
254
|
-
value
|
|
279
|
+
token_type,
|
|
280
|
+
value
|
|
255
281
|
}
|
|
282
|
+
}).catch((error) => {
|
|
283
|
+
console.error("[billing.usage] meterEvents.create failed", {
|
|
284
|
+
customer,
|
|
285
|
+
token_type,
|
|
286
|
+
value,
|
|
287
|
+
error: error == null ? void 0 : error.message
|
|
288
|
+
});
|
|
256
289
|
});
|
|
290
|
+
};
|
|
291
|
+
if (input) {
|
|
292
|
+
sendMeterEvent("input", input);
|
|
257
293
|
}
|
|
258
294
|
;
|
|
259
295
|
if (output) {
|
|
260
|
-
|
|
261
|
-
event_name,
|
|
262
|
-
payload: {
|
|
263
|
-
model,
|
|
264
|
-
stripe_customer_id: customer,
|
|
265
|
-
token_type: "output",
|
|
266
|
-
value: output
|
|
267
|
-
}
|
|
268
|
-
});
|
|
296
|
+
sendMeterEvent("output", output);
|
|
269
297
|
}
|
|
270
298
|
;
|
|
271
299
|
};
|
|
@@ -568,6 +596,7 @@ var require_subscription = __commonJS({
|
|
|
568
596
|
billingCycleAnchor,
|
|
569
597
|
customer,
|
|
570
598
|
discounts = [],
|
|
599
|
+
idempotencyKey,
|
|
571
600
|
metadata,
|
|
572
601
|
stripePaymentMethodId,
|
|
573
602
|
to
|
|
@@ -576,6 +605,7 @@ var require_subscription = __commonJS({
|
|
|
576
605
|
try {
|
|
577
606
|
const actions = await createItem({
|
|
578
607
|
currency: to.currency,
|
|
608
|
+
idempotencyKey,
|
|
579
609
|
interval: to.interval,
|
|
580
610
|
limit: to.limits.organization.actions,
|
|
581
611
|
metadata,
|
|
@@ -583,31 +613,37 @@ var require_subscription = __commonJS({
|
|
|
583
613
|
overage: to.overages.actions,
|
|
584
614
|
proration_behavior: "none"
|
|
585
615
|
});
|
|
586
|
-
const subscription = await stripe.subscriptions.create(
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
616
|
+
const subscription = await stripe.subscriptions.create(
|
|
617
|
+
{
|
|
618
|
+
automatic_tax: {
|
|
619
|
+
enabled: true
|
|
620
|
+
},
|
|
621
|
+
customer,
|
|
622
|
+
description: "Organization subscription: " + metadata.organization,
|
|
623
|
+
default_payment_method: stripePaymentMethodId,
|
|
624
|
+
discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
|
|
625
|
+
items: [
|
|
626
|
+
{
|
|
627
|
+
price: actions.price
|
|
628
|
+
}
|
|
629
|
+
],
|
|
630
|
+
metadata,
|
|
631
|
+
...billingCycleAnchor && {
|
|
632
|
+
billing_cycle_anchor: billingCycleAnchor
|
|
597
633
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
billing_cycle_anchor: billingCycleAnchor
|
|
602
|
-
}
|
|
603
|
-
});
|
|
634
|
+
},
|
|
635
|
+
idem(idempotencyKey, "subscription.create")
|
|
636
|
+
);
|
|
604
637
|
const { product } = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
|
|
605
|
-
const plan = await stripe.subscriptionItems.create(
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
638
|
+
const plan = await stripe.subscriptionItems.create(
|
|
639
|
+
{
|
|
640
|
+
subscription: subscription == null ? void 0 : subscription.id,
|
|
641
|
+
price: to == null ? void 0 : to.stripePriceId,
|
|
642
|
+
quantity: 1,
|
|
643
|
+
proration_behavior: "always_invoice"
|
|
644
|
+
},
|
|
645
|
+
idem(idempotencyKey, "plan.create")
|
|
646
|
+
);
|
|
611
647
|
return {
|
|
612
648
|
discounts,
|
|
613
649
|
items: {
|
|
@@ -629,6 +665,7 @@ var require_subscription = __commonJS({
|
|
|
629
665
|
}
|
|
630
666
|
},
|
|
631
667
|
pause: async ({
|
|
668
|
+
idempotencyKey,
|
|
632
669
|
stripeSubscriptionId
|
|
633
670
|
}) => {
|
|
634
671
|
try {
|
|
@@ -640,7 +677,15 @@ var require_subscription = __commonJS({
|
|
|
640
677
|
const { data } = await axios.post(
|
|
641
678
|
subscriptions + "/" + stripeSubscriptionId + "/pause",
|
|
642
679
|
body,
|
|
643
|
-
|
|
680
|
+
{
|
|
681
|
+
...options,
|
|
682
|
+
...idempotencyKey && {
|
|
683
|
+
headers: {
|
|
684
|
+
...options.headers,
|
|
685
|
+
"Idempotency-Key": idempotencyKey
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
644
689
|
);
|
|
645
690
|
return data;
|
|
646
691
|
} catch (error) {
|
|
@@ -648,6 +693,7 @@ var require_subscription = __commonJS({
|
|
|
648
693
|
}
|
|
649
694
|
},
|
|
650
695
|
resume: async ({
|
|
696
|
+
idempotencyKey,
|
|
651
697
|
stripeSubscriptionId
|
|
652
698
|
}) => {
|
|
653
699
|
try {
|
|
@@ -657,7 +703,15 @@ var require_subscription = __commonJS({
|
|
|
657
703
|
const { data } = await axios.post(
|
|
658
704
|
subscriptions + "/" + stripeSubscriptionId + "/resume",
|
|
659
705
|
body,
|
|
660
|
-
|
|
706
|
+
{
|
|
707
|
+
...options,
|
|
708
|
+
...idempotencyKey && {
|
|
709
|
+
headers: {
|
|
710
|
+
...options.headers,
|
|
711
|
+
"Idempotency-Key": idempotencyKey
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
661
715
|
);
|
|
662
716
|
return data;
|
|
663
717
|
} catch (error) {
|
package/package.json
CHANGED