@fantastic.dev/better-stripe 0.32.0 → 0.34.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 +14 -0
- package/README.md +36 -1
- package/dist/client/billing/checkoutPages.d.ts +26 -0
- package/dist/client/billing/checkoutPages.d.ts.map +1 -0
- package/dist/client/billing/checkoutPages.js +111 -0
- package/dist/client/billing/checkoutPages.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +36 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +18 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +36 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +18 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/refundContext.d.ts +6 -0
- package/dist/client/connect/refundContext.d.ts.map +1 -0
- package/dist/client/connect/refundContext.js +109 -0
- package/dist/client/connect/refundContext.js.map +1 -0
- package/dist/client/connect/refundContextHistory.d.ts +20 -0
- package/dist/client/connect/refundContextHistory.d.ts.map +1 -0
- package/dist/client/connect/refundContextHistory.js +55 -0
- package/dist/client/connect/refundContextHistory.js.map +1 -0
- package/dist/client/connect/refundContextTypes.d.ts +39 -0
- package/dist/client/connect/refundContextTypes.d.ts.map +1 -0
- package/dist/client/connect/refundContextTypes.js +15 -0
- package/dist/client/connect/refundContextTypes.js.map +1 -0
- package/dist/client/connect/refundTarget.d.ts +14 -0
- package/dist/client/connect/refundTarget.d.ts.map +1 -0
- package/dist/client/connect/refundTarget.js +17 -0
- package/dist/client/connect/refundTarget.js.map +1 -0
- package/dist/client/connect/refunds.d.ts +17 -1
- package/dist/client/connect/refunds.d.ts.map +1 -1
- package/dist/client/connect/refunds.js +7 -17
- package/dist/client/connect/refunds.js.map +1 -1
- package/dist/client/core/accountClosures.d.ts +15 -0
- package/dist/client/core/accountClosures.d.ts.map +1 -0
- package/dist/client/core/accountClosures.js +10 -0
- package/dist/client/core/accountClosures.js.map +1 -0
- package/dist/client/core/accounts.d.ts.map +1 -1
- package/dist/client/core/accounts.js +15 -23
- package/dist/client/core/accounts.js.map +1 -1
- package/dist/client/core/personalAccountMapping.d.ts +8 -0
- package/dist/client/core/personalAccountMapping.d.ts.map +1 -0
- package/dist/client/core/personalAccountMapping.js +11 -0
- package/dist/client/core/personalAccountMapping.js.map +1 -0
- package/dist/client/index.d.ts +57 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +13 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/invoiceReads.d.ts +7 -1
- package/dist/client/invoiceReads.d.ts.map +1 -1
- package/dist/client/invoiceReads.js +11 -1
- package/dist/client/invoiceReads.js.map +1 -1
- package/dist/client/publicTypes.d.ts +5 -0
- package/dist/client/publicTypes.d.ts.map +1 -0
- package/dist/client/publicTypes.js +2 -0
- package/dist/client/publicTypes.js.map +1 -0
- package/dist/component/_generated/component.d.ts +88 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/accountClosure/helpers.d.ts +18 -0
- package/dist/component/accountClosure/helpers.d.ts.map +1 -0
- package/dist/component/accountClosure/helpers.js +18 -0
- package/dist/component/accountClosure/helpers.js.map +1 -0
- package/dist/component/accountClosure/mutations.d.ts +20 -0
- package/dist/component/accountClosure/mutations.d.ts.map +1 -0
- package/dist/component/accountClosure/mutations.js +55 -0
- package/dist/component/accountClosure/mutations.js.map +1 -0
- package/dist/component/accountClosure/queries.d.ts +34 -0
- package/dist/component/accountClosure/queries.d.ts.map +1 -0
- package/dist/component/accountClosure/queries.js +36 -0
- package/dist/component/accountClosure/queries.js.map +1 -0
- package/dist/component/accountClosure/schema.d.ts +46 -0
- package/dist/component/accountClosure/schema.d.ts.map +1 -0
- package/dist/component/accountClosure/schema.js +20 -0
- package/dist/component/accountClosure/schema.js.map +1 -0
- package/dist/component/core/mutations.d.ts.map +1 -1
- package/dist/component/core/mutations.js +5 -0
- package/dist/component/core/mutations.js.map +1 -1
- package/dist/component/core/personalAccountMapping.d.ts +95 -0
- package/dist/component/core/personalAccountMapping.d.ts.map +1 -0
- package/dist/component/core/personalAccountMapping.js +54 -0
- package/dist/component/core/personalAccountMapping.js.map +1 -0
- package/dist/component/core/queries.d.ts +27 -0
- package/dist/component/core/queries.d.ts.map +1 -1
- package/dist/component/core/queries.js +7 -0
- package/dist/component/core/queries.js.map +1 -1
- package/dist/component/earnings/triggers.d.ts +42 -0
- package/dist/component/earnings/triggers.d.ts.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/schema.d.ts +18 -0
- package/dist/component/schema.js +2 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/checkout-session-pages.md +71 -0
- package/docs/personal-account-mapping.md +54 -0
- package/docs/read-contracts.md +28 -0
- package/docs/refund-context.md +84 -0
- package/package.json +5 -2
- package/src/client/billing/checkoutPages.test.ts +251 -0
- package/src/client/billing/checkoutPages.ts +135 -0
- package/src/client/connect/refundContext.ts +140 -0
- package/src/client/connect/refundContextHistory.ts +84 -0
- package/src/client/connect/refundContextTypes.ts +56 -0
- package/src/client/connect/refundTarget.ts +39 -0
- package/src/client/connect/refunds.test.ts +72 -0
- package/src/client/connect/refunds.ts +8 -22
- package/src/client/core/account-close-orchestration.test.ts +131 -0
- package/src/client/core/account-closure.test.ts +317 -0
- package/src/client/core/accountClosures.ts +37 -0
- package/src/client/core/accounts.test.ts +0 -111
- package/src/client/core/accounts.ts +24 -29
- package/src/client/core/personal-account-mapping.test.ts +71 -0
- package/src/client/core/personalAccountMapping.ts +23 -0
- package/src/client/index.ts +45 -33
- package/src/client/invoiceReads.ts +17 -1
- package/src/client/publicTypes.ts +35 -0
- package/src/client/refund-context-shapes.test.ts +127 -0
- package/src/client/refund-context.test.ts +404 -0
- package/src/component/_generated/api.ts +8 -0
- package/src/component/_generated/component.ts +126 -0
- package/src/component/accountClosure/helpers.ts +21 -0
- package/src/component/accountClosure/mutations.ts +56 -0
- package/src/component/accountClosure/queries.ts +43 -0
- package/src/component/accountClosure/schema.ts +20 -0
- package/src/component/core/mutations.ts +3 -0
- package/src/component/core/personal-account-mapping.test.ts +173 -0
- package/src/component/core/personalAccountMapping.ts +81 -0
- package/src/component/core/queries.ts +11 -0
- package/src/component/schema.ts +2 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
refundContextFixture as fixture,
|
|
5
|
+
refund,
|
|
6
|
+
page,
|
|
7
|
+
expectUnavailable,
|
|
8
|
+
} from "../../tests/fixtures/refund-context.js";
|
|
9
|
+
afterEach(() => vi.restoreAllMocks());
|
|
10
|
+
|
|
11
|
+
it("uses captured principal rather than authorized amount for a partial capture", async () => {
|
|
12
|
+
const f = fixture();
|
|
13
|
+
expect(await f.read()).toMatchObject({
|
|
14
|
+
status: "ready",
|
|
15
|
+
capturedAmount: 1000,
|
|
16
|
+
refundedAmount: 0,
|
|
17
|
+
remainingRefundableAmount: 1000,
|
|
18
|
+
});
|
|
19
|
+
expect(f.intentRead).toHaveBeenCalledTimes(1);
|
|
20
|
+
expect(f.chargeRead).toHaveBeenCalledTimes(1);
|
|
21
|
+
expect(f.refundRead).toHaveBeenCalledTimes(1);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it.each([
|
|
25
|
+
undefined,
|
|
26
|
+
null,
|
|
27
|
+
{},
|
|
28
|
+
{ type: "seller" },
|
|
29
|
+
{ type: "seller", accountId: "cus_legacy" },
|
|
30
|
+
{ type: "buyer", accountId: "acct_store" },
|
|
31
|
+
{ type: "admin", accountId: "acct_store" },
|
|
32
|
+
])("rejects malformed actor %j before any lookup", async (actor) => {
|
|
33
|
+
const f = fixture();
|
|
34
|
+
await expect(
|
|
35
|
+
f.readRaw({ stripePaymentIntentId: "pi_one", actor }),
|
|
36
|
+
).rejects.toThrow(/actor/i);
|
|
37
|
+
expect(f.runQuery).not.toHaveBeenCalled();
|
|
38
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
39
|
+
expect(f.chargeRead).not.toHaveBeenCalled();
|
|
40
|
+
expect(f.refundRead).not.toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it.each(["", " pi_one", "pi_one ", "ch_one", "pi_bad?"])(
|
|
44
|
+
"rejects noncanonical PI %j before any lookup",
|
|
45
|
+
async (stripePaymentIntentId) => {
|
|
46
|
+
const f = fixture();
|
|
47
|
+
await expect(f.read({ ...f.opts, stripePaymentIntentId })).rejects.toThrow(
|
|
48
|
+
/stripePaymentIntentId|canonical/i,
|
|
49
|
+
);
|
|
50
|
+
expect(f.runQuery).not.toHaveBeenCalled();
|
|
51
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
it.each([0, 1.5, 11, Number.NaN])(
|
|
56
|
+
"rejects invalid history budget %j before lookup",
|
|
57
|
+
async (maxRefundPages) => {
|
|
58
|
+
const f = fixture();
|
|
59
|
+
await expect(f.read({ ...f.opts, maxRefundPages })).rejects.toThrow(
|
|
60
|
+
/maxRefundPages|budget/i,
|
|
61
|
+
);
|
|
62
|
+
expect(f.runQuery).not.toHaveBeenCalled();
|
|
63
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
it.each(["", " acct_scope", "cus_legacy"])(
|
|
68
|
+
"rejects malformed connected scope %j before lookup",
|
|
69
|
+
async (stripeAccountId) => {
|
|
70
|
+
const f = fixture();
|
|
71
|
+
await expect(f.read({ ...f.opts, stripeAccountId })).rejects.toThrow(
|
|
72
|
+
/stripeAccountId|account/i,
|
|
73
|
+
);
|
|
74
|
+
expect(f.runQuery).not.toHaveBeenCalled();
|
|
75
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
it.each(["foreign", "affiliate", "other"])(
|
|
80
|
+
"denies %s seller before provider reads",
|
|
81
|
+
async (role) => {
|
|
82
|
+
const f = fixture();
|
|
83
|
+
f.runQuery.mockResolvedValue({
|
|
84
|
+
stripePaymentIntentId: "pi_one",
|
|
85
|
+
accountId: "acct_buyer",
|
|
86
|
+
splitRecipients: [{ destinationAccountId: "acct_store", role }],
|
|
87
|
+
amount: 2000,
|
|
88
|
+
currency: "usd",
|
|
89
|
+
});
|
|
90
|
+
await expect(f.read()).rejects.toThrow(/not authorized|unauthorized/i);
|
|
91
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
92
|
+
expect(f.chargeRead).not.toHaveBeenCalled();
|
|
93
|
+
expect(f.refundRead).not.toHaveBeenCalled();
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
it("allows the original store leg without granting minor legs authority", async () => {
|
|
98
|
+
const f = fixture();
|
|
99
|
+
f.runQuery.mockResolvedValue({
|
|
100
|
+
stripePaymentIntentId: "pi_one",
|
|
101
|
+
accountId: "acct_buyer",
|
|
102
|
+
splitRecipients: [
|
|
103
|
+
{ destinationAccountId: "acct_store", role: "store" },
|
|
104
|
+
{ destinationAccountId: "acct_affiliate", role: "affiliate" },
|
|
105
|
+
],
|
|
106
|
+
amount: 2000,
|
|
107
|
+
currency: "usd",
|
|
108
|
+
});
|
|
109
|
+
expect(await f.read()).toMatchObject({
|
|
110
|
+
status: "ready",
|
|
111
|
+
remainingRefundableAmount: 1000,
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("requires original component target correlation even for an explicit admin", async () => {
|
|
116
|
+
const f = fixture();
|
|
117
|
+
f.runQuery.mockResolvedValue(null);
|
|
118
|
+
expectUnavailable(await f.read({ ...f.opts, actor: { type: "admin" } }));
|
|
119
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it.each([
|
|
123
|
+
{ stripePaymentIntentId: "pi_other", accountId: "acct_buyer" },
|
|
124
|
+
{ stripePaymentIntentId: "pi_one", accountId: "cus_legacy" },
|
|
125
|
+
{ stripePaymentIntentId: "pi_one", accountId: undefined },
|
|
126
|
+
])(
|
|
127
|
+
"holds unavailable original target %j before provider lookup",
|
|
128
|
+
async (fields) => {
|
|
129
|
+
const f = fixture();
|
|
130
|
+
f.runQuery.mockResolvedValue({
|
|
131
|
+
...fields,
|
|
132
|
+
destinationAccountId: "acct_store",
|
|
133
|
+
amount: 2000,
|
|
134
|
+
currency: "usd",
|
|
135
|
+
});
|
|
136
|
+
expectUnavailable(await f.read());
|
|
137
|
+
expect(f.intentRead).not.toHaveBeenCalled();
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
it.each([
|
|
142
|
+
{ id: "pi_other" },
|
|
143
|
+
{ customer_account: "acct_other" },
|
|
144
|
+
{ customer_account: null },
|
|
145
|
+
{ status: "processing" as const },
|
|
146
|
+
])(
|
|
147
|
+
"holds mismatched or incomplete PI %j before charge lookup",
|
|
148
|
+
async (fields) => {
|
|
149
|
+
const f = fixture();
|
|
150
|
+
f.intentRead.mockResolvedValue({ ...f.payment, ...fields });
|
|
151
|
+
expectUnavailable(await f.read());
|
|
152
|
+
expect(f.chargeRead).not.toHaveBeenCalled();
|
|
153
|
+
expect(f.refundRead).not.toHaveBeenCalled();
|
|
154
|
+
},
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
it.each([
|
|
158
|
+
{ id: "ch_other" },
|
|
159
|
+
{ payment_intent: "pi_other" },
|
|
160
|
+
{ currency: "kwd" },
|
|
161
|
+
{ amount_captured: 999 },
|
|
162
|
+
{ amount_captured: -1 },
|
|
163
|
+
{ amount_captured: 1.5 },
|
|
164
|
+
{ amount_captured: Number.MAX_SAFE_INTEGER + 1 },
|
|
165
|
+
{ amount: 999 },
|
|
166
|
+
{ amount_refunded: -1 },
|
|
167
|
+
{ amount_refunded: 1001 },
|
|
168
|
+
{ amount_refunded: 1.5 },
|
|
169
|
+
{ paid: false },
|
|
170
|
+
{ captured: false },
|
|
171
|
+
{ disputed: true },
|
|
172
|
+
])("holds unsupported original charge %j", async (fields) => {
|
|
173
|
+
const f = fixture();
|
|
174
|
+
f.chargeRead.mockResolvedValue({ ...f.charge, ...fields });
|
|
175
|
+
expectUnavailable(await f.read());
|
|
176
|
+
expect(f.refundRead).not.toHaveBeenCalled();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("does not invent a V2 buyer field on Stripe23 Charge and validates supplied legacy customer correlation", async () => {
|
|
180
|
+
const f = fixture();
|
|
181
|
+
f.intentRead.mockResolvedValue({ ...f.payment, customer: "cus_original" });
|
|
182
|
+
f.chargeRead.mockResolvedValue({ ...f.charge, customer: "cus_other" });
|
|
183
|
+
expectUnavailable(await f.read());
|
|
184
|
+
expect(f.refundRead).not.toHaveBeenCalled();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("keeps connected request scope separate from the original buyer", async () => {
|
|
188
|
+
const f = fixture();
|
|
189
|
+
expect(
|
|
190
|
+
await f.read({ ...f.opts, stripeAccountId: "acct_provider_scope" }),
|
|
191
|
+
).toMatchObject({ status: "ready", remainingRefundableAmount: 1000 });
|
|
192
|
+
expect(f.intentRead).toHaveBeenCalledWith("pi_one", undefined, {
|
|
193
|
+
stripeAccount: "acct_provider_scope",
|
|
194
|
+
});
|
|
195
|
+
expect(f.chargeRead).toHaveBeenCalledWith("ch_one", undefined, {
|
|
196
|
+
stripeAccount: "acct_provider_scope",
|
|
197
|
+
});
|
|
198
|
+
expect(f.refundRead).toHaveBeenCalledWith(
|
|
199
|
+
{ payment_intent: "pi_one", limit: 100 },
|
|
200
|
+
{ stripeAccount: "acct_provider_scope" },
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("supports a completed partial refund followed by its remaining full refund", async () => {
|
|
205
|
+
const f = fixture();
|
|
206
|
+
f.chargeRead.mockResolvedValue({ ...f.charge, amount_refunded: 200 });
|
|
207
|
+
f.refundRead.mockResolvedValue(page([refund("re_partial", 200)]));
|
|
208
|
+
expect(await f.read()).toMatchObject({
|
|
209
|
+
status: "ready",
|
|
210
|
+
capturedAmount: 1000,
|
|
211
|
+
refundedAmount: 200,
|
|
212
|
+
remainingRefundableAmount: 800,
|
|
213
|
+
});
|
|
214
|
+
f.chargeRead.mockResolvedValue({
|
|
215
|
+
...f.charge,
|
|
216
|
+
amount_refunded: 1000,
|
|
217
|
+
refunded: true,
|
|
218
|
+
});
|
|
219
|
+
f.refundRead.mockResolvedValue(
|
|
220
|
+
page([refund("re_partial", 200), refund("re_rest", 800)]),
|
|
221
|
+
);
|
|
222
|
+
expect(await f.read()).toMatchObject({
|
|
223
|
+
status: "ready",
|
|
224
|
+
capturedAmount: 1000,
|
|
225
|
+
refundedAmount: 1000,
|
|
226
|
+
remainingRefundableAmount: 0,
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it.each(["jpy", "kwd", "isk"])(
|
|
231
|
+
"retains exact original %s units",
|
|
232
|
+
async (currency) => {
|
|
233
|
+
const f = fixture();
|
|
234
|
+
f.intentRead.mockResolvedValue({
|
|
235
|
+
...f.payment,
|
|
236
|
+
currency,
|
|
237
|
+
amount: 1234,
|
|
238
|
+
amount_received: 1234,
|
|
239
|
+
});
|
|
240
|
+
f.chargeRead.mockResolvedValue({
|
|
241
|
+
...f.charge,
|
|
242
|
+
currency,
|
|
243
|
+
amount: 1234,
|
|
244
|
+
amount_captured: 1234,
|
|
245
|
+
amount_refunded: 234,
|
|
246
|
+
});
|
|
247
|
+
f.runQuery.mockResolvedValue({
|
|
248
|
+
stripePaymentIntentId: "pi_one",
|
|
249
|
+
accountId: "acct_buyer",
|
|
250
|
+
destinationAccountId: "acct_store",
|
|
251
|
+
amount: 1234,
|
|
252
|
+
currency,
|
|
253
|
+
});
|
|
254
|
+
f.refundRead.mockResolvedValue(
|
|
255
|
+
page([{ ...refund("re_units", 234), currency }]),
|
|
256
|
+
);
|
|
257
|
+
expect(await f.read()).toMatchObject({
|
|
258
|
+
status: "ready",
|
|
259
|
+
capturedAmount: 1234,
|
|
260
|
+
refundedAmount: 234,
|
|
261
|
+
remainingRefundableAmount: 1000,
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
it.each(["pending", "requires_action", null, "future_state"])(
|
|
267
|
+
"keeps refund state %j unavailable",
|
|
268
|
+
async (status) => {
|
|
269
|
+
const f = fixture();
|
|
270
|
+
f.chargeRead.mockResolvedValue({ ...f.charge, amount_refunded: 200 });
|
|
271
|
+
f.refundRead.mockResolvedValue(
|
|
272
|
+
page([refund("re_pending", 200, status as Stripe.Refund["status"])]),
|
|
273
|
+
);
|
|
274
|
+
expectUnavailable(await f.read());
|
|
275
|
+
},
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
it.each(["failed", "canceled"])(
|
|
279
|
+
"does not subtract %s refund principal",
|
|
280
|
+
async (status) => {
|
|
281
|
+
const f = fixture();
|
|
282
|
+
f.refundRead.mockResolvedValue(
|
|
283
|
+
page([refund("re_failed", 200, status as Stripe.Refund["status"])]),
|
|
284
|
+
);
|
|
285
|
+
expect(await f.read()).toMatchObject({
|
|
286
|
+
status: "ready",
|
|
287
|
+
refundedAmount: 0,
|
|
288
|
+
remainingRefundableAmount: 1000,
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
it.each([
|
|
294
|
+
{ charge: "ch_other" },
|
|
295
|
+
{ payment_intent: "pi_other" },
|
|
296
|
+
{ currency: "kwd" },
|
|
297
|
+
{ amount: -1 },
|
|
298
|
+
{ amount: 1.5 },
|
|
299
|
+
])("holds inconsistent refund %j", async (fields) => {
|
|
300
|
+
const f = fixture();
|
|
301
|
+
f.refundRead.mockResolvedValue(
|
|
302
|
+
page([{ ...refund("re_bad", 100), ...fields }]),
|
|
303
|
+
);
|
|
304
|
+
expectUnavailable(await f.read());
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("does not expose a numeric remainder from inconsistent live counters", async () => {
|
|
308
|
+
const f = fixture();
|
|
309
|
+
f.chargeRead.mockResolvedValue({ ...f.charge, amount_refunded: 500 });
|
|
310
|
+
f.refundRead.mockResolvedValue(page([refund("re_observed", 200)]));
|
|
311
|
+
expectUnavailable(await f.read());
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("exhausts bounded pages and preserves the original payment scope", async () => {
|
|
315
|
+
const f = fixture();
|
|
316
|
+
f.chargeRead.mockResolvedValue({
|
|
317
|
+
...f.charge,
|
|
318
|
+
amount_refunded: 1000,
|
|
319
|
+
refunded: true,
|
|
320
|
+
});
|
|
321
|
+
f.refundRead
|
|
322
|
+
.mockResolvedValueOnce(page([refund("re_first", 400)], true))
|
|
323
|
+
.mockResolvedValueOnce(page([refund("re_last", 600)]));
|
|
324
|
+
expect(await f.read({ ...f.opts, maxRefundPages: 2 })).toMatchObject({
|
|
325
|
+
status: "ready",
|
|
326
|
+
remainingRefundableAmount: 0,
|
|
327
|
+
});
|
|
328
|
+
expect(f.refundRead).toHaveBeenNthCalledWith(
|
|
329
|
+
2,
|
|
330
|
+
{ payment_intent: "pi_one", limit: 100, starting_after: "re_first" },
|
|
331
|
+
undefined,
|
|
332
|
+
);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("holds exhausted history rather than returning a partial refundable balance", async () => {
|
|
336
|
+
const f = fixture();
|
|
337
|
+
f.refundRead.mockResolvedValue(page([refund("re_first", 200)], true));
|
|
338
|
+
expectUnavailable(await f.read({ ...f.opts, maxRefundPages: 1 }));
|
|
339
|
+
expect(f.refundRead).toHaveBeenCalledTimes(1);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("holds a nonterminal empty provider page", async () => {
|
|
343
|
+
const f = fixture();
|
|
344
|
+
f.refundRead.mockResolvedValue(page([], true));
|
|
345
|
+
expectUnavailable(await f.read());
|
|
346
|
+
expect(f.refundRead).toHaveBeenCalledTimes(1);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("holds duplicate or nonadvancing provider pages", async () => {
|
|
350
|
+
const f = fixture();
|
|
351
|
+
f.refundRead.mockResolvedValue(page([refund("re_repeated", 200)], true));
|
|
352
|
+
expectUnavailable(await f.read({ ...f.opts, maxRefundPages: 2 }));
|
|
353
|
+
expect(f.refundRead).toHaveBeenCalledTimes(2);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("preserves provider error identity instead of inventing a zero balance", async () => {
|
|
357
|
+
const f = fixture();
|
|
358
|
+
const error = new Error("Fixture provider unavailable");
|
|
359
|
+
f.refundRead.mockRejectedValue(error);
|
|
360
|
+
await expect(f.read()).rejects.toBe(error);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it.each([900, 1000])(
|
|
364
|
+
"holds a non-last duplicate across pages at counter %i without silently deduplicating",
|
|
365
|
+
async (amount_refunded) => {
|
|
366
|
+
const f = fixture();
|
|
367
|
+
f.chargeRead.mockResolvedValue({ ...f.charge, amount_refunded });
|
|
368
|
+
f.refundRead
|
|
369
|
+
.mockResolvedValueOnce(
|
|
370
|
+
page([refund("re_a", 100), refund("re_b", 200)], true),
|
|
371
|
+
)
|
|
372
|
+
.mockResolvedValueOnce(page([refund("re_a", 100), refund("re_c", 600)]));
|
|
373
|
+
expectUnavailable(await f.read({ ...f.opts, maxRefundPages: 2 }));
|
|
374
|
+
},
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
it.each([
|
|
378
|
+
{ data: null },
|
|
379
|
+
{ data: {} },
|
|
380
|
+
{ has_more: null },
|
|
381
|
+
{ has_more: "false" },
|
|
382
|
+
{ object: "refund" },
|
|
383
|
+
])("holds malformed refund envelope %j", async (fields) => {
|
|
384
|
+
const f = fixture();
|
|
385
|
+
f.refundRead.mockResolvedValue({
|
|
386
|
+
...page([]),
|
|
387
|
+
...fields,
|
|
388
|
+
} as Stripe.Response<Stripe.ApiList<Stripe.Refund>>);
|
|
389
|
+
expectUnavailable(await f.read());
|
|
390
|
+
expect(f.refundRead).toHaveBeenCalledTimes(1);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("holds oversized provider pages instead of processing an unbounded envelope", async () => {
|
|
394
|
+
const f = fixture();
|
|
395
|
+
f.refundRead.mockResolvedValue(
|
|
396
|
+
page(
|
|
397
|
+
Array.from({ length: 101 }, (_, i) =>
|
|
398
|
+
refund(`re_large_${i}`, 0, "failed"),
|
|
399
|
+
),
|
|
400
|
+
),
|
|
401
|
+
);
|
|
402
|
+
expectUnavailable(await f.read());
|
|
403
|
+
expect(f.refundRead).toHaveBeenCalledTimes(1);
|
|
404
|
+
});
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import type * as accountClosure_helpers from "../accountClosure/helpers.js";
|
|
12
|
+
import type * as accountClosure_mutations from "../accountClosure/mutations.js";
|
|
13
|
+
import type * as accountClosure_queries from "../accountClosure/queries.js";
|
|
11
14
|
import type * as affiliateEarnings_mutations from "../affiliateEarnings/mutations.js";
|
|
12
15
|
import type * as affiliatePayouts_mutations from "../affiliatePayouts/mutations.js";
|
|
13
16
|
import type * as affiliatePayouts_queries from "../affiliatePayouts/queries.js";
|
|
@@ -26,6 +29,7 @@ import type * as connect_reversalDiagnostics from "../connect/reversalDiagnostic
|
|
|
26
29
|
import type * as connect_reversalReceipts from "../connect/reversalReceipts.js";
|
|
27
30
|
import type * as connect_validators from "../connect/validators.js";
|
|
28
31
|
import type * as core_mutations from "../core/mutations.js";
|
|
32
|
+
import type * as core_personalAccountMapping from "../core/personalAccountMapping.js";
|
|
29
33
|
import type * as core_queries from "../core/queries.js";
|
|
30
34
|
import type * as core_resetSafety from "../core/resetSafety.js";
|
|
31
35
|
import type * as core_validators from "../core/validators.js";
|
|
@@ -99,6 +103,9 @@ import type {
|
|
|
99
103
|
import { anyApi, componentsGeneric } from "convex/server";
|
|
100
104
|
|
|
101
105
|
const fullApi: ApiFromModules<{
|
|
106
|
+
"accountClosure/helpers": typeof accountClosure_helpers;
|
|
107
|
+
"accountClosure/mutations": typeof accountClosure_mutations;
|
|
108
|
+
"accountClosure/queries": typeof accountClosure_queries;
|
|
102
109
|
"affiliateEarnings/mutations": typeof affiliateEarnings_mutations;
|
|
103
110
|
"affiliatePayouts/mutations": typeof affiliatePayouts_mutations;
|
|
104
111
|
"affiliatePayouts/queries": typeof affiliatePayouts_queries;
|
|
@@ -117,6 +124,7 @@ const fullApi: ApiFromModules<{
|
|
|
117
124
|
"connect/reversalReceipts": typeof connect_reversalReceipts;
|
|
118
125
|
"connect/validators": typeof connect_validators;
|
|
119
126
|
"core/mutations": typeof core_mutations;
|
|
127
|
+
"core/personalAccountMapping": typeof core_personalAccountMapping;
|
|
120
128
|
"core/queries": typeof core_queries;
|
|
121
129
|
"core/resetSafety": typeof core_resetSafety;
|
|
122
130
|
"core/validators": typeof core_validators;
|
|
@@ -23,6 +23,92 @@ import type { FunctionReference } from "convex/server";
|
|
|
23
23
|
*/
|
|
24
24
|
export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
25
25
|
{
|
|
26
|
+
accountClosure: {
|
|
27
|
+
mutations: {
|
|
28
|
+
complete: FunctionReference<
|
|
29
|
+
"mutation",
|
|
30
|
+
"internal",
|
|
31
|
+
{ stripeAccountId: string },
|
|
32
|
+
null,
|
|
33
|
+
Name
|
|
34
|
+
>;
|
|
35
|
+
prepare: FunctionReference<
|
|
36
|
+
"mutation",
|
|
37
|
+
"internal",
|
|
38
|
+
{ stripeAccountId: string },
|
|
39
|
+
{
|
|
40
|
+
closure: {
|
|
41
|
+
_creationTime: number;
|
|
42
|
+
_id: string;
|
|
43
|
+
appliedConfigurations?: Array<
|
|
44
|
+
"customer" | "merchant" | "recipient"
|
|
45
|
+
>;
|
|
46
|
+
closedAt?: number;
|
|
47
|
+
orgId?: string;
|
|
48
|
+
state: "pending" | "closed";
|
|
49
|
+
stripeAccountId: string;
|
|
50
|
+
userId?: string;
|
|
51
|
+
};
|
|
52
|
+
isRetry: boolean;
|
|
53
|
+
},
|
|
54
|
+
Name
|
|
55
|
+
>;
|
|
56
|
+
};
|
|
57
|
+
queries: {
|
|
58
|
+
getClosedAccountByStripeId: FunctionReference<
|
|
59
|
+
"query",
|
|
60
|
+
"internal",
|
|
61
|
+
{ stripeAccountId: string },
|
|
62
|
+
{
|
|
63
|
+
_creationTime: number;
|
|
64
|
+
_id: string;
|
|
65
|
+
appliedConfigurations?: Array<
|
|
66
|
+
"customer" | "merchant" | "recipient"
|
|
67
|
+
>;
|
|
68
|
+
closedAt?: number;
|
|
69
|
+
orgId?: string;
|
|
70
|
+
state: "pending" | "closed";
|
|
71
|
+
stripeAccountId: string;
|
|
72
|
+
userId?: string;
|
|
73
|
+
} | null,
|
|
74
|
+
Name
|
|
75
|
+
>;
|
|
76
|
+
listClosedAccountsByUserId: FunctionReference<
|
|
77
|
+
"query",
|
|
78
|
+
"internal",
|
|
79
|
+
{
|
|
80
|
+
paginationOpts: {
|
|
81
|
+
cursor: string | null;
|
|
82
|
+
endCursor?: string | null;
|
|
83
|
+
id?: number;
|
|
84
|
+
maximumBytesRead?: number;
|
|
85
|
+
maximumRowsRead?: number;
|
|
86
|
+
numItems: number;
|
|
87
|
+
};
|
|
88
|
+
userId: string;
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
continueCursor: string;
|
|
92
|
+
isDone: boolean;
|
|
93
|
+
page: Array<{
|
|
94
|
+
_creationTime: number;
|
|
95
|
+
_id: string;
|
|
96
|
+
appliedConfigurations?: Array<
|
|
97
|
+
"customer" | "merchant" | "recipient"
|
|
98
|
+
>;
|
|
99
|
+
closedAt?: number;
|
|
100
|
+
orgId?: string;
|
|
101
|
+
state: "pending" | "closed";
|
|
102
|
+
stripeAccountId: string;
|
|
103
|
+
userId?: string;
|
|
104
|
+
}>;
|
|
105
|
+
pageStatus?: "SplitRecommended" | "SplitRequired" | null;
|
|
106
|
+
splitCursor?: string | null;
|
|
107
|
+
},
|
|
108
|
+
Name
|
|
109
|
+
>;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
26
112
|
affiliateEarnings: {
|
|
27
113
|
mutations: {
|
|
28
114
|
confirmEarning: FunctionReference<
|
|
@@ -2223,6 +2309,46 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
2223
2309
|
} | null,
|
|
2224
2310
|
Name
|
|
2225
2311
|
>;
|
|
2312
|
+
getPersonalAccountMapping: FunctionReference<
|
|
2313
|
+
"query",
|
|
2314
|
+
"internal",
|
|
2315
|
+
{ userId: string },
|
|
2316
|
+
| {
|
|
2317
|
+
account: {
|
|
2318
|
+
_creationTime: number;
|
|
2319
|
+
_id: string;
|
|
2320
|
+
appliedConfigurations?: Array<
|
|
2321
|
+
"customer" | "merchant" | "recipient"
|
|
2322
|
+
>;
|
|
2323
|
+
capabilities?: any;
|
|
2324
|
+
configuration?: any;
|
|
2325
|
+
country?: string;
|
|
2326
|
+
email?: string;
|
|
2327
|
+
metadata?: any;
|
|
2328
|
+
missingRequirements?: Array<string>;
|
|
2329
|
+
name?: string;
|
|
2330
|
+
onboardingStatus:
|
|
2331
|
+
"pending" | "in_progress" | "complete" | "restricted";
|
|
2332
|
+
orgId?: string;
|
|
2333
|
+
requirements?: any;
|
|
2334
|
+
statementDescriptor?: string;
|
|
2335
|
+
stripeAccountId: string;
|
|
2336
|
+
userId: string;
|
|
2337
|
+
};
|
|
2338
|
+
status: "unique";
|
|
2339
|
+
}
|
|
2340
|
+
| {
|
|
2341
|
+
reason:
|
|
2342
|
+
| "missing"
|
|
2343
|
+
| "ambiguous"
|
|
2344
|
+
| "organization"
|
|
2345
|
+
| "invalid_account"
|
|
2346
|
+
| "closure_unresolved"
|
|
2347
|
+
| "closed";
|
|
2348
|
+
status: "unavailable";
|
|
2349
|
+
},
|
|
2350
|
+
Name
|
|
2351
|
+
>;
|
|
2226
2352
|
getPublishableKey: FunctionReference<
|
|
2227
2353
|
"query",
|
|
2228
2354
|
"internal",
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { MutationCtx, QueryCtx } from "../_generated/server.js";
|
|
2
|
+
export function closureByStripeId(ctx: QueryCtx, stripeAccountId: string) {
|
|
3
|
+
return ctx.db
|
|
4
|
+
.query("accountClosures")
|
|
5
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
6
|
+
q.eq("stripeAccountId", stripeAccountId),
|
|
7
|
+
)
|
|
8
|
+
.unique();
|
|
9
|
+
}
|
|
10
|
+
/** A delayed account event cannot revive a closed recipient or change a pinned owner. */
|
|
11
|
+
export async function canUpsertAccount(
|
|
12
|
+
ctx: MutationCtx,
|
|
13
|
+
account: { stripeAccountId: string; userId: string; orgId?: string },
|
|
14
|
+
) {
|
|
15
|
+
const closure = await closureByStripeId(ctx, account.stripeAccountId);
|
|
16
|
+
if (!closure) return true;
|
|
17
|
+
if (closure.state === "closed") return false;
|
|
18
|
+
if (closure.userId !== account.userId || closure.orgId !== account.orgId)
|
|
19
|
+
throw new Error("Account ownership is pinned while closure is unresolved");
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation } from "../_generated/server.js";
|
|
3
|
+
import { closureByStripeId } from "./helpers.js";
|
|
4
|
+
import { closureDoc } from "./schema.js";
|
|
5
|
+
/** Trusted host boundary: authenticate and authorize the actor before invoking close. */
|
|
6
|
+
export const prepare = mutation({
|
|
7
|
+
args: { stripeAccountId: v.string() },
|
|
8
|
+
returns: v.object({ closure: closureDoc, isRetry: v.boolean() }),
|
|
9
|
+
handler: async (ctx, { stripeAccountId }) => {
|
|
10
|
+
const existing = await closureByStripeId(ctx, stripeAccountId);
|
|
11
|
+
if (existing) return { closure: existing, isRetry: true };
|
|
12
|
+
const account = await ctx.db
|
|
13
|
+
.query("accounts")
|
|
14
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
15
|
+
q.eq("stripeAccountId", stripeAccountId),
|
|
16
|
+
)
|
|
17
|
+
.unique();
|
|
18
|
+
const id = await ctx.db.insert("accountClosures", {
|
|
19
|
+
stripeAccountId,
|
|
20
|
+
userId: account?.userId,
|
|
21
|
+
orgId: account?.orgId,
|
|
22
|
+
appliedConfigurations: account?.appliedConfigurations,
|
|
23
|
+
state: "pending",
|
|
24
|
+
});
|
|
25
|
+
const closure = await ctx.db.get("accountClosures", id);
|
|
26
|
+
if (!closure) throw new Error("Closure intent was not persisted");
|
|
27
|
+
return { closure, isRetry: false };
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const complete = mutation({
|
|
32
|
+
args: { stripeAccountId: v.string() },
|
|
33
|
+
returns: v.null(),
|
|
34
|
+
handler: async (ctx, { stripeAccountId }) => {
|
|
35
|
+
const closure = await closureByStripeId(ctx, stripeAccountId);
|
|
36
|
+
if (!closure) throw new Error("Closure intent is required");
|
|
37
|
+
if (closure.state === "closed") return null;
|
|
38
|
+
const account = await ctx.db
|
|
39
|
+
.query("accounts")
|
|
40
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
41
|
+
q.eq("stripeAccountId", stripeAccountId),
|
|
42
|
+
)
|
|
43
|
+
.unique();
|
|
44
|
+
if (
|
|
45
|
+
account &&
|
|
46
|
+
(account.userId !== closure.userId || account.orgId !== closure.orgId)
|
|
47
|
+
)
|
|
48
|
+
throw new Error("Account ownership changed during closure");
|
|
49
|
+
await ctx.db.patch("accountClosures", closure._id, {
|
|
50
|
+
state: "closed",
|
|
51
|
+
closedAt: Date.now(),
|
|
52
|
+
});
|
|
53
|
+
if (account) await ctx.db.delete("accounts", account._id);
|
|
54
|
+
return null;
|
|
55
|
+
},
|
|
56
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
paginationOptsValidator,
|
|
3
|
+
paginationResultValidator,
|
|
4
|
+
} from "convex/server";
|
|
5
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
6
|
+
import { v } from "convex/values";
|
|
7
|
+
import { query } from "../_generated/server.js";
|
|
8
|
+
import schema from "../schema.js";
|
|
9
|
+
import { closureByStripeId } from "./helpers.js";
|
|
10
|
+
import { closureDoc } from "./schema.js";
|
|
11
|
+
/** This is provenance, not end-user authentication; hosts must compare the authenticated owner. */
|
|
12
|
+
export const getClosedAccountByStripeId = query({
|
|
13
|
+
args: { stripeAccountId: v.string() },
|
|
14
|
+
returns: v.union(closureDoc, v.null()),
|
|
15
|
+
handler: async (ctx, { stripeAccountId }) => {
|
|
16
|
+
const closure = await closureByStripeId(ctx, stripeAccountId);
|
|
17
|
+
return closure?.state === "closed" ? closure : null;
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const listClosedAccountsByUserId = query({
|
|
22
|
+
args: { userId: v.string(), paginationOpts: paginationOptsValidator },
|
|
23
|
+
returns: paginationResultValidator(closureDoc),
|
|
24
|
+
handler: async (ctx, { userId, paginationOpts }) => {
|
|
25
|
+
if (
|
|
26
|
+
!Number.isInteger(paginationOpts.numItems) ||
|
|
27
|
+
paginationOpts.numItems < 1 ||
|
|
28
|
+
paginationOpts.numItems > 100
|
|
29
|
+
)
|
|
30
|
+
throw new Error("Closure history page size must be between 1 and 100");
|
|
31
|
+
return paginator(ctx.db, schema)
|
|
32
|
+
.query("accountClosures")
|
|
33
|
+
.withIndex("by_user_state", (q) =>
|
|
34
|
+
q.eq("userId", userId).eq("orgId", undefined).eq("state", "closed"),
|
|
35
|
+
)
|
|
36
|
+
.order("desc")
|
|
37
|
+
.paginate({
|
|
38
|
+
...paginationOpts,
|
|
39
|
+
maximumRowsRead: 200,
|
|
40
|
+
maximumBytesRead: 1_000_000,
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|