@fantastic.dev/better-stripe 0.39.0 → 0.41.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 +24 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +442 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +221 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +442 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +221 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/costRecovery-invoices.d.ts +2 -2
- package/dist/client/costRecovery-invoices.d.ts.map +1 -1
- package/dist/client/costRecovery-invoices.js +7 -6
- package/dist/client/costRecovery-invoices.js.map +1 -1
- package/dist/client/costRecovery.d.ts +5 -2
- package/dist/client/costRecovery.d.ts.map +1 -1
- package/dist/client/costRecovery.js +3 -1
- package/dist/client/costRecovery.js.map +1 -1
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/paymentSource/client.d.ts +21 -0
- package/dist/client/paymentSource/client.d.ts.map +1 -0
- package/dist/client/paymentSource/client.js +8 -0
- package/dist/client/paymentSource/client.js.map +1 -0
- package/dist/client/paymentSource/contracts.d.ts +30 -0
- package/dist/client/paymentSource/contracts.d.ts.map +1 -0
- package/dist/client/paymentSource/contracts.js +16 -0
- package/dist/client/paymentSource/contracts.js.map +1 -0
- package/dist/client/paymentSource/economics.d.ts +12 -0
- package/dist/client/paymentSource/economics.d.ts.map +1 -0
- package/dist/client/paymentSource/economics.js +13 -0
- package/dist/client/paymentSource/economics.js.map +1 -0
- package/dist/client/paymentSource/funding.d.ts +10 -0
- package/dist/client/paymentSource/funding.d.ts.map +1 -0
- package/dist/client/paymentSource/funding.js +12 -0
- package/dist/client/paymentSource/funding.js.map +1 -0
- package/dist/client/paymentSource/history.d.ts +13 -0
- package/dist/client/paymentSource/history.d.ts.map +1 -0
- package/dist/client/paymentSource/history.js +41 -0
- package/dist/client/paymentSource/history.js.map +1 -0
- package/dist/client/paymentSource/resolver.d.ts +6 -0
- package/dist/client/paymentSource/resolver.d.ts.map +1 -0
- package/dist/client/paymentSource/resolver.js +222 -0
- package/dist/client/paymentSource/resolver.js.map +1 -0
- package/dist/client/recoveryWorkflow/client.d.ts +14 -0
- package/dist/client/recoveryWorkflow/client.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/client.js +34 -0
- package/dist/client/recoveryWorkflow/client.js.map +1 -0
- package/dist/client/recoveryWorkflow/contracts.d.ts +63 -0
- package/dist/client/recoveryWorkflow/contracts.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/contracts.js +11 -0
- package/dist/client/recoveryWorkflow/contracts.js.map +1 -0
- package/dist/client/recoveryWorkflow/provider.d.ts +10 -0
- package/dist/client/recoveryWorkflow/provider.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/provider.js +80 -0
- package/dist/client/recoveryWorkflow/provider.js.map +1 -0
- package/dist/client/recoveryWorkflow/settlement.d.ts +6 -0
- package/dist/client/recoveryWorkflow/settlement.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/settlement.js +43 -0
- package/dist/client/recoveryWorkflow/settlement.js.map +1 -0
- package/dist/client/settlement/verifiedSale.d.ts.map +1 -1
- package/dist/client/settlement/verifiedSale.js +4 -6
- package/dist/client/settlement/verifiedSale.js.map +1 -1
- package/dist/component/_generated/api.d.ts +12 -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 +164 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +194 -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/recoveryWorkflow/bridge.d.ts +21 -0
- package/dist/component/recoveryWorkflow/bridge.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/bridge.js +52 -0
- package/dist/component/recoveryWorkflow/bridge.js.map +1 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts +83 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/helpers.js +48 -0
- package/dist/component/recoveryWorkflow/helpers.js.map +1 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts +91 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/mutations.js +177 -0
- package/dist/component/recoveryWorkflow/mutations.js.map +1 -0
- package/dist/component/recoveryWorkflow/queries.d.ts +118 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/queries.js +20 -0
- package/dist/component/recoveryWorkflow/queries.js.map +1 -0
- package/dist/component/recoveryWorkflow/schema.d.ts +653 -0
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/schema.js +81 -0
- package/dist/component/recoveryWorkflow/schema.js.map +1 -0
- package/dist/component/recoveryWorkflow/steps.d.ts +70 -0
- package/dist/component/recoveryWorkflow/steps.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/steps.js +94 -0
- package/dist/component/recoveryWorkflow/steps.js.map +1 -0
- package/dist/component/recoveryWorkflow/workflow.d.ts +22 -0
- package/dist/component/recoveryWorkflow/workflow.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/workflow.js +56 -0
- package/dist/component/recoveryWorkflow/workflow.js.map +1 -0
- package/dist/component/schema.d.ts +221 -0
- package/dist/component/schema.js +3 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/settlement.md +48 -0
- package/package.json +1 -1
- package/src/client/costRecovery-invoices.ts +9 -7
- package/src/client/costRecovery.ts +8 -3
- package/src/client/index.ts +6 -2
- package/src/client/paymentSource/client.ts +13 -0
- package/src/client/paymentSource/contracts.ts +33 -0
- package/src/client/paymentSource/economics.ts +20 -0
- package/src/client/paymentSource/funding.ts +23 -0
- package/src/client/paymentSource/history.ts +48 -0
- package/src/client/paymentSource/resolver.test.ts +582 -0
- package/src/client/paymentSource/resolver.ts +350 -0
- package/src/client/recoveryWorkflow/client.test.ts +37 -0
- package/src/client/recoveryWorkflow/client.ts +76 -0
- package/src/client/recoveryWorkflow/contracts.ts +72 -0
- package/src/client/recoveryWorkflow/provider.test.ts +424 -0
- package/src/client/recoveryWorkflow/provider.ts +119 -0
- package/src/client/recoveryWorkflow/settlement.ts +67 -0
- package/src/client/settlement/verifiedSale.ts +4 -10
- package/src/component/_generated/api.ts +12 -0
- package/src/component/_generated/component.ts +222 -0
- package/src/component/recoveryWorkflow/bridge.test.ts +481 -0
- package/src/component/recoveryWorkflow/bridge.ts +66 -0
- package/src/component/recoveryWorkflow/helpers.ts +58 -0
- package/src/component/recoveryWorkflow/journal.test.ts +104 -0
- package/src/component/recoveryWorkflow/mutations.ts +224 -0
- package/src/component/recoveryWorkflow/queries.ts +19 -0
- package/src/component/recoveryWorkflow/schema.ts +92 -0
- package/src/component/recoveryWorkflow/state.test.ts +191 -0
- package/src/component/recoveryWorkflow/steps.ts +97 -0
- package/src/component/recoveryWorkflow/workflow.ts +73 -0
- package/src/component/schema.ts +6 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import { componentsGeneric, type FunctionReference } from "convex/server";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import { initConvexTest } from "../setup.test.js";
|
|
6
|
+
import schema from "../../component/schema.js";
|
|
7
|
+
import { CostRecoveryClient } from "../costRecovery.js";
|
|
8
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
9
|
+
import type { RecoveryProviderRequest } from "./contracts.js";
|
|
10
|
+
const modules = import.meta.glob("../../component/**/*.ts");
|
|
11
|
+
const nested = componentsGeneric();
|
|
12
|
+
const scope = {
|
|
13
|
+
accountId: "acct_owner",
|
|
14
|
+
scopeKey: "store",
|
|
15
|
+
currency: "usd",
|
|
16
|
+
groupKey: "group",
|
|
17
|
+
};
|
|
18
|
+
async function setup(
|
|
19
|
+
kind: "invoice" | "settlement" = "invoice",
|
|
20
|
+
dispatch = true,
|
|
21
|
+
releaseUncollectible = false,
|
|
22
|
+
) {
|
|
23
|
+
const t = initConvexTest();
|
|
24
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
25
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
26
|
+
const component = nested.betterStripe as unknown as Component;
|
|
27
|
+
const client = new CostRecoveryClient(component);
|
|
28
|
+
let writes = 0,
|
|
29
|
+
failWrite = 0;
|
|
30
|
+
const ctx = {
|
|
31
|
+
runQuery: (
|
|
32
|
+
ref: FunctionReference<"query">,
|
|
33
|
+
args: Record<string, unknown>,
|
|
34
|
+
) => t.query(ref, args),
|
|
35
|
+
runMutation: (
|
|
36
|
+
ref: FunctionReference<"mutation">,
|
|
37
|
+
args: Record<string, unknown>,
|
|
38
|
+
) => {
|
|
39
|
+
if (++writes === failWrite) throw new Error("private database detail");
|
|
40
|
+
return t.mutation(ref, args);
|
|
41
|
+
},
|
|
42
|
+
} as unknown as RunCtx;
|
|
43
|
+
await client.recordClaim(ctx, {
|
|
44
|
+
accountId: scope.accountId,
|
|
45
|
+
scopeKey: scope.scopeKey,
|
|
46
|
+
currency: scope.currency,
|
|
47
|
+
claimKey: "claim",
|
|
48
|
+
sourceChargeId: "ch_original",
|
|
49
|
+
sourceGross: 1000,
|
|
50
|
+
sourceProcessingFee: 30,
|
|
51
|
+
amountDue: 30,
|
|
52
|
+
revision: 1,
|
|
53
|
+
});
|
|
54
|
+
const allocation = {
|
|
55
|
+
sourceChargeId: "ch_source",
|
|
56
|
+
gross: 1000,
|
|
57
|
+
currency: "usd",
|
|
58
|
+
processingFee: 30,
|
|
59
|
+
platformNet: 40,
|
|
60
|
+
adjustmentAmount: 30,
|
|
61
|
+
transfers: [
|
|
62
|
+
{
|
|
63
|
+
destinationAccountId: "acct_seller",
|
|
64
|
+
role: "store" as const,
|
|
65
|
+
amount: 900,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
};
|
|
69
|
+
await client.prepareGroup(ctx, {
|
|
70
|
+
...scope,
|
|
71
|
+
...(kind === "settlement"
|
|
72
|
+
? {
|
|
73
|
+
sourceKind: "settlement" as const,
|
|
74
|
+
sourceId: "ch_source",
|
|
75
|
+
sourceGross: 1000,
|
|
76
|
+
sourceProcessingFee: 30,
|
|
77
|
+
}
|
|
78
|
+
: { sourceKind: "invoice" as const, sourceId: "in_source" }),
|
|
79
|
+
maxDebit: 1000,
|
|
80
|
+
maxCredit: 0,
|
|
81
|
+
});
|
|
82
|
+
const op = await t.mutation(
|
|
83
|
+
nested.betterStripe.recoveryWorkflow.mutations.start,
|
|
84
|
+
{
|
|
85
|
+
...scope,
|
|
86
|
+
requestKey: "request",
|
|
87
|
+
provider: "function://fixture/provider",
|
|
88
|
+
providerApiVersion: "2026-09-30.endive",
|
|
89
|
+
definitionVersion: 1,
|
|
90
|
+
releaseUncollectible,
|
|
91
|
+
maxAttempts: 2,
|
|
92
|
+
retryDelayMs: 1,
|
|
93
|
+
...(kind === "settlement" ? { allocation } : {}),
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
const request: RecoveryProviderRequest = {
|
|
97
|
+
...scope,
|
|
98
|
+
operationId: op.operationId,
|
|
99
|
+
version: op.version,
|
|
100
|
+
phase: "execute",
|
|
101
|
+
providerApiVersion: "2026-09-30.endive",
|
|
102
|
+
definitionVersion: 1,
|
|
103
|
+
};
|
|
104
|
+
if (dispatch)
|
|
105
|
+
await t.mutation(nested.betterStripe.recoveryWorkflow.steps.beginProvider, {
|
|
106
|
+
...scope,
|
|
107
|
+
operationId: op.operationId,
|
|
108
|
+
version: 1,
|
|
109
|
+
phase: "execute",
|
|
110
|
+
});
|
|
111
|
+
return {
|
|
112
|
+
dispatchReconcile: () =>
|
|
113
|
+
t.mutation(nested.betterStripe.recoveryWorkflow.steps.beginProvider, {
|
|
114
|
+
...scope,
|
|
115
|
+
operationId: op.operationId,
|
|
116
|
+
version: 1,
|
|
117
|
+
phase: "reconcile",
|
|
118
|
+
}),
|
|
119
|
+
t,
|
|
120
|
+
client,
|
|
121
|
+
ctx,
|
|
122
|
+
request,
|
|
123
|
+
group: (await client.getGroup(ctx, { groupKey: scope.groupKey }))!,
|
|
124
|
+
failNext: (offset = 1) => {
|
|
125
|
+
failWrite = writes + offset;
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
it("recovers invoice-item provider success/database failure with the real Stripe SDK and original key", async () => {
|
|
130
|
+
const f = await setup();
|
|
131
|
+
let status = "draft";
|
|
132
|
+
let item: Record<string, unknown> | null = null;
|
|
133
|
+
let creates = 0;
|
|
134
|
+
const headers: {
|
|
135
|
+
version: string | null;
|
|
136
|
+
authorized: boolean;
|
|
137
|
+
key: string | null;
|
|
138
|
+
}[] = [];
|
|
139
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
140
|
+
const h = new Headers(init?.headers);
|
|
141
|
+
headers.push({
|
|
142
|
+
version: h.get("Stripe-Version"),
|
|
143
|
+
authorized: h.get("Authorization") === "Bearer sk_test_runtime_only",
|
|
144
|
+
key: h.get("Idempotency-Key"),
|
|
145
|
+
});
|
|
146
|
+
let body: unknown;
|
|
147
|
+
if (String(url).includes("/invoices/"))
|
|
148
|
+
body = {
|
|
149
|
+
id: "in_source",
|
|
150
|
+
customer_account: scope.accountId,
|
|
151
|
+
currency: "usd",
|
|
152
|
+
status,
|
|
153
|
+
};
|
|
154
|
+
else if (init?.method === "POST") {
|
|
155
|
+
creates++;
|
|
156
|
+
item = {
|
|
157
|
+
id: "ii_recovery",
|
|
158
|
+
invoice: "in_source",
|
|
159
|
+
customer_account: scope.accountId,
|
|
160
|
+
currency: "usd",
|
|
161
|
+
amount: 30,
|
|
162
|
+
discountable: false,
|
|
163
|
+
metadata: { bsRecoveryGroup: f.group._id },
|
|
164
|
+
};
|
|
165
|
+
body = item;
|
|
166
|
+
} else body = { object: "list", data: item ? [item] : [], has_more: false };
|
|
167
|
+
return new Response(JSON.stringify(body), {
|
|
168
|
+
status: 200,
|
|
169
|
+
headers: { "content-type": "application/json" },
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
f.failNext();
|
|
173
|
+
expect(
|
|
174
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
175
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
176
|
+
httpClient,
|
|
177
|
+
}),
|
|
178
|
+
).toMatchObject({ status: "unknown" });
|
|
179
|
+
expect(creates).toBe(1);
|
|
180
|
+
status = "paid";
|
|
181
|
+
await f.dispatchReconcile();
|
|
182
|
+
expect(
|
|
183
|
+
await f.client.executeGroupProvider(
|
|
184
|
+
f.ctx,
|
|
185
|
+
{ ...f.request, phase: "reconcile" },
|
|
186
|
+
{ stripeSecretKey: "sk_test_runtime_only", httpClient },
|
|
187
|
+
),
|
|
188
|
+
).toMatchObject({
|
|
189
|
+
status: "ready",
|
|
190
|
+
outcome: "collect",
|
|
191
|
+
sourceId: "in_source",
|
|
192
|
+
});
|
|
193
|
+
expect(creates).toBe(1);
|
|
194
|
+
expect(
|
|
195
|
+
headers.every(
|
|
196
|
+
(h) => h.authorized && h.version === f.request.providerApiVersion,
|
|
197
|
+
),
|
|
198
|
+
).toBe(true);
|
|
199
|
+
expect(headers.find((h) => h.key)?.key).toBe(
|
|
200
|
+
`better-stripe:recovery:${f.group._id}`,
|
|
201
|
+
);
|
|
202
|
+
expect(
|
|
203
|
+
(await f.client.getGroup(f.ctx, { groupKey: scope.groupKey }))?.status,
|
|
204
|
+
).toBe("sealed");
|
|
205
|
+
});
|
|
206
|
+
it("recovers a successful transfer after mirror failure without another provider write", async () => {
|
|
207
|
+
const f = await setup("settlement");
|
|
208
|
+
let transfer: Record<string, unknown> | null = null;
|
|
209
|
+
let creates = 0;
|
|
210
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
211
|
+
let body: unknown;
|
|
212
|
+
if (String(url).includes("/charges/"))
|
|
213
|
+
body = { id: "ch_source", created: 1 };
|
|
214
|
+
else if (init?.method === "POST") {
|
|
215
|
+
creates++;
|
|
216
|
+
transfer = {
|
|
217
|
+
id: "tr_source",
|
|
218
|
+
destination: "acct_seller",
|
|
219
|
+
amount: 900,
|
|
220
|
+
currency: "usd",
|
|
221
|
+
source_transaction: "ch_source",
|
|
222
|
+
amount_reversed: 0,
|
|
223
|
+
metadata: { bsRole: "store", bsSourceCharge: "ch_source" },
|
|
224
|
+
};
|
|
225
|
+
body = transfer;
|
|
226
|
+
} else
|
|
227
|
+
body = {
|
|
228
|
+
object: "list",
|
|
229
|
+
data: transfer ? [transfer] : [],
|
|
230
|
+
has_more: false,
|
|
231
|
+
};
|
|
232
|
+
return new Response(JSON.stringify(body), {
|
|
233
|
+
status: 200,
|
|
234
|
+
headers: { "content-type": "application/json" },
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
f.failNext(2); // Reserve allocation succeeds; transfer mirror fails after provider success.
|
|
238
|
+
expect(
|
|
239
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
240
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
241
|
+
httpClient,
|
|
242
|
+
}),
|
|
243
|
+
).toMatchObject({ status: "unknown" });
|
|
244
|
+
expect(creates).toBe(1);
|
|
245
|
+
await f.dispatchReconcile();
|
|
246
|
+
expect(
|
|
247
|
+
await f.client.executeGroupProvider(
|
|
248
|
+
f.ctx,
|
|
249
|
+
{ ...f.request, phase: "reconcile" },
|
|
250
|
+
{ stripeSecretKey: "sk_test_runtime_only", httpClient },
|
|
251
|
+
),
|
|
252
|
+
).toMatchObject({ status: "ready", outcome: "collect" });
|
|
253
|
+
expect(creates).toBe(1);
|
|
254
|
+
expect(
|
|
255
|
+
await f.t.query(nested.betterStripe.connect.queries.listTransfersByCharge, {
|
|
256
|
+
sourceChargeId: "ch_source",
|
|
257
|
+
}),
|
|
258
|
+
).toMatchObject([{ stripeTransferId: "tr_source", amount: 900 }]);
|
|
259
|
+
});
|
|
260
|
+
it("holds missing settlement legs on reconciliation and never creates them", async () => {
|
|
261
|
+
const f = await setup("settlement");
|
|
262
|
+
let creates = 0;
|
|
263
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
264
|
+
if (init?.method === "POST") creates++;
|
|
265
|
+
const body = String(url).includes("/charges/")
|
|
266
|
+
? { id: "ch_source", created: 1 }
|
|
267
|
+
: { object: "list", data: [], has_more: false };
|
|
268
|
+
return new Response(JSON.stringify(body), {
|
|
269
|
+
status: 200,
|
|
270
|
+
headers: { "content-type": "application/json" },
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
await f.dispatchReconcile();
|
|
274
|
+
expect(
|
|
275
|
+
await f.client.executeGroupProvider(
|
|
276
|
+
f.ctx,
|
|
277
|
+
{ ...f.request, phase: "reconcile" },
|
|
278
|
+
{ stripeSecretKey: "sk_test_runtime_only", httpClient },
|
|
279
|
+
),
|
|
280
|
+
).toMatchObject({ status: "unknown" });
|
|
281
|
+
expect(creates).toBe(0);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("refuses provider work without an active persisted dispatch", async () => {
|
|
285
|
+
const f = await setup("invoice", false);
|
|
286
|
+
let calls = 0;
|
|
287
|
+
const httpClient = Stripe.createFetchHttpClient(async () => {
|
|
288
|
+
calls++;
|
|
289
|
+
return new Response("{}", { status: 200 });
|
|
290
|
+
});
|
|
291
|
+
expect(
|
|
292
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
293
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
294
|
+
httpClient,
|
|
295
|
+
}),
|
|
296
|
+
).toMatchObject({ status: "unknown" });
|
|
297
|
+
expect(calls).toBe(0);
|
|
298
|
+
});
|
|
299
|
+
it("holds incomplete invoice history instead of creating an item after the scan bound", async () => {
|
|
300
|
+
const f = await setup();
|
|
301
|
+
let creates = 0;
|
|
302
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
303
|
+
if (init?.method === "POST") creates++;
|
|
304
|
+
const body = String(url).includes("/invoices/")
|
|
305
|
+
? {
|
|
306
|
+
id: "in_source",
|
|
307
|
+
customer_account: scope.accountId,
|
|
308
|
+
currency: "usd",
|
|
309
|
+
status: "draft",
|
|
310
|
+
}
|
|
311
|
+
: {
|
|
312
|
+
object: "list",
|
|
313
|
+
data: Array.from({ length: 10001 }, (_, i) => ({
|
|
314
|
+
id: `ii_other_${i}`,
|
|
315
|
+
metadata: {},
|
|
316
|
+
})),
|
|
317
|
+
has_more: false,
|
|
318
|
+
};
|
|
319
|
+
return new Response(JSON.stringify(body), {
|
|
320
|
+
status: 200,
|
|
321
|
+
headers: { "content-type": "application/json" },
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
expect(
|
|
325
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
326
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
327
|
+
httpClient,
|
|
328
|
+
}),
|
|
329
|
+
).toMatchObject({ status: "unknown" });
|
|
330
|
+
expect(creates).toBe(0);
|
|
331
|
+
});
|
|
332
|
+
it("rejects a changed provider definition or API version before transport", async () => {
|
|
333
|
+
const f = await setup();
|
|
334
|
+
let calls = 0;
|
|
335
|
+
const httpClient = Stripe.createFetchHttpClient(async () => {
|
|
336
|
+
calls++;
|
|
337
|
+
return new Response("{}", { status: 200 });
|
|
338
|
+
});
|
|
339
|
+
for (const patch of [
|
|
340
|
+
{ providerApiVersion: "2026-01-01.changed" },
|
|
341
|
+
{ definitionVersion: 2 },
|
|
342
|
+
{ phase: "reconcile" as const },
|
|
343
|
+
]) {
|
|
344
|
+
expect(
|
|
345
|
+
await f.client.executeGroupProvider(
|
|
346
|
+
f.ctx,
|
|
347
|
+
{ ...f.request, ...patch },
|
|
348
|
+
{ stripeSecretKey: "sk_test_runtime_only", httpClient },
|
|
349
|
+
),
|
|
350
|
+
).toMatchObject({ status: "unknown" });
|
|
351
|
+
}
|
|
352
|
+
expect(calls).toBe(0);
|
|
353
|
+
});
|
|
354
|
+
it("accepts a terminal void invoice without attempting to create an item", async () => {
|
|
355
|
+
const f = await setup();
|
|
356
|
+
let creates = 0;
|
|
357
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
358
|
+
if (init?.method === "POST") creates++;
|
|
359
|
+
const body = String(url).includes("/invoices/")
|
|
360
|
+
? {
|
|
361
|
+
id: "in_source",
|
|
362
|
+
customer_account: scope.accountId,
|
|
363
|
+
currency: "usd",
|
|
364
|
+
status: "void",
|
|
365
|
+
}
|
|
366
|
+
: { object: "list", data: [], has_more: false };
|
|
367
|
+
return new Response(JSON.stringify(body), {
|
|
368
|
+
status: 200,
|
|
369
|
+
headers: { "content-type": "application/json" },
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
expect(
|
|
373
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
374
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
375
|
+
httpClient,
|
|
376
|
+
}),
|
|
377
|
+
).toMatchObject({ status: "ready", outcome: "release" });
|
|
378
|
+
expect(creates).toBe(0);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it.each([false, true])(
|
|
382
|
+
"pins explicit uncollectible release policy %s",
|
|
383
|
+
async (release) => {
|
|
384
|
+
const f = await setup("invoice", true, release);
|
|
385
|
+
const httpClient = Stripe.createFetchHttpClient(async (url) => {
|
|
386
|
+
const body = String(url).includes("/invoices/")
|
|
387
|
+
? {
|
|
388
|
+
id: "in_source",
|
|
389
|
+
customer_account: scope.accountId,
|
|
390
|
+
currency: "usd",
|
|
391
|
+
status: "uncollectible",
|
|
392
|
+
}
|
|
393
|
+
: {
|
|
394
|
+
object: "list",
|
|
395
|
+
data: [
|
|
396
|
+
{
|
|
397
|
+
id: "ii_recovery",
|
|
398
|
+
invoice: "in_source",
|
|
399
|
+
customer_account: scope.accountId,
|
|
400
|
+
currency: "usd",
|
|
401
|
+
amount: 30,
|
|
402
|
+
discountable: false,
|
|
403
|
+
metadata: { bsRecoveryGroup: f.group._id },
|
|
404
|
+
},
|
|
405
|
+
],
|
|
406
|
+
has_more: false,
|
|
407
|
+
};
|
|
408
|
+
return new Response(JSON.stringify(body), {
|
|
409
|
+
status: 200,
|
|
410
|
+
headers: { "content-type": "application/json" },
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
expect(
|
|
414
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
415
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
416
|
+
httpClient,
|
|
417
|
+
}),
|
|
418
|
+
).toMatchObject(
|
|
419
|
+
release
|
|
420
|
+
? { status: "ready", outcome: "release" }
|
|
421
|
+
: { status: "not_ready", reason: "provider_pending" },
|
|
422
|
+
);
|
|
423
|
+
},
|
|
424
|
+
);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
3
|
+
import { runMutationOrThrow } from "../helpers.js";
|
|
4
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
5
|
+
import { attachRecoveryInvoice } from "../costRecovery-invoices.js";
|
|
6
|
+
import type { RecoveryGroup } from "../costRecovery.js";
|
|
7
|
+
import { executeResolvedTransfers } from "../settlement/execution.js";
|
|
8
|
+
import { reconcileRecoverySettlement } from "./settlement.js";
|
|
9
|
+
import { operationRef } from "./contracts.js";
|
|
10
|
+
import type {
|
|
11
|
+
RecoveryProviderRequest,
|
|
12
|
+
RecoveryProviderReceipt,
|
|
13
|
+
RecoverySnapshot,
|
|
14
|
+
} from "./contracts.js";
|
|
15
|
+
export type RecoveryProviderOptions = {
|
|
16
|
+
/** Runtime-only secret, supplied inside the registered internal host action. */
|
|
17
|
+
stripeSecretKey: string;
|
|
18
|
+
httpClient?: NonNullable<
|
|
19
|
+
ConstructorParameters<typeof Stripe>[1]
|
|
20
|
+
>["httpClient"];
|
|
21
|
+
};
|
|
22
|
+
export async function executeRecoveryProvider(
|
|
23
|
+
component: Component,
|
|
24
|
+
ctx: RunCtx,
|
|
25
|
+
request: RecoveryProviderRequest,
|
|
26
|
+
options: RecoveryProviderOptions,
|
|
27
|
+
): Promise<RecoveryProviderReceipt> {
|
|
28
|
+
const unknown = {
|
|
29
|
+
...operationRef(request),
|
|
30
|
+
phase: request.phase,
|
|
31
|
+
providerApiVersion: request.providerApiVersion,
|
|
32
|
+
definitionVersion: request.definitionVersion,
|
|
33
|
+
sourceId: "",
|
|
34
|
+
status: "unknown",
|
|
35
|
+
reason: "provider_outcome_unknown",
|
|
36
|
+
} as const;
|
|
37
|
+
try {
|
|
38
|
+
const op = (await ctx.runQuery(
|
|
39
|
+
componentRef(component, "recoveryWorkflow/queries/snapshot"),
|
|
40
|
+
operationRef(request),
|
|
41
|
+
)) as RecoverySnapshot;
|
|
42
|
+
const base = {
|
|
43
|
+
...operationRef(request),
|
|
44
|
+
phase: request.phase,
|
|
45
|
+
providerApiVersion: request.providerApiVersion,
|
|
46
|
+
definitionVersion: request.definitionVersion,
|
|
47
|
+
sourceId: op.sourceId,
|
|
48
|
+
};
|
|
49
|
+
if (
|
|
50
|
+
op.status !== "running" ||
|
|
51
|
+
!op.providerStarted ||
|
|
52
|
+
op.activePhase !== request.phase ||
|
|
53
|
+
op.providerApiVersion !== request.providerApiVersion ||
|
|
54
|
+
op.definitionVersion !== request.definitionVersion ||
|
|
55
|
+
request.definitionVersion !== 1 ||
|
|
56
|
+
(request.phase !== "execute" && request.phase !== "reconcile")
|
|
57
|
+
)
|
|
58
|
+
return { ...base, status: "unknown", reason: "provider_outcome_unknown" };
|
|
59
|
+
const stripe = new Stripe(options.stripeSecretKey, {
|
|
60
|
+
apiVersion: request.providerApiVersion as Stripe.LatestApiVersion,
|
|
61
|
+
maxNetworkRetries: 0,
|
|
62
|
+
...(options.httpClient ? { httpClient: options.httpClient } : {}),
|
|
63
|
+
});
|
|
64
|
+
if (op.sourceKind === "settlement") {
|
|
65
|
+
if (!op.allocation)
|
|
66
|
+
return {
|
|
67
|
+
...base,
|
|
68
|
+
status: "unknown",
|
|
69
|
+
reason: "provider_outcome_unknown",
|
|
70
|
+
};
|
|
71
|
+
if (request.phase === "execute")
|
|
72
|
+
await executeResolvedTransfers(stripe, component, ctx, op.allocation);
|
|
73
|
+
else if (
|
|
74
|
+
!(await reconcileRecoverySettlement(
|
|
75
|
+
stripe,
|
|
76
|
+
component,
|
|
77
|
+
ctx,
|
|
78
|
+
op.allocation,
|
|
79
|
+
))
|
|
80
|
+
)
|
|
81
|
+
return {
|
|
82
|
+
...base,
|
|
83
|
+
status: "unknown",
|
|
84
|
+
reason: "provider_outcome_unknown",
|
|
85
|
+
};
|
|
86
|
+
return { ...base, status: "ready", outcome: "collect" };
|
|
87
|
+
}
|
|
88
|
+
const group = (await ctx.runQuery(
|
|
89
|
+
componentRef(component, "costRecovery/queries/group"),
|
|
90
|
+
{ groupKey: op.groupKey },
|
|
91
|
+
)) as RecoveryGroup | null;
|
|
92
|
+
const bind = (invoiceItemId: string) =>
|
|
93
|
+
runMutationOrThrow(
|
|
94
|
+
ctx,
|
|
95
|
+
componentRef(component, "costRecovery/mutations/bindInvoiceItem"),
|
|
96
|
+
{ groupKey: op.groupKey, invoiceItemId },
|
|
97
|
+
) as Promise<RecoveryGroup>;
|
|
98
|
+
const create =
|
|
99
|
+
request.phase === "execute" &&
|
|
100
|
+
(await stripe.invoices.retrieve(op.sourceId)).status === "draft";
|
|
101
|
+
const receipt = create
|
|
102
|
+
? await attachRecoveryInvoice(stripe, group, op, bind, true, 10000)
|
|
103
|
+
: await attachRecoveryInvoice(stripe, group, op, bind, false, 10000);
|
|
104
|
+
const outcome =
|
|
105
|
+
receipt.invoiceStatus === "paid"
|
|
106
|
+
? "collect"
|
|
107
|
+
: receipt.invoiceStatus === "void" ||
|
|
108
|
+
(receipt.invoiceStatus === "uncollectible" &&
|
|
109
|
+
op.releaseUncollectible)
|
|
110
|
+
? "release"
|
|
111
|
+
: null;
|
|
112
|
+
return outcome
|
|
113
|
+
? { ...base, status: "ready", outcome }
|
|
114
|
+
: { ...base, status: "not_ready", reason: "provider_pending" };
|
|
115
|
+
} catch {
|
|
116
|
+
// Returning a closed outcome also prevents the host action from logging raw SDK failures.
|
|
117
|
+
return unknown;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
3
|
+
import { runMutationOrThrow } from "../helpers.js";
|
|
4
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
5
|
+
import {
|
|
6
|
+
matchingTransfer,
|
|
7
|
+
sourceTransfers,
|
|
8
|
+
recordSettlementTransfer,
|
|
9
|
+
} from "../settlement/reconciliation.js";
|
|
10
|
+
import type { ResolvedSettlement } from "../settlement/execution.js";
|
|
11
|
+
/** Read-only provider recovery: missing legs never create a second money attempt. */
|
|
12
|
+
export async function reconcileRecoverySettlement(
|
|
13
|
+
stripe: Stripe,
|
|
14
|
+
component: Component,
|
|
15
|
+
ctx: RunCtx,
|
|
16
|
+
claim: ResolvedSettlement,
|
|
17
|
+
): Promise<boolean> {
|
|
18
|
+
const effective = [];
|
|
19
|
+
for (const leg of claim.transfers) {
|
|
20
|
+
const earning =
|
|
21
|
+
leg.role === "affiliate" && leg.amount > 0
|
|
22
|
+
? ((await ctx.runQuery(
|
|
23
|
+
componentRef(component, "affiliateRecovery/queries/getEarning"),
|
|
24
|
+
{
|
|
25
|
+
sourceChargeId: claim.sourceChargeId,
|
|
26
|
+
affiliateAccountId: leg.destinationAccountId,
|
|
27
|
+
},
|
|
28
|
+
)) as { _id: string; releaseAmount: number } | null)
|
|
29
|
+
: null;
|
|
30
|
+
if (leg.role === "affiliate" && leg.amount > 0 && !earning) return false;
|
|
31
|
+
effective.push({
|
|
32
|
+
leg: { ...leg, amount: earning?.releaseAmount ?? leg.amount },
|
|
33
|
+
earning,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
let provider = await sourceTransfers(stripe, claim.sourceChargeId);
|
|
37
|
+
if (
|
|
38
|
+
effective.some(
|
|
39
|
+
({ leg }) => leg.amount > 0 && !matchingTransfer(provider, claim, leg),
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
provider = [
|
|
43
|
+
...new Map(
|
|
44
|
+
[
|
|
45
|
+
...provider,
|
|
46
|
+
...(await sourceTransfers(stripe, claim.sourceChargeId, true)),
|
|
47
|
+
].map((t) => [t.id, t]),
|
|
48
|
+
).values(),
|
|
49
|
+
];
|
|
50
|
+
for (const { leg, earning } of effective) {
|
|
51
|
+
const transfer =
|
|
52
|
+
leg.amount > 0 ? matchingTransfer(provider, claim, leg) : undefined;
|
|
53
|
+
if (leg.amount > 0 && !transfer) return false;
|
|
54
|
+
if (transfer)
|
|
55
|
+
await recordSettlementTransfer(component, ctx, claim, leg, transfer);
|
|
56
|
+
if (earning)
|
|
57
|
+
await runMutationOrThrow(
|
|
58
|
+
ctx,
|
|
59
|
+
componentRef(component, "affiliateEarnings/mutations/confirmEarning"),
|
|
60
|
+
{
|
|
61
|
+
earningId: earning._id,
|
|
62
|
+
...(transfer ? { stripeTransferId: transfer.id } : {}),
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type Stripe from "stripe";
|
|
2
|
+
import { saleAmountFailure } from "../paymentSource/economics.js";
|
|
2
3
|
import { isBetterStripeError, throwStripeError } from "../errors.js";
|
|
3
4
|
export type VerifiedSaleOptions = {
|
|
4
5
|
paymentIntentId: string;
|
|
@@ -62,20 +63,13 @@ export async function getVerifiedSale(
|
|
|
62
63
|
reason: "processing_fee_unavailable",
|
|
63
64
|
retryable: true,
|
|
64
65
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
charge.currency !== payment.currency
|
|
68
|
-
)
|
|
66
|
+
const amountFailure = saleAmountFailure(payment, charge, balance);
|
|
67
|
+
if (amountFailure === "currency")
|
|
69
68
|
throwStripeError(
|
|
70
69
|
"INVALID_CONFIGURATION",
|
|
71
70
|
"Settlement currency must match the payment and processing currency.",
|
|
72
71
|
);
|
|
73
|
-
if (
|
|
74
|
-
![payment.amount_received, balance.fee].every(Number.isSafeInteger) ||
|
|
75
|
-
balance.fee < 0 ||
|
|
76
|
-
balance.fee > payment.amount_received ||
|
|
77
|
-
charge.amount !== payment.amount_received
|
|
78
|
-
)
|
|
72
|
+
if (amountFailure === "amounts")
|
|
79
73
|
throwStripeError(
|
|
80
74
|
"INVALID_CONFIGURATION",
|
|
81
75
|
"Unsupported payment or processing amounts.",
|
|
@@ -119,6 +119,12 @@ import type * as payoutWorker_queries from "../payoutWorker/queries.js";
|
|
|
119
119
|
import type * as products_mutations from "../products/mutations.js";
|
|
120
120
|
import type * as products_queries from "../products/queries.js";
|
|
121
121
|
import type * as products_validators from "../products/validators.js";
|
|
122
|
+
import type * as recoveryWorkflow_bridge from "../recoveryWorkflow/bridge.js";
|
|
123
|
+
import type * as recoveryWorkflow_helpers from "../recoveryWorkflow/helpers.js";
|
|
124
|
+
import type * as recoveryWorkflow_mutations from "../recoveryWorkflow/mutations.js";
|
|
125
|
+
import type * as recoveryWorkflow_queries from "../recoveryWorkflow/queries.js";
|
|
126
|
+
import type * as recoveryWorkflow_steps from "../recoveryWorkflow/steps.js";
|
|
127
|
+
import type * as recoveryWorkflow_workflow from "../recoveryWorkflow/workflow.js";
|
|
122
128
|
import type * as refundHistory_commandBinding from "../refundHistory/commandBinding.js";
|
|
123
129
|
import type * as refundHistory_commandEvidence from "../refundHistory/commandEvidence.js";
|
|
124
130
|
import type * as refundHistory_commandEvidenceValidator from "../refundHistory/commandEvidenceValidator.js";
|
|
@@ -253,6 +259,12 @@ const fullApi: ApiFromModules<{
|
|
|
253
259
|
"products/mutations": typeof products_mutations;
|
|
254
260
|
"products/queries": typeof products_queries;
|
|
255
261
|
"products/validators": typeof products_validators;
|
|
262
|
+
"recoveryWorkflow/bridge": typeof recoveryWorkflow_bridge;
|
|
263
|
+
"recoveryWorkflow/helpers": typeof recoveryWorkflow_helpers;
|
|
264
|
+
"recoveryWorkflow/mutations": typeof recoveryWorkflow_mutations;
|
|
265
|
+
"recoveryWorkflow/queries": typeof recoveryWorkflow_queries;
|
|
266
|
+
"recoveryWorkflow/steps": typeof recoveryWorkflow_steps;
|
|
267
|
+
"recoveryWorkflow/workflow": typeof recoveryWorkflow_workflow;
|
|
256
268
|
"refundHistory/commandBinding": typeof refundHistory_commandBinding;
|
|
257
269
|
"refundHistory/commandEvidence": typeof refundHistory_commandEvidence;
|
|
258
270
|
"refundHistory/commandEvidenceValidator": typeof refundHistory_commandEvidenceValidator;
|