@fantastic.dev/better-stripe 0.85.0 → 0.87.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 +46 -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/configureReceipt.d.ts +19 -0
- package/dist/client/billing/renewalV2/configureReceipt.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/configureReceipt.js +91 -0
- package/dist/client/billing/renewalV2/configureReceipt.js.map +1 -0
- package/dist/client/billing/renewalV2/index.d.ts +55 -0
- package/dist/client/billing/renewalV2/index.d.ts.map +1 -1
- package/dist/client/billing/renewalV2/index.js +145 -1
- package/dist/client/billing/renewalV2/index.js.map +1 -1
- package/dist/client/billing/renewalV2/receipt.d.ts +1 -0
- package/dist/client/billing/renewalV2/receipt.d.ts.map +1 -1
- package/dist/client/billing/renewalV2/receipt.js +3 -3
- package/dist/client/billing/renewalV2/receipt.js.map +1 -1
- 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/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/docs/renewal-context-reads.md +10 -2
- package/docs/renewal-phase-assembly.md +141 -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 +10 -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/configureReceipt.test.ts +558 -0
- package/src/client/billing/renewalV2/configureReceipt.ts +96 -0
- package/src/client/billing/renewalV2/index.ts +241 -0
- package/src/client/billing/renewalV2/receipt.ts +3 -3
- package/src/client/renewal.ts +7 -0
|
@@ -10,13 +10,24 @@ import {
|
|
|
10
10
|
timestamp,
|
|
11
11
|
resourceId,
|
|
12
12
|
equal,
|
|
13
|
+
conflict,
|
|
14
|
+
failure,
|
|
15
|
+
type Failure,
|
|
13
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";
|
|
14
23
|
import { idempotencyOptions } from "../../core/idempotency.js";
|
|
15
24
|
import { renewalBindingV2 } from "./binding.js";
|
|
16
25
|
import {
|
|
17
26
|
captureAttachmentReceipt,
|
|
18
27
|
assertAttachmentIdentity,
|
|
19
28
|
} from "./receipt.js";
|
|
29
|
+
import { captureConfiguration } from "./configureInput.js";
|
|
30
|
+
import { captureConfigurationReceipt } from "./configureReceipt.js";
|
|
20
31
|
|
|
21
32
|
export type PrepareRenewalPriceReplacementV2Options =
|
|
22
33
|
PrepareRenewalPriceReplacementOptions;
|
|
@@ -212,3 +223,233 @@ export async function constructRenewalScheduleAttachmentReceiptV2(
|
|
|
212
223
|
binding: await renewalBindingV2("renewal-attach-receipt", receipt),
|
|
213
224
|
};
|
|
214
225
|
}
|
|
226
|
+
|
|
227
|
+
export type ProjectRenewalConfigurationV2Options = {
|
|
228
|
+
preparation: PrepareRenewalPriceReplacementV2Options;
|
|
229
|
+
/** Raw attachment inputs, not a stored receipt or caller verification handle. */
|
|
230
|
+
attachReceipt: ConstructRenewalScheduleAttachmentReceiptV2Options;
|
|
231
|
+
freshSubscription: unknown;
|
|
232
|
+
targetPrice: unknown;
|
|
233
|
+
billingCustomer?: unknown;
|
|
234
|
+
schedule: unknown;
|
|
235
|
+
apiVersion: string;
|
|
236
|
+
observedAt: number;
|
|
237
|
+
firstDispatchedAt: number;
|
|
238
|
+
configureKey: string;
|
|
239
|
+
};
|
|
240
|
+
export type ProjectedRenewalConfigurationV2 = Omit<
|
|
241
|
+
ProjectedRenewalConfiguration,
|
|
242
|
+
"version"
|
|
243
|
+
> & { version: 2 };
|
|
244
|
+
export type RenewalConfigurationProjectionFailure = Failure;
|
|
245
|
+
|
|
246
|
+
/** Pure raw-only configuration candidate; provider qualification remains required. */
|
|
247
|
+
export async function projectRenewalConfigurationV2(
|
|
248
|
+
options: ProjectRenewalConfigurationV2Options,
|
|
249
|
+
): Promise<
|
|
250
|
+
ProjectedRenewalConfigurationV2 | RenewalConfigurationProjectionFailure
|
|
251
|
+
> {
|
|
252
|
+
const args = captureConfiguration(options);
|
|
253
|
+
let ownedScheduleId: string | undefined;
|
|
254
|
+
try {
|
|
255
|
+
if (!equal(args.preparation, args.attachReceipt.attachRequest.preparation))
|
|
256
|
+
conflict("invalid_prepared_binding");
|
|
257
|
+
let prepared: PreparedRenewalPriceReplacementV2;
|
|
258
|
+
try {
|
|
259
|
+
prepared = await prepareRenewalPriceReplacementV2({
|
|
260
|
+
...args.preparation,
|
|
261
|
+
expected: args.preparation
|
|
262
|
+
.expected as PrepareRenewalPriceReplacementOptions["expected"],
|
|
263
|
+
});
|
|
264
|
+
} catch {
|
|
265
|
+
conflict("invalid_prepared_binding");
|
|
266
|
+
}
|
|
267
|
+
let receipt: RenewalScheduleAttachmentReceiptV2;
|
|
268
|
+
try {
|
|
269
|
+
receipt = await constructRenewalScheduleAttachmentReceiptV2(
|
|
270
|
+
args.attachReceipt as ConstructRenewalScheduleAttachmentReceiptV2Options,
|
|
271
|
+
);
|
|
272
|
+
} catch {
|
|
273
|
+
conflict("invalid_attach_receipt_binding");
|
|
274
|
+
}
|
|
275
|
+
if (
|
|
276
|
+
receipt.logicalBinding !== prepared.requestIdentity.binding ||
|
|
277
|
+
receipt.request.logicalBinding !== prepared.requestIdentity.binding ||
|
|
278
|
+
receipt.request.apiVersion !== args.apiVersion ||
|
|
279
|
+
receipt.request.payload.from_subscription !==
|
|
280
|
+
prepared.expected.subscriptionId ||
|
|
281
|
+
receipt.subscriptionId !== prepared.expected.subscriptionId ||
|
|
282
|
+
receipt.customerAccountId !== prepared.expected.customerAccountId ||
|
|
283
|
+
receipt.livemode !== prepared.expected.livemode ||
|
|
284
|
+
receipt.request.firstDispatchedAt > args.observedAt
|
|
285
|
+
)
|
|
286
|
+
conflict("invalid_attach_receipt_binding");
|
|
287
|
+
ownedScheduleId = receipt.scheduleId;
|
|
288
|
+
const key = validateConfigureStart(args, receipt);
|
|
289
|
+
const reads = validateReadMaterial(args, prepared, receipt.scheduleId);
|
|
290
|
+
const { schedule, payload } = configureMaterial(
|
|
291
|
+
// Unlike V1's record parser, retain every own key of the captured body.
|
|
292
|
+
z
|
|
293
|
+
.custom<Record<string, unknown>>(
|
|
294
|
+
(value) =>
|
|
295
|
+
value !== null &&
|
|
296
|
+
typeof value === "object" &&
|
|
297
|
+
!Array.isArray(value),
|
|
298
|
+
)
|
|
299
|
+
.parse(args.schedule),
|
|
300
|
+
prepared,
|
|
301
|
+
receipt,
|
|
302
|
+
reads,
|
|
303
|
+
);
|
|
304
|
+
const request = {
|
|
305
|
+
version: 2 as const,
|
|
306
|
+
stage: "configure" as const,
|
|
307
|
+
scope: "platform" as const,
|
|
308
|
+
apiVersion: args.apiVersion,
|
|
309
|
+
logicalBinding: prepared.requestIdentity.binding,
|
|
310
|
+
scheduleId: receipt.scheduleId,
|
|
311
|
+
method: "POST" as const,
|
|
312
|
+
path: `/v1/subscription_schedules/${receipt.scheduleId}`,
|
|
313
|
+
key,
|
|
314
|
+
payload,
|
|
315
|
+
firstDispatchedAt: args.firstDispatchedAt,
|
|
316
|
+
attachReceiptBinding: receipt.binding,
|
|
317
|
+
baselineBinding: await renewalBindingV2(
|
|
318
|
+
"renewal-baseline",
|
|
319
|
+
receipt.providerSnapshot,
|
|
320
|
+
),
|
|
321
|
+
defaultSettingsBinding: await renewalBindingV2(
|
|
322
|
+
"renewal-defaults",
|
|
323
|
+
schedule.default_settings,
|
|
324
|
+
),
|
|
325
|
+
};
|
|
326
|
+
return renewalSnapshot({
|
|
327
|
+
...request,
|
|
328
|
+
status: "projected_configure_request" as const,
|
|
329
|
+
providerQualification: "required" as const,
|
|
330
|
+
profile: reads.profile,
|
|
331
|
+
requestIdentity: {
|
|
332
|
+
key,
|
|
333
|
+
binding: await renewalBindingV2("renewal-configure", request),
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
} catch (error) {
|
|
337
|
+
return failure(error, ownedScheduleId);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export type ConstructRenewalConfigurationReceiptV2Options = {
|
|
342
|
+
configuration: ProjectRenewalConfigurationV2Options;
|
|
343
|
+
actualDispatch: Pick<
|
|
344
|
+
ProjectedRenewalConfigurationV2,
|
|
345
|
+
| "scope"
|
|
346
|
+
| "apiVersion"
|
|
347
|
+
| "method"
|
|
348
|
+
| "path"
|
|
349
|
+
| "payload"
|
|
350
|
+
| "key"
|
|
351
|
+
| "firstDispatchedAt"
|
|
352
|
+
>;
|
|
353
|
+
providerSnapshot: Record<string, unknown>;
|
|
354
|
+
transport: { status: number; requestId?: string };
|
|
355
|
+
};
|
|
356
|
+
export type RenewalConfigurationReceiptV2 = {
|
|
357
|
+
version: 2;
|
|
358
|
+
stage: "configure_acknowledged";
|
|
359
|
+
scope: "platform";
|
|
360
|
+
verification: "owned_response_only";
|
|
361
|
+
apiVersion: string;
|
|
362
|
+
logicalBinding: string;
|
|
363
|
+
request: ProjectedRenewalConfigurationV2;
|
|
364
|
+
attachReceiptBinding: string;
|
|
365
|
+
scheduleId: string;
|
|
366
|
+
subscriptionId: string;
|
|
367
|
+
customerAccountId: string;
|
|
368
|
+
billingCustomerId: string;
|
|
369
|
+
livemode: boolean;
|
|
370
|
+
providerSnapshot: Record<string, unknown>;
|
|
371
|
+
transport: { status: number; requestId?: string };
|
|
372
|
+
providerSnapshotBinding: string;
|
|
373
|
+
defaultSettingsBinding?: string;
|
|
374
|
+
binding: string;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/** Unsigned operation consistency only. Does not authenticate an acknowledgement. */
|
|
378
|
+
export async function constructRenewalConfigurationReceiptV2(
|
|
379
|
+
options: ConstructRenewalConfigurationReceiptV2Options,
|
|
380
|
+
): Promise<RenewalConfigurationReceiptV2> {
|
|
381
|
+
try {
|
|
382
|
+
const args = captureConfigurationReceipt(options);
|
|
383
|
+
const configuration =
|
|
384
|
+
args.configuration as ProjectRenewalConfigurationV2Options;
|
|
385
|
+
const request = await projectRenewalConfigurationV2(configuration);
|
|
386
|
+
if (request.status !== "projected_configure_request") throw Error();
|
|
387
|
+
const { scope, apiVersion, method, path, payload, key, firstDispatchedAt } =
|
|
388
|
+
request;
|
|
389
|
+
if (
|
|
390
|
+
!equal(args.actualDispatch, {
|
|
391
|
+
scope,
|
|
392
|
+
apiVersion,
|
|
393
|
+
method,
|
|
394
|
+
path,
|
|
395
|
+
payload,
|
|
396
|
+
key,
|
|
397
|
+
firstDispatchedAt,
|
|
398
|
+
})
|
|
399
|
+
)
|
|
400
|
+
throw Error();
|
|
401
|
+
// These raw selectors have just been revalidated through complete C1-C6.
|
|
402
|
+
const expected = configuration.preparation.expected;
|
|
403
|
+
const billingCustomerId = resourceId("cus").parse(
|
|
404
|
+
(configuration.billingCustomer as Record<string, unknown>).id,
|
|
405
|
+
);
|
|
406
|
+
assertAttachmentIdentity(args.providerSnapshot, {
|
|
407
|
+
scheduleId: request.scheduleId,
|
|
408
|
+
subscriptionId: expected.subscriptionId,
|
|
409
|
+
customerAccountId: expected.customerAccountId,
|
|
410
|
+
billingCustomerId,
|
|
411
|
+
livemode: expected.livemode,
|
|
412
|
+
});
|
|
413
|
+
const providerSnapshotBinding = await renewalBindingV2(
|
|
414
|
+
"renewal-configured-baseline",
|
|
415
|
+
args.providerSnapshot,
|
|
416
|
+
);
|
|
417
|
+
const defaultsPresent = Object.prototype.hasOwnProperty.call(
|
|
418
|
+
args.providerSnapshot,
|
|
419
|
+
"default_settings",
|
|
420
|
+
);
|
|
421
|
+
const defaults = defaultsPresent
|
|
422
|
+
? {
|
|
423
|
+
defaultSettingsBinding: await renewalBindingV2(
|
|
424
|
+
"renewal-configured-defaults",
|
|
425
|
+
args.providerSnapshot.default_settings,
|
|
426
|
+
),
|
|
427
|
+
}
|
|
428
|
+
: {};
|
|
429
|
+
const receipt = {
|
|
430
|
+
version: 2 as const,
|
|
431
|
+
stage: "configure_acknowledged" as const,
|
|
432
|
+
scope: "platform" as const,
|
|
433
|
+
verification: "owned_response_only" as const,
|
|
434
|
+
apiVersion: request.apiVersion,
|
|
435
|
+
logicalBinding: request.logicalBinding,
|
|
436
|
+
request,
|
|
437
|
+
attachReceiptBinding: request.attachReceiptBinding,
|
|
438
|
+
scheduleId: request.scheduleId,
|
|
439
|
+
subscriptionId: expected.subscriptionId,
|
|
440
|
+
customerAccountId: expected.customerAccountId,
|
|
441
|
+
billingCustomerId,
|
|
442
|
+
livemode: expected.livemode,
|
|
443
|
+
providerSnapshot: args.providerSnapshot,
|
|
444
|
+
transport: args.transport,
|
|
445
|
+
providerSnapshotBinding,
|
|
446
|
+
...defaults,
|
|
447
|
+
};
|
|
448
|
+
return renewalSnapshot({
|
|
449
|
+
...receipt,
|
|
450
|
+
binding: await renewalBindingV2("renewal-configure-receipt", receipt),
|
|
451
|
+
});
|
|
452
|
+
} catch {
|
|
453
|
+
throw Error("Invalid renewal configuration receipt input.");
|
|
454
|
+
}
|
|
455
|
+
}
|
|
@@ -66,7 +66,7 @@ function ownData(raw: unknown, required: string[], optional: string[] = []) {
|
|
|
66
66
|
}
|
|
67
67
|
return result;
|
|
68
68
|
}
|
|
69
|
-
function
|
|
69
|
+
export function validateAttachmentReceiptFields(raw: unknown) {
|
|
70
70
|
const root = ownData(raw, [
|
|
71
71
|
"attachRequest",
|
|
72
72
|
"actualDispatch",
|
|
@@ -95,10 +95,10 @@ function closed(raw: unknown) {
|
|
|
95
95
|
ownData(wire.payload, ["from_subscription"]);
|
|
96
96
|
}
|
|
97
97
|
export function captureAttachmentReceipt(raw: unknown) {
|
|
98
|
-
|
|
98
|
+
validateAttachmentReceiptFields(raw);
|
|
99
99
|
const captured = renewalSnapshot(raw);
|
|
100
100
|
// Recheck the detached object: hostile reflection can mutate the live source.
|
|
101
|
-
|
|
101
|
+
validateAttachmentReceiptFields(captured);
|
|
102
102
|
return input.parse(captured);
|
|
103
103
|
}
|
|
104
104
|
|
package/src/client/renewal.ts
CHANGED
|
@@ -15,6 +15,8 @@ export {
|
|
|
15
15
|
prepareRenewalPriceReplacementV2,
|
|
16
16
|
prepareRenewalScheduleAttachmentV2,
|
|
17
17
|
constructRenewalScheduleAttachmentReceiptV2,
|
|
18
|
+
projectRenewalConfigurationV2,
|
|
19
|
+
constructRenewalConfigurationReceiptV2,
|
|
18
20
|
} from "./billing/renewalV2/index.js";
|
|
19
21
|
export type {
|
|
20
22
|
PrepareRenewalPriceReplacementV2Options,
|
|
@@ -23,4 +25,9 @@ export type {
|
|
|
23
25
|
PreparedRenewalScheduleAttachmentV2,
|
|
24
26
|
ConstructRenewalScheduleAttachmentReceiptV2Options,
|
|
25
27
|
RenewalScheduleAttachmentReceiptV2,
|
|
28
|
+
ProjectRenewalConfigurationV2Options,
|
|
29
|
+
ProjectedRenewalConfigurationV2,
|
|
30
|
+
RenewalConfigurationProjectionFailure,
|
|
31
|
+
ConstructRenewalConfigurationReceiptV2Options,
|
|
32
|
+
RenewalConfigurationReceiptV2,
|
|
26
33
|
} from "./billing/renewalV2/index.js";
|