@fantastic.dev/better-stripe 0.88.0 → 0.89.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -1
  4. package/dist/client/billing/renewalAggregate/readMaterial.js +4 -3
  5. package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -1
  6. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts +27 -0
  7. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts.map +1 -0
  8. package/dist/client/billing/renewalAttach/internal/originalOwner.js +236 -0
  9. package/dist/client/billing/renewalAttach/internal/originalOwner.js.map +1 -0
  10. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts +5 -0
  11. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts.map +1 -0
  12. package/dist/client/billing/renewalAttach/internal/sourceBindings.js +9 -0
  13. package/dist/client/billing/renewalAttach/internal/sourceBindings.js.map +1 -0
  14. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts +6 -0
  15. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts.map +1 -0
  16. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js +35 -0
  17. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js.map +1 -0
  18. package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
  19. package/dist/client/billing/renewalPriceReplacement.js +7 -3
  20. package/dist/client/billing/renewalPriceReplacement.js.map +1 -1
  21. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  22. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
  23. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  24. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  25. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  26. package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
  27. package/docs/renewal-context-reads.md +25 -0
  28. package/docs/renewal-phase-assembly.md +7 -0
  29. package/package.json +1 -1
  30. package/src/client/billing/renewalAggregate/readMaterial.ts +5 -3
  31. package/src/client/billing/renewalAttach/internal/originalOwner.test.ts +258 -0
  32. package/src/client/billing/renewalAttach/internal/originalOwner.ts +280 -0
  33. package/src/client/billing/renewalAttach/internal/sourceBindings.test.ts +13 -0
  34. package/src/client/billing/renewalAttach/internal/sourceBindings.ts +8 -0
  35. package/src/client/billing/renewalAttach/internal/stockRawDelegate.test.ts +156 -0
  36. package/src/client/billing/renewalAttach/internal/stockRawDelegate.ts +43 -0
  37. package/src/client/billing/renewalPriceReplacement.ts +7 -3
  38. package/src/client/billing/renewalV2/rawRetention.test.ts +293 -0
