@fantastic.dev/better-stripe 0.45.0 → 0.45.2
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 +14 -0
- package/MIGRATION.md +6 -4
- package/README.md +8 -6
- package/docs/affiliate-clawbacks.md +3 -3
- package/docs/architecture/component-extraction.md +415 -0
- package/docs/architecture/durable-execution.md +200 -0
- package/docs/architecture/recovery-workflow.md +69 -0
- package/docs/balance-funding-state.md +114 -0
- package/docs/balance-funding-workflow.md +53 -0
- package/docs/example-balance-funding-cutover.md +94 -0
- package/docs/example-recovery-cutover.md +21 -0
- package/docs/processing-cost-reconciliation.md +23 -0
- package/docs/release-handoff.md +196 -0
- package/docs/releasing.md +41 -0
- package/package.json +12 -2
- package/src/component/durableExecution/crossDomain.test.ts +241 -0
- package/src/component/durableExecution/crossDomainBoundaries.test.ts +233 -0
- package/src/component/durableExecution/crossDomainDisputes.test.ts +108 -0
- package/src/component/durableExecution/crossDomainMigration.test.ts +192 -0
- package/src/component/durableExecution/crossDomainWebhooks.test.ts +122 -0
- package/src/testing/crossDomain/fixture.ts +96 -0
- package/src/testing/crossDomain/host.ts +288 -0
- package/src/testing/crossDomain/transport.ts +293 -0
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
it("preserves both legacy ledgers when new recovery collects an imported opening balance", async () => {
|
|
12
|
+
const t = fixture();
|
|
13
|
+
const fundingImport = {
|
|
14
|
+
accountId: "acct_a",
|
|
15
|
+
scopeKey: "funding:a",
|
|
16
|
+
sourceKey: "legacy:funding:original",
|
|
17
|
+
preparation: {
|
|
18
|
+
customerAccount: "acct_a",
|
|
19
|
+
stripeSubscriptionId: "sub_a",
|
|
20
|
+
paymentMethodId: "pm_balance_a",
|
|
21
|
+
previousPaymentMethodId: "pm_card_a",
|
|
22
|
+
previousPaymentMethodTypes: [],
|
|
23
|
+
},
|
|
24
|
+
consentAt: 123,
|
|
25
|
+
consentReference: "original-consent",
|
|
26
|
+
providerApiVersion: "2026-09-30.preview",
|
|
27
|
+
enabled: false,
|
|
28
|
+
operationVersion: 7,
|
|
29
|
+
pending: {
|
|
30
|
+
enabled: true,
|
|
31
|
+
providerKey: "original-legacy-apply-key",
|
|
32
|
+
requestKey: "original-legacy-request",
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const imported = await t.mutation(
|
|
36
|
+
nested.balanceFunding.mutations.importPreparation,
|
|
37
|
+
fundingImport,
|
|
38
|
+
);
|
|
39
|
+
const opening = {
|
|
40
|
+
...scope(),
|
|
41
|
+
claimKey: "legacy:cost",
|
|
42
|
+
sourceChargeId: "ch_legacy",
|
|
43
|
+
sourceGross: 1000,
|
|
44
|
+
sourceProcessingFee: 30,
|
|
45
|
+
amountDue: 100,
|
|
46
|
+
revision: 4,
|
|
47
|
+
operationKey: "legacy:collection",
|
|
48
|
+
collectedAmount: 25,
|
|
49
|
+
};
|
|
50
|
+
await t.mutation(nested.costRecovery.mutations.importClaim, opening);
|
|
51
|
+
await t.mutation(anyApi.host.prepareRecovery, {
|
|
52
|
+
name: "a",
|
|
53
|
+
kind: "settlement",
|
|
54
|
+
amount: 60,
|
|
55
|
+
});
|
|
56
|
+
const sale = await t.mutation(anyApi.host.startRecovery, {
|
|
57
|
+
name: "a",
|
|
58
|
+
kind: "settlement",
|
|
59
|
+
requestKey: "collect-openings",
|
|
60
|
+
});
|
|
61
|
+
expect(
|
|
62
|
+
await t.action(anyApi.host.funding, { requestKey: "must-not-reprepare" }),
|
|
63
|
+
).toMatchObject({ status: "conflict" });
|
|
64
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
65
|
+
expect(
|
|
66
|
+
await t.query(nested.recoveryWorkflow.queries.get, {
|
|
67
|
+
...scope(),
|
|
68
|
+
groupKey: "settlement:a",
|
|
69
|
+
operationId: sale.operationId,
|
|
70
|
+
version: sale.version,
|
|
71
|
+
}),
|
|
72
|
+
).toMatchObject({ status: "completed" });
|
|
73
|
+
expect(
|
|
74
|
+
await t.mutation(nested.costRecovery.mutations.importClaim, opening),
|
|
75
|
+
).toMatchObject({
|
|
76
|
+
collectedAmount: 100,
|
|
77
|
+
revision: 4,
|
|
78
|
+
outstanding: 0,
|
|
79
|
+
openingCollection: {
|
|
80
|
+
collectedAmount: 25,
|
|
81
|
+
operationKey: "legacy:collection",
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
expect(
|
|
85
|
+
await t.mutation(
|
|
86
|
+
nested.balanceFunding.mutations.importPreparation,
|
|
87
|
+
fundingImport,
|
|
88
|
+
),
|
|
89
|
+
).toEqual(imported);
|
|
90
|
+
const saved = await t.query(
|
|
91
|
+
nested.balanceFunding.queries.readScopePreparation,
|
|
92
|
+
{ ...fundingScope(), providerApiVersion: "2026-09-30.preview" },
|
|
93
|
+
);
|
|
94
|
+
expect(saved).toMatchObject({
|
|
95
|
+
operationVersion: 7,
|
|
96
|
+
startsPaused: true,
|
|
97
|
+
enabled: false,
|
|
98
|
+
preparation: { previousPaymentMethodTypes: [] },
|
|
99
|
+
});
|
|
100
|
+
expect(
|
|
101
|
+
await t.query(nested.balanceFunding.queries.inspectOperation, {
|
|
102
|
+
...imported.operation,
|
|
103
|
+
stripeSubscriptionId: "sub_a",
|
|
104
|
+
}),
|
|
105
|
+
).toMatchObject({
|
|
106
|
+
state: "unknown",
|
|
107
|
+
providerKeys: {
|
|
108
|
+
enable: "original-legacy-apply-key:enable",
|
|
109
|
+
disable: "original-legacy-apply-key:disable",
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
expect(
|
|
113
|
+
wire.calls.filter((r) => r.method === "POST").map((r) => r.path),
|
|
114
|
+
).toEqual(["/v1/transfers"]);
|
|
115
|
+
expect(wire.transfers[0].amount).toBe(765);
|
|
116
|
+
await expect(
|
|
117
|
+
t.mutation(nested.balanceFunding.mutations.importPreparation, {
|
|
118
|
+
...fundingImport,
|
|
119
|
+
preparation: {
|
|
120
|
+
...fundingImport.preparation,
|
|
121
|
+
previousPaymentMethodTypes: null,
|
|
122
|
+
},
|
|
123
|
+
}),
|
|
124
|
+
).rejects.toThrow();
|
|
125
|
+
});
|
|
126
|
+
it("allows only one racing refund revision to commit while funding completes independently", async () => {
|
|
127
|
+
const t = fixture();
|
|
128
|
+
wire.refunded = true;
|
|
129
|
+
const first = await t.mutation(anyApi.host.costs, {
|
|
130
|
+
requestKey: "refund-event-a",
|
|
131
|
+
expectedRevision: -1,
|
|
132
|
+
});
|
|
133
|
+
const competing = await t.mutation(anyApi.host.costs, {
|
|
134
|
+
requestKey: "refund-event-b",
|
|
135
|
+
expectedRevision: -1,
|
|
136
|
+
});
|
|
137
|
+
await t.action(anyApi.host.funding, { requestKey: "parallel-funding" });
|
|
138
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
139
|
+
const states = await Promise.all(
|
|
140
|
+
[first, competing].map((op) =>
|
|
141
|
+
t.query(nested.processingCostReconciliation.queries.get, {
|
|
142
|
+
...scope(),
|
|
143
|
+
operationId: op.operationId,
|
|
144
|
+
version: op.version,
|
|
145
|
+
}),
|
|
146
|
+
),
|
|
147
|
+
);
|
|
148
|
+
expect(states.map((s) => s.status).sort()).toEqual(["completed", "conflict"]);
|
|
149
|
+
expect(
|
|
150
|
+
await t.query(nested.costRecovery.queries.claim, {
|
|
151
|
+
claimKey: "cost:pi_source",
|
|
152
|
+
}),
|
|
153
|
+
).toMatchObject({ amountDue: 30, revision: 0 });
|
|
154
|
+
expect(
|
|
155
|
+
(await t.run((ctx) => ctx.db.query("reports").take(10))).filter(
|
|
156
|
+
(r) => r.domain === "cost",
|
|
157
|
+
),
|
|
158
|
+
).toHaveLength(1);
|
|
159
|
+
expect(
|
|
160
|
+
(
|
|
161
|
+
await t.query(nested.balanceFunding.queries.readScopePreparation, {
|
|
162
|
+
...fundingScope(),
|
|
163
|
+
providerApiVersion: "2026-09-30.preview",
|
|
164
|
+
})
|
|
165
|
+
).verified,
|
|
166
|
+
).toBe(true);
|
|
167
|
+
const requests = wire.calls.length;
|
|
168
|
+
await expect(
|
|
169
|
+
t.query(nested.processingCostReconciliation.queries.get, {
|
|
170
|
+
...scope("b"),
|
|
171
|
+
operationId: first.operationId,
|
|
172
|
+
version: first.version,
|
|
173
|
+
}),
|
|
174
|
+
).rejects.toThrow();
|
|
175
|
+
await expect(
|
|
176
|
+
t.query(nested.processingCostReconciliation.queries.get, {
|
|
177
|
+
...scope(),
|
|
178
|
+
currency: "eur",
|
|
179
|
+
operationId: first.operationId,
|
|
180
|
+
version: first.version,
|
|
181
|
+
}),
|
|
182
|
+
).rejects.toThrow();
|
|
183
|
+
expect(wire.calls).toHaveLength(requests);
|
|
184
|
+
expect(
|
|
185
|
+
wire.calls.some(
|
|
186
|
+
(r) =>
|
|
187
|
+
r.method === "POST" &&
|
|
188
|
+
r.path !== "/v1/payment_methods" &&
|
|
189
|
+
!r.path.endsWith("/attach"),
|
|
190
|
+
),
|
|
191
|
+
).toBe(false);
|
|
192
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
3
|
+
import Stripe from "stripe";
|
|
4
|
+
import { expect, it, vi } from "vitest";
|
|
5
|
+
import { handleWebhookRequest } from "../../client/webhooks/handler.js";
|
|
6
|
+
import type { Component } from "../../client/helpers.js";
|
|
7
|
+
import type { RegisterRoutesConfig } from "../../client/types.js";
|
|
8
|
+
import {
|
|
9
|
+
fixture,
|
|
10
|
+
fundingScope,
|
|
11
|
+
scope,
|
|
12
|
+
wire,
|
|
13
|
+
} from "../../testing/crossDomain/fixture.js";
|
|
14
|
+
import { ga, secret } from "../../testing/crossDomain/transport.js";
|
|
15
|
+
const nested = componentsGeneric().betterStripe;
|
|
16
|
+
it("retries a signed invoice hook and deduplicates delivery without restarting settlement or funding", async () => {
|
|
17
|
+
const t = fixture();
|
|
18
|
+
await t.mutation(anyApi.host.prepareRecovery, {
|
|
19
|
+
name: "a",
|
|
20
|
+
kind: "settlement",
|
|
21
|
+
amount: 60,
|
|
22
|
+
});
|
|
23
|
+
await t.action(anyApi.host.funding, { requestKey: "parallel-funding" });
|
|
24
|
+
const event = {
|
|
25
|
+
id: "evt_cross_domain_invoice",
|
|
26
|
+
object: "event",
|
|
27
|
+
type: "invoice.paid",
|
|
28
|
+
api_version: ga,
|
|
29
|
+
livemode: false,
|
|
30
|
+
data: {
|
|
31
|
+
object: {
|
|
32
|
+
id: "in_original",
|
|
33
|
+
object: "invoice",
|
|
34
|
+
status: "paid",
|
|
35
|
+
currency: "usd",
|
|
36
|
+
amount_due: 1000,
|
|
37
|
+
amount_paid: 1000,
|
|
38
|
+
metadata: {},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const starts: { operationId: string; version: number }[] = [];
|
|
43
|
+
const config: RegisterRoutesConfig = {
|
|
44
|
+
stripeSecretKey: secret,
|
|
45
|
+
stripeApiVersion: ga,
|
|
46
|
+
webhookSecret: "whsec_cross_domain_signature_only",
|
|
47
|
+
retryOnHookError: () => true,
|
|
48
|
+
events: {
|
|
49
|
+
"invoice.paid": async (ctx, verified) => {
|
|
50
|
+
const op = await ctx.runMutation(anyApi.host.startRecovery, {
|
|
51
|
+
name: "a",
|
|
52
|
+
kind: "settlement",
|
|
53
|
+
requestKey: verified.id,
|
|
54
|
+
});
|
|
55
|
+
starts.push(op);
|
|
56
|
+
if (starts.length === 1)
|
|
57
|
+
throw new Error("Fixture host response interrupted after scheduling");
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
const deliver = async (tamper = false) => {
|
|
62
|
+
const body = JSON.stringify(event);
|
|
63
|
+
const signature = await Stripe.webhooks.generateTestHeaderStringAsync({
|
|
64
|
+
payload: body,
|
|
65
|
+
secret: config.webhookSecret!,
|
|
66
|
+
});
|
|
67
|
+
return t.action(async (ctx) => {
|
|
68
|
+
const response = await handleWebhookRequest(
|
|
69
|
+
ctx,
|
|
70
|
+
new Request("https://example.test/stripe/webhook", {
|
|
71
|
+
method: "POST",
|
|
72
|
+
body: tamper ? body.replace("in_original", "in_tampered") : body,
|
|
73
|
+
headers: { "stripe-signature": signature },
|
|
74
|
+
}),
|
|
75
|
+
nested as unknown as Component,
|
|
76
|
+
config,
|
|
77
|
+
);
|
|
78
|
+
return { status: response.status, body: await response.json() };
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const ledger = () =>
|
|
82
|
+
t.query(nested.webhooks.queries.getWebhookEvent, {
|
|
83
|
+
stripeEventId: event.id,
|
|
84
|
+
});
|
|
85
|
+
expect((await deliver(true)).status).toBe(400);
|
|
86
|
+
expect(await ledger()).toBeNull();
|
|
87
|
+
expect(starts).toHaveLength(0);
|
|
88
|
+
expect((await deliver()).status).toBe(500);
|
|
89
|
+
expect(await ledger()).toMatchObject({ status: "failed" });
|
|
90
|
+
expect((await deliver()).status).toBe(200);
|
|
91
|
+
expect(await deliver()).toMatchObject({
|
|
92
|
+
status: 200,
|
|
93
|
+
body: { deduplicated: true },
|
|
94
|
+
});
|
|
95
|
+
expect(starts).toHaveLength(2);
|
|
96
|
+
expect(starts[1].operationId).toBe(starts[0].operationId);
|
|
97
|
+
expect(await ledger()).toMatchObject({ status: "processed" });
|
|
98
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
99
|
+
expect(
|
|
100
|
+
await t.query(nested.recoveryWorkflow.queries.get, {
|
|
101
|
+
...scope(),
|
|
102
|
+
groupKey: "settlement:a",
|
|
103
|
+
operationId: starts[0].operationId,
|
|
104
|
+
version: starts[0].version,
|
|
105
|
+
}),
|
|
106
|
+
).toMatchObject({ status: "completed" });
|
|
107
|
+
expect(
|
|
108
|
+
await t.query(nested.balanceFunding.queries.readScopePreparation, {
|
|
109
|
+
...fundingScope(),
|
|
110
|
+
providerApiVersion: "2026-09-30.preview",
|
|
111
|
+
}),
|
|
112
|
+
).toMatchObject({
|
|
113
|
+
verified: true,
|
|
114
|
+
preparation: { previousPaymentMethodId: "pm_card_a" },
|
|
115
|
+
});
|
|
116
|
+
expect(
|
|
117
|
+
wire.calls.filter((r) => r.method === "POST" && r.path === "/v1/transfers"),
|
|
118
|
+
).toHaveLength(1);
|
|
119
|
+
expect(await t.run((ctx) => ctx.db.query("reports").take(10))).toHaveLength(
|
|
120
|
+
1,
|
|
121
|
+
);
|
|
122
|
+
});
|