@fantastic.dev/better-stripe 0.77.0 → 0.79.0
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/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/client/core/accountLinks.d.ts +17 -1
- package/dist/client/core/accountLinks.d.ts.map +1 -1
- package/dist/client/core/accountLinks.js +72 -11
- package/dist/client/core/accountLinks.js.map +1 -1
- package/dist/client/core/billingPortalClient.d.ts.map +1 -1
- package/dist/client/core/billingPortalClient.js +1 -1
- package/dist/client/core/billingPortalClient.js.map +1 -1
- package/dist/client/core/billingPortalFlow.d.ts +69 -0
- package/dist/client/core/billingPortalFlow.d.ts.map +1 -0
- package/dist/client/core/billingPortalFlow.js +81 -0
- package/dist/client/core/billingPortalFlow.js.map +1 -0
- package/dist/client/core/idempotency.d.ts.map +1 -1
- package/dist/client/core/idempotency.js +4 -1
- package/dist/client/core/idempotency.js.map +1 -1
- package/dist/client/helpers.d.ts +3 -1
- package/dist/client/helpers.d.ts.map +1 -1
- package/dist/client/helpers.js +13 -2
- package/dist/client/helpers.js.map +1 -1
- package/dist/client/index.d.ts +5 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/invoiceReads.d.ts +3 -1
- package/dist/client/invoiceReads.d.ts.map +1 -1
- package/dist/client/singleAttemptHttpClient.d.ts +11 -0
- package/dist/client/singleAttemptHttpClient.d.ts.map +1 -0
- package/dist/client/singleAttemptHttpClient.js +51 -0
- package/dist/client/singleAttemptHttpClient.js.map +1 -0
- package/dist/component/balanceFunding/mutations.d.ts +16 -16
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/react/workflows/Billing.d.ts.map +1 -1
- package/dist/react/workflows/Billing.js +6 -4
- package/dist/react/workflows/Billing.js.map +1 -1
- package/docs/client-api.md +123 -0
- package/package.json +1 -1
- package/src/client/core/accountLinks.test.ts +4 -4
- package/src/client/core/accountLinks.ts +120 -13
- package/src/client/core/billingPortal.test.ts +8 -6
- package/src/client/core/billingPortalClient.ts +5 -1
- package/src/client/core/billingPortalFlow.test.ts +349 -0
- package/src/client/core/billingPortalFlow.ts +98 -0
- package/src/client/core/billingPortalIdentity.test.ts +418 -0
- package/src/client/core/idempotency.ts +6 -1
- package/src/client/customer-preview.test.ts +188 -7
- package/src/client/helpers.ts +15 -1
- package/src/client/index.ts +5 -2
- package/src/client/invoiceReads.ts +1 -1
- package/src/client/singleAttemptHttpClient.test.ts +70 -0
- package/src/client/singleAttemptHttpClient.ts +58 -0
- package/src/react/workflows/Billing.tsx +15 -8
- package/src/react/workflows/BillingPortal.test.tsx +101 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
BetterStripe,
|
|
5
|
+
prepareBillingPortalSessionRequestIdentity,
|
|
6
|
+
type BetterStripeComponent,
|
|
7
|
+
} from "../index.js";
|
|
8
|
+
import type { RunCtx } from "../helpers.js";
|
|
9
|
+
import type {
|
|
10
|
+
BillingPortalSubscriptionUpdateFlow,
|
|
11
|
+
CreateBillingPortalSessionOptions,
|
|
12
|
+
} from "./accountLinks.js";
|
|
13
|
+
|
|
14
|
+
const ctx = {} as RunCtx;
|
|
15
|
+
const flowData = {
|
|
16
|
+
type: "subscription_update_confirm",
|
|
17
|
+
subscription_update_confirm: {
|
|
18
|
+
subscription: "sub_original",
|
|
19
|
+
items: [{ id: "si_original", price: "price_target" }],
|
|
20
|
+
},
|
|
21
|
+
after_completion: {
|
|
22
|
+
type: "redirect",
|
|
23
|
+
redirect: { return_url: "https://host.example/billing/confirmed" },
|
|
24
|
+
},
|
|
25
|
+
} satisfies BillingPortalSubscriptionUpdateFlow;
|
|
26
|
+
|
|
27
|
+
function fixture(connectedAccountId?: string) {
|
|
28
|
+
const subscription = {
|
|
29
|
+
object: "subscription",
|
|
30
|
+
id: "sub_original",
|
|
31
|
+
customer_account: "acct_customer",
|
|
32
|
+
items: {
|
|
33
|
+
has_more: false,
|
|
34
|
+
data: [{ id: "si_original", price: { id: "price_current" } }],
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const retrieve = vi.fn().mockResolvedValue(subscription);
|
|
38
|
+
const create = vi
|
|
39
|
+
.fn()
|
|
40
|
+
.mockResolvedValue({ url: "https://billing.stripe.test" });
|
|
41
|
+
class Client extends BetterStripe {
|
|
42
|
+
protected stripe() {
|
|
43
|
+
return {
|
|
44
|
+
subscriptions: { retrieve },
|
|
45
|
+
billingPortal: { sessions: { create } },
|
|
46
|
+
} as unknown as Stripe;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const client = new Client({} as BetterStripeComponent);
|
|
50
|
+
const options = {
|
|
51
|
+
stripeAccountId: "acct_customer",
|
|
52
|
+
connectedAccountId,
|
|
53
|
+
configurationId: "bpc_policy",
|
|
54
|
+
returnUrl: "https://host.example/billing",
|
|
55
|
+
flowData,
|
|
56
|
+
} satisfies CreateBillingPortalSessionOptions;
|
|
57
|
+
return { client, options, subscription, retrieve, create };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe("caller-owned Billing Portal request identity", () => {
|
|
61
|
+
it.each([undefined, "acct_connected"])(
|
|
62
|
+
"replays an ordinary configured request identity in scope %s",
|
|
63
|
+
async (connectedAccountId) => {
|
|
64
|
+
const f = fixture(connectedAccountId);
|
|
65
|
+
const { flowData: _flowData, ...ordinary } = f.options;
|
|
66
|
+
const requestIdentity = prepareBillingPortalSessionRequestIdentity(
|
|
67
|
+
ordinary,
|
|
68
|
+
"portal:ordinary",
|
|
69
|
+
);
|
|
70
|
+
await f.client.createBillingPortalSession(ctx, {
|
|
71
|
+
...ordinary,
|
|
72
|
+
requestIdentity,
|
|
73
|
+
});
|
|
74
|
+
await f.client.createBillingPortalSession(ctx, {
|
|
75
|
+
...ordinary,
|
|
76
|
+
requestIdentity: JSON.parse(JSON.stringify(requestIdentity)),
|
|
77
|
+
});
|
|
78
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
79
|
+
expect(f.create).toHaveBeenCalledTimes(2);
|
|
80
|
+
expect(f.create.mock.calls[0]).toEqual([
|
|
81
|
+
{
|
|
82
|
+
customer_account: "acct_customer",
|
|
83
|
+
configuration: "bpc_policy",
|
|
84
|
+
return_url: ordinary.returnUrl,
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
...(connectedAccountId ? { stripeAccount: connectedAccountId } : {}),
|
|
88
|
+
maxNetworkRetries: 0,
|
|
89
|
+
idempotencyKey: "portal:ordinary",
|
|
90
|
+
},
|
|
91
|
+
]);
|
|
92
|
+
expect(f.create.mock.calls[1]).toEqual(f.create.mock.calls[0]);
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
it("rejects an ordinary request's mismatched retained binding before provider I/O", async () => {
|
|
96
|
+
const f = fixture();
|
|
97
|
+
const { flowData: _flowData, ...ordinary } = f.options;
|
|
98
|
+
await expect(
|
|
99
|
+
f.client.createBillingPortalSession(ctx, {
|
|
100
|
+
...ordinary,
|
|
101
|
+
requestIdentity: {
|
|
102
|
+
key: "portal:original",
|
|
103
|
+
binding: "different-request",
|
|
104
|
+
},
|
|
105
|
+
} as CreateBillingPortalSessionOptions),
|
|
106
|
+
).rejects.toThrow();
|
|
107
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
108
|
+
expect(f.create).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it.each([undefined, "acct_connected"])(
|
|
112
|
+
"retains one caller key and complete targeted request in scope %s",
|
|
113
|
+
async (connectedAccountId) => {
|
|
114
|
+
const f = fixture(connectedAccountId);
|
|
115
|
+
const identity = prepareBillingPortalSessionRequestIdentity(
|
|
116
|
+
f.options,
|
|
117
|
+
"portal:original",
|
|
118
|
+
);
|
|
119
|
+
const retained = JSON.parse(JSON.stringify(identity));
|
|
120
|
+
const responses = await Promise.all([
|
|
121
|
+
f.client.createBillingPortalSession(ctx, {
|
|
122
|
+
...f.options,
|
|
123
|
+
requestIdentity: retained,
|
|
124
|
+
}),
|
|
125
|
+
f.client.createBillingPortalSession(ctx, {
|
|
126
|
+
...f.options,
|
|
127
|
+
requestIdentity: retained,
|
|
128
|
+
}),
|
|
129
|
+
]);
|
|
130
|
+
expect(responses).toEqual([
|
|
131
|
+
{ url: "https://billing.stripe.test" },
|
|
132
|
+
{ url: "https://billing.stripe.test" },
|
|
133
|
+
]);
|
|
134
|
+
expect(f.create).toHaveBeenCalledTimes(2);
|
|
135
|
+
for (const call of f.create.mock.calls)
|
|
136
|
+
expect(call[1]).toEqual({
|
|
137
|
+
...(connectedAccountId ? { stripeAccount: connectedAccountId } : {}),
|
|
138
|
+
maxNetworkRetries: 0,
|
|
139
|
+
idempotencyKey: "portal:original",
|
|
140
|
+
});
|
|
141
|
+
for (const call of f.retrieve.mock.calls)
|
|
142
|
+
expect(call).toEqual([
|
|
143
|
+
"sub_original",
|
|
144
|
+
{},
|
|
145
|
+
{
|
|
146
|
+
...(connectedAccountId
|
|
147
|
+
? { stripeAccount: connectedAccountId }
|
|
148
|
+
: {}),
|
|
149
|
+
maxNetworkRetries: 0,
|
|
150
|
+
},
|
|
151
|
+
]);
|
|
152
|
+
expect(f.create.mock.calls[0]?.[0]).toEqual(f.create.mock.calls[1]?.[0]);
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const changes: Array<
|
|
157
|
+
[string, (options: CreateBillingPortalSessionOptions) => void]
|
|
158
|
+
> = [
|
|
159
|
+
[
|
|
160
|
+
"customer account",
|
|
161
|
+
(o) => {
|
|
162
|
+
o.stripeAccountId = "acct_other";
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
[
|
|
166
|
+
"connected account",
|
|
167
|
+
(o) => {
|
|
168
|
+
o.connectedAccountId = "acct_other";
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
[
|
|
172
|
+
"platform scope",
|
|
173
|
+
(o) => {
|
|
174
|
+
o.connectedAccountId = undefined;
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
[
|
|
178
|
+
"configuration",
|
|
179
|
+
(o) => {
|
|
180
|
+
o.configurationId = "bpc_other";
|
|
181
|
+
},
|
|
182
|
+
],
|
|
183
|
+
[
|
|
184
|
+
"default configuration",
|
|
185
|
+
(o) => {
|
|
186
|
+
o.configurationId = undefined;
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
[
|
|
190
|
+
"return routing",
|
|
191
|
+
(o) => {
|
|
192
|
+
o.returnUrl = "https://host.example/other";
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
[
|
|
196
|
+
"subscription",
|
|
197
|
+
(o) => {
|
|
198
|
+
o.flowData!.subscription_update_confirm.subscription = "sub_other";
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
[
|
|
202
|
+
"item",
|
|
203
|
+
(o) => {
|
|
204
|
+
o.flowData!.subscription_update_confirm.items[0].id = "si_other";
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
[
|
|
208
|
+
"price",
|
|
209
|
+
(o) => {
|
|
210
|
+
o.flowData!.subscription_update_confirm.items[0].price = "price_other";
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
[
|
|
214
|
+
"quantity",
|
|
215
|
+
(o) => {
|
|
216
|
+
o.flowData!.subscription_update_confirm.items[0].quantity = 2;
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
[
|
|
220
|
+
"completion routing",
|
|
221
|
+
(o) => {
|
|
222
|
+
o.flowData!.after_completion!.redirect.return_url =
|
|
223
|
+
"https://host.example/other";
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
[
|
|
227
|
+
"default completion",
|
|
228
|
+
(o) => {
|
|
229
|
+
o.flowData!.after_completion = undefined;
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
"account-wide flow",
|
|
234
|
+
(o) => {
|
|
235
|
+
o.flowData = undefined;
|
|
236
|
+
},
|
|
237
|
+
],
|
|
238
|
+
];
|
|
239
|
+
it.each(changes)(
|
|
240
|
+
"rejects a changed %s under the retained identity before I/O",
|
|
241
|
+
async (_name, change) => {
|
|
242
|
+
const f = fixture("acct_connected");
|
|
243
|
+
const original = structuredClone(f.options);
|
|
244
|
+
const requestIdentity = prepareBillingPortalSessionRequestIdentity(
|
|
245
|
+
original,
|
|
246
|
+
"portal:immutable",
|
|
247
|
+
);
|
|
248
|
+
const changed: CreateBillingPortalSessionOptions =
|
|
249
|
+
structuredClone(original);
|
|
250
|
+
change(changed);
|
|
251
|
+
await expect(
|
|
252
|
+
f.client.createBillingPortalSession(ctx, {
|
|
253
|
+
...changed,
|
|
254
|
+
requestIdentity,
|
|
255
|
+
}),
|
|
256
|
+
).rejects.toMatchObject({ data: { code: "INVALID_CONFIGURATION" } });
|
|
257
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
258
|
+
expect(f.create).not.toHaveBeenCalled();
|
|
259
|
+
},
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
it("normalizes supported object ordering and omitted optional fields", async () => {
|
|
263
|
+
const f = fixture();
|
|
264
|
+
const first = prepareBillingPortalSessionRequestIdentity(
|
|
265
|
+
f.options,
|
|
266
|
+
"portal:normalized",
|
|
267
|
+
);
|
|
268
|
+
const reordered = {
|
|
269
|
+
flowData: {
|
|
270
|
+
after_completion: flowData.after_completion,
|
|
271
|
+
subscription_update_confirm: {
|
|
272
|
+
items: [
|
|
273
|
+
{ price: "price_target", quantity: undefined, id: "si_original" },
|
|
274
|
+
],
|
|
275
|
+
subscription: "sub_original",
|
|
276
|
+
},
|
|
277
|
+
type: "subscription_update_confirm" as const,
|
|
278
|
+
},
|
|
279
|
+
returnUrl: f.options.returnUrl,
|
|
280
|
+
stripeAccountId: f.options.stripeAccountId,
|
|
281
|
+
configurationId: f.options.configurationId,
|
|
282
|
+
} satisfies CreateBillingPortalSessionOptions;
|
|
283
|
+
expect(
|
|
284
|
+
prepareBillingPortalSessionRequestIdentity(reordered, first.key),
|
|
285
|
+
).toEqual(first);
|
|
286
|
+
await f.client.createBillingPortalSession(ctx, {
|
|
287
|
+
...reordered,
|
|
288
|
+
requestIdentity: first,
|
|
289
|
+
});
|
|
290
|
+
expect(f.create).toHaveBeenCalledOnce();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it.each([
|
|
294
|
+
"",
|
|
295
|
+
" ",
|
|
296
|
+
"bad\nkey",
|
|
297
|
+
"bad\rkey",
|
|
298
|
+
"x".repeat(256),
|
|
299
|
+
null,
|
|
300
|
+
42,
|
|
301
|
+
undefined,
|
|
302
|
+
])("rejects invalid caller key %j", (key) => {
|
|
303
|
+
const f = fixture();
|
|
304
|
+
expect(() =>
|
|
305
|
+
prepareBillingPortalSessionRequestIdentity(f.options, key as string),
|
|
306
|
+
).toThrow();
|
|
307
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
308
|
+
expect(f.create).not.toHaveBeenCalled();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it.each([
|
|
312
|
+
null,
|
|
313
|
+
[],
|
|
314
|
+
"key",
|
|
315
|
+
{},
|
|
316
|
+
{ key: "key", binding: "wrong", extra: true },
|
|
317
|
+
{ key: 42, binding: "wrong" },
|
|
318
|
+
{ key: "key", binding: 42 },
|
|
319
|
+
{ key: "key", binding: "better-stripe:billing-portal:v2:{}" },
|
|
320
|
+
])(
|
|
321
|
+
"rejects malformed or unsupported identity %j before dispatch",
|
|
322
|
+
async (requestIdentity) => {
|
|
323
|
+
const f = fixture();
|
|
324
|
+
const { flowData: _flowData, ...ordinary } = f.options;
|
|
325
|
+
await expect(
|
|
326
|
+
f.client.createBillingPortalSession(ctx, {
|
|
327
|
+
...ordinary,
|
|
328
|
+
requestIdentity,
|
|
329
|
+
} as CreateBillingPortalSessionOptions),
|
|
330
|
+
).rejects.toThrow();
|
|
331
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
332
|
+
expect(f.create).not.toHaveBeenCalled();
|
|
333
|
+
},
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
it.each([
|
|
337
|
+
{
|
|
338
|
+
type: "StripeIdempotencyError",
|
|
339
|
+
code: "idempotency_key_in_use",
|
|
340
|
+
message: "Request conflicts with the original parameters",
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
type: "StripeConnectionError",
|
|
344
|
+
code: "ETIMEDOUT",
|
|
345
|
+
message: "Outcome unknown",
|
|
346
|
+
},
|
|
347
|
+
])(
|
|
348
|
+
"keeps provider $type visible after one keyed attempt",
|
|
349
|
+
async (failure) => {
|
|
350
|
+
const f = fixture("acct_connected");
|
|
351
|
+
const requestIdentity = prepareBillingPortalSessionRequestIdentity(
|
|
352
|
+
f.options,
|
|
353
|
+
"portal:unknown",
|
|
354
|
+
);
|
|
355
|
+
f.create.mockRejectedValue(failure);
|
|
356
|
+
await expect(
|
|
357
|
+
f.client.createBillingPortalSession(ctx, {
|
|
358
|
+
...f.options,
|
|
359
|
+
requestIdentity,
|
|
360
|
+
}),
|
|
361
|
+
).rejects.toMatchObject({
|
|
362
|
+
data: { code: "STRIPE_API_ERROR", stripeError: failure },
|
|
363
|
+
});
|
|
364
|
+
expect(f.create).toHaveBeenCalledTimes(1);
|
|
365
|
+
expect(f.create.mock.calls[0]?.[1]).toEqual({
|
|
366
|
+
stripeAccount: "acct_connected",
|
|
367
|
+
maxNetworkRetries: 0,
|
|
368
|
+
idempotencyKey: "portal:unknown",
|
|
369
|
+
});
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
it("snapshots the caller identity and request before awaiting provider reads", async () => {
|
|
374
|
+
const f = fixture("acct_connected");
|
|
375
|
+
const options: CreateBillingPortalSessionOptions = structuredClone(
|
|
376
|
+
f.options,
|
|
377
|
+
);
|
|
378
|
+
options.requestIdentity = prepareBillingPortalSessionRequestIdentity(
|
|
379
|
+
options,
|
|
380
|
+
"portal:snapshot",
|
|
381
|
+
);
|
|
382
|
+
let release!: (value: typeof f.subscription) => void;
|
|
383
|
+
f.retrieve.mockReturnValue(
|
|
384
|
+
new Promise((resolve) => {
|
|
385
|
+
release = resolve;
|
|
386
|
+
}),
|
|
387
|
+
);
|
|
388
|
+
const pending = f.client.createBillingPortalSession(ctx, options);
|
|
389
|
+
options.requestIdentity.key = "rotated";
|
|
390
|
+
options.requestIdentity.binding = "changed";
|
|
391
|
+
options.connectedAccountId = "acct_changed";
|
|
392
|
+
options.flowData!.subscription_update_confirm.items[0].price =
|
|
393
|
+
"price_changed";
|
|
394
|
+
release(f.subscription);
|
|
395
|
+
await pending;
|
|
396
|
+
expect(f.create.mock.calls[0]?.[1]).toEqual({
|
|
397
|
+
stripeAccount: "acct_connected",
|
|
398
|
+
maxNetworkRetries: 0,
|
|
399
|
+
idempotencyKey: "portal:snapshot",
|
|
400
|
+
});
|
|
401
|
+
expect(f.create.mock.calls[0]?.[0].flow_data).toEqual(flowData);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("keeps ordinary omission compatible while disabling automatic creation retries", async () => {
|
|
405
|
+
const f = fixture();
|
|
406
|
+
const { flowData: _flowData, ...ordinary } = f.options;
|
|
407
|
+
await f.client.createBillingPortalSession(ctx, ordinary);
|
|
408
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
409
|
+
expect(f.create).toHaveBeenCalledExactlyOnceWith(
|
|
410
|
+
{
|
|
411
|
+
customer_account: "acct_customer",
|
|
412
|
+
return_url: ordinary.returnUrl,
|
|
413
|
+
configuration: "bpc_policy",
|
|
414
|
+
},
|
|
415
|
+
{ maxNetworkRetries: 0 },
|
|
416
|
+
);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
@@ -2,7 +2,12 @@ import { throwStripeError } from "../errors.js";
|
|
|
2
2
|
/** Explicit request identity. Omission preserves the SDK's existing behavior. */
|
|
3
3
|
export function idempotencyOptions(key: string | undefined) {
|
|
4
4
|
if (key === undefined) return undefined;
|
|
5
|
-
if (
|
|
5
|
+
if (
|
|
6
|
+
typeof key !== "string" ||
|
|
7
|
+
!key.trim() ||
|
|
8
|
+
key.length > 255 ||
|
|
9
|
+
/[\r\n]/.test(key)
|
|
10
|
+
)
|
|
6
11
|
throwStripeError(
|
|
7
12
|
"INVALID_CONFIGURATION",
|
|
8
13
|
"Idempotency keys must be nonempty, at most 255 characters, and contain no line breaks.",
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import Stripe from "stripe";
|
|
2
2
|
import { componentsGeneric } from "convex/server";
|
|
3
3
|
import { expect, it, vi } from "vitest";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
BetterStripe,
|
|
6
|
+
prepareBillingPortalSessionRequestIdentity,
|
|
7
|
+
type BetterStripeOptions,
|
|
8
|
+
} from "./index.js";
|
|
5
9
|
import { getStripeClient, type Component, type RunCtx } from "./helpers.js";
|
|
6
10
|
|
|
7
11
|
const preview = "2026-09-30.preview";
|
|
@@ -19,7 +23,13 @@ const ctx = {
|
|
|
19
23
|
),
|
|
20
24
|
runMutation: vi.fn().mockResolvedValue("row"),
|
|
21
25
|
} as unknown as RunCtx;
|
|
22
|
-
|
|
26
|
+
type TransportFailure =
|
|
27
|
+
"retryable" | "conflict" | "mismatch" | "ECONNRESET" | "EPIPE" | "ETIMEDOUT";
|
|
28
|
+
function fixture(
|
|
29
|
+
version: string | undefined,
|
|
30
|
+
portalFailure?: TransportFailure,
|
|
31
|
+
failureTarget: "portal" | "subscription" = "portal",
|
|
32
|
+
) {
|
|
23
33
|
const key = `sk_test_preview_transport_${crypto.randomUUID()}`;
|
|
24
34
|
const bs = new BetterStripe(component, {
|
|
25
35
|
STRIPE_SECRET_KEY: key,
|
|
@@ -29,13 +39,12 @@ function fixture(version: string | undefined) {
|
|
|
29
39
|
path: string;
|
|
30
40
|
version: string;
|
|
31
41
|
idempotency: string | undefined;
|
|
42
|
+
connectedAccount: string | undefined;
|
|
32
43
|
body: string;
|
|
33
44
|
}[] = [];
|
|
34
45
|
for (const selected of new Set([version, "2026-09-30.endive"])) {
|
|
35
46
|
const sdk = getStripeClient(key, selected);
|
|
36
|
-
|
|
37
|
-
sdk as unknown as { _setApiField: (name: string, value: unknown) => void }
|
|
38
|
-
)._setApiField("httpClient", {
|
|
47
|
+
const transport = {
|
|
39
48
|
getClientName: () => "preview-contract-test",
|
|
40
49
|
makeRequest: async (
|
|
41
50
|
...[, , path, , headers, body]: Parameters<
|
|
@@ -46,8 +55,50 @@ function fixture(version: string | undefined) {
|
|
|
46
55
|
path,
|
|
47
56
|
version: String(headers["Stripe-Version"]),
|
|
48
57
|
idempotency: headers["Idempotency-Key"] as string | undefined,
|
|
58
|
+
connectedAccount: headers["Stripe-Account"] as string | undefined,
|
|
49
59
|
body: String(body),
|
|
50
60
|
});
|
|
61
|
+
if (
|
|
62
|
+
portalFailure &&
|
|
63
|
+
path.includes(
|
|
64
|
+
failureTarget === "portal"
|
|
65
|
+
? "/billing_portal/sessions"
|
|
66
|
+
: "/subscriptions/",
|
|
67
|
+
)
|
|
68
|
+
) {
|
|
69
|
+
if (!["retryable", "conflict", "mismatch"].includes(portalFailure))
|
|
70
|
+
throw Object.assign(
|
|
71
|
+
new Error("Controlled socket closure; outcome unknown"),
|
|
72
|
+
{ code: portalFailure },
|
|
73
|
+
);
|
|
74
|
+
return {
|
|
75
|
+
getStatusCode: () =>
|
|
76
|
+
portalFailure === "conflict"
|
|
77
|
+
? 409
|
|
78
|
+
: portalFailure === "mismatch"
|
|
79
|
+
? 400
|
|
80
|
+
: 500,
|
|
81
|
+
getHeaders: () => ({ "stripe-should-retry": "true" }),
|
|
82
|
+
getRawResponse: () => ({}),
|
|
83
|
+
toJSON: async () => ({
|
|
84
|
+
error: {
|
|
85
|
+
type: ["conflict", "mismatch"].includes(portalFailure)
|
|
86
|
+
? "idempotency_error"
|
|
87
|
+
: "api_error",
|
|
88
|
+
code:
|
|
89
|
+
portalFailure === "conflict"
|
|
90
|
+
? "idempotency_key_in_use"
|
|
91
|
+
: portalFailure === "mismatch"
|
|
92
|
+
? "idempotency_mismatch"
|
|
93
|
+
: "upstream_failure",
|
|
94
|
+
message: "Controlled provider error",
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
toStream: () => {
|
|
98
|
+
throw new Error("Unexpected stream");
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
51
102
|
const data = path.includes("/checkout/sessions")
|
|
52
103
|
? {
|
|
53
104
|
id: "cs_preview",
|
|
@@ -96,9 +147,21 @@ function fixture(version: string | undefined) {
|
|
|
96
147
|
? { id: "coupon_preview", valid: true }
|
|
97
148
|
: {
|
|
98
149
|
id: "sub_preview",
|
|
150
|
+
object: "subscription",
|
|
99
151
|
status: "active",
|
|
100
152
|
customer_account: "acct_buyer",
|
|
101
|
-
items: {
|
|
153
|
+
items: {
|
|
154
|
+
has_more: false,
|
|
155
|
+
data: [
|
|
156
|
+
{
|
|
157
|
+
id: "si_preview",
|
|
158
|
+
price: {
|
|
159
|
+
id: "price_preview",
|
|
160
|
+
product: "prod_preview",
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
},
|
|
102
165
|
metadata: {},
|
|
103
166
|
};
|
|
104
167
|
return {
|
|
@@ -111,10 +174,118 @@ function fixture(version: string | undefined) {
|
|
|
111
174
|
},
|
|
112
175
|
};
|
|
113
176
|
},
|
|
114
|
-
}
|
|
177
|
+
};
|
|
178
|
+
(
|
|
179
|
+
sdk as unknown as { _setApiField: (name: string, value: unknown) => void }
|
|
180
|
+
)._setApiField("httpClient", transport);
|
|
181
|
+
// Inject below the production adapter, so a test cannot bypass its guard.
|
|
182
|
+
const fetchFactory = vi
|
|
183
|
+
.spyOn(Stripe, "createFetchHttpClient")
|
|
184
|
+
.mockReturnValue(transport);
|
|
185
|
+
try {
|
|
186
|
+
const portal = getStripeClient(key, selected, { singleAttempt: true });
|
|
187
|
+
expect(portal).not.toBe(sdk);
|
|
188
|
+
expect(portal.getApiField("maxNetworkRetries")).toBe(0);
|
|
189
|
+
expect(portal.getApiField("version")).toBe(sdk.getApiField("version"));
|
|
190
|
+
} finally {
|
|
191
|
+
fetchFactory.mockRestore();
|
|
192
|
+
}
|
|
115
193
|
}
|
|
116
194
|
return { bs, calls };
|
|
117
195
|
}
|
|
196
|
+
it.each([
|
|
197
|
+
"retryable",
|
|
198
|
+
"conflict",
|
|
199
|
+
"mismatch",
|
|
200
|
+
"ECONNRESET",
|
|
201
|
+
"EPIPE",
|
|
202
|
+
"ETIMEDOUT",
|
|
203
|
+
] as const)(
|
|
204
|
+
"makes one real Stripe-client HTTP attempt for a portal %s failure",
|
|
205
|
+
async (failure) => {
|
|
206
|
+
const { bs, calls } = fixture(undefined, failure);
|
|
207
|
+
const options = {
|
|
208
|
+
stripeAccountId: "acct_buyer",
|
|
209
|
+
connectedAccountId: "acct_connected",
|
|
210
|
+
configurationId: "bpc_policy",
|
|
211
|
+
returnUrl: "https://example.com/billing",
|
|
212
|
+
};
|
|
213
|
+
const requestIdentity = prepareBillingPortalSessionRequestIdentity(
|
|
214
|
+
options,
|
|
215
|
+
"portal:http-original",
|
|
216
|
+
);
|
|
217
|
+
await expect(
|
|
218
|
+
bs.createBillingPortalSession(ctx, { ...options, requestIdentity }),
|
|
219
|
+
).rejects.toMatchObject({
|
|
220
|
+
data: {
|
|
221
|
+
code: "STRIPE_API_ERROR",
|
|
222
|
+
stripeError: {
|
|
223
|
+
type:
|
|
224
|
+
failure === "mismatch"
|
|
225
|
+
? "StripeIdempotencyError"
|
|
226
|
+
: failure === "retryable" || failure === "conflict"
|
|
227
|
+
? "StripeAPIError"
|
|
228
|
+
: "StripeConnectionError",
|
|
229
|
+
...(failure === "conflict" ? { code: "idempotency_key_in_use" } : {}),
|
|
230
|
+
...(failure === "mismatch" ? { code: "idempotency_mismatch" } : {}),
|
|
231
|
+
...(failure === "ECONNRESET" || failure === "EPIPE"
|
|
232
|
+
? {
|
|
233
|
+
code: failure,
|
|
234
|
+
message: expect.stringContaining("unknown outcome"),
|
|
235
|
+
}
|
|
236
|
+
: {}),
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
expect(calls).toHaveLength(1);
|
|
241
|
+
expect(calls[0]).toMatchObject({
|
|
242
|
+
path: "/v1/billing_portal/sessions",
|
|
243
|
+
connectedAccount: "acct_connected",
|
|
244
|
+
idempotency: "portal:http-original",
|
|
245
|
+
});
|
|
246
|
+
expect(new URLSearchParams(calls[0]!.body).get("customer_account")).toBe(
|
|
247
|
+
"acct_buyer",
|
|
248
|
+
);
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
it.each(["ECONNRESET", "EPIPE", "ETIMEDOUT"] as const)(
|
|
252
|
+
"does not replay a targeted subscription read after %s",
|
|
253
|
+
async (failure) => {
|
|
254
|
+
const { bs, calls } = fixture(undefined, failure, "subscription");
|
|
255
|
+
const options = {
|
|
256
|
+
stripeAccountId: "acct_buyer",
|
|
257
|
+
connectedAccountId: "acct_connected",
|
|
258
|
+
returnUrl: "https://example.com/billing",
|
|
259
|
+
flowData: {
|
|
260
|
+
type: "subscription_update_confirm" as const,
|
|
261
|
+
subscription_update_confirm: {
|
|
262
|
+
subscription: "sub_preview",
|
|
263
|
+
items: [{ id: "si_preview", price: "price_next" }] as [
|
|
264
|
+
{ id: string; price: string },
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
const requestIdentity = prepareBillingPortalSessionRequestIdentity(
|
|
270
|
+
options,
|
|
271
|
+
"portal:get-original",
|
|
272
|
+
);
|
|
273
|
+
await expect(
|
|
274
|
+
bs.createBillingPortalSession(ctx, { ...options, requestIdentity }),
|
|
275
|
+
).rejects.toMatchObject({
|
|
276
|
+
data: {
|
|
277
|
+
code: "STRIPE_API_ERROR",
|
|
278
|
+
stripeError: { type: "StripeConnectionError" },
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
expect(calls).toHaveLength(1);
|
|
282
|
+
expect(calls[0]).toMatchObject({
|
|
283
|
+
path: "/v1/subscriptions/sub_preview",
|
|
284
|
+
connectedAccount: "acct_connected",
|
|
285
|
+
idempotency: undefined,
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
);
|
|
118
289
|
it.each([undefined, preview])(
|
|
119
290
|
"uses one selected version for account, Checkout, billing, refunds and authoritative reads (%s)",
|
|
120
291
|
async (version) => {
|
|
@@ -183,3 +354,13 @@ it.each(["", "2020-08-27", "2026-09-30.preview; unsafe", "not-a-version"])(
|
|
|
183
354
|
).toThrow("Unsupported Stripe API version");
|
|
184
355
|
},
|
|
185
356
|
);
|
|
357
|
+
it("isolates portal transport policy from caller-controlled API version cache keys", () => {
|
|
358
|
+
const key = `sk_test_cache_${crypto.randomUUID()}`;
|
|
359
|
+
const normal = getStripeClient(key, "2026-09-30.endive:single-attempt");
|
|
360
|
+
const portal = getStripeClient(key, "2026-09-30.endive", {
|
|
361
|
+
singleAttempt: true,
|
|
362
|
+
});
|
|
363
|
+
expect(portal).not.toBe(normal);
|
|
364
|
+
expect(portal.getApiField("version")).toBe("2026-09-30.endive");
|
|
365
|
+
expect(portal.getApiField("maxNetworkRetries")).toBe(0);
|
|
366
|
+
});
|