@fantastic.dev/better-stripe 0.82.0 → 0.83.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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/couponJoins.d.ts +23 -0
- package/dist/client/billing/renewalAttach/couponJoins.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/couponJoins.js +76 -0
- package/dist/client/billing/renewalAttach/couponJoins.js.map +1 -0
- package/dist/client/billing/renewalAttach/index.d.ts +3 -0
- package/dist/client/billing/renewalAttach/index.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/index.js +3 -0
- package/dist/client/billing/renewalAttach/index.js.map +1 -0
- package/dist/client/billing/renewalAttach/reader.d.ts +141 -0
- package/dist/client/billing/renewalAttach/reader.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/reader.js +77 -0
- package/dist/client/billing/renewalAttach/reader.js.map +1 -0
- package/dist/client/billing/renewalAttach/request.d.ts +30 -0
- package/dist/client/billing/renewalAttach/request.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/request.js +39 -0
- package/dist/client/billing/renewalAttach/request.js.map +1 -0
- package/dist/client/billing/renewalAttach/wireJson.d.ts +24 -0
- package/dist/client/billing/renewalAttach/wireJson.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/wireJson.js +51 -0
- package/dist/client/billing/renewalAttach/wireJson.js.map +1 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/helpers.d.ts.map +1 -1
- package/dist/client/helpers.js +2 -2
- package/dist/client/helpers.js.map +1 -1
- package/dist/client/index.d.ts +3 -5
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -3
- package/dist/client/index.js.map +1 -1
- package/dist/client/renewal.d.ts +8 -0
- package/dist/client/renewal.d.ts.map +1 -0
- package/dist/client/renewal.js +4 -0
- package/dist/client/renewal.js.map +1 -0
- package/dist/client/renewalContextClient.d.ts +130 -0
- package/dist/client/renewalContextClient.d.ts.map +1 -0
- package/dist/client/renewalContextClient.js +10 -0
- package/dist/client/renewalContextClient.js.map +1 -0
- package/dist/client/singleAttemptHttpClient.d.ts +1 -1
- package/dist/client/singleAttemptHttpClient.d.ts.map +1 -1
- package/dist/client/singleAttemptHttpClient.js +1 -1
- package/dist/client/singleAttemptHttpClient.js.map +1 -1
- package/dist/client/strictStripeTransport.d.ts +4 -0
- package/dist/client/strictStripeTransport.d.ts.map +1 -0
- package/dist/client/strictStripeTransport.js +7 -0
- package/dist/client/strictStripeTransport.js.map +1 -0
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/workflowValidators.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/component/usage/allowance.fixtures.d.ts +16 -16
- package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
- package/docs/renewal-context-reads.md +146 -0
- package/docs/renewal-price-replacements.md +55 -16
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/couponEvidence.test.ts +103 -0
- package/src/client/billing/renewalAttach/couponJoins.ts +92 -0
- package/src/client/billing/renewalAttach/index.ts +9 -0
- package/src/client/billing/renewalAttach/reader.test.ts +409 -0
- package/src/client/billing/renewalAttach/reader.ts +125 -0
- package/src/client/billing/renewalAttach/request.ts +49 -0
- package/src/client/billing/renewalAttach/wireJson.test.ts +265 -0
- package/src/client/billing/renewalAttach/wireJson.ts +75 -0
- package/src/client/helpers.ts +2 -4
- package/src/client/index.ts +3 -8
- package/src/client/renewal.ts +13 -0
- package/src/client/renewalContextClient.ts +23 -0
- package/src/client/singleAttemptHttpClient.ts +1 -1
- package/src/client/strictStripeTransport.test.ts +73 -0
- package/src/client/strictStripeTransport.ts +11 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { it, expect } from "vitest";
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { SingleAttemptHttpClient } from "../../singleAttemptHttpClient.js";
|
|
4
|
+
import {
|
|
5
|
+
fixture,
|
|
6
|
+
API_VERSION,
|
|
7
|
+
} from "../../../../tests/renewalAggregate/fixtures.js";
|
|
8
|
+
import { readRenewalPriceReplacementContext } from "./reader.js";
|
|
9
|
+
import { readRenewalWireJSON } from "./wireJson.js";
|
|
10
|
+
export function wireHarness(status = 200, withDecimals = true) {
|
|
11
|
+
const f = fixture();
|
|
12
|
+
const sub = JSON.parse(JSON.stringify(f.subscription));
|
|
13
|
+
const p = {
|
|
14
|
+
...sub.items.data[0].price,
|
|
15
|
+
unit_amount: 1000,
|
|
16
|
+
unit_amount_decimal: withDecimals ? "1000.00" : null,
|
|
17
|
+
created: 1,
|
|
18
|
+
metadata: { keep: "wire" },
|
|
19
|
+
nickname: null,
|
|
20
|
+
tiers_mode: null,
|
|
21
|
+
transform_quantity: null,
|
|
22
|
+
};
|
|
23
|
+
sub.items.data[0].price = p;
|
|
24
|
+
sub.items.data[0].plan = {
|
|
25
|
+
id: p.id,
|
|
26
|
+
object: "plan",
|
|
27
|
+
active: p.active,
|
|
28
|
+
amount: 1000,
|
|
29
|
+
amount_decimal: withDecimals ? "1000.00" : null,
|
|
30
|
+
billing_scheme: p.billing_scheme,
|
|
31
|
+
created: 1,
|
|
32
|
+
currency: p.currency,
|
|
33
|
+
interval: p.recurring.interval,
|
|
34
|
+
interval_count: p.recurring.interval_count,
|
|
35
|
+
livemode: p.livemode,
|
|
36
|
+
metadata: { keep: "wire" },
|
|
37
|
+
meter: null,
|
|
38
|
+
nickname: null,
|
|
39
|
+
product: p.product,
|
|
40
|
+
tiers_mode: null,
|
|
41
|
+
transform_usage: null,
|
|
42
|
+
trial_period_days: null,
|
|
43
|
+
usage_type: "licensed",
|
|
44
|
+
};
|
|
45
|
+
sub.opaque = { facts: ["keep", "all"] };
|
|
46
|
+
const target = {
|
|
47
|
+
...f.targetPrice,
|
|
48
|
+
unit_amount_decimal: withDecimals ? "2000.00" : null,
|
|
49
|
+
opaque: { keep: "yes" },
|
|
50
|
+
};
|
|
51
|
+
const calls: {
|
|
52
|
+
path: string;
|
|
53
|
+
method: string;
|
|
54
|
+
version: string | null;
|
|
55
|
+
connected: string | null;
|
|
56
|
+
body: string;
|
|
57
|
+
}[] = [];
|
|
58
|
+
const coupons = new Map(
|
|
59
|
+
[...f.subscription.discounts, ...f.originalItem.discounts].map((d) => [
|
|
60
|
+
d.source.coupon.id,
|
|
61
|
+
d.source.coupon,
|
|
62
|
+
]),
|
|
63
|
+
);
|
|
64
|
+
const stripe = new Stripe("sk_test_ONLY_CONTROLLED_WIRE", {
|
|
65
|
+
apiVersion: API_VERSION,
|
|
66
|
+
maxNetworkRetries: 0,
|
|
67
|
+
httpClient: new SingleAttemptHttpClient(
|
|
68
|
+
Stripe.createFetchHttpClient(async (url, init) => {
|
|
69
|
+
const u = new URL(String(url)),
|
|
70
|
+
headers = new Headers(init?.headers);
|
|
71
|
+
calls.push({
|
|
72
|
+
path: u.pathname + u.search,
|
|
73
|
+
method: String(init?.method),
|
|
74
|
+
version: headers.get("stripe-version"),
|
|
75
|
+
connected: headers.get("stripe-account"),
|
|
76
|
+
body: String(init?.body ?? ""),
|
|
77
|
+
});
|
|
78
|
+
const body = u.pathname.startsWith("/v1/subscriptions/")
|
|
79
|
+
? sub
|
|
80
|
+
: u.pathname.startsWith("/v1/prices/")
|
|
81
|
+
? target
|
|
82
|
+
: u.pathname.startsWith("/v1/customers/")
|
|
83
|
+
? f.billingCustomer
|
|
84
|
+
: coupons.get(decodeURIComponent(u.pathname.slice(12)));
|
|
85
|
+
return new Response(JSON.stringify(body), {
|
|
86
|
+
status,
|
|
87
|
+
headers: {
|
|
88
|
+
"content-type": "application/json",
|
|
89
|
+
"request-id": "req_controlledwire",
|
|
90
|
+
"stripe-version": API_VERSION,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
95
|
+
});
|
|
96
|
+
return {
|
|
97
|
+
f,
|
|
98
|
+
sub,
|
|
99
|
+
target,
|
|
100
|
+
calls,
|
|
101
|
+
stripe,
|
|
102
|
+
read: () =>
|
|
103
|
+
readRenewalPriceReplacementContext(
|
|
104
|
+
stripe,
|
|
105
|
+
{ expected: f.prepared.expected },
|
|
106
|
+
API_VERSION,
|
|
107
|
+
),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
it("reader preserves actualSDK nativePlan/Price decimal stringlexemes and every opaqueJSONfact", async () => {
|
|
111
|
+
const h = wireHarness();
|
|
112
|
+
const r = await h.read();
|
|
113
|
+
expect(
|
|
114
|
+
(
|
|
115
|
+
(r.subscription.items.data[0] as Record<string, unknown>).plan as {
|
|
116
|
+
amount_decimal: string;
|
|
117
|
+
}
|
|
118
|
+
).amount_decimal,
|
|
119
|
+
).toBe("1000.00");
|
|
120
|
+
expect(r.subscription.items.data[0].price.unit_amount_decimal).toBe(
|
|
121
|
+
"1000.00",
|
|
122
|
+
);
|
|
123
|
+
expect(r.subscription).toEqual(h.sub);
|
|
124
|
+
expect(r.targetPrice).toEqual(h.target);
|
|
125
|
+
expect(h.calls).toHaveLength(5);
|
|
126
|
+
expect(
|
|
127
|
+
h.calls.every(
|
|
128
|
+
(c) =>
|
|
129
|
+
c.method === "GET" &&
|
|
130
|
+
c.version === API_VERSION &&
|
|
131
|
+
c.connected === null &&
|
|
132
|
+
c.body === "",
|
|
133
|
+
),
|
|
134
|
+
).toBe(true);
|
|
135
|
+
const first = new URL(h.calls[0].path, "https://api.stripe.com");
|
|
136
|
+
expect([...first.searchParams]).toEqual([
|
|
137
|
+
["expand[0]", "discounts"],
|
|
138
|
+
["expand[1]", "items.data.discounts"],
|
|
139
|
+
["expand[2]", "items.data.price"],
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
142
|
+
it.each([201, 204, 299])(
|
|
143
|
+
"admitsvalid2xx JSON or failsclosed onbodyless%s",
|
|
144
|
+
async (status) => {
|
|
145
|
+
// 201/299 are valid2xx. The fixedbody stillneeds schema; unlike204 they preserveJSON.
|
|
146
|
+
const h = wireHarness(status);
|
|
147
|
+
if (status === 204) await expect(h.read()).rejects.toBeDefined();
|
|
148
|
+
else expect((await h.read()).subscription.id).toBe("sub_original");
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
it.each([307, 308, 400, 500])(
|
|
152
|
+
"rejects fulfillednon2xx JSONstatus%s before acquiring otherresources",
|
|
153
|
+
async (status) => {
|
|
154
|
+
const h = wireHarness(status, false);
|
|
155
|
+
await expect(h.read()).rejects.toBeDefined();
|
|
156
|
+
expect(h.calls).toHaveLength(1);
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
it.each([
|
|
161
|
+
{},
|
|
162
|
+
{ status: "200" },
|
|
163
|
+
{ statusCode: "200" },
|
|
164
|
+
{ status: 200, statusCode: 201 },
|
|
165
|
+
{ status: 200, statusCode: 500 },
|
|
166
|
+
{ status: NaN },
|
|
167
|
+
])("rejects missing/invalid/contradictory statusmetadata %j", async (meta) => {
|
|
168
|
+
const f = fixture();
|
|
169
|
+
const stripe = new Stripe("sk_test_CONTROLLED_META", {
|
|
170
|
+
apiVersion: API_VERSION,
|
|
171
|
+
maxNetworkRetries: 0,
|
|
172
|
+
httpClient: {
|
|
173
|
+
getClientName: () => "controlledmeta",
|
|
174
|
+
makeRequest: async () => ({
|
|
175
|
+
getStatusCode: () => 200,
|
|
176
|
+
getHeaders: () => ({ "request-id": "req_meta" }),
|
|
177
|
+
getRawResponse: () => meta,
|
|
178
|
+
toJSON: async () => JSON.parse(JSON.stringify(f.billingCustomer)),
|
|
179
|
+
toStream: () => {
|
|
180
|
+
throw Error("unused");
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
await expect(
|
|
186
|
+
readRenewalWireJSON(stripe, "customer", "cus_buyer"),
|
|
187
|
+
).rejects.toMatchObject({
|
|
188
|
+
message: expect.stringContaining("provider_http_status"),
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
it.each([
|
|
192
|
+
{ status: 200 },
|
|
193
|
+
{ statusCode: 200 },
|
|
194
|
+
{ status: 200, statusCode: 200 },
|
|
195
|
+
])("admits supported successfultransportmetadata %j", async (meta) => {
|
|
196
|
+
const f = fixture();
|
|
197
|
+
const stripe = new Stripe("sk_test_CONTROLLED_META_VALID", {
|
|
198
|
+
apiVersion: API_VERSION,
|
|
199
|
+
maxNetworkRetries: 0,
|
|
200
|
+
httpClient: {
|
|
201
|
+
getClientName: () => "controlledmeta",
|
|
202
|
+
makeRequest: async () => ({
|
|
203
|
+
getStatusCode: () => 200,
|
|
204
|
+
getHeaders: () => ({ "request-id": "req_meta" }),
|
|
205
|
+
getRawResponse: () => meta,
|
|
206
|
+
toJSON: async () => JSON.parse(JSON.stringify(f.billingCustomer)),
|
|
207
|
+
toStream: () => {
|
|
208
|
+
throw Error("unused");
|
|
209
|
+
},
|
|
210
|
+
}),
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
expect(await readRenewalWireJSON(stripe, "customer", "cus_buyer")).toEqual(
|
|
214
|
+
f.billingCustomer,
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
it("encodes arbitrary supported customcoupon IDs into one fixedpath segment", async () => {
|
|
218
|
+
const paths: string[] = [];
|
|
219
|
+
const stripe = new Stripe("sk_test_CONTROLLED_ENCODE", {
|
|
220
|
+
apiVersion: API_VERSION,
|
|
221
|
+
maxNetworkRetries: 0,
|
|
222
|
+
httpClient: {
|
|
223
|
+
getClientName: () => "controlledencoding",
|
|
224
|
+
makeRequest: async (...args) => {
|
|
225
|
+
paths.push(args[2]);
|
|
226
|
+
return {
|
|
227
|
+
getStatusCode: () => 200,
|
|
228
|
+
getHeaders: () => ({}),
|
|
229
|
+
getRawResponse: () => ({ statusCode: 200 }),
|
|
230
|
+
toJSON: async () => ({ id: "custom /?#%", object: "coupon" }),
|
|
231
|
+
toStream: () => {
|
|
232
|
+
throw Error("unused");
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
expect(await readRenewalWireJSON(stripe, "coupon", "custom /?#%")).toEqual({
|
|
239
|
+
id: "custom /?#%",
|
|
240
|
+
object: "coupon",
|
|
241
|
+
});
|
|
242
|
+
expect(paths).toEqual(["/v1/coupons/custom%20%2F%3F%23%25"]);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it.each([".", ".."])(
|
|
246
|
+
"rejects dot-only coupon selector %s before actualSDK issues anyrequest",
|
|
247
|
+
async (id) => {
|
|
248
|
+
let requests = 0;
|
|
249
|
+
const stripe = new Stripe("sk_test_CONTROLLED_DOT_SEGMENT", {
|
|
250
|
+
apiVersion: API_VERSION,
|
|
251
|
+
maxNetworkRetries: 0,
|
|
252
|
+
httpClient: Stripe.createFetchHttpClient(async () => {
|
|
253
|
+
requests++;
|
|
254
|
+
return new Response(JSON.stringify({ id, object: "coupon" }), {
|
|
255
|
+
status: 200,
|
|
256
|
+
headers: { "content-type": "application/json" },
|
|
257
|
+
});
|
|
258
|
+
}),
|
|
259
|
+
});
|
|
260
|
+
await expect(
|
|
261
|
+
readRenewalWireJSON(stripe, "coupon", id),
|
|
262
|
+
).rejects.toBeDefined();
|
|
263
|
+
expect(requests).toBe(0);
|
|
264
|
+
},
|
|
265
|
+
);
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
4
|
+
import { resourceId, unsupported } from "../renewalAggregate/shared.js";
|
|
5
|
+
|
|
6
|
+
const resources = {
|
|
7
|
+
subscription: { path: "subscriptions", id: resourceId("sub") },
|
|
8
|
+
price: { path: "prices", id: resourceId("price") },
|
|
9
|
+
customer: { path: "customers", id: resourceId("cus") },
|
|
10
|
+
coupon: {
|
|
11
|
+
path: "coupons",
|
|
12
|
+
id: z
|
|
13
|
+
.string()
|
|
14
|
+
.min(1)
|
|
15
|
+
.max(255)
|
|
16
|
+
.regex(/\S/)
|
|
17
|
+
.refine(
|
|
18
|
+
(value) => value !== "." && value !== "..",
|
|
19
|
+
"Coupon ID must not be a URL dot segment.",
|
|
20
|
+
),
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
const expansions = ["discounts", "items.data.discounts", "items.data.price"];
|
|
24
|
+
|
|
25
|
+
/** Fixed renewal GETs only. rawRequest bypasses typed resource Decimal coercion. */
|
|
26
|
+
export async function readRenewalWireJSON(
|
|
27
|
+
stripe: Stripe,
|
|
28
|
+
resource: keyof typeof resources,
|
|
29
|
+
id: string,
|
|
30
|
+
) {
|
|
31
|
+
const spec = resources[resource];
|
|
32
|
+
const selectedId = spec.id.parse(id);
|
|
33
|
+
const query =
|
|
34
|
+
resource === "subscription"
|
|
35
|
+
? `?${new URLSearchParams(expansions.map((value, i) => [`expand[${i}]`, value]))}`
|
|
36
|
+
: "";
|
|
37
|
+
const reply: unknown = await stripe.rawRequest(
|
|
38
|
+
"GET",
|
|
39
|
+
`/v1/${spec.path}/${encodeURIComponent(selectedId)}${query}`,
|
|
40
|
+
undefined,
|
|
41
|
+
{ apiVersion: "2026-09-30.endive", maxNetworkRetries: 0 },
|
|
42
|
+
);
|
|
43
|
+
if (!reply || typeof reply !== "object")
|
|
44
|
+
unsupported("missing_provider_http_status");
|
|
45
|
+
const descriptor = Object.getOwnPropertyDescriptor(reply, "lastResponse");
|
|
46
|
+
if (
|
|
47
|
+
!descriptor ||
|
|
48
|
+
descriptor.enumerable ||
|
|
49
|
+
!("value" in descriptor) ||
|
|
50
|
+
!descriptor.value ||
|
|
51
|
+
typeof descriptor.value !== "object"
|
|
52
|
+
)
|
|
53
|
+
unsupported("missing_provider_http_status");
|
|
54
|
+
// Fetch's public Response uses status; Node IncomingMessage uses statusCode.
|
|
55
|
+
// Neither is provider JSON. If both are present they must agree.
|
|
56
|
+
const meta = descriptor.value as { status?: unknown; statusCode?: unknown };
|
|
57
|
+
const codes = [meta.status, meta.statusCode].filter(
|
|
58
|
+
(code) => code !== undefined,
|
|
59
|
+
);
|
|
60
|
+
if (
|
|
61
|
+
!codes.length ||
|
|
62
|
+
codes.some(
|
|
63
|
+
(code) =>
|
|
64
|
+
typeof code !== "number" ||
|
|
65
|
+
!Number.isInteger(code) ||
|
|
66
|
+
code < 200 ||
|
|
67
|
+
code >= 300,
|
|
68
|
+
) ||
|
|
69
|
+
codes.some((code) => code !== codes[0])
|
|
70
|
+
)
|
|
71
|
+
unsupported("unverified_provider_http_status");
|
|
72
|
+
// Strict JSON copy remains unchanged. Nonenumerable transport metadata is
|
|
73
|
+
// excluded; every enumerable wire-body fact, including unknown fields, remains.
|
|
74
|
+
return renewalSnapshot(reply);
|
|
75
|
+
}
|
package/src/client/helpers.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type {
|
|
|
4
4
|
OptionalRestArgs,
|
|
5
5
|
} from "convex/server";
|
|
6
6
|
import Stripe from "stripe";
|
|
7
|
-
import {
|
|
7
|
+
import { createSingleAttemptHttpClient } from "./strictStripeTransport.js";
|
|
8
8
|
|
|
9
9
|
import type { ComponentApi } from "../component/_generated/component.js";
|
|
10
10
|
import { STRIPE_API_VERSION } from "./constants.js";
|
|
@@ -58,9 +58,7 @@ export function getStripeClient(
|
|
|
58
58
|
...(policy?.singleAttempt
|
|
59
59
|
? {
|
|
60
60
|
maxNetworkRetries: 0,
|
|
61
|
-
httpClient:
|
|
62
|
-
Stripe.createFetchHttpClient(),
|
|
63
|
-
),
|
|
61
|
+
httpClient: createSingleAttemptHttpClient(),
|
|
64
62
|
}
|
|
65
63
|
: {}),
|
|
66
64
|
});
|
package/src/client/index.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type {
|
|
3
|
-
PrepareRenewalPriceReplacementOptions,
|
|
4
|
-
PreparedRenewalPriceReplacement,
|
|
5
|
-
} from "./billing/renewalPriceReplacement.js";
|
|
6
|
-
export type { RenewalPriceReplacementExpected } from "./billing/renewalReplacementSchema.js";
|
|
1
|
+
export * from "./renewal.js";
|
|
7
2
|
export { qualifyCapturedCheckoutPayment } from "./billing/capturedCheckoutPayment.js";
|
|
8
3
|
export type {
|
|
9
4
|
CapturedCheckoutPaymentExpected,
|
|
@@ -73,7 +68,7 @@ import type { CreateCheckoutSessionOptions } from "./billing/checkoutOptions.js"
|
|
|
73
68
|
export type { CreateCheckoutSessionOptions } from "./billing/checkoutOptions.js";
|
|
74
69
|
import * as intentsImpl from "./billing/intents.js";
|
|
75
70
|
export type * from "./billing/intents.js";
|
|
76
|
-
import {
|
|
71
|
+
import { RenewalContextClient } from "./renewalContextClient.js";
|
|
77
72
|
export type {
|
|
78
73
|
CreateBillingPortalConfigurationOptions,
|
|
79
74
|
RetrieveBillingPortalConfigurationOptions,
|
|
@@ -281,7 +276,7 @@ export type BetterStripeComponent = ComponentApi;
|
|
|
281
276
|
|
|
282
277
|
// BETTER STRIPE CLIENT
|
|
283
278
|
|
|
284
|
-
export class BetterStripe extends
|
|
279
|
+
export class BetterStripe extends RenewalContextClient {
|
|
285
280
|
get usage() {
|
|
286
281
|
return new UsageLedgerClient(this.component);
|
|
287
282
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Public pure renewal preparation functions and their input/result types. */
|
|
2
|
+
export { prepareRenewalPriceReplacement } from "./billing/renewalPriceReplacement.js";
|
|
3
|
+
export type {
|
|
4
|
+
PrepareRenewalPriceReplacementOptions,
|
|
5
|
+
PreparedRenewalPriceReplacement,
|
|
6
|
+
} from "./billing/renewalPriceReplacement.js";
|
|
7
|
+
export type { RenewalPriceReplacementExpected } from "./billing/renewalReplacementSchema.js";
|
|
8
|
+
export { prepareRenewalScheduleAttachment } from "./billing/renewalAttach/request.js";
|
|
9
|
+
export type {
|
|
10
|
+
PrepareRenewalScheduleAttachmentOptions,
|
|
11
|
+
PreparedRenewalScheduleAttachment,
|
|
12
|
+
} from "./billing/renewalAttach/request.js";
|
|
13
|
+
export type { ReadRenewalPriceReplacementContextOptions } from "./billing/renewalAttach/reader.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PaymentSourceClient } from "./paymentSource/client.js";
|
|
2
|
+
import type { RunCtx } from "./helpers.js";
|
|
3
|
+
import {
|
|
4
|
+
readRenewalPriceReplacementContext,
|
|
5
|
+
type ReadRenewalPriceReplacementContextOptions,
|
|
6
|
+
} from "./billing/renewalAttach/reader.js";
|
|
7
|
+
|
|
8
|
+
/** Provider context acquisition inherited by the public server client. */
|
|
9
|
+
export abstract class RenewalContextClient extends PaymentSourceClient {
|
|
10
|
+
abstract readonly stripeApiVersion: string;
|
|
11
|
+
|
|
12
|
+
/** Context only. Hosts authorize exact selectors; no attachment occurs. */
|
|
13
|
+
async readRenewalPriceReplacementContext(
|
|
14
|
+
_ctx: RunCtx,
|
|
15
|
+
opts: ReadRenewalPriceReplacementContextOptions,
|
|
16
|
+
) {
|
|
17
|
+
return readRenewalPriceReplacementContext(
|
|
18
|
+
this.stripe({ singleAttempt: true }),
|
|
19
|
+
opts,
|
|
20
|
+
this.stripeApiVersion,
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -12,7 +12,7 @@ class SingleAttemptConnectionClosed extends Error {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
/**
|
|
15
|
+
/** Guard for Stripe's closed-connection retry exception. */
|
|
16
16
|
export class SingleAttemptHttpClient implements Stripe.HttpClient {
|
|
17
17
|
constructor(private readonly delegate: Stripe.HttpClient) {}
|
|
18
18
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { afterEach, it, expect, vi } from "vitest";
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { getStripeClient } from "./helpers.js";
|
|
5
|
+
afterEach(() => vi.restoreAllMocks());
|
|
6
|
+
it.each([307, 308])(
|
|
7
|
+
"shared singleAttempt factory sends onlyone POST on%s redirect",
|
|
8
|
+
async (status) => {
|
|
9
|
+
const requests: { path: string; method: string }[] = [];
|
|
10
|
+
const server = createServer((req, res) => {
|
|
11
|
+
requests.push({ path: req.url!, method: req.method! });
|
|
12
|
+
req.resume();
|
|
13
|
+
if (req.url?.startsWith("/v1/")) {
|
|
14
|
+
res.writeHead(status, { Location: "/redirected" });
|
|
15
|
+
res.end();
|
|
16
|
+
} else {
|
|
17
|
+
res.writeHead(200, { "content-type": "application/json" });
|
|
18
|
+
res.end(
|
|
19
|
+
JSON.stringify({
|
|
20
|
+
id: "sub_sched_controlled",
|
|
21
|
+
object: "subscription_schedule",
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
|
|
27
|
+
const addr = server.address();
|
|
28
|
+
if (!addr || typeof addr === "string") throw Error("missingephemeralport");
|
|
29
|
+
const original = Stripe.createFetchHttpClient.bind(Stripe);
|
|
30
|
+
// Public Fetchfactory seam rewrites ONLY ownedloopback destination. The tested
|
|
31
|
+
// factory owns redirectmode; this fixture deliberately does not set it.
|
|
32
|
+
vi.spyOn(Stripe, "createFetchHttpClient").mockImplementation((fetchFn) =>
|
|
33
|
+
original((url, init) => {
|
|
34
|
+
const local = new URL(String(url));
|
|
35
|
+
local.protocol = "http:";
|
|
36
|
+
local.hostname = "127.0.0.1";
|
|
37
|
+
local.port = String(addr.port);
|
|
38
|
+
return (fetchFn ?? fetch)(local.toString(), init);
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
try {
|
|
42
|
+
const stripe = getStripeClient(
|
|
43
|
+
"sk_test_CONTROLLED_SHARED_" + status,
|
|
44
|
+
"2026-09-30.endive",
|
|
45
|
+
{ singleAttempt: true },
|
|
46
|
+
);
|
|
47
|
+
await expect(
|
|
48
|
+
stripe.subscriptionSchedules.update(
|
|
49
|
+
"sub_sched_controlled",
|
|
50
|
+
{ end_behavior: "release" },
|
|
51
|
+
{ maxNetworkRetries: 0, idempotencyKey: "retained-controlled-key" },
|
|
52
|
+
),
|
|
53
|
+
).rejects.toMatchObject({ type: "StripeConnectionError" });
|
|
54
|
+
expect(requests).toEqual([
|
|
55
|
+
{
|
|
56
|
+
path: "/v1/subscription_schedules/sub_sched_controlled",
|
|
57
|
+
method: "POST",
|
|
58
|
+
},
|
|
59
|
+
]);
|
|
60
|
+
} finally {
|
|
61
|
+
server.closeAllConnections();
|
|
62
|
+
await new Promise<void>((resolve, reject) =>
|
|
63
|
+
server.close((error) => (error ? reject(error) : resolve())),
|
|
64
|
+
);
|
|
65
|
+
expect(server.listening).toBe(false);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
it("ordinary cached client remains on its normal transport factory path", () => {
|
|
70
|
+
const factory = vi.spyOn(Stripe, "createFetchHttpClient");
|
|
71
|
+
getStripeClient("sk_test_CONTROLLED_ORDINARY_UNCHANGED", "2026-09-30.endive");
|
|
72
|
+
expect(factory).not.toHaveBeenCalled();
|
|
73
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import { SingleAttemptHttpClient } from "./singleAttemptHttpClient.js";
|
|
3
|
+
|
|
4
|
+
/** No fetch redirect replay, plus the Stripe closed-connection retry guard. */
|
|
5
|
+
export function createSingleAttemptHttpClient(): Stripe.HttpClient {
|
|
6
|
+
return new SingleAttemptHttpClient(
|
|
7
|
+
Stripe.createFetchHttpClient((input, init) =>
|
|
8
|
+
fetch(input, { ...init, redirect: "error" }),
|
|
9
|
+
),
|
|
10
|
+
);
|
|
11
|
+
}
|