@fantastic.dev/better-stripe 0.73.0 → 0.75.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 +12 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/core/accountCloseOptions.d.ts +21 -0
- package/dist/client/core/accountCloseOptions.d.ts.map +1 -0
- package/dist/client/core/accountCloseOptions.js +15 -0
- package/dist/client/core/accountCloseOptions.js.map +1 -0
- package/dist/client/core/accountCloseScope.d.ts +9 -0
- package/dist/client/core/accountCloseScope.d.ts.map +1 -0
- package/dist/client/core/accountCloseScope.js +49 -0
- package/dist/client/core/accountCloseScope.js.map +1 -0
- package/dist/client/core/accountClosurePreview.d.ts +40 -0
- package/dist/client/core/accountClosurePreview.d.ts.map +1 -0
- package/dist/client/core/accountClosurePreview.js +98 -0
- package/dist/client/core/accountClosurePreview.js.map +1 -0
- package/dist/client/core/accounts.d.ts +3 -6
- package/dist/client/core/accounts.d.ts.map +1 -1
- package/dist/client/core/accounts.js +21 -5
- package/dist/client/core/accounts.js.map +1 -1
- package/dist/client/index.d.ts +8 -6
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +7 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/products/catalogClient.d.ts +2 -0
- package/dist/client/products/catalogClient.d.ts.map +1 -1
- package/dist/client/products/catalogClient.js.map +1 -1
- package/dist/client/products/products.d.ts +2 -0
- package/dist/client/products/products.d.ts.map +1 -1
- package/dist/client/products/products.js +3 -1
- package/dist/client/products/products.js.map +1 -1
- package/dist/client/publicTypes.d.ts +2 -0
- package/dist/client/publicTypes.d.ts.map +1 -1
- package/dist/client/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +2 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +14 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/accountClosure/blockers.d.ts +6 -0
- package/dist/component/accountClosure/blockers.d.ts.map +1 -0
- package/dist/component/accountClosure/blockers.js +43 -0
- package/dist/component/accountClosure/blockers.js.map +1 -0
- package/dist/component/accountClosure/mutations.d.ts.map +1 -1
- package/dist/component/accountClosure/mutations.js +6 -31
- package/dist/component/accountClosure/mutations.js.map +1 -1
- package/dist/component/accountClosure/queries.d.ts +48 -0
- package/dist/component/accountClosure/queries.d.ts.map +1 -1
- package/dist/component/accountClosure/queries.js +32 -0
- package/dist/component/accountClosure/queries.js.map +1 -1
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/docs/client-api.md +74 -0
- package/docs/react-workflows.md +31 -0
- package/package.json +1 -1
- package/src/client/core/account-close-guard.test.ts +349 -0
- package/src/client/core/account-closure-preview.test.ts +219 -0
- package/src/client/core/accountCloseOptions.ts +23 -0
- package/src/client/core/accountCloseScope.ts +87 -0
- package/src/client/core/accountClosurePreview.ts +149 -0
- package/src/client/core/accounts.ts +33 -7
- package/src/client/index.ts +14 -2
- package/src/client/products/catalogClient.ts +2 -0
- package/src/client/products/product-descriptor-inheritance.test.ts +245 -0
- package/src/client/products/products.ts +9 -4
- package/src/client/publicTypes.ts +6 -0
- package/src/component/_generated/api.ts +2 -0
- package/src/component/_generated/component.ts +16 -0
- package/src/component/accountClosure/blockers.test.ts +114 -0
- package/src/component/accountClosure/blockers.ts +60 -0
- package/src/component/accountClosure/mutations.ts +6 -39
- package/src/component/accountClosure/queries.ts +41 -1
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { expect, it, vi } from "vitest";
|
|
3
|
+
import type Stripe from "stripe";
|
|
4
|
+
import type { RunCtx } from "../helpers.js";
|
|
5
|
+
import { components, initConvexTest } from "../setup.test.js";
|
|
6
|
+
import schema from "../../component/schema.js";
|
|
7
|
+
import { closeAccount } from "./accounts.js";
|
|
8
|
+
import type { AppliedConfiguration } from "../../component/core/validators.js";
|
|
9
|
+
|
|
10
|
+
const modules = import.meta.glob("../../component/**/*.ts");
|
|
11
|
+
async function fixture() {
|
|
12
|
+
const t = initConvexTest();
|
|
13
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
14
|
+
const component = components.betterStripe;
|
|
15
|
+
await t.mutation(component.core.mutations.upsertAccount, {
|
|
16
|
+
stripeAccountId: "acct_scope",
|
|
17
|
+
userId: "original",
|
|
18
|
+
appliedConfigurations: ["recipient"],
|
|
19
|
+
});
|
|
20
|
+
const close = vi.fn().mockResolvedValue({ id: "acct_scope", closed: true });
|
|
21
|
+
const retrieve = vi.fn().mockResolvedValue({
|
|
22
|
+
id: "acct_scope",
|
|
23
|
+
closed: false,
|
|
24
|
+
applied_configurations: ["merchant", "recipient"],
|
|
25
|
+
});
|
|
26
|
+
const stripe = {
|
|
27
|
+
v2: { core: { accounts: { close, retrieve } } },
|
|
28
|
+
} as unknown as Stripe;
|
|
29
|
+
const ctx = { runQuery: t.query, runMutation: t.mutation } as RunCtx;
|
|
30
|
+
return { t, component, ctx, stripe, close, retrieve };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
it("refuses first guarded close when fresh provider roles differ from confirmed cached roles", async () => {
|
|
34
|
+
const f = await fixture();
|
|
35
|
+
await expect(
|
|
36
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
37
|
+
stripeAccountId: "acct_scope",
|
|
38
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
39
|
+
}),
|
|
40
|
+
).rejects.toThrow();
|
|
41
|
+
expect(f.retrieve).toHaveBeenCalledWith("acct_scope");
|
|
42
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
43
|
+
expect(
|
|
44
|
+
await f.t.query(f.component.core.queries.getAccountByStripeId, {
|
|
45
|
+
stripeAccountId: "acct_scope",
|
|
46
|
+
}),
|
|
47
|
+
).toMatchObject({ userId: "original" });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("holds unknown provider scope rather than treating missing roles as an empty confirmed set", async () => {
|
|
51
|
+
const f = await fixture();
|
|
52
|
+
f.retrieve.mockResolvedValue({ id: "acct_scope", closed: false });
|
|
53
|
+
await expect(
|
|
54
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
55
|
+
stripeAccountId: "acct_scope",
|
|
56
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
57
|
+
}),
|
|
58
|
+
).rejects.toThrow();
|
|
59
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("reconciles observed terminal state with exact original ownership and scope without claiming a new POST", async () => {
|
|
63
|
+
const f = await fixture();
|
|
64
|
+
f.retrieve.mockResolvedValue({
|
|
65
|
+
id: "acct_scope",
|
|
66
|
+
closed: true,
|
|
67
|
+
applied_configurations: [],
|
|
68
|
+
});
|
|
69
|
+
expect(
|
|
70
|
+
await closeAccount(f.stripe, f.component, f.ctx, {
|
|
71
|
+
stripeAccountId: "acct_scope",
|
|
72
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
73
|
+
}),
|
|
74
|
+
).toEqual({ closed: true });
|
|
75
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("reconciles the same original guarded lost ACK without another POST when closed roles are cleared", async () => {
|
|
79
|
+
const f = await fixture();
|
|
80
|
+
f.retrieve.mockResolvedValue({
|
|
81
|
+
id: "acct_scope",
|
|
82
|
+
closed: false,
|
|
83
|
+
applied_configurations: ["recipient"],
|
|
84
|
+
});
|
|
85
|
+
f.close.mockRejectedValueOnce(new Error("Synthetic lost ACK"));
|
|
86
|
+
const opts = {
|
|
87
|
+
stripeAccountId: "acct_scope",
|
|
88
|
+
expectedAppliedConfigurations: ["recipient" as const],
|
|
89
|
+
};
|
|
90
|
+
await expect(
|
|
91
|
+
closeAccount(f.stripe, f.component, f.ctx, opts),
|
|
92
|
+
).rejects.toThrow("Synthetic lost ACK");
|
|
93
|
+
f.retrieve.mockResolvedValue({
|
|
94
|
+
id: "acct_scope",
|
|
95
|
+
closed: true,
|
|
96
|
+
applied_configurations: [],
|
|
97
|
+
});
|
|
98
|
+
expect(await closeAccount(f.stripe, f.component, f.ctx, opts)).toEqual({
|
|
99
|
+
closed: true,
|
|
100
|
+
});
|
|
101
|
+
expect(f.close).toHaveBeenCalledTimes(1);
|
|
102
|
+
const history = await f.t.query(
|
|
103
|
+
f.component.accountClosure.queries.getClosedAccountByStripeId,
|
|
104
|
+
{ stripeAccountId: "acct_scope" },
|
|
105
|
+
);
|
|
106
|
+
expect(history).toMatchObject({
|
|
107
|
+
userId: "original",
|
|
108
|
+
appliedConfigurations: ["recipient"],
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("holds an unknown or foreign close acknowledgement without completing local history", async () => {
|
|
113
|
+
const f = await fixture();
|
|
114
|
+
f.retrieve.mockResolvedValue({
|
|
115
|
+
id: "acct_scope",
|
|
116
|
+
closed: false,
|
|
117
|
+
applied_configurations: ["recipient"],
|
|
118
|
+
});
|
|
119
|
+
f.close.mockResolvedValue({ id: "acct_foreign", closed: true });
|
|
120
|
+
await expect(
|
|
121
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
122
|
+
stripeAccountId: "acct_scope",
|
|
123
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
124
|
+
}),
|
|
125
|
+
).rejects.toThrow();
|
|
126
|
+
expect(
|
|
127
|
+
await f.t.query(
|
|
128
|
+
f.component.accountClosure.queries.getClosedAccountByStripeId,
|
|
129
|
+
{ stripeAccountId: "acct_scope" },
|
|
130
|
+
),
|
|
131
|
+
).toBeNull();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it.each<AppliedConfiguration[]>([
|
|
135
|
+
[],
|
|
136
|
+
["customer"],
|
|
137
|
+
["merchant"],
|
|
138
|
+
["recipient"],
|
|
139
|
+
["customer", "merchant"],
|
|
140
|
+
["customer", "recipient"],
|
|
141
|
+
["merchant", "recipient"],
|
|
142
|
+
["customer", "merchant", "recipient"],
|
|
143
|
+
])(
|
|
144
|
+
"freshly verifies and preserves each supported configuration set %j",
|
|
145
|
+
async (...roles) => {
|
|
146
|
+
const f = await fixture();
|
|
147
|
+
await f.t.mutation(f.component.core.mutations.upsertAccount, {
|
|
148
|
+
stripeAccountId: "acct_scope",
|
|
149
|
+
userId: "original",
|
|
150
|
+
appliedConfigurations: roles,
|
|
151
|
+
});
|
|
152
|
+
f.retrieve.mockResolvedValue({
|
|
153
|
+
id: "acct_scope",
|
|
154
|
+
closed: false,
|
|
155
|
+
applied_configurations: [...roles].reverse(),
|
|
156
|
+
});
|
|
157
|
+
expect(
|
|
158
|
+
await closeAccount(f.stripe, f.component, f.ctx, {
|
|
159
|
+
stripeAccountId: "acct_scope",
|
|
160
|
+
expectedAppliedConfigurations: roles,
|
|
161
|
+
}),
|
|
162
|
+
).toEqual({ closed: true });
|
|
163
|
+
expect(f.retrieve).toHaveBeenCalledTimes(1);
|
|
164
|
+
expect(f.close).toHaveBeenCalledWith(
|
|
165
|
+
"acct_scope",
|
|
166
|
+
{ applied_configurations: roles },
|
|
167
|
+
{ idempotencyKey: "betterstripe-close:acct_scope" },
|
|
168
|
+
);
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
it.each([
|
|
173
|
+
{ id: "acct_foreign", closed: true, applied_configurations: [] },
|
|
174
|
+
{ id: "acct_scope", applied_configurations: ["recipient"] },
|
|
175
|
+
{
|
|
176
|
+
id: "acct_scope",
|
|
177
|
+
closed: false,
|
|
178
|
+
applied_configurations: ["recipient", "recipient"],
|
|
179
|
+
},
|
|
180
|
+
{ id: "acct_scope", closed: false, applied_configurations: ["unsupported"] },
|
|
181
|
+
])("holds malformed/foreign current provider scope %j", async (provider) => {
|
|
182
|
+
const f = await fixture();
|
|
183
|
+
f.retrieve.mockResolvedValue(provider);
|
|
184
|
+
await expect(
|
|
185
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
186
|
+
stripeAccountId: "acct_scope",
|
|
187
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
188
|
+
}),
|
|
189
|
+
).rejects.toThrow();
|
|
190
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("does not expand an existing original pending scope after failed dispatch", async () => {
|
|
194
|
+
const f = await fixture();
|
|
195
|
+
f.retrieve.mockResolvedValue({
|
|
196
|
+
id: "acct_scope",
|
|
197
|
+
closed: false,
|
|
198
|
+
applied_configurations: ["recipient"],
|
|
199
|
+
});
|
|
200
|
+
f.close.mockRejectedValueOnce(new Error("Synthetic unknown response"));
|
|
201
|
+
await expect(
|
|
202
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
203
|
+
stripeAccountId: "acct_scope",
|
|
204
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
205
|
+
}),
|
|
206
|
+
).rejects.toThrow();
|
|
207
|
+
f.retrieve.mockResolvedValue({
|
|
208
|
+
id: "acct_scope",
|
|
209
|
+
closed: true,
|
|
210
|
+
applied_configurations: [],
|
|
211
|
+
});
|
|
212
|
+
await expect(
|
|
213
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
214
|
+
stripeAccountId: "acct_scope",
|
|
215
|
+
expectedAppliedConfigurations: ["merchant", "recipient"],
|
|
216
|
+
}),
|
|
217
|
+
).rejects.toThrow();
|
|
218
|
+
expect(f.close).toHaveBeenCalledTimes(1);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("holds absent original scope even when the same provider account is already closed", async () => {
|
|
222
|
+
const f = await fixture();
|
|
223
|
+
await f.t.mutation(f.component.core.mutations.upsertAccount, {
|
|
224
|
+
stripeAccountId: "acct_unknown",
|
|
225
|
+
userId: "original",
|
|
226
|
+
});
|
|
227
|
+
f.retrieve.mockResolvedValue({
|
|
228
|
+
id: "acct_unknown",
|
|
229
|
+
closed: true,
|
|
230
|
+
applied_configurations: [],
|
|
231
|
+
});
|
|
232
|
+
await expect(
|
|
233
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
234
|
+
stripeAccountId: "acct_unknown",
|
|
235
|
+
expectedAppliedConfigurations: [],
|
|
236
|
+
}),
|
|
237
|
+
).rejects.toThrow();
|
|
238
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("reconciles observed closed state after a pre-dispatch crash without attributing provider causality", async () => {
|
|
242
|
+
const f = await fixture();
|
|
243
|
+
await f.t.mutation(f.component.accountClosure.mutations.prepare, {
|
|
244
|
+
stripeAccountId: "acct_scope",
|
|
245
|
+
});
|
|
246
|
+
f.retrieve.mockResolvedValue({
|
|
247
|
+
id: "acct_scope",
|
|
248
|
+
closed: true,
|
|
249
|
+
applied_configurations: [],
|
|
250
|
+
});
|
|
251
|
+
expect(
|
|
252
|
+
await closeAccount(f.stripe, f.component, f.ctx, {
|
|
253
|
+
stripeAccountId: "acct_scope",
|
|
254
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
255
|
+
}),
|
|
256
|
+
).toEqual({ closed: true });
|
|
257
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("holds removal of the current original mapping while the fresh provider read is suspended", async () => {
|
|
261
|
+
const f = await fixture();
|
|
262
|
+
f.retrieve.mockImplementationOnce(async () => {
|
|
263
|
+
await expect(
|
|
264
|
+
f.t.mutation(f.component.core.mutations.upsertAccount, {
|
|
265
|
+
stripeAccountId: "acct_scope",
|
|
266
|
+
userId: "replacement",
|
|
267
|
+
appliedConfigurations: ["recipient"],
|
|
268
|
+
}),
|
|
269
|
+
).rejects.toThrow("ownership is pinned");
|
|
270
|
+
expect(
|
|
271
|
+
await f.t.mutation(f.component.core.mutations.deleteAccountByStripeId, {
|
|
272
|
+
stripeAccountId: "acct_scope",
|
|
273
|
+
}),
|
|
274
|
+
).toBe(true);
|
|
275
|
+
return {
|
|
276
|
+
id: "acct_scope",
|
|
277
|
+
closed: false,
|
|
278
|
+
applied_configurations: ["recipient"],
|
|
279
|
+
};
|
|
280
|
+
});
|
|
281
|
+
let failure: unknown;
|
|
282
|
+
try {
|
|
283
|
+
await closeAccount(f.stripe, f.component, f.ctx, {
|
|
284
|
+
stripeAccountId: "acct_scope",
|
|
285
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
286
|
+
});
|
|
287
|
+
} catch (error) {
|
|
288
|
+
failure = error;
|
|
289
|
+
}
|
|
290
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
291
|
+
expect(failure).toBeDefined();
|
|
292
|
+
expect(
|
|
293
|
+
await f.t.query(
|
|
294
|
+
f.component.accountClosure.queries.getClosedAccountByStripeId,
|
|
295
|
+
{ stripeAccountId: "acct_scope" },
|
|
296
|
+
),
|
|
297
|
+
).toBeNull();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("preserves known original history from terminal observation even when the live mapping was removed", async () => {
|
|
301
|
+
const f = await fixture();
|
|
302
|
+
f.retrieve.mockImplementationOnce(async () => {
|
|
303
|
+
await f.t.mutation(f.component.core.mutations.deleteAccountByStripeId, {
|
|
304
|
+
stripeAccountId: "acct_scope",
|
|
305
|
+
});
|
|
306
|
+
return { id: "acct_scope", closed: true, applied_configurations: [] };
|
|
307
|
+
});
|
|
308
|
+
expect(
|
|
309
|
+
await closeAccount(f.stripe, f.component, f.ctx, {
|
|
310
|
+
stripeAccountId: "acct_scope",
|
|
311
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
312
|
+
}),
|
|
313
|
+
).toEqual({ closed: true });
|
|
314
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
315
|
+
expect(
|
|
316
|
+
await f.t.query(
|
|
317
|
+
f.component.accountClosure.queries.getClosedAccountByStripeId,
|
|
318
|
+
{ stripeAccountId: "acct_scope" },
|
|
319
|
+
),
|
|
320
|
+
).toMatchObject({ userId: "original", appliedConfigurations: ["recipient"] });
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("preserves the existing unguarded closed-argument validator for unsupported runtime keys", async () => {
|
|
324
|
+
const f = await fixture();
|
|
325
|
+
await expect(
|
|
326
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
327
|
+
stripeAccountId: "acct_scope",
|
|
328
|
+
...{ unsupportedCloseMode: true },
|
|
329
|
+
}),
|
|
330
|
+
).rejects.toThrow();
|
|
331
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("validates guarded inputs before consuming an original pending request", async () => {
|
|
335
|
+
const f = await fixture();
|
|
336
|
+
await expect(
|
|
337
|
+
closeAccount(f.stripe, f.component, f.ctx, {
|
|
338
|
+
stripeAccountId: "acct_scope",
|
|
339
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
340
|
+
...{ unsupportedCloseMode: true },
|
|
341
|
+
}),
|
|
342
|
+
).rejects.toThrow();
|
|
343
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
344
|
+
const original = await f.t.mutation(
|
|
345
|
+
f.component.accountClosure.mutations.prepare,
|
|
346
|
+
{ stripeAccountId: "acct_scope" },
|
|
347
|
+
);
|
|
348
|
+
expect(original.isRetry).toBe(false);
|
|
349
|
+
});
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import type Stripe from "stripe";
|
|
4
|
+
import type { RunCtx } from "../helpers.js";
|
|
5
|
+
import { components, initConvexTest } from "../setup.test.js";
|
|
6
|
+
import schema from "../../component/schema.js";
|
|
7
|
+
import { previewAccountClosure } from "./accountClosurePreview.js";
|
|
8
|
+
|
|
9
|
+
const modules = import.meta.glob("../../component/**/*.ts");
|
|
10
|
+
afterEach(() => vi.restoreAllMocks());
|
|
11
|
+
async function fixture() {
|
|
12
|
+
const t = initConvexTest();
|
|
13
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
14
|
+
const component = components.betterStripe;
|
|
15
|
+
await t.mutation(component.core.mutations.upsertAccount, {
|
|
16
|
+
stripeAccountId: "acct_preview",
|
|
17
|
+
userId: "owner",
|
|
18
|
+
appliedConfigurations: ["recipient"],
|
|
19
|
+
});
|
|
20
|
+
const retrieve = vi.fn().mockResolvedValue({
|
|
21
|
+
id: "acct_preview",
|
|
22
|
+
closed: false,
|
|
23
|
+
applied_configurations: ["customer", "recipient"],
|
|
24
|
+
});
|
|
25
|
+
const balance = vi.fn().mockResolvedValue({
|
|
26
|
+
available: [
|
|
27
|
+
{ amount: 100, currency: "usd" },
|
|
28
|
+
{ amount: -50, currency: "eur" },
|
|
29
|
+
],
|
|
30
|
+
pending: [{ amount: -100, currency: "usd" }],
|
|
31
|
+
});
|
|
32
|
+
const sessions = vi.fn().mockResolvedValue({
|
|
33
|
+
data: [
|
|
34
|
+
{
|
|
35
|
+
id: "cs_open",
|
|
36
|
+
object: "checkout.session",
|
|
37
|
+
customer_account: "acct_preview",
|
|
38
|
+
status: "open",
|
|
39
|
+
created: 100,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
has_more: true,
|
|
43
|
+
});
|
|
44
|
+
const close = vi.fn();
|
|
45
|
+
const stripe = {
|
|
46
|
+
v2: { core: { accounts: { retrieve, close } } },
|
|
47
|
+
balance: { retrieve: balance },
|
|
48
|
+
checkout: { sessions: { list: sessions } },
|
|
49
|
+
} as unknown as Stripe;
|
|
50
|
+
const runMutation = vi.fn(t.mutation);
|
|
51
|
+
const ctx = { runQuery: t.query, runMutation } as RunCtx;
|
|
52
|
+
return {
|
|
53
|
+
t,
|
|
54
|
+
component,
|
|
55
|
+
ctx,
|
|
56
|
+
stripe,
|
|
57
|
+
retrieve,
|
|
58
|
+
balance,
|
|
59
|
+
sessions,
|
|
60
|
+
close,
|
|
61
|
+
runMutation,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
it("observes exact scope and separate currencies, retaining known blockers and unknown completeness without writes", async () => {
|
|
66
|
+
const f = await fixture();
|
|
67
|
+
const before = await f.t.query(
|
|
68
|
+
f.component.core.queries.getAccountByStripeId,
|
|
69
|
+
{ stripeAccountId: "acct_preview" },
|
|
70
|
+
);
|
|
71
|
+
const result = await previewAccountClosure(f.stripe, f.component, f.ctx, {
|
|
72
|
+
stripeAccountId: "acct_preview",
|
|
73
|
+
sessionLimit: 10,
|
|
74
|
+
});
|
|
75
|
+
expect(result.account).toEqual({
|
|
76
|
+
status: "observed",
|
|
77
|
+
value: { closed: false, appliedConfigurations: ["customer", "recipient"] },
|
|
78
|
+
});
|
|
79
|
+
expect(result.balances).toEqual({
|
|
80
|
+
status: "observed",
|
|
81
|
+
value: [
|
|
82
|
+
{ currency: "usd", available: 100, pending: -100 },
|
|
83
|
+
{ currency: "eur", available: -50, pending: 0 },
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
expect(result.knownBlockers).toEqual(
|
|
87
|
+
expect.arrayContaining(["nonzero_balance", "open_checkout_sessions"]),
|
|
88
|
+
);
|
|
89
|
+
expect(result.unknownReasons).toEqual(
|
|
90
|
+
expect.arrayContaining([
|
|
91
|
+
"financial_coverage_unknown",
|
|
92
|
+
"open_sessions_truncated",
|
|
93
|
+
]),
|
|
94
|
+
);
|
|
95
|
+
expect(result.financialCoverage).toBe("unknown");
|
|
96
|
+
expect(result).not.toHaveProperty("eligible");
|
|
97
|
+
expect(f.runMutation).not.toHaveBeenCalled();
|
|
98
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
99
|
+
expect(
|
|
100
|
+
await f.t.query(f.component.core.queries.getAccountByStripeId, {
|
|
101
|
+
stripeAccountId: "acct_preview",
|
|
102
|
+
}),
|
|
103
|
+
).toEqual(before);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("keeps failed or unsupported observations unknown without inventing zero balances or empty sessions", async () => {
|
|
107
|
+
const f = await fixture();
|
|
108
|
+
f.retrieve.mockRejectedValue(new Error("Unavailable"));
|
|
109
|
+
f.balance.mockRejectedValue(new Error("Unsupported"));
|
|
110
|
+
f.sessions.mockResolvedValue({ data: [], has_more: true });
|
|
111
|
+
const result = await previewAccountClosure(f.stripe, f.component, f.ctx, {
|
|
112
|
+
stripeAccountId: "acct_preview",
|
|
113
|
+
});
|
|
114
|
+
expect(result.account.status).toBe("unknown");
|
|
115
|
+
expect(result.balances.status).toBe("unknown");
|
|
116
|
+
expect(result.openSessions.status).toBe("unknown");
|
|
117
|
+
expect(result.unknownReasons).toEqual(
|
|
118
|
+
expect.arrayContaining([
|
|
119
|
+
"account_unavailable",
|
|
120
|
+
"balances_unavailable",
|
|
121
|
+
"open_sessions_unavailable",
|
|
122
|
+
]),
|
|
123
|
+
);
|
|
124
|
+
expect(f.runMutation).not.toHaveBeenCalled();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("keeps complete empty observations separate from financial clearance and bounds the exact open-session read", async () => {
|
|
128
|
+
const f = await fixture();
|
|
129
|
+
f.balance.mockResolvedValue({ available: [], pending: [] });
|
|
130
|
+
f.sessions.mockResolvedValue({ data: [], has_more: false });
|
|
131
|
+
const result = await previewAccountClosure(f.stripe, f.component, f.ctx, {
|
|
132
|
+
stripeAccountId: "acct_preview",
|
|
133
|
+
sessionLimit: 1,
|
|
134
|
+
});
|
|
135
|
+
expect(result.knownBlockers).toEqual([]);
|
|
136
|
+
expect(result.openSessions).toEqual({
|
|
137
|
+
status: "observed",
|
|
138
|
+
value: { sessionIds: [], hasMore: false },
|
|
139
|
+
});
|
|
140
|
+
expect(result.unknownReasons).toEqual(["financial_coverage_unknown"]);
|
|
141
|
+
expect(f.sessions).toHaveBeenCalledWith({
|
|
142
|
+
customer_account: "acct_preview",
|
|
143
|
+
status: "open",
|
|
144
|
+
limit: 1,
|
|
145
|
+
});
|
|
146
|
+
expect(f.runMutation).not.toHaveBeenCalled();
|
|
147
|
+
await expect(
|
|
148
|
+
previewAccountClosure(f.stripe, f.component, f.ctx, {
|
|
149
|
+
stripeAccountId: "acct_preview",
|
|
150
|
+
sessionLimit: 101,
|
|
151
|
+
}),
|
|
152
|
+
).rejects.toThrow();
|
|
153
|
+
expect(f.sessions).toHaveBeenCalledTimes(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("does not substitute foreign provider scope or malformed balance amounts into an observation", async () => {
|
|
157
|
+
const f = await fixture();
|
|
158
|
+
f.retrieve.mockResolvedValue({
|
|
159
|
+
id: "acct_foreign",
|
|
160
|
+
closed: false,
|
|
161
|
+
applied_configurations: [],
|
|
162
|
+
});
|
|
163
|
+
f.balance.mockResolvedValue({
|
|
164
|
+
available: [{ amount: Number.NaN, currency: "usd" }],
|
|
165
|
+
pending: [],
|
|
166
|
+
});
|
|
167
|
+
const result = await previewAccountClosure(f.stripe, f.component, f.ctx, {
|
|
168
|
+
stripeAccountId: "acct_preview",
|
|
169
|
+
});
|
|
170
|
+
expect(result.account.status).toBe("unknown");
|
|
171
|
+
expect(result.balances.status).toBe("unknown");
|
|
172
|
+
expect(result.financialCoverage).toBe("unknown");
|
|
173
|
+
expect(f.close).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("exposes the supported public SDK preview and guarded close using actual registered component reads", async () => {
|
|
177
|
+
const f = await fixture();
|
|
178
|
+
const {
|
|
179
|
+
BetterStripe,
|
|
180
|
+
closeAccountOptionsSchema,
|
|
181
|
+
accountClosurePreviewOptionsSchema,
|
|
182
|
+
} = await import("../index.js");
|
|
183
|
+
const { getStripeClient } = await import("../helpers.js");
|
|
184
|
+
const key = `sk_test_closure_preview_${crypto.randomUUID()}`;
|
|
185
|
+
const client = new BetterStripe(f.component, { STRIPE_SECRET_KEY: key });
|
|
186
|
+
const raw = getStripeClient(key);
|
|
187
|
+
vi.spyOn(raw.v2.core.accounts, "retrieve").mockImplementation(f.retrieve);
|
|
188
|
+
vi.spyOn(raw.balance, "retrieve").mockImplementation(f.balance);
|
|
189
|
+
vi.spyOn(raw.checkout.sessions, "list").mockImplementation(f.sessions);
|
|
190
|
+
const close = vi
|
|
191
|
+
.spyOn(raw.v2.core.accounts, "close")
|
|
192
|
+
.mockRejectedValue(new Error("No provider write expected"));
|
|
193
|
+
expect(
|
|
194
|
+
closeAccountOptionsSchema.safeParse({
|
|
195
|
+
stripeAccountId: "acct_preview",
|
|
196
|
+
expectedAppliedConfigurations: ["recipient", "recipient"],
|
|
197
|
+
}).success,
|
|
198
|
+
).toBe(false);
|
|
199
|
+
expect(
|
|
200
|
+
accountClosurePreviewOptionsSchema.safeParse({
|
|
201
|
+
stripeAccountId: "acct_preview",
|
|
202
|
+
sessionLimit: 101,
|
|
203
|
+
}).success,
|
|
204
|
+
).toBe(false);
|
|
205
|
+
const result = await client.previewAccountClosure(f.ctx, {
|
|
206
|
+
stripeAccountId: "acct_preview",
|
|
207
|
+
});
|
|
208
|
+
expect(result.account.status).toBe("observed");
|
|
209
|
+
expect(result.componentBlockers).toEqual({ status: "observed", value: [] });
|
|
210
|
+
expect(result.financialCoverage).toBe("unknown");
|
|
211
|
+
expect(f.runMutation).not.toHaveBeenCalled();
|
|
212
|
+
await expect(
|
|
213
|
+
client.closeAccount(f.ctx, {
|
|
214
|
+
stripeAccountId: "acct_preview",
|
|
215
|
+
expectedAppliedConfigurations: ["recipient"],
|
|
216
|
+
}),
|
|
217
|
+
).rejects.toThrow();
|
|
218
|
+
expect(close).not.toHaveBeenCalled();
|
|
219
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AppliedConfiguration } from "../../component/core/validators.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export const closureConfigurations = z
|
|
5
|
+
.array(z.enum(["customer", "merchant", "recipient"]))
|
|
6
|
+
.max(3)
|
|
7
|
+
.refine((values) => new Set(values).size === values.length);
|
|
8
|
+
|
|
9
|
+
export const closeAccountOptionsSchema = z
|
|
10
|
+
.object({
|
|
11
|
+
stripeAccountId: z
|
|
12
|
+
.string()
|
|
13
|
+
.regex(/^acct_[A-Za-z0-9_]+$/)
|
|
14
|
+
.max(128),
|
|
15
|
+
expectedAppliedConfigurations: closureConfigurations.optional(),
|
|
16
|
+
})
|
|
17
|
+
.strict();
|
|
18
|
+
|
|
19
|
+
/** Host-authenticated scope confirmation, not financial clearance. */
|
|
20
|
+
export type CloseAccountOptions = {
|
|
21
|
+
stripeAccountId: string;
|
|
22
|
+
expectedAppliedConfigurations?: AppliedConfiguration[];
|
|
23
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
4
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
5
|
+
import type { CurrentClosureOwner } from "../../component/accountClosure/queries.js";
|
|
6
|
+
import type { StripeAccountClosure } from "./accountClosures.js";
|
|
7
|
+
import {
|
|
8
|
+
closureConfigurations,
|
|
9
|
+
closeAccountOptionsSchema,
|
|
10
|
+
type CloseAccountOptions,
|
|
11
|
+
} from "./accountCloseOptions.js";
|
|
12
|
+
|
|
13
|
+
export function confirmedClosureScope(
|
|
14
|
+
opts: CloseAccountOptions,
|
|
15
|
+
closure: StripeAccountClosure,
|
|
16
|
+
) {
|
|
17
|
+
const expected =
|
|
18
|
+
closeAccountOptionsSchema.parse(opts).expectedAppliedConfigurations;
|
|
19
|
+
const original = closureConfigurations.safeParse(
|
|
20
|
+
closure.appliedConfigurations,
|
|
21
|
+
);
|
|
22
|
+
if (
|
|
23
|
+
!expected ||
|
|
24
|
+
!original.success ||
|
|
25
|
+
closure.stripeAccountId !== opts.stripeAccountId ||
|
|
26
|
+
(!closure.userId && !closure.orgId) ||
|
|
27
|
+
!sameScope(expected, original.data)
|
|
28
|
+
)
|
|
29
|
+
throw new Error(
|
|
30
|
+
"Account closure original scope does not match confirmation",
|
|
31
|
+
);
|
|
32
|
+
return expected;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Point-in-time scope observation, not financial clearance or provider-side fencing. */
|
|
36
|
+
export async function guardedAccountIsClosed(
|
|
37
|
+
stripe: Stripe,
|
|
38
|
+
component: Component,
|
|
39
|
+
ctx: RunCtx,
|
|
40
|
+
opts: CloseAccountOptions,
|
|
41
|
+
closure: StripeAccountClosure,
|
|
42
|
+
) {
|
|
43
|
+
const expected = confirmedClosureScope(opts, closure);
|
|
44
|
+
const current = z
|
|
45
|
+
.object({
|
|
46
|
+
id: z.literal(opts.stripeAccountId),
|
|
47
|
+
closed: z.boolean(),
|
|
48
|
+
applied_configurations: closureConfigurations.optional(),
|
|
49
|
+
})
|
|
50
|
+
.passthrough()
|
|
51
|
+
.parse(await stripe.v2.core.accounts.retrieve(opts.stripeAccountId));
|
|
52
|
+
const account: CurrentClosureOwner = await ctx.runQuery(
|
|
53
|
+
componentRef(component, "accountClosure/queries/currentOwner"),
|
|
54
|
+
{ stripeAccountId: opts.stripeAccountId },
|
|
55
|
+
);
|
|
56
|
+
if (
|
|
57
|
+
account.status === "ambiguous" ||
|
|
58
|
+
(account.status === "unique" &&
|
|
59
|
+
(account.userId !== closure.userId || account.orgId !== closure.orgId))
|
|
60
|
+
)
|
|
61
|
+
throw new Error("Account closure original ownership changed or is unknown");
|
|
62
|
+
// Terminal account state may clear roles. This observation does not attribute its cause.
|
|
63
|
+
if (current.closed) return true;
|
|
64
|
+
if (account.status !== "unique")
|
|
65
|
+
throw new Error("Account closure current ownership is unavailable");
|
|
66
|
+
if (
|
|
67
|
+
!current.applied_configurations ||
|
|
68
|
+
!sameScope(expected, current.applied_configurations)
|
|
69
|
+
)
|
|
70
|
+
throw new Error("Account closure provider scope changed or is unknown");
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function sameScope(left: string[], right: string[]) {
|
|
75
|
+
return (
|
|
76
|
+
left.length === right.length && left.every((value) => right.includes(value))
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function assertGuardedCloseAcknowledgement(
|
|
81
|
+
value: unknown,
|
|
82
|
+
stripeAccountId: string,
|
|
83
|
+
) {
|
|
84
|
+
z.object({ id: z.literal(stripeAccountId), closed: z.literal(true) })
|
|
85
|
+
.passthrough()
|
|
86
|
+
.parse(value);
|
|
87
|
+
}
|