@fantastic.dev/better-stripe 0.64.0 → 0.65.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 +16 -0
- package/README.md +3 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/recoveryWorkflow/provider.d.ts +3 -0
- package/dist/client/recoveryWorkflow/provider.d.ts.map +1 -1
- package/dist/client/recoveryWorkflow/provider.js +1 -1
- package/dist/client/recoveryWorkflow/provider.js.map +1 -1
- package/dist/client/settlement/authorization.d.ts +19 -0
- package/dist/client/settlement/authorization.d.ts.map +1 -0
- package/dist/client/settlement/authorization.fixture.d.ts +112 -0
- package/dist/client/settlement/authorization.fixture.d.ts.map +1 -0
- package/dist/client/settlement/authorization.fixture.js +178 -0
- package/dist/client/settlement/authorization.fixture.js.map +1 -0
- package/dist/client/settlement/authorization.js +28 -0
- package/dist/client/settlement/authorization.js.map +1 -0
- package/dist/client/settlement/execution.d.ts +2 -1
- package/dist/client/settlement/execution.d.ts.map +1 -1
- package/dist/client/settlement/execution.js +8 -4
- package/dist/client/settlement/execution.js.map +1 -1
- package/dist/client/settlement/settleSale.d.ts +3 -0
- package/dist/client/settlement/settleSale.d.ts.map +1 -1
- package/dist/client/settlement/settleSale.js +5 -2
- package/dist/client/settlement/settleSale.js.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/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/balanceFunding/queries.d.ts +1 -1
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/docs/README.md +2 -0
- package/docs/settlement.md +56 -0
- package/docs/usage-verification.md +117 -0
- package/package.json +3 -2
- package/src/client/index.ts +4 -0
- package/src/client/recoveryWorkflow/provider.test.ts +149 -7
- package/src/client/recoveryWorkflow/provider.ts +10 -1
- package/src/client/settlement/authorization.fixture.ts +199 -0
- package/src/client/settlement/authorization.test.ts +160 -0
- package/src/client/settlement/authorization.ts +53 -0
- package/src/client/settlement/execution.ts +11 -4
- package/src/client/settlement/settleSale.ts +16 -2
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { BetterStripe } from "../index.js";
|
|
2
|
+
import { getStripeClient } from "../helpers.js";
|
|
3
|
+
import { components, initConvexTest } from "../setup.test.js";
|
|
4
|
+
import schema from "../../component/schema.js";
|
|
5
|
+
import { vi } from "vitest";
|
|
6
|
+
import type { SettleSaleOptions } from "./settleSale.js";
|
|
7
|
+
const modules = import.meta.glob("../../component/**/*.ts");
|
|
8
|
+
|
|
9
|
+
export async function authorizationFixture(twoLegs = false) {
|
|
10
|
+
const t = initConvexTest();
|
|
11
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
12
|
+
const key = `sk_test_qa_settlement_${crypto.randomUUID()}`;
|
|
13
|
+
const sdk = new BetterStripe(components.betterStripe, {
|
|
14
|
+
STRIPE_SECRET_KEY: key,
|
|
15
|
+
});
|
|
16
|
+
const transfers: Record<string, unknown>[] = [];
|
|
17
|
+
const requests: { method: string; path: string }[] = [];
|
|
18
|
+
let authorized = true;
|
|
19
|
+
let stopAfterFirst = false;
|
|
20
|
+
let failLedger = false;
|
|
21
|
+
let pause: "payment" | "history" | undefined;
|
|
22
|
+
let reached = () => {};
|
|
23
|
+
let release = () => {};
|
|
24
|
+
const reachedRead = new Promise<void>((resolve) => {
|
|
25
|
+
reached = resolve;
|
|
26
|
+
});
|
|
27
|
+
const released = new Promise<void>((resolve) => {
|
|
28
|
+
release = resolve;
|
|
29
|
+
});
|
|
30
|
+
const options: SettleSaleOptions = {
|
|
31
|
+
paymentIntentId: "pi_authorize",
|
|
32
|
+
allocation: {
|
|
33
|
+
gross: 1000,
|
|
34
|
+
currency: "usd",
|
|
35
|
+
processingFee: 60,
|
|
36
|
+
platformNet: 0,
|
|
37
|
+
transfers: twoLegs
|
|
38
|
+
? [
|
|
39
|
+
{
|
|
40
|
+
destinationAccountId: "acct_creator",
|
|
41
|
+
role: "store",
|
|
42
|
+
amount: 840,
|
|
43
|
+
},
|
|
44
|
+
{ destinationAccountId: "acct_other", role: "other", amount: 100 },
|
|
45
|
+
]
|
|
46
|
+
: [
|
|
47
|
+
{
|
|
48
|
+
destinationAccountId: "acct_creator",
|
|
49
|
+
role: "store",
|
|
50
|
+
amount: 940,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
vi.spyOn(
|
|
56
|
+
getStripeClient(key).getApiField("httpClient"),
|
|
57
|
+
"makeRequest",
|
|
58
|
+
).mockImplementation(
|
|
59
|
+
async (_host, _port, requestPath, method, _headers, data) => {
|
|
60
|
+
if (typeof requestPath !== "string" || typeof method !== "string")
|
|
61
|
+
throw new Error("Malformed synthetic transport request");
|
|
62
|
+
const path = new URL(requestPath, "https://qa.invalid").pathname;
|
|
63
|
+
requests.push({ method, path });
|
|
64
|
+
let body: unknown;
|
|
65
|
+
if (method === "GET" && path === "/v1/payment_intents/pi_authorize") {
|
|
66
|
+
if (pause === "payment") {
|
|
67
|
+
pause = undefined;
|
|
68
|
+
reached();
|
|
69
|
+
await released;
|
|
70
|
+
}
|
|
71
|
+
body = {
|
|
72
|
+
id: "pi_authorize",
|
|
73
|
+
status: "succeeded",
|
|
74
|
+
amount_received: 1000,
|
|
75
|
+
currency: "usd",
|
|
76
|
+
metadata: {},
|
|
77
|
+
latest_charge: {
|
|
78
|
+
id: "ch_authorize",
|
|
79
|
+
amount: 1000,
|
|
80
|
+
currency: "usd",
|
|
81
|
+
amount_refunded: 0,
|
|
82
|
+
disputed: false,
|
|
83
|
+
balance_transaction: {
|
|
84
|
+
id: "txn_authorize",
|
|
85
|
+
fee: 60,
|
|
86
|
+
currency: "usd",
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
} else if (method === "GET" && path === "/v1/transfers") {
|
|
91
|
+
if (pause === "history") {
|
|
92
|
+
pause = undefined;
|
|
93
|
+
reached();
|
|
94
|
+
await released;
|
|
95
|
+
}
|
|
96
|
+
body = { data: transfers, has_more: false };
|
|
97
|
+
} else if (method === "GET" && path === "/v1/charges/ch_authorize") {
|
|
98
|
+
body = { id: "ch_authorize", created: 1 };
|
|
99
|
+
} else if (method === "POST" && path === "/v1/transfers") {
|
|
100
|
+
const params = new URLSearchParams(
|
|
101
|
+
typeof data === "string" ? data : "",
|
|
102
|
+
);
|
|
103
|
+
body = {
|
|
104
|
+
id: `tr_${transfers.length}`,
|
|
105
|
+
destination: params.get("destination"),
|
|
106
|
+
amount: Number(params.get("amount")),
|
|
107
|
+
currency: params.get("currency"),
|
|
108
|
+
source_transaction: params.get("source_transaction"),
|
|
109
|
+
transfer_group: params.get("transfer_group"),
|
|
110
|
+
metadata: {
|
|
111
|
+
bsRole: params.get("metadata[bsRole]"),
|
|
112
|
+
bsSourceCharge: params.get("metadata[bsSourceCharge]"),
|
|
113
|
+
},
|
|
114
|
+
amount_reversed: 0,
|
|
115
|
+
};
|
|
116
|
+
transfers.push(body as Record<string, unknown>);
|
|
117
|
+
if (stopAfterFirst) authorized = false;
|
|
118
|
+
} else {
|
|
119
|
+
throw new Error("All unlisted provider transport is forbidden");
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
getStatusCode: () => 200,
|
|
123
|
+
getHeaders: () => ({}),
|
|
124
|
+
getRawResponse: () => ({}),
|
|
125
|
+
toStream: () => {
|
|
126
|
+
throw new Error("Streams forbidden");
|
|
127
|
+
},
|
|
128
|
+
toJSON: async () => structuredClone(body),
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
return {
|
|
133
|
+
t,
|
|
134
|
+
sdk,
|
|
135
|
+
options,
|
|
136
|
+
requests,
|
|
137
|
+
transfers,
|
|
138
|
+
reachedRead,
|
|
139
|
+
release,
|
|
140
|
+
pauseRead: (which: "payment" | "history") => {
|
|
141
|
+
pause = which;
|
|
142
|
+
},
|
|
143
|
+
revoke: () => {
|
|
144
|
+
authorized = false;
|
|
145
|
+
},
|
|
146
|
+
allow: () => {
|
|
147
|
+
authorized = true;
|
|
148
|
+
stopAfterFirst = false;
|
|
149
|
+
},
|
|
150
|
+
stopAfterFirst: () => {
|
|
151
|
+
stopAfterFirst = true;
|
|
152
|
+
},
|
|
153
|
+
failLedger: () => {
|
|
154
|
+
failLedger = true;
|
|
155
|
+
},
|
|
156
|
+
authorize: () => {
|
|
157
|
+
if (!authorized) throw new Error("Host settlement authorization denied");
|
|
158
|
+
},
|
|
159
|
+
settle: (input = options) =>
|
|
160
|
+
t.action((ctx) =>
|
|
161
|
+
sdk.settleSale(
|
|
162
|
+
{
|
|
163
|
+
runQuery: ctx.runQuery,
|
|
164
|
+
runMutation: async (reference, ...args) => {
|
|
165
|
+
const path: unknown = Reflect.get(
|
|
166
|
+
reference,
|
|
167
|
+
Symbol.for("toReferencePath"),
|
|
168
|
+
);
|
|
169
|
+
if (
|
|
170
|
+
failLedger &&
|
|
171
|
+
typeof path === "string" &&
|
|
172
|
+
path.endsWith("connect/mutations/upsertTransfer")
|
|
173
|
+
) {
|
|
174
|
+
failLedger = false;
|
|
175
|
+
throw new Error("Synthetic ledger write failure");
|
|
176
|
+
}
|
|
177
|
+
return ctx.runMutation(reference, ...args);
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
input,
|
|
181
|
+
),
|
|
182
|
+
),
|
|
183
|
+
reserve: (allocation: SettleSaleOptions["allocation"]) =>
|
|
184
|
+
t.mutation(
|
|
185
|
+
components.betterStripe.settlement.mutations
|
|
186
|
+
.reserveSettlementAllocation,
|
|
187
|
+
{
|
|
188
|
+
...allocation,
|
|
189
|
+
sourceChargeId: "ch_authorize",
|
|
190
|
+
paymentIntentId: "pi_authorize",
|
|
191
|
+
},
|
|
192
|
+
),
|
|
193
|
+
allocation: () =>
|
|
194
|
+
t.query(
|
|
195
|
+
components.betterStripe.settlement.queries.getSettlementAllocation,
|
|
196
|
+
{ sourceChargeId: "ch_authorize" },
|
|
197
|
+
),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import { authorizationFixture } from "./authorization.fixture.js";
|
|
4
|
+
import type { SettlementAuthorizationContext } from "./authorization.js";
|
|
5
|
+
|
|
6
|
+
afterEach(() => vi.restoreAllMocks());
|
|
7
|
+
|
|
8
|
+
it("retains the public no-hook settlement and registered component ledger", async () => {
|
|
9
|
+
const f = await authorizationFixture();
|
|
10
|
+
expect(await f.settle()).toMatchObject({
|
|
11
|
+
status: "settled",
|
|
12
|
+
executedTransfers: [{ amount: 940 }],
|
|
13
|
+
});
|
|
14
|
+
expect(f.transfers).toHaveLength(1);
|
|
15
|
+
expect(await f.allocation()).toMatchObject({
|
|
16
|
+
sourceChargeId: "ch_authorize",
|
|
17
|
+
gross: 1000,
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("denies admission before an allocation or transfer when the host hook rejects", async () => {
|
|
22
|
+
const f = await authorizationFixture();
|
|
23
|
+
f.revoke();
|
|
24
|
+
await expect(
|
|
25
|
+
f.settle({ ...f.options, authorize: async () => f.authorize() }),
|
|
26
|
+
).rejects.toThrow("Host settlement authorization denied");
|
|
27
|
+
expect(await f.allocation()).toBeNull();
|
|
28
|
+
expect(f.transfers).toHaveLength(0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("reauthorizes after provider source reads before admitting allocation", async () => {
|
|
32
|
+
const f = await authorizationFixture();
|
|
33
|
+
f.pauseRead("payment");
|
|
34
|
+
const pending = f.settle({
|
|
35
|
+
...f.options,
|
|
36
|
+
authorize: async () => f.authorize(),
|
|
37
|
+
});
|
|
38
|
+
await f.reachedRead;
|
|
39
|
+
f.revoke();
|
|
40
|
+
f.release();
|
|
41
|
+
await expect(pending).rejects.toThrow("Host settlement authorization denied");
|
|
42
|
+
expect(await f.allocation()).toBeNull();
|
|
43
|
+
expect(f.transfers).toHaveLength(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("reauthorizes after transfer reconciliation immediately before new money dispatch", async () => {
|
|
47
|
+
const f = await authorizationFixture();
|
|
48
|
+
f.pauseRead("history");
|
|
49
|
+
const pending = f.settle({
|
|
50
|
+
...f.options,
|
|
51
|
+
authorize: async () => f.authorize(),
|
|
52
|
+
});
|
|
53
|
+
await f.reachedRead;
|
|
54
|
+
f.revoke();
|
|
55
|
+
f.release();
|
|
56
|
+
await expect(pending).rejects.toThrow("Host settlement authorization denied");
|
|
57
|
+
expect(f.transfers).toHaveLength(0);
|
|
58
|
+
expect(await f.allocation()).toMatchObject({ gross: 1000 });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("stops later legs on denial and retries only missing legs under the frozen allocation", async () => {
|
|
62
|
+
const f = await authorizationFixture(true);
|
|
63
|
+
f.stopAfterFirst();
|
|
64
|
+
const contexts: SettlementAuthorizationContext[] = [];
|
|
65
|
+
const authorize = async (context: SettlementAuthorizationContext) => {
|
|
66
|
+
contexts.push(context);
|
|
67
|
+
f.authorize();
|
|
68
|
+
};
|
|
69
|
+
await expect(f.settle({ ...f.options, authorize })).rejects.toThrow(
|
|
70
|
+
"Host settlement authorization denied",
|
|
71
|
+
);
|
|
72
|
+
expect(f.transfers).toHaveLength(1);
|
|
73
|
+
f.allow();
|
|
74
|
+
await f.settle({
|
|
75
|
+
...f.options,
|
|
76
|
+
allocation: {
|
|
77
|
+
...f.options.allocation,
|
|
78
|
+
platformNet: 100,
|
|
79
|
+
transfers: [
|
|
80
|
+
{ destinationAccountId: "acct_changed", role: "store", amount: 840 },
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
authorize,
|
|
84
|
+
});
|
|
85
|
+
expect(f.transfers).toHaveLength(2);
|
|
86
|
+
expect(f.transfers.map((t) => t.destination)).toEqual([
|
|
87
|
+
"acct_creator",
|
|
88
|
+
"acct_other",
|
|
89
|
+
]);
|
|
90
|
+
expect(
|
|
91
|
+
contexts
|
|
92
|
+
.filter((c) => c.phase === "transfer")
|
|
93
|
+
.map((c) => c.transfer.destinationAccountId),
|
|
94
|
+
).toEqual(["acct_creator", "acct_other", "acct_other"]);
|
|
95
|
+
expect(
|
|
96
|
+
contexts
|
|
97
|
+
.filter((c) => c.phase === "reserve")
|
|
98
|
+
.map((c) => c.settlement.platformNet),
|
|
99
|
+
).toEqual([0, 0]);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("checks the actual saved foreign leg when another reservation wins after admission", async () => {
|
|
103
|
+
const f = await authorizationFixture();
|
|
104
|
+
const authorize = async (context: SettlementAuthorizationContext) => {
|
|
105
|
+
if (context.phase === "reserve") {
|
|
106
|
+
await f.reserve({
|
|
107
|
+
...f.options.allocation,
|
|
108
|
+
transfers: [
|
|
109
|
+
{ destinationAccountId: "acct_foreign", role: "store", amount: 940 },
|
|
110
|
+
],
|
|
111
|
+
});
|
|
112
|
+
} else if (context.transfer.destinationAccountId !== "acct_creator") {
|
|
113
|
+
throw new Error("Host rejected actual foreign recipient");
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
await expect(f.settle({ ...f.options, authorize })).rejects.toThrow(
|
|
117
|
+
"Host rejected actual foreign recipient",
|
|
118
|
+
);
|
|
119
|
+
expect(f.transfers).toHaveLength(0);
|
|
120
|
+
expect(await f.allocation()).toMatchObject({
|
|
121
|
+
transfers: [{ destinationAccountId: "acct_foreign" }],
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("recovers known provider success without invoking another new-money hook or POST", async () => {
|
|
126
|
+
const f = await authorizationFixture();
|
|
127
|
+
f.failLedger();
|
|
128
|
+
const authorize = vi.fn(async () => {});
|
|
129
|
+
await expect(f.settle({ ...f.options, authorize })).rejects.toThrow(
|
|
130
|
+
"Synthetic ledger write failure",
|
|
131
|
+
);
|
|
132
|
+
expect(f.transfers).toHaveLength(1);
|
|
133
|
+
authorize.mockClear();
|
|
134
|
+
expect(await f.settle({ ...f.options, authorize })).toMatchObject({
|
|
135
|
+
status: "settled",
|
|
136
|
+
});
|
|
137
|
+
expect(f.transfers).toHaveLength(1);
|
|
138
|
+
expect(authorize.mock.calls).toHaveLength(1);
|
|
139
|
+
expect(authorize).toHaveBeenCalledWith(
|
|
140
|
+
expect.objectContaining({ phase: "reserve" }),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("detaches authorization snapshots so callback mutation cannot change financial inputs", async () => {
|
|
145
|
+
const f = await authorizationFixture();
|
|
146
|
+
const authorize = async (context: SettlementAuthorizationContext) => {
|
|
147
|
+
Object.assign(context.settlement, { platformNet: 500 });
|
|
148
|
+
if (context.phase === "reserve")
|
|
149
|
+
Object.assign(context.settlement.transfers[0], { amount: 1 });
|
|
150
|
+
else
|
|
151
|
+
Object.assign(context.transfer, {
|
|
152
|
+
amount: 1,
|
|
153
|
+
destinationAccountId: "acct_foreign",
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
expect(await f.settle({ ...f.options, authorize })).toMatchObject({
|
|
157
|
+
platformNet: 0,
|
|
158
|
+
executedTransfers: [{ amount: 940, destinationAccountId: "acct_creator" }],
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { SplitTransfer } from "../core/fees.js";
|
|
2
|
+
import type { ResolvedSettlement } from "./execution.js";
|
|
3
|
+
|
|
4
|
+
type Source = Readonly<Omit<ResolvedSettlement, "transfers">>;
|
|
5
|
+
export type SettlementAuthorizationContext =
|
|
6
|
+
| {
|
|
7
|
+
phase: "reserve";
|
|
8
|
+
settlement: Source & {
|
|
9
|
+
readonly transfers: readonly Readonly<SplitTransfer>[];
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
| {
|
|
13
|
+
phase: "transfer";
|
|
14
|
+
settlement: Source;
|
|
15
|
+
transfer: Readonly<SplitTransfer>;
|
|
16
|
+
};
|
|
17
|
+
/** Server-only check. Throw to deny; returning never changes the allocation. */
|
|
18
|
+
export type SettlementAuthorization = (
|
|
19
|
+
context: SettlementAuthorizationContext,
|
|
20
|
+
) => Promise<void>;
|
|
21
|
+
|
|
22
|
+
/** Detached snapshots prevent host callbacks from modifying claimed economics. */
|
|
23
|
+
export async function authorizeSettlement(
|
|
24
|
+
authorize: SettlementAuthorization | undefined,
|
|
25
|
+
settlement: ResolvedSettlement,
|
|
26
|
+
transfer?: SplitTransfer,
|
|
27
|
+
) {
|
|
28
|
+
if (!authorize) return;
|
|
29
|
+
const source = structuredClone({
|
|
30
|
+
sourceChargeId: settlement.sourceChargeId,
|
|
31
|
+
paymentIntentId: settlement.paymentIntentId,
|
|
32
|
+
sourceInvoiceId: settlement.sourceInvoiceId,
|
|
33
|
+
usageSource: settlement.usageSource,
|
|
34
|
+
creditSource: settlement.creditSource,
|
|
35
|
+
gross: settlement.gross,
|
|
36
|
+
currency: settlement.currency,
|
|
37
|
+
processingFee: settlement.processingFee,
|
|
38
|
+
platformNet: settlement.platformNet,
|
|
39
|
+
adjustmentAmount: settlement.adjustmentAmount,
|
|
40
|
+
paymentId: settlement.paymentId,
|
|
41
|
+
});
|
|
42
|
+
await authorize(
|
|
43
|
+
transfer
|
|
44
|
+
? { phase: "transfer", settlement: source, transfer: { ...transfer } }
|
|
45
|
+
: {
|
|
46
|
+
phase: "reserve",
|
|
47
|
+
settlement: {
|
|
48
|
+
...source,
|
|
49
|
+
transfers: structuredClone(settlement.transfers),
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
}
|
|
@@ -9,6 +9,10 @@ import type { SplitTransfer } from "../core/fees.js";
|
|
|
9
9
|
import type { Component, RunCtx } from "../helpers.js";
|
|
10
10
|
import { runMutationOrThrow } from "../helpers.js";
|
|
11
11
|
import { componentRef } from "../webhooks/helpers.js";
|
|
12
|
+
import {
|
|
13
|
+
authorizeSettlement,
|
|
14
|
+
type SettlementAuthorization,
|
|
15
|
+
} from "./authorization.js";
|
|
12
16
|
export type ResolvedSettlement = {
|
|
13
17
|
sourceChargeId: string;
|
|
14
18
|
paymentIntentId?: string;
|
|
@@ -65,6 +69,7 @@ export async function executeResolvedTransfers(
|
|
|
65
69
|
component: Component,
|
|
66
70
|
ctx: RunCtx,
|
|
67
71
|
opts: ResolvedSettlement,
|
|
72
|
+
authorize?: SettlementAuthorization,
|
|
68
73
|
): Promise<SettlementResult> {
|
|
69
74
|
const saved = (await runMutationOrThrow(
|
|
70
75
|
ctx,
|
|
@@ -160,9 +165,10 @@ export async function executeResolvedTransfers(
|
|
|
160
165
|
let transferId = recorded?.stripeTransferId;
|
|
161
166
|
if (!transferId) {
|
|
162
167
|
const recovered = matchingTransfer(provider, claim, leg);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
168
|
+
let transfer = recovered;
|
|
169
|
+
if (!transfer) {
|
|
170
|
+
await authorizeSettlement(authorize, claim, leg);
|
|
171
|
+
transfer = await stripe.transfers.create(
|
|
166
172
|
{
|
|
167
173
|
amount: leg.amount,
|
|
168
174
|
currency: claim.currency,
|
|
@@ -177,7 +183,8 @@ export async function executeResolvedTransfers(
|
|
|
177
183
|
{
|
|
178
184
|
idempotencyKey: `bs_split_${claim.sourceChargeId}_${leg.destinationAccountId}_${leg.role}`,
|
|
179
185
|
},
|
|
180
|
-
)
|
|
186
|
+
);
|
|
187
|
+
}
|
|
181
188
|
transferId = transfer.id;
|
|
182
189
|
await recordSettlementTransfer(component, ctx, claim, leg, transfer);
|
|
183
190
|
}
|
|
@@ -4,6 +4,10 @@ import type { Component, RunCtx } from "../helpers.js";
|
|
|
4
4
|
import { componentRef } from "../webhooks/helpers.js";
|
|
5
5
|
import { throwStripeError } from "../errors.js";
|
|
6
6
|
import { getVerifiedSale, type VerifiedSaleOptions } from "./verifiedSale.js";
|
|
7
|
+
import {
|
|
8
|
+
authorizeSettlement,
|
|
9
|
+
type SettlementAuthorization,
|
|
10
|
+
} from "./authorization.js";
|
|
7
11
|
import {
|
|
8
12
|
executeResolvedTransfers,
|
|
9
13
|
type SettlementResult,
|
|
@@ -19,6 +23,8 @@ export type SettleSaleOptions = VerifiedSaleOptions & {
|
|
|
19
23
|
};
|
|
20
24
|
paymentId?: string;
|
|
21
25
|
onSettled?: (result: SettlementResult) => Promise<void>;
|
|
26
|
+
/** Reauthorize after source reads and immediately before each new transfer. */
|
|
27
|
+
authorize?: SettlementAuthorization;
|
|
22
28
|
};
|
|
23
29
|
export async function settleSale(
|
|
24
30
|
stripe: Stripe,
|
|
@@ -47,13 +53,21 @@ export async function settleSale(
|
|
|
47
53
|
"INVALID_CONFIGURATION",
|
|
48
54
|
"Allocation must use verified payment currency, gross and actual processing cost.",
|
|
49
55
|
);
|
|
50
|
-
const
|
|
56
|
+
const request = {
|
|
51
57
|
...opts.allocation,
|
|
52
58
|
sourceChargeId: sale.chargeId,
|
|
53
59
|
paymentIntentId: sale.paymentIntentId,
|
|
54
60
|
sourceInvoiceId: sale.invoiceId,
|
|
55
61
|
paymentId: opts.paymentId,
|
|
56
|
-
}
|
|
62
|
+
};
|
|
63
|
+
await authorizeSettlement(opts.authorize, prior ?? request);
|
|
64
|
+
const result = await executeResolvedTransfers(
|
|
65
|
+
stripe,
|
|
66
|
+
component,
|
|
67
|
+
ctx,
|
|
68
|
+
request,
|
|
69
|
+
opts.authorize,
|
|
70
|
+
);
|
|
57
71
|
await opts.onSettled?.(result);
|
|
58
72
|
return result;
|
|
59
73
|
}
|