@fantastic.dev/better-stripe 0.64.1 → 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 +10 -0
- package/README.md +1 -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/settlement.md +56 -0
- package/package.json +1 -1
- 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
|
@@ -372,14 +372,6 @@ export declare const mutation: import("convex-helpers/server/customFunctions").C
|
|
|
372
372
|
preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
|
|
373
373
|
requestKey: string;
|
|
374
374
|
result?: {
|
|
375
|
-
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
376
|
-
retryable: false;
|
|
377
|
-
status: "conflict";
|
|
378
|
-
} | {
|
|
379
|
-
reason: "policy_invalid";
|
|
380
|
-
retryable: false;
|
|
381
|
-
status: "rejected";
|
|
382
|
-
} | {
|
|
383
375
|
operation: {
|
|
384
376
|
accountId: string;
|
|
385
377
|
operationId: import("convex/values").GenericId<"balanceFundingOperations">;
|
|
@@ -390,6 +382,14 @@ export declare const mutation: import("convex-helpers/server/customFunctions").C
|
|
|
390
382
|
} | {
|
|
391
383
|
enabled: boolean;
|
|
392
384
|
status: "unchanged";
|
|
385
|
+
} | {
|
|
386
|
+
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
387
|
+
retryable: false;
|
|
388
|
+
status: "conflict";
|
|
389
|
+
} | {
|
|
390
|
+
reason: "policy_invalid";
|
|
391
|
+
retryable: false;
|
|
392
|
+
status: "rejected";
|
|
393
393
|
} | {
|
|
394
394
|
reason: "provider_pending";
|
|
395
395
|
retryable: true;
|
|
@@ -3216,14 +3216,6 @@ export declare const mutation: import("convex-helpers/server/customFunctions").C
|
|
|
3216
3216
|
preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
|
|
3217
3217
|
requestKey: string;
|
|
3218
3218
|
result?: {
|
|
3219
|
-
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
3220
|
-
retryable: false;
|
|
3221
|
-
status: "conflict";
|
|
3222
|
-
} | {
|
|
3223
|
-
reason: "policy_invalid";
|
|
3224
|
-
retryable: false;
|
|
3225
|
-
status: "rejected";
|
|
3226
|
-
} | {
|
|
3227
3219
|
operation: {
|
|
3228
3220
|
accountId: string;
|
|
3229
3221
|
operationId: import("convex/values").GenericId<"balanceFundingOperations">;
|
|
@@ -3234,6 +3226,14 @@ export declare const mutation: import("convex-helpers/server/customFunctions").C
|
|
|
3234
3226
|
} | {
|
|
3235
3227
|
enabled: boolean;
|
|
3236
3228
|
status: "unchanged";
|
|
3229
|
+
} | {
|
|
3230
|
+
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
3231
|
+
retryable: false;
|
|
3232
|
+
status: "conflict";
|
|
3233
|
+
} | {
|
|
3234
|
+
reason: "policy_invalid";
|
|
3235
|
+
retryable: false;
|
|
3236
|
+
status: "rejected";
|
|
3237
3237
|
} | {
|
|
3238
3238
|
reason: "provider_pending";
|
|
3239
3239
|
retryable: true;
|
|
@@ -14,7 +14,7 @@ export declare function checkRollback(ctx: MutationCtx, op: Doc<"processingCostR
|
|
|
14
14
|
readonly bindFunding?: undefined;
|
|
15
15
|
} | {
|
|
16
16
|
reason?: undefined;
|
|
17
|
-
|
|
17
|
+
retryable?: undefined;
|
|
18
18
|
readonly status: "ready";
|
|
19
19
|
readonly bindFunding: boolean;
|
|
20
20
|
}>;
|
|
@@ -2137,14 +2137,6 @@ export declare function setupAllowance(): Promise<{
|
|
|
2137
2137
|
preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
|
|
2138
2138
|
requestKey: string;
|
|
2139
2139
|
result?: {
|
|
2140
|
-
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
2141
|
-
retryable: false;
|
|
2142
|
-
status: "conflict";
|
|
2143
|
-
} | {
|
|
2144
|
-
reason: "policy_invalid";
|
|
2145
|
-
retryable: false;
|
|
2146
|
-
status: "rejected";
|
|
2147
|
-
} | {
|
|
2148
2140
|
operation: {
|
|
2149
2141
|
accountId: string;
|
|
2150
2142
|
operationId: import("convex/values").GenericId<"balanceFundingOperations">;
|
|
@@ -2155,6 +2147,14 @@ export declare function setupAllowance(): Promise<{
|
|
|
2155
2147
|
} | {
|
|
2156
2148
|
enabled: boolean;
|
|
2157
2149
|
status: "unchanged";
|
|
2150
|
+
} | {
|
|
2151
|
+
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
2152
|
+
retryable: false;
|
|
2153
|
+
status: "conflict";
|
|
2154
|
+
} | {
|
|
2155
|
+
reason: "policy_invalid";
|
|
2156
|
+
retryable: false;
|
|
2157
|
+
status: "rejected";
|
|
2158
2158
|
} | {
|
|
2159
2159
|
reason: "provider_pending";
|
|
2160
2160
|
retryable: true;
|
|
@@ -2165,14 +2165,6 @@ export declare function setupAllowance(): Promise<{
|
|
|
2165
2165
|
payload: import("convex/values").VString<string, "required">;
|
|
2166
2166
|
preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
|
|
2167
2167
|
result: import("convex/values").VUnion<{
|
|
2168
|
-
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
2169
|
-
retryable: false;
|
|
2170
|
-
status: "conflict";
|
|
2171
|
-
} | {
|
|
2172
|
-
reason: "policy_invalid";
|
|
2173
|
-
retryable: false;
|
|
2174
|
-
status: "rejected";
|
|
2175
|
-
} | {
|
|
2176
2168
|
operation: {
|
|
2177
2169
|
accountId: string;
|
|
2178
2170
|
operationId: import("convex/values").GenericId<"balanceFundingOperations">;
|
|
@@ -2183,6 +2175,14 @@ export declare function setupAllowance(): Promise<{
|
|
|
2183
2175
|
} | {
|
|
2184
2176
|
enabled: boolean;
|
|
2185
2177
|
status: "unchanged";
|
|
2178
|
+
} | {
|
|
2179
|
+
reason: "identity_changed" | "revision_changed" | "scope_busy";
|
|
2180
|
+
retryable: false;
|
|
2181
|
+
status: "conflict";
|
|
2182
|
+
} | {
|
|
2183
|
+
reason: "policy_invalid";
|
|
2184
|
+
retryable: false;
|
|
2185
|
+
status: "rejected";
|
|
2186
2186
|
} | {
|
|
2187
2187
|
reason: "provider_pending";
|
|
2188
2188
|
retryable: true;
|
package/docs/settlement.md
CHANGED
|
@@ -120,6 +120,62 @@ closed. Existing refund/dispute reversal APIs recover these original legs too.
|
|
|
120
120
|
Stripe API idempotency is an additional safeguard, not the only recovery record.
|
|
121
121
|
Do not delete financial component records while payments can be retried or reversed.
|
|
122
122
|
|
|
123
|
+
### Reauthorize before financial dispatch
|
|
124
|
+
|
|
125
|
+
`settleSale` accepts an optional **server-only** `authorize` callback. Existing
|
|
126
|
+
callers without it retain their behavior. Authenticate the actor and derive the
|
|
127
|
+
original purchase, community, charge and recipient scope on your server first;
|
|
128
|
+
then use the callback to re-read current authorization rather than trusting
|
|
129
|
+
browser account IDs or provider metadata:
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
await stripe.settleSale(ctx, {
|
|
133
|
+
...originalSettlement,
|
|
134
|
+
authorize: async (context) => {
|
|
135
|
+
// Application-owned check: current actor, original purchase/source and recipients.
|
|
136
|
+
await authorizeOriginalSale(context);
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The discriminated `SettlementAuthorizationContext` has two phases:
|
|
142
|
+
|
|
143
|
+
- `reserve`: after provider sale reads and before allocation admission. On replay,
|
|
144
|
+
the snapshot contains the prior immutable allocation when one exists.
|
|
145
|
+
- `transfer`: after provider transfer reconciliation and immediately before each
|
|
146
|
+
**new** transfer POST. It contains the actual saved source/economics and the
|
|
147
|
+
effective leg, including a reduced affiliate release after package-owned debt
|
|
148
|
+
offsets. A competing reservation cannot substitute an unexamined recipient.
|
|
149
|
+
|
|
150
|
+
Snapshots are detached from engine inputs; callback mutation cannot change the
|
|
151
|
+
claim or transfer. Throwing from a direct `settleSale` callback propagates the
|
|
152
|
+
host error and prevents that write; the SDK does not retry it. The callback does
|
|
153
|
+
not return replacement economics or establish original cash provenance.
|
|
154
|
+
|
|
155
|
+
The last check and Stripe are not a distributed transaction. A later denial can
|
|
156
|
+
leave an admitted immutable allocation or earlier successful legs in place;
|
|
157
|
+
partial success is not undone. Retrying reuses the original claim and still
|
|
158
|
+
checks each missing new leg. Already-recovered provider success can be persisted
|
|
159
|
+
without another `transfer` callback or another POST: it is reconciliation of
|
|
160
|
+
money already moved, not new financial dispatch. `onSettled` remains a
|
|
161
|
+
post-execution, replayable completion callback, not an authorization check.
|
|
162
|
+
|
|
163
|
+
The active Vite adapter uses fresh original-sale and current seller-mapping
|
|
164
|
+
queries; demo metadata is not a production authorization policy. For its actual
|
|
165
|
+
sealed recovery-group path, pass the same server-only callback in the runtime
|
|
166
|
+
options to `costRecovery.executeGroupProvider`. This path already has persisted
|
|
167
|
+
group/allocation admission and dispatch: it invokes only the final `transfer`
|
|
168
|
+
phase for each new settlement transfer, not an earlier `reserve` guarantee.
|
|
169
|
+
The existing snapshot/definition/phase checks still apply. Reconciliation-only
|
|
170
|
+
work never creates a missing transfer or invokes a new-money callback.
|
|
171
|
+
|
|
172
|
+
A recovery callback rejection uses the existing conservative `unknown` hold
|
|
173
|
+
receipt and does not automatically retry or complete the group. This is the
|
|
174
|
+
workflow's disposition of a denied dispatch, **not** an observed unknown Stripe
|
|
175
|
+
cash result or provider failure. Original group/allocation state and earlier
|
|
176
|
+
success remain retained. No callback, credential or authority flag is persisted
|
|
177
|
+
in workflow arguments; supply it inside the registered internal host action.
|
|
178
|
+
|
|
123
179
|
The Vite example exposes a [fee-first allocation preview](http://localhost:5173/demo/allocation)
|
|
124
180
|
from Admin → Testing Utilities. Its illustrative inputs use the public helper
|
|
125
181
|
and the package React `SplitBreakdown`; they do not alter store settings.
|
package/package.json
CHANGED
package/src/client/index.ts
CHANGED
|
@@ -137,6 +137,10 @@ export type {
|
|
|
137
137
|
VerifiedPaymentSourceOptions,
|
|
138
138
|
} from "./paymentSource/contracts.js";
|
|
139
139
|
export type { SettleSaleOptions } from "./settlement/settleSale.js";
|
|
140
|
+
export type {
|
|
141
|
+
SettlementAuthorization,
|
|
142
|
+
SettlementAuthorizationContext,
|
|
143
|
+
} from "./settlement/authorization.js";
|
|
140
144
|
export type { SettlementResult } from "./settlement/execution.js";
|
|
141
145
|
export type {
|
|
142
146
|
CommissionTerm,
|
|
@@ -19,6 +19,7 @@ async function setup(
|
|
|
19
19
|
kind: "invoice" | "settlement" = "invoice",
|
|
20
20
|
dispatch = true,
|
|
21
21
|
releaseUncollectible = false,
|
|
22
|
+
twoLegs = false,
|
|
22
23
|
) {
|
|
23
24
|
const t = initConvexTest();
|
|
24
25
|
t.registerComponent("betterStripe", schema, modules);
|
|
@@ -58,13 +59,26 @@ async function setup(
|
|
|
58
59
|
processingFee: 30,
|
|
59
60
|
platformNet: 40,
|
|
60
61
|
adjustmentAmount: 30,
|
|
61
|
-
transfers:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
62
|
+
transfers: twoLegs
|
|
63
|
+
? [
|
|
64
|
+
{
|
|
65
|
+
destinationAccountId: "acct_seller",
|
|
66
|
+
role: "store" as const,
|
|
67
|
+
amount: 800,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
destinationAccountId: "acct_other",
|
|
71
|
+
role: "other" as const,
|
|
72
|
+
amount: 100,
|
|
73
|
+
},
|
|
74
|
+
]
|
|
75
|
+
: [
|
|
76
|
+
{
|
|
77
|
+
destinationAccountId: "acct_seller",
|
|
78
|
+
role: "store" as const,
|
|
79
|
+
amount: 900,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
68
82
|
};
|
|
69
83
|
await client.prepareGroup(ctx, {
|
|
70
84
|
...scope,
|
|
@@ -422,3 +436,131 @@ it.each([false, true])(
|
|
|
422
436
|
);
|
|
423
437
|
},
|
|
424
438
|
);
|
|
439
|
+
|
|
440
|
+
it("holds partial authorized recovery success and never creates the denied missing leg during reconciliation", async () => {
|
|
441
|
+
const f = await setup("settlement", true, false, true);
|
|
442
|
+
let current = true;
|
|
443
|
+
const transfers: Record<string, unknown>[] = [];
|
|
444
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
445
|
+
const path = new URL(String(url)).pathname;
|
|
446
|
+
let body: unknown;
|
|
447
|
+
if (path === "/v1/transfers" && init?.method === "POST") {
|
|
448
|
+
const params = new URLSearchParams(String(init.body));
|
|
449
|
+
body = {
|
|
450
|
+
id: "tr_partial",
|
|
451
|
+
destination: params.get("destination"),
|
|
452
|
+
amount: Number(params.get("amount")),
|
|
453
|
+
currency: "usd",
|
|
454
|
+
source_transaction: "ch_source",
|
|
455
|
+
amount_reversed: 0,
|
|
456
|
+
metadata: {
|
|
457
|
+
bsRole: params.get("metadata[bsRole]"),
|
|
458
|
+
bsSourceCharge: "ch_source",
|
|
459
|
+
},
|
|
460
|
+
};
|
|
461
|
+
transfers.push(body as Record<string, unknown>);
|
|
462
|
+
current = false;
|
|
463
|
+
} else if (path === "/v1/transfers" && init?.method === "GET") {
|
|
464
|
+
body = { object: "list", data: transfers, has_more: false };
|
|
465
|
+
} else if (path === "/v1/charges/ch_source" && init?.method === "GET") {
|
|
466
|
+
body = { id: "ch_source", created: 1 };
|
|
467
|
+
} else throw new Error("Unlisted synthetic provider request");
|
|
468
|
+
return new Response(JSON.stringify(body), {
|
|
469
|
+
status: 200,
|
|
470
|
+
headers: { "content-type": "application/json" },
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
const authorize = async () => {
|
|
474
|
+
if (!current) throw new Error("Host authorization denied");
|
|
475
|
+
};
|
|
476
|
+
const result = await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
477
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
478
|
+
httpClient,
|
|
479
|
+
authorize,
|
|
480
|
+
});
|
|
481
|
+
expect(result).toMatchObject({ status: "unknown" });
|
|
482
|
+
if (result.status !== "unknown") throw new Error("Expected a held dispatch");
|
|
483
|
+
expect(transfers).toHaveLength(1);
|
|
484
|
+
expect(
|
|
485
|
+
await f.t.query(nested.betterStripe.connect.queries.listTransfersByCharge, {
|
|
486
|
+
sourceChargeId: "ch_source",
|
|
487
|
+
}),
|
|
488
|
+
).toMatchObject([{ amount: 800 }]);
|
|
489
|
+
await f.t.mutation(nested.betterStripe.recoveryWorkflow.steps.saveReceipt, {
|
|
490
|
+
...scope,
|
|
491
|
+
operationId: f.request.operationId,
|
|
492
|
+
version: 1,
|
|
493
|
+
result: { status: result.status, reason: result.reason },
|
|
494
|
+
});
|
|
495
|
+
await f.dispatchReconcile();
|
|
496
|
+
expect(
|
|
497
|
+
await f.client.executeGroupProvider(
|
|
498
|
+
f.ctx,
|
|
499
|
+
{ ...f.request, phase: "reconcile" },
|
|
500
|
+
{ stripeSecretKey: "sk_test_runtime_only", httpClient, authorize },
|
|
501
|
+
),
|
|
502
|
+
).toMatchObject({ status: "unknown" });
|
|
503
|
+
expect(transfers).toHaveLength(1);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it.each([false, true])(
|
|
507
|
+
"reauthorizes an actual frozen recovery transfer after reconciliation (revoked=%s)",
|
|
508
|
+
async (revoked) => {
|
|
509
|
+
const f = await setup("settlement");
|
|
510
|
+
let current = true;
|
|
511
|
+
let creates = 0;
|
|
512
|
+
const checked: { charge: string; destination: string; amount: number }[] =
|
|
513
|
+
[];
|
|
514
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
515
|
+
const path = new URL(String(url)).pathname;
|
|
516
|
+
let body: unknown;
|
|
517
|
+
if (path === "/v1/charges/ch_source" && init?.method === "GET") {
|
|
518
|
+
body = { id: "ch_source", created: 1 };
|
|
519
|
+
} else if (path === "/v1/transfers" && init?.method === "GET") {
|
|
520
|
+
if (revoked) current = false;
|
|
521
|
+
body = { object: "list", data: [], has_more: false };
|
|
522
|
+
} else if (path === "/v1/transfers" && init?.method === "POST") {
|
|
523
|
+
creates++;
|
|
524
|
+
body = {
|
|
525
|
+
id: "tr_authorized",
|
|
526
|
+
destination: "acct_seller",
|
|
527
|
+
amount: 900,
|
|
528
|
+
currency: "usd",
|
|
529
|
+
source_transaction: "ch_source",
|
|
530
|
+
metadata: { bsRole: "store", bsSourceCharge: "ch_source" },
|
|
531
|
+
};
|
|
532
|
+
} else throw new Error("Unlisted synthetic provider request");
|
|
533
|
+
return new Response(JSON.stringify(body), {
|
|
534
|
+
status: 200,
|
|
535
|
+
headers: { "content-type": "application/json" },
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
const options = {
|
|
539
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
540
|
+
httpClient,
|
|
541
|
+
authorize: async (
|
|
542
|
+
context: import("../settlement/authorization.js").SettlementAuthorizationContext,
|
|
543
|
+
) => {
|
|
544
|
+
if (context.phase !== "transfer")
|
|
545
|
+
throw new Error("Recovery group was already admitted");
|
|
546
|
+
checked.push({
|
|
547
|
+
charge: context.settlement.sourceChargeId,
|
|
548
|
+
destination: context.transfer.destinationAccountId,
|
|
549
|
+
amount: context.transfer.amount,
|
|
550
|
+
});
|
|
551
|
+
if (!current) throw new Error("Current host authority denied");
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
expect(
|
|
555
|
+
await f.client.executeGroupProvider(f.ctx, f.request, options),
|
|
556
|
+
).toMatchObject(
|
|
557
|
+
revoked
|
|
558
|
+
? { status: "unknown", reason: "provider_outcome_unknown" }
|
|
559
|
+
: { status: "ready", outcome: "collect" },
|
|
560
|
+
);
|
|
561
|
+
expect(creates).toBe(revoked ? 0 : 1);
|
|
562
|
+
expect(checked).toEqual([
|
|
563
|
+
{ charge: "ch_source", destination: "acct_seller", amount: 900 },
|
|
564
|
+
]);
|
|
565
|
+
},
|
|
566
|
+
);
|
|
@@ -5,6 +5,7 @@ import { componentRef } from "../webhooks/helpers.js";
|
|
|
5
5
|
import { attachRecoveryInvoice } from "../costRecovery-invoices.js";
|
|
6
6
|
import type { RecoveryGroup } from "../costRecovery.js";
|
|
7
7
|
import { executeResolvedTransfers } from "../settlement/execution.js";
|
|
8
|
+
import type { SettlementAuthorization } from "../settlement/authorization.js";
|
|
8
9
|
import { reconcileRecoverySettlement } from "./settlement.js";
|
|
9
10
|
import { operationRef } from "./contracts.js";
|
|
10
11
|
import type {
|
|
@@ -18,6 +19,8 @@ export type RecoveryProviderOptions = {
|
|
|
18
19
|
httpClient?: NonNullable<
|
|
19
20
|
ConstructorParameters<typeof Stripe>[1]
|
|
20
21
|
>["httpClient"];
|
|
22
|
+
/** Runtime-only host check before each new settlement transfer; group admission already occurred. */
|
|
23
|
+
authorize?: SettlementAuthorization;
|
|
21
24
|
};
|
|
22
25
|
export async function executeRecoveryProvider(
|
|
23
26
|
component: Component,
|
|
@@ -69,7 +72,13 @@ export async function executeRecoveryProvider(
|
|
|
69
72
|
reason: "provider_outcome_unknown",
|
|
70
73
|
};
|
|
71
74
|
if (request.phase === "execute")
|
|
72
|
-
await executeResolvedTransfers(
|
|
75
|
+
await executeResolvedTransfers(
|
|
76
|
+
stripe,
|
|
77
|
+
component,
|
|
78
|
+
ctx,
|
|
79
|
+
op.allocation,
|
|
80
|
+
options.authorize,
|
|
81
|
+
);
|
|
73
82
|
else if (
|
|
74
83
|
!(await reconcileRecoverySettlement(
|
|
75
84
|
stripe,
|
|
@@ -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
|
+
}
|