@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.
Files changed (59) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAggregate/configure.d.ts.map +1 -1
  4. package/dist/client/billing/renewalAggregate/configure.js +4 -56
  5. package/dist/client/billing/renewalAggregate/configure.js.map +1 -1
  6. package/dist/client/billing/renewalAggregate/configureMaterial.d.ts +29 -0
  7. package/dist/client/billing/renewalAggregate/configureMaterial.d.ts.map +1 -0
  8. package/dist/client/billing/renewalAggregate/configureMaterial.js +63 -0
  9. package/dist/client/billing/renewalAggregate/configureMaterial.js.map +1 -0
  10. package/dist/client/billing/renewalAggregate/phases.d.ts +2 -2
  11. package/dist/client/billing/renewalAggregate/phases.d.ts.map +1 -1
  12. package/dist/client/billing/renewalAggregate/phases.js.map +1 -1
  13. package/dist/client/billing/renewalAggregate/preflight.d.ts +1 -118
  14. package/dist/client/billing/renewalAggregate/preflight.d.ts.map +1 -1
  15. package/dist/client/billing/renewalAggregate/preflight.js +3 -118
  16. package/dist/client/billing/renewalAggregate/preflight.js.map +1 -1
  17. package/dist/client/billing/renewalAggregate/readMaterial.d.ts +335 -0
  18. package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -0
  19. package/dist/client/billing/renewalAggregate/readMaterial.js +121 -0
  20. package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -0
  21. package/dist/client/billing/renewalV2/binding.d.ts +1 -1
  22. package/dist/client/billing/renewalV2/binding.d.ts.map +1 -1
  23. package/dist/client/billing/renewalV2/binding.js.map +1 -1
  24. package/dist/client/billing/renewalV2/configureInput.d.ts +45 -0
  25. package/dist/client/billing/renewalV2/configureInput.d.ts.map +1 -0
  26. package/dist/client/billing/renewalV2/configureInput.js +69 -0
  27. package/dist/client/billing/renewalV2/configureInput.js.map +1 -0
  28. package/dist/client/billing/renewalV2/index.d.ts +46 -0
  29. package/dist/client/billing/renewalV2/index.d.ts.map +1 -1
  30. package/dist/client/billing/renewalV2/index.js +134 -1
  31. package/dist/client/billing/renewalV2/index.js.map +1 -1
  32. package/dist/client/billing/renewalV2/receipt.d.ts +37 -0
  33. package/dist/client/billing/renewalV2/receipt.d.ts.map +1 -0
  34. package/dist/client/billing/renewalV2/receipt.js +131 -0
  35. package/dist/client/billing/renewalV2/receipt.js.map +1 -0
  36. package/dist/client/renewal.d.ts +2 -2
  37. package/dist/client/renewal.d.ts.map +1 -1
  38. package/dist/client/renewal.js +1 -1
  39. package/dist/client/renewal.js.map +1 -1
  40. package/dist/client/usage/observationClient.d.ts +2 -2
  41. package/dist/client/usage/observationClient.d.ts.map +1 -1
  42. package/dist/component/payoutRelease/queries.d.ts +2 -2
  43. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  44. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  45. package/docs/renewal-context-reads.md +73 -3
  46. package/docs/renewal-phase-assembly.md +78 -0
  47. package/package.json +1 -1
  48. package/src/client/billing/renewalAggregate/configure.ts +12 -79
  49. package/src/client/billing/renewalAggregate/configureMaterial.ts +122 -0
  50. package/src/client/billing/renewalAggregate/phases.ts +2 -2
  51. package/src/client/billing/renewalAggregate/preflight.ts +6 -161
  52. package/src/client/billing/renewalAggregate/readMaterial.ts +173 -0
  53. package/src/client/billing/renewalV2/binding.ts +7 -1
  54. package/src/client/billing/renewalV2/configure.test.ts +362 -0
  55. package/src/client/billing/renewalV2/configureInput.ts +84 -0
  56. package/src/client/billing/renewalV2/index.ts +228 -1
  57. package/src/client/billing/renewalV2/receipt.test.ts +289 -0
  58. package/src/client/billing/renewalV2/receipt.ts +163 -0
  59. package/src/client/renewal.ts +7 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fantastic.dev/better-stripe",
