@fantastic.dev/better-stripe 0.80.0 → 0.82.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 +35 -0
- package/dist/client/billing/renewalAggregate/configure.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/configure.js +96 -0
- package/dist/client/billing/renewalAggregate/configure.js.map +1 -0
- package/dist/client/billing/renewalAggregate/index.d.ts +3 -0
- package/dist/client/billing/renewalAggregate/index.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/index.js +3 -0
- package/dist/client/billing/renewalAggregate/index.js.map +1 -0
- package/dist/client/billing/renewalAggregate/phaseContext.d.ts +176 -0
- package/dist/client/billing/renewalAggregate/phaseContext.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/phaseContext.js +112 -0
- package/dist/client/billing/renewalAggregate/phaseContext.js.map +1 -0
- package/dist/client/billing/renewalAggregate/phases.d.ts +7 -0
- package/dist/client/billing/renewalAggregate/phases.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/phases.js +192 -0
- package/dist/client/billing/renewalAggregate/phases.js.map +1 -0
- package/dist/client/billing/renewalAggregate/preflight.d.ts +594 -0
- package/dist/client/billing/renewalAggregate/preflight.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/preflight.js +164 -0
- package/dist/client/billing/renewalAggregate/preflight.js.map +1 -0
- package/dist/client/billing/renewalAggregate/profile.d.ts +8 -0
- package/dist/client/billing/renewalAggregate/profile.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/profile.js +60 -0
- package/dist/client/billing/renewalAggregate/profile.js.map +1 -0
- package/dist/client/billing/renewalAggregate/receipt.d.ts +40 -0
- package/dist/client/billing/renewalAggregate/receipt.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/receipt.js +62 -0
- package/dist/client/billing/renewalAggregate/receipt.js.map +1 -0
- package/dist/client/billing/renewalAggregate/shared.d.ts +51 -0
- package/dist/client/billing/renewalAggregate/shared.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/shared.js +87 -0
- package/dist/client/billing/renewalAggregate/shared.js.map +1 -0
- package/dist/client/billing/renewalAggregate/sourceDiscounts.d.ts +42 -0
- package/dist/client/billing/renewalAggregate/sourceDiscounts.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/sourceDiscounts.js +90 -0
- package/dist/client/billing/renewalAggregate/sourceDiscounts.js.map +1 -0
- package/dist/client/billing/renewalAggregate/sourceSettings.d.ts +136 -0
- package/dist/client/billing/renewalAggregate/sourceSettings.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/sourceSettings.js +82 -0
- package/dist/client/billing/renewalAggregate/sourceSettings.js.map +1 -0
- package/dist/client/billing/renewalAggregate/taxRetention.d.ts +8 -0
- package/dist/client/billing/renewalAggregate/taxRetention.d.ts.map +1 -0
- package/dist/client/billing/renewalAggregate/taxRetention.js +23 -0
- package/dist/client/billing/renewalAggregate/taxRetention.js.map +1 -0
- package/dist/client/billing/renewalPriceReplacement.d.ts +2 -0
- package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
- package/dist/client/billing/renewalProjection/discounts.d.ts +20 -0
- package/dist/client/billing/renewalProjection/discounts.d.ts.map +1 -0
- package/dist/client/billing/renewalProjection/discounts.js +121 -0
- package/dist/client/billing/renewalProjection/discounts.js.map +1 -0
- package/dist/client/billing/renewalProjection/itemSettings.d.ts +14 -0
- package/dist/client/billing/renewalProjection/itemSettings.d.ts.map +1 -0
- package/dist/client/billing/renewalProjection/itemSettings.js +41 -0
- package/dist/client/billing/renewalProjection/itemSettings.js.map +1 -0
- package/dist/client/billing/renewalProjection/settings.d.ts +18 -0
- package/dist/client/billing/renewalProjection/settings.d.ts.map +1 -0
- package/dist/client/billing/renewalProjection/settings.js +132 -0
- package/dist/client/billing/renewalProjection/settings.js.map +1 -0
- package/dist/client/billing/renewalProjection/settingsSchema.d.ts +200 -0
- package/dist/client/billing/renewalProjection/settingsSchema.d.ts.map +1 -0
- package/dist/client/billing/renewalProjection/settingsSchema.js +150 -0
- package/dist/client/billing/renewalProjection/settingsSchema.js.map +1 -0
- package/dist/client/billing/renewalReplacementSchema.d.ts +2 -0
- package/dist/client/billing/renewalReplacementSchema.d.ts.map +1 -1
- package/dist/client/billing/renewalReplacementSchema.js +10 -4
- package/dist/client/billing/renewalReplacementSchema.js.map +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/component/balanceFunding/mutations.d.ts +43 -43
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/docs/renewal-phase-assembly.md +180 -0
- package/docs/renewal-preservation-mappings.md +94 -0
- package/docs/renewal-price-replacements.md +7 -0
- package/package.json +1 -1
- package/src/client/billing/renewal-discount-projection.test.ts +207 -0
- package/src/client/billing/renewal-item-settings-projection.test.ts +79 -0
- package/src/client/billing/renewal-price-replacement.test.ts +46 -0
- package/src/client/billing/renewal-settings-projection.test.ts +300 -0
- package/src/client/billing/renewalAggregate/configure.test.ts +372 -0
- package/src/client/billing/renewalAggregate/configure.ts +164 -0
- package/src/client/billing/renewalAggregate/index.ts +2 -0
- package/src/client/billing/renewalAggregate/phaseContext.ts +144 -0
- package/src/client/billing/renewalAggregate/phases.ts +247 -0
- package/src/client/billing/renewalAggregate/preflight.test.ts +233 -0
- package/src/client/billing/renewalAggregate/preflight.ts +211 -0
- package/src/client/billing/renewalAggregate/profile.ts +76 -0
- package/src/client/billing/renewalAggregate/receipt.ts +76 -0
- package/src/client/billing/renewalAggregate/shared.ts +106 -0
- package/src/client/billing/renewalAggregate/sourceAndTaxRetention.test.ts +247 -0
- package/src/client/billing/renewalAggregate/sourceDiscounts.ts +112 -0
- package/src/client/billing/renewalAggregate/sourceSettings.ts +88 -0
- package/src/client/billing/renewalAggregate/taxRetention.ts +28 -0
- package/src/client/billing/renewalProjection/discounts.ts +152 -0
- package/src/client/billing/renewalProjection/itemSettings.ts +55 -0
- package/src/client/billing/renewalProjection/settings.ts +163 -0
- package/src/client/billing/renewalProjection/settingsSchema.ts +154 -0
- package/src/client/billing/renewalReplacementSchema.ts +10 -4
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
preflightRenewalReplacement,
|
|
4
|
+
projectRenewalConfiguration,
|
|
5
|
+
} from "./index.js";
|
|
6
|
+
import { fixture } from "../../../../tests/renewalAggregate/fixtures.js";
|
|
7
|
+
|
|
8
|
+
const rate = (id: string) => ({ id, object: "tax_rate", livemode: false });
|
|
9
|
+
const automatic = (liability: unknown = null) => ({
|
|
10
|
+
enabled: true,
|
|
11
|
+
liability,
|
|
12
|
+
disabled_reason: null,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it.each([
|
|
16
|
+
{
|
|
17
|
+
start_date: 1768435200,
|
|
18
|
+
end_date: 1771113600,
|
|
19
|
+
trial_offer: "trial_offer_active",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
start_date: 1771113600,
|
|
23
|
+
end_date: 1773532800,
|
|
24
|
+
trial_offer: "trial_offer_future",
|
|
25
|
+
},
|
|
26
|
+
])(
|
|
27
|
+
"rejects an actual item current_trial while the subscription remains active: %j",
|
|
28
|
+
(current_trial) => {
|
|
29
|
+
const f = fixture({ item: { current_trial } });
|
|
30
|
+
expect(f.subscription.status).toBe("active");
|
|
31
|
+
expect(preflightRenewalReplacement(f.preflightInput)).toMatchObject({
|
|
32
|
+
status: "unsupported",
|
|
33
|
+
reason: expect.stringMatching(/trial/i),
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
it("requires explicit no-current-trial evidence instead of inventing null for a missing item field", () => {
|
|
39
|
+
const f = fixture({ omitItemCurrentTrial: true });
|
|
40
|
+
expect(f.originalItem).not.toHaveProperty("current_trial");
|
|
41
|
+
expect(preflightRenewalReplacement(f.preflightInput)).toMatchObject({
|
|
42
|
+
status: "unsupported",
|
|
43
|
+
reason: expect.stringMatching(/trial/i),
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it.each([
|
|
48
|
+
{
|
|
49
|
+
name: "disabled automatic tax",
|
|
50
|
+
subscription: {
|
|
51
|
+
automatic_tax: {
|
|
52
|
+
enabled: false,
|
|
53
|
+
liability: null,
|
|
54
|
+
disabled_reason: "requires_location_inputs",
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "unknown liability",
|
|
60
|
+
subscription: { automatic_tax: automatic({ type: "future_owner" }) },
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "foreign payment method",
|
|
64
|
+
subscription: {
|
|
65
|
+
default_payment_method: {
|
|
66
|
+
id: "pm_foreign",
|
|
67
|
+
object: "payment_method",
|
|
68
|
+
customer: "cus_foreign",
|
|
69
|
+
livemode: false,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: "malformed invoice issuer",
|
|
75
|
+
subscription: {
|
|
76
|
+
invoice_settings: {
|
|
77
|
+
account_tax_ids: null,
|
|
78
|
+
custom_fields: null,
|
|
79
|
+
description: null,
|
|
80
|
+
footer: null,
|
|
81
|
+
issuer: { type: "future_issuer" },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: "invalid transfer precision",
|
|
87
|
+
subscription: {
|
|
88
|
+
transfer_data: {
|
|
89
|
+
destination: "acct_destination",
|
|
90
|
+
amount_percent: 12.345,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: "deleted routing account",
|
|
96
|
+
subscription: {
|
|
97
|
+
on_behalf_of: {
|
|
98
|
+
id: "acct_issuer",
|
|
99
|
+
object: "account",
|
|
100
|
+
deleted: true,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: "automatic plus subscription manual tax",
|
|
106
|
+
subscription: {
|
|
107
|
+
automatic_tax: automatic(),
|
|
108
|
+
default_tax_rates: [rate("txr_manual")],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "automatic plus item manual tax",
|
|
113
|
+
subscription: { automatic_tax: automatic() },
|
|
114
|
+
item: { tax_rates: [rate("txr_manual")] },
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "unproven subscription tax inheritance",
|
|
118
|
+
subscription: { default_tax_rates: null },
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "unproven item tax inheritance",
|
|
122
|
+
subscription: {},
|
|
123
|
+
item: { tax_rates: null },
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: "invoice due days under automatic collection",
|
|
127
|
+
subscription: { days_until_due: 3 },
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: "unknown collection method",
|
|
131
|
+
subscription: { collection_method: "future_collection" },
|
|
132
|
+
},
|
|
133
|
+
])(
|
|
134
|
+
"rejects knowable original billing facts before attach: $name",
|
|
135
|
+
(options) => {
|
|
136
|
+
const result = preflightRenewalReplacement(fixture(options).preflightInput);
|
|
137
|
+
expect(result).toMatchObject({
|
|
138
|
+
status: "unsupported",
|
|
139
|
+
reason: expect.any(String),
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
it("accepts mappable original routing, payment and manual-tax facts without an invented phase", () => {
|
|
145
|
+
const f = fixture({
|
|
146
|
+
subscription: {
|
|
147
|
+
default_payment_method: {
|
|
148
|
+
id: "pm_owned",
|
|
149
|
+
object: "payment_method",
|
|
150
|
+
customer: "cus_buyer",
|
|
151
|
+
livemode: false,
|
|
152
|
+
},
|
|
153
|
+
on_behalf_of: "acct_issuer",
|
|
154
|
+
transfer_data: { destination: "acct_destination", amount_percent: 0 },
|
|
155
|
+
default_tax_rates: [rate("txr_original")],
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
expect(preflightRenewalReplacement(f.preflightInput)).toMatchObject({
|
|
159
|
+
status: "preflighted_for_projection",
|
|
160
|
+
providerQualification: "required",
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it.each([
|
|
165
|
+
{
|
|
166
|
+
name: "different phase rates",
|
|
167
|
+
subscription: { default_tax_rates: [rate("txr_original")] },
|
|
168
|
+
phase: { default_tax_rates: [rate("txr_other")] },
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: "different item rates",
|
|
172
|
+
item: { tax_rates: [rate("txr_original")] },
|
|
173
|
+
phaseItem: { tax_rates: [rate("txr_other")] },
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "new item override with the same current effective rate",
|
|
177
|
+
subscription: { default_tax_rates: [rate("txr_original")] },
|
|
178
|
+
phase: { default_tax_rates: [rate("txr_original")] },
|
|
179
|
+
phaseItem: { tax_rates: [rate("txr_original")] },
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "removed item override with the same current effective rate",
|
|
183
|
+
subscription: { default_tax_rates: [rate("txr_original")] },
|
|
184
|
+
item: { tax_rates: [rate("txr_original")] },
|
|
185
|
+
phase: { default_tax_rates: [rate("txr_original")] },
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "changed ordered phase rates",
|
|
189
|
+
subscription: {
|
|
190
|
+
default_tax_rates: [rate("txr_first"), rate("txr_second")],
|
|
191
|
+
},
|
|
192
|
+
phase: { default_tax_rates: [rate("txr_second"), rate("txr_first")] },
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: "unproven null phase tax hierarchy",
|
|
196
|
+
phase: { default_tax_rates: null },
|
|
197
|
+
},
|
|
198
|
+
{ name: "unproven null phase item override", phaseItem: { tax_rates: null } },
|
|
199
|
+
])("retains exact tax values and override level: $name", (options) => {
|
|
200
|
+
expect(
|
|
201
|
+
projectRenewalConfiguration(fixture(options).configureInput),
|
|
202
|
+
).toMatchObject({
|
|
203
|
+
status: "attached_unconfigured",
|
|
204
|
+
reason: expect.stringMatching(/tax/i),
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("preserves the subscription tax level and an empty item fallback", () => {
|
|
209
|
+
const f = fixture({
|
|
210
|
+
subscription: { default_tax_rates: [rate("txr_original")] },
|
|
211
|
+
phase: { default_tax_rates: [rate("txr_original")] },
|
|
212
|
+
});
|
|
213
|
+
const result = projectRenewalConfiguration(f.configureInput);
|
|
214
|
+
expect(result).toMatchObject({
|
|
215
|
+
status: "projected_configure_request",
|
|
216
|
+
payload: {
|
|
217
|
+
phases: [
|
|
218
|
+
{ default_tax_rates: ["txr_original"], items: [{ tax_rates: "" }] },
|
|
219
|
+
{ default_tax_rates: ["txr_original"], items: [{ tax_rates: "" }] },
|
|
220
|
+
],
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("preserves an explicit item override separately from phase default rates", () => {
|
|
226
|
+
const f = fixture({
|
|
227
|
+
subscription: { default_tax_rates: [rate("txr_phase")] },
|
|
228
|
+
item: { tax_rates: [rate("txr_item")] },
|
|
229
|
+
phase: { default_tax_rates: [rate("txr_phase")] },
|
|
230
|
+
phaseItem: { tax_rates: [rate("txr_item")] },
|
|
231
|
+
});
|
|
232
|
+
expect(projectRenewalConfiguration(f.configureInput)).toMatchObject({
|
|
233
|
+
status: "projected_configure_request",
|
|
234
|
+
payload: {
|
|
235
|
+
phases: [
|
|
236
|
+
{
|
|
237
|
+
default_tax_rates: ["txr_phase"],
|
|
238
|
+
items: [{ tax_rates: ["txr_item"] }],
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
default_tax_rates: ["txr_phase"],
|
|
242
|
+
items: [{ tax_rates: ["txr_item"] }],
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { resourceId, unsupported } from "./shared.js";
|
|
3
|
+
|
|
4
|
+
const customId = z.string().min(1).max(255).regex(/\S/);
|
|
5
|
+
const originalRedemptions = z
|
|
6
|
+
.array(
|
|
7
|
+
z
|
|
8
|
+
.object({
|
|
9
|
+
id: resourceId("di"),
|
|
10
|
+
object: z.literal("discount"),
|
|
11
|
+
deleted: z.literal(false).optional(),
|
|
12
|
+
customer_account: resourceId("acct"),
|
|
13
|
+
customer: z
|
|
14
|
+
.union([
|
|
15
|
+
resourceId("cus"),
|
|
16
|
+
z
|
|
17
|
+
.object({
|
|
18
|
+
id: resourceId("cus"),
|
|
19
|
+
object: z.literal("customer"),
|
|
20
|
+
deleted: z.literal(false).optional(),
|
|
21
|
+
livemode: z.boolean().optional(),
|
|
22
|
+
customer_account: resourceId("acct").nullable().optional(),
|
|
23
|
+
})
|
|
24
|
+
.passthrough(),
|
|
25
|
+
])
|
|
26
|
+
.nullable()
|
|
27
|
+
.optional(),
|
|
28
|
+
subscription: resourceId("sub").nullable(),
|
|
29
|
+
subscription_item: resourceId("si").nullable(),
|
|
30
|
+
start: z.number().int().nonnegative(),
|
|
31
|
+
end: z.null(),
|
|
32
|
+
source: z
|
|
33
|
+
.object({
|
|
34
|
+
type: z.literal("coupon"),
|
|
35
|
+
coupon: z
|
|
36
|
+
.object({
|
|
37
|
+
id: customId,
|
|
38
|
+
object: z.literal("coupon"),
|
|
39
|
+
deleted: z.literal(false).optional(),
|
|
40
|
+
livemode: z.boolean().optional(),
|
|
41
|
+
duration: z.literal("forever"),
|
|
42
|
+
applies_to: z
|
|
43
|
+
.object({ products: z.array(customId) })
|
|
44
|
+
.strict()
|
|
45
|
+
.optional(),
|
|
46
|
+
})
|
|
47
|
+
.passthrough(),
|
|
48
|
+
})
|
|
49
|
+
.passthrough(),
|
|
50
|
+
})
|
|
51
|
+
.passthrough(),
|
|
52
|
+
)
|
|
53
|
+
.max(20);
|
|
54
|
+
export type OriginalRedemptions = z.infer<typeof originalRedemptions>;
|
|
55
|
+
|
|
56
|
+
export function qualifySourceDiscounts(
|
|
57
|
+
value: unknown,
|
|
58
|
+
level: "subscription" | "item",
|
|
59
|
+
context: {
|
|
60
|
+
customerAccountId: string;
|
|
61
|
+
subscriptionId: string;
|
|
62
|
+
itemId: string;
|
|
63
|
+
currentProductId: string;
|
|
64
|
+
targetProductId: string;
|
|
65
|
+
livemode: boolean;
|
|
66
|
+
},
|
|
67
|
+
customerId: string,
|
|
68
|
+
observedAt: number,
|
|
69
|
+
): OriginalRedemptions {
|
|
70
|
+
const parsed = originalRedemptions.safeParse(value);
|
|
71
|
+
if (!parsed.success)
|
|
72
|
+
unsupported("incomplete_or_unsupported_discount_evidence");
|
|
73
|
+
if (!parsed.data.length) unsupported("ambiguous_empty_discount_hierarchy");
|
|
74
|
+
if (
|
|
75
|
+
new Set(parsed.data.map((discount) => discount.id)).size !==
|
|
76
|
+
parsed.data.length
|
|
77
|
+
)
|
|
78
|
+
unsupported("duplicate_discount_redemptions");
|
|
79
|
+
for (const discount of parsed.data) {
|
|
80
|
+
const customer = discount.customer;
|
|
81
|
+
if (
|
|
82
|
+
discount.customer_account !== context.customerAccountId ||
|
|
83
|
+
discount.start > observedAt ||
|
|
84
|
+
(typeof customer === "string" && customer !== customerId) ||
|
|
85
|
+
(typeof customer === "object" &&
|
|
86
|
+
customer !== null &&
|
|
87
|
+
(customer.id !== customerId ||
|
|
88
|
+
(customer.livemode !== undefined &&
|
|
89
|
+
customer.livemode !== context.livemode) ||
|
|
90
|
+
(customer.customer_account !== undefined &&
|
|
91
|
+
customer.customer_account !== context.customerAccountId))) ||
|
|
92
|
+
(level === "subscription" &&
|
|
93
|
+
(discount.subscription !== context.subscriptionId ||
|
|
94
|
+
discount.subscription_item !== null)) ||
|
|
95
|
+
(level === "item" &&
|
|
96
|
+
(discount.subscription_item !== context.itemId ||
|
|
97
|
+
(discount.subscription !== null &&
|
|
98
|
+
discount.subscription !== context.subscriptionId)))
|
|
99
|
+
)
|
|
100
|
+
unsupported("discount_owner_or_start_mismatch");
|
|
101
|
+
const coupon = discount.source.coupon;
|
|
102
|
+
if (coupon.livemode !== undefined && coupon.livemode !== context.livemode)
|
|
103
|
+
unsupported("discount_coupon_mode_mismatch");
|
|
104
|
+
if (
|
|
105
|
+
coupon.applies_to &&
|
|
106
|
+
(!coupon.applies_to.products.includes(context.currentProductId) ||
|
|
107
|
+
!coupon.applies_to.products.includes(context.targetProductId))
|
|
108
|
+
)
|
|
109
|
+
unsupported("discount_product_applicability");
|
|
110
|
+
}
|
|
111
|
+
return parsed.data;
|
|
112
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { settingsInput } from "../renewalProjection/settingsSchema.js";
|
|
2
|
+
import { projectRenewalPhaseSettings } from "../renewalProjection/settings.js";
|
|
3
|
+
import { projectRenewalItemSettings } from "../renewalProjection/itemSettings.js";
|
|
4
|
+
import { explicitTaxIds } from "./taxRetention.js";
|
|
5
|
+
import { unsupported } from "./shared.js";
|
|
6
|
+
|
|
7
|
+
const invoiceSchema = settingsInput.shape.settings.shape.invoice_settings
|
|
8
|
+
.unwrap()
|
|
9
|
+
.omit({ days_until_due: true })
|
|
10
|
+
.extend({
|
|
11
|
+
issuer: settingsInput.shape.context.shape.effectiveInvoiceIssuer,
|
|
12
|
+
})
|
|
13
|
+
.strict();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Classify actual original subscription/item fields before any attachment.
|
|
17
|
+
* The field projections below are validation views, not fabricated phase reads
|
|
18
|
+
* or provider acknowledgement. Their output is never dispatched or adopted.
|
|
19
|
+
*/
|
|
20
|
+
export function qualifyOriginalSettings(
|
|
21
|
+
subscription: Record<string, unknown>,
|
|
22
|
+
item: Record<string, unknown>,
|
|
23
|
+
customer: { id: string; customer_account: string; livemode: boolean },
|
|
24
|
+
) {
|
|
25
|
+
const invoice = invoiceSchema.safeParse(subscription.invoice_settings);
|
|
26
|
+
if (!invoice.success) unsupported("unsupported_original_invoice_settings");
|
|
27
|
+
const automaticTax = settingsInput.shape.settings.shape.automatic_tax
|
|
28
|
+
.unwrap()
|
|
29
|
+
.safeParse(subscription.automatic_tax);
|
|
30
|
+
if (!automaticTax.success) unsupported("unsupported_original_automatic_tax");
|
|
31
|
+
const parsedContext = settingsInput.shape.context.safeParse({
|
|
32
|
+
customerId: customer.id,
|
|
33
|
+
customerAccountId: customer.customer_account,
|
|
34
|
+
livemode: customer.livemode,
|
|
35
|
+
effectiveCollectionMethod: subscription.collection_method,
|
|
36
|
+
effectiveAutomaticTax: automaticTax.data.enabled,
|
|
37
|
+
effectiveInvoiceIssuer: invoice.data.issuer,
|
|
38
|
+
});
|
|
39
|
+
if (!parsedContext.success)
|
|
40
|
+
unsupported("unsupported_original_billing_context");
|
|
41
|
+
const context = parsedContext.data;
|
|
42
|
+
const taxLevels = {
|
|
43
|
+
subscription: explicitTaxIds(
|
|
44
|
+
subscription.default_tax_rates,
|
|
45
|
+
"original_subscription",
|
|
46
|
+
),
|
|
47
|
+
item: explicitTaxIds(item.tax_rates, "original_item"),
|
|
48
|
+
};
|
|
49
|
+
const projection = projectRenewalPhaseSettings({
|
|
50
|
+
context,
|
|
51
|
+
settings: {
|
|
52
|
+
application_fee_percent: subscription.application_fee_percent,
|
|
53
|
+
billing_thresholds: subscription.billing_thresholds,
|
|
54
|
+
collection_method: subscription.collection_method,
|
|
55
|
+
default_payment_method: subscription.default_payment_method,
|
|
56
|
+
on_behalf_of: subscription.on_behalf_of,
|
|
57
|
+
transfer_data: subscription.transfer_data,
|
|
58
|
+
automatic_tax: automaticTax.data,
|
|
59
|
+
default_tax_rates: subscription.default_tax_rates,
|
|
60
|
+
// days_until_due is a top-level Subscription field in Stripe 23.
|
|
61
|
+
invoice_settings: {
|
|
62
|
+
...invoice.data,
|
|
63
|
+
days_until_due: subscription.days_until_due,
|
|
64
|
+
},
|
|
65
|
+
description: subscription.description,
|
|
66
|
+
metadata: null,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (projection.status !== "projected")
|
|
70
|
+
unsupported(`unsupported_original_settings_${projection.reason}`);
|
|
71
|
+
const itemProjection = projectRenewalItemSettings({
|
|
72
|
+
effectiveAutomaticTax: context.effectiveAutomaticTax,
|
|
73
|
+
livemode: context.livemode,
|
|
74
|
+
settings: {
|
|
75
|
+
billing_thresholds: item.billing_thresholds,
|
|
76
|
+
tax_rates: item.tax_rates,
|
|
77
|
+
metadata: item.metadata,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
if (itemProjection.status !== "projected")
|
|
81
|
+
unsupported(`unsupported_original_${itemProjection.reason}`);
|
|
82
|
+
return {
|
|
83
|
+
context,
|
|
84
|
+
invoice: invoice.data,
|
|
85
|
+
automaticTax: automaticTax.data,
|
|
86
|
+
taxLevels,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { taxRate } from "../renewalProjection/settingsSchema.js";
|
|
3
|
+
import { equal, unsupported } from "./shared.js";
|
|
4
|
+
|
|
5
|
+
/** Null/absent read hierarchies are not evidence of an explicit empty list. */
|
|
6
|
+
export function explicitTaxIds(value: unknown, level: string): string[] {
|
|
7
|
+
const parsed = z.array(taxRate).max(20).safeParse(value);
|
|
8
|
+
if (!parsed.success) unsupported(`unproven_${level}_tax_hierarchy`);
|
|
9
|
+
return parsed.data.map((tax) => tax.id);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type OriginalTaxLevels = { subscription: string[]; item: string[] };
|
|
13
|
+
|
|
14
|
+
export function preserveTaxLevels(
|
|
15
|
+
original: OriginalTaxLevels,
|
|
16
|
+
phaseTax: unknown,
|
|
17
|
+
phaseItemTax: unknown,
|
|
18
|
+
) {
|
|
19
|
+
const phase = explicitTaxIds(phaseTax, "phase_default");
|
|
20
|
+
const item = explicitTaxIds(phaseItemTax, "phase_item");
|
|
21
|
+
if (!equal(original.subscription, phase))
|
|
22
|
+
unsupported("phase_default_tax_retention_mismatch");
|
|
23
|
+
// An empty item array means no item override. Its subscription/phase default
|
|
24
|
+
// applies instead. Equal current totals cannot authorize changing that level.
|
|
25
|
+
if (original.item.length > 0 !== item.length > 0)
|
|
26
|
+
unsupported("item_tax_override_level_changed");
|
|
27
|
+
if (!equal(original.item, item)) unsupported("item_tax_retention_mismatch");
|
|
28
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
3
|
+
|
|
4
|
+
// Coupons and products may have caller-supplied IDs. Do not require a prefix.
|
|
5
|
+
const id = z.string().min(1).max(255).regex(/\S/);
|
|
6
|
+
const resourceId = (prefix: string) =>
|
|
7
|
+
z
|
|
8
|
+
.string()
|
|
9
|
+
.max(255)
|
|
10
|
+
.regex(new RegExp(`^${prefix}_[A-Za-z0-9_]+$`));
|
|
11
|
+
const coupon = z
|
|
12
|
+
.object({
|
|
13
|
+
id,
|
|
14
|
+
object: z.literal("coupon"),
|
|
15
|
+
deleted: z.literal(false).optional(),
|
|
16
|
+
duration: z.enum(["once", "repeating", "forever"]),
|
|
17
|
+
applies_to: z
|
|
18
|
+
.object({ products: z.array(id) })
|
|
19
|
+
.strict()
|
|
20
|
+
.optional(),
|
|
21
|
+
})
|
|
22
|
+
.passthrough();
|
|
23
|
+
const redemption = z
|
|
24
|
+
.object({
|
|
25
|
+
id: resourceId("di"),
|
|
26
|
+
object: z.literal("discount"),
|
|
27
|
+
deleted: z.literal(false).optional(),
|
|
28
|
+
customer_account: resourceId("acct"),
|
|
29
|
+
subscription: resourceId("sub").nullable(),
|
|
30
|
+
subscription_item: resourceId("si").nullable(),
|
|
31
|
+
start: z.number().int().nonnegative(),
|
|
32
|
+
end: z.number().int().nonnegative().nullable(),
|
|
33
|
+
source: z.object({ type: z.literal("coupon"), coupon }).passthrough(),
|
|
34
|
+
})
|
|
35
|
+
.passthrough();
|
|
36
|
+
const reference = (object: string, identity = id) =>
|
|
37
|
+
z.union([
|
|
38
|
+
identity,
|
|
39
|
+
z
|
|
40
|
+
.object({
|
|
41
|
+
id: identity,
|
|
42
|
+
object: z.literal(object),
|
|
43
|
+
deleted: z.literal(false).optional(),
|
|
44
|
+
})
|
|
45
|
+
.strict(),
|
|
46
|
+
]);
|
|
47
|
+
const inputSchema = z
|
|
48
|
+
.object({
|
|
49
|
+
customerAccountId: resourceId("acct"),
|
|
50
|
+
subscriptionId: resourceId("sub"),
|
|
51
|
+
level: z.enum(["subscription", "subscription_item"]),
|
|
52
|
+
itemId: resourceId("si").optional(),
|
|
53
|
+
currentProductId: id,
|
|
54
|
+
targetProductId: id,
|
|
55
|
+
redemptions: z.array(redemption).max(20),
|
|
56
|
+
phaseDiscounts: z
|
|
57
|
+
.array(
|
|
58
|
+
z
|
|
59
|
+
.object({
|
|
60
|
+
discount: reference("discount", resourceId("di")),
|
|
61
|
+
coupon: reference("coupon").nullable().optional(),
|
|
62
|
+
promotion_code: z.null().optional(),
|
|
63
|
+
})
|
|
64
|
+
.passthrough(),
|
|
65
|
+
)
|
|
66
|
+
.max(20),
|
|
67
|
+
})
|
|
68
|
+
.strict();
|
|
69
|
+
|
|
70
|
+
type Reason =
|
|
71
|
+
| "incomplete_discount_evidence"
|
|
72
|
+
| "ambiguous_empty_discount_hierarchy"
|
|
73
|
+
| "discount_not_owned_at_level"
|
|
74
|
+
| "discount_duration_not_qualified"
|
|
75
|
+
| "coupon_product_applicability"
|
|
76
|
+
| "phase_redemption_mismatch"
|
|
77
|
+
| "duplicate_redemptions";
|
|
78
|
+
export type RenewalDiscountProjection =
|
|
79
|
+
| { status: "unsupported"; reason: Reason }
|
|
80
|
+
| {
|
|
81
|
+
status: "projected";
|
|
82
|
+
discounts: { discount: string }[];
|
|
83
|
+
requiresProviderQualification: true;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Project only a direct, unambiguous existing forever redemption. Ancestor-owned
|
|
88
|
+
* redemptions are unsupported. Phase references must be IDs or minimal reference
|
|
89
|
+
* objects; full expanded objects require separate normalization and correlation.
|
|
90
|
+
* This field projection requires provider qualification before writer eligibility.
|
|
91
|
+
*/
|
|
92
|
+
export function projectRenewalDiscounts(
|
|
93
|
+
input: unknown,
|
|
94
|
+
): RenewalDiscountProjection {
|
|
95
|
+
let parsed: ReturnType<typeof inputSchema.safeParse>;
|
|
96
|
+
try {
|
|
97
|
+
parsed = inputSchema.safeParse(renewalSnapshot(input));
|
|
98
|
+
} catch {
|
|
99
|
+
return { status: "unsupported", reason: "incomplete_discount_evidence" };
|
|
100
|
+
}
|
|
101
|
+
if (!parsed.success)
|
|
102
|
+
return { status: "unsupported", reason: "incomplete_discount_evidence" };
|
|
103
|
+
const args = parsed.data;
|
|
104
|
+
const unsupported = (reason: Reason): RenewalDiscountProjection => ({
|
|
105
|
+
status: "unsupported",
|
|
106
|
+
reason,
|
|
107
|
+
});
|
|
108
|
+
if (args.redemptions.length === 0 && args.phaseDiscounts.length === 0)
|
|
109
|
+
return unsupported("ambiguous_empty_discount_hierarchy");
|
|
110
|
+
if (args.redemptions.length !== args.phaseDiscounts.length)
|
|
111
|
+
return unsupported("phase_redemption_mismatch");
|
|
112
|
+
if (
|
|
113
|
+
new Set(args.redemptions.map((v) => v.id)).size !== args.redemptions.length
|
|
114
|
+
)
|
|
115
|
+
return unsupported("duplicate_redemptions");
|
|
116
|
+
const refId = (value: z.infer<ReturnType<typeof reference>>) =>
|
|
117
|
+
typeof value === "string" ? value : value.id;
|
|
118
|
+
for (const [index, value] of args.redemptions.entries()) {
|
|
119
|
+
if (
|
|
120
|
+
value.customer_account !== args.customerAccountId ||
|
|
121
|
+
(args.level === "subscription" &&
|
|
122
|
+
(value.subscription !== args.subscriptionId ||
|
|
123
|
+
value.subscription_item !== null)) ||
|
|
124
|
+
(args.level === "subscription_item" &&
|
|
125
|
+
(!args.itemId ||
|
|
126
|
+
value.subscription_item !== args.itemId ||
|
|
127
|
+
(value.subscription !== null &&
|
|
128
|
+
value.subscription !== args.subscriptionId)))
|
|
129
|
+
)
|
|
130
|
+
return unsupported("discount_not_owned_at_level");
|
|
131
|
+
if (value.source.coupon.duration !== "forever" || value.end !== null)
|
|
132
|
+
return unsupported("discount_duration_not_qualified");
|
|
133
|
+
const products = value.source.coupon.applies_to?.products;
|
|
134
|
+
if (
|
|
135
|
+
products &&
|
|
136
|
+
(!products.includes(args.currentProductId) ||
|
|
137
|
+
!products.includes(args.targetProductId))
|
|
138
|
+
)
|
|
139
|
+
return unsupported("coupon_product_applicability");
|
|
140
|
+
const phase = args.phaseDiscounts[index]!;
|
|
141
|
+
if (
|
|
142
|
+
refId(phase.discount) !== value.id ||
|
|
143
|
+
(phase.coupon != null && refId(phase.coupon) !== value.source.coupon.id)
|
|
144
|
+
)
|
|
145
|
+
return unsupported("phase_redemption_mismatch");
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
status: "projected",
|
|
149
|
+
discounts: args.redemptions.map(({ id: discount }) => ({ discount })),
|
|
150
|
+
requiresProviderQualification: true,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
4
|
+
import { taxRate } from "./settingsSchema.js";
|
|
5
|
+
|
|
6
|
+
const inputSchema = z
|
|
7
|
+
.object({
|
|
8
|
+
effectiveAutomaticTax: z.boolean(),
|
|
9
|
+
livemode: z.boolean(),
|
|
10
|
+
settings: z
|
|
11
|
+
.object({
|
|
12
|
+
billing_thresholds: z.null(),
|
|
13
|
+
tax_rates: z.array(taxRate).max(20).nullable().optional(),
|
|
14
|
+
metadata: z
|
|
15
|
+
.record(z.string().max(40), z.string().min(1).max(500))
|
|
16
|
+
.nullable(),
|
|
17
|
+
})
|
|
18
|
+
.strict(),
|
|
19
|
+
})
|
|
20
|
+
.strict();
|
|
21
|
+
type Settings = Pick<
|
|
22
|
+
Stripe.SubscriptionScheduleUpdateParams.Phase.Item,
|
|
23
|
+
"tax_rates" | "metadata"
|
|
24
|
+
>;
|
|
25
|
+
type Result =
|
|
26
|
+
| { status: "unsupported"; reason: string }
|
|
27
|
+
| {
|
|
28
|
+
status: "projected";
|
|
29
|
+
settings: Settings;
|
|
30
|
+
requiresProviderQualification: true;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** Field projection only. The assembler must derive effective tax from defaults. */
|
|
34
|
+
export function projectRenewalItemSettings(input: unknown): Result {
|
|
35
|
+
let args: z.infer<typeof inputSchema>;
|
|
36
|
+
try {
|
|
37
|
+
args = inputSchema.parse(renewalSnapshot(input));
|
|
38
|
+
} catch {
|
|
39
|
+
return { status: "unsupported", reason: "unsupported_item_settings" };
|
|
40
|
+
}
|
|
41
|
+
const { settings: source } = args;
|
|
42
|
+
if (
|
|
43
|
+
(args.effectiveAutomaticTax && source.tax_rates?.length) ||
|
|
44
|
+
source.tax_rates?.some((tax) => tax.livemode !== args.livemode)
|
|
45
|
+
)
|
|
46
|
+
return { status: "unsupported", reason: "incompatible_item_tax" };
|
|
47
|
+
const settings: Settings = {};
|
|
48
|
+
if (source.tax_rates != null)
|
|
49
|
+
settings.tax_rates = source.tax_rates.length
|
|
50
|
+
? source.tax_rates.map((tax) => tax.id)
|
|
51
|
+
: "";
|
|
52
|
+
if (source.metadata !== null && Object.keys(source.metadata).length)
|
|
53
|
+
settings.metadata = source.metadata;
|
|
54
|
+
return { status: "projected", settings, requiresProviderQualification: true };
|
|
55
|
+
}
|