@@ -0,0 +1,293 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { webcrypto } from "node:crypto";
3
+ import * as sdk from "./index.js";
4
+ import { prepareRenewalPriceReplacement as legacy } from "../renewalPriceReplacement.js";
5
+ import { validateReadMaterial } from "../renewalAggregate/readMaterial.js";
6
+ import { fixture } from "../../../../tests/renewalAggregate/fixtures.js";
7
+ import {
8
+ at,
9
+ binding,
10
+ bound,
11
+ configuration,
12
+ expandedInput,
13
+ hash,
14
+ input,
15
+ own,
16
+ owns,
17
+ paths,
18
+ } from "../../../../tests/renewalAggregate/rawRetention.js";
19
+
20
+ afterEach(() => vi.unstubAllGlobals());
21
+ it("records unaffected normal full-output vectors and independent C1 oracle", async () => {
22
+ const raw = input(),
23
+ old = legacy(raw),
24
+ p = await sdk.prepareRenewalPriceReplacementV2(raw);
25
+ expect(p.requestIdentity.binding).toBe(
26
+ binding("renewal", { ...bound(old), version: 2 }),
27
+ );
28
+ const c = configuration(),
29
+ receipt = await sdk.constructRenewalScheduleAttachmentReceiptV2(
30
+ c.attachReceipt,
31
+ ),
32
+ projected = await sdk.projectRenewalConfigurationV2(c);
33
+ expect(projected.status).toBe("projected_configure_request");
34
+ expect({
35
+ legacy: hash(old),
36
+ prepared: hash(p),
37
+ receipt: hash(receipt),
38
+ configuration: hash(projected),
39
+ }).toEqual({
40
+ legacy: "ed0e7fe3641e216caf66fd225b1a7366a4a3ab7f2825448ec9fbbf697621e150",
41
+ prepared:
42
+ "4c27c7f4c0f4743d6a5ff3c76da42c024c4f2e29f6bc65b4ad29bb21dfdbc8d3",
43
+ receipt: "dd79636e1248efc8efad99737bcd156d322f3078aea7ecebf007f0d1d953b01c",
44
+ configuration:
45
+ "ef08ad09044c32eaa3152b3a7fe058a68f77edf704c3176ada86cd5c5c6179d8",
46
+ });
47
+ });
48
+ it.each(paths.map((path) => ({ path })))(
49
+ "retains and binds safe own proto at passthrough body $path",
50
+ async ({ path }) => {
51
+ const raw = expandedInput(),
52
+ body = at(raw, path as (string | number)[]);
53
+ own(body, "__proto__", { safe: "retained" });
54
+ const expected = {
55
+ subscription: raw.subscription,
56
+ targetPrice: raw.targetPrice,
57
+ billingCustomer: raw.billingCustomer,
58
+ };
59
+ const old = legacy(raw),
60
+ p = await sdk.prepareRenewalPriceReplacementV2(raw);
61
+ expect(old.providerSnapshot).toEqual(expected);
62
+ expect(p.providerSnapshot).toEqual(expected);
63
+ expect(
64
+ owns(at(p.providerSnapshot, path as (string | number)[]), "__proto__"),
65
+ ).toBe(true);
66
+ expect(p.requestIdentity.binding).toBe(
67
+ binding("renewal", {
68
+ version: 2,
69
+ scope: "platform",
70
+ expected: raw.expected,
71
+ quantity: raw.subscription.items.data[0].quantity,
72
+ providerSnapshot: expected,
73
+ }),
74
+ );
75
+ own(body, "__proto__", { safe: "changed" });
76
+ expect(
77
+ (await sdk.prepareRenewalPriceReplacementV2(raw)).requestIdentity.binding,
78
+ ).not.toBe(p.requestIdentity.binding);
79
+ },
80
+ );
81
+ it.each(["subscription", "targetPrice", "billingCustomer"])(
82
+ "same raw %s special key passes internal read eligibility",
83
+ (field) => {
84
+ const raw: any = input();
85
+ own(raw[field], "__proto__", { safe: "same" });
86
+ const f = fixture();
87
+ const prepared: any = {
88
+ expected: raw.expected,
89
+ quantity: raw.subscription.items.data[0].quantity,
90
+ providerSnapshot: {
91
+ subscription: raw.subscription,
92
+ targetPrice: raw.targetPrice,
93
+ billingCustomer: raw.billingCustomer,
94
+ },
95
+ };
96
+ const args = {
97
+ freshSubscription: structuredClone(raw.subscription),
98
+ targetPrice: structuredClone(raw.targetPrice),
99
+ billingCustomer: structuredClone(raw.billingCustomer),
100
+ apiVersion: f.preflightInput.apiVersion,
101
+ observedAt: f.preflightInput.observedAt,
102
+ };
103
+ expect(() => validateReadMaterial(args, prepared)).not.toThrow();
104
+ own(
105
+ (args as any)[field === "subscription" ? "freshSubscription" : field],
106
+ "__proto__",
107
+ { safe: "drift" },
108
+ );
109
+ expect(() => validateReadMaterial(args, prepared)).toThrow();
110
+ },
111
+ );
112
+ it.each(["subscription", "targetPrice", "billingCustomer"])(
113
+ "C1 C3 C6 uses identical full raw %s and rejects fresh drift",
114
+ async (field) => {
115
+ const raw: any = input();
116
+ own(raw[field], "__proto__", { safe: "same" });
117
+ const c = configuration(raw);
118
+ expect((await sdk.projectRenewalConfigurationV2(c)).status).toBe(
119
+ "projected_configure_request",
120
+ );
121
+ own(
122
+ c[field === "subscription" ? "freshSubscription" : field],
123
+ "__proto__",
124
+ { safe: "drift" },
125
+ );
126
+ expect((await sdk.projectRenewalConfigurationV2(c)).status).toBe(
127
+ "conflict",
128
+ );
129
+ },
130
+ );
131
+ it.each(["constructor", "prototype"])(
132
+ "retains safe own %s without prototype pollution",
133
+ async (key) => {
134
+ const raw: any = input();
135
+ for (const body of [raw.subscription, raw.targetPrice, raw.billingCustomer])
136
+ own(body, key, { prototype: { BTS313_POLLUTED: true } });
137
+ const p = await sdk.prepareRenewalPriceReplacementV2(raw);
138
+ expect(p.providerSnapshot.subscription[key]).toEqual(raw.subscription[key]);
139
+ expect(Object.getPrototypeOf(p.providerSnapshot.subscription)).toBe(
140
+ Object.prototype,
141
+ );
142
+ expect((Object.prototype as any).BTS313_POLLUTED).toBeUndefined();
143
+ },
144
+ );
145
+ it("keeps opaque unknown nested raw JSON and strict eligibility distinct", async () => {
146
+ const raw: any = input();
147
+ own(raw.subscription.metadata, "__proto__", { opaque: true });
148
+ raw.subscription.items.data[0].plan = { opaque: true };
149
+ own(raw.subscription.items.data[0].plan, "__proto__", { nested: true });
150
+ const p = await sdk.prepareRenewalPriceReplacementV2(raw);
151
+ expect(p.providerSnapshot.subscription.metadata).toEqual(
152
+ raw.subscription.metadata,
153
+ );
154
+ expect((p.providerSnapshot.subscription.items.data[0] as any).plan).toEqual(
155
+ raw.subscription.items.data[0].plan,
156
+ );
157
+ for (const key of ["__proto__", "constructor", "prototype"]) {
158
+ const invalid: any = input();
159
+ own(invalid.subscription.payment_settings, key, { extra: true });
160
+ await expect(
161
+ sdk.prepareRenewalPriceReplacementV2(invalid),
162
+ ).rejects.toThrow();
163
+ }
164
+ });
165
+ it("preserves Customer absence and rejects invalid present Customer", async () => {
166
+ const raw: any = input();
167
+ delete raw.billingCustomer;
168
+ expect(
169
+ owns(
170
+ (await sdk.prepareRenewalPriceReplacementV2(raw)).providerSnapshot,
171
+ "billingCustomer",
172
+ ),
173
+ ).toBe(false);
174
+ raw.billingCustomer = null;
175
+ await expect(sdk.prepareRenewalPriceReplacementV2(raw)).rejects.toThrow();
176
+ });
177
+ it("keeps C3 C4 complete Schedule bodies and C5 raw default preimage", async () => {
178
+ const c = configuration();
179
+ own(c.attachReceipt.providerSnapshot, "__proto__", { schedule: true });
180
+ own(c.schedule, "__proto__", { schedule: true });
181
+ const r = await sdk.constructRenewalScheduleAttachmentReceiptV2(
182
+ c.attachReceipt,
183
+ );
184
+ expect(owns(r.providerSnapshot, "__proto__")).toBe(true);
185
+ expect(r.providerSnapshot).toEqual(c.attachReceipt.providerSnapshot);
186
+ const projected = await sdk.projectRenewalConfigurationV2(c);
187
+ expect(projected.status).toBe("projected_configure_request");
188
+ if (projected.status !== "projected_configure_request")
189
+ throw Error("fixture");
190
+ expect(projected.baselineBinding).toBe(
191
+ binding("renewal-baseline", c.attachReceipt.providerSnapshot),
192
+ );
193
+ expect(projected.defaultSettingsBinding).toBe(
194
+ binding("renewal-defaults", c.schedule.default_settings),
195
+ );
196
+ own(c.schedule.default_settings, "__proto__", { unsupported: true });
197
+ c.attachReceipt.providerSnapshot = structuredClone(c.schedule);
198
+ expect((await sdk.projectRenewalConfigurationV2(c)).status).toBe(
199
+ "attached_unconfigured",
200
+ );
201
+ });
202
+ it("captures retained raw keys before WebCrypto await and leaves no live alias", async () => {
203
+ const raw: any = input();
204
+ own(raw.subscription, "__proto__", { value: "before" });
205
+ let release!: () => void, entered!: () => void;
206
+ const hold = new Promise<void>((r) => (release = r)),
207
+ started = new Promise<void>((r) => (entered = r));
208
+ vi.stubGlobal("crypto", {
209
+ subtle: {
210
+ digest: async (n: any, b: any) => {
211
+ entered();
212
+ await hold;
213
+ return webcrypto.subtle.digest(n, b);
214
+ },
215
+ },
216
+ });
217
+ const pending = sdk.prepareRenewalPriceReplacementV2(raw);
218
+ await Promise.race([started, pending]);
219
+ raw.subscription.__proto__.value = "after";
220
+ release();
221
+ const p = await pending;
222
+ expect((p.providerSnapshot.subscription as any).__proto__).toEqual({
223
+ value: "before",
224
+ });
225
+ });
226
+ it("does not turn custom prototypes/accessors or overbudget raw data into retained JSON", async () => {
227
+ for (const kind of ["prototype", "accessor", "budget"]) {
228
+ const raw: any = input();
229
+ if (kind === "prototype")
230
+ Object.setPrototypeOf(raw.subscription, { foreign: true });
231
+ else if (kind === "accessor")
232
+ Object.defineProperty(raw.subscription, "bad", {
233
+ enumerable: true,
234
+ get() {
235
+ throw Error("must not invoke getter");
236
+ },
237
+ });
238
+ else own(raw.subscription, "__proto__", { pad: "x".repeat(500001) });
239
+ await expect(sdk.prepareRenewalPriceReplacementV2(raw)).rejects.toThrow();
240
+ }
241
+ });
242
+ it("retained restored fields keep UTF8 hash caps and cannot adopt a stale binding", async () => {
243
+ const raw: any = input();
244
+ own(raw.subscription, "__proto__", { pad: "雪".repeat(170000) });
245
+ expect(legacy(raw).providerSnapshot.subscription).toEqual(raw.subscription);
246
+ const spy = vi.fn();
247
+ vi.stubGlobal("crypto", { subtle: { digest: spy } });
248
+ await expect(sdk.prepareRenewalPriceReplacementV2(raw)).rejects.toThrow();
249
+ expect(spy).not.toHaveBeenCalled();
250
+ vi.unstubAllGlobals();
251
+ const p: any = await sdk.prepareRenewalPriceReplacementV2(input());
252
+ own(p.providerSnapshot.subscription, "__proto__", { newRaw: true });
253
+ await expect(
254
+ sdk.prepareRenewalScheduleAttachmentV2({
255
+ prepared: p,
256
+ apiVersion: "2026-09-30.endive",
257
+ attachKey: "explicit:attach",
258
+ firstDispatchedAt: 1768867200,
259
+ }),
260
+ ).rejects.toThrow();
261
+ });
262
+ it("C5 retains eligible opaque special keys inside validated expanded defaults", async () => {
263
+ const p: any = input();
264
+ p.subscription.default_payment_method = {
265
+ id: "pm_retained",
266
+ object: "payment_method",
267
+ customer: "cus_buyer",
268
+ customer_account: "acct_buyer",
269
+ livemode: false,
270
+ };
271
+ const c = configuration(p);
272
+ c.schedule.default_settings.default_payment_method = structuredClone(
273
+ p.subscription.default_payment_method,
274
+ );
275
+ own(c.schedule.default_settings.default_payment_method, "__proto__", {
276
+ safe: "one",
277
+ });
278
+ c.attachReceipt.providerSnapshot = structuredClone(c.schedule);
279
+ const first = await sdk.projectRenewalConfigurationV2(c);
280
+ expect(first.status).toBe("projected_configure_request");
281
+ if (first.status !== "projected_configure_request") throw Error("fixture");
282
+ expect(first.defaultSettingsBinding).toBe(
283
+ binding("renewal-defaults", c.schedule.default_settings),
284
+ );
285
+ own(c.schedule.default_settings.default_payment_method, "__proto__", {
286
+ safe: "two",
287
+ });
288
+ c.attachReceipt.providerSnapshot = structuredClone(c.schedule);
289
+ const second = await sdk.projectRenewalConfigurationV2(c);
290
+ expect(second.status).toBe("projected_configure_request");
291
+ if (second.status !== "projected_configure_request") throw Error("fixture");
292
+ expect(second.defaultSettingsBinding).not.toBe(first.defaultSettingsBinding);
293
+ });