@fantastic.dev/better-stripe 0.82.0 → 0.83.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 +6 -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 +80 -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/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- 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/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- 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/component/usage/allowance.fixtures.d.ts +16 -16
- package/docs/renewal-context-reads.md +113 -0
- package/docs/renewal-price-replacements.md +55 -16
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/couponEvidence.test.ts +104 -0
- package/src/client/billing/renewalAttach/couponJoins.ts +95 -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 +131 -0
- package/src/client/billing/renewalAttach/request.ts +49 -0
- package/src/client/index.ts +3 -8
- package/src/client/renewal.ts +13 -0
- package/src/client/renewalContextClient.ts +23 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { describe, expect, it, vi, afterEach } from "vitest";
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { SingleAttemptHttpClient } from "../../singleAttemptHttpClient.js";
|
|
4
|
+
import {
|
|
5
|
+
fixture,
|
|
6
|
+
API_VERSION,
|
|
7
|
+
alter,
|
|
8
|
+
} from "../../../../tests/renewalAggregate/fixtures.js";
|
|
9
|
+
import {
|
|
10
|
+
readRenewalPriceReplacementContext,
|
|
11
|
+
prepareRenewalScheduleAttachment,
|
|
12
|
+
} from "./index.js";
|
|
13
|
+
import { binding } from "../renewalAggregate/shared.js";
|
|
14
|
+
|
|
15
|
+
type Wire = {
|
|
16
|
+
path: string;
|
|
17
|
+
method: string;
|
|
18
|
+
headers: Parameters<Stripe.HttpClient["makeRequest"]>[4];
|
|
19
|
+
body: string;
|
|
20
|
+
};
|
|
21
|
+
function harness(
|
|
22
|
+
options: {
|
|
23
|
+
sub?: unknown;
|
|
24
|
+
customer?: unknown;
|
|
25
|
+
target?: unknown;
|
|
26
|
+
coupon?: (id: string) => unknown;
|
|
27
|
+
fail?: string;
|
|
28
|
+
} = {},
|
|
29
|
+
) {
|
|
30
|
+
const f = fixture();
|
|
31
|
+
const wires: Wire[] = [];
|
|
32
|
+
const sub = structuredClone(
|
|
33
|
+
options.sub ?? f.subscription,
|
|
34
|
+
) as typeof f.subscription;
|
|
35
|
+
const coupons = new Map<string, unknown>();
|
|
36
|
+
for (const d of [...f.subscription.discounts, ...f.originalItem.discounts])
|
|
37
|
+
coupons.set(d.source.coupon.id, d.source.coupon);
|
|
38
|
+
const delegate = {
|
|
39
|
+
getClientName: () => "controlled-renewal-reader",
|
|
40
|
+
async makeRequest(...args: Parameters<Stripe.HttpClient["makeRequest"]>) {
|
|
41
|
+
const wire = {
|
|
42
|
+
path: args[2],
|
|
43
|
+
method: args[3],
|
|
44
|
+
headers: args[4],
|
|
45
|
+
body: args[5],
|
|
46
|
+
};
|
|
47
|
+
wires.push(wire);
|
|
48
|
+
if (options.fail && wire.path.startsWith(options.fail))
|
|
49
|
+
throw Object.assign(new Error("controlled closed response"), {
|
|
50
|
+
code: "ECONNRESET",
|
|
51
|
+
});
|
|
52
|
+
const url = new URL(wire.path, "https://api.stripe.com");
|
|
53
|
+
const data =
|
|
54
|
+
url.pathname === "/v1/subscriptions/sub_original"
|
|
55
|
+
? sub
|
|
56
|
+
: url.pathname === "/v1/prices/price_target"
|
|
57
|
+
? (options.target ?? f.targetPrice)
|
|
58
|
+
: url.pathname === "/v1/customers/cus_buyer"
|
|
59
|
+
? (options.customer ?? f.billingCustomer)
|
|
60
|
+
: url.pathname.startsWith("/v1/coupons/")
|
|
61
|
+
? (options.coupon?.(
|
|
62
|
+
decodeURIComponent(url.pathname.slice(12)),
|
|
63
|
+
) ?? coupons.get(decodeURIComponent(url.pathname.slice(12))))
|
|
64
|
+
: (() => {
|
|
65
|
+
throw Error("unplanned request");
|
|
66
|
+
})();
|
|
67
|
+
return {
|
|
68
|
+
getStatusCode: () => 200,
|
|
69
|
+
getHeaders: () => ({
|
|
70
|
+
"request-id": "req_controlled",
|
|
71
|
+
"stripe-version": API_VERSION,
|
|
72
|
+
}),
|
|
73
|
+
toJSON: async () => structuredClone(data),
|
|
74
|
+
getRawResponse: () => ({}),
|
|
75
|
+
toStream: () => {
|
|
76
|
+
throw Error("not used");
|
|
77
|
+
},
|
|
78
|
+
} as unknown as Stripe.HttpClientResponse;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const stripe = new Stripe("sk_test_CONTROLLED", {
|
|
82
|
+
apiVersion: API_VERSION,
|
|
83
|
+
maxNetworkRetries: 0,
|
|
84
|
+
httpClient: new SingleAttemptHttpClient(delegate),
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
f,
|
|
88
|
+
wires,
|
|
89
|
+
stripe,
|
|
90
|
+
read: () =>
|
|
91
|
+
readRenewalPriceReplacementContext(
|
|
92
|
+
stripe,
|
|
93
|
+
{ expected: f.prepared.expected },
|
|
94
|
+
API_VERSION,
|
|
95
|
+
),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
afterEach(() => vi.restoreAllMocks());
|
|
99
|
+
describe("actual SDK renewal context acquisition", () => {
|
|
100
|
+
it("uses fixed bounded GET plan, joins coupons and returns complete snapshots", async () => {
|
|
101
|
+
const h = harness();
|
|
102
|
+
vi.spyOn(Date, "now").mockReturnValue(h.f.preflightInput.observedAt * 1000);
|
|
103
|
+
const result = await h.read();
|
|
104
|
+
expect(result).toMatchObject({
|
|
105
|
+
version: 1,
|
|
106
|
+
scope: "platform",
|
|
107
|
+
apiVersion: API_VERSION,
|
|
108
|
+
readProfile: "renewal-context-v1",
|
|
109
|
+
providerQualification: "required",
|
|
110
|
+
observedAt: h.f.preflightInput.observedAt,
|
|
111
|
+
subscription: h.f.subscription,
|
|
112
|
+
targetPrice: h.f.targetPrice,
|
|
113
|
+
billingCustomer: h.f.billingCustomer,
|
|
114
|
+
});
|
|
115
|
+
expect(
|
|
116
|
+
h.wires.map((w) => new URL(w.path, "https://api.stripe.com").pathname),
|
|
117
|
+
).toEqual([
|
|
118
|
+
"/v1/subscriptions/sub_original",
|
|
119
|
+
"/v1/prices/price_target",
|
|
120
|
+
"/v1/customers/cus_buyer",
|
|
121
|
+
"/v1/coupons/custom-subscription-offer",
|
|
122
|
+
"/v1/coupons/custom-item-offer",
|
|
123
|
+
]);
|
|
124
|
+
expect(
|
|
125
|
+
Array.from(
|
|
126
|
+
new URL(
|
|
127
|
+
h.wires[0].path,
|
|
128
|
+
"https://api.stripe.com",
|
|
129
|
+
).searchParams.values(),
|
|
130
|
+
),
|
|
131
|
+
).toEqual(["discounts", "items.data.discounts", "items.data.price"]);
|
|
132
|
+
for (const w of h.wires) {
|
|
133
|
+
expect(w.method).toBe("GET");
|
|
134
|
+
expect(w.body).toBe("");
|
|
135
|
+
expect(w.headers["Stripe-Version"]).toBe(API_VERSION);
|
|
136
|
+
expect(w.headers["Stripe-Account"]).toBeUndefined();
|
|
137
|
+
expect(w.headers["Stripe-Context"]).toBeUndefined();
|
|
138
|
+
expect(w.headers["Idempotency-Key"]).toBeUndefined();
|
|
139
|
+
}
|
|
140
|
+
expect(h.wires.slice(1).every((w) => !w.path.includes("?"))).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
it("performs explicit coupon GET joins when refs are strings, never deep expansion", async () => {
|
|
143
|
+
const f = fixture();
|
|
144
|
+
const sub = alter(
|
|
145
|
+
alter(
|
|
146
|
+
f.subscription,
|
|
147
|
+
["discounts", 0, "source", "coupon"],
|
|
148
|
+
"custom-subscription-offer",
|
|
149
|
+
),
|
|
150
|
+
["items", "data", 0, "discounts", 0, "source", "coupon"],
|
|
151
|
+
"custom-item-offer",
|
|
152
|
+
);
|
|
153
|
+
const h = harness({ sub });
|
|
154
|
+
expect((await h.read()).subscription).toEqual(f.subscription);
|
|
155
|
+
expect(h.wires).toHaveLength(5);
|
|
156
|
+
});
|
|
157
|
+
it("deduplicates coupon IDs while retaining independent discount ownership/order", async () => {
|
|
158
|
+
const f = fixture();
|
|
159
|
+
const sub = alter(
|
|
160
|
+
f.subscription,
|
|
161
|
+
["items", "data", 0, "discounts", 0, "source", "coupon"],
|
|
162
|
+
f.subscription.discounts[0].source.coupon,
|
|
163
|
+
);
|
|
164
|
+
const h = harness({ sub });
|
|
165
|
+
const result = await h.read();
|
|
166
|
+
expect(h.wires).toHaveLength(4);
|
|
167
|
+
expect(result.subscription.items.data[0].discounts[0].id).toBe("di_item");
|
|
168
|
+
});
|
|
169
|
+
it("retains actual nonnull schedule association without adopting it or fabricating null", async () => {
|
|
170
|
+
const h = harness({
|
|
171
|
+
sub: { ...fixture().subscription, schedule: "sub_sched_observed" },
|
|
172
|
+
});
|
|
173
|
+
expect((await h.read()).subscription.schedule).toBe("sub_sched_observed");
|
|
174
|
+
expect(h.wires.every((w) => w.method === "GET")).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
it.each([
|
|
177
|
+
[
|
|
178
|
+
"foreign subscription",
|
|
179
|
+
() => harness({ sub: { ...fixture().subscription, id: "sub_foreign" } }),
|
|
180
|
+
],
|
|
181
|
+
[
|
|
182
|
+
"truncated item list",
|
|
183
|
+
() =>
|
|
184
|
+
harness({
|
|
185
|
+
sub: alter(fixture().subscription, ["items", "has_more"], true),
|
|
186
|
+
}),
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
"foreign target",
|
|
190
|
+
() =>
|
|
191
|
+
harness({ target: { ...fixture().targetPrice, id: "price_foreign" } }),
|
|
192
|
+
],
|
|
193
|
+
[
|
|
194
|
+
"foreign customer",
|
|
195
|
+
() =>
|
|
196
|
+
harness({
|
|
197
|
+
customer: {
|
|
198
|
+
...fixture().billingCustomer,
|
|
199
|
+
customer_account: "acct_foreign",
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
],
|
|
203
|
+
[
|
|
204
|
+
"unexpanded discount",
|
|
205
|
+
() =>
|
|
206
|
+
harness({
|
|
207
|
+
sub: alter(
|
|
208
|
+
fixture().subscription,
|
|
209
|
+
["discounts"],
|
|
210
|
+
["di_subscription"],
|
|
211
|
+
),
|
|
212
|
+
}),
|
|
213
|
+
],
|
|
214
|
+
[
|
|
215
|
+
"promotion source",
|
|
216
|
+
() =>
|
|
217
|
+
harness({
|
|
218
|
+
sub: alter(
|
|
219
|
+
fixture().subscription,
|
|
220
|
+
["discounts", 0, "source", "type"],
|
|
221
|
+
"promotion_code",
|
|
222
|
+
),
|
|
223
|
+
}),
|
|
224
|
+
],
|
|
225
|
+
[
|
|
226
|
+
"wrong coupon",
|
|
227
|
+
() =>
|
|
228
|
+
harness({
|
|
229
|
+
coupon: () => ({
|
|
230
|
+
id: "foreign",
|
|
231
|
+
object: "coupon",
|
|
232
|
+
duration: "forever",
|
|
233
|
+
livemode: false,
|
|
234
|
+
}),
|
|
235
|
+
}),
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
"deleted coupon",
|
|
239
|
+
() =>
|
|
240
|
+
harness({
|
|
241
|
+
coupon: (id: string) => ({ id, object: "coupon", deleted: true }),
|
|
242
|
+
}),
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
"foreign coupon mode",
|
|
246
|
+
() =>
|
|
247
|
+
harness({
|
|
248
|
+
coupon: (id: string) => ({
|
|
249
|
+
id,
|
|
250
|
+
object: "coupon",
|
|
251
|
+
livemode: true,
|
|
252
|
+
duration: "forever",
|
|
253
|
+
}),
|
|
254
|
+
}),
|
|
255
|
+
],
|
|
256
|
+
])("rejects %s without POST or alternate fallback", async (_label, build) => {
|
|
257
|
+
const h = build();
|
|
258
|
+
await expect(h.read()).rejects.toThrow();
|
|
259
|
+
expect(h.wires.every((w) => w.method === "GET")).toBe(true);
|
|
260
|
+
});
|
|
261
|
+
it("does not overwrite contradictory expanded coupon mode with a later join", async () => {
|
|
262
|
+
const h = harness({
|
|
263
|
+
sub: alter(
|
|
264
|
+
fixture().subscription,
|
|
265
|
+
["discounts", 0, "source", "coupon", "livemode"],
|
|
266
|
+
true,
|
|
267
|
+
),
|
|
268
|
+
});
|
|
269
|
+
await expect(h.read()).rejects.toThrow();
|
|
270
|
+
expect(
|
|
271
|
+
h.wires.filter((w) => w.path.startsWith("/v1/coupons/")),
|
|
272
|
+
).toHaveLength(0);
|
|
273
|
+
});
|
|
274
|
+
it("does not retry closed connections or fall back after planned read fails", async () => {
|
|
275
|
+
const h = harness({ fail: "/v1/coupons/" });
|
|
276
|
+
await expect(h.read()).rejects.toThrow();
|
|
277
|
+
expect(
|
|
278
|
+
h.wires.filter((w) => w.path.startsWith("/v1/coupons/")),
|
|
279
|
+
).toHaveLength(1);
|
|
280
|
+
expect(h.wires).toHaveLength(4);
|
|
281
|
+
});
|
|
282
|
+
it("rejects unqualified API before reading", async () => {
|
|
283
|
+
const h = harness();
|
|
284
|
+
await expect(
|
|
285
|
+
readRenewalPriceReplacementContext(
|
|
286
|
+
h.stripe,
|
|
287
|
+
{ expected: h.f.prepared.expected },
|
|
288
|
+
"2025-03-31.basil",
|
|
289
|
+
),
|
|
290
|
+
).rejects.toThrow();
|
|
291
|
+
expect(h.wires).toHaveLength(0);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
describe("pure retained attach request", () => {
|
|
295
|
+
function options() {
|
|
296
|
+
const f = fixture();
|
|
297
|
+
return {
|
|
298
|
+
prepared: f.prepared,
|
|
299
|
+
apiVersion: API_VERSION,
|
|
300
|
+
attachKey: "caller:attach",
|
|
301
|
+
firstDispatchedAt: f.preflightInput.observedAt,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
it("binds the exact full stage request with no mutating provider capability", () => {
|
|
305
|
+
const o = options();
|
|
306
|
+
const r = prepareRenewalScheduleAttachment(o);
|
|
307
|
+
const { requestIdentity, ...unsigned } = r;
|
|
308
|
+
expect(unsigned).toEqual({
|
|
309
|
+
version: 1,
|
|
310
|
+
stage: "attach",
|
|
311
|
+
scope: "platform",
|
|
312
|
+
apiVersion: API_VERSION,
|
|
313
|
+
logicalBinding: o.prepared.requestIdentity.binding,
|
|
314
|
+
method: "POST",
|
|
315
|
+
path: "/v1/subscription_schedules",
|
|
316
|
+
payload: { from_subscription: "sub_original" },
|
|
317
|
+
key: o.attachKey,
|
|
318
|
+
firstDispatchedAt: o.firstDispatchedAt,
|
|
319
|
+
});
|
|
320
|
+
expect(requestIdentity).toEqual({
|
|
321
|
+
key: o.attachKey,
|
|
322
|
+
binding: binding("renewal-attach", unsigned),
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
it("permits explicit caller choice of same logical and attach key; never derives one", () => {
|
|
326
|
+
const o = options();
|
|
327
|
+
o.attachKey = o.prepared.requestIdentity.key;
|
|
328
|
+
expect(prepareRenewalScheduleAttachment(o).key).toBe(o.attachKey);
|
|
329
|
+
});
|
|
330
|
+
it.each(["", "x\ny", "x".repeat(256)])(
|
|
331
|
+
"rejects invalid explicit key",
|
|
332
|
+
(key) =>
|
|
333
|
+
expect(() =>
|
|
334
|
+
prepareRenewalScheduleAttachment({ ...options(), attachKey: key }),
|
|
335
|
+
).toThrow(),
|
|
336
|
+
);
|
|
337
|
+
it("rejects forged preparation and arbitrary create options", () => {
|
|
338
|
+
expect(() =>
|
|
339
|
+
prepareRenewalScheduleAttachment({
|
|
340
|
+
...options(),
|
|
341
|
+
prepared: { ...options().prepared, quantity: 9 },
|
|
342
|
+
}),
|
|
343
|
+
).toThrow();
|
|
344
|
+
expect(() =>
|
|
345
|
+
(prepareRenewalScheduleAttachment as (value: unknown) => unknown)({
|
|
346
|
+
...options(),
|
|
347
|
+
metadata: { unexpected: "yes" },
|
|
348
|
+
}),
|
|
349
|
+
).toThrow();
|
|
350
|
+
});
|
|
351
|
+
it("rejects missing key/time and wrong API without generating replacement", () => {
|
|
352
|
+
const invalid = prepareRenewalScheduleAttachment as (
|
|
353
|
+
value: unknown,
|
|
354
|
+
) => unknown;
|
|
355
|
+
const o = options();
|
|
356
|
+
for (const x of [
|
|
357
|
+
{ ...o, attachKey: undefined },
|
|
358
|
+
{ ...o, firstDispatchedAt: undefined },
|
|
359
|
+
{ ...o, firstDispatchedAt: -1 },
|
|
360
|
+
{ ...o, apiVersion: "unknown" },
|
|
361
|
+
])
|
|
362
|
+
expect(() => invalid(x)).toThrow();
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
describe("public server SDK placement", () => {
|
|
367
|
+
it("exposes actual class reader and pure builder, selecting single-attempt client", async () => {
|
|
368
|
+
const publicApi = await import("../../index.js");
|
|
369
|
+
expect(publicApi.prepareRenewalPriceReplacement).toBeTypeOf("function");
|
|
370
|
+
const helpers = await import("../../helpers.js");
|
|
371
|
+
const h = harness();
|
|
372
|
+
const selected = vi
|
|
373
|
+
.spyOn(helpers, "getStripeClient")
|
|
374
|
+
.mockReturnValue(h.stripe);
|
|
375
|
+
const client = new publicApi.BetterStripe(
|
|
376
|
+
{} as ConstructorParameters<typeof publicApi.BetterStripe>[0],
|
|
377
|
+
{
|
|
378
|
+
STRIPE_SECRET_KEY: "sk_test_CONTROLLED",
|
|
379
|
+
stripeApiVersion: API_VERSION,
|
|
380
|
+
},
|
|
381
|
+
);
|
|
382
|
+
const reader = (
|
|
383
|
+
client as unknown as {
|
|
384
|
+
readRenewalPriceReplacementContext: (
|
|
385
|
+
ctx: unknown,
|
|
386
|
+
opts: unknown,
|
|
387
|
+
) => Promise<unknown>;
|
|
388
|
+
}
|
|
389
|
+
).readRenewalPriceReplacementContext;
|
|
390
|
+
expect(typeof reader).toBe("function");
|
|
391
|
+
await reader.call(client, {}, { expected: h.f.prepared.expected });
|
|
392
|
+
expect(selected).toHaveBeenCalledWith("sk_test_CONTROLLED", API_VERSION, {
|
|
393
|
+
singleAttempt: true,
|
|
394
|
+
});
|
|
395
|
+
const exported = (
|
|
396
|
+
publicApi as unknown as {
|
|
397
|
+
prepareRenewalScheduleAttachment: typeof prepareRenewalScheduleAttachment;
|
|
398
|
+
}
|
|
399
|
+
).prepareRenewalScheduleAttachment;
|
|
400
|
+
expect(
|
|
401
|
+
exported({
|
|
402
|
+
prepared: h.f.prepared,
|
|
403
|
+
apiVersion: API_VERSION,
|
|
404
|
+
attachKey: "caller:public",
|
|
405
|
+
firstDispatchedAt: h.f.preflightInput.observedAt,
|
|
406
|
+
}).key,
|
|
407
|
+
).toBe("caller:public");
|
|
408
|
+
});
|
|
409
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import {
|
|
4
|
+
renewalExpected,
|
|
5
|
+
renewalSubscription,
|
|
6
|
+
renewalPrice,
|
|
7
|
+
renewalBillingCustomer,
|
|
8
|
+
} from "../renewalReplacementSchema.js";
|
|
9
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
10
|
+
import {
|
|
11
|
+
productId,
|
|
12
|
+
referenceId,
|
|
13
|
+
resourceId,
|
|
14
|
+
supportedVersion,
|
|
15
|
+
unsupported,
|
|
16
|
+
timestamp,
|
|
17
|
+
} from "../renewalAggregate/shared.js";
|
|
18
|
+
import { joinRenewalCoupons } from "./couponJoins.js";
|
|
19
|
+
|
|
20
|
+
const input = z.object({ expected: renewalExpected }).strict();
|
|
21
|
+
export type ReadRenewalPriceReplacementContextOptions = z.infer<typeof input>;
|
|
22
|
+
const observedSchedule = z.union([
|
|
23
|
+
z.null(),
|
|
24
|
+
resourceId("sub_sched"),
|
|
25
|
+
z
|
|
26
|
+
.object({
|
|
27
|
+
id: resourceId("sub_sched"),
|
|
28
|
+
object: z.literal("subscription_schedule"),
|
|
29
|
+
deleted: z.literal(false).optional(),
|
|
30
|
+
})
|
|
31
|
+
.passthrough(),
|
|
32
|
+
]);
|
|
33
|
+
const readSubscription = renewalSubscription.extend({
|
|
34
|
+
schedule: observedSchedule,
|
|
35
|
+
});
|
|
36
|
+
/** Fixed provider read profile. Evidence acquisition is not write qualification. */
|
|
37
|
+
export async function readRenewalPriceReplacementContext(
|
|
38
|
+
stripe: Stripe,
|
|
39
|
+
options: ReadRenewalPriceReplacementContextOptions,
|
|
40
|
+
apiVersion: string,
|
|
41
|
+
) {
|
|
42
|
+
const args = input.parse(renewalSnapshot(options));
|
|
43
|
+
supportedVersion(apiVersion);
|
|
44
|
+
const expected = args.expected;
|
|
45
|
+
const requestOptions = {
|
|
46
|
+
apiVersion: "2026-09-30.endive" as const,
|
|
47
|
+
maxNetworkRetries: 0,
|
|
48
|
+
};
|
|
49
|
+
const subscription = readSubscription.parse(
|
|
50
|
+
renewalSnapshot(
|
|
51
|
+
await stripe.subscriptions.retrieve(
|
|
52
|
+
expected.subscriptionId,
|
|
53
|
+
{ expand: ["discounts", "items.data.discounts", "items.data.price"] },
|
|
54
|
+
requestOptions,
|
|
55
|
+
),
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
const item = subscription.items.data[0];
|
|
59
|
+
const context = {
|
|
60
|
+
customerAccountId: expected.customerAccountId,
|
|
61
|
+
livemode: expected.livemode,
|
|
62
|
+
};
|
|
63
|
+
if (
|
|
64
|
+
subscription.id !== expected.subscriptionId ||
|
|
65
|
+
subscription.customer_account !== expected.customerAccountId ||
|
|
66
|
+
subscription.livemode !== expected.livemode ||
|
|
67
|
+
item.id !== expected.itemId ||
|
|
68
|
+
item.subscription !== expected.subscriptionId ||
|
|
69
|
+
item.price.id !== expected.currentPriceId ||
|
|
70
|
+
item.price.livemode !== expected.livemode ||
|
|
71
|
+
productId(item.price.product, context) !== expected.currentProductId ||
|
|
72
|
+
item.current_period_start !== expected.periodStart ||
|
|
73
|
+
item.current_period_end !== expected.periodEnd
|
|
74
|
+
)
|
|
75
|
+
unsupported("original_read_identity_mismatch");
|
|
76
|
+
const targetPrice = renewalPrice.parse(
|
|
77
|
+
renewalSnapshot(
|
|
78
|
+
await stripe.prices.retrieve(expected.targetPriceId, {}, requestOptions),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
if (
|
|
82
|
+
targetPrice.id !== expected.targetPriceId ||
|
|
83
|
+
targetPrice.livemode !== expected.livemode ||
|
|
84
|
+
productId(targetPrice.product, context) !== expected.targetProductId
|
|
85
|
+
)
|
|
86
|
+
unsupported("target_read_identity_mismatch");
|
|
87
|
+
const customerId = referenceId(
|
|
88
|
+
subscription.customer,
|
|
89
|
+
"cus",
|
|
90
|
+
"customer",
|
|
91
|
+
context,
|
|
92
|
+
);
|
|
93
|
+
const billingCustomer = renewalBillingCustomer.parse(
|
|
94
|
+
renewalSnapshot(
|
|
95
|
+
await stripe.customers.retrieve(customerId, {}, requestOptions),
|
|
96
|
+
),
|
|
97
|
+
);
|
|
98
|
+
if (
|
|
99
|
+
billingCustomer.id !== customerId ||
|
|
100
|
+
billingCustomer.customer_account !== expected.customerAccountId ||
|
|
101
|
+
billingCustomer.livemode !== expected.livemode
|
|
102
|
+
)
|
|
103
|
+
unsupported("billing_customer_identity_mismatch");
|
|
104
|
+
const joined = await joinRenewalCoupons(
|
|
105
|
+
stripe,
|
|
106
|
+
subscription,
|
|
107
|
+
item,
|
|
108
|
+
expected.livemode,
|
|
109
|
+
requestOptions,
|
|
110
|
+
);
|
|
111
|
+
const normalizedSubscription = {
|
|
112
|
+
...subscription,
|
|
113
|
+
discounts: joined.subscriptionDiscounts,
|
|
114
|
+
items: {
|
|
115
|
+
...subscription.items,
|
|
116
|
+
data: [{ ...item, discounts: joined.itemDiscounts }],
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
const observedAt = timestamp.parse(Math.floor(Date.now() / 1000));
|
|
120
|
+
return renewalSnapshot({
|
|
121
|
+
version: 1 as const,
|
|
122
|
+
scope: "platform" as const,
|
|
123
|
+
apiVersion,
|
|
124
|
+
readProfile: "renewal-context-v1" as const,
|
|
125
|
+
providerQualification: "required" as const,
|
|
126
|
+
observedAt,
|
|
127
|
+
subscription: normalizedSubscription,
|
|
128
|
+
targetPrice,
|
|
129
|
+
billingCustomer,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { retainedPreparation } from "../renewalAggregate/preflight.js";
|
|
3
|
+
import {
|
|
4
|
+
binding,
|
|
5
|
+
supportedVersion,
|
|
6
|
+
timestamp,
|
|
7
|
+
unsupported,
|
|
8
|
+
} from "../renewalAggregate/shared.js";
|
|
9
|
+
import { idempotencyOptions } from "../../core/idempotency.js";
|
|
10
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
11
|
+
|
|
12
|
+
const input = z
|
|
13
|
+
.object({
|
|
14
|
+
prepared: z.unknown(),
|
|
15
|
+
apiVersion: z.string(),
|
|
16
|
+
attachKey: z.string(),
|
|
17
|
+
firstDispatchedAt: timestamp,
|
|
18
|
+
})
|
|
19
|
+
.strict();
|
|
20
|
+
export type PrepareRenewalScheduleAttachmentOptions = z.infer<typeof input>;
|
|
21
|
+
/** Pure retained request only. It neither authorizes nor dispatches attachment. */
|
|
22
|
+
export function prepareRenewalScheduleAttachment(
|
|
23
|
+
options: PrepareRenewalScheduleAttachmentOptions,
|
|
24
|
+
) {
|
|
25
|
+
const args = input.parse(renewalSnapshot(options));
|
|
26
|
+
supportedVersion(args.apiVersion);
|
|
27
|
+
const prepared = retainedPreparation(args.prepared);
|
|
28
|
+
const key = idempotencyOptions(args.attachKey)?.idempotencyKey;
|
|
29
|
+
if (!key) unsupported("missing_explicit_attach_key");
|
|
30
|
+
const request = {
|
|
31
|
+
version: 1 as const,
|
|
32
|
+
stage: "attach" as const,
|
|
33
|
+
scope: "platform" as const,
|
|
34
|
+
apiVersion: args.apiVersion,
|
|
35
|
+
logicalBinding: prepared.requestIdentity.binding,
|
|
36
|
+
method: "POST" as const,
|
|
37
|
+
path: "/v1/subscription_schedules" as const,
|
|
38
|
+
payload: { from_subscription: prepared.expected.subscriptionId },
|
|
39
|
+
key,
|
|
40
|
+
firstDispatchedAt: args.firstDispatchedAt,
|
|
41
|
+
};
|
|
42
|
+
return renewalSnapshot({
|
|
43
|
+
...request,
|
|
44
|
+
requestIdentity: { key, binding: binding("renewal-attach", request) },
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export type PreparedRenewalScheduleAttachment = ReturnType<
|
|
48
|
+
typeof prepareRenewalScheduleAttachment
|
|
49
|
+
>;
|
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
|
+
}
|