@fantastic.dev/better-stripe 0.79.1 → 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 +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalPriceReplacement.d.ts +133 -0
- package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -0
- package/dist/client/billing/renewalPriceReplacement.js +90 -0
- package/dist/client/billing/renewalPriceReplacement.js.map +1 -0
- 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 +126 -0
- package/dist/client/billing/renewalReplacementSchema.d.ts.map +1 -0
- package/dist/client/billing/renewalReplacementSchema.js +127 -0
- package/dist/client/billing/renewalReplacementSchema.js.map +1 -0
- package/dist/client/billing/renewalReplacementSnapshot.d.ts +3 -0
- package/dist/client/billing/renewalReplacementSnapshot.d.ts.map +1 -0
- package/dist/client/billing/renewalReplacementSnapshot.js +69 -0
- package/dist/client/billing/renewalReplacementSnapshot.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/component/balanceFunding/mutations.d.ts +16 -16
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/docs/client-api.md +5 -0
- package/docs/renewal-preservation-mappings.md +94 -0
- package/docs/renewal-price-replacements.md +191 -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 +503 -0
- package/src/client/billing/renewal-settings-projection.test.ts +300 -0
- package/src/client/billing/renewalPriceReplacement.ts +127 -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 +135 -0
- package/src/client/billing/renewalReplacementSnapshot.ts +85 -0
- package/src/client/index.ts +6 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import * as projection from "./renewalProjection/itemSettings.js";
|
|
3
|
+
|
|
4
|
+
const input = () => ({
|
|
5
|
+
effectiveAutomaticTax: false,
|
|
6
|
+
livemode: false,
|
|
7
|
+
settings: { billing_thresholds: null, tax_rates: [], metadata: null },
|
|
8
|
+
});
|
|
9
|
+
it("preserves explicit empty item rates as a clear override", () => {
|
|
10
|
+
expect(projection.projectRenewalItemSettings(input())).toEqual({
|
|
11
|
+
status: "projected",
|
|
12
|
+
settings: { tax_rates: "" },
|
|
13
|
+
requiresProviderQualification: true,
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
it("keeps absent tax overrides absent and null metadata without actions", () => {
|
|
17
|
+
expect(
|
|
18
|
+
projection.projectRenewalItemSettings({
|
|
19
|
+
...input(),
|
|
20
|
+
settings: { billing_thresholds: null, metadata: null },
|
|
21
|
+
}),
|
|
22
|
+
).toMatchObject({ status: "projected", settings: {} });
|
|
23
|
+
});
|
|
24
|
+
it("maps ordered item tax IDs and exact metadata without changing reads", () => {
|
|
25
|
+
const value = {
|
|
26
|
+
...input(),
|
|
27
|
+
settings: {
|
|
28
|
+
billing_thresholds: null,
|
|
29
|
+
tax_rates: ["first", "second"].map((name) => ({
|
|
30
|
+
id: `txr_${name}`,
|
|
31
|
+
object: "tax_rate",
|
|
32
|
+
livemode: false,
|
|
33
|
+
})),
|
|
34
|
+
metadata: { original: "kept" },
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const before = structuredClone(value);
|
|
38
|
+
expect(projection.projectRenewalItemSettings(value)).toMatchObject({
|
|
39
|
+
status: "projected",
|
|
40
|
+
settings: {
|
|
41
|
+
tax_rates: ["txr_first", "txr_second"],
|
|
42
|
+
metadata: { original: "kept" },
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
expect(value).toEqual(before);
|
|
46
|
+
});
|
|
47
|
+
it.each([
|
|
48
|
+
{ billing_thresholds: { usage_gte: 100 } },
|
|
49
|
+
{ metadata: { unset: "" } },
|
|
50
|
+
{ tax_rates: [{ id: "txr_live", object: "tax_rate", livemode: true }] },
|
|
51
|
+
{
|
|
52
|
+
tax_rates: [
|
|
53
|
+
{ id: "txr_deleted", object: "tax_rate", livemode: false, deleted: true },
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
{ unknown_future_setting: true },
|
|
57
|
+
])("rejects unsupported item state %j", (extra) => {
|
|
58
|
+
expect(
|
|
59
|
+
projection.projectRenewalItemSettings({
|
|
60
|
+
...input(),
|
|
61
|
+
settings: {
|
|
62
|
+
...input().settings,
|
|
63
|
+
...extra,
|
|
64
|
+
},
|
|
65
|
+
}).status,
|
|
66
|
+
).toBe("unsupported");
|
|
67
|
+
});
|
|
68
|
+
it("rejects item rates under inherited effective automatic tax", () => {
|
|
69
|
+
expect(
|
|
70
|
+
projection.projectRenewalItemSettings({
|
|
71
|
+
...input(),
|
|
72
|
+
effectiveAutomaticTax: true,
|
|
73
|
+
settings: {
|
|
74
|
+
...input().settings,
|
|
75
|
+
tax_rates: [{ id: "txr_manual", object: "tax_rate", livemode: false }],
|
|
76
|
+
},
|
|
77
|
+
}).status,
|
|
78
|
+
).toBe("unsupported");
|
|
79
|
+
});
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import * as sdk from "../index.js";
|
|
3
|
+
|
|
4
|
+
const expected = {
|
|
5
|
+
customerAccountId: "acct_buyer",
|
|
6
|
+
subscriptionId: "sub_original",
|
|
7
|
+
itemId: "si_original",
|
|
8
|
+
currentPriceId: "price_original",
|
|
9
|
+
currentProductId: "prod_original",
|
|
10
|
+
targetPriceId: "price_target",
|
|
11
|
+
targetProductId: "prod_other",
|
|
12
|
+
periodStart: 1700000000,
|
|
13
|
+
periodEnd: 1702592000,
|
|
14
|
+
livemode: false,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
it.each(["current", "target"] as const)(
|
|
18
|
+
"accepts provider-valid custom %s product IDs",
|
|
19
|
+
(which) => {
|
|
20
|
+
const args = structuredClone(input());
|
|
21
|
+
if (which === "current") {
|
|
22
|
+
args.expected.currentProductId = "custom-original-catalog-item";
|
|
23
|
+
args.subscription.items.data[0].price.product =
|
|
24
|
+
args.expected.currentProductId;
|
|
25
|
+
} else {
|
|
26
|
+
args.expected.targetProductId = "custom-target-catalog-item";
|
|
27
|
+
args.targetPrice.product = args.expected.targetProductId;
|
|
28
|
+
}
|
|
29
|
+
expect(sdk.prepareRenewalPriceReplacement(args).status).toBe("prepared");
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
function price(id = "price_original", product = "prod_original") {
|
|
33
|
+
return {
|
|
34
|
+
id,
|
|
35
|
+
product,
|
|
36
|
+
object: "price",
|
|
37
|
+
active: true,
|
|
38
|
+
livemode: false,
|
|
39
|
+
type: "recurring",
|
|
40
|
+
currency: "usd",
|
|
41
|
+
billing_scheme: "per_unit",
|
|
42
|
+
recurring: { interval: "month", interval_count: 1, usage_type: "licensed" },
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function input() {
|
|
46
|
+
return {
|
|
47
|
+
expected,
|
|
48
|
+
key: "renewal:original-operation",
|
|
49
|
+
subscription: {
|
|
50
|
+
id: "sub_original",
|
|
51
|
+
object: "subscription",
|
|
52
|
+
customer_account: "acct_buyer",
|
|
53
|
+
status: "active",
|
|
54
|
+
currency: "usd",
|
|
55
|
+
livemode: false,
|
|
56
|
+
schedule: null,
|
|
57
|
+
cancel_at: null,
|
|
58
|
+
cancel_at_period_end: false,
|
|
59
|
+
pause_collection: null,
|
|
60
|
+
pending_update: null,
|
|
61
|
+
default_source: null,
|
|
62
|
+
payment_settings: {
|
|
63
|
+
payment_method_options: null,
|
|
64
|
+
payment_method_types: null,
|
|
65
|
+
save_default_payment_method: "off",
|
|
66
|
+
},
|
|
67
|
+
application_fee_percent: null,
|
|
68
|
+
trial_end: 1690000000,
|
|
69
|
+
items: {
|
|
70
|
+
has_more: false,
|
|
71
|
+
data: [
|
|
72
|
+
{
|
|
73
|
+
id: "si_original",
|
|
74
|
+
object: "subscription_item",
|
|
75
|
+
subscription: "sub_original",
|
|
76
|
+
current_period_start: expected.periodStart,
|
|
77
|
+
current_period_end: expected.periodEnd,
|
|
78
|
+
quantity: 3,
|
|
79
|
+
price: price(),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
metadata: { original: "keep" },
|
|
84
|
+
},
|
|
85
|
+
targetPrice: price("price_target", "prod_other"),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
it("accepts an undeleted expanded custom product reference", () => {
|
|
90
|
+
const args = structuredClone(input());
|
|
91
|
+
args.expected.targetProductId = "custom-target";
|
|
92
|
+
expect(
|
|
93
|
+
sdk.prepareRenewalPriceReplacement({
|
|
94
|
+
...args,
|
|
95
|
+
targetPrice: {
|
|
96
|
+
...args.targetPrice,
|
|
97
|
+
product: { id: "custom-target", object: "product" },
|
|
98
|
+
},
|
|
99
|
+
}).status,
|
|
100
|
+
).toBe("prepared");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it.each(["coupon", "customer"])(
|
|
104
|
+
"rejects a wrong expanded product object %s",
|
|
105
|
+
(object) => {
|
|
106
|
+
const args = input();
|
|
107
|
+
expect(() =>
|
|
108
|
+
sdk.prepareRenewalPriceReplacement({
|
|
109
|
+
...args,
|
|
110
|
+
targetPrice: {
|
|
111
|
+
...args.targetPrice,
|
|
112
|
+
product: { id: args.expected.targetProductId, object },
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
).toThrow();
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
it("exports read-only renewal preparation without exposing a schedule writer", () => {
|
|
120
|
+
expect(sdk).toHaveProperty(
|
|
121
|
+
"prepareRenewalPriceReplacement",
|
|
122
|
+
expect.any(Function),
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("prepares an exact cross-product renewal snapshot without changing the paid item", () => {
|
|
127
|
+
const args = input();
|
|
128
|
+
const before = structuredClone(args);
|
|
129
|
+
const result = sdk.prepareRenewalPriceReplacement(args);
|
|
130
|
+
expect(result).toMatchObject({
|
|
131
|
+
version: 1,
|
|
132
|
+
status: "prepared",
|
|
133
|
+
scope: "platform",
|
|
134
|
+
expected,
|
|
135
|
+
quantity: 3,
|
|
136
|
+
requestIdentity: { key: args.key, binding: expect.any(String) },
|
|
137
|
+
preservation: "requires_schedule_validation",
|
|
138
|
+
});
|
|
139
|
+
expect(result.providerSnapshot.subscription.items.data[0].price.id).toBe(
|
|
140
|
+
"price_original",
|
|
141
|
+
);
|
|
142
|
+
expect(result.providerSnapshot.targetPrice.id).toBe("price_target");
|
|
143
|
+
expect(args).toEqual(before);
|
|
144
|
+
args.subscription.metadata.original = "changed after preparation";
|
|
145
|
+
expect(result.providerSnapshot.subscription).toMatchObject({
|
|
146
|
+
metadata: { original: "keep" },
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("binds material source settings and complete target data with stable object ordering", () => {
|
|
151
|
+
const args = input();
|
|
152
|
+
const first = sdk.prepareRenewalPriceReplacement(args);
|
|
153
|
+
const reordered = {
|
|
154
|
+
...args,
|
|
155
|
+
subscription: Object.fromEntries(
|
|
156
|
+
Object.entries(args.subscription).reverse(),
|
|
157
|
+
),
|
|
158
|
+
};
|
|
159
|
+
expect(sdk.prepareRenewalPriceReplacement(reordered).requestIdentity).toEqual(
|
|
160
|
+
first.requestIdentity,
|
|
161
|
+
);
|
|
162
|
+
args.subscription.metadata.original = "different";
|
|
163
|
+
expect(
|
|
164
|
+
sdk.prepareRenewalPriceReplacement(args).requestIdentity.binding,
|
|
165
|
+
).not.toBe(first.requestIdentity.binding);
|
|
166
|
+
args.targetPrice.recurring.interval_count = 2;
|
|
167
|
+
expect(
|
|
168
|
+
sdk.prepareRenewalPriceReplacement(args).requestIdentity.binding,
|
|
169
|
+
).not.toBe(first.requestIdentity.binding);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it.each([
|
|
173
|
+
["customerAccountId", "acct_foreign"],
|
|
174
|
+
["subscriptionId", "sub_foreign"],
|
|
175
|
+
["itemId", "si_foreign"],
|
|
176
|
+
["currentPriceId", "price_foreign"],
|
|
177
|
+
["currentProductId", "prod_foreign"],
|
|
178
|
+
["targetPriceId", "price_foreign"],
|
|
179
|
+
["targetProductId", "prod_foreign"],
|
|
180
|
+
["periodStart", 1699999999],
|
|
181
|
+
["periodEnd", 1702592001],
|
|
182
|
+
["livemode", true],
|
|
183
|
+
] as const)(
|
|
184
|
+
"rejects a mismatched original/target selector %s",
|
|
185
|
+
(field, value) => {
|
|
186
|
+
const args = input();
|
|
187
|
+
expect(() =>
|
|
188
|
+
sdk.prepareRenewalPriceReplacement({
|
|
189
|
+
...args,
|
|
190
|
+
expected: { ...expected, [field]: value },
|
|
191
|
+
}),
|
|
192
|
+
).toThrow();
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
it.each([
|
|
197
|
+
["schedule", "sub_sched_unrelated"],
|
|
198
|
+
["cancel_at_period_end", true],
|
|
199
|
+
["cancel_at", expected.periodEnd + 100],
|
|
200
|
+
["pause_collection", { behavior: "void" }],
|
|
201
|
+
["pending_update", {}],
|
|
202
|
+
["default_source", "card_legacy"],
|
|
203
|
+
["application_fee_percent", 0],
|
|
204
|
+
["status", "trialing"],
|
|
205
|
+
["payment_settings", { payment_method_types: ["card"] }],
|
|
206
|
+
] as const)(
|
|
207
|
+
"rejects unsupported pre-attach state %s without a writer",
|
|
208
|
+
(field, value) => {
|
|
209
|
+
const args = input();
|
|
210
|
+
expect(() =>
|
|
211
|
+
sdk.prepareRenewalPriceReplacement({
|
|
212
|
+
...args,
|
|
213
|
+
subscription: { ...args.subscription, [field]: value },
|
|
214
|
+
}),
|
|
215
|
+
).toThrow();
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
it("rejects incomplete/multi-item reads and an unexpanded original price", () => {
|
|
220
|
+
const args = input();
|
|
221
|
+
expect(() =>
|
|
222
|
+
sdk.prepareRenewalPriceReplacement({
|
|
223
|
+
...args,
|
|
224
|
+
subscription: {
|
|
225
|
+
...args.subscription,
|
|
226
|
+
items: { ...args.subscription.items, has_more: true },
|
|
227
|
+
},
|
|
228
|
+
}),
|
|
229
|
+
).toThrow();
|
|
230
|
+
expect(() =>
|
|
231
|
+
sdk.prepareRenewalPriceReplacement({
|
|
232
|
+
...args,
|
|
233
|
+
subscription: {
|
|
234
|
+
...args.subscription,
|
|
235
|
+
items: {
|
|
236
|
+
has_more: false,
|
|
237
|
+
data: [
|
|
238
|
+
...args.subscription.items.data,
|
|
239
|
+
...args.subscription.items.data,
|
|
240
|
+
],
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
}),
|
|
244
|
+
).toThrow();
|
|
245
|
+
expect(() =>
|
|
246
|
+
sdk.prepareRenewalPriceReplacement({
|
|
247
|
+
...args,
|
|
248
|
+
subscription: {
|
|
249
|
+
...args.subscription,
|
|
250
|
+
items: {
|
|
251
|
+
has_more: false,
|
|
252
|
+
data: [
|
|
253
|
+
{ ...args.subscription.items.data[0], price: "price_original" },
|
|
254
|
+
],
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
}),
|
|
258
|
+
).toThrow();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it.each(["", " ", "bad\nkey", "a".repeat(256)])(
|
|
262
|
+
"requires an explicit valid retained key %j",
|
|
263
|
+
(key) => {
|
|
264
|
+
expect(() =>
|
|
265
|
+
sdk.prepareRenewalPriceReplacement({ ...input(), key }),
|
|
266
|
+
).toThrow();
|
|
267
|
+
},
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
it("rejects an unexpected connected scope instead of silently assuming platform scope", () => {
|
|
271
|
+
expect(() =>
|
|
272
|
+
sdk.prepareRenewalPriceReplacement({
|
|
273
|
+
...input(),
|
|
274
|
+
connectedAccountId: "acct_other",
|
|
275
|
+
} as Parameters<typeof sdk.prepareRenewalPriceReplacement>[0]),
|
|
276
|
+
).toThrow(/Unrecognized|scope/);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("rejects sparse JSON arrays instead of silently converting holes to null", () => {
|
|
280
|
+
const args = input();
|
|
281
|
+
expect(() =>
|
|
282
|
+
sdk.prepareRenewalPriceReplacement({
|
|
283
|
+
...args,
|
|
284
|
+
subscription: {
|
|
285
|
+
...args.subscription,
|
|
286
|
+
metadata: { sparse: new Array(1) },
|
|
287
|
+
},
|
|
288
|
+
}),
|
|
289
|
+
).toThrow(/JSON/);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("rejects enumerable accessors without executing them while copying provider data", () => {
|
|
293
|
+
const args = input();
|
|
294
|
+
let evaluated = false;
|
|
295
|
+
Object.defineProperty(args.subscription, "metadata", {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
get() {
|
|
298
|
+
evaluated = true;
|
|
299
|
+
return { original: "getter" };
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
expect(() => sdk.prepareRenewalPriceReplacement(args)).toThrow(/JSON/);
|
|
303
|
+
expect(evaluated).toBe(false);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("accepts a full provider read with an empty billing_schedules list", () => {
|
|
307
|
+
const args = input();
|
|
308
|
+
expect(
|
|
309
|
+
sdk.prepareRenewalPriceReplacement({
|
|
310
|
+
...args,
|
|
311
|
+
subscription: { ...args.subscription, billing_schedules: [] },
|
|
312
|
+
}).status,
|
|
313
|
+
).toBe("prepared");
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("retains a provider default-null payment setting without certifying preservation", () => {
|
|
317
|
+
const args = input();
|
|
318
|
+
expect(
|
|
319
|
+
sdk.prepareRenewalPriceReplacement({
|
|
320
|
+
...args,
|
|
321
|
+
subscription: {
|
|
322
|
+
...args.subscription,
|
|
323
|
+
payment_settings: {
|
|
324
|
+
...args.subscription.payment_settings,
|
|
325
|
+
save_default_payment_method: null,
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
}),
|
|
329
|
+
).toMatchObject({
|
|
330
|
+
preservation: "requires_schedule_validation",
|
|
331
|
+
providerSnapshot: {
|
|
332
|
+
subscription: { payment_settings: { save_default_payment_method: null } },
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("bounds array traversal before inspecting its elements", () => {
|
|
338
|
+
const args = input();
|
|
339
|
+
let inspected = 0;
|
|
340
|
+
const large = Array.from({ length: 10_001 }, () => ({}));
|
|
341
|
+
for (const value of large)
|
|
342
|
+
Object.defineProperty(value, "data", {
|
|
343
|
+
enumerable: true,
|
|
344
|
+
get() {
|
|
345
|
+
inspected++;
|
|
346
|
+
return "x";
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
expect(() =>
|
|
350
|
+
sdk.prepareRenewalPriceReplacement({
|
|
351
|
+
...args,
|
|
352
|
+
subscription: { ...args.subscription, metadata: { large } },
|
|
353
|
+
}),
|
|
354
|
+
).toThrow(/too large|budget/);
|
|
355
|
+
expect(inspected).toBe(0);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
const billingCustomer = {
|
|
359
|
+
id: "cus_buyer",
|
|
360
|
+
object: "customer",
|
|
361
|
+
customer_account: "acct_buyer",
|
|
362
|
+
livemode: false,
|
|
363
|
+
discount: null,
|
|
364
|
+
invoice_settings: { default_payment_method: null },
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
it("binds the correlated billing-customer bridge without claiming discount hierarchy proof", () => {
|
|
368
|
+
const args = input();
|
|
369
|
+
const result = sdk.prepareRenewalPriceReplacement({
|
|
370
|
+
...args,
|
|
371
|
+
subscription: { ...args.subscription, customer: "cus_buyer" },
|
|
372
|
+
billingCustomer,
|
|
373
|
+
});
|
|
374
|
+
expect(result.providerSnapshot).toMatchObject({ billingCustomer });
|
|
375
|
+
expect(result.preservation).toBe("requires_schedule_validation");
|
|
376
|
+
const changed = sdk.prepareRenewalPriceReplacement({
|
|
377
|
+
...args,
|
|
378
|
+
subscription: { ...args.subscription, customer: "cus_buyer" },
|
|
379
|
+
billingCustomer: {
|
|
380
|
+
...billingCustomer,
|
|
381
|
+
invoice_settings: { default_payment_method: "pm_changed" },
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
expect(changed.requestIdentity.binding).not.toBe(
|
|
385
|
+
result.requestIdentity.binding,
|
|
386
|
+
);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it.each([
|
|
390
|
+
["id", "cus_foreign"],
|
|
391
|
+
["customer_account", "acct_foreign"],
|
|
392
|
+
["livemode", true],
|
|
393
|
+
["deleted", true],
|
|
394
|
+
] as const)(
|
|
395
|
+
"rejects foreign/deleted billing-customer evidence %s",
|
|
396
|
+
(field, value) => {
|
|
397
|
+
const args = input();
|
|
398
|
+
expect(() =>
|
|
399
|
+
sdk.prepareRenewalPriceReplacement({
|
|
400
|
+
...args,
|
|
401
|
+
subscription: { ...args.subscription, customer: "cus_buyer" },
|
|
402
|
+
billingCustomer: { ...billingCustomer, [field]: value },
|
|
403
|
+
}),
|
|
404
|
+
).toThrow();
|
|
405
|
+
},
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
it("requires an explicit discount read in supplied ancestor evidence", () => {
|
|
409
|
+
const args = input();
|
|
410
|
+
expect(() =>
|
|
411
|
+
sdk.prepareRenewalPriceReplacement({
|
|
412
|
+
...args,
|
|
413
|
+
subscription: { ...args.subscription, customer: "cus_buyer" },
|
|
414
|
+
billingCustomer: {
|
|
415
|
+
id: "cus_buyer",
|
|
416
|
+
object: "customer",
|
|
417
|
+
customer_account: "acct_buyer",
|
|
418
|
+
livemode: false,
|
|
419
|
+
},
|
|
420
|
+
}),
|
|
421
|
+
).toThrow();
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("accepts an expanded original customer and retains an expanded existing redemption as evidence only", () => {
|
|
425
|
+
const args = input();
|
|
426
|
+
const customer = {
|
|
427
|
+
...billingCustomer,
|
|
428
|
+
discount: {
|
|
429
|
+
id: "di_existing",
|
|
430
|
+
object: "discount",
|
|
431
|
+
start: 1690000000,
|
|
432
|
+
end: null,
|
|
433
|
+
source: {
|
|
434
|
+
type: "coupon",
|
|
435
|
+
coupon: { id: "coupon_existing", duration: "once" },
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
};
|
|
439
|
+
expect(
|
|
440
|
+
sdk.prepareRenewalPriceReplacement({
|
|
441
|
+
...args,
|
|
442
|
+
subscription: { ...args.subscription, customer: billingCustomer },
|
|
443
|
+
billingCustomer: customer,
|
|
444
|
+
}),
|
|
445
|
+
).toMatchObject({
|
|
446
|
+
preservation: "requires_schedule_validation",
|
|
447
|
+
providerSnapshot: { billingCustomer: customer },
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("rejects a deleted expanded original customer even when the separate billing read matches its ID", () => {
|
|
452
|
+
const args = input();
|
|
453
|
+
expect(() =>
|
|
454
|
+
sdk.prepareRenewalPriceReplacement({
|
|
455
|
+
...args,
|
|
456
|
+
subscription: {
|
|
457
|
+
...args.subscription,
|
|
458
|
+
customer: { ...billingCustomer, deleted: true },
|
|
459
|
+
},
|
|
460
|
+
billingCustomer,
|
|
461
|
+
}),
|
|
462
|
+
).toThrow(/does not match/);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it.each([
|
|
466
|
+
["nonfinite", Number.POSITIVE_INFINITY],
|
|
467
|
+
["undefined", undefined],
|
|
468
|
+
["function", () => 1],
|
|
469
|
+
["bigint", 1n],
|
|
470
|
+
["symbol", Symbol("value")],
|
|
471
|
+
["class instance", new Date(0)],
|
|
472
|
+
["oversized string", "x".repeat(500_001)],
|
|
473
|
+
])("rejects %s in private provider JSON", (_label, value) => {
|
|
474
|
+
const args = input();
|
|
475
|
+
expect(() =>
|
|
476
|
+
sdk.prepareRenewalPriceReplacement({
|
|
477
|
+
...args,
|
|
478
|
+
subscription: { ...args.subscription, metadata: { value } },
|
|
479
|
+
}),
|
|
480
|
+
).toThrow(/JSON/);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("rejects a cyclic provider snapshot", () => {
|
|
484
|
+
const args = input();
|
|
485
|
+
const cyclic: Record<string, unknown> = {};
|
|
486
|
+
cyclic.self = cyclic;
|
|
487
|
+
expect(() =>
|
|
488
|
+
sdk.prepareRenewalPriceReplacement({
|
|
489
|
+
...args,
|
|
490
|
+
subscription: { ...args.subscription, metadata: cyclic },
|
|
491
|
+
}),
|
|
492
|
+
).toThrow(/JSON/);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it("rejects an alternative subscription currency even when both default price currencies match", () => {
|
|
496
|
+
const args = input();
|
|
497
|
+
expect(() =>
|
|
498
|
+
sdk.prepareRenewalPriceReplacement({
|
|
499
|
+
...args,
|
|
500
|
+
subscription: { ...args.subscription, currency: "eur" },
|
|
501
|
+
}),
|
|
502
|
+
).toThrow(/currency/);
|
|
503
|
+
});
|