@fantastic.dev/better-stripe 0.29.0 → 0.30.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.
- package/CHANGELOG.md +16 -0
- package/MIGRATION.md +8 -0
- package/README.md +10 -1
- package/dist/client/connect/payoutActivationProof.d.ts.map +1 -1
- package/dist/client/connect/payoutActivationProof.js +88 -42
- package/dist/client/connect/payoutActivationProof.js.map +1 -1
- package/dist/client/connect/payoutAdoption.d.ts +3 -0
- package/dist/client/connect/payoutAdoption.d.ts.map +1 -1
- package/dist/client/connect/payoutAdoption.js +218 -183
- package/dist/client/connect/payoutAdoption.js.map +1 -1
- package/dist/client/connect/payoutAdoptionCollection.d.ts +21 -0
- package/dist/client/connect/payoutAdoptionCollection.d.ts.map +1 -0
- package/dist/client/connect/payoutAdoptionCollection.js +44 -0
- package/dist/client/connect/payoutAdoptionCollection.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +26 -6
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +13 -3
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +3745 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.js +55 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.js.map +1 -0
- package/dist/client/connect/payoutOutcomeBatches.d.ts +5 -0
- package/dist/client/connect/payoutOutcomeBatches.d.ts.map +1 -0
- package/dist/client/connect/payoutOutcomeBatches.js +21 -0
- package/dist/client/connect/payoutOutcomeBatches.js.map +1 -0
- package/dist/client/connect/payoutPolicyState.d.ts +14 -0
- package/dist/client/connect/payoutPolicyState.d.ts.map +1 -0
- package/dist/client/connect/payoutPolicyState.js +19 -0
- package/dist/client/connect/payoutPolicyState.js.map +1 -0
- package/dist/client/connect/payoutQualification.js +1 -1
- package/dist/client/connect/payoutQualification.js.map +1 -1
- package/dist/client/connect/payoutReconciliation.d.ts +11 -6
- package/dist/client/connect/payoutReconciliation.d.ts.map +1 -1
- package/dist/client/connect/payoutReconciliation.js +62 -16
- package/dist/client/connect/payoutReconciliation.js.map +1 -1
- package/dist/client/connect/payoutResumableProof.d.ts +17 -0
- package/dist/client/connect/payoutResumableProof.d.ts.map +1 -0
- package/dist/client/connect/payoutResumableProof.js +25 -0
- package/dist/client/connect/payoutResumableProof.js.map +1 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +13 -3
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/transferReceipt.d.ts +6 -2
- package/dist/client/connect/transferReceipt.d.ts.map +1 -1
- package/dist/client/connect/transferReceipt.js +3 -3
- package/dist/client/connect/transferReceipt.js.map +1 -1
- package/dist/client/payoutPolicy.d.ts.map +1 -1
- package/dist/client/payoutPolicy.js +24 -14
- package/dist/client/payoutPolicy.js.map +1 -1
- package/dist/component/_generated/component.d.ts +72 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +12 -4
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/payoutPolicy/adoption.d.ts.map +1 -1
- package/dist/component/payoutPolicy/adoption.js +12 -7
- package/dist/component/payoutPolicy/adoption.js.map +1 -1
- package/dist/component/payoutPolicy/mutations.d.ts +12 -0
- package/dist/component/payoutPolicy/mutations.d.ts.map +1 -1
- package/dist/component/payoutPolicy/queries.d.ts +2 -0
- package/dist/component/payoutPolicy/queries.d.ts.map +1 -1
- package/dist/component/payoutPolicy/schema.d.ts +31 -7
- package/dist/component/payoutPolicy/schema.d.ts.map +1 -1
- package/dist/component/payoutPolicy/schema.js +2 -0
- package/dist/component/payoutPolicy/schema.js.map +1 -1
- package/dist/component/payoutRelease/adoption.d.ts +3 -1
- package/dist/component/payoutRelease/adoption.d.ts.map +1 -1
- package/dist/component/payoutRelease/adoption.js +33 -16
- package/dist/component/payoutRelease/adoption.js.map +1 -1
- package/dist/component/payoutRelease/fees.d.ts.map +1 -1
- package/dist/component/payoutRelease/fees.js +7 -0
- package/dist/component/payoutRelease/fees.js.map +1 -1
- package/dist/component/payoutRelease/mutations.d.ts +59 -0
- package/dist/component/payoutRelease/mutations.d.ts.map +1 -1
- package/dist/component/payoutRelease/mutations.js +25 -80
- package/dist/component/payoutRelease/mutations.js.map +1 -1
- package/dist/component/payoutRelease/outcome.d.ts +69 -0
- package/dist/component/payoutRelease/outcome.d.ts.map +1 -0
- package/dist/component/payoutRelease/outcome.js +94 -0
- package/dist/component/payoutRelease/outcome.js.map +1 -0
- package/dist/component/payoutRelease/planning.d.ts.map +1 -1
- package/dist/component/payoutRelease/planning.js +5 -2
- package/dist/component/payoutRelease/planning.js.map +1 -1
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/payoutRelease/queries.js +2 -0
- package/dist/component/payoutRelease/queries.js.map +1 -1
- package/dist/component/payoutSnapshots/adoption.d.ts +88 -0
- package/dist/component/payoutSnapshots/adoption.d.ts.map +1 -0
- package/dist/component/payoutSnapshots/adoption.js +49 -0
- package/dist/component/payoutSnapshots/adoption.js.map +1 -0
- package/dist/component/payoutSnapshots/financial.d.ts +31 -19
- package/dist/component/payoutSnapshots/financial.d.ts.map +1 -1
- package/dist/component/payoutSnapshots/financial.js +28 -14
- package/dist/component/payoutSnapshots/financial.js.map +1 -1
- package/dist/component/payoutSnapshots/localState.d.ts +20 -16
- package/dist/component/payoutSnapshots/localState.d.ts.map +1 -1
- package/dist/component/payoutSnapshots/localState.js +13 -4
- package/dist/component/payoutSnapshots/localState.js.map +1 -1
- package/dist/component/payoutSnapshots/mutations.d.ts.map +1 -1
- package/dist/component/payoutSnapshots/mutations.js +1 -1
- package/dist/component/payoutSnapshots/mutations.js.map +1 -1
- package/dist/component/payoutSnapshots/queries.d.ts.map +1 -1
- package/dist/component/payoutSnapshots/queries.js +6 -4
- package/dist/component/payoutSnapshots/queries.js.map +1 -1
- package/dist/component/payoutSnapshots/schema.d.ts +5 -1
- package/dist/component/payoutSnapshots/schema.d.ts.map +1 -1
- package/dist/component/payoutSnapshots/schema.js +2 -0
- package/dist/component/payoutSnapshots/schema.js.map +1 -1
- package/dist/component/schema.d.ts +13 -3
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/lib/payout-format.d.ts +5 -0
- package/dist/react/lib/payout-format.d.ts.map +1 -0
- package/dist/react/lib/payout-format.js +13 -0
- package/dist/react/lib/payout-format.js.map +1 -0
- package/dist/react/workflows/Payouts.js +4 -4
- package/dist/react/workflows/Payouts.js.map +1 -1
- package/docs/react-workflows.md +9 -4
- package/docs/settlement.md +61 -8
- package/package.json +1 -1
- package/src/client/connect/payoutActivationProof.ts +123 -53
- package/src/client/connect/payoutAdoption.ts +299 -215
- package/src/client/connect/payoutAdoptionCollection.ts +78 -0
- package/src/client/connect/payoutAdoptionConcurrency.test.ts +266 -0
- package/src/client/connect/payoutCacheIdentity.test.ts +93 -0
- package/src/client/connect/payoutLargeAdoption.test.ts +541 -0
- package/src/client/connect/payoutLargeAdoptionTestHelpers.ts +74 -0
- package/src/client/connect/payoutOutcomeBatches.test.ts +40 -0
- package/src/client/connect/payoutOutcomeBatches.ts +24 -0
- package/src/client/connect/payoutPolicyState.ts +36 -0
- package/src/client/connect/payoutQualification.ts +10 -4
- package/src/client/connect/payoutReconciliation.ts +86 -7
- package/src/client/connect/payoutResumableProof.ts +41 -0
- package/src/client/connect/transferReceipt.ts +3 -2
- package/src/client/connect/transferReceiptBaseline.test.ts +55 -0
- package/src/client/payoutPolicy.ts +37 -23
- package/src/component/_generated/api.ts +4 -0
- package/src/component/_generated/component.ts +85 -0
- package/src/component/payout-snapshots.test.ts +45 -1
- package/src/component/payoutPolicy/adoption.ts +20 -7
- package/src/component/payoutPolicy/schema.ts +2 -0
- package/src/component/payoutRelease/adoption.ts +57 -27
- package/src/component/payoutRelease/fees.ts +9 -0
- package/src/component/payoutRelease/mutations.ts +26 -96
- package/src/component/payoutRelease/outcome.ts +119 -0
- package/src/component/payoutRelease/planning.ts +5 -2
- package/src/component/payoutRelease/queries.ts +2 -0
- package/src/component/payoutSnapshots/adoption.ts +73 -0
- package/src/component/payoutSnapshots/financial.ts +33 -20
- package/src/component/payoutSnapshots/localState.ts +17 -3
- package/src/component/payoutSnapshots/mutations.ts +8 -1
- package/src/component/payoutSnapshots/queries.ts +8 -6
- package/src/component/payoutSnapshots/schema.ts +2 -0
- package/src/react/index.ts +1 -0
- package/src/react/lib/payout-format.ts +16 -0
- package/src/react/workflows/Payouts.tsx +7 -5
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import { large, withHistory } from "./payoutLargeAdoptionTestHelpers.js";
|
|
2
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
3
|
+
import { makeFunctionReference } from "convex/server";
|
|
4
|
+
// @vitest-environment edge-runtime
|
|
5
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
6
|
+
afterEach(() => vi.restoreAllMocks());
|
|
7
|
+
import type Stripe from "stripe";
|
|
8
|
+
import { adoptRecipientPayouts } from "./payoutAdoption.js";
|
|
9
|
+
import { scope, component } from "./payoutAdoptionTestHelpers.js";
|
|
10
|
+
it("verifies all 205 existing funding sources through public adoption without a lifetime cutoff", async () => {
|
|
11
|
+
const s = await large();
|
|
12
|
+
const result = await adoptRecipientPayouts(
|
|
13
|
+
s.provider as unknown as Stripe,
|
|
14
|
+
component,
|
|
15
|
+
s.ctx,
|
|
16
|
+
{
|
|
17
|
+
accountId: scope.accountId,
|
|
18
|
+
ownerKey: scope.ownerKey,
|
|
19
|
+
currency: "usd",
|
|
20
|
+
expectedRevision: 0,
|
|
21
|
+
payouts: [],
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
expect(result).toMatchObject({ status: "adopted" });
|
|
25
|
+
if (result.status !== "adopted")
|
|
26
|
+
throw new Error("Expected complete adoption verification");
|
|
27
|
+
expect(result.observations).toHaveLength(205);
|
|
28
|
+
expect(result.oldestHeldAt).toBe(100000);
|
|
29
|
+
expect(s.provider.payouts.create).not.toHaveBeenCalled();
|
|
30
|
+
expect(
|
|
31
|
+
await s.t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
|
|
32
|
+
).toEqual([]);
|
|
33
|
+
expect(
|
|
34
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
35
|
+
).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("atomically adopts 101 prior payouts against 205 verified sources and replays without duplicate principal", async () => {
|
|
39
|
+
const { s, adopt } = await withHistory();
|
|
40
|
+
const result = await adopt();
|
|
41
|
+
expect(result).toMatchObject({ status: "adopted" });
|
|
42
|
+
if (result.status !== "adopted")
|
|
43
|
+
throw Error("Expected complete historical import");
|
|
44
|
+
expect(result.claims).toHaveLength(101);
|
|
45
|
+
const principal = () =>
|
|
46
|
+
s.t.run(async (ctx) =>
|
|
47
|
+
(await ctx.db.query("payoutSourceAllocations").collect()).reduce(
|
|
48
|
+
(n, a) => n + a.paidAmount,
|
|
49
|
+
0,
|
|
50
|
+
),
|
|
51
|
+
);
|
|
52
|
+
expect(await principal()).toBe(1010);
|
|
53
|
+
expect(await adopt()).toMatchObject({ status: "adopted" });
|
|
54
|
+
expect(await principal()).toBe(1010);
|
|
55
|
+
expect(
|
|
56
|
+
await s.t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
|
|
57
|
+
).toHaveLength(101);
|
|
58
|
+
expect(
|
|
59
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
60
|
+
).toEqual([]);
|
|
61
|
+
expect(s.provider.payouts.create).not.toHaveBeenCalled();
|
|
62
|
+
}, 15000);
|
|
63
|
+
|
|
64
|
+
it("activates an adopted 205-source and 101-payout history then releases only 1040 unpaid cents", async () => {
|
|
65
|
+
const { s, adopt } = await withHistory();
|
|
66
|
+
const now = Math.floor(Date.now() / 1000);
|
|
67
|
+
s.setTransactions(
|
|
68
|
+
s
|
|
69
|
+
.transactions()
|
|
70
|
+
.map((t) =>
|
|
71
|
+
t.type === "payment"
|
|
72
|
+
? { ...t, created: now - 100, available_on: now - 50 }
|
|
73
|
+
: t,
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
expect(await adopt()).toMatchObject({ status: "adopted" });
|
|
77
|
+
let schedule: Record<string, unknown> = {
|
|
78
|
+
interval: "weekly",
|
|
79
|
+
weekly_payout_days: ["monday"],
|
|
80
|
+
};
|
|
81
|
+
Object.assign(s.provider, {
|
|
82
|
+
v2: {
|
|
83
|
+
core: {
|
|
84
|
+
accounts: {
|
|
85
|
+
retrieve: async () => ({
|
|
86
|
+
id: scope.accountId,
|
|
87
|
+
dashboard: "none",
|
|
88
|
+
identity: { country: "US" },
|
|
89
|
+
}),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
balanceSettings: {
|
|
94
|
+
retrieve: async () => ({
|
|
95
|
+
payments: { payouts: { status: "enabled", schedule } },
|
|
96
|
+
}),
|
|
97
|
+
update: async (args: {
|
|
98
|
+
payments: { payouts: { schedule: Record<string, unknown> } };
|
|
99
|
+
}) => {
|
|
100
|
+
schedule = args.payments.payouts.schedule;
|
|
101
|
+
return { payments: { payouts: { status: "enabled", schedule } } };
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
const { PayoutPolicyClient } = await import("../payoutPolicy.js");
|
|
106
|
+
const client = new PayoutPolicyClient(
|
|
107
|
+
component,
|
|
108
|
+
() => s.provider as unknown as Stripe,
|
|
109
|
+
);
|
|
110
|
+
const opts = {
|
|
111
|
+
accountId: scope.accountId,
|
|
112
|
+
ownerKey: scope.ownerKey,
|
|
113
|
+
operationKey: "large-activate",
|
|
114
|
+
expectedRevision: 0,
|
|
115
|
+
mode: "custom" as const,
|
|
116
|
+
thresholds: [{ currency: "usd", amount: 1000 }],
|
|
117
|
+
consent: true as const,
|
|
118
|
+
adoptExistingBalance: true as const,
|
|
119
|
+
};
|
|
120
|
+
expect(await client.prepare(s.ctx, opts)).toMatchObject({
|
|
121
|
+
status: "prepared",
|
|
122
|
+
});
|
|
123
|
+
expect(
|
|
124
|
+
await client.apply(s.ctx, { ...scope, operationKey: opts.operationKey }),
|
|
125
|
+
).toMatchObject({ status: "ready" });
|
|
126
|
+
expect(await client.release(s.ctx, scope)).toMatchObject({
|
|
127
|
+
status: "updated",
|
|
128
|
+
claim: { status: "paid", amount: 1040 },
|
|
129
|
+
});
|
|
130
|
+
expect(s.provider.payouts.create).toHaveBeenCalledTimes(1);
|
|
131
|
+
expect(
|
|
132
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
133
|
+
).toEqual([]);
|
|
134
|
+
}, 15000);
|
|
135
|
+
|
|
136
|
+
it("renews verified source observations after a slow complete adoption history scan", async () => {
|
|
137
|
+
const s = await large();
|
|
138
|
+
let clock = Date.now();
|
|
139
|
+
vi.spyOn(Date, "now").mockImplementation(() => clock);
|
|
140
|
+
const retrieve = s.provider.transfers.retrieve.getMockImplementation()!;
|
|
141
|
+
s.provider.transfers.retrieve.mockImplementation(async (id) => {
|
|
142
|
+
clock += 1000;
|
|
143
|
+
return retrieve(id);
|
|
144
|
+
});
|
|
145
|
+
const result = await adoptRecipientPayouts(
|
|
146
|
+
s.provider as unknown as Stripe,
|
|
147
|
+
component,
|
|
148
|
+
s.ctx,
|
|
149
|
+
{
|
|
150
|
+
accountId: scope.accountId,
|
|
151
|
+
ownerKey: scope.ownerKey,
|
|
152
|
+
currency: "usd",
|
|
153
|
+
expectedRevision: 0,
|
|
154
|
+
payouts: [],
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
expect(result).toMatchObject({ status: "adopted" });
|
|
158
|
+
if (result.status !== "adopted")
|
|
159
|
+
throw Error("Expected complete verification");
|
|
160
|
+
expect(result.observations.every((o) => clock - o.observedAt <= 30000)).toBe(
|
|
161
|
+
true,
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("bounds concurrent source receipt verification to five while making progress", async () => {
|
|
166
|
+
const s = await large();
|
|
167
|
+
let active = 0,
|
|
168
|
+
maximum = 0;
|
|
169
|
+
const retrieve = s.provider.transfers.retrieve.getMockImplementation()!;
|
|
170
|
+
s.provider.transfers.retrieve.mockImplementation(async (id) => {
|
|
171
|
+
active++;
|
|
172
|
+
maximum = Math.max(maximum, active);
|
|
173
|
+
try {
|
|
174
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
175
|
+
return await retrieve(id);
|
|
176
|
+
} finally {
|
|
177
|
+
active--;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
const result = await adoptRecipientPayouts(
|
|
181
|
+
s.provider as unknown as Stripe,
|
|
182
|
+
component,
|
|
183
|
+
s.ctx,
|
|
184
|
+
{
|
|
185
|
+
accountId: scope.accountId,
|
|
186
|
+
ownerKey: scope.ownerKey,
|
|
187
|
+
currency: "usd",
|
|
188
|
+
expectedRevision: 0,
|
|
189
|
+
payouts: [],
|
|
190
|
+
},
|
|
191
|
+
);
|
|
192
|
+
expect(result).toMatchObject({ status: "adopted" });
|
|
193
|
+
expect(maximum).toBeGreaterThan(1);
|
|
194
|
+
expect(maximum).toBeLessThanOrEqual(5);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("rejects positive funding added after verification before historical import, without partial allocations", async () => {
|
|
198
|
+
const { s, adopt } = await withHistory();
|
|
199
|
+
const mutation = s.ctx.runMutation!;
|
|
200
|
+
vi.spyOn(s.ctx, "runMutation").mockImplementation(async (...args) => {
|
|
201
|
+
const ref = args[0] as unknown as Record<symbol, string>;
|
|
202
|
+
if (
|
|
203
|
+
ref[Symbol.for("toReferencePath")].endsWith(
|
|
204
|
+
"payoutRelease/mutations/adoptHistoricalBatch",
|
|
205
|
+
)
|
|
206
|
+
)
|
|
207
|
+
await s.t.mutation(
|
|
208
|
+
makeFunctionReference<"mutation">("connect/mutations:upsertTransfer"),
|
|
209
|
+
{
|
|
210
|
+
stripeTransferId: "tr_late",
|
|
211
|
+
destinationAccountId: scope.accountId,
|
|
212
|
+
amount: 1,
|
|
213
|
+
currency: "usd",
|
|
214
|
+
status: "paid",
|
|
215
|
+
role: "store",
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
return mutation(args[0], args[1] ?? {});
|
|
219
|
+
});
|
|
220
|
+
await expect(adopt()).rejects.toThrow(/Adoption collection changed/);
|
|
221
|
+
expect(
|
|
222
|
+
await s.t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
|
|
223
|
+
).toEqual([]);
|
|
224
|
+
expect(
|
|
225
|
+
await s.t.run((ctx) => ctx.db.query("payoutSourceAllocations").collect()),
|
|
226
|
+
).toEqual([]);
|
|
227
|
+
expect(
|
|
228
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
229
|
+
).toEqual([]);
|
|
230
|
+
expect(s.provider.payouts.create).not.toHaveBeenCalled();
|
|
231
|
+
});
|
|
232
|
+
it("keeps every currency's activation observations fresh after a complete slow multi-currency proof", async () => {
|
|
233
|
+
const s = await large();
|
|
234
|
+
const initial = Date.now(),
|
|
235
|
+
now = Math.floor(initial / 1000);
|
|
236
|
+
await s.t.run(async (ctx) => {
|
|
237
|
+
for (const row of await ctx.db.query("transfers").collect())
|
|
238
|
+
if (Number(row.stripeTransferId.slice(3)) % 2)
|
|
239
|
+
await ctx.db.patch(row._id, { currency: "eur" });
|
|
240
|
+
});
|
|
241
|
+
s.setTransactions(
|
|
242
|
+
s.transactions().map((t, i) => ({
|
|
243
|
+
...t,
|
|
244
|
+
currency: i % 2 ? "eur" : "usd",
|
|
245
|
+
created: now - 100,
|
|
246
|
+
available_on: now - 50,
|
|
247
|
+
})),
|
|
248
|
+
);
|
|
249
|
+
let clock = initial;
|
|
250
|
+
vi.spyOn(Date, "now").mockImplementation(() => clock);
|
|
251
|
+
const transfer = s.provider.transfers.retrieve.getMockImplementation()!,
|
|
252
|
+
charge = s.provider.charges.retrieve.getMockImplementation()!;
|
|
253
|
+
s.provider.transfers.retrieve.mockImplementation(async (id) => {
|
|
254
|
+
clock += 250;
|
|
255
|
+
return {
|
|
256
|
+
...(await transfer(id)),
|
|
257
|
+
currency: Number(id.slice(3)) % 2 ? "eur" : "usd",
|
|
258
|
+
};
|
|
259
|
+
});
|
|
260
|
+
s.provider.charges.retrieve.mockImplementation(async (id) => ({
|
|
261
|
+
...(await charge(id)),
|
|
262
|
+
currency: Number(id.slice(3)) % 2 ? "eur" : "usd",
|
|
263
|
+
}));
|
|
264
|
+
s.provider.balance.retrieve.mockImplementation(async () => ({
|
|
265
|
+
available: ["usd", "eur"].map((currency) => {
|
|
266
|
+
const amount = s
|
|
267
|
+
.transactions()
|
|
268
|
+
.filter((t) => t.currency === currency)
|
|
269
|
+
.reduce((n, t) => n + t.net, 0);
|
|
270
|
+
return { currency, amount, source_types: { card: amount } };
|
|
271
|
+
}),
|
|
272
|
+
pending: [],
|
|
273
|
+
}));
|
|
274
|
+
Object.assign(s.provider, {
|
|
275
|
+
v2: {
|
|
276
|
+
core: {
|
|
277
|
+
accounts: {
|
|
278
|
+
retrieve: async () => ({
|
|
279
|
+
id: scope.accountId,
|
|
280
|
+
dashboard: "none",
|
|
281
|
+
identity: { country: "US" },
|
|
282
|
+
}),
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
balanceSettings: {
|
|
287
|
+
retrieve: async () => ({
|
|
288
|
+
payments: {
|
|
289
|
+
payouts: {
|
|
290
|
+
status: "enabled",
|
|
291
|
+
schedule: { interval: "weekly", weekly_payout_days: ["monday"] },
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
}),
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
const { PayoutPolicyClient } = await import("../payoutPolicy.js");
|
|
298
|
+
const client = new PayoutPolicyClient(
|
|
299
|
+
component,
|
|
300
|
+
() => s.provider as unknown as Stripe,
|
|
301
|
+
);
|
|
302
|
+
expect(
|
|
303
|
+
await client.prepare(s.ctx, {
|
|
304
|
+
accountId: scope.accountId,
|
|
305
|
+
ownerKey: scope.ownerKey,
|
|
306
|
+
operationKey: "mixed-activate",
|
|
307
|
+
expectedRevision: 0,
|
|
308
|
+
mode: "custom",
|
|
309
|
+
thresholds: [
|
|
310
|
+
{ currency: "usd", amount: 1000 },
|
|
311
|
+
{ currency: "eur", amount: 1000 },
|
|
312
|
+
],
|
|
313
|
+
consent: true,
|
|
314
|
+
adoptExistingBalance: true,
|
|
315
|
+
}),
|
|
316
|
+
).toMatchObject({ status: "prepared" });
|
|
317
|
+
expect(
|
|
318
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
319
|
+
).toEqual([]);
|
|
320
|
+
});
|
|
321
|
+
it("resumes a pending adoption collection without duplicating prior paid principal", async () => {
|
|
322
|
+
const { s, opts } = await withHistory();
|
|
323
|
+
const adopt = (maxPages: number) =>
|
|
324
|
+
adoptRecipientPayouts(s.provider as unknown as Stripe, component, s.ctx, {
|
|
325
|
+
...opts,
|
|
326
|
+
maxPages,
|
|
327
|
+
});
|
|
328
|
+
expect(await adopt(10)).toMatchObject({ status: "adopted" });
|
|
329
|
+
expect(await adopt(1)).toMatchObject({
|
|
330
|
+
status: "held",
|
|
331
|
+
reason: "collection_pending",
|
|
332
|
+
});
|
|
333
|
+
expect(
|
|
334
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
335
|
+
).toHaveLength(1);
|
|
336
|
+
expect(
|
|
337
|
+
await s.t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
|
|
338
|
+
).toHaveLength(101);
|
|
339
|
+
expect(await adopt(10)).toMatchObject({ status: "adopted" });
|
|
340
|
+
expect(
|
|
341
|
+
await s.t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
|
|
342
|
+
).toHaveLength(101);
|
|
343
|
+
expect(
|
|
344
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
345
|
+
).toEqual([]);
|
|
346
|
+
expect(s.provider.payouts.create).not.toHaveBeenCalled();
|
|
347
|
+
}, 15000);
|
|
348
|
+
it("discards a liability-invalidated collection and allows a fresh non-paying adoption", async () => {
|
|
349
|
+
const { s, opts } = await withHistory();
|
|
350
|
+
const adopt = (maxPages: number) =>
|
|
351
|
+
adoptRecipientPayouts(s.provider as unknown as Stripe, component, s.ctx, {
|
|
352
|
+
...opts,
|
|
353
|
+
maxPages,
|
|
354
|
+
});
|
|
355
|
+
expect(await adopt(10)).toMatchObject({ status: "adopted" });
|
|
356
|
+
expect(await adopt(1)).toMatchObject({
|
|
357
|
+
status: "held",
|
|
358
|
+
reason: "collection_pending",
|
|
359
|
+
});
|
|
360
|
+
await s.t.mutation(
|
|
361
|
+
makeFunctionReference<"mutation">("costRecovery/mutations:record"),
|
|
362
|
+
{
|
|
363
|
+
accountId: scope.accountId,
|
|
364
|
+
scopeKey: "user:owner",
|
|
365
|
+
currency: "usd",
|
|
366
|
+
claimKey: "refund:new",
|
|
367
|
+
sourceChargeId: "ch_refunded",
|
|
368
|
+
sourceGross: 100,
|
|
369
|
+
sourceProcessingFee: 1,
|
|
370
|
+
amountDue: 1,
|
|
371
|
+
revision: 1,
|
|
372
|
+
},
|
|
373
|
+
);
|
|
374
|
+
expect(await adopt(10)).toMatchObject({
|
|
375
|
+
status: "held",
|
|
376
|
+
reason: "collection_invalidated",
|
|
377
|
+
});
|
|
378
|
+
expect(
|
|
379
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
380
|
+
).toEqual([]);
|
|
381
|
+
expect(await adopt(10)).toMatchObject({ status: "adopted" });
|
|
382
|
+
expect(
|
|
383
|
+
await s.t.run((ctx) => ctx.db.query("recoveryBalances").first()),
|
|
384
|
+
).toMatchObject({ amountDue: 1, collectedAmount: 0 });
|
|
385
|
+
expect(s.provider.payouts.create).not.toHaveBeenCalled();
|
|
386
|
+
}, 15000);
|
|
387
|
+
|
|
388
|
+
it("imports more than 100 provider payouts even when only two sources funded them", async () => {
|
|
389
|
+
const { s, adopt } = await withHistory(2, 1000);
|
|
390
|
+
const result = await adopt();
|
|
391
|
+
expect(result).toMatchObject({ status: "adopted" });
|
|
392
|
+
if (result.status !== "adopted") throw Error("Expected full import");
|
|
393
|
+
expect(result.claims).toHaveLength(101);
|
|
394
|
+
expect(
|
|
395
|
+
await s.t.run((ctx) => ctx.db.query("payoutSourceAllocations").collect()),
|
|
396
|
+
).toHaveLength(2);
|
|
397
|
+
expect(s.provider.payouts.create).not.toHaveBeenCalled();
|
|
398
|
+
});
|
|
399
|
+
it("cleans an invalidated activation collection so a fresh proof can be collected", async () => {
|
|
400
|
+
const s = await large();
|
|
401
|
+
const { adoptionCollectionKey, discardAdoptionProof } =
|
|
402
|
+
await import("./payoutAdoptionCollection.js");
|
|
403
|
+
const { collectPayoutSnapshot } = await import("./payoutCollection.js");
|
|
404
|
+
const { collectAdoptionProof } = await import("./payoutActivationProof.js");
|
|
405
|
+
const key = await adoptionCollectionKey(scope, "enable", "import:usd");
|
|
406
|
+
await collectPayoutSnapshot(component, s.ctx, {
|
|
407
|
+
accountId: scope.accountId,
|
|
408
|
+
ownerKey: scope.ownerKey,
|
|
409
|
+
operationKey: key,
|
|
410
|
+
expectedRevision: 0,
|
|
411
|
+
});
|
|
412
|
+
await s.t.mutation(
|
|
413
|
+
makeFunctionReference<"mutation">("costRecovery/mutations:record"),
|
|
414
|
+
{
|
|
415
|
+
accountId: scope.accountId,
|
|
416
|
+
scopeKey: "user:owner",
|
|
417
|
+
currency: "usd",
|
|
418
|
+
claimKey: "refund:new",
|
|
419
|
+
sourceChargeId: "ch_refunded",
|
|
420
|
+
sourceGross: 100,
|
|
421
|
+
sourceProcessingFee: 1,
|
|
422
|
+
amountDue: 1,
|
|
423
|
+
revision: 1,
|
|
424
|
+
},
|
|
425
|
+
);
|
|
426
|
+
const collect = () =>
|
|
427
|
+
collectAdoptionProof(
|
|
428
|
+
s.provider as unknown as Stripe,
|
|
429
|
+
component,
|
|
430
|
+
s.ctx,
|
|
431
|
+
scope,
|
|
432
|
+
0,
|
|
433
|
+
["usd"],
|
|
434
|
+
);
|
|
435
|
+
expect(await collect()).toMatchObject({
|
|
436
|
+
status: "held",
|
|
437
|
+
reason: "collection_invalidated",
|
|
438
|
+
});
|
|
439
|
+
expect(
|
|
440
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
441
|
+
).toEqual([]);
|
|
442
|
+
const fresh = await collect();
|
|
443
|
+
expect(fresh).toMatchObject({ status: "verified" });
|
|
444
|
+
if (fresh.status !== "verified") throw Error("Expected fresh proof");
|
|
445
|
+
await discardAdoptionProof(component, s.ctx, scope, fresh.proof);
|
|
446
|
+
expect(
|
|
447
|
+
await s.t.run((ctx) => ctx.db.query("payoutSnapshots").collect()),
|
|
448
|
+
).toEqual([]);
|
|
449
|
+
});
|
|
450
|
+
it("retains each historical payout's complete allocated-source proof without copying unrelated proofs", async () => {
|
|
451
|
+
const { adopt } = await withHistory();
|
|
452
|
+
const result = await adopt();
|
|
453
|
+
expect(result).toMatchObject({ status: "adopted" });
|
|
454
|
+
if (result.status !== "adopted") throw Error("Expected import");
|
|
455
|
+
expect(result.observations).toHaveLength(205);
|
|
456
|
+
for (const claim of result.claims)
|
|
457
|
+
expect(claim.observations.map((o) => o.stripeTransferId).sort()).toEqual(
|
|
458
|
+
claim.allocations.map((a) => a.stripeTransferId).sort(),
|
|
459
|
+
);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("reconciles 101 known payouts with bounded outcome transactions", async () => {
|
|
463
|
+
const { s, adopt } = await withHistory();
|
|
464
|
+
expect(await adopt()).toMatchObject({ status: "adopted" });
|
|
465
|
+
const mutation = s.ctx.runMutation!;
|
|
466
|
+
let outcomeTransactions = 0;
|
|
467
|
+
s.ctx.runMutation = async (ref, ...args) => {
|
|
468
|
+
const path = (ref as Record<symbol, string>)[Symbol.for("toReferencePath")];
|
|
469
|
+
if (/payoutRelease\/mutations\/recordOutcomes?$/.test(path))
|
|
470
|
+
outcomeTransactions++;
|
|
471
|
+
return mutation(ref, ...args);
|
|
472
|
+
};
|
|
473
|
+
expect(await adopt()).toMatchObject({ status: "adopted" });
|
|
474
|
+
expect(outcomeTransactions).toBeGreaterThan(0);
|
|
475
|
+
expect(outcomeTransactions).toBeLessThanOrEqual(5);
|
|
476
|
+
expect(
|
|
477
|
+
await s.t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
|
|
478
|
+
).toHaveLength(101);
|
|
479
|
+
}, 15000);
|
|
480
|
+
|
|
481
|
+
it("rolls back a whole outcome batch if a later receipt is invalid", async () => {
|
|
482
|
+
const { s, adopt } = await withHistory();
|
|
483
|
+
expect(await adopt()).toMatchObject({ status: "adopted" });
|
|
484
|
+
const state = () =>
|
|
485
|
+
s.t.run(async (ctx) => ({
|
|
486
|
+
claims: await ctx.db.query("payoutReleaseClaims").collect(),
|
|
487
|
+
allocations: await ctx.db.query("payoutSourceAllocations").collect(),
|
|
488
|
+
}));
|
|
489
|
+
const before = await state();
|
|
490
|
+
const [first, second] = before.claims;
|
|
491
|
+
const outcome = (c: typeof first) => ({
|
|
492
|
+
accountId: c.accountId,
|
|
493
|
+
ownerKey: c.ownerKey,
|
|
494
|
+
operationKey: c.operationKey,
|
|
495
|
+
stripePayoutId: c.stripePayoutId!,
|
|
496
|
+
amount: c.amount,
|
|
497
|
+
currency: c.currency,
|
|
498
|
+
providerStatus: "paid" as const,
|
|
499
|
+
debitTransactionId: c.debitTransactionId,
|
|
500
|
+
debitFee: c.debitFee,
|
|
501
|
+
});
|
|
502
|
+
await expect(
|
|
503
|
+
s.ctx.runMutation!(
|
|
504
|
+
componentRef(component, "payoutRelease/mutations/recordOutcomes"),
|
|
505
|
+
{
|
|
506
|
+
accountId: scope.accountId,
|
|
507
|
+
ownerKey: scope.ownerKey,
|
|
508
|
+
outcomes: [
|
|
509
|
+
{
|
|
510
|
+
...outcome(first),
|
|
511
|
+
providerStatus: "failed",
|
|
512
|
+
returnTransactionId: "txn_atomic_return",
|
|
513
|
+
returnFee: 0,
|
|
514
|
+
},
|
|
515
|
+
{ ...outcome(second), amount: second.amount + 1 },
|
|
516
|
+
],
|
|
517
|
+
},
|
|
518
|
+
),
|
|
519
|
+
).rejects.toThrow("identity or amount");
|
|
520
|
+
expect(await state()).toEqual(before);
|
|
521
|
+
}, 15000);
|
|
522
|
+
|
|
523
|
+
it("bounds parallel historical receipt reads to five", async () => {
|
|
524
|
+
const { s, adopt } = await withHistory();
|
|
525
|
+
const retrieve = s.provider.payouts.retrieve.getMockImplementation()!;
|
|
526
|
+
let active = 0,
|
|
527
|
+
peak = 0;
|
|
528
|
+
s.provider.payouts.retrieve.mockImplementation(async (id) => {
|
|
529
|
+
active++;
|
|
530
|
+
peak = Math.max(peak, active);
|
|
531
|
+
try {
|
|
532
|
+
await Promise.resolve();
|
|
533
|
+
return await retrieve(id);
|
|
534
|
+
} finally {
|
|
535
|
+
active--;
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
expect(await adopt()).toMatchObject({ status: "adopted" });
|
|
539
|
+
expect(peak).toBeGreaterThan(1);
|
|
540
|
+
expect(peak).toBeLessThanOrEqual(5);
|
|
541
|
+
}, 15000);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import { adoptRecipientPayouts } from "./payoutAdoption.js";
|
|
3
|
+
import { setup, scope, component } from "./payoutAdoptionTestHelpers.js";
|
|
4
|
+
export async function large(count = 205, amount = 10) {
|
|
5
|
+
const s = await setup(Array.from({ length: count }, () => amount));
|
|
6
|
+
s.provider.balanceTransactions.list.mockImplementation(async (params) => {
|
|
7
|
+
const p = params as typeof params & {
|
|
8
|
+
currency?: string;
|
|
9
|
+
starting_after?: string;
|
|
10
|
+
};
|
|
11
|
+
const all = s
|
|
12
|
+
.transactions()
|
|
13
|
+
.filter(
|
|
14
|
+
(t) =>
|
|
15
|
+
(!p.source || t.source === p.source) &&
|
|
16
|
+
(!p.currency || t.currency === p.currency),
|
|
17
|
+
);
|
|
18
|
+
const start = p.starting_after
|
|
19
|
+
? all.findIndex((t) => t.id === p.starting_after) + 1
|
|
20
|
+
: 0;
|
|
21
|
+
const data = all.slice(start, start + (p.limit ?? 100));
|
|
22
|
+
return { data, has_more: start + data.length < all.length };
|
|
23
|
+
});
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function withHistory(count = 205, amount = 10) {
|
|
28
|
+
const s = await large(count, amount);
|
|
29
|
+
const payouts: {
|
|
30
|
+
stripePayoutId: string;
|
|
31
|
+
allocations: { stripeTransferId: string; amount: number }[];
|
|
32
|
+
}[] = [];
|
|
33
|
+
const history: Record<string, unknown>[] = [];
|
|
34
|
+
for (let i = 0; i < 101; i++) {
|
|
35
|
+
const p = await s.provider.payouts.create({
|
|
36
|
+
amount: 10,
|
|
37
|
+
currency: "usd",
|
|
38
|
+
metadata: {},
|
|
39
|
+
});
|
|
40
|
+
history.push(p);
|
|
41
|
+
payouts.push({
|
|
42
|
+
stripePayoutId: String(p.id),
|
|
43
|
+
allocations: [{ stripeTransferId: `tr_${i % count}`, amount: 10 }],
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
s.provider.payouts.create.mockClear();
|
|
47
|
+
s.provider.payouts.list.mockImplementation(async (...args: unknown[]) => {
|
|
48
|
+
const params = (args[0] ?? {}) as {
|
|
49
|
+
limit?: number;
|
|
50
|
+
starting_after?: string;
|
|
51
|
+
};
|
|
52
|
+
const all = [...history].reverse();
|
|
53
|
+
const start = params.starting_after
|
|
54
|
+
? all.findIndex((p) => p.id === params.starting_after) + 1
|
|
55
|
+
: 0;
|
|
56
|
+
const data = all.slice(start, start + (params.limit ?? 100));
|
|
57
|
+
return { data, has_more: start + data.length < all.length };
|
|
58
|
+
});
|
|
59
|
+
const opts = {
|
|
60
|
+
accountId: scope.accountId,
|
|
61
|
+
ownerKey: scope.ownerKey,
|
|
62
|
+
currency: "usd",
|
|
63
|
+
expectedRevision: 0,
|
|
64
|
+
payouts,
|
|
65
|
+
};
|
|
66
|
+
const adopt = () =>
|
|
67
|
+
adoptRecipientPayouts(
|
|
68
|
+
s.provider as unknown as Stripe,
|
|
69
|
+
component,
|
|
70
|
+
s.ctx,
|
|
71
|
+
opts,
|
|
72
|
+
);
|
|
73
|
+
return { s, adopt, opts };
|
|
74
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import { payoutOutcomeBatches } from "./payoutOutcomeBatches.js";
|
|
3
|
+
it("bounds transaction work while retaining every outcome in order", () => {
|
|
4
|
+
const rows = Array.from({ length: 103 }, (_, id) => ({
|
|
5
|
+
id,
|
|
6
|
+
allocationCount: id % 2 ? 600 : 100,
|
|
7
|
+
}));
|
|
8
|
+
const batches = payoutOutcomeBatches(rows);
|
|
9
|
+
expect(batches.flat()).toEqual(rows);
|
|
10
|
+
expect(batches.length).toBeGreaterThan(5);
|
|
11
|
+
for (const batch of batches) {
|
|
12
|
+
expect(batch.length).toBeLessThanOrEqual(25);
|
|
13
|
+
expect(
|
|
14
|
+
batch.reduce((n, row) => n + row.allocationCount, 0),
|
|
15
|
+
).toBeLessThanOrEqual(1000);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
it("retains a large individual outcome instead of imposing a new lifetime or claim cutoff", () => {
|
|
19
|
+
const rows = [
|
|
20
|
+
{ id: 0, allocationCount: 2 },
|
|
21
|
+
{ id: 1, allocationCount: 2000 },
|
|
22
|
+
{ id: 2, allocationCount: 1 },
|
|
23
|
+
];
|
|
24
|
+
expect(payoutOutcomeBatches(rows)).toEqual(rows.map((row) => [row]));
|
|
25
|
+
});
|
|
26
|
+
it("also bounds batches when many outcomes have no source movement", () => {
|
|
27
|
+
const rows = Array.from({ length: 101 }, (_, id) => ({
|
|
28
|
+
id,
|
|
29
|
+
allocationCount: 0,
|
|
30
|
+
}));
|
|
31
|
+
expect(payoutOutcomeBatches(rows).map((b) => b.length)).toEqual([
|
|
32
|
+
25, 25, 25, 25, 1,
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
it("rejects invalid allocation work counts before reconciliation", () => {
|
|
36
|
+
for (const allocationCount of [-1, 0.5, NaN, Infinity])
|
|
37
|
+
expect(() => payoutOutcomeBatches([{ allocationCount }])).toThrow(
|
|
38
|
+
"allocation count",
|
|
39
|
+
);
|
|
40
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Bound repeated allocation work; preserve a large individual outcome's original atomic contract. */
|
|
2
|
+
export function payoutOutcomeBatches<T extends { allocationCount: number }>(
|
|
3
|
+
rows: T[],
|
|
4
|
+
): T[][] {
|
|
5
|
+
const batches: T[][] = [];
|
|
6
|
+
let batch: T[] = [],
|
|
7
|
+
work = 0;
|
|
8
|
+
for (const row of rows) {
|
|
9
|
+
if (!Number.isSafeInteger(row.allocationCount) || row.allocationCount < 0)
|
|
10
|
+
throw new Error("Invalid payout outcome allocation count.");
|
|
11
|
+
if (
|
|
12
|
+
batch.length &&
|
|
13
|
+
(batch.length === 25 || work + row.allocationCount > 1000)
|
|
14
|
+
) {
|
|
15
|
+
batches.push(batch);
|
|
16
|
+
batch = [];
|
|
17
|
+
work = 0;
|
|
18
|
+
}
|
|
19
|
+
batch.push(row);
|
|
20
|
+
work += row.allocationCount;
|
|
21
|
+
}
|
|
22
|
+
if (batch.length) batches.push(batch);
|
|
23
|
+
return batches;
|
|
24
|
+
}
|