@fantastic.dev/better-stripe 0.84.0 → 0.86.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/billing/renewalAggregate/configure.d.ts.map +1 -1
- package/dist/client/billing/renewalAggregate/configure.js +4 -56
- package/dist/client/billing/renewalAggregate/configure.js.map +1 -1
- package/dist/client/billing/renewalAggregate/configureMaterial.d.ts +29 -0
- package/dist/client/billing/renewalAggregate/configureMaterial.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/configureMaterial.js +63 -0
- package/dist/client/billing/renewalAggregate/configureMaterial.js.map +1 -0
- package/dist/client/billing/renewalAggregate/phases.d.ts +2 -2
- package/dist/client/billing/renewalAggregate/phases.d.ts.map +1 -1
- package/dist/client/billing/renewalAggregate/phases.js.map +1 -1
- package/dist/client/billing/renewalAggregate/preflight.d.ts +1 -118
- package/dist/client/billing/renewalAggregate/preflight.d.ts.map +1 -1
- package/dist/client/billing/renewalAggregate/preflight.js +3 -118
- package/dist/client/billing/renewalAggregate/preflight.js.map +1 -1
- package/dist/client/billing/renewalAggregate/readMaterial.d.ts +335 -0
- package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/readMaterial.js +121 -0
- package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -0
- package/dist/client/billing/renewalV2/binding.d.ts +1 -1
- package/dist/client/billing/renewalV2/binding.d.ts.map +1 -1
- package/dist/client/billing/renewalV2/binding.js.map +1 -1
- package/dist/client/billing/renewalV2/configureInput.d.ts +45 -0
- package/dist/client/billing/renewalV2/configureInput.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/configureInput.js +69 -0
- package/dist/client/billing/renewalV2/configureInput.js.map +1 -0
- package/dist/client/billing/renewalV2/index.d.ts +46 -0
- package/dist/client/billing/renewalV2/index.d.ts.map +1 -1
- package/dist/client/billing/renewalV2/index.js +134 -1
- package/dist/client/billing/renewalV2/index.js.map +1 -1
- package/dist/client/billing/renewalV2/receipt.d.ts +37 -0
- package/dist/client/billing/renewalV2/receipt.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/receipt.js +131 -0
- package/dist/client/billing/renewalV2/receipt.js.map +1 -0
- package/dist/client/renewal.d.ts +2 -2
- package/dist/client/renewal.d.ts.map +1 -1
- package/dist/client/renewal.js +1 -1
- package/dist/client/renewal.js.map +1 -1
- package/dist/client/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- 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/docs/renewal-context-reads.md +73 -3
- package/docs/renewal-phase-assembly.md +78 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAggregate/configure.ts +12 -79
- package/src/client/billing/renewalAggregate/configureMaterial.ts +122 -0
- package/src/client/billing/renewalAggregate/phases.ts +2 -2
- package/src/client/billing/renewalAggregate/preflight.ts +6 -161
- package/src/client/billing/renewalAggregate/readMaterial.ts +173 -0
- package/src/client/billing/renewalV2/binding.ts +7 -1
- package/src/client/billing/renewalV2/configure.test.ts +362 -0
- package/src/client/billing/renewalV2/configureInput.ts +84 -0
- package/src/client/billing/renewalV2/index.ts +228 -1
- package/src/client/billing/renewalV2/receipt.test.ts +289 -0
- package/src/client/billing/renewalV2/receipt.ts +163 -0
- package/src/client/renewal.ts +7 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
|
+
import { createHash, webcrypto } from "node:crypto";
|
|
3
|
+
import * as sdk from "./index.js";
|
|
4
|
+
import { projectRenewalConfiguration as legacy } from "../renewalAggregate/configure.js";
|
|
5
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
6
|
+
import {
|
|
7
|
+
fixture,
|
|
8
|
+
type FixtureOptions,
|
|
9
|
+
} from "../../../../tests/renewalAggregate/fixtures.js";
|
|
10
|
+
|
|
11
|
+
const project = (raw: any): Promise<any> =>
|
|
12
|
+
sdk.projectRenewalConfigurationV2(raw);
|
|
13
|
+
function scenario(options: FixtureOptions = {}) {
|
|
14
|
+
const f = fixture(options);
|
|
15
|
+
const preparation = {
|
|
16
|
+
subscription: f.subscription,
|
|
17
|
+
targetPrice: f.targetPrice,
|
|
18
|
+
billingCustomer: f.billingCustomer,
|
|
19
|
+
expected: f.prepared.expected,
|
|
20
|
+
key: f.prepared.requestIdentity.key,
|
|
21
|
+
};
|
|
22
|
+
const { apiVersion, payload, key, firstDispatchedAt } = f.attachRequest;
|
|
23
|
+
const raw = {
|
|
24
|
+
preparation,
|
|
25
|
+
attachReceipt: {
|
|
26
|
+
attachRequest: {
|
|
27
|
+
preparation: structuredClone(preparation),
|
|
28
|
+
apiVersion,
|
|
29
|
+
attachKey: key,
|
|
30
|
+
firstDispatchedAt,
|
|
31
|
+
},
|
|
32
|
+
actualDispatch: {
|
|
33
|
+
scope: "platform" as const,
|
|
34
|
+
apiVersion,
|
|
35
|
+
method: "POST" as const,
|
|
36
|
+
path: "/v1/subscription_schedules" as const,
|
|
37
|
+
payload,
|
|
38
|
+
key,
|
|
39
|
+
firstDispatchedAt,
|
|
40
|
+
},
|
|
41
|
+
providerSnapshot: structuredClone(f.schedule),
|
|
42
|
+
},
|
|
43
|
+
freshSubscription: structuredClone(f.configureInput.freshSubscription),
|
|
44
|
+
targetPrice: structuredClone(f.targetPrice),
|
|
45
|
+
billingCustomer: structuredClone(f.billingCustomer),
|
|
46
|
+
schedule: structuredClone(f.schedule),
|
|
47
|
+
apiVersion,
|
|
48
|
+
observedAt: f.configureInput.observedAt,
|
|
49
|
+
firstDispatchedAt: f.configureInput.firstDispatchedAt,
|
|
50
|
+
configureKey: f.configureInput.configureKey,
|
|
51
|
+
};
|
|
52
|
+
return { raw, old: f.configureInput };
|
|
53
|
+
}
|
|
54
|
+
const digest = (family: string, value: unknown) =>
|
|
55
|
+
`${family}:v2:sha256:${createHash("sha256")
|
|
56
|
+
.update(
|
|
57
|
+
JSON.stringify(
|
|
58
|
+
renewalSnapshot(["betterstripe-renewal-binding", 2, family, value]),
|
|
59
|
+
),
|
|
60
|
+
"utf8",
|
|
61
|
+
)
|
|
62
|
+
.digest("hex")}`;
|
|
63
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
64
|
+
it.each([
|
|
65
|
+
{},
|
|
66
|
+
{ targetInterval: "year" },
|
|
67
|
+
{ start: 1769731200, end: 1772150400, observedAt: 1769817600 },
|
|
68
|
+
])(
|
|
69
|
+
"projects complete V1-equivalent phases/profile with direct C4/C5/C6 bindings %j",
|
|
70
|
+
async (options) => {
|
|
71
|
+
expect(sdk.projectRenewalConfigurationV2).toBeTypeOf("function");
|
|
72
|
+
const { raw, old } = scenario(options),
|
|
73
|
+
before = structuredClone(raw);
|
|
74
|
+
const configurationOptions: sdk.ProjectRenewalConfigurationV2Options = raw;
|
|
75
|
+
const result =
|
|
76
|
+
await sdk.projectRenewalConfigurationV2(configurationOptions),
|
|
77
|
+
original = legacy(old);
|
|
78
|
+
expect(original.status).toBe("projected_configure_request");
|
|
79
|
+
if (original.status !== "projected_configure_request")
|
|
80
|
+
throw Error("fixture");
|
|
81
|
+
expect(result.status).toBe("projected_configure_request");
|
|
82
|
+
if (result.status !== "projected_configure_request")
|
|
83
|
+
throw Error("projection");
|
|
84
|
+
expect(result.payload).toEqual(original.payload);
|
|
85
|
+
expect(result.profile).toEqual(original.profile);
|
|
86
|
+
expect(result.version).toBe(2);
|
|
87
|
+
expect(result.providerQualification).toBe("required");
|
|
88
|
+
expect(result.baselineBinding).toBe(
|
|
89
|
+
digest("renewal-baseline", raw.attachReceipt.providerSnapshot),
|
|
90
|
+
);
|
|
91
|
+
expect(result.defaultSettingsBinding).toBe(
|
|
92
|
+
digest("renewal-defaults", raw.schedule.default_settings),
|
|
93
|
+
);
|
|
94
|
+
const {
|
|
95
|
+
status: _status,
|
|
96
|
+
profile: _profile,
|
|
97
|
+
providerQualification: _qualification,
|
|
98
|
+
requestIdentity,
|
|
99
|
+
...unsigned
|
|
100
|
+
} = result;
|
|
101
|
+
expect(requestIdentity).toEqual({
|
|
102
|
+
key: raw.configureKey,
|
|
103
|
+
binding: digest("renewal-configure", unsigned),
|
|
104
|
+
});
|
|
105
|
+
expect(raw).toEqual(before);
|
|
106
|
+
expect(result.payload).not.toHaveProperty("default_settings");
|
|
107
|
+
expect(result.payload.phases[1]).not.toHaveProperty("end_date");
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
it.each([
|
|
111
|
+
[
|
|
112
|
+
"distinct key",
|
|
113
|
+
(x: any) => {
|
|
114
|
+
x.configureKey =
|
|
115
|
+
x.attachReceipt.actualDispatch?.key ?? x.attachReceipt.request.key;
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
[
|
|
119
|
+
"dispatch ordering",
|
|
120
|
+
(x: any) => {
|
|
121
|
+
x.firstDispatchedAt = x.observedAt + 1;
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
[
|
|
125
|
+
"source drift",
|
|
126
|
+
(x: any) => {
|
|
127
|
+
x.freshSubscription.currency = "eur";
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
[
|
|
131
|
+
"association",
|
|
132
|
+
(x: any) => {
|
|
133
|
+
x.freshSubscription.schedule = null;
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
[
|
|
137
|
+
"baseline",
|
|
138
|
+
(x: any) => {
|
|
139
|
+
x.schedule.opaque_future_field = true;
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
])("preserves exact existing Failure data for %s", async (_name, mutate) => {
|
|
143
|
+
const { raw, old } = scenario();
|
|
144
|
+
mutate(raw);
|
|
145
|
+
mutate(old);
|
|
146
|
+
expect(await project(raw)).toEqual(legacy(old));
|
|
147
|
+
});
|
|
148
|
+
it.each([
|
|
149
|
+
{ item: { current_trial: { trial_end: 1770000000 } } },
|
|
150
|
+
{ phase: { trial: true } },
|
|
151
|
+
{ defaults: { collection_method: "unsupported" } },
|
|
152
|
+
])(
|
|
153
|
+
"preserves unsupported owned-Schedule Failure reasons %j",
|
|
154
|
+
async (options) => {
|
|
155
|
+
const { raw, old } = scenario(options);
|
|
156
|
+
expect(await project(raw)).toEqual(legacy(old));
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
it("keeps invalid receipt aliases before the owned-Schedule failure boundary", async () => {
|
|
160
|
+
const { raw } = scenario();
|
|
161
|
+
raw.apiVersion = "different";
|
|
162
|
+
expect(await project(raw)).toEqual({
|
|
163
|
+
status: "conflict",
|
|
164
|
+
reason: "invalid_attach_receipt_binding",
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
it("rejects repeated raw preparation differences instead of accepting child claims", async () => {
|
|
168
|
+
const { raw } = scenario();
|
|
169
|
+
raw.attachReceipt.attachRequest.preparation.key = "other:logical";
|
|
170
|
+
expect((await project(raw)).status).not.toBe("projected_configure_request");
|
|
171
|
+
const invalid: any = scenario().raw;
|
|
172
|
+
invalid.prepared = fixture().prepared;
|
|
173
|
+
await expect(project(invalid)).rejects.toThrow();
|
|
174
|
+
});
|
|
175
|
+
it.each(["root", "preparation", "attachReceipt"])(
|
|
176
|
+
"rejects unknown %s names before inspecting values",
|
|
177
|
+
async (where) => {
|
|
178
|
+
const { raw } = scenario();
|
|
179
|
+
const value: any =
|
|
180
|
+
where === "root" ? raw : raw[where as "preparation" | "attachReceipt"];
|
|
181
|
+
const read = vi.fn();
|
|
182
|
+
Object.defineProperty(value, "unexpected", { enumerable: true, get: read });
|
|
183
|
+
await expect(project(raw)).rejects.toThrow();
|
|
184
|
+
expect(read).not.toHaveBeenCalled();
|
|
185
|
+
},
|
|
186
|
+
);
|
|
187
|
+
it("rechecks captured required fields before any digest after reflection mutation", async () => {
|
|
188
|
+
const { raw } = scenario();
|
|
189
|
+
const target: any = raw;
|
|
190
|
+
const proxy = new Proxy(target, {
|
|
191
|
+
getPrototypeOf(object) {
|
|
192
|
+
delete object.observedAt;
|
|
193
|
+
return Object.prototype;
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
const digest = vi.fn();
|
|
197
|
+
vi.stubGlobal("crypto", { subtle: { digest } });
|
|
198
|
+
await expect(project(proxy)).rejects.toThrow();
|
|
199
|
+
expect(digest).not.toHaveBeenCalled();
|
|
200
|
+
});
|
|
201
|
+
it("preserves absent-vs-present input fields and requires actual default settings", async () => {
|
|
202
|
+
const missing: any = scenario().raw;
|
|
203
|
+
delete missing.schedule.default_settings;
|
|
204
|
+
expect((await project(missing)).status).not.toBe(
|
|
205
|
+
"projected_configure_request",
|
|
206
|
+
);
|
|
207
|
+
const explicit: any = scenario().raw;
|
|
208
|
+
explicit.billingCustomer = undefined;
|
|
209
|
+
await expect(project(explicit)).rejects.toThrow();
|
|
210
|
+
});
|
|
211
|
+
it("owns all inputs before awaiting hashing", async () => {
|
|
212
|
+
const { raw } = scenario();
|
|
213
|
+
let release!: () => void;
|
|
214
|
+
const held = new Promise<void>((r) => {
|
|
215
|
+
release = r;
|
|
216
|
+
});
|
|
217
|
+
let calls = 0;
|
|
218
|
+
vi.stubGlobal("crypto", {
|
|
219
|
+
subtle: {
|
|
220
|
+
async digest(
|
|
221
|
+
algorithm: string,
|
|
222
|
+
bytes: Parameters<typeof webcrypto.subtle.digest>[1],
|
|
223
|
+
) {
|
|
224
|
+
if (++calls === 1) await held;
|
|
225
|
+
return webcrypto.subtle.digest(algorithm, bytes);
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
const pending = project(raw);
|
|
230
|
+
raw.freshSubscription.schedule = null as any;
|
|
231
|
+
raw.configureKey = "changed";
|
|
232
|
+
release();
|
|
233
|
+
const result = await pending;
|
|
234
|
+
expect(result.status).toBe("projected_configure_request");
|
|
235
|
+
expect(result.key).not.toBe("changed");
|
|
236
|
+
});
|
|
237
|
+
it("projects substantial bounded raw bodies without building nested V1 claims", async () => {
|
|
238
|
+
const { raw } = scenario();
|
|
239
|
+
for (const body of [raw.schedule, raw.attachReceipt.providerSnapshot])
|
|
240
|
+
(body as any).opaque_retained = "x".repeat(90000);
|
|
241
|
+
expect(JSON.stringify(raw).length).toBeLessThan(500000);
|
|
242
|
+
const result = await project(raw);
|
|
243
|
+
expect(result.status).toBe("projected_configure_request");
|
|
244
|
+
expect(result.requestIdentity.binding.length).toBeLessThan(100);
|
|
245
|
+
});
|
|
246
|
+
it("returns the existing unsupported union when hashing fails", async () => {
|
|
247
|
+
vi.stubGlobal("crypto", {
|
|
248
|
+
subtle: { digest: () => Promise.reject(Error("failure")) },
|
|
249
|
+
});
|
|
250
|
+
expect(await project(scenario().raw)).toEqual({
|
|
251
|
+
status: "conflict",
|
|
252
|
+
reason: "invalid_prepared_binding",
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
it("preserves the owned-Schedule failure boundary when a later binding fails", async () => {
|
|
256
|
+
let calls = 0;
|
|
257
|
+
vi.stubGlobal("crypto", {
|
|
258
|
+
subtle: {
|
|
259
|
+
async digest(
|
|
260
|
+
algorithm: string,
|
|
261
|
+
bytes: Parameters<typeof webcrypto.subtle.digest>[1],
|
|
262
|
+
) {
|
|
263
|
+
if (++calls === 6) throw Error("late digest failure");
|
|
264
|
+
return webcrypto.subtle.digest(algorithm, bytes);
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
expect(await project(scenario().raw)).toEqual({
|
|
269
|
+
status: "attached_unconfigured",
|
|
270
|
+
scheduleId: "sub_sched_owned",
|
|
271
|
+
reason: "incomplete_or_unsupported_schema",
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
it("retains the old missing-optional-Customer failure without inventing a read", async () => {
|
|
275
|
+
const f: any = scenario();
|
|
276
|
+
delete f.raw.billingCustomer;
|
|
277
|
+
delete f.old.billingCustomer;
|
|
278
|
+
expect(await project(f.raw)).toEqual(legacy(f.old));
|
|
279
|
+
});
|
|
280
|
+
it("does not accept a mismatched complete attachment dispatch through C6", async () => {
|
|
281
|
+
const f = scenario();
|
|
282
|
+
f.raw.attachReceipt.actualDispatch.payload.from_subscription = "sub_foreign";
|
|
283
|
+
expect(await project(f.raw)).toEqual({
|
|
284
|
+
status: "conflict",
|
|
285
|
+
reason: "invalid_attach_receipt_binding",
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
const ownSpecial = (body: object, value: unknown) =>
|
|
289
|
+
Object.defineProperty(body, "__proto__", { enumerable: true, value });
|
|
290
|
+
it("preserves identical full Schedule special own JSON keys in C4 and comparison", async () => {
|
|
291
|
+
const f = scenario();
|
|
292
|
+
ownSpecial(f.raw.schedule, { retained: true });
|
|
293
|
+
ownSpecial(f.raw.attachReceipt.providerSnapshot, { retained: true });
|
|
294
|
+
const before = structuredClone(f.raw),
|
|
295
|
+
result = await project(f.raw);
|
|
296
|
+
expect(result.status).toBe("projected_configure_request");
|
|
297
|
+
expect(result.baselineBinding).toBe(
|
|
298
|
+
digest("renewal-baseline", f.raw.attachReceipt.providerSnapshot),
|
|
299
|
+
);
|
|
300
|
+
expect(result.defaultSettingsBinding).toBe(
|
|
301
|
+
digest("renewal-defaults", f.raw.schedule.default_settings),
|
|
302
|
+
);
|
|
303
|
+
expect(f.raw).toEqual(before);
|
|
304
|
+
});
|
|
305
|
+
it("binds special own-key changes and rejects real full-baseline drift", async () => {
|
|
306
|
+
const a = scenario(),
|
|
307
|
+
b = scenario();
|
|
308
|
+
for (const body of [a.raw.schedule, a.raw.attachReceipt.providerSnapshot])
|
|
309
|
+
ownSpecial(body, { retained: 1 });
|
|
310
|
+
for (const body of [b.raw.schedule, b.raw.attachReceipt.providerSnapshot])
|
|
311
|
+
ownSpecial(body, { retained: 2 });
|
|
312
|
+
const x = await project(a.raw),
|
|
313
|
+
y = await project(b.raw);
|
|
314
|
+
expect(x.status).toBe("projected_configure_request");
|
|
315
|
+
expect(y.status).toBe("projected_configure_request");
|
|
316
|
+
expect(x.baselineBinding).not.toBe(y.baselineBinding);
|
|
317
|
+
expect(x.requestIdentity.binding).not.toBe(y.requestIdentity.binding);
|
|
318
|
+
const mismatched = scenario();
|
|
319
|
+
ownSpecial(mismatched.raw.schedule, { retained: true });
|
|
320
|
+
expect(await project(mismatched.raw)).toEqual({
|
|
321
|
+
status: "conflict",
|
|
322
|
+
reason: "acknowledged_baseline_or_defaults_drift",
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
it("does not drop unknown special own keys from strict default mappings to accept them", async () => {
|
|
326
|
+
const f = scenario();
|
|
327
|
+
for (const body of [
|
|
328
|
+
f.raw.schedule.default_settings,
|
|
329
|
+
f.raw.attachReceipt.providerSnapshot.default_settings,
|
|
330
|
+
])
|
|
331
|
+
ownSpecial(body, { unsupported: true });
|
|
332
|
+
expect(await project(f.raw)).toEqual({
|
|
333
|
+
status: "attached_unconfigured",
|
|
334
|
+
scheduleId: "sub_sched_owned",
|
|
335
|
+
reason: "unsupported_schedule_defaults",
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
it("keeps V1 Schedule parsing after its original key validation boundary", () => {
|
|
339
|
+
const f = scenario();
|
|
340
|
+
f.old.configureKey = f.old.attachReceipt.request.key;
|
|
341
|
+
f.old.schedule = null as any;
|
|
342
|
+
expect(legacy(f.old)).toEqual({
|
|
343
|
+
status: "attached_unconfigured",
|
|
344
|
+
scheduleId: "sub_sched_owned",
|
|
345
|
+
reason: "configure_key_must_be_distinct",
|
|
346
|
+
});
|
|
347
|
+
f.old.configureKey = "another:configure-key";
|
|
348
|
+
expect(legacy(f.old)).toEqual({
|
|
349
|
+
status: "attached_unconfigured",
|
|
350
|
+
scheduleId: "sub_sched_owned",
|
|
351
|
+
reason: "incomplete_or_unsupported_schema",
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
it("exports the pure public API without exposing shared internal material helpers", async () => {
|
|
355
|
+
const root = await import("../../index.js");
|
|
356
|
+
expect(root).toHaveProperty(
|
|
357
|
+
"projectRenewalConfigurationV2",
|
|
358
|
+
sdk.projectRenewalConfigurationV2,
|
|
359
|
+
);
|
|
360
|
+
expect(root).not.toHaveProperty("validateReadMaterial");
|
|
361
|
+
expect(root).not.toHaveProperty("configureMaterial");
|
|
362
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
3
|
+
import { timestamp, type ObjectData } from "../renewalAggregate/shared.js";
|
|
4
|
+
import {
|
|
5
|
+
captureAttachmentReceipt,
|
|
6
|
+
validateAttachmentReceiptFields,
|
|
7
|
+
} from "./receipt.js";
|
|
8
|
+
|
|
9
|
+
function ownData(raw: unknown, required: string[], optional: string[] = []) {
|
|
10
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
11
|
+
throw Error("Invalid raw renewal configuration.");
|
|
12
|
+
const keys = Reflect.ownKeys(raw);
|
|
13
|
+
if (
|
|
14
|
+
keys.some(
|
|
15
|
+
(k) => typeof k !== "string" || ![...required, ...optional].includes(k),
|
|
16
|
+
) ||
|
|
17
|
+
required.some((k) => !keys.includes(k))
|
|
18
|
+
)
|
|
19
|
+
throw Error("Invalid raw renewal configuration fields.");
|
|
20
|
+
const fields: ObjectData = {};
|
|
21
|
+
for (const key of keys) {
|
|
22
|
+
const d = Object.getOwnPropertyDescriptor(raw, key);
|
|
23
|
+
if (!d || !d.enumerable || !("value" in d))
|
|
24
|
+
throw Error("Renewal configuration requires own JSON data.");
|
|
25
|
+
fields[key as string] = d.value;
|
|
26
|
+
}
|
|
27
|
+
return fields;
|
|
28
|
+
}
|
|
29
|
+
function closed(raw: unknown) {
|
|
30
|
+
const root = ownData(
|
|
31
|
+
raw,
|
|
32
|
+
[
|
|
33
|
+
"preparation",
|
|
34
|
+
"attachReceipt",
|
|
35
|
+
"freshSubscription",
|
|
36
|
+
"targetPrice",
|
|
37
|
+
"schedule",
|
|
38
|
+
"apiVersion",
|
|
39
|
+
"observedAt",
|
|
40
|
+
"firstDispatchedAt",
|
|
41
|
+
"configureKey",
|
|
42
|
+
],
|
|
43
|
+
["billingCustomer"],
|
|
44
|
+
);
|
|
45
|
+
ownData(
|
|
46
|
+
root.preparation,
|
|
47
|
+
["subscription", "targetPrice", "expected", "key"],
|
|
48
|
+
["billingCustomer"],
|
|
49
|
+
);
|
|
50
|
+
validateAttachmentReceiptFields(root.attachReceipt);
|
|
51
|
+
}
|
|
52
|
+
const preparation = z
|
|
53
|
+
.object({
|
|
54
|
+
subscription: z.unknown(),
|
|
55
|
+
targetPrice: z.unknown(),
|
|
56
|
+
expected: z.unknown(),
|
|
57
|
+
key: z.string(),
|
|
58
|
+
billingCustomer: z.unknown().optional(),
|
|
59
|
+
})
|
|
60
|
+
.strict();
|
|
61
|
+
const input = z
|
|
62
|
+
.object({
|
|
63
|
+
preparation,
|
|
64
|
+
attachReceipt: z.unknown(),
|
|
65
|
+
freshSubscription: z.unknown(),
|
|
66
|
+
targetPrice: z.unknown(),
|
|
67
|
+
billingCustomer: z.unknown().optional(),
|
|
68
|
+
schedule: z.unknown(),
|
|
69
|
+
apiVersion: z.string(),
|
|
70
|
+
observedAt: timestamp,
|
|
71
|
+
firstDispatchedAt: timestamp,
|
|
72
|
+
configureKey: z.string(),
|
|
73
|
+
})
|
|
74
|
+
.strict();
|
|
75
|
+
export function captureConfiguration(raw: unknown) {
|
|
76
|
+
closed(raw);
|
|
77
|
+
const captured = renewalSnapshot(raw);
|
|
78
|
+
closed(captured);
|
|
79
|
+
const args = input.parse(captured);
|
|
80
|
+
return {
|
|
81
|
+
...args,
|
|
82
|
+
attachReceipt: captureAttachmentReceipt(args.attachReceipt),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -5,9 +5,28 @@ import {
|
|
|
5
5
|
type PreparedRenewalPriceReplacement,
|
|
6
6
|
} from "../renewalPriceReplacement.js";
|
|
7
7
|
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
supportedVersion,
|
|
10
|
+
timestamp,
|
|
11
|
+
resourceId,
|
|
12
|
+
equal,
|
|
13
|
+
conflict,
|
|
14
|
+
failure,
|
|
15
|
+
type Failure,
|
|
16
|
+
} from "../renewalAggregate/shared.js";
|
|
17
|
+
import { validateReadMaterial } from "../renewalAggregate/readMaterial.js";
|
|
18
|
+
import {
|
|
19
|
+
configureMaterial,
|
|
20
|
+
validateConfigureStart,
|
|
21
|
+
} from "../renewalAggregate/configureMaterial.js";
|
|
22
|
+
import type { ProjectedRenewalConfiguration } from "../renewalAggregate/configure.js";
|
|
9
23
|
import { idempotencyOptions } from "../../core/idempotency.js";
|
|
10
24
|
import { renewalBindingV2 } from "./binding.js";
|
|
25
|
+
import {
|
|
26
|
+
captureAttachmentReceipt,
|
|
27
|
+
assertAttachmentIdentity,
|
|
28
|
+
} from "./receipt.js";
|
|
29
|
+
import { captureConfiguration } from "./configureInput.js";
|
|
11
30
|
|
|
12
31
|
export type PrepareRenewalPriceReplacementV2Options =
|
|
13
32
|
PrepareRenewalPriceReplacementOptions;
|
|
@@ -109,3 +128,211 @@ export async function prepareRenewalScheduleAttachmentV2(
|
|
|
109
128
|
const binding = await renewalBindingV2("renewal-attach", request);
|
|
110
129
|
return { ...request, requestIdentity: { key, binding } };
|
|
111
130
|
}
|
|
131
|
+
|
|
132
|
+
export type ConstructRenewalScheduleAttachmentReceiptV2Options = {
|
|
133
|
+
attachRequest: {
|
|
134
|
+
preparation: PrepareRenewalPriceReplacementV2Options;
|
|
135
|
+
apiVersion: string;
|
|
136
|
+
attachKey: string;
|
|
137
|
+
firstDispatchedAt: number;
|
|
138
|
+
};
|
|
139
|
+
actualDispatch: Pick<
|
|
140
|
+
PreparedRenewalScheduleAttachmentV2,
|
|
141
|
+
| "scope"
|
|
142
|
+
| "apiVersion"
|
|
143
|
+
| "method"
|
|
144
|
+
| "path"
|
|
145
|
+
| "payload"
|
|
146
|
+
| "key"
|
|
147
|
+
| "firstDispatchedAt"
|
|
148
|
+
>;
|
|
149
|
+
providerSnapshot: Record<string, unknown>;
|
|
150
|
+
};
|
|
151
|
+
export type RenewalScheduleAttachmentReceiptV2 = {
|
|
152
|
+
version: 2;
|
|
153
|
+
stage: "attached_unconfigured";
|
|
154
|
+
scope: "platform";
|
|
155
|
+
logicalBinding: string;
|
|
156
|
+
request: PreparedRenewalScheduleAttachmentV2;
|
|
157
|
+
scheduleId: string;
|
|
158
|
+
subscriptionId: string;
|
|
159
|
+
customerAccountId: string;
|
|
160
|
+
livemode: boolean;
|
|
161
|
+
providerSnapshot: Record<string, unknown>;
|
|
162
|
+
binding: string;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/** Pure unsigned consistency data. Does not authenticate or acknowledge a POST. */
|
|
166
|
+
export async function constructRenewalScheduleAttachmentReceiptV2(
|
|
167
|
+
options: ConstructRenewalScheduleAttachmentReceiptV2Options,
|
|
168
|
+
): Promise<RenewalScheduleAttachmentReceiptV2> {
|
|
169
|
+
const args = captureAttachmentReceipt(options);
|
|
170
|
+
const prepared = await prepareRenewalPriceReplacementV2({
|
|
171
|
+
...args.attachRequest.preparation,
|
|
172
|
+
expected: args.attachRequest.preparation
|
|
173
|
+
.expected as PrepareRenewalPriceReplacementOptions["expected"],
|
|
174
|
+
});
|
|
175
|
+
const request = await prepareRenewalScheduleAttachmentV2({
|
|
176
|
+
prepared,
|
|
177
|
+
apiVersion: args.attachRequest.apiVersion,
|
|
178
|
+
attachKey: args.attachRequest.attachKey,
|
|
179
|
+
firstDispatchedAt: args.attachRequest.firstDispatchedAt,
|
|
180
|
+
});
|
|
181
|
+
const { scope, apiVersion, method, path, payload, key, firstDispatchedAt } =
|
|
182
|
+
request;
|
|
183
|
+
if (
|
|
184
|
+
!equal(args.actualDispatch, {
|
|
185
|
+
scope,
|
|
186
|
+
apiVersion,
|
|
187
|
+
method,
|
|
188
|
+
path,
|
|
189
|
+
payload,
|
|
190
|
+
key,
|
|
191
|
+
firstDispatchedAt,
|
|
192
|
+
})
|
|
193
|
+
)
|
|
194
|
+
throw Error(
|
|
195
|
+
"Renewal attachment dispatch does not match its complete request.",
|
|
196
|
+
);
|
|
197
|
+
const billingCustomer = prepared.providerSnapshot.billingCustomer;
|
|
198
|
+
if (!billingCustomer)
|
|
199
|
+
throw Error("Renewal attachment requires its retained billing Customer.");
|
|
200
|
+
const scheduleId = resourceId("sub_sched").parse(args.providerSnapshot.id);
|
|
201
|
+
assertAttachmentIdentity(args.providerSnapshot, {
|
|
202
|
+
scheduleId,
|
|
203
|
+
subscriptionId: prepared.expected.subscriptionId,
|
|
204
|
+
billingCustomerId: billingCustomer.id,
|
|
205
|
+
customerAccountId: prepared.expected.customerAccountId,
|
|
206
|
+
livemode: prepared.expected.livemode,
|
|
207
|
+
});
|
|
208
|
+
const receipt = {
|
|
209
|
+
version: 2 as const,
|
|
210
|
+
stage: "attached_unconfigured" as const,
|
|
211
|
+
scope: "platform" as const,
|
|
212
|
+
logicalBinding: prepared.requestIdentity.binding,
|
|
213
|
+
request,
|
|
214
|
+
scheduleId,
|
|
215
|
+
subscriptionId: prepared.expected.subscriptionId,
|
|
216
|
+
customerAccountId: prepared.expected.customerAccountId,
|
|
217
|
+
livemode: prepared.expected.livemode,
|
|
218
|
+
providerSnapshot: args.providerSnapshot,
|
|
219
|
+
};
|
|
220
|
+
return {
|
|
221
|
+
...receipt,
|
|
222
|
+
binding: await renewalBindingV2("renewal-attach-receipt", receipt),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export type ProjectRenewalConfigurationV2Options = {
|
|
227
|
+
preparation: PrepareRenewalPriceReplacementV2Options;
|
|
228
|
+
/** Raw attachment inputs, not a stored receipt or caller verification handle. */
|
|
229
|
+
attachReceipt: ConstructRenewalScheduleAttachmentReceiptV2Options;
|
|
230
|
+
freshSubscription: unknown;
|
|
231
|
+
targetPrice: unknown;
|
|
232
|
+
billingCustomer?: unknown;
|
|
233
|
+
schedule: unknown;
|
|
234
|
+
apiVersion: string;
|
|
235
|
+
observedAt: number;
|
|
236
|
+
firstDispatchedAt: number;
|
|
237
|
+
configureKey: string;
|
|
238
|
+
};
|
|
239
|
+
export type ProjectedRenewalConfigurationV2 = Omit<
|
|
240
|
+
ProjectedRenewalConfiguration,
|
|
241
|
+
"version"
|
|
242
|
+
> & { version: 2 };
|
|
243
|
+
export type RenewalConfigurationProjectionFailure = Failure;
|
|
244
|
+
|
|
245
|
+
/** Pure raw-only configuration candidate; provider qualification remains required. */
|
|
246
|
+
export async function projectRenewalConfigurationV2(
|
|
247
|
+
options: ProjectRenewalConfigurationV2Options,
|
|
248
|
+
): Promise<
|
|
249
|
+
ProjectedRenewalConfigurationV2 | RenewalConfigurationProjectionFailure
|
|
250
|
+
> {
|
|
251
|
+
const args = captureConfiguration(options);
|
|
252
|
+
let ownedScheduleId: string | undefined;
|
|
253
|
+
try {
|
|
254
|
+
if (!equal(args.preparation, args.attachReceipt.attachRequest.preparation))
|
|
255
|
+
conflict("invalid_prepared_binding");
|
|
256
|
+
let prepared: PreparedRenewalPriceReplacementV2;
|
|
257
|
+
try {
|
|
258
|
+
prepared = await prepareRenewalPriceReplacementV2({
|
|
259
|
+
...args.preparation,
|
|
260
|
+
expected: args.preparation
|
|
261
|
+
.expected as PrepareRenewalPriceReplacementOptions["expected"],
|
|
262
|
+
});
|
|
263
|
+
} catch {
|
|
264
|
+
conflict("invalid_prepared_binding");
|
|
265
|
+
}
|
|
266
|
+
let receipt: RenewalScheduleAttachmentReceiptV2;
|
|
267
|
+
try {
|
|
268
|
+
receipt = await constructRenewalScheduleAttachmentReceiptV2(
|
|
269
|
+
args.attachReceipt as ConstructRenewalScheduleAttachmentReceiptV2Options,
|
|
270
|
+
);
|
|
271
|
+
} catch {
|
|
272
|
+
conflict("invalid_attach_receipt_binding");
|
|
273
|
+
}
|
|
274
|
+
if (
|
|
275
|
+
receipt.logicalBinding !== prepared.requestIdentity.binding ||
|
|
276
|
+
receipt.request.logicalBinding !== prepared.requestIdentity.binding ||
|
|
277
|
+
receipt.request.apiVersion !== args.apiVersion ||
|
|
278
|
+
receipt.request.payload.from_subscription !==
|
|
279
|
+
prepared.expected.subscriptionId ||
|
|
280
|
+
receipt.subscriptionId !== prepared.expected.subscriptionId ||
|
|
281
|
+
receipt.customerAccountId !== prepared.expected.customerAccountId ||
|
|
282
|
+
receipt.livemode !== prepared.expected.livemode ||
|
|
283
|
+
receipt.request.firstDispatchedAt > args.observedAt
|
|
284
|
+
)
|
|
285
|
+
conflict("invalid_attach_receipt_binding");
|
|
286
|
+
ownedScheduleId = receipt.scheduleId;
|
|
287
|
+
const key = validateConfigureStart(args, receipt);
|
|
288
|
+
const reads = validateReadMaterial(args, prepared, receipt.scheduleId);
|
|
289
|
+
const { schedule, payload } = configureMaterial(
|
|
290
|
+
// Unlike V1's record parser, retain every own key of the captured body.
|
|
291
|
+
z
|
|
292
|
+
.custom<Record<string, unknown>>(
|
|
293
|
+
(value) =>
|
|
294
|
+
value !== null &&
|
|
295
|
+
typeof value === "object" &&
|
|
296
|
+
!Array.isArray(value),
|
|
297
|
+
)
|
|
298
|
+
.parse(args.schedule),
|
|
299
|
+
prepared,
|
|
300
|
+
receipt,
|
|
301
|
+
reads,
|
|
302
|
+
);
|
|
303
|
+
const request = {
|
|
304
|
+
version: 2 as const,
|
|
305
|
+
stage: "configure" as const,
|
|
306
|
+
scope: "platform" as const,
|
|
307
|
+
apiVersion: args.apiVersion,
|
|
308
|
+
logicalBinding: prepared.requestIdentity.binding,
|
|
309
|
+
scheduleId: receipt.scheduleId,
|
|
310
|
+
method: "POST" as const,
|
|
311
|
+
path: `/v1/subscription_schedules/${receipt.scheduleId}`,
|
|
312
|
+
key,
|
|
313
|
+
payload,
|
|
314
|
+
firstDispatchedAt: args.firstDispatchedAt,
|
|
315
|
+
attachReceiptBinding: receipt.binding,
|
|
316
|
+
baselineBinding: await renewalBindingV2(
|
|
317
|
+
"renewal-baseline",
|
|
318
|
+
receipt.providerSnapshot,
|
|
319
|
+
),
|
|
320
|
+
defaultSettingsBinding: await renewalBindingV2(
|
|
321
|
+
"renewal-defaults",
|
|
322
|
+
schedule.default_settings,
|
|
323
|
+
),
|
|
324
|
+
};
|
|
325
|
+
return renewalSnapshot({
|
|
326
|
+
...request,
|
|
327
|
+
status: "projected_configure_request" as const,
|
|
328
|
+
providerQualification: "required" as const,
|
|
329
|
+
profile: reads.profile,
|
|
330
|
+
requestIdentity: {
|
|
331
|
+
key,
|
|
332
|
+
binding: await renewalBindingV2("renewal-configure", request),
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
} catch (error) {
|
|
336
|
+
return failure(error, ownedScheduleId);
|
|
337
|
+
}
|
|
338
|
+
}
|