3
- "version": "0.84.0",
3
+ "version": "0.86.0",
4
4
  "description": "Convex component for Stripe Accounts v2, subscriptions, metered billing and Connect marketplaces, with React workflows.",
5
5
  "keywords": [
6
6
  "stripe",
@@ -1,20 +1,17 @@
1
- import { z } from "zod";
2
1
  import type Stripe from "stripe";
3
- import { idempotencyOptions } from "../../core/idempotency.js";
4
2
  import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
5
3
  import { retainedPreparation, validateReads } from "./preflight.js";
6
4
  import { validateAttachReceipt } from "./receipt.js";
7
- import { assemblePhases } from "./phases.js";
5
+ import {
6
+ configureMaterial,
7
+ validateConfigureStart,
8
+ } from "./configureMaterial.js";
8
9
  import type { RenewalProfile } from "./profile.js";
9
10
  import {
10
11
  binding,
11
- object,
12
12
  configureInput,
13
- equal,
14
- conflict,
15
- unsupported,
16
13
  failure,
17
- referenceId,
14
+ object,
18
15
  type Failure,
19
16
  } from "./shared.js";
20
17
 
@@ -60,78 +57,14 @@ export function projectRenewalConfiguration(
60
57
  args.observedAt,
61
58
  );
62
59
  ownedScheduleId = receipt.scheduleId;
63
- const key = idempotencyOptions(args.configureKey)?.idempotencyKey;
64
- if (!key || key === receipt.request.requestIdentity.key)
65
- unsupported("configure_key_must_be_distinct");
66
- if (
67
- args.firstDispatchedAt > args.observedAt ||
68
- args.firstDispatchedAt < receipt.request.firstDispatchedAt
69
- )
70
- unsupported("invalid_retained_dispatch_time");
71
- const rawSubscription = object(args.freshSubscription);
72
- if (
73
- rawSubscription.schedule == null ||
74
- referenceId(
75
- rawSubscription.schedule,
76
- "sub_sched",
77
- "subscription_schedule",
78
- ) !== receipt.scheduleId
79
- )
80
- conflict("schedule_association_mismatch");
60
+ const key = validateConfigureStart(args, receipt);
81
61
  const reads = validateReads(args, receipt.scheduleId);
82
- const schedule = object(args.schedule);
83
- if (!equal(schedule, receipt.providerSnapshot))
84
- conflict("acknowledged_baseline_or_defaults_drift");
85
- const expected = prepared.expected;
86
- const referenceContext = {
87
- livemode: expected.livemode,
88
- customerAccountId: expected.customerAccountId,
89
- };
90
- if (
91
- schedule.id !== receipt.scheduleId ||
92
- schedule.object !== "subscription_schedule" ||
93
- schedule.customer_account !== expected.customerAccountId ||
94
- schedule.livemode !== expected.livemode ||
95
- referenceId(schedule.customer, "cus", "customer", referenceContext) !==
96
- reads.customer.id ||
97
- referenceId(
98
- schedule.subscription,
99
- "sub",
100
- "subscription",
101
- referenceContext,
102
- ) !== expected.subscriptionId
103
- )
104
- conflict("acknowledged_schedule_identity_mismatch");
105
- if (
106
- schedule.status !== "active" ||
107
- schedule.end_behavior !== "release" ||
108
- schedule.application !== null ||
109
- schedule.canceled_at !== null ||
110
- schedule.completed_at !== null ||
111
- schedule.released_at !== null ||
112
- schedule.released_subscription !== null ||
113
- (schedule.pause_schedules !== undefined &&
114
- (!Array.isArray(schedule.pause_schedules) ||
115
- schedule.pause_schedules.length))
116
- )
117
- unsupported("unsupported_acknowledged_schedule_state");
118
- if (!equal(schedule.billing_mode, reads.subscription.billing_mode))
119
- unsupported("schedule_billing_mode_mismatch");
120
- const current = z
121
- .object({ start_date: z.number(), end_date: z.number() })
122
- .strict()
123
- .parse(schedule.current_phase);
124
- if (
125
- current.start_date !== expected.periodStart ||
126
- current.end_date !== expected.periodEnd
127
- )
128
- conflict("current_phase_period_drift");
129
- const phases = assemblePhases(reads, schedule);
130
- const payload = {
131
- end_behavior: "release" as const,
132
- proration_behavior: "none" as const,
133
- phases,
134
- };
62
+ const { schedule, payload } = configureMaterial(
63
+ object(args.schedule),
64
+ prepared,
65
+ receipt,
66
+ reads,
67
+ );
135
68
  const request = {
136
69
  version: 1 as const,
137
70
  stage: "configure" as const,
@@ -0,0 +1,122 @@
1
+ import { z } from "zod";
2
+ import type Stripe from "stripe";
3
+ import { idempotencyOptions } from "../../core/idempotency.js";
4
+ import { assemblePhases } from "./phases.js";
5
+ import type {
6
+ RenewalPreparationMaterial,
7
+ RenewalReadMaterial,
8
+ } from "./readMaterial.js";
9
+ import {
10
+ object,
11
+ configureInput,
12
+ equal,
13
+ conflict,
14
+ unsupported,
15
+ referenceId,
16
+ type ObjectData,
17
+ } from "./shared.js";
18
+ type Args = Pick<
19
+ z.infer<typeof configureInput>,
20
+ | "configureKey"
21
+ | "firstDispatchedAt"
22
+ | "observedAt"
23
+ | "freshSubscription"
24
+ | "schedule"
25
+ >;
26
+ type Receipt = {
27
+ scheduleId: string;
28
+ providerSnapshot: unknown;
29
+ request: { firstDispatchedAt: number; requestIdentity: { key: string } };
30
+ };
31
+ // Internal only: all callers validate their own receipt before entering these predicates.
32
+ export function validateConfigureStart(args: Args, receipt: Receipt) {
33
+ const key = idempotencyOptions(args.configureKey)?.idempotencyKey;
34
+ if (!key || key === receipt.request.requestIdentity.key)
35
+ unsupported("configure_key_must_be_distinct");
36
+ if (
37
+ args.firstDispatchedAt > args.observedAt ||
38
+ args.firstDispatchedAt < receipt.request.firstDispatchedAt
39
+ )
40
+ unsupported("invalid_retained_dispatch_time");
41
+ const rawSubscription = object(args.freshSubscription);
42
+ if (
43
+ rawSubscription.schedule == null ||
44
+ referenceId(
45
+ rawSubscription.schedule,
46
+ "sub_sched",
47
+ "subscription_schedule",
48
+ ) !== receipt.scheduleId
49
+ )
50
+ conflict("schedule_association_mismatch");
51
+ return key;
52
+ }
53
+ export function configureMaterial(
54
+ schedule: ObjectData,
55
+ prepared: RenewalPreparationMaterial,
56
+ receipt: Receipt,
57
+ reads: RenewalReadMaterial,
58
+ ): {
59
+ schedule: ObjectData;
60
+ payload: {
61
+ end_behavior: "release";
62
+ proration_behavior: "none";
63
+ phases: [
64
+ Stripe.SubscriptionScheduleUpdateParams.Phase,
65
+ Stripe.SubscriptionScheduleUpdateParams.Phase,
66
+ ];
67
+ };
68
+ } {
69
+ if (!equal(schedule, receipt.providerSnapshot))
70
+ conflict("acknowledged_baseline_or_defaults_drift");
71
+ const expected = prepared.expected;
72
+ const referenceContext = {
73
+ livemode: expected.livemode,
74
+ customerAccountId: expected.customerAccountId,
75
+ };
76
+ if (
77
+ schedule.id !== receipt.scheduleId ||
78
+ schedule.object !== "subscription_schedule" ||
79
+ schedule.customer_account !== expected.customerAccountId ||
80
+ schedule.livemode !== expected.livemode ||
81
+ referenceId(schedule.customer, "cus", "customer", referenceContext) !==
82
+ reads.customer.id ||
83
+ referenceId(
84
+ schedule.subscription,
85
+ "sub",
86
+ "subscription",
87
+ referenceContext,
88
+ ) !== expected.subscriptionId
89
+ )
90
+ conflict("acknowledged_schedule_identity_mismatch");
91
+ if (
92
+ schedule.status !== "active" ||
93
+ schedule.end_behavior !== "release" ||
94
+ schedule.application !== null ||
95
+ schedule.canceled_at !== null ||
96
+ schedule.completed_at !== null ||
97
+ schedule.released_at !== null ||
98
+ schedule.released_subscription !== null ||
99
+ (schedule.pause_schedules !== undefined &&
100
+ (!Array.isArray(schedule.pause_schedules) ||
101
+ schedule.pause_schedules.length))
102
+ )
103
+ unsupported("unsupported_acknowledged_schedule_state");
104
+ if (!equal(schedule.billing_mode, reads.subscription.billing_mode))
105
+ unsupported("schedule_billing_mode_mismatch");
106
+ const current = z
107
+ .object({ start_date: z.number(), end_date: z.number() })
108
+ .strict()
109
+ .parse(schedule.current_phase);
110
+ if (
111
+ current.start_date !== expected.periodStart ||
112
+ current.end_date !== expected.periodEnd
113
+ )
114
+ conflict("current_phase_period_drift");
115
+ const phases = assemblePhases(reads, schedule);
116
+ const payload = {
117
+ end_behavior: "release" as const,
118
+ proration_behavior: "none" as const,
119
+ phases,
120
+ };
121
+ return { schedule, payload };
122
+ }
@@ -13,7 +13,7 @@ import {
13
13
  equal,
14
14
  unsupported,
15
15
  } from "./shared.js";
16
- import type { validateReads } from "./preflight.js";
16
+ import type { RenewalReadMaterial } from "./readMaterial.js";
17
17
  import type { OriginalRedemptions } from "./sourceDiscounts.js";
18
18
  import { preserveTaxLevels } from "./taxRetention.js";
19
19
 
@@ -85,7 +85,7 @@ function metadataMatches(
85
85
  }
86
86
 
87
87
  export function assemblePhases(
88
- reads: ReturnType<typeof validateReads>,
88
+ reads: RenewalReadMaterial,
89
89
  schedule: Record<string, unknown>,
90
90
  ): [
91
91
  Stripe.SubscriptionScheduleUpdateParams.Phase,
@@ -1,28 +1,8 @@
1
1
  import { z } from "zod";
2
2
  import { prepareRenewalPriceReplacement } from "../renewalPriceReplacement.js";
3
- import {
4
- renewalSubscription,
5
- renewalPrice,
6
- renewalBillingCustomer,
7
- } from "../renewalReplacementSchema.js";
8
3
  import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
9
- import {
10
- object,
11
- readInput,
12
- referenceId,
13
- equal,
14
- conflict,
15
- unsupported,
16
- failure,
17
- supportedVersion,
18
- timestamp,
19
- resourceId,
20
- productId,
21
- } from "./shared.js";
22
- import { profileOf } from "./profile.js";
23
- import { qualifySourceDiscounts } from "./sourceDiscounts.js";
24
- import { qualifyOriginalSettings } from "./sourceSettings.js";
25
-
4
+ import { object, readInput, equal, conflict, failure } from "./shared.js";
5
+ import { validateReadMaterial } from "./readMaterial.js";
26
6
  export function retainedPreparation(value: unknown) {
27
7
  try {
28
8
  const candidate = object(value);
@@ -46,150 +26,15 @@ export function retainedPreparation(value: unknown) {
46
26
  }
47
27
  }
48
28
 
49
- const attachedReference = z.union([
50
- resourceId("sub_sched"),
51
- z
52
- .object({
53
- id: resourceId("sub_sched"),
54
- object: z.literal("subscription_schedule"),
55
- deleted: z.literal(false).optional(),
56
- customer_account: resourceId("acct").optional(),
57
- livemode: z.boolean().optional(),
58
- })
59
- .passthrough(),
60
- ]);
61
-
62
29
  export function validateReads(
63
30
  args: z.infer<typeof readInput>,
64
31
  scheduleId?: string,
65
32
  ) {
66
- const prepared = retainedPreparation(args.prepared);
67
- supportedVersion(args.apiVersion);
68
- const subscription = renewalSubscription
69
- .extend({ schedule: scheduleId ? attachedReference : z.null() })
70
- .safeParse(args.freshSubscription);
71
- if (!subscription.success)
72
- unsupported("unsupported_original_subscription_state");
73
- const source = subscription.data;
74
- if (scheduleId) {
75
- if (
76
- referenceId(source.schedule, "sub_sched", "subscription_schedule") !==
77
- scheduleId
78
- )
79
- conflict("schedule_association_mismatch");
80
- if (
81
- typeof source.schedule === "object" &&
82
- source.schedule !== null &&
83
- ((source.schedule.customer_account !== undefined &&
84
- source.schedule.customer_account !==
85
- prepared.expected.customerAccountId) ||
86
- (source.schedule.livemode !== undefined &&
87
- source.schedule.livemode !== prepared.expected.livemode))
88
- )
89
- conflict("schedule_association_mismatch");
90
- }
91
- const { schedule: originalSchedule, ...original } =
92
- prepared.providerSnapshot.subscription;
93
- const { schedule: freshSchedule, ...fresh } = source;
94
- if (scheduleId ? originalSchedule !== null : freshSchedule !== null)
95
- conflict("schedule_association_mismatch");
96
- if (!equal(original, fresh)) conflict("original_source_drift");
97
- const target = renewalPrice.safeParse(args.targetPrice);
98
- if (!target.success || !target.data.active)
99
- unsupported("unsupported_target_price");
100
- if (!equal(target.data, prepared.providerSnapshot.targetPrice))
101
- conflict("target_price_drift");
102
- if (
103
- prepared.providerSnapshot.billingCustomer === undefined ||
104
- args.billingCustomer === undefined
105
- )
106
- unsupported("missing_retained_billing_customer");
107
- const customer = renewalBillingCustomer.safeParse(args.billingCustomer);
108
- if (!customer.success) unsupported("unsupported_billing_customer");
109
- if (!equal(customer.data, prepared.providerSnapshot.billingCustomer))
110
- conflict("billing_customer_drift");
111
- const referenceContext = {
112
- customerAccountId: prepared.expected.customerAccountId,
113
- livemode: prepared.expected.livemode,
114
- };
115
- if (
116
- referenceId(source.customer, "cus", "customer", referenceContext) !==
117
- customer.data.id ||
118
- customer.data.customer_account !== prepared.expected.customerAccountId ||
119
- customer.data.livemode !== prepared.expected.livemode
120
- )
121
- conflict("billing_customer_identity_mismatch");
122
- const item = source.items.data[0];
123
- if (
124
- productId(item.price.product, referenceContext) !==
125
- prepared.expected.currentProductId ||
126
- productId(target.data.product, referenceContext) !==
127
- prepared.expected.targetProductId
128
- )
129
- unsupported("product_identity_mismatch");
130
- if (
131
- args.observedAt < prepared.expected.periodStart ||
132
- args.observedAt >= prepared.expected.periodEnd
133
- )
134
- unsupported("outside_original_paid_period");
135
- for (const field of [
136
- "billing_thresholds",
137
- "managed_payments",
138
- "next_pending_invoice_item_invoice",
139
- "pending_invoice_item_interval",
140
- "pending_setup_intent",
141
- "canceled_at",
142
- "ended_at",
143
- "trial_settings",
144
- ])
145
- if (source[field] !== null) unsupported(`unsupported_original_${field}`);
146
- if (item.billing_thresholds !== null)
147
- unsupported("unsupported_item_billing_thresholds");
148
- if (item.current_trial !== null)
149
- unsupported("unsupported_or_missing_item_current_trial");
150
- if (
151
- !Array.isArray(source.billing_schedules) ||
152
- source.billing_schedules.length
153
- )
154
- unsupported("unsupported_original_billing_schedules");
155
- if (
156
- source.trial_end !== null &&
157
- (!timestamp.safeParse(source.trial_end).success ||
158
- Number(source.trial_end) > prepared.expected.periodStart)
159
- )
160
- unsupported("unsupported_original_trial");
161
- if (source.status_details != null || source.presentment_details != null)
162
- unsupported("unsupported_special_billing_state");
163
- const originalSettings = qualifyOriginalSettings(source, item, customer.data);
164
- const profile = profileOf(source, item.price, target.data, args.apiVersion);
165
- const anchor = timestamp.parse(source.billing_cycle_anchor);
166
- if (anchor > prepared.expected.periodStart)
167
- unsupported("unqualified_anchor_after_paid_period_start");
168
- const discounts = qualifySourceDiscounts(
169
- source.discounts,
170
- "subscription",
171
- prepared.expected,
172
- customer.data.id,
173
- args.observedAt,
174
- );
175
- const itemDiscounts = qualifySourceDiscounts(
176
- item.discounts,
177
- "item",
178
- prepared.expected,
179
- customer.data.id,
180
- args.observedAt,
33
+ return validateReadMaterial(
34
+ args,
35
+ retainedPreparation(args.prepared),
36
+ scheduleId,
181
37
  );
182
- return {
183
- prepared,
184
- subscription: source,
185
- item,
186
- customer: customer.data,
187
- targetPrice: target.data,
188
- profile,
189
- discounts,
190
- itemDiscounts,
191
- originalSettings,
192
- };
193
38
  }
194
39
 
195
40
  export function preflightRenewalReplacement(input: unknown) {
@@ -0,0 +1,173 @@
1
+ import { z } from "zod";
2
+ import type { PreparedRenewalPriceReplacement } from "../renewalPriceReplacement.js";
3
+ import {
4
+ renewalSubscription,
5
+ renewalPrice,
6
+ renewalBillingCustomer,
7
+ } from "../renewalReplacementSchema.js";
8
+ import {
9
+ readInput,
10
+ referenceId,
11
+ equal,
12
+ conflict,
13
+ unsupported,
14
+ supportedVersion,
15
+ timestamp,
16
+ resourceId,
17
+ productId,
18
+ } from "./shared.js";
19
+ import { profileOf } from "./profile.js";
20
+ import { qualifySourceDiscounts } from "./sourceDiscounts.js";
21
+ import { qualifyOriginalSettings } from "./sourceSettings.js";
22
+ // Internal semantic material only. Public wrappers must rebuild their own preparation.
23
+ export type RenewalPreparationMaterial = Pick<
24
+ PreparedRenewalPriceReplacement,
25
+ "expected" | "quantity" | "providerSnapshot"
26
+ >;
27
+ const attachedReference = z.union([
28
+ resourceId("sub_sched"),
29
+ z
30
+ .object({
31
+ id: resourceId("sub_sched"),
32
+ object: z.literal("subscription_schedule"),
33
+ deleted: z.literal(false).optional(),
34
+ customer_account: resourceId("acct").optional(),
35
+ livemode: z.boolean().optional(),
36
+ })
37
+ .passthrough(),
38
+ ]);
39
+
40
+ export function validateReadMaterial(
41
+ args: Omit<z.infer<typeof readInput>, "prepared">,
42
+ prepared: RenewalPreparationMaterial,
43
+ scheduleId?: string,
44
+ ) {
45
+ supportedVersion(args.apiVersion);
46
+ const subscription = renewalSubscription
47
+ .extend({ schedule: scheduleId ? attachedReference : z.null() })
48
+ .safeParse(args.freshSubscription);
49
+ if (!subscription.success)
50
+ unsupported("unsupported_original_subscription_state");
51
+ const source = subscription.data;
52
+ if (scheduleId) {
53
+ if (
54
+ referenceId(source.schedule, "sub_sched", "subscription_schedule") !==
55
+ scheduleId
56
+ )
57
+ conflict("schedule_association_mismatch");
58
+ if (
59
+ typeof source.schedule === "object" &&
60
+ source.schedule !== null &&
61
+ ((source.schedule.customer_account !== undefined &&
62
+ source.schedule.customer_account !==
63
+ prepared.expected.customerAccountId) ||
64
+ (source.schedule.livemode !== undefined &&
65
+ source.schedule.livemode !== prepared.expected.livemode))
66
+ )
67
+ conflict("schedule_association_mismatch");
68
+ }
69
+ const { schedule: originalSchedule, ...original } =
70
+ prepared.providerSnapshot.subscription;
71
+ const { schedule: freshSchedule, ...fresh } = source;
72
+ if (scheduleId ? originalSchedule !== null : freshSchedule !== null)
73
+ conflict("schedule_association_mismatch");
74
+ if (!equal(original, fresh)) conflict("original_source_drift");
75
+ const target = renewalPrice.safeParse(args.targetPrice);
76
+ if (!target.success || !target.data.active)
77
+ unsupported("unsupported_target_price");
78
+ if (!equal(target.data, prepared.providerSnapshot.targetPrice))
79
+ conflict("target_price_drift");
80
+ if (
81
+ prepared.providerSnapshot.billingCustomer === undefined ||
82
+ args.billingCustomer === undefined
83
+ )
84
+ unsupported("missing_retained_billing_customer");
85
+ const customer = renewalBillingCustomer.safeParse(args.billingCustomer);
86
+ if (!customer.success) unsupported("unsupported_billing_customer");
87
+ if (!equal(customer.data, prepared.providerSnapshot.billingCustomer))
88
+ conflict("billing_customer_drift");
89
+ const referenceContext = {
90
+ customerAccountId: prepared.expected.customerAccountId,
91
+ livemode: prepared.expected.livemode,
92
+ };
93
+ if (
94
+ referenceId(source.customer, "cus", "customer", referenceContext) !==
95
+ customer.data.id ||
96
+ customer.data.customer_account !== prepared.expected.customerAccountId ||
97
+ customer.data.livemode !== prepared.expected.livemode
98
+ )
99
+ conflict("billing_customer_identity_mismatch");
100
+ const item = source.items.data[0];
101
+ if (
102
+ productId(item.price.product, referenceContext) !==
103
+ prepared.expected.currentProductId ||
104
+ productId(target.data.product, referenceContext) !==
105
+ prepared.expected.targetProductId
106
+ )
107
+ unsupported("product_identity_mismatch");
108
+ if (
109
+ args.observedAt < prepared.expected.periodStart ||
110
+ args.observedAt >= prepared.expected.periodEnd
111
+ )
112
+ unsupported("outside_original_paid_period");
113
+ for (const field of [
114
+ "billing_thresholds",
115
+ "managed_payments",
116
+ "next_pending_invoice_item_invoice",
117
+ "pending_invoice_item_interval",
118
+ "pending_setup_intent",
119
+ "canceled_at",
120
+ "ended_at",
121
+ "trial_settings",
122
+ ])
123
+ if (source[field] !== null) unsupported(`unsupported_original_${field}`);
124
+ if (item.billing_thresholds !== null)
125
+ unsupported("unsupported_item_billing_thresholds");
126
+ if (item.current_trial !== null)
127
+ unsupported("unsupported_or_missing_item_current_trial");
128
+ if (
129
+ !Array.isArray(source.billing_schedules) ||
130
+ source.billing_schedules.length
131
+ )
132
+ unsupported("unsupported_original_billing_schedules");
133
+ if (
134
+ source.trial_end !== null &&
135
+ (!timestamp.safeParse(source.trial_end).success ||
136
+ Number(source.trial_end) > prepared.expected.periodStart)
137
+ )
138
+ unsupported("unsupported_original_trial");
139
+ if (source.status_details != null || source.presentment_details != null)
140
+ unsupported("unsupported_special_billing_state");
141
+ const originalSettings = qualifyOriginalSettings(source, item, customer.data);
142
+ const profile = profileOf(source, item.price, target.data, args.apiVersion);
143
+ const anchor = timestamp.parse(source.billing_cycle_anchor);
144
+ if (anchor > prepared.expected.periodStart)
145
+ unsupported("unqualified_anchor_after_paid_period_start");
146
+ const discounts = qualifySourceDiscounts(
147
+ source.discounts,
148
+ "subscription",
149
+ prepared.expected,
150
+ customer.data.id,
151
+ args.observedAt,
152
+ );
153
+ const itemDiscounts = qualifySourceDiscounts(
154
+ item.discounts,
155
+ "item",
156
+ prepared.expected,
157
+ customer.data.id,
158
+ args.observedAt,
159
+ );
160
+ return {
161
+ prepared,
162
+ subscription: source,
163
+ item,
164
+ customer: customer.data,
165
+ targetPrice: target.data,
166
+ profile,
167
+ discounts,
168
+ itemDiscounts,
169
+ originalSettings,
170
+ };
171
+ }
172
+
173
+ export type RenewalReadMaterial = ReturnType<typeof validateReadMaterial>;
@@ -2,7 +2,13 @@ import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
2
2
 
3
3
  /** Internal fixed-family encoding. This is consistency, not authorization. */
4
4
  export async function renewalBindingV2(
5
- family: "renewal" | "renewal-attach",
5
+ family:
6
+ | "renewal"
7
+ | "renewal-attach"
8
+ | "renewal-attach-receipt"
9
+ | "renewal-baseline"
10
+ | "renewal-defaults"
11
+ | "renewal-configure",
6
12
  preimage: unknown,
7
13
  ): Promise<string> {
8
14
  // Finish all input capture and canonical bytes before the first await.