@fantastic.dev/better-stripe 0.44.0 → 0.45.1

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.
@@ -0,0 +1,241 @@
1
+ // @vitest-environment edge-runtime
2
+ import { anyApi, componentsGeneric } from "convex/server";
3
+ import { expect, it, vi } from "vitest";
4
+ import {
5
+ fixture,
6
+ scope,
7
+ fundingScope,
8
+ wire,
9
+ } from "../../testing/crossDomain/fixture.js";
10
+ const nested = componentsGeneric().betterStripe;
11
+ const groupRef = (
12
+ op: { operationId: string; version: number },
13
+ name = "a",
14
+ kind = "settlement",
15
+ ) => ({
16
+ ...scope(name),
17
+ groupKey: `${kind}:${name}`,
18
+ operationId: op.operationId,
19
+ version: op.version,
20
+ });
21
+ const preparation = (t: ReturnType<typeof fixture>) =>
22
+ t.query(nested.balanceFunding.queries.readScopePreparation, {
23
+ ...fundingScope(),
24
+ providerApiVersion: "2026-09-30.preview",
25
+ });
26
+ const status = (
27
+ t: ReturnType<typeof fixture>,
28
+ op: { operationId: string; version: number },
29
+ name = "a",
30
+ kind = "settlement",
31
+ ) =>
32
+ t.query(
33
+ nested.recoveryWorkflow.queries.get,
34
+ groupRef({ operationId: op.operationId, version: op.version }, name, kind),
35
+ );
36
+ const posts = (path: string) =>
37
+ wire.calls.filter((r) => r.method === "POST" && r.path === path);
38
+ it("keeps unrelated recovery progressing through the shared runtime while funding provisioning is in flight", async () => {
39
+ const t = fixture();
40
+ let release!: () => void;
41
+ wire.gate = new Promise<void>((resolve) => {
42
+ release = resolve;
43
+ });
44
+ await t.mutation(anyApi.host.prepareRecovery, {
45
+ name: "a",
46
+ kind: "settlement",
47
+ amount: 60,
48
+ });
49
+ await t.mutation(anyApi.host.prepareRecovery, {
50
+ name: "b",
51
+ kind: "invoice",
52
+ amount: 25,
53
+ });
54
+ const funding = await t.action(anyApi.host.funding, {
55
+ requestKey: "funding-once",
56
+ });
57
+ expect(
58
+ await t.action(anyApi.host.funding, { requestKey: "funding-once" }),
59
+ ).toEqual(funding);
60
+ const sale = await t.mutation(anyApi.host.startRecovery, {
61
+ name: "a",
62
+ kind: "settlement",
63
+ requestKey: "sale-once",
64
+ });
65
+ const invoice = await t.mutation(anyApi.host.startRecovery, {
66
+ name: "b",
67
+ kind: "invoice",
68
+ requestKey: "invoice-once",
69
+ });
70
+ const drain = t.finishAllScheduledFunctions(vi.runAllTimers);
71
+ try {
72
+ await vi.waitFor(() =>
73
+ expect(posts("/v1/payment_methods")).toHaveLength(1),
74
+ );
75
+ await vi.waitFor(() => expect(wire.items).toHaveLength(1));
76
+ wire.invoicePaid = true;
77
+ await t.mutation(
78
+ nested.recoveryWorkflow.mutations.resume,
79
+ groupRef(invoice, "b", "invoice"),
80
+ );
81
+ await vi.waitFor(async () =>
82
+ expect(await status(t, sale)).toMatchObject({ status: "completed" }),
83
+ );
84
+ await vi.waitFor(async () =>
85
+ expect(await status(t, invoice, "b", "invoice")).toMatchObject({
86
+ status: "completed",
87
+ }),
88
+ );
89
+ expect((await preparation(t)).verified).toBe(false);
90
+ expect(posts("/v1/transfers")).toHaveLength(1);
91
+ expect(posts("/v1/invoiceitems")).toHaveLength(1);
92
+ } finally {
93
+ release();
94
+ await drain;
95
+ }
96
+ expect((await preparation(t)).verified).toBe(true);
97
+ expect(wire.calls.every((r) => r.authenticated)).toBe(true);
98
+ expect(
99
+ wire.calls
100
+ .filter(
101
+ (r) =>
102
+ r.path.includes("payment_methods") ||
103
+ r.path.includes("subscriptions"),
104
+ )
105
+ .every((r) => r.version === "2026-09-30.preview"),
106
+ ).toBe(true);
107
+ expect(
108
+ wire.calls
109
+ .filter(
110
+ (r) =>
111
+ !r.path.includes("payment_methods") &&
112
+ !r.path.includes("subscriptions"),
113
+ )
114
+ .every((r) => r.version === "2026-09-30.endive"),
115
+ ).toBe(true);
116
+ });
117
+ it("recovers transfer success and a failed host projection without replaying money or disturbing funding history", async () => {
118
+ const t = fixture();
119
+ wire.loseTransfer = true;
120
+ wire.projectionFailures = 1;
121
+ await t.mutation(anyApi.host.prepareRecovery, {
122
+ name: "a",
123
+ kind: "settlement",
124
+ amount: 60,
125
+ });
126
+ const funding = await t.action(anyApi.host.funding, {
127
+ requestKey: "prepare",
128
+ });
129
+ const sale = await t.mutation(anyApi.host.startRecovery, {
130
+ name: "a",
131
+ kind: "settlement",
132
+ requestKey: "sale",
133
+ });
134
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
135
+ expect(posts("/v1/transfers")).toHaveLength(1);
136
+ expect((await preparation(t)).verified).toBe(true);
137
+ await t.mutation(nested.recoveryWorkflow.mutations.resume, groupRef(sale));
138
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
139
+ expect(await t.run((ctx) => ctx.db.query("reports").take(10))).toEqual([]);
140
+ expect(
141
+ (
142
+ await t.query(nested.costRecovery.queries.group, {
143
+ groupKey: "settlement:a",
144
+ })
145
+ ).status,
146
+ ).toBe("collected");
147
+ const providerReads = wire.calls.length;
148
+ await t.mutation(nested.recoveryWorkflow.mutations.resume, groupRef(sale));
149
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
150
+ expect(await status(t, sale)).toMatchObject({ status: "completed" });
151
+ expect(posts("/v1/transfers")).toHaveLength(1);
152
+ expect(wire.calls).toHaveLength(providerReads);
153
+ const prep = await preparation(t);
154
+ expect(prep.preparation.previousPaymentMethodId).toBe("pm_card_a");
155
+ expect(prep.preparation.previousPaymentMethodTypes).toEqual(["card"]);
156
+ expect(
157
+ (
158
+ await t.query(nested.balanceFunding.queries.getPreparation, {
159
+ ...funding.operation,
160
+ stripeSubscriptionId: "sub_a",
161
+ })
162
+ ).status,
163
+ ).toBe("prepared");
164
+ expect(await t.run((ctx) => ctx.db.query("reports").take(10))).toHaveLength(
165
+ 1,
166
+ );
167
+ const journal = JSON.stringify(
168
+ await t.query(nested.crossDomainFixture.journals, {}),
169
+ );
170
+ expect(journal).not.toContain("sk_test_cross_domain_runtime_only");
171
+ expect(journal).not.toContain("private transfer body");
172
+ expect(journal).not.toContain("private database projection");
173
+ });
174
+ it("replays a refund observation and its collected settlement rollback atomically", async () => {
175
+ const t = fixture();
176
+ await t.mutation(anyApi.host.prepareRecovery, {
177
+ name: "a",
178
+ kind: "settlement",
179
+ amount: 60,
180
+ });
181
+ const sale = await t.mutation(anyApi.host.startRecovery, {
182
+ name: "a",
183
+ kind: "settlement",
184
+ requestKey: "sale-refund",
185
+ });
186
+ await t.action(anyApi.host.funding, { requestKey: "parallel-funding" });
187
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
188
+ expect(await status(t, sale)).toMatchObject({ status: "completed" });
189
+ wire.refunded = true;
190
+ wire.projectionFailures = 1;
191
+ const args = {
192
+ requestKey: "refund-original",
193
+ expectedRevision: -1,
194
+ rollback: { groupKey: "settlement:a", expectedRevision: -1 },
195
+ };
196
+ const first = await t.mutation(anyApi.host.costs, args);
197
+ expect(await t.mutation(anyApi.host.costs, args)).toEqual(first);
198
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
199
+ const costRef = {
200
+ ...scope(),
201
+ operationId: first.operationId,
202
+ version: first.version,
203
+ };
204
+ expect(
205
+ await t.query(nested.costRecovery.queries.claim, {
206
+ claimKey: "cost:pi_source",
207
+ }),
208
+ ).toBeNull();
209
+ expect(
210
+ await t.query(nested.costRecovery.queries.claim, {
211
+ claimKey: "rollback:settlement:a",
212
+ }),
213
+ ).toBeNull();
214
+ expect(
215
+ (await t.run((ctx) => ctx.db.query("reports").take(10))).filter(
216
+ (r) => r.domain === "cost",
217
+ ),
218
+ ).toHaveLength(0);
219
+ const reads = wire.calls.length;
220
+ await t.mutation(
221
+ nested.processingCostReconciliation.mutations.resume,
222
+ costRef,
223
+ );
224
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
225
+ expect(
226
+ await t.query(nested.processingCostReconciliation.queries.get, costRef),
227
+ ).toMatchObject({ status: "completed" });
228
+ expect(
229
+ await t.query(nested.costRecovery.queries.claim, {
230
+ claimKey: "cost:pi_source",
231
+ }),
232
+ ).toMatchObject({ amountDue: 30, revision: 0 });
233
+ expect(
234
+ await t.query(nested.costRecovery.queries.claim, {
235
+ claimKey: "rollback:settlement:a",
236
+ }),
237
+ ).toMatchObject({ amountDue: 60, revision: 0 });
238
+ expect(wire.calls).toHaveLength(reads);
239
+ expect(posts("/v1/transfers")).toHaveLength(1);
240
+ expect((await preparation(t)).verified).toBe(true);
241
+ });
@@ -0,0 +1,233 @@
1
+ // @vitest-environment edge-runtime
2
+ import { anyApi, componentsGeneric } from "convex/server";
3
+ import { expect, it, vi } from "vitest";
4
+ import {
5
+ fixture,
6
+ scope,
7
+ fundingScope,
8
+ wire,
9
+ } from "../../testing/crossDomain/fixture.js";
10
+ const nested = componentsGeneric().betterStripe;
11
+ const fundingRef = (op: { operationId: string; version: number }) => ({
12
+ operationId: op.operationId,
13
+ version: op.version,
14
+ });
15
+ const groupRef = (op: { operationId: string; version: number }) => ({
16
+ ...scope(),
17
+ groupKey: "settlement:a",
18
+ operationId: op.operationId,
19
+ version: op.version,
20
+ });
21
+ const prep = (t: ReturnType<typeof fixture>) =>
22
+ t.query(nested.balanceFunding.queries.readScopePreparation, {
23
+ ...fundingScope(),
24
+ providerApiVersion: "2026-09-30.preview",
25
+ });
26
+ const posts = (path: string) =>
27
+ wire.calls.filter((r) => r.method === "POST" && r.path === path);
28
+ it("holds competing recovery and funding identities while unrelated domains complete", async () => {
29
+ const t = fixture();
30
+ await t.mutation(anyApi.host.prepareRecovery, {
31
+ name: "a",
32
+ kind: "settlement",
33
+ amount: 60,
34
+ });
35
+ expect(
36
+ await t.mutation(anyApi.host.prepareRecovery, {
37
+ name: "a",
38
+ kind: "invoice",
39
+ amount: 60,
40
+ }),
41
+ ).toMatchObject({ status: "conflict", reason: "scope_busy" });
42
+ const funding = await t.action(anyApi.host.funding, {
43
+ requestKey: "funding",
44
+ });
45
+ const sale = await t.mutation(anyApi.host.startRecovery, {
46
+ name: "a",
47
+ kind: "settlement",
48
+ requestKey: "sale",
49
+ affiliate: true,
50
+ });
51
+ expect(
52
+ (
53
+ await t.mutation(anyApi.host.startRecovery, {
54
+ name: "a",
55
+ kind: "settlement",
56
+ requestKey: "alias",
57
+ affiliate: true,
58
+ })
59
+ ).operationId,
60
+ ).toBe(sale.operationId);
61
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
62
+ const captured = await prep(t);
63
+ wire.loseApply = true;
64
+ const applying = await t.action(anyApi.host.apply, {
65
+ preparationId: captured._id,
66
+ enabled: true,
67
+ requestKey: "enable",
68
+ });
69
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
70
+ expect(
71
+ (
72
+ await t.query(nested.balanceFunding.queries.getOperation, {
73
+ ...applying.operation,
74
+ stripeSubscriptionId: "sub_a",
75
+ })
76
+ ).state,
77
+ ).toBe("unknown");
78
+ expect(
79
+ await t.action(anyApi.host.apply, {
80
+ preparationId: captured._id,
81
+ enabled: false,
82
+ requestKey: "opposite",
83
+ }),
84
+ ).toMatchObject({ status: "conflict" });
85
+ await t.action(anyApi.host.fundingControl, {
86
+ ...fundingRef(applying.operation),
87
+ command: "resume",
88
+ });
89
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
90
+ expect((await prep(t)).enabled).toBe(true);
91
+ expect(posts("/v1/subscriptions/sub_a")).toHaveLength(1);
92
+ expect(posts("/v1/transfers").map((r) => r.key)).toHaveLength(2);
93
+ expect(wire.transfers.map((t) => t.amount)).toEqual([740, 100]);
94
+ expect(
95
+ (
96
+ await t.query(nested.balanceFunding.queries.getPreparation, {
97
+ ...funding.operation,
98
+ stripeSubscriptionId: "sub_a",
99
+ })
100
+ ).status,
101
+ ).toBe("prepared");
102
+ });
103
+ it("cancels before dispatch without releasing reserved recovery or creating provider effects", async () => {
104
+ const t = fixture();
105
+ await t.mutation(anyApi.host.prepareRecovery, {
106
+ name: "a",
107
+ kind: "settlement",
108
+ amount: 60,
109
+ });
110
+ const funding = await t.action(anyApi.host.funding, {
111
+ requestKey: "cancel-funding",
112
+ });
113
+ const sale = await t.mutation(anyApi.host.startRecovery, {
114
+ name: "a",
115
+ kind: "settlement",
116
+ requestKey: "cancel-sale",
117
+ });
118
+ await t.action(anyApi.host.fundingControl, {
119
+ ...fundingRef(funding.operation),
120
+ command: "cancel",
121
+ });
122
+ await t.mutation(
123
+ nested.recoveryWorkflow.mutations.requestCancel,
124
+ groupRef(sale),
125
+ );
126
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
127
+ expect(posts("/v1/payment_methods")).toEqual([]);
128
+ expect(posts("/v1/transfers")).toEqual([]);
129
+ expect(
130
+ await t.query(nested.recoveryWorkflow.queries.get, groupRef(sale)),
131
+ ).toMatchObject({ status: "canceled" });
132
+ expect(
133
+ await t.query(nested.costRecovery.queries.group, {
134
+ groupKey: "settlement:a",
135
+ }),
136
+ ).toMatchObject({ status: "sealed", amount: 60 });
137
+ expect(
138
+ await t.query(nested.costRecovery.queries.claim, { claimKey: "debt:a" }),
139
+ ).toMatchObject({ reservedAmount: 60, collectedAmount: 0 });
140
+ });
141
+ it("keeps a known in-flight collection after cancellation without a second transfer", async () => {
142
+ const t = fixture();
143
+ await t.action(anyApi.host.funding, { requestKey: "funding" });
144
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
145
+ await t.mutation(anyApi.host.prepareRecovery, {
146
+ name: "a",
147
+ kind: "settlement",
148
+ amount: 60,
149
+ });
150
+ let release!: () => void;
151
+ wire.transferGate = new Promise<void>((resolve) => {
152
+ release = resolve;
153
+ });
154
+ const sale = await t.mutation(anyApi.host.startRecovery, {
155
+ name: "a",
156
+ kind: "settlement",
157
+ requestKey: "inflight",
158
+ });
159
+ const drain = t.finishAllScheduledFunctions(vi.runAllTimers);
160
+ try {
161
+ await vi.waitFor(() => expect(posts("/v1/transfers")).toHaveLength(1));
162
+ await t.mutation(
163
+ nested.recoveryWorkflow.mutations.requestCancel,
164
+ groupRef(sale),
165
+ );
166
+ expect((await prep(t)).verified).toBe(true);
167
+ } finally {
168
+ release();
169
+ await drain;
170
+ }
171
+ expect(
172
+ await t.query(nested.recoveryWorkflow.queries.get, groupRef(sale)),
173
+ ).toMatchObject({
174
+ status: "completed",
175
+ cancelRequested: true,
176
+ outcome: "collect",
177
+ });
178
+ expect(
179
+ await t.query(nested.costRecovery.queries.claim, { claimKey: "debt:a" }),
180
+ ).toMatchObject({ collectedAmount: 60, reservedAmount: 0 });
181
+ expect(posts("/v1/transfers")).toHaveLength(1);
182
+ });
183
+ it("never dispatches a changed-version or cleaned-history funding operation while recovery succeeds", async () => {
184
+ const t = fixture();
185
+ wire.loseCreate = true;
186
+ const funding = await t.action(anyApi.host.funding, {
187
+ requestKey: "unknown-create",
188
+ });
189
+ await t.mutation(anyApi.host.prepareRecovery, {
190
+ name: "a",
191
+ kind: "settlement",
192
+ amount: 60,
193
+ });
194
+ const sale = await t.mutation(anyApi.host.startRecovery, {
195
+ name: "a",
196
+ kind: "settlement",
197
+ requestKey: "parallel-sale",
198
+ });
199
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
200
+ const reference = {
201
+ ...funding.operation,
202
+ stripeSubscriptionId: "sub_a",
203
+ providerApiVersion: "2026-09-30.preview",
204
+ definitionVersion: "balance-funding/v1",
205
+ };
206
+ expect(
207
+ await t.mutation(nested.balanceFunding.mutations.resumeWorkflow, {
208
+ ...reference,
209
+ definitionVersion: "balance-funding/v2",
210
+ }),
211
+ ).toMatchObject({ status: "conflict" });
212
+ expect(
213
+ await t.mutation(nested.balanceFunding.mutations.resumeWorkflow, {
214
+ ...reference,
215
+ providerApiVersion: "2026-06-30.preview",
216
+ }),
217
+ ).toMatchObject({ status: "conflict" });
218
+ await t.mutation(nested.crossDomainFixture.cleanFundingHistory, {
219
+ operationId: funding.operation.operationId,
220
+ });
221
+ expect(
222
+ await t.action(anyApi.host.fundingControl, {
223
+ ...fundingRef(funding.operation),
224
+ command: "resume",
225
+ }),
226
+ ).toMatchObject({ status: "unknown", reason: "history_incomplete" });
227
+ expect((await prep(t)).preparation.previousPaymentMethodId).toBe("pm_card_a");
228
+ expect(
229
+ await t.query(nested.recoveryWorkflow.queries.get, groupRef(sale)),
230
+ ).toMatchObject({ status: "completed" });
231
+ expect(posts("/v1/payment_methods")).toHaveLength(1);
232
+ expect(posts("/v1/transfers")).toHaveLength(1);
233
+ });
@@ -0,0 +1,108 @@
1
+ // @vitest-environment edge-runtime
2
+ import { anyApi, componentsGeneric } from "convex/server";
3
+ import { expect, it, vi } from "vitest";
4
+ import { fixture, scope, wire } from "../../testing/crossDomain/fixture.js";
5
+ const nested = componentsGeneric().betterStripe;
6
+ it("caps overlapping refund/dispute principal and revises only the fee after a won dispute", async () => {
7
+ const t = fixture();
8
+ await t.mutation(anyApi.host.prepareRecovery, {
9
+ name: "a",
10
+ kind: "settlement",
11
+ amount: 60,
12
+ });
13
+ await t.mutation(anyApi.host.startRecovery, {
14
+ name: "a",
15
+ kind: "settlement",
16
+ requestKey: "original-sale",
17
+ });
18
+ await t.action(anyApi.host.funding, { requestKey: "parallel-funding" });
19
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
20
+ wire.refunded = true;
21
+ wire.dispute = "lost";
22
+ const args = {
23
+ requestKey: "refund-and-lost-dispute",
24
+ expectedRevision: -1,
25
+ rollback: { groupKey: "settlement:a", expectedRevision: -1 },
26
+ };
27
+ const lost = await t.mutation(anyApi.host.costs, args);
28
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
29
+ const ref = {
30
+ ...scope(),
31
+ operationId: lost.operationId,
32
+ version: lost.version,
33
+ };
34
+ const original = await t.query(
35
+ nested.processingCostReconciliation.queries.get,
36
+ ref,
37
+ );
38
+ expect(original).toMatchObject({
39
+ status: "completed",
40
+ result: {
41
+ claim: { amountDue: 45, revision: 0 },
42
+ rollbackClaim: { amountDue: 60, revision: 0 },
43
+ },
44
+ });
45
+ const snapshot = await t.query(
46
+ nested.processingCostReconciliation.queries.snapshot,
47
+ ref,
48
+ );
49
+ expect(snapshot.result?.observation).toMatchObject({
50
+ principalReturned: 1000,
51
+ disputeFees: 15,
52
+ originalProcessingFee: 30,
53
+ provenance: { invoiceId: "in_original", stripePriceId: "price_original" },
54
+ });
55
+ const reads = wire.calls.length;
56
+ expect(await t.mutation(anyApi.host.costs, args)).toEqual(original);
57
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
58
+ expect(wire.calls).toHaveLength(reads);
59
+ wire.dispute = "won";
60
+ const wonArgs = {
61
+ ...args,
62
+ requestKey: "dispute-won",
63
+ expectedRevision: 0,
64
+ rollback: { groupKey: "settlement:a", expectedRevision: 0 },
65
+ };
66
+ const won = await t.mutation(anyApi.host.costs, wonArgs);
67
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
68
+ expect(
69
+ await t.query(nested.processingCostReconciliation.queries.get, {
70
+ ...scope(),
71
+ operationId: won.operationId,
72
+ version: won.version,
73
+ }),
74
+ ).toMatchObject({
75
+ status: "completed",
76
+ result: {
77
+ claim: { amountDue: 30, revision: 1 },
78
+ rollbackClaim: { amountDue: 60, revision: 1 },
79
+ },
80
+ });
81
+ expect(
82
+ await t.query(nested.processingCostReconciliation.queries.get, ref),
83
+ ).toEqual(original);
84
+ expect(
85
+ await t.query(nested.costRecovery.queries.balance, scope()),
86
+ ).toMatchObject({
87
+ amountDue: 150,
88
+ collectedAmount: 60,
89
+ outstanding: 90,
90
+ reservedAmount: 0,
91
+ });
92
+ const finalReads = wire.calls.length;
93
+ expect(await t.mutation(anyApi.host.costs, wonArgs)).toMatchObject({
94
+ operationId: won.operationId,
95
+ version: won.version,
96
+ status: "completed",
97
+ });
98
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
99
+ expect(wire.calls).toHaveLength(finalReads);
100
+ expect(
101
+ wire.calls.filter((r) => r.method === "POST" && r.path === "/v1/transfers"),
102
+ ).toHaveLength(1);
103
+ expect(
104
+ (await t.run((ctx) => ctx.db.query("reports").take(10))).filter(
105
+ (r) => r.domain === "cost",
106
+ ),
107
+ ).toHaveLength(2);
108
+ });