@fantastic.dev/better-stripe 0.45.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/component/durableExecution/crossDomain.test.ts +241 -0
- package/src/component/durableExecution/crossDomainBoundaries.test.ts +233 -0
- package/src/component/durableExecution/crossDomainDisputes.test.ts +108 -0
- package/src/component/durableExecution/crossDomainMigration.test.ts +192 -0
- package/src/component/durableExecution/crossDomainWebhooks.test.ts +122 -0
- package/src/testing/crossDomain/fixture.ts +96 -0
- package/src/testing/crossDomain/host.ts +288 -0
- package/src/testing/crossDomain/transport.ts +293 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
3
|
+
import { expect, it, vi } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
fixture,
|
|
6
|
+
scope,
|
|
7
|
+
fundingScope,
|
|
8
|
+
wire,
|
|
9
|
+
} from "../../testing/crossDomain/fixture.js";
|
|
10
|
+
const nested = componentsGeneric().betterStripe;
|
|
11
|
+
it("preserves both legacy ledgers when new recovery collects an imported opening balance", async () => {
|
|
12
|
+
const t = fixture();
|
|
13
|
+
const fundingImport = {
|
|
14
|
+
accountId: "acct_a",
|
|
15
|
+
scopeKey: "funding:a",
|
|
16
|
+
sourceKey: "legacy:funding:original",
|
|
17
|
+
preparation: {
|
|
18
|
+
customerAccount: "acct_a",
|
|
19
|
+
stripeSubscriptionId: "sub_a",
|
|
20
|
+
paymentMethodId: "pm_balance_a",
|
|
21
|
+
previousPaymentMethodId: "pm_card_a",
|
|
22
|
+
previousPaymentMethodTypes: [],
|
|
23
|
+
},
|
|
24
|
+
consentAt: 123,
|
|
25
|
+
consentReference: "original-consent",
|
|
26
|
+
providerApiVersion: "2026-09-30.preview",
|
|
27
|
+
enabled: false,
|
|
28
|
+
operationVersion: 7,
|
|
29
|
+
pending: {
|
|
30
|
+
enabled: true,
|
|
31
|
+
providerKey: "original-legacy-apply-key",
|
|
32
|
+
requestKey: "original-legacy-request",
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const imported = await t.mutation(
|
|
36
|
+
nested.balanceFunding.mutations.importPreparation,
|
|
37
|
+
fundingImport,
|
|
38
|
+
);
|
|
39
|
+
const opening = {
|
|
40
|
+
...scope(),
|
|
41
|
+
claimKey: "legacy:cost",
|
|
42
|
+
sourceChargeId: "ch_legacy",
|
|
43
|
+
sourceGross: 1000,
|
|
44
|
+
sourceProcessingFee: 30,
|
|
45
|
+
amountDue: 100,
|
|
46
|
+
revision: 4,
|
|
47
|
+
operationKey: "legacy:collection",
|
|
48
|
+
collectedAmount: 25,
|
|
49
|
+
};
|
|
50
|
+
await t.mutation(nested.costRecovery.mutations.importClaim, opening);
|
|
51
|
+
await t.mutation(anyApi.host.prepareRecovery, {
|
|
52
|
+
name: "a",
|
|
53
|
+
kind: "settlement",
|
|
54
|
+
amount: 60,
|
|
55
|
+
});
|
|
56
|
+
const sale = await t.mutation(anyApi.host.startRecovery, {
|
|
57
|
+
name: "a",
|
|
58
|
+
kind: "settlement",
|
|
59
|
+
requestKey: "collect-openings",
|
|
60
|
+
});
|
|
61
|
+
expect(
|
|
62
|
+
await t.action(anyApi.host.funding, { requestKey: "must-not-reprepare" }),
|
|
63
|
+
).toMatchObject({ status: "conflict" });
|
|
64
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
65
|
+
expect(
|
|
66
|
+
await t.query(nested.recoveryWorkflow.queries.get, {
|
|
67
|
+
...scope(),
|
|
68
|
+
groupKey: "settlement:a",
|
|
69
|
+
operationId: sale.operationId,
|
|
70
|
+
version: sale.version,
|
|
71
|
+
}),
|
|
72
|
+
).toMatchObject({ status: "completed" });
|
|
73
|
+
expect(
|
|
74
|
+
await t.mutation(nested.costRecovery.mutations.importClaim, opening),
|
|
75
|
+
).toMatchObject({
|
|
76
|
+
collectedAmount: 100,
|
|
77
|
+
revision: 4,
|
|
78
|
+
outstanding: 0,
|
|
79
|
+
openingCollection: {
|
|
80
|
+
collectedAmount: 25,
|
|
81
|
+
operationKey: "legacy:collection",
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
expect(
|
|
85
|
+
await t.mutation(
|
|
86
|
+
nested.balanceFunding.mutations.importPreparation,
|
|
87
|
+
fundingImport,
|
|
88
|
+
),
|
|
89
|
+
).toEqual(imported);
|
|
90
|
+
const saved = await t.query(
|
|
91
|
+
nested.balanceFunding.queries.readScopePreparation,
|
|
92
|
+
{ ...fundingScope(), providerApiVersion: "2026-09-30.preview" },
|
|
93
|
+
);
|
|
94
|
+
expect(saved).toMatchObject({
|
|
95
|
+
operationVersion: 7,
|
|
96
|
+
startsPaused: true,
|
|
97
|
+
enabled: false,
|
|
98
|
+
preparation: { previousPaymentMethodTypes: [] },
|
|
99
|
+
});
|
|
100
|
+
expect(
|
|
101
|
+
await t.query(nested.balanceFunding.queries.inspectOperation, {
|
|
102
|
+
...imported.operation,
|
|
103
|
+
stripeSubscriptionId: "sub_a",
|
|
104
|
+
}),
|
|
105
|
+
).toMatchObject({
|
|
106
|
+
state: "unknown",
|
|
107
|
+
providerKeys: {
|
|
108
|
+
enable: "original-legacy-apply-key:enable",
|
|
109
|
+
disable: "original-legacy-apply-key:disable",
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
expect(
|
|
113
|
+
wire.calls.filter((r) => r.method === "POST").map((r) => r.path),
|
|
114
|
+
).toEqual(["/v1/transfers"]);
|
|
115
|
+
expect(wire.transfers[0].amount).toBe(765);
|
|
116
|
+
await expect(
|
|
117
|
+
t.mutation(nested.balanceFunding.mutations.importPreparation, {
|
|
118
|
+
...fundingImport,
|
|
119
|
+
preparation: {
|
|
120
|
+
...fundingImport.preparation,
|
|
121
|
+
previousPaymentMethodTypes: null,
|
|
122
|
+
},
|
|
123
|
+
}),
|
|
124
|
+
).rejects.toThrow();
|
|
125
|
+
});
|
|
126
|
+
it("allows only one racing refund revision to commit while funding completes independently", async () => {
|
|
127
|
+
const t = fixture();
|
|
128
|
+
wire.refunded = true;
|
|
129
|
+
const first = await t.mutation(anyApi.host.costs, {
|
|
130
|
+
requestKey: "refund-event-a",
|
|
131
|
+
expectedRevision: -1,
|
|
132
|
+
});
|
|
133
|
+
const competing = await t.mutation(anyApi.host.costs, {
|
|
134
|
+
requestKey: "refund-event-b",
|
|
135
|
+
expectedRevision: -1,
|
|
136
|
+
});
|
|
137
|
+
await t.action(anyApi.host.funding, { requestKey: "parallel-funding" });
|
|
138
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
139
|
+
const states = await Promise.all(
|
|
140
|
+
[first, competing].map((op) =>
|
|
141
|
+
t.query(nested.processingCostReconciliation.queries.get, {
|
|
142
|
+
...scope(),
|
|
143
|
+
operationId: op.operationId,
|
|
144
|
+
version: op.version,
|
|
145
|
+
}),
|
|
146
|
+
),
|
|
147
|
+
);
|
|
148
|
+
expect(states.map((s) => s.status).sort()).toEqual(["completed", "conflict"]);
|
|
149
|
+
expect(
|
|
150
|
+
await t.query(nested.costRecovery.queries.claim, {
|
|
151
|
+
claimKey: "cost:pi_source",
|
|
152
|
+
}),
|
|
153
|
+
).toMatchObject({ amountDue: 30, revision: 0 });
|
|
154
|
+
expect(
|
|
155
|
+
(await t.run((ctx) => ctx.db.query("reports").take(10))).filter(
|
|
156
|
+
(r) => r.domain === "cost",
|
|
157
|
+
),
|
|
158
|
+
).toHaveLength(1);
|
|
159
|
+
expect(
|
|
160
|
+
(
|
|
161
|
+
await t.query(nested.balanceFunding.queries.readScopePreparation, {
|
|
162
|
+
...fundingScope(),
|
|
163
|
+
providerApiVersion: "2026-09-30.preview",
|
|
164
|
+
})
|
|
165
|
+
).verified,
|
|
166
|
+
).toBe(true);
|
|
167
|
+
const requests = wire.calls.length;
|
|
168
|
+
await expect(
|
|
169
|
+
t.query(nested.processingCostReconciliation.queries.get, {
|
|
170
|
+
...scope("b"),
|
|
171
|
+
operationId: first.operationId,
|
|
172
|
+
version: first.version,
|
|
173
|
+
}),
|
|
174
|
+
).rejects.toThrow();
|
|
175
|
+
await expect(
|
|
176
|
+
t.query(nested.processingCostReconciliation.queries.get, {
|
|
177
|
+
...scope(),
|
|
178
|
+
currency: "eur",
|
|
179
|
+
operationId: first.operationId,
|
|
180
|
+
version: first.version,
|
|
181
|
+
}),
|
|
182
|
+
).rejects.toThrow();
|
|
183
|
+
expect(wire.calls).toHaveLength(requests);
|
|
184
|
+
expect(
|
|
185
|
+
wire.calls.some(
|
|
186
|
+
(r) =>
|
|
187
|
+
r.method === "POST" &&
|
|
188
|
+
r.path !== "/v1/payment_methods" &&
|
|
189
|
+
!r.path.endsWith("/attach"),
|
|
190
|
+
),
|
|
191
|
+
).toBe(false);
|
|
192
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
3
|
+
import Stripe from "stripe";
|
|
4
|
+
import { expect, it, vi } from "vitest";
|
|
5
|
+
import { handleWebhookRequest } from "../../client/webhooks/handler.js";
|
|
6
|
+
import type { Component } from "../../client/helpers.js";
|
|
7
|
+
import type { RegisterRoutesConfig } from "../../client/types.js";
|
|
8
|
+
import {
|
|
9
|
+
fixture,
|
|
10
|
+
fundingScope,
|
|
11
|
+
scope,
|
|
12
|
+
wire,
|
|
13
|
+
} from "../../testing/crossDomain/fixture.js";
|
|
14
|
+
import { ga, secret } from "../../testing/crossDomain/transport.js";
|
|
15
|
+
const nested = componentsGeneric().betterStripe;
|
|
16
|
+
it("retries a signed invoice hook and deduplicates delivery without restarting settlement or funding", async () => {
|
|
17
|
+
const t = fixture();
|
|
18
|
+
await t.mutation(anyApi.host.prepareRecovery, {
|
|
19
|
+
name: "a",
|
|
20
|
+
kind: "settlement",
|
|
21
|
+
amount: 60,
|
|
22
|
+
});
|
|
23
|
+
await t.action(anyApi.host.funding, { requestKey: "parallel-funding" });
|
|
24
|
+
const event = {
|
|
25
|
+
id: "evt_cross_domain_invoice",
|
|
26
|
+
object: "event",
|
|
27
|
+
type: "invoice.paid",
|
|
28
|
+
api_version: ga,
|
|
29
|
+
livemode: false,
|
|
30
|
+
data: {
|
|
31
|
+
object: {
|
|
32
|
+
id: "in_original",
|
|
33
|
+
object: "invoice",
|
|
34
|
+
status: "paid",
|
|
35
|
+
currency: "usd",
|
|
36
|
+
amount_due: 1000,
|
|
37
|
+
amount_paid: 1000,
|
|
38
|
+
metadata: {},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const starts: { operationId: string; version: number }[] = [];
|
|
43
|
+
const config: RegisterRoutesConfig = {
|
|
44
|
+
stripeSecretKey: secret,
|
|
45
|
+
stripeApiVersion: ga,
|
|
46
|
+
webhookSecret: "whsec_cross_domain_signature_only",
|
|
47
|
+
retryOnHookError: () => true,
|
|
48
|
+
events: {
|
|
49
|
+
"invoice.paid": async (ctx, verified) => {
|
|
50
|
+
const op = await ctx.runMutation(anyApi.host.startRecovery, {
|
|
51
|
+
name: "a",
|
|
52
|
+
kind: "settlement",
|
|
53
|
+
requestKey: verified.id,
|
|
54
|
+
});
|
|
55
|
+
starts.push(op);
|
|
56
|
+
if (starts.length === 1)
|
|
57
|
+
throw new Error("Fixture host response interrupted after scheduling");
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
const deliver = async (tamper = false) => {
|
|
62
|
+
const body = JSON.stringify(event);
|
|
63
|
+
const signature = await Stripe.webhooks.generateTestHeaderStringAsync({
|
|
64
|
+
payload: body,
|
|
65
|
+
secret: config.webhookSecret!,
|
|
66
|
+
});
|
|
67
|
+
return t.action(async (ctx) => {
|
|
68
|
+
const response = await handleWebhookRequest(
|
|
69
|
+
ctx,
|
|
70
|
+
new Request("https://example.test/stripe/webhook", {
|
|
71
|
+
method: "POST",
|
|
72
|
+
body: tamper ? body.replace("in_original", "in_tampered") : body,
|
|
73
|
+
headers: { "stripe-signature": signature },
|
|
74
|
+
}),
|
|
75
|
+
nested as unknown as Component,
|
|
76
|
+
config,
|
|
77
|
+
);
|
|
78
|
+
return { status: response.status, body: await response.json() };
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const ledger = () =>
|
|
82
|
+
t.query(nested.webhooks.queries.getWebhookEvent, {
|
|
83
|
+
stripeEventId: event.id,
|
|
84
|
+
});
|
|
85
|
+
expect((await deliver(true)).status).toBe(400);
|
|
86
|
+
expect(await ledger()).toBeNull();
|
|
87
|
+
expect(starts).toHaveLength(0);
|
|
88
|
+
expect((await deliver()).status).toBe(500);
|
|
89
|
+
expect(await ledger()).toMatchObject({ status: "failed" });
|
|
90
|
+
expect((await deliver()).status).toBe(200);
|
|
91
|
+
expect(await deliver()).toMatchObject({
|
|
92
|
+
status: 200,
|
|
93
|
+
body: { deduplicated: true },
|
|
94
|
+
});
|
|
95
|
+
expect(starts).toHaveLength(2);
|
|
96
|
+
expect(starts[1].operationId).toBe(starts[0].operationId);
|
|
97
|
+
expect(await ledger()).toMatchObject({ status: "processed" });
|
|
98
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
99
|
+
expect(
|
|
100
|
+
await t.query(nested.recoveryWorkflow.queries.get, {
|
|
101
|
+
...scope(),
|
|
102
|
+
groupKey: "settlement:a",
|
|
103
|
+
operationId: starts[0].operationId,
|
|
104
|
+
version: starts[0].version,
|
|
105
|
+
}),
|
|
106
|
+
).toMatchObject({ status: "completed" });
|
|
107
|
+
expect(
|
|
108
|
+
await t.query(nested.balanceFunding.queries.readScopePreparation, {
|
|
109
|
+
...fundingScope(),
|
|
110
|
+
providerApiVersion: "2026-09-30.preview",
|
|
111
|
+
}),
|
|
112
|
+
).toMatchObject({
|
|
113
|
+
verified: true,
|
|
114
|
+
preparation: { previousPaymentMethodId: "pm_card_a" },
|
|
115
|
+
});
|
|
116
|
+
expect(
|
|
117
|
+
wire.calls.filter((r) => r.method === "POST" && r.path === "/v1/transfers"),
|
|
118
|
+
).toHaveLength(1);
|
|
119
|
+
expect(await t.run((ctx) => ctx.db.query("reports").take(10))).toHaveLength(
|
|
120
|
+
1,
|
|
121
|
+
);
|
|
122
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// One real BetterStripe component and its nested runtime; only provider HTTP responses are synthetic.
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import { convexTest } from "convex-test";
|
|
4
|
+
import {
|
|
5
|
+
defineSchema,
|
|
6
|
+
defineTable,
|
|
7
|
+
internalMutationGeneric,
|
|
8
|
+
internalQueryGeneric,
|
|
9
|
+
} from "convex/server";
|
|
10
|
+
import type { WorkflowId } from "@convex-dev/workflow";
|
|
11
|
+
import { v } from "convex/values";
|
|
12
|
+
import { afterEach, beforeEach, vi } from "vitest";
|
|
13
|
+
import { schema, modules } from "../../test.js";
|
|
14
|
+
import { getStripeClient } from "../../client/helpers.js";
|
|
15
|
+
import { executionWorkflow } from "../../component/durableExecution/runtime.js";
|
|
16
|
+
import { host } from "./host.js";
|
|
17
|
+
import { reset, httpClient, secret, preview } from "./transport.js";
|
|
18
|
+
export { scope, fundingScope } from "./host.js";
|
|
19
|
+
export { wire } from "./transport.js";
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.useFakeTimers();
|
|
22
|
+
reset();
|
|
23
|
+
// Funding has no transport option; configure only the test-key SDK cache entry.
|
|
24
|
+
const stripe = getStripeClient(secret, preview);
|
|
25
|
+
const set = stripe as unknown as {
|
|
26
|
+
_setApiField: (key: string, value: unknown) => void;
|
|
27
|
+
};
|
|
28
|
+
set._setApiField("httpClient", httpClient);
|
|
29
|
+
set._setApiField("maxNetworkRetries", 0);
|
|
30
|
+
});
|
|
31
|
+
afterEach(() => vi.useRealTimers());
|
|
32
|
+
export function fixture() {
|
|
33
|
+
const t = convexTest(
|
|
34
|
+
defineSchema({
|
|
35
|
+
reports: defineTable({
|
|
36
|
+
domain: v.string(),
|
|
37
|
+
operationId: v.string(),
|
|
38
|
+
version: v.number(),
|
|
39
|
+
amount: v.number(),
|
|
40
|
+
}).index("by_operation", ["operationId"]),
|
|
41
|
+
}),
|
|
42
|
+
{
|
|
43
|
+
"./_generated/api.ts": async () => ({}),
|
|
44
|
+
"./host.ts": async () => host,
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
t.registerComponent("betterStripe", schema, {
|
|
48
|
+
...modules,
|
|
49
|
+
"./component/crossDomainFixture.ts": async () => ({
|
|
50
|
+
cleanFundingHistory: internalMutationGeneric({
|
|
51
|
+
args: { operationId: v.id("balanceFundingOperations") },
|
|
52
|
+
returns: v.any(),
|
|
53
|
+
handler: async (ctx, args) => {
|
|
54
|
+
const op = await ctx.db.get(
|
|
55
|
+
"balanceFundingOperations",
|
|
56
|
+
args.operationId,
|
|
57
|
+
);
|
|
58
|
+
if (!op?.execution?.workflowId)
|
|
59
|
+
throw new Error("Missing test workflow");
|
|
60
|
+
return executionWorkflow.cleanup(
|
|
61
|
+
ctx,
|
|
62
|
+
op.execution.workflowId as WorkflowId,
|
|
63
|
+
);
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
journals: internalQueryGeneric({
|
|
67
|
+
args: {},
|
|
68
|
+
returns: v.any(),
|
|
69
|
+
handler: async (ctx) => {
|
|
70
|
+
const ids: string[] = [];
|
|
71
|
+
for (const op of await ctx.db
|
|
72
|
+
.query("balanceFundingOperations")
|
|
73
|
+
.take(20))
|
|
74
|
+
if (op.execution?.workflowId) ids.push(op.execution.workflowId);
|
|
75
|
+
for (const op of await ctx.db
|
|
76
|
+
.query("recoveryWorkflowOperations")
|
|
77
|
+
.take(20))
|
|
78
|
+
if (op.workflowId) ids.push(op.workflowId);
|
|
79
|
+
for (const op of await ctx.db
|
|
80
|
+
.query("processingCostReconciliationOperations")
|
|
81
|
+
.take(20))
|
|
82
|
+
if (op.workflowId) ids.push(op.workflowId);
|
|
83
|
+
return Promise.all(
|
|
84
|
+
ids.map((id) =>
|
|
85
|
+
executionWorkflow.listSteps(ctx, id as WorkflowId, {
|
|
86
|
+
paginationOpts: { cursor: null, numItems: 100 },
|
|
87
|
+
}),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
95
|
+
return t;
|
|
96
|
+
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {
|
|
2
|
+
anyApi,
|
|
3
|
+
componentsGeneric,
|
|
4
|
+
internalActionGeneric as action,
|
|
5
|
+
internalMutationGeneric as mutation,
|
|
6
|
+
} from "convex/server";
|
|
7
|
+
import { v } from "convex/values";
|
|
8
|
+
import { BetterStripe } from "../../client/index.js";
|
|
9
|
+
import type { Component } from "../../client/helpers.js";
|
|
10
|
+
import { scopeFields } from "../../component/costRecovery/schema.js";
|
|
11
|
+
import { httpClient, secret, ga, preview, wire } from "./transport.js";
|
|
12
|
+
const nested = componentsGeneric().betterStripe;
|
|
13
|
+
const sdk = () =>
|
|
14
|
+
new BetterStripe(nested as unknown as Component, {
|
|
15
|
+
STRIPE_SECRET_KEY: secret,
|
|
16
|
+
stripeApiVersion: preview,
|
|
17
|
+
});
|
|
18
|
+
const recovery = () => sdk().costRecovery;
|
|
19
|
+
export const scope = (name = "a") => ({
|
|
20
|
+
accountId: `acct_${name}`,
|
|
21
|
+
scopeKey: `cost:${name}`,
|
|
22
|
+
currency: "usd",
|
|
23
|
+
});
|
|
24
|
+
export const fundingScope = (name = "a") => ({
|
|
25
|
+
accountId: `acct_${name}`,
|
|
26
|
+
scopeKey: `funding:${name}`,
|
|
27
|
+
stripeSubscriptionId: `sub_${name}`,
|
|
28
|
+
});
|
|
29
|
+
const ref = { ...scopeFields, operationId: v.string(), version: v.number() };
|
|
30
|
+
const groupRef = { ...ref, groupKey: v.string() };
|
|
31
|
+
const phase = {
|
|
32
|
+
accountId: v.string(),
|
|
33
|
+
scopeKey: v.string(),
|
|
34
|
+
operationId: v.string(),
|
|
35
|
+
version: v.number(),
|
|
36
|
+
stripeSubscriptionId: v.string(),
|
|
37
|
+
providerApiVersion: v.string(),
|
|
38
|
+
definitionVersion: v.string(),
|
|
39
|
+
phase: v.union(v.literal("create"), v.literal("attach"), v.literal("apply")),
|
|
40
|
+
providerKey: v.string(),
|
|
41
|
+
};
|
|
42
|
+
const groupArgs = {
|
|
43
|
+
name: v.string(),
|
|
44
|
+
kind: v.union(v.literal("invoice"), v.literal("settlement")),
|
|
45
|
+
};
|
|
46
|
+
export const host = {
|
|
47
|
+
fundingControl: action({
|
|
48
|
+
args: {
|
|
49
|
+
operationId: v.string(),
|
|
50
|
+
version: v.number(),
|
|
51
|
+
command: v.union(v.literal("resume"), v.literal("cancel")),
|
|
52
|
+
},
|
|
53
|
+
returns: v.any(),
|
|
54
|
+
handler: (ctx, { command, ...op }) =>
|
|
55
|
+
sdk().balanceFundingWorkflow[command](ctx, { ...fundingScope(), ...op }),
|
|
56
|
+
}),
|
|
57
|
+
funding: action({
|
|
58
|
+
args: { requestKey: v.string() },
|
|
59
|
+
returns: v.any(),
|
|
60
|
+
handler: (ctx, args) =>
|
|
61
|
+
sdk().balanceFundingWorkflow.prepare(ctx, {
|
|
62
|
+
...fundingScope(),
|
|
63
|
+
...args,
|
|
64
|
+
consent: true,
|
|
65
|
+
consentAt: 123,
|
|
66
|
+
consentReference: "original-consent",
|
|
67
|
+
providerAction: anyApi.host.fundingProvider,
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
apply: action({
|
|
71
|
+
args: {
|
|
72
|
+
preparationId: v.string(),
|
|
73
|
+
enabled: v.boolean(),
|
|
74
|
+
requestKey: v.string(),
|
|
75
|
+
},
|
|
76
|
+
returns: v.any(),
|
|
77
|
+
handler: (ctx, args) =>
|
|
78
|
+
sdk().balanceFundingWorkflow.apply(ctx, {
|
|
79
|
+
...fundingScope(),
|
|
80
|
+
...args,
|
|
81
|
+
...(args.enabled ? { consent: true as const } : {}),
|
|
82
|
+
providerAction: anyApi.host.fundingProvider,
|
|
83
|
+
}),
|
|
84
|
+
}),
|
|
85
|
+
fundingProvider: action({
|
|
86
|
+
args: phase,
|
|
87
|
+
returns: v.any(),
|
|
88
|
+
handler: (ctx, args) =>
|
|
89
|
+
sdk().balanceFundingWorkflow.executeProviderPhase(ctx, args),
|
|
90
|
+
}),
|
|
91
|
+
prepareRecovery: mutation({
|
|
92
|
+
args: { ...groupArgs, amount: v.number() },
|
|
93
|
+
returns: v.any(),
|
|
94
|
+
handler: async (ctx, args) => {
|
|
95
|
+
const s = scope(args.name);
|
|
96
|
+
await recovery().recordClaim(ctx, {
|
|
97
|
+
...s,
|
|
98
|
+
claimKey: `debt:${args.name}`,
|
|
99
|
+
sourceChargeId: `ch_debt_${args.name}`,
|
|
100
|
+
sourceGross: 1000,
|
|
101
|
+
sourceProcessingFee: 30,
|
|
102
|
+
amountDue: args.amount,
|
|
103
|
+
revision: 0,
|
|
104
|
+
});
|
|
105
|
+
return recovery().prepareGroup(ctx, {
|
|
106
|
+
...s,
|
|
107
|
+
groupKey: `${args.kind}:${args.name}`,
|
|
108
|
+
...(args.kind === "settlement"
|
|
109
|
+
? {
|
|
110
|
+
sourceKind: "settlement" as const,
|
|
111
|
+
sourceId: "ch_source",
|
|
112
|
+
sourceGross: 1000,
|
|
113
|
+
sourceProcessingFee: 30,
|
|
114
|
+
}
|
|
115
|
+
: { sourceKind: "invoice" as const, sourceId: `in_${args.name}` }),
|
|
116
|
+
maxDebit: 1000,
|
|
117
|
+
maxCredit: 1000,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
121
|
+
startRecovery: mutation({
|
|
122
|
+
args: {
|
|
123
|
+
...groupArgs,
|
|
124
|
+
requestKey: v.string(),
|
|
125
|
+
affiliate: v.optional(v.boolean()),
|
|
126
|
+
},
|
|
127
|
+
returns: v.any(),
|
|
128
|
+
handler: async (ctx, args) => {
|
|
129
|
+
const s = scope(args.name),
|
|
130
|
+
groupKey = `${args.kind}:${args.name}`;
|
|
131
|
+
const group = await recovery().getGroup(ctx, { groupKey });
|
|
132
|
+
return recovery().startGroup(ctx, {
|
|
133
|
+
...s,
|
|
134
|
+
groupKey,
|
|
135
|
+
requestKey: args.requestKey,
|
|
136
|
+
provider: anyApi.host.recoveryProvider,
|
|
137
|
+
completion: anyApi.host.recoveryProjection,
|
|
138
|
+
providerApiVersion: ga,
|
|
139
|
+
definitionVersion: 1,
|
|
140
|
+
releaseUncollectible: false,
|
|
141
|
+
maxAttempts: 2,
|
|
142
|
+
retryDelayMs: 1,
|
|
143
|
+
...(args.kind === "settlement"
|
|
144
|
+
? {
|
|
145
|
+
allocation: {
|
|
146
|
+
sourceChargeId: "ch_source",
|
|
147
|
+
paymentIntentId: "pi_source",
|
|
148
|
+
sourceInvoiceId: "in_original",
|
|
149
|
+
gross: 1000,
|
|
150
|
+
currency: "usd",
|
|
151
|
+
processingFee: 30,
|
|
152
|
+
platformNet: 70,
|
|
153
|
+
adjustmentAmount: group!.amount,
|
|
154
|
+
transfers: [
|
|
155
|
+
{
|
|
156
|
+
destinationAccountId: s.accountId,
|
|
157
|
+
role: "store" as const,
|
|
158
|
+
amount: 900 - group!.amount - (args.affiliate ? 100 : 0),
|
|
159
|
+
},
|
|
160
|
+
...(args.affiliate
|
|
161
|
+
? [
|
|
162
|
+
{
|
|
163
|
+
destinationAccountId: "acct_affiliate",
|
|
164
|
+
role: "affiliate" as const,
|
|
165
|
+
amount: 100,
|
|
166
|
+
},
|
|
167
|
+
]
|
|
168
|
+
: []),
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
: {}),
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
}),
|
|
176
|
+
recoveryProvider: action({
|
|
177
|
+
args: {
|
|
178
|
+
...groupRef,
|
|
179
|
+
phase: v.union(v.literal("execute"), v.literal("reconcile")),
|
|
180
|
+
providerApiVersion: v.string(),
|
|
181
|
+
definitionVersion: v.number(),
|
|
182
|
+
},
|
|
183
|
+
returns: v.any(),
|
|
184
|
+
handler: (ctx, args) =>
|
|
185
|
+
recovery().executeGroupProvider(ctx, args, {
|
|
186
|
+
stripeSecretKey: secret,
|
|
187
|
+
httpClient,
|
|
188
|
+
}),
|
|
189
|
+
}),
|
|
190
|
+
recoveryProjection: mutation({
|
|
191
|
+
args: {
|
|
192
|
+
...groupRef,
|
|
193
|
+
sourceKind: v.string(),
|
|
194
|
+
sourceId: v.string(),
|
|
195
|
+
status: v.string(),
|
|
196
|
+
cancelRequested: v.boolean(),
|
|
197
|
+
outcome: v.optional(v.string()),
|
|
198
|
+
},
|
|
199
|
+
returns: v.null(),
|
|
200
|
+
handler: async (ctx, args) => {
|
|
201
|
+
const prior = await ctx.db
|
|
202
|
+
.query("reports")
|
|
203
|
+
.withIndex("by_operation", (q) => q.eq("operationId", args.operationId))
|
|
204
|
+
.unique();
|
|
205
|
+
if (!prior)
|
|
206
|
+
await ctx.db.insert("reports", {
|
|
207
|
+
domain: "recovery",
|
|
208
|
+
operationId: args.operationId,
|
|
209
|
+
version: args.version,
|
|
210
|
+
amount: 0,
|
|
211
|
+
});
|
|
212
|
+
if (wire.projectionFailures > 0) {
|
|
213
|
+
wire.projectionFailures--;
|
|
214
|
+
throw new Error("private database projection");
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
},
|
|
218
|
+
}),
|
|
219
|
+
costs: mutation({
|
|
220
|
+
args: {
|
|
221
|
+
requestKey: v.string(),
|
|
222
|
+
expectedRevision: v.number(),
|
|
223
|
+
rollback: v.optional(
|
|
224
|
+
v.object({ groupKey: v.string(), expectedRevision: v.number() }),
|
|
225
|
+
),
|
|
226
|
+
},
|
|
227
|
+
returns: v.any(),
|
|
228
|
+
handler: (ctx, args) =>
|
|
229
|
+
recovery().startProcessingCostReconciliation(ctx, {
|
|
230
|
+
...scope(),
|
|
231
|
+
...args,
|
|
232
|
+
claimKey: "cost:pi_source",
|
|
233
|
+
source: {
|
|
234
|
+
kind: "sale",
|
|
235
|
+
paymentIntentId: "pi_source",
|
|
236
|
+
gross: 1000,
|
|
237
|
+
customerAccountId: "acct_buyer",
|
|
238
|
+
invoiceId: "in_original",
|
|
239
|
+
},
|
|
240
|
+
policy: {
|
|
241
|
+
sellingFee: 100,
|
|
242
|
+
liability: { processingCosts: "uncovered", disputeCosts: "all" },
|
|
243
|
+
rounding: "nearest",
|
|
244
|
+
},
|
|
245
|
+
provider: anyApi.host.costProvider,
|
|
246
|
+
projection: anyApi.host.costProjection,
|
|
247
|
+
providerApiVersion: ga,
|
|
248
|
+
definitionVersion: 1,
|
|
249
|
+
maxAttempts: 2,
|
|
250
|
+
retryDelayMs: 1,
|
|
251
|
+
}),
|
|
252
|
+
}),
|
|
253
|
+
costProvider: action({
|
|
254
|
+
args: {
|
|
255
|
+
...ref,
|
|
256
|
+
providerApiVersion: v.string(),
|
|
257
|
+
definitionVersion: v.number(),
|
|
258
|
+
},
|
|
259
|
+
returns: v.any(),
|
|
260
|
+
handler: (ctx, args) =>
|
|
261
|
+
recovery().observeProcessingCosts(ctx, args, {
|
|
262
|
+
stripeSecretKey: secret,
|
|
263
|
+
httpClient,
|
|
264
|
+
}),
|
|
265
|
+
}),
|
|
266
|
+
costProjection: mutation({
|
|
267
|
+
args: { ...ref, status: v.literal("completed"), result: v.any() },
|
|
268
|
+
returns: v.null(),
|
|
269
|
+
handler: async (ctx, args) => {
|
|
270
|
+
const prior = await ctx.db
|
|
271
|
+
.query("reports")
|
|
272
|
+
.withIndex("by_operation", (q) => q.eq("operationId", args.operationId))
|
|
273
|
+
.unique();
|
|
274
|
+
if (!prior)
|
|
275
|
+
await ctx.db.insert("reports", {
|
|
276
|
+
domain: "cost",
|
|
277
|
+
operationId: args.operationId,
|
|
278
|
+
version: args.version,
|
|
279
|
+
amount: args.result.claim.amountDue,
|
|
280
|
+
});
|
|
281
|
+
if (wire.projectionFailures > 0) {
|
|
282
|
+
wire.projectionFailures--;
|
|
283
|
+
throw new Error("private cost projection");
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
},
|
|
287
|
+
}),
|
|
288
|
+
};
|