@fantastic.dev/better-stripe 0.44.0 → 0.45.1

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.
@@ -0,0 +1,293 @@
1
+ // Test-only Stripe HTTP boundary. Unexpected endpoints fail instead of reaching Stripe.
2
+ import Stripe from "stripe";
3
+ export const secret = "sk_test_cross_domain_runtime_only";
4
+ export const ga = "2026-09-30.endive";
5
+ export const preview = "2026-09-30.preview";
6
+ export const wire = {
7
+ calls: [] as {
8
+ path: string;
9
+ method: string;
10
+ version: string | null;
11
+ key: string | null;
12
+ authenticated: boolean;
13
+ }[],
14
+ enabled: new Set<string>(),
15
+ attached: new Set<string>(),
16
+ transfers: [] as Record<string, unknown>[],
17
+ items: [] as Record<string, unknown>[],
18
+ refunded: false,
19
+ dispute: "none" as "none" | "lost" | "won",
20
+ invoicePaid: false,
21
+ loseApply: false,
22
+ loseTransfer: false,
23
+ loseCreate: false,
24
+ gate: null as Promise<void> | null,
25
+ transferGate: null as Promise<void> | null,
26
+ projectionFailures: 0,
27
+ };
28
+ const list = (data: unknown[]) => ({ object: "list", data, has_more: false });
29
+ const tx = (id: string, source: string, amount: number, fee: number) => ({
30
+ id,
31
+ object: "balance_transaction",
32
+ source,
33
+ amount,
34
+ fee,
35
+ net: amount - fee,
36
+ currency: "usd",
37
+ created: 1,
38
+ fee_details: [],
39
+ });
40
+ export function reset() {
41
+ Object.assign(wire, {
42
+ calls: [],
43
+ enabled: new Set(),
44
+ attached: new Set(),
45
+ transfers: [],
46
+ items: [],
47
+ refunded: false,
48
+ dispute: "none",
49
+ invoicePaid: false,
50
+ loseApply: false,
51
+ loseTransfer: false,
52
+ loseCreate: false,
53
+ gate: null,
54
+ transferGate: null,
55
+ projectionFailures: 0,
56
+ });
57
+ }
58
+ export const fixtureFetch: typeof fetch = async (url, init) => {
59
+ const u = new URL(String(url)),
60
+ path = u.pathname;
61
+ const method = init?.method ?? "GET",
62
+ h = new Headers(init?.headers);
63
+ wire.calls.push({
64
+ path,
65
+ method,
66
+ version: h.get("Stripe-Version"),
67
+ key: h.get("Idempotency-Key"),
68
+ authenticated: h.get("Authorization") === `Bearer ${secret}`,
69
+ });
70
+ const p = new URLSearchParams(String(init?.body ?? ""));
71
+ let body: unknown;
72
+ if (path.includes("/subscriptions/")) {
73
+ const suffix = path.split("_").pop()!;
74
+ if (method === "POST") {
75
+ if (p.get("default_payment_method") === `pm_balance_${suffix}`)
76
+ wire.enabled.add(suffix);
77
+ else wire.enabled.delete(suffix);
78
+ if (wire.loseApply) {
79
+ wire.loseApply = false;
80
+ throw new TypeError("private transport body");
81
+ }
82
+ }
83
+ body = {
84
+ id: `sub_${suffix}`,
85
+ object: "subscription",
86
+ status: "active",
87
+ customer_account: `acct_${suffix}`,
88
+ default_payment_method: wire.enabled.has(suffix)
89
+ ? `pm_balance_${suffix}`
90
+ : `pm_card_${suffix}`,
91
+ payment_settings: {
92
+ payment_method_types: [
93
+ wire.enabled.has(suffix) ? "stripe_balance" : "card",
94
+ ],
95
+ },
96
+ };
97
+ } else if (path.includes("payment_methods")) {
98
+ let suffix = path.split("_").pop()!.split("/")[0];
99
+ if (path.endsWith("/payment_methods")) {
100
+ suffix = "a";
101
+ if (wire.gate) await wire.gate;
102
+ if (wire.loseCreate) {
103
+ wire.loseCreate = false;
104
+ throw new TypeError("private create body");
105
+ }
106
+ }
107
+ if (path.endsWith("/attach")) wire.attached.add(suffix);
108
+ const balance = !path.includes("pm_card_");
109
+ body = {
110
+ id: `pm_${balance ? "balance" : "card"}_${suffix}`,
111
+ object: "payment_method",
112
+ type: balance ? "stripe_balance" : "card",
113
+ customer_account:
114
+ balance && !wire.attached.has(suffix) ? null : `acct_${suffix}`,
115
+ };
116
+ } else if (path.endsWith("/transfers")) {
117
+ if (method === "POST") {
118
+ const transfer = {
119
+ id: `tr_${wire.transfers.length + 1}`,
120
+ object: "transfer",
121
+ amount: Number(p.get("amount")),
122
+ currency: p.get("currency"),
123
+ destination: p.get("destination"),
124
+ source_transaction: p.get("source_transaction"),
125
+ transfer_group: p.get("transfer_group"),
126
+ amount_reversed: 0,
127
+ metadata: {
128
+ bsRole: p.get("metadata[bsRole]"),
129
+ bsSourceCharge: p.get("metadata[bsSourceCharge]"),
130
+ },
131
+ };
132
+ wire.transfers.push(transfer);
133
+ body = transfer;
134
+ if (wire.transferGate) await wire.transferGate;
135
+ if (wire.loseTransfer) {
136
+ wire.loseTransfer = false;
137
+ throw new TypeError("private transfer body");
138
+ }
139
+ } else
140
+ body = list(
141
+ wire.transfers.filter(
142
+ (t) =>
143
+ !u.searchParams.has("transfer_group") ||
144
+ t.transfer_group === u.searchParams.get("transfer_group"),
145
+ ),
146
+ );
147
+ } else if (path.endsWith("/invoiceitems")) {
148
+ if (method === "POST") {
149
+ const item = {
150
+ id: `ii_${wire.items.length + 1}`,
151
+ object: "invoiceitem",
152
+ invoice: p.get("invoice"),
153
+ customer_account: p.get("customer_account"),
154
+ currency: p.get("currency"),
155
+ amount: Number(p.get("amount")),
156
+ discountable: false,
157
+ metadata: { bsRecoveryGroup: p.get("metadata[bsRecoveryGroup]") },
158
+ };
159
+ wire.items.push(item);
160
+ body = item;
161
+ } else
162
+ body = list(
163
+ wire.items.filter((i) => i.invoice === u.searchParams.get("invoice")),
164
+ );
165
+ } else if (path.includes("/payment_intents/")) {
166
+ body = {
167
+ id: "pi_source",
168
+ object: "payment_intent",
169
+ status: "succeeded",
170
+ amount: 1000,
171
+ amount_received: 1000,
172
+ currency: "usd",
173
+ customer_account: "acct_buyer",
174
+ livemode: false,
175
+ metadata: {},
176
+ latest_charge: charge(),
177
+ };
178
+ } else if (path.includes("/charges/")) body = charge();
179
+ else if (path.endsWith("/refunds"))
180
+ body = list(
181
+ wire.refunded
182
+ ? [
183
+ {
184
+ id: "re_source",
185
+ object: "refund",
186
+ amount: 1000,
187
+ status: "succeeded",
188
+ currency: "usd",
189
+ charge: "ch_source",
190
+ payment_intent: "pi_source",
191
+ balance_transaction: tx("txn_refund", "re_source", -1000, 0),
192
+ failure_balance_transaction: null,
193
+ },
194
+ ]
195
+ : [],
196
+ );
197
+ else if (path.endsWith("/disputes"))
198
+ body = list(
199
+ wire.dispute === "none"
200
+ ? []
201
+ : [
202
+ {
203
+ id: "dp_source",
204
+ object: "dispute",
205
+ amount: 600,
206
+ status: wire.dispute,
207
+ currency: "usd",
208
+ charge: "ch_source",
209
+ payment_intent: "pi_source",
210
+ balance_transactions: [
211
+ tx("txn_dispute", "dp_source", -600, 15),
212
+ ...(wire.dispute === "won"
213
+ ? [tx("txn_dispute_credit", "dp_source", 600, -15)]
214
+ : []),
215
+ ],
216
+ },
217
+ ],
218
+ );
219
+ else if (path.endsWith("/invoice_payments"))
220
+ body = list([
221
+ {
222
+ id: "inpay_source",
223
+ object: "invoice_payment",
224
+ invoice: "in_original",
225
+ status: "paid",
226
+ currency: "usd",
227
+ amount_paid: 1000,
228
+ amount_requested: 1000,
229
+ livemode: false,
230
+ payment: { type: "payment_intent", payment_intent: "pi_source" },
231
+ },
232
+ ]);
233
+ else if (path.endsWith("/lines"))
234
+ body = list([
235
+ {
236
+ id: "il_original",
237
+ object: "line_item",
238
+ invoice: "in_original",
239
+ currency: "usd",
240
+ amount: 1000,
241
+ quantity: 1,
242
+ livemode: false,
243
+ parent: {
244
+ type: "invoice_item_details",
245
+ invoice_item_details: { proration: false },
246
+ },
247
+ pricing: {
248
+ type: "price_details",
249
+ price_details: { price: "price_original", product: "prod_original" },
250
+ },
251
+ },
252
+ ]);
253
+ else if (path.includes("/invoices/")) {
254
+ const id = path.split("/").pop()!;
255
+ const original = id === "in_original";
256
+ body = {
257
+ id,
258
+ object: "invoice",
259
+ status: original || wire.invoicePaid ? "paid" : "draft",
260
+ currency: "usd",
261
+ amount_paid: original ? 1000 : 0,
262
+ amount_due: original ? 1000 : 0,
263
+ amount_remaining: 0,
264
+ total: original ? 1000 : 0,
265
+ amount_overpaid: 0,
266
+ customer_account: original ? "acct_buyer" : `acct_${id.split("_").pop()}`,
267
+ livemode: false,
268
+ };
269
+ } else throw new Error(`Unexpected fixture endpoint ${method} ${path}`);
270
+ return new Response(JSON.stringify(body), {
271
+ status: 200,
272
+ headers: { "content-type": "application/json" },
273
+ });
274
+ };
275
+ export const httpClient = Stripe.createFetchHttpClient(fixtureFetch);
276
+ function charge() {
277
+ return {
278
+ id: "ch_source",
279
+ object: "charge",
280
+ payment_intent: "pi_source",
281
+ amount: 1000,
282
+ amount_captured: 1000,
283
+ currency: "usd",
284
+ paid: true,
285
+ captured: true,
286
+ status: "succeeded",
287
+ amount_refunded: wire.refunded ? 1000 : 0,
288
+ disputed: wire.dispute !== "none",
289
+ livemode: false,
290
+ created: 1,
291
+ balance_transaction: tx("txn_source", "ch_source", 1000, 30),
292
+ };
293
+ }