@fantastic.dev/better-stripe 0.66.0 → 0.67.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 +22 -0
- package/README.md +17 -20
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +126 -12
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +79 -22
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +158 -44
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +63 -6
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- 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.fixture.d.ts +21 -0
- package/dist/client/recoveryWorkflow/provider.fixture.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/provider.fixture.js +125 -0
- package/dist/client/recoveryWorkflow/provider.fixture.js.map +1 -0
- package/dist/client/settlement/authorization.d.ts.map +1 -1
- package/dist/client/settlement/authorization.fixture.d.ts +10 -0
- package/dist/client/settlement/authorization.fixture.d.ts.map +1 -1
- package/dist/client/settlement/authorization.js +1 -0
- package/dist/client/settlement/authorization.js.map +1 -1
- package/dist/client/settlement/execution.d.ts +2 -0
- package/dist/client/settlement/execution.d.ts.map +1 -1
- package/dist/client/settlement/execution.js +3 -0
- package/dist/client/settlement/execution.js.map +1 -1
- package/dist/client/settlement/settleSale.d.ts +2 -0
- package/dist/client/settlement/settleSale.d.ts.map +1 -1
- package/dist/client/settlement/settleSale.js +4 -0
- 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/_generated/component.d.ts +25 -0
- package/dist/component/_generated/component.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/earnings/triggers.d.ts +36 -6
- 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/helpers.d.ts +5 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/mutations.d.ts +5 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/mutations.js +2 -0
- package/dist/component/recoveryWorkflow/mutations.js.map +1 -1
- package/dist/component/recoveryWorkflow/queries.d.ts +10 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/schema.d.ts +114 -10
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
- package/dist/component/schema.d.ts +63 -6
- package/dist/component/settlement/mutations.d.ts +10 -0
- package/dist/component/settlement/mutations.d.ts.map +1 -1
- package/dist/component/settlement/mutations.js +3 -0
- package/dist/component/settlement/mutations.js.map +1 -1
- package/dist/component/settlement/queries.d.ts +5 -0
- package/dist/component/settlement/queries.d.ts.map +1 -1
- package/dist/component/settlement/schema.d.ts +24 -1
- package/dist/component/settlement/schema.d.ts.map +1 -1
- package/dist/component/settlement/schema.js +5 -0
- package/dist/component/settlement/schema.js.map +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +79 -22
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
- package/dist/component/usageCredits/accounting.fixtures.d.ts +63 -6
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
- package/dist/shared/settlementPolicyContext.d.ts +10 -0
- package/dist/shared/settlementPolicyContext.d.ts.map +1 -0
- package/dist/shared/settlementPolicyContext.js +40 -0
- package/dist/shared/settlementPolicyContext.js.map +1 -0
- package/docs/README.md +2 -1
- package/docs/metered-usage.md +174 -356
- package/docs/metering-setup.md +175 -168
- package/docs/settlement.md +38 -0
- package/docs/usage-catalog.md +1 -1
- package/docs/usage-ledger.md +9 -8
- package/docs/usage-rollout.md +147 -0
- package/package.json +3 -2
- package/src/client/index.ts +1 -0
- package/src/client/recoveryWorkflow/provider.fixture.ts +143 -0
- package/src/client/recoveryWorkflow/provider.test.ts +83 -140
- package/src/client/settlement/authorization.ts +1 -0
- package/src/client/settlement/execution.ts +7 -0
- package/src/client/settlement/policy-context.test.ts +277 -0
- package/src/client/settlement/settleSale.ts +12 -0
- package/src/component/_generated/component.ts +25 -0
- package/src/component/recoveryWorkflow/mutations.ts +2 -0
- package/src/component/settlement/mutations.ts +6 -0
- package/src/component/settlement/schema.ts +7 -0
- package/src/component/tsconfig.json +5 -0
- package/src/shared/settlementPolicyContext.ts +62 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
2
|
+
import { componentsGeneric, type FunctionReference } from "convex/server";
|
|
3
|
+
import { initConvexTest } from "../setup.test.js";
|
|
4
|
+
import schema from "../../component/schema.js";
|
|
5
|
+
import { CostRecoveryClient } from "../costRecovery.js";
|
|
6
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
7
|
+
import type { SettlementPolicyContext } from "../../shared/settlementPolicyContext.js";
|
|
8
|
+
import type { RecoveryProviderRequest } from "./contracts.js";
|
|
9
|
+
const modules = import.meta.glob("../../component/**/*.ts");
|
|
10
|
+
export const nested = componentsGeneric();
|
|
11
|
+
export const scope = {
|
|
12
|
+
accountId: "acct_owner",
|
|
13
|
+
scopeKey: "store",
|
|
14
|
+
currency: "usd",
|
|
15
|
+
groupKey: "group",
|
|
16
|
+
};
|
|
17
|
+
export async function setup(
|
|
18
|
+
kind: "invoice" | "settlement" = "invoice",
|
|
19
|
+
dispatch = true,
|
|
20
|
+
releaseUncollectible = false,
|
|
21
|
+
twoLegs = false,
|
|
22
|
+
policyContext?: SettlementPolicyContext,
|
|
23
|
+
) {
|
|
24
|
+
const t = initConvexTest();
|
|
25
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
26
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
27
|
+
const component = nested.betterStripe as unknown as Component;
|
|
28
|
+
const client = new CostRecoveryClient(component);
|
|
29
|
+
let writes = 0,
|
|
30
|
+
failWrite = 0;
|
|
31
|
+
const ctx = {
|
|
32
|
+
runQuery: (
|
|
33
|
+
ref: FunctionReference<"query">,
|
|
34
|
+
args: Record<string, unknown>,
|
|
35
|
+
) => t.query(ref, args),
|
|
36
|
+
runMutation: (
|
|
37
|
+
ref: FunctionReference<"mutation">,
|
|
38
|
+
args: Record<string, unknown>,
|
|
39
|
+
) => {
|
|
40
|
+
if (++writes === failWrite) throw new Error("private database detail");
|
|
41
|
+
return t.mutation(ref, args);
|
|
42
|
+
},
|
|
43
|
+
} as unknown as RunCtx;
|
|
44
|
+
await client.recordClaim(ctx, {
|
|
45
|
+
accountId: scope.accountId,
|
|
46
|
+
scopeKey: scope.scopeKey,
|
|
47
|
+
currency: scope.currency,
|
|
48
|
+
claimKey: "claim",
|
|
49
|
+
sourceChargeId: "ch_original",
|
|
50
|
+
sourceGross: 1000,
|
|
51
|
+
sourceProcessingFee: 30,
|
|
52
|
+
amountDue: 30,
|
|
53
|
+
revision: 1,
|
|
54
|
+
});
|
|
55
|
+
const allocation = {
|
|
56
|
+
...(policyContext ? { policyContext } : {}),
|
|
57
|
+
sourceChargeId: "ch_source",
|
|
58
|
+
gross: 1000,
|
|
59
|
+
currency: "usd",
|
|
60
|
+
processingFee: 30,
|
|
61
|
+
platformNet: 40,
|
|
62
|
+
adjustmentAmount: 30,
|
|
63
|
+
transfers: twoLegs
|
|
64
|
+
? [
|
|
65
|
+
{
|
|
66
|
+
destinationAccountId: "acct_seller",
|
|
67
|
+
role: "store" as const,
|
|
68
|
+
amount: 800,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
destinationAccountId: "acct_other",
|
|
72
|
+
role: "other" as const,
|
|
73
|
+
amount: 100,
|
|
74
|
+
},
|
|
75
|
+
]
|
|
76
|
+
: [
|
|
77
|
+
{
|
|
78
|
+
destinationAccountId: "acct_seller",
|
|
79
|
+
role: "store" as const,
|
|
80
|
+
amount: 900,
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
};
|
|
84
|
+
await client.prepareGroup(ctx, {
|
|
85
|
+
...scope,
|
|
86
|
+
...(kind === "settlement"
|
|
87
|
+
? {
|
|
88
|
+
sourceKind: "settlement" as const,
|
|
89
|
+
sourceId: "ch_source",
|
|
90
|
+
sourceGross: 1000,
|
|
91
|
+
sourceProcessingFee: 30,
|
|
92
|
+
}
|
|
93
|
+
: { sourceKind: "invoice" as const, sourceId: "in_source" }),
|
|
94
|
+
maxDebit: 1000,
|
|
95
|
+
maxCredit: 0,
|
|
96
|
+
});
|
|
97
|
+
const op = await t.mutation(
|
|
98
|
+
nested.betterStripe.recoveryWorkflow.mutations.start,
|
|
99
|
+
{
|
|
100
|
+
...scope,
|
|
101
|
+
requestKey: "request",
|
|
102
|
+
provider: "function://fixture/provider",
|
|
103
|
+
providerApiVersion: "2026-09-30.endive",
|
|
104
|
+
definitionVersion: 1,
|
|
105
|
+
releaseUncollectible,
|
|
106
|
+
maxAttempts: 2,
|
|
107
|
+
retryDelayMs: 1,
|
|
108
|
+
...(kind === "settlement" ? { allocation } : {}),
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
const request: RecoveryProviderRequest = {
|
|
112
|
+
...scope,
|
|
113
|
+
operationId: op.operationId,
|
|
114
|
+
version: op.version,
|
|
115
|
+
phase: "execute",
|
|
116
|
+
providerApiVersion: "2026-09-30.endive",
|
|
117
|
+
definitionVersion: 1,
|
|
118
|
+
};
|
|
119
|
+
if (dispatch)
|
|
120
|
+
await t.mutation(nested.betterStripe.recoveryWorkflow.steps.beginProvider, {
|
|
121
|
+
...scope,
|
|
122
|
+
operationId: op.operationId,
|
|
123
|
+
version: 1,
|
|
124
|
+
phase: "execute",
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
dispatchReconcile: () =>
|
|
128
|
+
t.mutation(nested.betterStripe.recoveryWorkflow.steps.beginProvider, {
|
|
129
|
+
...scope,
|
|
130
|
+
operationId: op.operationId,
|
|
131
|
+
version: 1,
|
|
132
|
+
phase: "reconcile",
|
|
133
|
+
}),
|
|
134
|
+
t,
|
|
135
|
+
client,
|
|
136
|
+
ctx,
|
|
137
|
+
request,
|
|
138
|
+
group: (await client.getGroup(ctx, { groupKey: scope.groupKey }))!,
|
|
139
|
+
failNext: (offset = 1) => {
|
|
140
|
+
failWrite = writes + offset;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -1,145 +1,7 @@
|
|
|
1
1
|
import Stripe from "stripe";
|
|
2
|
-
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
-
import { componentsGeneric, type FunctionReference } from "convex/server";
|
|
4
2
|
import { expect, it } from "vitest";
|
|
5
|
-
import {
|
|
6
|
-
|
|
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
|
-
twoLegs = false,
|
|
23
|
-
) {
|
|
24
|
-
const t = initConvexTest();
|
|
25
|
-
t.registerComponent("betterStripe", schema, modules);
|
|
26
|
-
workflowTest.register(t, "betterStripe/workflow");
|
|
27
|
-
const component = nested.betterStripe as unknown as Component;
|
|
28
|
-
const client = new CostRecoveryClient(component);
|
|
29
|
-
let writes = 0,
|
|
30
|
-
failWrite = 0;
|
|
31
|
-
const ctx = {
|
|
32
|
-
runQuery: (
|
|
33
|
-
ref: FunctionReference<"query">,
|
|
34
|
-
args: Record<string, unknown>,
|
|
35
|
-
) => t.query(ref, args),
|
|
36
|
-
runMutation: (
|
|
37
|
-
ref: FunctionReference<"mutation">,
|
|
38
|
-
args: Record<string, unknown>,
|
|
39
|
-
) => {
|
|
40
|
-
if (++writes === failWrite) throw new Error("private database detail");
|
|
41
|
-
return t.mutation(ref, args);
|
|
42
|
-
},
|
|
43
|
-
} as unknown as RunCtx;
|
|
44
|
-
await client.recordClaim(ctx, {
|
|
45
|
-
accountId: scope.accountId,
|
|
46
|
-
scopeKey: scope.scopeKey,
|
|
47
|
-
currency: scope.currency,
|
|
48
|
-
claimKey: "claim",
|
|
49
|
-
sourceChargeId: "ch_original",
|
|
50
|
-
sourceGross: 1000,
|
|
51
|
-
sourceProcessingFee: 30,
|
|
52
|
-
amountDue: 30,
|
|
53
|
-
revision: 1,
|
|
54
|
-
});
|
|
55
|
-
const allocation = {
|
|
56
|
-
sourceChargeId: "ch_source",
|
|
57
|
-
gross: 1000,
|
|
58
|
-
currency: "usd",
|
|
59
|
-
processingFee: 30,
|
|
60
|
-
platformNet: 40,
|
|
61
|
-
adjustmentAmount: 30,
|
|
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
|
-
],
|
|
82
|
-
};
|
|
83
|
-
await client.prepareGroup(ctx, {
|
|
84
|
-
...scope,
|
|
85
|
-
...(kind === "settlement"
|
|
86
|
-
? {
|
|
87
|
-
sourceKind: "settlement" as const,
|
|
88
|
-
sourceId: "ch_source",
|
|
89
|
-
sourceGross: 1000,
|
|
90
|
-
sourceProcessingFee: 30,
|
|
91
|
-
}
|
|
92
|
-
: { sourceKind: "invoice" as const, sourceId: "in_source" }),
|
|
93
|
-
maxDebit: 1000,
|
|
94
|
-
maxCredit: 0,
|
|
95
|
-
});
|
|
96
|
-
const op = await t.mutation(
|
|
97
|
-
nested.betterStripe.recoveryWorkflow.mutations.start,
|
|
98
|
-
{
|
|
99
|
-
...scope,
|
|
100
|
-
requestKey: "request",
|
|
101
|
-
provider: "function://fixture/provider",
|
|
102
|
-
providerApiVersion: "2026-09-30.endive",
|
|
103
|
-
definitionVersion: 1,
|
|
104
|
-
releaseUncollectible,
|
|
105
|
-
maxAttempts: 2,
|
|
106
|
-
retryDelayMs: 1,
|
|
107
|
-
...(kind === "settlement" ? { allocation } : {}),
|
|
108
|
-
},
|
|
109
|
-
);
|
|
110
|
-
const request: RecoveryProviderRequest = {
|
|
111
|
-
...scope,
|
|
112
|
-
operationId: op.operationId,
|
|
113
|
-
version: op.version,
|
|
114
|
-
phase: "execute",
|
|
115
|
-
providerApiVersion: "2026-09-30.endive",
|
|
116
|
-
definitionVersion: 1,
|
|
117
|
-
};
|
|
118
|
-
if (dispatch)
|
|
119
|
-
await t.mutation(nested.betterStripe.recoveryWorkflow.steps.beginProvider, {
|
|
120
|
-
...scope,
|
|
121
|
-
operationId: op.operationId,
|
|
122
|
-
version: 1,
|
|
123
|
-
phase: "execute",
|
|
124
|
-
});
|
|
125
|
-
return {
|
|
126
|
-
dispatchReconcile: () =>
|
|
127
|
-
t.mutation(nested.betterStripe.recoveryWorkflow.steps.beginProvider, {
|
|
128
|
-
...scope,
|
|
129
|
-
operationId: op.operationId,
|
|
130
|
-
version: 1,
|
|
131
|
-
phase: "reconcile",
|
|
132
|
-
}),
|
|
133
|
-
t,
|
|
134
|
-
client,
|
|
135
|
-
ctx,
|
|
136
|
-
request,
|
|
137
|
-
group: (await client.getGroup(ctx, { groupKey: scope.groupKey }))!,
|
|
138
|
-
failNext: (offset = 1) => {
|
|
139
|
-
failWrite = writes + offset;
|
|
140
|
-
},
|
|
141
|
-
};
|
|
142
|
-
}
|
|
3
|
+
import { nested, scope, setup } from "./provider.fixture.js";
|
|
4
|
+
|
|
143
5
|
it("recovers invoice-item provider success/database failure with the real Stripe SDK and original key", async () => {
|
|
144
6
|
const f = await setup();
|
|
145
7
|
let status = "draft";
|
|
@@ -564,3 +426,84 @@ it.each([false, true])(
|
|
|
564
426
|
]);
|
|
565
427
|
},
|
|
566
428
|
);
|
|
429
|
+
|
|
430
|
+
it("keeps saved policy context in genuine recovery and rejects a conflicting operation binding", async () => {
|
|
431
|
+
const policyContext = {
|
|
432
|
+
namespace: "host.sale-policy",
|
|
433
|
+
version: 1 as const,
|
|
434
|
+
fields: { planVersion: "original-v2" },
|
|
435
|
+
};
|
|
436
|
+
const f = await setup("settlement", true, false, false, policyContext);
|
|
437
|
+
const snapshot = await f.t.query(
|
|
438
|
+
nested.betterStripe.recoveryWorkflow.queries.snapshot,
|
|
439
|
+
{ ...scope, operationId: f.request.operationId, version: 1 },
|
|
440
|
+
);
|
|
441
|
+
expect(snapshot.allocation).toMatchObject({ policyContext });
|
|
442
|
+
const conflict = await f.t.mutation(
|
|
443
|
+
nested.betterStripe.recoveryWorkflow.mutations.start,
|
|
444
|
+
{
|
|
445
|
+
...scope,
|
|
446
|
+
requestKey: "request-conflicting-policy",
|
|
447
|
+
provider: "function://fixture/provider",
|
|
448
|
+
providerApiVersion: "2026-09-30.endive",
|
|
449
|
+
definitionVersion: 1,
|
|
450
|
+
releaseUncollectible: false,
|
|
451
|
+
maxAttempts: 2,
|
|
452
|
+
retryDelayMs: 1,
|
|
453
|
+
allocation: {
|
|
454
|
+
...snapshot.allocation,
|
|
455
|
+
policyContext: {
|
|
456
|
+
...policyContext,
|
|
457
|
+
fields: { planVersion: "today-v3" },
|
|
458
|
+
},
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
);
|
|
462
|
+
expect(conflict).toMatchObject({
|
|
463
|
+
status: "conflict",
|
|
464
|
+
reason: "identity_changed",
|
|
465
|
+
});
|
|
466
|
+
let creates = 0;
|
|
467
|
+
const phases: string[] = [];
|
|
468
|
+
const httpClient = Stripe.createFetchHttpClient(async (url, init) => {
|
|
469
|
+
const path = new URL(String(url)).pathname;
|
|
470
|
+
let body: unknown;
|
|
471
|
+
if (path === "/v1/charges/ch_source" && init?.method === "GET")
|
|
472
|
+
body = { id: "ch_source", created: 1 };
|
|
473
|
+
else if (path === "/v1/transfers" && init?.method === "GET")
|
|
474
|
+
body = { object: "list", data: [], has_more: false };
|
|
475
|
+
else if (path === "/v1/transfers" && init?.method === "POST") {
|
|
476
|
+
creates++;
|
|
477
|
+
body = {
|
|
478
|
+
id: "tr_context",
|
|
479
|
+
destination: "acct_seller",
|
|
480
|
+
amount: 900,
|
|
481
|
+
currency: "usd",
|
|
482
|
+
source_transaction: "ch_source",
|
|
483
|
+
metadata: { bsRole: "store", bsSourceCharge: "ch_source" },
|
|
484
|
+
};
|
|
485
|
+
} else throw new Error("Unlisted synthetic provider request");
|
|
486
|
+
return new Response(JSON.stringify(body), {
|
|
487
|
+
status: 200,
|
|
488
|
+
headers: { "content-type": "application/json" },
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
expect(
|
|
492
|
+
await f.client.executeGroupProvider(f.ctx, f.request, {
|
|
493
|
+
stripeSecretKey: "sk_test_runtime_only",
|
|
494
|
+
httpClient,
|
|
495
|
+
authorize: async (context) => {
|
|
496
|
+
phases.push(context.phase);
|
|
497
|
+
expect(context.settlement).toMatchObject({ policyContext });
|
|
498
|
+
if (context.phase !== "transfer")
|
|
499
|
+
throw new Error("Group admission already occurred");
|
|
500
|
+
expect(context.transfer).toMatchObject({
|
|
501
|
+
destinationAccountId: "acct_seller",
|
|
502
|
+
amount: 900,
|
|
503
|
+
});
|
|
504
|
+
},
|
|
505
|
+
}),
|
|
506
|
+
).toMatchObject({ status: "ready", outcome: "collect" });
|
|
507
|
+
expect(phases).toEqual(["transfer"]);
|
|
508
|
+
expect(creates).toBe(1);
|
|
509
|
+
});
|
|
@@ -27,6 +27,7 @@ export async function authorizeSettlement(
|
|
|
27
27
|
) {
|
|
28
28
|
if (!authorize) return;
|
|
29
29
|
const source = structuredClone({
|
|
30
|
+
policyContext: settlement.policyContext,
|
|
30
31
|
sourceChargeId: settlement.sourceChargeId,
|
|
31
32
|
paymentIntentId: settlement.paymentIntentId,
|
|
32
33
|
sourceInvoiceId: settlement.sourceInvoiceId,
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
policyContextBinding,
|
|
3
|
+
type SettlementPolicyContext,
|
|
4
|
+
} from "../../shared/settlementPolicyContext.js";
|
|
1
5
|
import { reconcileAffiliateCharge } from "../affiliateRecovery/engine.js";
|
|
2
6
|
import {
|
|
3
7
|
sourceTransfers,
|
|
@@ -14,6 +18,7 @@ import {
|
|
|
14
18
|
type SettlementAuthorization,
|
|
15
19
|
} from "./authorization.js";
|
|
16
20
|
export type ResolvedSettlement = {
|
|
21
|
+
policyContext?: SettlementPolicyContext;
|
|
17
22
|
sourceChargeId: string;
|
|
18
23
|
paymentIntentId?: string;
|
|
19
24
|
sourceInvoiceId?: string;
|
|
@@ -71,12 +76,14 @@ export async function executeResolvedTransfers(
|
|
|
71
76
|
opts: ResolvedSettlement,
|
|
72
77
|
authorize?: SettlementAuthorization,
|
|
73
78
|
): Promise<SettlementResult> {
|
|
79
|
+
policyContextBinding(opts.policyContext);
|
|
74
80
|
const saved = (await runMutationOrThrow(
|
|
75
81
|
ctx,
|
|
76
82
|
componentRef(component, "settlement/mutations/reserveSettlementAllocation"),
|
|
77
83
|
opts,
|
|
78
84
|
)) as ResolvedSettlement;
|
|
79
85
|
const claim: ResolvedSettlement = {
|
|
86
|
+
policyContext: saved.policyContext,
|
|
80
87
|
sourceChargeId: saved.sourceChargeId,
|
|
81
88
|
paymentIntentId: saved.paymentIntentId,
|
|
82
89
|
sourceInvoiceId: saved.sourceInvoiceId,
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import { authorizationFixture } from "./authorization.fixture.js";
|
|
4
|
+
import type { SettlementPolicyContext } from "../../shared/settlementPolicyContext.js";
|
|
5
|
+
import type { SettlementAuthorizationContext } from "./authorization.js";
|
|
6
|
+
|
|
7
|
+
afterEach(() => vi.restoreAllMocks());
|
|
8
|
+
|
|
9
|
+
const policyContext = {
|
|
10
|
+
namespace: "host.sale-policy",
|
|
11
|
+
version: 1 as const,
|
|
12
|
+
fields: { planVersion: "plan-v3", originalOwner: "seller-original" },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
it("retains ordinary public no-context settlement and the actual registered allocation", async () => {
|
|
16
|
+
const f = await authorizationFixture();
|
|
17
|
+
expect(await f.settle()).toMatchObject({
|
|
18
|
+
status: "settled",
|
|
19
|
+
executedTransfers: [{ amount: 940 }],
|
|
20
|
+
});
|
|
21
|
+
expect(await f.allocation()).toMatchObject({
|
|
22
|
+
sourceChargeId: "ch_authorize",
|
|
23
|
+
gross: 1000,
|
|
24
|
+
transfers: [{ destinationAccountId: "acct_creator", amount: 940 }],
|
|
25
|
+
});
|
|
26
|
+
expect(f.transfers).toHaveLength(1);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("persists opt-in policy context through actual component reserve and public read", async () => {
|
|
30
|
+
const f = await authorizationFixture();
|
|
31
|
+
const allocation = { ...f.options.allocation, policyContext };
|
|
32
|
+
await expect(f.reserve(allocation)).resolves.toMatchObject({ policyContext });
|
|
33
|
+
expect(await f.allocation()).toMatchObject({ policyContext });
|
|
34
|
+
const reordered = {
|
|
35
|
+
...allocation,
|
|
36
|
+
policyContext: {
|
|
37
|
+
...policyContext,
|
|
38
|
+
fields: { originalOwner: "seller-original", planVersion: "plan-v3" },
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
await expect(f.reserve(reordered)).resolves.toMatchObject({ policyContext });
|
|
42
|
+
expect(f.transfers).toHaveLength(0);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("includes the validated proposal context in initial public reserve admission before persistence", async () => {
|
|
46
|
+
const f = await authorizationFixture();
|
|
47
|
+
const contexts: SettlementAuthorizationContext[] = [];
|
|
48
|
+
const input = {
|
|
49
|
+
...f.options,
|
|
50
|
+
allocation: { ...f.options.allocation, policyContext },
|
|
51
|
+
authorize: async (context: SettlementAuthorizationContext) => {
|
|
52
|
+
if (contexts.length === 0) expect(await f.allocation()).toBeNull();
|
|
53
|
+
contexts.push(context);
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const outcome = await f.settle(input).then(
|
|
57
|
+
(result) => ({ result }),
|
|
58
|
+
(error: unknown) => ({ error }),
|
|
59
|
+
);
|
|
60
|
+
expect(contexts[0]).toMatchObject({
|
|
61
|
+
phase: "reserve",
|
|
62
|
+
settlement: { policyContext },
|
|
63
|
+
});
|
|
64
|
+
expect(outcome).toMatchObject({ result: { status: "settled" } });
|
|
65
|
+
expect(contexts[1]).toMatchObject({
|
|
66
|
+
phase: "transfer",
|
|
67
|
+
settlement: { policyContext },
|
|
68
|
+
});
|
|
69
|
+
expect(await f.allocation()).toMatchObject({ policyContext });
|
|
70
|
+
expect(f.transfers).toHaveLength(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("rejects a changed or missing context without changing the first allocation", async () => {
|
|
74
|
+
const f = await authorizationFixture();
|
|
75
|
+
const allocation = { ...f.options.allocation, policyContext };
|
|
76
|
+
const original = await f.reserve(allocation);
|
|
77
|
+
for (const changed of [
|
|
78
|
+
{ ...policyContext, namespace: "foreign.policy" },
|
|
79
|
+
{
|
|
80
|
+
...policyContext,
|
|
81
|
+
fields: { ...policyContext.fields, planVersion: "v4" },
|
|
82
|
+
},
|
|
83
|
+
undefined,
|
|
84
|
+
]) {
|
|
85
|
+
await expect(
|
|
86
|
+
f.reserve({ ...allocation, policyContext: changed }),
|
|
87
|
+
).rejects.toThrow(/policy context/i);
|
|
88
|
+
expect(await f.allocation()).toEqual(original);
|
|
89
|
+
}
|
|
90
|
+
expect(f.transfers).toHaveLength(0);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("never backfills a legacy allocation with a new context", async () => {
|
|
94
|
+
const f = await authorizationFixture();
|
|
95
|
+
const original = await f.reserve(f.options.allocation);
|
|
96
|
+
await expect(
|
|
97
|
+
f.reserve({ ...f.options.allocation, policyContext }),
|
|
98
|
+
).rejects.toThrow(/policy context/i);
|
|
99
|
+
expect(await f.allocation()).toEqual(original);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("rejects oversized context at public entry before any synthetic provider request", async () => {
|
|
103
|
+
const f = await authorizationFixture();
|
|
104
|
+
const fields = Object.fromEntries(
|
|
105
|
+
Array.from({ length: 32 }, (_, i) => [`key${i}`, "x".repeat(512)]),
|
|
106
|
+
);
|
|
107
|
+
await expect(
|
|
108
|
+
f.settle({
|
|
109
|
+
...f.options,
|
|
110
|
+
allocation: {
|
|
111
|
+
...f.options.allocation,
|
|
112
|
+
policyContext: { ...policyContext, fields },
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
).rejects.toThrow(/policy context/i);
|
|
116
|
+
expect(f.requests).toHaveLength(0);
|
|
117
|
+
expect(await f.allocation()).toBeNull();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("keeps the actual concurrent winner's context and prevents a losing request's POST", async () => {
|
|
121
|
+
const f = await authorizationFixture();
|
|
122
|
+
const other = { ...policyContext, fields: { planVersion: "winner" } };
|
|
123
|
+
const input = {
|
|
124
|
+
...f.options,
|
|
125
|
+
allocation: { ...f.options.allocation, policyContext },
|
|
126
|
+
authorize: async (context: SettlementAuthorizationContext) => {
|
|
127
|
+
if (context.phase === "reserve")
|
|
128
|
+
await f.reserve({ ...f.options.allocation, policyContext: other });
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
await expect(f.settle(input)).rejects.toThrow(/policy context/i);
|
|
132
|
+
expect(await f.allocation()).toMatchObject({ policyContext: other });
|
|
133
|
+
expect(f.transfers).toHaveLength(0);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("detaches nested context so callback mutation cannot alter the saved claim", async () => {
|
|
137
|
+
const f = await authorizationFixture();
|
|
138
|
+
const seen: SettlementAuthorizationContext[] = [];
|
|
139
|
+
const input = {
|
|
140
|
+
...f.options,
|
|
141
|
+
allocation: { ...f.options.allocation, policyContext },
|
|
142
|
+
authorize: async (context: SettlementAuthorizationContext) => {
|
|
143
|
+
seen.push(structuredClone(context));
|
|
144
|
+
const copy = context.settlement;
|
|
145
|
+
if ("policyContext" in copy && copy.policyContext)
|
|
146
|
+
Object.assign(copy.policyContext.fields, { planVersion: "attacker" });
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
expect(await f.settle(input)).toMatchObject({
|
|
150
|
+
status: "settled",
|
|
151
|
+
policyContext,
|
|
152
|
+
});
|
|
153
|
+
expect(await f.allocation()).toMatchObject({ policyContext });
|
|
154
|
+
expect(seen).toHaveLength(2);
|
|
155
|
+
for (const context of seen)
|
|
156
|
+
expect(context.settlement).toMatchObject({ policyContext });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("resumes only the missing leg with its original context after current policy changes", async () => {
|
|
160
|
+
const f = await authorizationFixture(true);
|
|
161
|
+
f.stopAfterFirst();
|
|
162
|
+
const input = {
|
|
163
|
+
...f.options,
|
|
164
|
+
allocation: { ...f.options.allocation, policyContext },
|
|
165
|
+
authorize: async () => f.authorize(),
|
|
166
|
+
};
|
|
167
|
+
await expect(f.settle(input)).rejects.toThrow(
|
|
168
|
+
"Host settlement authorization denied",
|
|
169
|
+
);
|
|
170
|
+
expect(f.transfers).toHaveLength(1);
|
|
171
|
+
f.allow();
|
|
172
|
+
await expect(
|
|
173
|
+
f.settle({
|
|
174
|
+
...input,
|
|
175
|
+
allocation: {
|
|
176
|
+
...input.allocation,
|
|
177
|
+
policyContext: {
|
|
178
|
+
...policyContext,
|
|
179
|
+
fields: { planVersion: "today-v4" },
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
).rejects.toThrow(/policy context/i);
|
|
184
|
+
expect(f.transfers).toHaveLength(1);
|
|
185
|
+
expect(await f.settle(input)).toMatchObject({
|
|
186
|
+
status: "settled",
|
|
187
|
+
policyContext,
|
|
188
|
+
});
|
|
189
|
+
expect(f.transfers).toHaveLength(2);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const invalidContexts: SettlementPolicyContext[] = [
|
|
193
|
+
{ ...policyContext, fields: { $reserved: "value" } },
|
|
194
|
+
{ ...policyContext, fields: { "line\nbreak": "value" } },
|
|
195
|
+
{ ...policyContext, fields: { é: "value" } },
|
|
196
|
+
{ ...policyContext, namespace: "" },
|
|
197
|
+
{ ...policyContext, namespace: "n".repeat(81) },
|
|
198
|
+
{ ...policyContext, fields: { "": "value" } },
|
|
199
|
+
{ ...policyContext, fields: { ["k".repeat(65)]: "value" } },
|
|
200
|
+
{ ...policyContext, fields: { key: "v".repeat(513) } },
|
|
201
|
+
{
|
|
202
|
+
...policyContext,
|
|
203
|
+
fields: Object.fromEntries(
|
|
204
|
+
Array.from({ length: 33 }, (_, i) => [`k${i}`, "v"]),
|
|
205
|
+
),
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
...policyContext,
|
|
209
|
+
fields: Object.fromEntries(
|
|
210
|
+
Array.from({ length: 8 }, (_, i) => [`k${i}`, "€".repeat(512)]),
|
|
211
|
+
),
|
|
212
|
+
},
|
|
213
|
+
];
|
|
214
|
+
it.each(invalidContexts)(
|
|
215
|
+
"rejects invalid context before public I/O and raw component persistence %#",
|
|
216
|
+
async (invalid) => {
|
|
217
|
+
const f = await authorizationFixture();
|
|
218
|
+
const allocation = { ...f.options.allocation, policyContext: invalid };
|
|
219
|
+
await expect(f.settle({ ...f.options, allocation })).rejects.toThrow();
|
|
220
|
+
expect(f.requests).toHaveLength(0);
|
|
221
|
+
await expect(f.reserve(allocation)).rejects.toThrow();
|
|
222
|
+
expect(await f.allocation()).toBeNull();
|
|
223
|
+
},
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
it("accepts valid maximum field/key/value bounds and an exactly 8 KiB canonical envelope", async () => {
|
|
227
|
+
const f = await authorizationFixture();
|
|
228
|
+
const bounded = {
|
|
229
|
+
namespace: "n".repeat(80),
|
|
230
|
+
version: 1 as const,
|
|
231
|
+
fields: Object.fromEntries(
|
|
232
|
+
Array.from({ length: 32 }, (_, i) => [String(i).padEnd(64, "k"), "v"]),
|
|
233
|
+
),
|
|
234
|
+
};
|
|
235
|
+
await expect(
|
|
236
|
+
f.reserve({ ...f.options.allocation, policyContext: bounded }),
|
|
237
|
+
).resolves.toMatchObject({ policyContext: bounded });
|
|
238
|
+
const g = await authorizationFixture();
|
|
239
|
+
const fields: Record<string, string> = Object.fromEntries(
|
|
240
|
+
Array.from({ length: 15 }, (_, i) => [`k${i}`, "v".repeat(512)]),
|
|
241
|
+
);
|
|
242
|
+
fields.last = "";
|
|
243
|
+
const exact = { namespace: "host", version: 1 as const, fields };
|
|
244
|
+
const bytes = new TextEncoder().encode(JSON.stringify(exact)).byteLength;
|
|
245
|
+
fields.last = "v".repeat(8192 - bytes);
|
|
246
|
+
expect(fields.last.length).toBeLessThanOrEqual(512);
|
|
247
|
+
expect(new TextEncoder().encode(JSON.stringify(exact)).byteLength).toBe(8192);
|
|
248
|
+
await expect(
|
|
249
|
+
g.reserve({ ...g.options.allocation, policyContext: exact }),
|
|
250
|
+
).resolves.toMatchObject({ policyContext: exact });
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it.each([
|
|
254
|
+
Object.assign(new Date(0), policyContext),
|
|
255
|
+
null,
|
|
256
|
+
{ ...policyContext, version: 2 },
|
|
257
|
+
{ ...policyContext, namespace: 42 },
|
|
258
|
+
{ ...policyContext, extra: "not in the envelope" },
|
|
259
|
+
{ ...policyContext, fields: [] },
|
|
260
|
+
{ ...policyContext, fields: { planVersion: 42 } },
|
|
261
|
+
{ ...policyContext, fields: new Date(0) },
|
|
262
|
+
])(
|
|
263
|
+
"rejects malformed JavaScript context before I/O without relying on TypeScript %#",
|
|
264
|
+
async (raw) => {
|
|
265
|
+
const f = await authorizationFixture();
|
|
266
|
+
// Reflect invokes the actual JS boundary with malformed values, without changing public types.
|
|
267
|
+
const allocation = { ...f.options.allocation, policyContext: raw };
|
|
268
|
+
await expect(
|
|
269
|
+
Reflect.apply(f.settle, undefined, [{ ...f.options, allocation }]),
|
|
270
|
+
).rejects.toThrow();
|
|
271
|
+
expect(f.requests).toHaveLength(0);
|
|
272
|
+
await expect(
|
|
273
|
+
Reflect.apply(f.reserve, undefined, [allocation]),
|
|
274
|
+
).rejects.toThrow();
|
|
275
|
+
expect(await f.allocation()).toBeNull();
|
|
276
|
+
},
|
|
277
|
+
);
|