@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.
Files changed (103) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAggregate/configure.d.ts +35 -0
  4. package/dist/client/billing/renewalAggregate/configure.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAggregate/configure.js +96 -0
  6. package/dist/client/billing/renewalAggregate/configure.js.map +1 -0
  7. package/dist/client/billing/renewalAggregate/index.d.ts +3 -0
  8. package/dist/client/billing/renewalAggregate/index.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAggregate/index.js +3 -0
  10. package/dist/client/billing/renewalAggregate/index.js.map +1 -0
  11. package/dist/client/billing/renewalAggregate/phaseContext.d.ts +176 -0
  12. package/dist/client/billing/renewalAggregate/phaseContext.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAggregate/phaseContext.js +112 -0
  14. package/dist/client/billing/renewalAggregate/phaseContext.js.map +1 -0
  15. package/dist/client/billing/renewalAggregate/phases.d.ts +7 -0
  16. package/dist/client/billing/renewalAggregate/phases.d.ts.map +1 -0
  17. package/dist/client/billing/renewalAggregate/phases.js +192 -0
  18. package/dist/client/billing/renewalAggregate/phases.js.map +1 -0
  19. package/dist/client/billing/renewalAggregate/preflight.d.ts +594 -0
  20. package/dist/client/billing/renewalAggregate/preflight.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAggregate/preflight.js +164 -0
  22. package/dist/client/billing/renewalAggregate/preflight.js.map +1 -0
  23. package/dist/client/billing/renewalAggregate/profile.d.ts +8 -0
  24. package/dist/client/billing/renewalAggregate/profile.d.ts.map +1 -0
  25. package/dist/client/billing/renewalAggregate/profile.js +60 -0
  26. package/dist/client/billing/renewalAggregate/profile.js.map +1 -0
  27. package/dist/client/billing/renewalAggregate/receipt.d.ts +40 -0
  28. package/dist/client/billing/renewalAggregate/receipt.d.ts.map +1 -0
  29. package/dist/client/billing/renewalAggregate/receipt.js +62 -0
  30. package/dist/client/billing/renewalAggregate/receipt.js.map +1 -0
  31. package/dist/client/billing/renewalAggregate/shared.d.ts +51 -0
  32. package/dist/client/billing/renewalAggregate/shared.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAggregate/shared.js +87 -0
  34. package/dist/client/billing/renewalAggregate/shared.js.map +1 -0
  35. package/dist/client/billing/renewalAggregate/sourceDiscounts.d.ts +42 -0
  36. package/dist/client/billing/renewalAggregate/sourceDiscounts.d.ts.map +1 -0
  37. package/dist/client/billing/renewalAggregate/sourceDiscounts.js +90 -0
  38. package/dist/client/billing/renewalAggregate/sourceDiscounts.js.map +1 -0
  39. package/dist/client/billing/renewalAggregate/sourceSettings.d.ts +136 -0
  40. package/dist/client/billing/renewalAggregate/sourceSettings.d.ts.map +1 -0
  41. package/dist/client/billing/renewalAggregate/sourceSettings.js +82 -0
  42. package/dist/client/billing/renewalAggregate/sourceSettings.js.map +1 -0
  43. package/dist/client/billing/renewalAggregate/taxRetention.d.ts +8 -0
  44. package/dist/client/billing/renewalAggregate/taxRetention.d.ts.map +1 -0
  45. package/dist/client/billing/renewalAggregate/taxRetention.js +23 -0
  46. package/dist/client/billing/renewalAggregate/taxRetention.js.map +1 -0
  47. package/dist/client/billing/renewalPriceReplacement.d.ts +2 -0
  48. package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
  49. package/dist/client/billing/renewalProjection/discounts.d.ts +20 -0
  50. package/dist/client/billing/renewalProjection/discounts.d.ts.map +1 -0
  51. package/dist/client/billing/renewalProjection/discounts.js +121 -0
  52. package/dist/client/billing/renewalProjection/discounts.js.map +1 -0
  53. package/dist/client/billing/renewalProjection/itemSettings.d.ts +14 -0
  54. package/dist/client/billing/renewalProjection/itemSettings.d.ts.map +1 -0
  55. package/dist/client/billing/renewalProjection/itemSettings.js +41 -0
  56. package/dist/client/billing/renewalProjection/itemSettings.js.map +1 -0
  57. package/dist/client/billing/renewalProjection/settings.d.ts +18 -0
  58. package/dist/client/billing/renewalProjection/settings.d.ts.map +1 -0
  59. package/dist/client/billing/renewalProjection/settings.js +132 -0
  60. package/dist/client/billing/renewalProjection/settings.js.map +1 -0
  61. package/dist/client/billing/renewalProjection/settingsSchema.d.ts +200 -0
  62. package/dist/client/billing/renewalProjection/settingsSchema.d.ts.map +1 -0
  63. package/dist/client/billing/renewalProjection/settingsSchema.js +150 -0
  64. package/dist/client/billing/renewalProjection/settingsSchema.js.map +1 -0
  65. package/dist/client/billing/renewalReplacementSchema.d.ts +2 -0
  66. package/dist/client/billing/renewalReplacementSchema.d.ts.map +1 -1
  67. package/dist/client/billing/renewalReplacementSchema.js +10 -4
  68. package/dist/client/billing/renewalReplacementSchema.js.map +1 -1
  69. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
  70. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  71. package/dist/component/balanceFunding/mutations.d.ts +43 -43
  72. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  73. package/dist/component/payoutRelease/queries.d.ts +2 -2
  74. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  75. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  76. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  77. package/docs/renewal-phase-assembly.md +180 -0
  78. package/docs/renewal-preservation-mappings.md +94 -0
  79. package/docs/renewal-price-replacements.md +7 -0
  80. package/package.json +1 -1
  81. package/src/client/billing/renewal-discount-projection.test.ts +207 -0
  82. package/src/client/billing/renewal-item-settings-projection.test.ts +79 -0
  83. package/src/client/billing/renewal-price-replacement.test.ts +46 -0
  84. package/src/client/billing/renewal-settings-projection.test.ts +300 -0
  85. package/src/client/billing/renewalAggregate/configure.test.ts +372 -0
  86. package/src/client/billing/renewalAggregate/configure.ts +164 -0
  87. package/src/client/billing/renewalAggregate/index.ts +2 -0
  88. package/src/client/billing/renewalAggregate/phaseContext.ts +144 -0
  89. package/src/client/billing/renewalAggregate/phases.ts +247 -0
  90. package/src/client/billing/renewalAggregate/preflight.test.ts +233 -0
  91. package/src/client/billing/renewalAggregate/preflight.ts +211 -0
  92. package/src/client/billing/renewalAggregate/profile.ts +76 -0
  93. package/src/client/billing/renewalAggregate/receipt.ts +76 -0
  94. package/src/client/billing/renewalAggregate/shared.ts +106 -0
  95. package/src/client/billing/renewalAggregate/sourceAndTaxRetention.test.ts +247 -0
  96. package/src/client/billing/renewalAggregate/sourceDiscounts.ts +112 -0
  97. package/src/client/billing/renewalAggregate/sourceSettings.ts +88 -0
  98. package/src/client/billing/renewalAggregate/taxRetention.ts +28 -0
  99. package/src/client/billing/renewalProjection/discounts.ts +152 -0
  100. package/src/client/billing/renewalProjection/itemSettings.ts +55 -0
  101. package/src/client/billing/renewalProjection/settings.ts +163 -0
  102. package/src/client/billing/renewalProjection/settingsSchema.ts +154 -0
  103. 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
- id("prod"),
12
+ productId,
11
13
  z
12
- .object({ id: id("prod"), deleted: z.literal(false).optional() })
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: id("prod"),
47
+ currentProductId: productId,
42
48
  targetPriceId: id("price"),
43
- targetProductId: id("prod"),
49
+ targetProductId: productId,
44
50
  periodStart: integer,
45
51
  periodEnd: integer,
46
52
  livemode: z.boolean(),