@fantastic.dev/better-stripe 0.37.1 → 0.39.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 +53 -0
- package/MIGRATION.md +9 -0
- package/README.md +25 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +506 -2
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +253 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +506 -2
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +253 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/core/accountCapabilities.d.ts +20 -0
- package/dist/client/core/accountCapabilities.d.ts.map +1 -0
- package/dist/client/core/accountCapabilities.js +7 -0
- package/dist/client/core/accountCapabilities.js.map +1 -0
- package/dist/client/core/accountConfigurations.d.ts +46 -0
- package/dist/client/core/accountConfigurations.d.ts.map +1 -0
- package/dist/client/core/accountConfigurations.js +99 -0
- package/dist/client/core/accountConfigurations.js.map +1 -0
- package/dist/client/core/accountLinks.d.ts +29 -2
- package/dist/client/core/accountLinks.d.ts.map +1 -1
- package/dist/client/core/accountLinks.js +56 -4
- package/dist/client/core/accountLinks.js.map +1 -1
- package/dist/client/core/accounts.d.ts +2 -36
- package/dist/client/core/accounts.d.ts.map +1 -1
- package/dist/client/core/accounts.js +3 -73
- package/dist/client/core/accounts.js.map +1 -1
- package/dist/client/core/billingPortalClient.d.ts +14 -0
- package/dist/client/core/billingPortalClient.d.ts.map +1 -0
- package/dist/client/core/billingPortalClient.js +18 -0
- package/dist/client/core/billingPortalClient.js.map +1 -0
- package/dist/client/costRecovery-invoices.d.ts +4 -4
- package/dist/client/costRecovery.d.ts +19 -1
- package/dist/client/costRecovery.d.ts.map +1 -1
- package/dist/client/costRecovery.js +4 -0
- package/dist/client/costRecovery.js.map +1 -1
- package/dist/client/financialOutcomes/adapters.d.ts +18 -0
- package/dist/client/financialOutcomes/adapters.d.ts.map +1 -0
- package/dist/client/financialOutcomes/adapters.js +60 -0
- package/dist/client/financialOutcomes/adapters.js.map +1 -0
- package/dist/client/financialOutcomes/contracts.d.ts +42 -0
- package/dist/client/financialOutcomes/contracts.d.ts.map +1 -0
- package/dist/client/financialOutcomes/contracts.js +39 -0
- package/dist/client/financialOutcomes/contracts.js.map +1 -0
- package/dist/client/financialOutcomes/errors.d.ts +6 -0
- package/dist/client/financialOutcomes/errors.d.ts.map +1 -0
- package/dist/client/financialOutcomes/errors.js +22 -0
- package/dist/client/financialOutcomes/errors.js.map +1 -0
- package/dist/client/financialOutcomes/index.d.ts +6 -0
- package/dist/client/financialOutcomes/index.d.ts.map +1 -0
- package/dist/client/financialOutcomes/index.js +4 -0
- package/dist/client/financialOutcomes/index.js.map +1 -0
- package/dist/client/financialOutcomes/retry.d.ts +24 -0
- package/dist/client/financialOutcomes/retry.d.ts.map +1 -0
- package/dist/client/financialOutcomes/retry.js +31 -0
- package/dist/client/financialOutcomes/retry.js.map +1 -0
- package/dist/client/index.d.ts +21 -111
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +11 -85
- package/dist/client/index.js.map +1 -1
- package/dist/client/processingCostDeficit/index.d.ts +27 -0
- package/dist/client/processingCostDeficit/index.d.ts.map +1 -0
- package/dist/client/processingCostDeficit/index.js +54 -0
- package/dist/client/processingCostDeficit/index.js.map +1 -0
- package/dist/client/products/catalog.d.ts +26 -0
- package/dist/client/products/catalog.d.ts.map +1 -0
- package/dist/client/products/catalog.js +140 -0
- package/dist/client/products/catalog.js.map +1 -0
- package/dist/client/products/catalogClient.d.ts +109 -0
- package/dist/client/products/catalogClient.d.ts.map +1 -0
- package/dist/client/products/catalogClient.js +84 -0
- package/dist/client/products/catalogClient.js.map +1 -0
- package/dist/client/products/prices.d.ts +1 -0
- package/dist/client/products/prices.d.ts.map +1 -1
- package/dist/client/products/prices.js +1 -0
- package/dist/client/products/prices.js.map +1 -1
- package/dist/client/publicTypes.d.ts +2 -0
- package/dist/client/publicTypes.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +284 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +535 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/helpers.d.ts +86 -0
- package/dist/component/balanceFunding/helpers.d.ts.map +1 -0
- package/dist/component/balanceFunding/helpers.js +68 -0
- package/dist/component/balanceFunding/helpers.js.map +1 -0
- package/dist/component/balanceFunding/importHandler.d.ts +13 -0
- package/dist/component/balanceFunding/importHandler.d.ts.map +1 -0
- package/dist/component/balanceFunding/importHandler.js +130 -0
- package/dist/component/balanceFunding/importHandler.js.map +1 -0
- package/dist/component/balanceFunding/lifecycleHandlers.d.ts +85 -0
- package/dist/component/balanceFunding/lifecycleHandlers.d.ts.map +1 -0
- package/dist/component/balanceFunding/lifecycleHandlers.js +60 -0
- package/dist/component/balanceFunding/lifecycleHandlers.js.map +1 -0
- package/dist/component/balanceFunding/mutationValidators.d.ts +238 -0
- package/dist/component/balanceFunding/mutationValidators.d.ts.map +1 -0
- package/dist/component/balanceFunding/mutationValidators.js +55 -0
- package/dist/component/balanceFunding/mutationValidators.js.map +1 -0
- package/dist/component/balanceFunding/mutations.d.ts +335 -0
- package/dist/component/balanceFunding/mutations.d.ts.map +1 -0
- package/dist/component/balanceFunding/mutations.js +106 -0
- package/dist/component/balanceFunding/mutations.js.map +1 -0
- package/dist/component/balanceFunding/preparationHandlers.d.ts +55 -0
- package/dist/component/balanceFunding/preparationHandlers.d.ts.map +1 -0
- package/dist/component/balanceFunding/preparationHandlers.js +165 -0
- package/dist/component/balanceFunding/preparationHandlers.js.map +1 -0
- package/dist/component/balanceFunding/queries.d.ts +151 -0
- package/dist/component/balanceFunding/queries.d.ts.map +1 -0
- package/dist/component/balanceFunding/queries.js +180 -0
- package/dist/component/balanceFunding/queries.js.map +1 -0
- package/dist/component/balanceFunding/schema.d.ts +748 -0
- package/dist/component/balanceFunding/schema.d.ts.map +1 -0
- package/dist/component/balanceFunding/schema.js +124 -0
- package/dist/component/balanceFunding/schema.js.map +1 -0
- package/dist/component/balanceFunding/stateHandlers.d.ts +58 -0
- package/dist/component/balanceFunding/stateHandlers.d.ts.map +1 -0
- package/dist/component/balanceFunding/stateHandlers.js +160 -0
- package/dist/component/balanceFunding/stateHandlers.js.map +1 -0
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/convex.config.js +2 -0
- package/dist/component/convex.config.js.map +1 -1
- package/dist/component/costRecovery/completion.d.ts +1 -0
- package/dist/component/costRecovery/completion.d.ts.map +1 -1
- package/dist/component/costRecovery/helpers.d.ts +1 -0
- package/dist/component/costRecovery/helpers.d.ts.map +1 -1
- package/dist/component/costRecovery/mutations.d.ts +5 -0
- package/dist/component/costRecovery/mutations.d.ts.map +1 -1
- package/dist/component/costRecovery/preparation.d.ts +50 -0
- package/dist/component/costRecovery/preparation.d.ts.map +1 -0
- package/dist/component/costRecovery/preparation.js +108 -0
- package/dist/component/costRecovery/preparation.js.map +1 -0
- package/dist/component/costRecovery/queries.d.ts +1 -0
- package/dist/component/costRecovery/queries.d.ts.map +1 -1
- package/dist/component/costRecovery/reservations.d.ts +2 -0
- package/dist/component/costRecovery/reservations.d.ts.map +1 -1
- package/dist/component/costRecovery/schema.d.ts +7 -2
- package/dist/component/costRecovery/schema.d.ts.map +1 -1
- package/dist/component/costRecovery/schema.js +1 -0
- package/dist/component/costRecovery/schema.js.map +1 -1
- package/dist/component/durableExecution/runtime.d.ts +3 -0
- package/dist/component/durableExecution/runtime.d.ts.map +1 -0
- package/dist/component/durableExecution/runtime.js +12 -0
- package/dist/component/durableExecution/runtime.js.map +1 -0
- package/dist/component/earnings/triggers.d.ts +242 -2
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/products/mutations.d.ts +9 -0
- package/dist/component/products/mutations.d.ts.map +1 -1
- package/dist/component/products/mutations.js +31 -2
- package/dist/component/products/mutations.js.map +1 -1
- package/dist/component/schema.d.ts +253 -1
- package/dist/component/schema.js +4 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/read-contracts.md +33 -0
- package/package.json +4 -2
- package/src/client/core/account-session-client.test.ts +74 -0
- package/src/client/core/accountCapabilities.ts +22 -0
- package/src/client/core/accountConfigurations.test.ts +183 -0
- package/src/client/core/accountConfigurations.ts +161 -0
- package/src/client/core/accountLinks.test.ts +6 -2
- package/src/client/core/accountLinks.ts +122 -5
- package/src/client/core/accounts.ts +8 -115
- package/src/client/core/billingPortal.test.ts +247 -0
- package/src/client/core/billingPortalClient.ts +35 -0
- package/src/client/costRecovery.test.ts +34 -0
- package/src/client/costRecovery.ts +21 -0
- package/src/client/financialOutcomes/adapters.ts +88 -0
- package/src/client/financialOutcomes/contracts.ts +43 -0
- package/src/client/financialOutcomes/errors.ts +29 -0
- package/src/client/financialOutcomes/index.ts +5 -0
- package/src/client/financialOutcomes/outcomes.test.ts +254 -0
- package/src/client/financialOutcomes/retry.ts +52 -0
- package/src/client/index.test.ts +0 -64
- package/src/client/index.ts +35 -185
- package/src/client/processingCostDeficit/README.md +35 -0
- package/src/client/processingCostDeficit/calculator.test.ts +176 -0
- package/src/client/processingCostDeficit/index.ts +91 -0
- package/src/client/products/catalog.test.ts +389 -0
- package/src/client/products/catalog.ts +225 -0
- package/src/client/products/catalogClient.ts +172 -0
- package/src/client/products/prices.test.ts +24 -0
- package/src/client/products/prices.ts +2 -0
- package/src/client/publicTypes.ts +13 -0
- package/src/component/_generated/api.ts +27 -1
- package/src/component/_generated/component.ts +707 -0
- package/src/component/balanceFunding/failure.test.ts +116 -0
- package/src/component/balanceFunding/helpers.ts +102 -0
- package/src/component/balanceFunding/import.test.ts +221 -0
- package/src/component/balanceFunding/importHandler.ts +146 -0
- package/src/component/balanceFunding/lifecycleHandlers.ts +90 -0
- package/src/component/balanceFunding/lookup.test.ts +113 -0
- package/src/component/balanceFunding/mutationValidators.ts +63 -0
- package/src/component/balanceFunding/mutations.ts +136 -0
- package/src/component/balanceFunding/preparation.test.ts +146 -0
- package/src/component/balanceFunding/preparationHandlers.ts +204 -0
- package/src/component/balanceFunding/queries.ts +221 -0
- package/src/component/balanceFunding/requestLookup.test.ts +160 -0
- package/src/component/balanceFunding/schema.ts +169 -0
- package/src/component/balanceFunding/state.test.ts +352 -0
- package/src/component/balanceFunding/stateHandlers.ts +198 -0
- package/src/component/balanceFunding/validation.test.ts +100 -0
- package/src/component/convex.config.ts +2 -0
- package/src/component/costRecovery/preparation.test.ts +355 -0
- package/src/component/costRecovery/preparation.ts +124 -0
- package/src/component/costRecovery/schema.ts +1 -0
- package/src/component/durableExecution/composition.test.ts +568 -0
- package/src/component/durableExecution/runtime.ts +12 -0
- package/src/component/products/mutations.ts +39 -2
- package/src/component/schema.ts +8 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Processing-cost deficit
|
|
2
|
+
|
|
3
|
+
`calculateProcessingCostDeficit` is a pure SDK calculation. The host authorizes
|
|
4
|
+
the original same-currency source and explicitly chooses liability and rounding.
|
|
5
|
+
The helper creates no charges, claims or transfers. The example's existing
|
|
6
|
+
business policy stays in the host until its financial cutover.
|
|
7
|
+
|
|
8
|
+
All amounts use nonnegative safe integer API ledger units. Gross must be positive
|
|
9
|
+
and the original inclusive selling fee cannot exceed gross. Currency must have
|
|
10
|
+
supported package charge-currency metadata. Metadata does not establish account,
|
|
11
|
+
country or payment-method eligibility. This helper does not convert display
|
|
12
|
+
amounts or validate a provider-specific charge/transfer quantum.
|
|
13
|
+
|
|
14
|
+
Required policy fields:
|
|
15
|
+
|
|
16
|
+
- `liability.processingCosts: "uncovered"` subtracts the proportional retained
|
|
17
|
+
inclusive selling fee from processing costs and floors the liability at zero.
|
|
18
|
+
- `"all"` assigns all processing costs; `"none"` assigns none.
|
|
19
|
+
- `liability.disputeCosts: "all" | "none"` selects dispute-cost liability
|
|
20
|
+
independently. Actual credited costs should be resolved before calling.
|
|
21
|
+
- `rounding: "nearest" | "floor" | "ceil"` applies to the retained fee.
|
|
22
|
+
Nearest rounds a positive exact half up. No policy has a default.
|
|
23
|
+
|
|
24
|
+
The calculation caps overlapping refunded/disputed principal at original gross.
|
|
25
|
+
It returns normalized currency, retained fee, processing liability, dispute
|
|
26
|
+
liability and total `amountDue`. Unsafe input amounts or totals are rejected.
|
|
27
|
+
|
|
28
|
+
The old example formula's ordinary fixtures use processing `"uncovered"`,
|
|
29
|
+
disputes `"all"` and rounding `"nearest"`. USD, JPY, ISK and UGX fixture values
|
|
30
|
+
retain their historical integer-unit results. Multiplication and division now
|
|
31
|
+
use exact integers. At gross 9007199254740991, selling fee 9007199254740990 and
|
|
32
|
+
returned principal 3002399751580330, retained fee is exactly 6004799503160660.
|
|
33
|
+
The old floating-point intermediate incorrectly rounded it to 6004799503160661.
|
|
34
|
+
This correction intentionally preserves a one-unit liability that the old
|
|
35
|
+
formula lost. No host calculation changes until its caller adopts this API.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
calculateProcessingCostDeficit,
|
|
4
|
+
type ProcessingCostDeficitOptions,
|
|
5
|
+
} from "./index.js";
|
|
6
|
+
|
|
7
|
+
const original: ProcessingCostDeficitOptions = {
|
|
8
|
+
currency: "usd",
|
|
9
|
+
gross: 10000,
|
|
10
|
+
sellingFee: 900,
|
|
11
|
+
processingFee: 320,
|
|
12
|
+
principalReturned: 10000,
|
|
13
|
+
disputeFees: 0,
|
|
14
|
+
liability: { processingCosts: "uncovered", disputeCosts: "all" },
|
|
15
|
+
rounding: "nearest",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe("explicit processing cost liability", () => {
|
|
19
|
+
it("passes uncovered costs on a full return under the host-selected policy", () => {
|
|
20
|
+
expect(calculateProcessingCostDeficit(original)).toEqual({
|
|
21
|
+
currency: "usd",
|
|
22
|
+
retainedFee: 0,
|
|
23
|
+
processingLiability: 320,
|
|
24
|
+
disputeLiability: 0,
|
|
25
|
+
amountDue: 320,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
it.each(["usd", "jpy", "isk", "ugx"])(
|
|
29
|
+
"matches historical integer-unit rounding for ordinary %s values",
|
|
30
|
+
(currency) => {
|
|
31
|
+
for (const principalReturned of [0, 1, 1000, 5000, 9999, 10000, 12000]) {
|
|
32
|
+
for (const sellingFee of [0, 320, 490, 900]) {
|
|
33
|
+
const retainedFee = Math.round(
|
|
34
|
+
(sellingFee * (10000 - Math.min(10000, principalReturned))) / 10000,
|
|
35
|
+
);
|
|
36
|
+
const result = calculateProcessingCostDeficit({
|
|
37
|
+
...original,
|
|
38
|
+
currency,
|
|
39
|
+
principalReturned,
|
|
40
|
+
sellingFee,
|
|
41
|
+
disputeFees: 1500,
|
|
42
|
+
});
|
|
43
|
+
expect(result.retainedFee).toBe(retainedFee);
|
|
44
|
+
expect(result.amountDue).toBe(Math.max(0, 320 - retainedFee) + 1500);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
it("lets the host retain all liability instead of assuming a seller pays", () => {
|
|
50
|
+
expect(
|
|
51
|
+
calculateProcessingCostDeficit({
|
|
52
|
+
...original,
|
|
53
|
+
disputeFees: 1500,
|
|
54
|
+
liability: { processingCosts: "none", disputeCosts: "none" },
|
|
55
|
+
}),
|
|
56
|
+
).toMatchObject({
|
|
57
|
+
processingLiability: 0,
|
|
58
|
+
disputeLiability: 0,
|
|
59
|
+
amountDue: 0,
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it("supports all processing costs with an independent dispute policy", () => {
|
|
63
|
+
expect(
|
|
64
|
+
calculateProcessingCostDeficit({
|
|
65
|
+
...original,
|
|
66
|
+
principalReturned: 0,
|
|
67
|
+
disputeFees: 1500,
|
|
68
|
+
liability: { processingCosts: "all", disputeCosts: "none" },
|
|
69
|
+
}),
|
|
70
|
+
).toMatchObject({
|
|
71
|
+
retainedFee: 900,
|
|
72
|
+
processingLiability: 320,
|
|
73
|
+
amountDue: 320,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
it.each([
|
|
77
|
+
["nearest", 5, 0],
|
|
78
|
+
["floor", 4, 1],
|
|
79
|
+
["ceil", 5, 0],
|
|
80
|
+
] as const)(
|
|
81
|
+
"uses the selected %s rounding for retained fee",
|
|
82
|
+
(rounding, retainedFee, amountDue) => {
|
|
83
|
+
expect(
|
|
84
|
+
calculateProcessingCostDeficit({
|
|
85
|
+
...original,
|
|
86
|
+
gross: 10,
|
|
87
|
+
sellingFee: 5,
|
|
88
|
+
processingFee: 5,
|
|
89
|
+
principalReturned: 1,
|
|
90
|
+
rounding,
|
|
91
|
+
}),
|
|
92
|
+
).toMatchObject({ retainedFee, amountDue });
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
it("does not lose a unit when an intermediate product exceeds safe integer precision", () => {
|
|
96
|
+
expect(
|
|
97
|
+
calculateProcessingCostDeficit({
|
|
98
|
+
...original,
|
|
99
|
+
gross: 9007199254740991,
|
|
100
|
+
sellingFee: 9007199254740990,
|
|
101
|
+
principalReturned: 3002399751580330,
|
|
102
|
+
processingFee: 6004799503160661,
|
|
103
|
+
}),
|
|
104
|
+
).toMatchObject({ retainedFee: 6004799503160660, amountDue: 1 });
|
|
105
|
+
});
|
|
106
|
+
it("caps overlapping returned principal at original gross and includes actual dispute costs", () => {
|
|
107
|
+
expect(
|
|
108
|
+
calculateProcessingCostDeficit({
|
|
109
|
+
...original,
|
|
110
|
+
principalReturned: 20000,
|
|
111
|
+
disputeFees: 1500,
|
|
112
|
+
}),
|
|
113
|
+
).toMatchObject({ retainedFee: 0, amountDue: 1820 });
|
|
114
|
+
});
|
|
115
|
+
it("normalizes currency codes without converting any ledger units", () => {
|
|
116
|
+
expect(
|
|
117
|
+
calculateProcessingCostDeficit({ ...original, currency: "USD" }).currency,
|
|
118
|
+
).toBe("usd");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("invalid financial inputs", () => {
|
|
123
|
+
it.each([null, undefined])(
|
|
124
|
+
"rejects an absent options object %s",
|
|
125
|
+
(options) => {
|
|
126
|
+
expect(() =>
|
|
127
|
+
calculateProcessingCostDeficit(
|
|
128
|
+
options as unknown as ProcessingCostDeficitOptions,
|
|
129
|
+
),
|
|
130
|
+
).toThrow(RangeError);
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
it.each([
|
|
134
|
+
"gross",
|
|
135
|
+
"sellingFee",
|
|
136
|
+
"processingFee",
|
|
137
|
+
"principalReturned",
|
|
138
|
+
"disputeFees",
|
|
139
|
+
] as const)("rejects invalid %s values", (field) => {
|
|
140
|
+
for (const value of [-1, 1.5, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1]) {
|
|
141
|
+
expect(() =>
|
|
142
|
+
calculateProcessingCostDeficit({ ...original, [field]: value }),
|
|
143
|
+
).toThrow(RangeError);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
it.each([
|
|
147
|
+
{ gross: 0 },
|
|
148
|
+
{ sellingFee: 10001 },
|
|
149
|
+
{ currency: "unknown" },
|
|
150
|
+
{ currency: "kwd" },
|
|
151
|
+
{ rounding: "random" },
|
|
152
|
+
{ liability: { processingCosts: "seller", disputeCosts: "all" } },
|
|
153
|
+
{ liability: { processingCosts: "uncovered", disputeCosts: "seller" } },
|
|
154
|
+
{ liability: undefined },
|
|
155
|
+
{ rounding: undefined },
|
|
156
|
+
])(
|
|
157
|
+
"rejects missing/unsupported currency, policy or amount combinations %j",
|
|
158
|
+
(invalid) => {
|
|
159
|
+
expect(() =>
|
|
160
|
+
calculateProcessingCostDeficit({
|
|
161
|
+
...original,
|
|
162
|
+
...invalid,
|
|
163
|
+
} as ProcessingCostDeficitOptions),
|
|
164
|
+
).toThrow(RangeError);
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
it("rejects an unrepresentable total even when each input is a safe integer", () => {
|
|
168
|
+
expect(() =>
|
|
169
|
+
calculateProcessingCostDeficit({
|
|
170
|
+
...original,
|
|
171
|
+
processingFee: Number.MAX_SAFE_INTEGER,
|
|
172
|
+
disputeFees: 1,
|
|
173
|
+
}),
|
|
174
|
+
).toThrow(RangeError);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { getChargeCurrencyMetadata } from "../charge-currency.js";
|
|
2
|
+
|
|
3
|
+
export type ProcessingCostDeficitOptions = {
|
|
4
|
+
currency: string;
|
|
5
|
+
gross: number;
|
|
6
|
+
sellingFee: number;
|
|
7
|
+
processingFee: number;
|
|
8
|
+
principalReturned: number;
|
|
9
|
+
disputeFees: number;
|
|
10
|
+
liability: {
|
|
11
|
+
processingCosts: "uncovered" | "all" | "none";
|
|
12
|
+
disputeCosts: "all" | "none";
|
|
13
|
+
};
|
|
14
|
+
rounding: "nearest" | "floor" | "ceil";
|
|
15
|
+
};
|
|
16
|
+
export type ProcessingCostDeficit = {
|
|
17
|
+
currency: string;
|
|
18
|
+
retainedFee: number;
|
|
19
|
+
processingLiability: number;
|
|
20
|
+
disputeLiability: number;
|
|
21
|
+
amountDue: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Pure same-currency ledger calculation. Host selects liability and rounding.
|
|
26
|
+
* All amounts are nonnegative integer API units, not decimal/display amounts.
|
|
27
|
+
* This does not establish provider eligibility or create a charge/transfer.
|
|
28
|
+
*/
|
|
29
|
+
export function calculateProcessingCostDeficit(
|
|
30
|
+
options: ProcessingCostDeficitOptions,
|
|
31
|
+
): ProcessingCostDeficit {
|
|
32
|
+
if (!options || typeof options !== "object")
|
|
33
|
+
throw new RangeError("Processing cost options are required.");
|
|
34
|
+
const currency =
|
|
35
|
+
typeof options.currency === "string"
|
|
36
|
+
? getChargeCurrencyMetadata(options.currency)
|
|
37
|
+
: null;
|
|
38
|
+
if (
|
|
39
|
+
!currency ||
|
|
40
|
+
[
|
|
41
|
+
options.gross,
|
|
42
|
+
options.sellingFee,
|
|
43
|
+
options.processingFee,
|
|
44
|
+
options.principalReturned,
|
|
45
|
+
options.disputeFees,
|
|
46
|
+
].some((amount) => !Number.isSafeInteger(amount) || amount < 0) ||
|
|
47
|
+
options.gross === 0 ||
|
|
48
|
+
options.sellingFee > options.gross ||
|
|
49
|
+
!["nearest", "floor", "ceil"].includes(options.rounding) ||
|
|
50
|
+
!["uncovered", "all", "none"].includes(
|
|
51
|
+
options.liability?.processingCosts,
|
|
52
|
+
) ||
|
|
53
|
+
!["all", "none"].includes(options.liability?.disputeCosts)
|
|
54
|
+
)
|
|
55
|
+
throw new RangeError(
|
|
56
|
+
"Invalid processing cost amounts, currency or policy.",
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const gross = BigInt(options.gross);
|
|
60
|
+
const remaining =
|
|
61
|
+
gross - BigInt(Math.min(options.gross, options.principalReturned));
|
|
62
|
+
const numerator = BigInt(options.sellingFee) * remaining;
|
|
63
|
+
const remainder = numerator % gross;
|
|
64
|
+
const increment =
|
|
65
|
+
options.rounding === "ceil"
|
|
66
|
+
? remainder > 0n
|
|
67
|
+
: options.rounding === "nearest"
|
|
68
|
+
? 2n * remainder >= gross
|
|
69
|
+
: false;
|
|
70
|
+
const retainedFee = Number(numerator / gross + (increment ? 1n : 0n));
|
|
71
|
+
const processingLiability =
|
|
72
|
+
options.liability.processingCosts === "none"
|
|
73
|
+
? 0
|
|
74
|
+
: options.liability.processingCosts === "all"
|
|
75
|
+
? options.processingFee
|
|
76
|
+
: Math.max(0, options.processingFee - retainedFee);
|
|
77
|
+
const disputeLiability =
|
|
78
|
+
options.liability.disputeCosts === "all" ? options.disputeFees : 0;
|
|
79
|
+
const amountDue = processingLiability + disputeLiability;
|
|
80
|
+
if (!Number.isSafeInteger(amountDue))
|
|
81
|
+
throw new RangeError(
|
|
82
|
+
"Processing cost liability exceeds safe integer units.",
|
|
83
|
+
);
|
|
84
|
+
return {
|
|
85
|
+
currency: currency.currency,
|
|
86
|
+
retainedFee,
|
|
87
|
+
processingLiability,
|
|
88
|
+
disputeLiability,
|
|
89
|
+
amountDue,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import { componentsGeneric, type FunctionReference } from "convex/server";
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import schema from "../../component/schema.js";
|
|
5
|
+
import { initConvexTest } from "../setup.test.js";
|
|
6
|
+
import { BetterStripe } from "../index.js";
|
|
7
|
+
import { getStripeClient, type Component, type RunCtx } from "../helpers.js";
|
|
8
|
+
const modules = import.meta.glob("../../component/**/*.{ts,js,tsx,jsx}");
|
|
9
|
+
|
|
10
|
+
describe("lookup-key catalog", () => {
|
|
11
|
+
const options = {
|
|
12
|
+
stripeProductId: "prod_catalog",
|
|
13
|
+
lookupKey: "plan:v1",
|
|
14
|
+
unitAmount: 900,
|
|
15
|
+
currency: "usd",
|
|
16
|
+
type: "recurring" as const,
|
|
17
|
+
interval: "month" as const,
|
|
18
|
+
idempotencyKey: "catalog:plan:v1",
|
|
19
|
+
};
|
|
20
|
+
let client: BetterStripe;
|
|
21
|
+
let sdk: Stripe;
|
|
22
|
+
let ctx: RunCtx;
|
|
23
|
+
let price: Stripe.Price;
|
|
24
|
+
let list: ReturnType<typeof vi.spyOn>;
|
|
25
|
+
let create: ReturnType<typeof vi.spyOn>;
|
|
26
|
+
let retrieve: ReturnType<typeof vi.spyOn>;
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
const t = initConvexTest();
|
|
29
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
30
|
+
const key = `sk_test_lookup_${crypto.randomUUID()}`;
|
|
31
|
+
client = new BetterStripe(
|
|
32
|
+
componentsGeneric().betterStripe as unknown as Component,
|
|
33
|
+
{ STRIPE_SECRET_KEY: key },
|
|
34
|
+
);
|
|
35
|
+
sdk = getStripeClient(key);
|
|
36
|
+
ctx = {
|
|
37
|
+
runQuery: (
|
|
38
|
+
ref: FunctionReference<"query">,
|
|
39
|
+
args: Record<string, unknown>,
|
|
40
|
+
) => t.query(ref, args),
|
|
41
|
+
runMutation: (
|
|
42
|
+
ref: FunctionReference<"mutation">,
|
|
43
|
+
args: Record<string, unknown>,
|
|
44
|
+
) => t.mutation(ref, args),
|
|
45
|
+
} as unknown as RunCtx;
|
|
46
|
+
price = {
|
|
47
|
+
id: "price_catalog",
|
|
48
|
+
product: "prod_catalog",
|
|
49
|
+
active: true,
|
|
50
|
+
currency: "usd",
|
|
51
|
+
unit_amount: 900,
|
|
52
|
+
unit_amount_decimal: "900",
|
|
53
|
+
lookup_key: "plan:v1",
|
|
54
|
+
type: "recurring",
|
|
55
|
+
billing_scheme: "per_unit",
|
|
56
|
+
recurring: {
|
|
57
|
+
interval: "month",
|
|
58
|
+
interval_count: 1,
|
|
59
|
+
usage_type: "licensed",
|
|
60
|
+
},
|
|
61
|
+
metadata: {},
|
|
62
|
+
nickname: null,
|
|
63
|
+
} as unknown as Stripe.Price;
|
|
64
|
+
list = vi.spyOn(sdk.prices, "list").mockResolvedValue({
|
|
65
|
+
data: [price],
|
|
66
|
+
has_more: false,
|
|
67
|
+
} as Stripe.Response<Stripe.ApiList<Stripe.Price>>);
|
|
68
|
+
create = vi
|
|
69
|
+
.spyOn(sdk.prices, "create")
|
|
70
|
+
.mockResolvedValue(price as Stripe.Response<Stripe.Price>);
|
|
71
|
+
retrieve = vi.spyOn(sdk.products, "retrieve").mockResolvedValue({
|
|
72
|
+
id: "prod_catalog",
|
|
73
|
+
active: true,
|
|
74
|
+
name: "Catalog",
|
|
75
|
+
description: null,
|
|
76
|
+
metadata: {},
|
|
77
|
+
} as Stripe.Response<Stripe.Product>);
|
|
78
|
+
});
|
|
79
|
+
it("looks up without modifying the component", async () => {
|
|
80
|
+
expect(
|
|
81
|
+
(
|
|
82
|
+
await client.getStripePriceByLookupKey(ctx, {
|
|
83
|
+
lookupKey: options.lookupKey,
|
|
84
|
+
})
|
|
85
|
+
)?.id,
|
|
86
|
+
).toBe(price.id);
|
|
87
|
+
expect(
|
|
88
|
+
await client.getProductByStripeId(ctx, {
|
|
89
|
+
stripeProductId: options.stripeProductId,
|
|
90
|
+
}),
|
|
91
|
+
).toBeNull();
|
|
92
|
+
expect(list).toHaveBeenCalledWith({
|
|
93
|
+
lookup_keys: [options.lookupKey],
|
|
94
|
+
limit: 2,
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
it("adopts a matching price and product into existing mirrors", async () => {
|
|
98
|
+
expect(await client.ensurePrice(ctx, options)).toEqual({
|
|
99
|
+
stripePriceId: price.id,
|
|
100
|
+
});
|
|
101
|
+
const stored = await client.getPriceByStripeId(ctx, {
|
|
102
|
+
stripePriceId: price.id,
|
|
103
|
+
});
|
|
104
|
+
expect(stored).toMatchObject({
|
|
105
|
+
unitAmount: 900,
|
|
106
|
+
intervalCount: 1,
|
|
107
|
+
type: "recurring",
|
|
108
|
+
});
|
|
109
|
+
expect(
|
|
110
|
+
await client.getProductByStripeId(ctx, {
|
|
111
|
+
stripeProductId: options.stripeProductId,
|
|
112
|
+
}),
|
|
113
|
+
).toMatchObject({ name: "Catalog" });
|
|
114
|
+
expect(create).not.toHaveBeenCalled();
|
|
115
|
+
});
|
|
116
|
+
it("creates a missing price with a caller-scoped idempotency key", async () => {
|
|
117
|
+
list.mockResolvedValue({ data: [], has_more: false });
|
|
118
|
+
await client.ensurePrice(ctx, options);
|
|
119
|
+
expect(create).toHaveBeenCalledWith(
|
|
120
|
+
expect.objectContaining({
|
|
121
|
+
lookup_key: options.lookupKey,
|
|
122
|
+
product: options.stripeProductId,
|
|
123
|
+
recurring: { interval: "month", interval_count: 1 },
|
|
124
|
+
}),
|
|
125
|
+
{ idempotencyKey: options.idempotencyKey },
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
it.each([
|
|
129
|
+
{ product: "prod_other" },
|
|
130
|
+
{ unit_amount: 901 },
|
|
131
|
+
{ currency: "eur" },
|
|
132
|
+
{ active: false },
|
|
133
|
+
{
|
|
134
|
+
recurring: {
|
|
135
|
+
interval: "year",
|
|
136
|
+
interval_count: 1,
|
|
137
|
+
usage_type: "licensed",
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
recurring: {
|
|
142
|
+
interval: "month",
|
|
143
|
+
interval_count: 2,
|
|
144
|
+
usage_type: "licensed",
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
recurring: {
|
|
149
|
+
interval: "month",
|
|
150
|
+
interval_count: 1,
|
|
151
|
+
usage_type: "metered",
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
{ type: "one_time", recurring: null },
|
|
155
|
+
{ transform_quantity: { divide_by: 10, round: "up" } },
|
|
156
|
+
{ billing_scheme: "tiered" },
|
|
157
|
+
])(
|
|
158
|
+
"rejects incompatible lookup results %j before mirroring",
|
|
159
|
+
async (patch) => {
|
|
160
|
+
Object.assign(price, patch);
|
|
161
|
+
await expect(client.ensurePrice(ctx, options)).rejects.toThrow(
|
|
162
|
+
/incompatible/,
|
|
163
|
+
);
|
|
164
|
+
expect(retrieve).not.toHaveBeenCalled();
|
|
165
|
+
expect(create).not.toHaveBeenCalled();
|
|
166
|
+
expect(
|
|
167
|
+
await client.getPriceByStripeId(ctx, { stripePriceId: price.id }),
|
|
168
|
+
).toBeNull();
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
it("reconciles another creator winning the lookup key", async () => {
|
|
172
|
+
list.mockResolvedValueOnce({ data: [], has_more: false });
|
|
173
|
+
create.mockRejectedValueOnce(new Error("lookup key already exists"));
|
|
174
|
+
await expect(client.ensurePrice(ctx, options)).resolves.toEqual({
|
|
175
|
+
stripePriceId: price.id,
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
it("preserves a provider error when no winning price exists", async () => {
|
|
179
|
+
list.mockResolvedValue({ data: [], has_more: false });
|
|
180
|
+
const failure = new Error("provider unavailable");
|
|
181
|
+
create.mockRejectedValue(failure);
|
|
182
|
+
await expect(client.ensurePrice(ctx, options)).rejects.toBe(failure);
|
|
183
|
+
});
|
|
184
|
+
it("repairs component persistence after Stripe succeeds", async () => {
|
|
185
|
+
list.mockResolvedValueOnce({ data: [], has_more: false });
|
|
186
|
+
const failing = {
|
|
187
|
+
...ctx,
|
|
188
|
+
runMutation: vi
|
|
189
|
+
.fn()
|
|
190
|
+
.mockRejectedValueOnce(new Error("persistence outage")),
|
|
191
|
+
} as RunCtx;
|
|
192
|
+
await expect(client.ensurePrice(failing, options)).rejects.toThrow(
|
|
193
|
+
"persistence outage",
|
|
194
|
+
);
|
|
195
|
+
await client.ensurePrice(ctx, options);
|
|
196
|
+
expect(create).toHaveBeenCalledTimes(1);
|
|
197
|
+
expect(
|
|
198
|
+
await client.getPriceByStripeId(ctx, { stripePriceId: price.id }),
|
|
199
|
+
).not.toBeNull();
|
|
200
|
+
});
|
|
201
|
+
it.each([
|
|
202
|
+
{ unitAmount: -1 },
|
|
203
|
+
{ unitAmount: 1.5 },
|
|
204
|
+
{ currency: "bogus" },
|
|
205
|
+
{ interval: undefined },
|
|
206
|
+
{ intervalCount: 0 },
|
|
207
|
+
{ lookupKey: "" },
|
|
208
|
+
{ idempotencyKey: "" },
|
|
209
|
+
])("rejects invalid inputs before provider calls %j", async (patch) => {
|
|
210
|
+
await expect(
|
|
211
|
+
client.ensurePrice(ctx, { ...options, ...patch }),
|
|
212
|
+
).rejects.toThrow();
|
|
213
|
+
expect(list).not.toHaveBeenCalled();
|
|
214
|
+
expect(create).not.toHaveBeenCalled();
|
|
215
|
+
});
|
|
216
|
+
it("rejects an ambiguous lookup without adoption", async () => {
|
|
217
|
+
list.mockResolvedValue({ data: [price, price], has_more: false });
|
|
218
|
+
await expect(client.ensurePrice(ctx, options)).rejects.toThrow(/Ambiguous/);
|
|
219
|
+
expect(retrieve).not.toHaveBeenCalled();
|
|
220
|
+
});
|
|
221
|
+
it("creates and mirrors a zero-cost one-time price", async () => {
|
|
222
|
+
Object.assign(price, { type: "one_time", recurring: null, unit_amount: 0 });
|
|
223
|
+
await client.ensurePrice(ctx, {
|
|
224
|
+
...options,
|
|
225
|
+
type: "one_time",
|
|
226
|
+
interval: undefined,
|
|
227
|
+
unitAmount: 0,
|
|
228
|
+
currency: "USD",
|
|
229
|
+
});
|
|
230
|
+
expect(
|
|
231
|
+
await client.getPriceByStripeId(ctx, { stripePriceId: price.id }),
|
|
232
|
+
).toMatchObject({ type: "one_time", unitAmount: 0 });
|
|
233
|
+
});
|
|
234
|
+
it("keeps the original provider error if reconciliation fails", async () => {
|
|
235
|
+
const failure = new Error("provider timed out");
|
|
236
|
+
list
|
|
237
|
+
.mockResolvedValueOnce({ data: [], has_more: false })
|
|
238
|
+
.mockRejectedValueOnce(new Error("read failed"));
|
|
239
|
+
create.mockRejectedValueOnce(failure);
|
|
240
|
+
await expect(client.ensurePrice(ctx, options)).rejects.toBe(failure);
|
|
241
|
+
});
|
|
242
|
+
it("repairs a failed price mirror write after the product was adopted", async () => {
|
|
243
|
+
const original = ctx.runMutation!;
|
|
244
|
+
let calls = 0;
|
|
245
|
+
const failing = {
|
|
246
|
+
...ctx,
|
|
247
|
+
runMutation: async (
|
|
248
|
+
ref: FunctionReference<"mutation">,
|
|
249
|
+
args: Record<string, unknown>,
|
|
250
|
+
) => {
|
|
251
|
+
if (++calls === 2) throw new Error("price mirror outage");
|
|
252
|
+
return original(ref, args);
|
|
253
|
+
},
|
|
254
|
+
} as RunCtx;
|
|
255
|
+
await expect(client.ensurePrice(failing, options)).rejects.toThrow(
|
|
256
|
+
"price mirror outage",
|
|
257
|
+
);
|
|
258
|
+
await client.ensurePrice(ctx, options);
|
|
259
|
+
expect(
|
|
260
|
+
await client.getPriceByStripeId(ctx, { stripePriceId: price.id }),
|
|
261
|
+
).not.toBeNull();
|
|
262
|
+
expect(create).not.toHaveBeenCalled();
|
|
263
|
+
});
|
|
264
|
+
it("converges concurrent callers on the provider lookup winner", async () => {
|
|
265
|
+
list
|
|
266
|
+
.mockResolvedValueOnce({ data: [], has_more: false })
|
|
267
|
+
.mockResolvedValueOnce({ data: [], has_more: false });
|
|
268
|
+
create
|
|
269
|
+
.mockResolvedValueOnce(price)
|
|
270
|
+
.mockRejectedValueOnce(new Error("lookup collision"));
|
|
271
|
+
const results = await Promise.all([
|
|
272
|
+
client.ensurePrice(ctx, options),
|
|
273
|
+
client.ensurePrice(ctx, {
|
|
274
|
+
...options,
|
|
275
|
+
idempotencyKey: "other-agent:same-plan",
|
|
276
|
+
}),
|
|
277
|
+
]);
|
|
278
|
+
expect(results).toEqual([
|
|
279
|
+
{ stripePriceId: price.id },
|
|
280
|
+
{ stripePriceId: price.id },
|
|
281
|
+
]);
|
|
282
|
+
expect(await client.listPrices(ctx)).toHaveLength(1);
|
|
283
|
+
expect(await client.listProducts(ctx)).toHaveLength(1);
|
|
284
|
+
});
|
|
285
|
+
it("atomically rejects concurrent adoption into different owning accounts", async () => {
|
|
286
|
+
let reads = 0;
|
|
287
|
+
let release!: () => void;
|
|
288
|
+
const bothRead = new Promise<void>((resolve) => {
|
|
289
|
+
release = resolve;
|
|
290
|
+
});
|
|
291
|
+
const synchronized = {
|
|
292
|
+
...ctx,
|
|
293
|
+
runQuery: async (
|
|
294
|
+
ref: FunctionReference<"query">,
|
|
295
|
+
args: Record<string, unknown>,
|
|
296
|
+
) => {
|
|
297
|
+
const result = await ctx.runQuery(ref, args);
|
|
298
|
+
if (++reads <= 2) {
|
|
299
|
+
if (reads === 2) release();
|
|
300
|
+
await bothRead;
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
},
|
|
304
|
+
} as RunCtx;
|
|
305
|
+
const outcomes = await Promise.allSettled([
|
|
306
|
+
client.adoptProduct(synchronized, {
|
|
307
|
+
stripeProductId: options.stripeProductId,
|
|
308
|
+
accountId: "acct_a",
|
|
309
|
+
}),
|
|
310
|
+
client.adoptProduct(synchronized, {
|
|
311
|
+
stripeProductId: options.stripeProductId,
|
|
312
|
+
accountId: "acct_b",
|
|
313
|
+
}),
|
|
314
|
+
]);
|
|
315
|
+
expect(
|
|
316
|
+
outcomes.filter((result) => result.status === "fulfilled"),
|
|
317
|
+
).toHaveLength(1);
|
|
318
|
+
const rejected = outcomes.find((result) => result.status === "rejected");
|
|
319
|
+
expect(rejected?.status === "rejected" && String(rejected.reason)).toMatch(
|
|
320
|
+
/ownership/,
|
|
321
|
+
);
|
|
322
|
+
const stored = await client.getProductByStripeId(ctx, {
|
|
323
|
+
stripeProductId: options.stripeProductId,
|
|
324
|
+
});
|
|
325
|
+
const winner = outcomes[0].status === "fulfilled" ? "acct_a" : "acct_b";
|
|
326
|
+
expect(stored?.accountId).toBe(winner);
|
|
327
|
+
});
|
|
328
|
+
it("adopts products without changing existing ownership", async () => {
|
|
329
|
+
await client.upsertProduct(ctx, {
|
|
330
|
+
stripeProductId: options.stripeProductId,
|
|
331
|
+
accountId: "acct_owner",
|
|
332
|
+
name: "Old",
|
|
333
|
+
active: true,
|
|
334
|
+
});
|
|
335
|
+
await client.adoptProduct(ctx, {
|
|
336
|
+
stripeProductId: options.stripeProductId,
|
|
337
|
+
});
|
|
338
|
+
expect(
|
|
339
|
+
await client.getProductByStripeId(ctx, {
|
|
340
|
+
stripeProductId: options.stripeProductId,
|
|
341
|
+
}),
|
|
342
|
+
).toMatchObject({ name: "Catalog", accountId: "acct_owner" });
|
|
343
|
+
await expect(
|
|
344
|
+
client.adoptProduct(ctx, {
|
|
345
|
+
stripeProductId: options.stripeProductId,
|
|
346
|
+
accountId: "acct_other",
|
|
347
|
+
}),
|
|
348
|
+
).rejects.toThrow(/ownership/);
|
|
349
|
+
});
|
|
350
|
+
it("rejects a different explicit owner when the stored owner is an empty string", async () => {
|
|
351
|
+
await client.upsertProduct(ctx, {
|
|
352
|
+
stripeProductId: options.stripeProductId,
|
|
353
|
+
accountId: "",
|
|
354
|
+
name: "Existing",
|
|
355
|
+
active: true,
|
|
356
|
+
});
|
|
357
|
+
const mutation = vi.fn().mockResolvedValue(null);
|
|
358
|
+
await expect(
|
|
359
|
+
client.adoptProduct({ ...ctx, runMutation: mutation } as RunCtx, {
|
|
360
|
+
stripeProductId: options.stripeProductId,
|
|
361
|
+
accountId: "acct_other",
|
|
362
|
+
}),
|
|
363
|
+
).rejects.toThrow(/ownership/);
|
|
364
|
+
expect(mutation).not.toHaveBeenCalled();
|
|
365
|
+
});
|
|
366
|
+
it("retains deliberate legacy upsert ownership changes", async () => {
|
|
367
|
+
await client.adoptProduct(ctx, {
|
|
368
|
+
stripeProductId: options.stripeProductId,
|
|
369
|
+
accountId: "acct_a",
|
|
370
|
+
});
|
|
371
|
+
await client.upsertProduct(ctx, {
|
|
372
|
+
stripeProductId: options.stripeProductId,
|
|
373
|
+
accountId: "acct_b",
|
|
374
|
+
name: "Deliberately reassigned",
|
|
375
|
+
active: true,
|
|
376
|
+
});
|
|
377
|
+
expect(
|
|
378
|
+
await client.getProductByStripeId(ctx, {
|
|
379
|
+
stripeProductId: options.stripeProductId,
|
|
380
|
+
}),
|
|
381
|
+
).toMatchObject({ accountId: "acct_b", name: "Deliberately reassigned" });
|
|
382
|
+
});
|
|
383
|
+
it("rejects deleted products", async () => {
|
|
384
|
+
retrieve.mockResolvedValue({ id: options.stripeProductId, deleted: true });
|
|
385
|
+
await expect(
|
|
386
|
+
client.adoptProduct(ctx, { stripeProductId: options.stripeProductId }),
|
|
387
|
+
).rejects.toThrow(/deleted/);
|
|
388
|
+
});
|
|
389
|
+
});
|