@fantastic.dev/better-stripe 0.80.0 → 0.81.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 +6 -0
- package/README.md +1 -1
- 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/docs/renewal-preservation-mappings.md +94 -0
- package/docs/renewal-price-replacements.md +5 -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/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,163 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
4
|
+
import { settingsInput, type reference } from "./settingsSchema.js";
|
|
5
|
+
|
|
6
|
+
type Settings = Pick<
|
|
7
|
+
Stripe.SubscriptionScheduleUpdateParams.Phase,
|
|
8
|
+
| "collection_method"
|
|
9
|
+
| "default_payment_method"
|
|
10
|
+
| "on_behalf_of"
|
|
11
|
+
| "transfer_data"
|
|
12
|
+
| "automatic_tax"
|
|
13
|
+
| "default_tax_rates"
|
|
14
|
+
| "invoice_settings"
|
|
15
|
+
| "description"
|
|
16
|
+
| "metadata"
|
|
17
|
+
>;
|
|
18
|
+
type Result =
|
|
19
|
+
| { status: "unsupported"; reason: string }
|
|
20
|
+
| {
|
|
21
|
+
status: "projected";
|
|
22
|
+
settings: Settings;
|
|
23
|
+
requiresProviderQualification: true;
|
|
24
|
+
};
|
|
25
|
+
type Reference = z.infer<ReturnType<typeof reference>>;
|
|
26
|
+
const identity = (ref: Reference) => (typeof ref === "string" ? ref : ref.id);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Map only the named fields of a provider phase. The aggregate assembler must
|
|
30
|
+
* correlate context with its full acknowledged schedule and unchanged defaults.
|
|
31
|
+
* A projection is neither a complete phase nor evidence of provider retention.
|
|
32
|
+
*/
|
|
33
|
+
export function projectRenewalPhaseSettings(input: unknown): Result {
|
|
34
|
+
let args: z.infer<typeof settingsInput>;
|
|
35
|
+
try {
|
|
36
|
+
args = settingsInput.parse(renewalSnapshot(input));
|
|
37
|
+
} catch {
|
|
38
|
+
return { status: "unsupported", reason: "unsupported_phase_settings" };
|
|
39
|
+
}
|
|
40
|
+
const { settings: source, context } = args;
|
|
41
|
+
const failure = (reason: string): Result => ({
|
|
42
|
+
status: "unsupported",
|
|
43
|
+
reason,
|
|
44
|
+
});
|
|
45
|
+
const referenceMatches = (ref: Reference | null | undefined) => {
|
|
46
|
+
if (ref == null || typeof ref === "string") return true;
|
|
47
|
+
const customer = ref.customer;
|
|
48
|
+
if (
|
|
49
|
+
typeof customer === "object" &&
|
|
50
|
+
customer !== null &&
|
|
51
|
+
((customer.livemode !== undefined &&
|
|
52
|
+
customer.livemode !== context.livemode) ||
|
|
53
|
+
(customer.customer_account !== undefined &&
|
|
54
|
+
customer.customer_account !== context.customerAccountId))
|
|
55
|
+
)
|
|
56
|
+
return false;
|
|
57
|
+
return (
|
|
58
|
+
(ref.livemode === undefined || ref.livemode === context.livemode) &&
|
|
59
|
+
(ref.customer_account == null ||
|
|
60
|
+
ref.customer_account === context.customerAccountId) &&
|
|
61
|
+
(customer == null || identity(customer) === context.customerId)
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
const references = [
|
|
65
|
+
source.default_payment_method,
|
|
66
|
+
source.on_behalf_of,
|
|
67
|
+
source.transfer_data?.destination,
|
|
68
|
+
...(source.invoice_settings?.account_tax_ids ?? []),
|
|
69
|
+
...(source.default_tax_rates ?? []),
|
|
70
|
+
source.automatic_tax?.liability?.account,
|
|
71
|
+
source.invoice_settings?.issuer?.account,
|
|
72
|
+
];
|
|
73
|
+
if (!references.every(referenceMatches))
|
|
74
|
+
return failure("foreign_setting_reference");
|
|
75
|
+
const issuerIdentity = (issuer: typeof context.effectiveInvoiceIssuer) =>
|
|
76
|
+
issuer.type === "self" ? "self" : identity(issuer.account);
|
|
77
|
+
if (
|
|
78
|
+
source.invoice_settings?.issuer != null &&
|
|
79
|
+
issuerIdentity(source.invoice_settings.issuer) !==
|
|
80
|
+
issuerIdentity(context.effectiveInvoiceIssuer)
|
|
81
|
+
)
|
|
82
|
+
return failure("invoice_issuer_mismatch");
|
|
83
|
+
for (const taxId of source.invoice_settings?.account_tax_ids ?? []) {
|
|
84
|
+
if (typeof taxId === "string") continue;
|
|
85
|
+
const owner = taxId.owner;
|
|
86
|
+
if (
|
|
87
|
+
issuerIdentity(owner) !==
|
|
88
|
+
issuerIdentity(context.effectiveInvoiceIssuer) ||
|
|
89
|
+
(owner.type === "account" && !referenceMatches(owner.account))
|
|
90
|
+
)
|
|
91
|
+
return failure("foreign_invoice_tax_owner");
|
|
92
|
+
}
|
|
93
|
+
if (
|
|
94
|
+
source.collection_method != null &&
|
|
95
|
+
source.collection_method !== context.effectiveCollectionMethod
|
|
96
|
+
)
|
|
97
|
+
return failure("collection_method_mismatch");
|
|
98
|
+
if (
|
|
99
|
+
source.automatic_tax !== undefined &&
|
|
100
|
+
source.automatic_tax.enabled !== context.effectiveAutomaticTax
|
|
101
|
+
)
|
|
102
|
+
return failure("automatic_tax_mismatch");
|
|
103
|
+
if (context.effectiveAutomaticTax && source.default_tax_rates?.length)
|
|
104
|
+
return failure("automatic_and_manual_tax");
|
|
105
|
+
const out: Settings = {};
|
|
106
|
+
if (source.collection_method !== null)
|
|
107
|
+
out.collection_method = source.collection_method;
|
|
108
|
+
if (source.default_payment_method !== null)
|
|
109
|
+
out.default_payment_method = identity(source.default_payment_method);
|
|
110
|
+
if (source.on_behalf_of !== null)
|
|
111
|
+
out.on_behalf_of = identity(source.on_behalf_of);
|
|
112
|
+
if (source.transfer_data !== null) {
|
|
113
|
+
out.transfer_data = {
|
|
114
|
+
destination: identity(source.transfer_data.destination),
|
|
115
|
+
};
|
|
116
|
+
if (source.transfer_data.amount_percent !== null)
|
|
117
|
+
out.transfer_data.amount_percent = source.transfer_data.amount_percent;
|
|
118
|
+
}
|
|
119
|
+
const mapIssuer = (
|
|
120
|
+
issuer: NonNullable<typeof source.automatic_tax>["liability"],
|
|
121
|
+
) =>
|
|
122
|
+
issuer?.type === "account"
|
|
123
|
+
? { type: "account" as const, account: identity(issuer.account) }
|
|
124
|
+
: { type: "self" as const };
|
|
125
|
+
if (source.automatic_tax !== undefined) {
|
|
126
|
+
out.automatic_tax = { enabled: source.automatic_tax.enabled };
|
|
127
|
+
if (source.automatic_tax.liability != null)
|
|
128
|
+
out.automatic_tax.liability = mapIssuer(source.automatic_tax.liability);
|
|
129
|
+
}
|
|
130
|
+
if (source.default_tax_rates != null)
|
|
131
|
+
out.default_tax_rates = source.default_tax_rates.length
|
|
132
|
+
? source.default_tax_rates.map(identity)
|
|
133
|
+
: "";
|
|
134
|
+
const invoice = source.invoice_settings;
|
|
135
|
+
if (invoice !== null) {
|
|
136
|
+
const mapped: NonNullable<Settings["invoice_settings"]> = {};
|
|
137
|
+
if (invoice.account_tax_ids !== null)
|
|
138
|
+
mapped.account_tax_ids = invoice.account_tax_ids.length
|
|
139
|
+
? invoice.account_tax_ids.map(identity)
|
|
140
|
+
: "";
|
|
141
|
+
if (invoice.custom_fields !== null)
|
|
142
|
+
mapped.custom_fields = invoice.custom_fields.length
|
|
143
|
+
? invoice.custom_fields.map(({ name, value }) => ({ name, value }))
|
|
144
|
+
: "";
|
|
145
|
+
if (invoice.issuer !== null) mapped.issuer = mapIssuer(invoice.issuer);
|
|
146
|
+
if (invoice.days_until_due !== null) {
|
|
147
|
+
if (context.effectiveCollectionMethod !== "send_invoice")
|
|
148
|
+
return failure("incompatible_invoice_due_days");
|
|
149
|
+
mapped.days_until_due = invoice.days_until_due;
|
|
150
|
+
}
|
|
151
|
+
if (invoice.description !== null) mapped.description = invoice.description;
|
|
152
|
+
if (invoice.footer !== null) mapped.footer = invoice.footer;
|
|
153
|
+
if (Object.keys(mapped).length) out.invoice_settings = mapped;
|
|
154
|
+
}
|
|
155
|
+
if (source.description !== null) out.description = source.description;
|
|
156
|
+
if (source.metadata !== null && Object.keys(source.metadata).length)
|
|
157
|
+
out.metadata = source.metadata;
|
|
158
|
+
return {
|
|
159
|
+
status: "projected",
|
|
160
|
+
settings: out,
|
|
161
|
+
requiresProviderQualification: true,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const id = (prefix: string) =>
|
|
4
|
+
z
|
|
5
|
+
.string()
|
|
6
|
+
.max(255)
|
|
7
|
+
.regex(new RegExp(`^${prefix}_[A-Za-z0-9_]+$`));
|
|
8
|
+
export const reference = (prefix: string, object: string) =>
|
|
9
|
+
z.union([
|
|
10
|
+
id(prefix),
|
|
11
|
+
z
|
|
12
|
+
.object({
|
|
13
|
+
id: id(prefix),
|
|
14
|
+
object: z.literal(object),
|
|
15
|
+
deleted: z.literal(false).optional(),
|
|
16
|
+
livemode: z.boolean().optional(),
|
|
17
|
+
customer: z
|
|
18
|
+
.union([
|
|
19
|
+
id("cus"),
|
|
20
|
+
z
|
|
21
|
+
.object({
|
|
22
|
+
id: id("cus"),
|
|
23
|
+
object: z.literal("customer"),
|
|
24
|
+
deleted: z.literal(false).optional(),
|
|
25
|
+
livemode: z.boolean().optional(),
|
|
26
|
+
customer_account: id("acct").nullable().optional(),
|
|
27
|
+
})
|
|
28
|
+
.passthrough(),
|
|
29
|
+
])
|
|
30
|
+
.nullable()
|
|
31
|
+
.optional(),
|
|
32
|
+
customer_account: id("acct").nullable().optional(),
|
|
33
|
+
})
|
|
34
|
+
.passthrough(),
|
|
35
|
+
]);
|
|
36
|
+
const account = reference("acct", "account");
|
|
37
|
+
const issuer = z.discriminatedUnion("type", [
|
|
38
|
+
z.object({ type: z.literal("self"), account: z.null().optional() }).strict(),
|
|
39
|
+
z.object({ type: z.literal("account"), account }).strict(),
|
|
40
|
+
]);
|
|
41
|
+
const invoiceTaxId = z.union([
|
|
42
|
+
id("txi"),
|
|
43
|
+
z
|
|
44
|
+
.object({
|
|
45
|
+
id: id("txi"),
|
|
46
|
+
object: z.literal("tax_id"),
|
|
47
|
+
deleted: z.literal(false).optional(),
|
|
48
|
+
livemode: z.boolean(),
|
|
49
|
+
customer: z.null().optional(),
|
|
50
|
+
customer_account: z.null().optional(),
|
|
51
|
+
owner: z.discriminatedUnion("type", [
|
|
52
|
+
z
|
|
53
|
+
.object({
|
|
54
|
+
type: z.literal("self"),
|
|
55
|
+
customer_account: z.null(),
|
|
56
|
+
account: z.null().optional(),
|
|
57
|
+
customer: z.null().optional(),
|
|
58
|
+
})
|
|
59
|
+
.strict(),
|
|
60
|
+
z
|
|
61
|
+
.object({
|
|
62
|
+
type: z.literal("account"),
|
|
63
|
+
account,
|
|
64
|
+
customer_account: z.null(),
|
|
65
|
+
customer: z.null().optional(),
|
|
66
|
+
})
|
|
67
|
+
.strict(),
|
|
68
|
+
]),
|
|
69
|
+
})
|
|
70
|
+
.passthrough(),
|
|
71
|
+
]);
|
|
72
|
+
export const taxRate = z
|
|
73
|
+
.object({
|
|
74
|
+
id: id("txr"),
|
|
75
|
+
object: z.literal("tax_rate"),
|
|
76
|
+
deleted: z.literal(false).optional(),
|
|
77
|
+
livemode: z.boolean(),
|
|
78
|
+
})
|
|
79
|
+
.passthrough();
|
|
80
|
+
const string = z.string().max(5000);
|
|
81
|
+
export const settingsInput = z
|
|
82
|
+
.object({
|
|
83
|
+
context: z
|
|
84
|
+
.object({
|
|
85
|
+
customerAccountId: id("acct"),
|
|
86
|
+
customerId: id("cus"),
|
|
87
|
+
livemode: z.boolean(),
|
|
88
|
+
effectiveCollectionMethod: z.enum([
|
|
89
|
+
"charge_automatically",
|
|
90
|
+
"send_invoice",
|
|
91
|
+
]),
|
|
92
|
+
effectiveAutomaticTax: z.boolean(),
|
|
93
|
+
effectiveInvoiceIssuer: issuer,
|
|
94
|
+
})
|
|
95
|
+
.strict(),
|
|
96
|
+
settings: z
|
|
97
|
+
.object({
|
|
98
|
+
application_fee_percent: z.null(),
|
|
99
|
+
billing_thresholds: z.null(),
|
|
100
|
+
collection_method: z
|
|
101
|
+
.enum(["charge_automatically", "send_invoice"])
|
|
102
|
+
.nullable(),
|
|
103
|
+
default_payment_method: reference("pm", "payment_method").nullable(),
|
|
104
|
+
on_behalf_of: account.nullable(),
|
|
105
|
+
transfer_data: z
|
|
106
|
+
.object({
|
|
107
|
+
destination: account,
|
|
108
|
+
amount_percent: z
|
|
109
|
+
.number()
|
|
110
|
+
.min(0)
|
|
111
|
+
.max(100)
|
|
112
|
+
.multipleOf(0.01)
|
|
113
|
+
.nullable(),
|
|
114
|
+
})
|
|
115
|
+
.strict()
|
|
116
|
+
.nullable(),
|
|
117
|
+
automatic_tax: z
|
|
118
|
+
.object({
|
|
119
|
+
enabled: z.boolean(),
|
|
120
|
+
liability: issuer.nullable().optional(),
|
|
121
|
+
disabled_reason: z.null().optional(),
|
|
122
|
+
})
|
|
123
|
+
.strict()
|
|
124
|
+
.optional(),
|
|
125
|
+
default_tax_rates: z.array(taxRate).max(20).nullable().optional(),
|
|
126
|
+
invoice_settings: z
|
|
127
|
+
.object({
|
|
128
|
+
account_tax_ids: z.array(invoiceTaxId).max(20).nullable(),
|
|
129
|
+
custom_fields: z
|
|
130
|
+
.array(
|
|
131
|
+
z
|
|
132
|
+
.object({
|
|
133
|
+
name: z.string().max(40),
|
|
134
|
+
value: z.string().max(140),
|
|
135
|
+
})
|
|
136
|
+
.strict(),
|
|
137
|
+
)
|
|
138
|
+
.max(4)
|
|
139
|
+
.nullable(),
|
|
140
|
+
days_until_due: z.number().int().nonnegative().max(3650).nullable(),
|
|
141
|
+
description: string.nullable(),
|
|
142
|
+
footer: string.nullable(),
|
|
143
|
+
issuer: issuer.nullable(),
|
|
144
|
+
})
|
|
145
|
+
.strict()
|
|
146
|
+
.nullable(),
|
|
147
|
+
description: string.nullable(),
|
|
148
|
+
metadata: z
|
|
149
|
+
.record(z.string().max(40), z.string().min(1).max(500))
|
|
150
|
+
.nullable(),
|
|
151
|
+
})
|
|
152
|
+
.strict(),
|
|
153
|
+
})
|
|
154
|
+
.strict();
|
|
@@ -6,10 +6,16 @@ const id = (prefix: string) =>
|
|
|
6
6
|
.max(255)
|
|
7
7
|
.regex(new RegExp(`^${prefix}_[A-Za-z0-9_]+$`));
|
|
8
8
|
const integer = z.number().int().positive().max(Number.MAX_SAFE_INTEGER);
|
|
9
|
+
// Stripe allows caller-supplied product IDs, including IDs without prod_.
|
|
10
|
+
const productId = z.string().min(1).max(255).regex(/\S/);
|
|
9
11
|
const product = z.union([
|
|
10
|
-
|
|
12
|
+
productId,
|
|
11
13
|
z
|
|
12
|
-
.object({
|
|
14
|
+
.object({
|
|
15
|
+
id: productId,
|
|
16
|
+
object: z.literal("product"),
|
|
17
|
+
deleted: z.literal(false).optional(),
|
|
18
|
+
})
|
|
13
19
|
.passthrough(),
|
|
14
20
|
]);
|
|
15
21
|
export const renewalPrice = z
|
|
@@ -38,9 +44,9 @@ export const renewalExpected = z
|
|
|
38
44
|
subscriptionId: id("sub"),
|
|
39
45
|
itemId: id("si"),
|
|
40
46
|
currentPriceId: id("price"),
|
|
41
|
-
currentProductId:
|
|
47
|
+
currentProductId: productId,
|
|
42
48
|
targetPriceId: id("price"),
|
|
43
|
-
targetProductId:
|
|
49
|
+
targetProductId: productId,
|
|
44
50
|
periodStart: integer,
|
|
45
51
|
periodEnd: integer,
|
|
46
52
|
livemode: z.boolean(),
|