@fantastic.dev/better-stripe 0.22.0 → 0.23.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 +6 -0
- package/README.md +5 -3
- package/dist/client/connect/payoutActivationProof.d.ts +16 -0
- package/dist/client/connect/payoutActivationProof.d.ts.map +1 -0
- package/dist/client/connect/payoutActivationProof.js +72 -0
- package/dist/client/connect/payoutActivationProof.js.map +1 -0
- package/dist/client/connect/payoutAdoption.d.ts +35 -0
- package/dist/client/connect/payoutAdoption.d.ts.map +1 -0
- package/dist/client/connect/payoutAdoption.js +230 -0
- package/dist/client/connect/payoutAdoption.js.map +1 -0
- package/dist/client/connect/payoutAdoptionAge.d.ts +19 -0
- package/dist/client/connect/payoutAdoptionAge.d.ts.map +1 -0
- package/dist/client/connect/payoutAdoptionAge.js +56 -0
- package/dist/client/connect/payoutAdoptionAge.js.map +1 -0
- package/dist/client/connect/payoutAdoptionAllocation.d.ts +24 -0
- package/dist/client/connect/payoutAdoptionAllocation.d.ts.map +1 -0
- package/dist/client/connect/payoutAdoptionAllocation.js +78 -0
- package/dist/client/connect/payoutAdoptionAllocation.js.map +1 -0
- package/dist/client/connect/payoutReconciliation.d.ts.map +1 -1
- package/dist/client/connect/payoutReconciliation.js +4 -2
- package/dist/client/connect/payoutReconciliation.js.map +1 -1
- package/dist/client/payoutPolicy.d.ts +7 -1
- package/dist/client/payoutPolicy.d.ts.map +1 -1
- package/dist/client/payoutPolicy.js +78 -7
- package/dist/client/payoutPolicy.js.map +1 -1
- package/dist/component/_generated/component.d.ts +196 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +38 -4
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/payoutPolicy/adoption.d.ts +9 -0
- package/dist/component/payoutPolicy/adoption.d.ts.map +1 -0
- package/dist/component/payoutPolicy/adoption.js +45 -0
- package/dist/component/payoutPolicy/adoption.js.map +1 -0
- package/dist/component/payoutPolicy/mutations.d.ts +96 -0
- package/dist/component/payoutPolicy/mutations.d.ts.map +1 -1
- package/dist/component/payoutPolicy/mutations.js +38 -5
- package/dist/component/payoutPolicy/mutations.js.map +1 -1
- package/dist/component/payoutPolicy/queries.d.ts +16 -0
- package/dist/component/payoutPolicy/queries.d.ts.map +1 -1
- package/dist/component/payoutPolicy/schema.d.ts +299 -2
- package/dist/component/payoutPolicy/schema.d.ts.map +1 -1
- package/dist/component/payoutPolicy/schema.js +7 -0
- package/dist/component/payoutPolicy/schema.js.map +1 -1
- package/dist/component/payoutRelease/adoption.d.ts +269 -0
- package/dist/component/payoutRelease/adoption.d.ts.map +1 -0
- package/dist/component/payoutRelease/adoption.js +197 -0
- package/dist/component/payoutRelease/adoption.js.map +1 -0
- package/dist/component/payoutRelease/helpers.d.ts +3 -0
- package/dist/component/payoutRelease/helpers.d.ts.map +1 -1
- package/dist/component/payoutRelease/helpers.js +20 -0
- package/dist/component/payoutRelease/helpers.js.map +1 -1
- package/dist/component/payoutRelease/mutations.d.ts +81 -0
- package/dist/component/payoutRelease/mutations.d.ts.map +1 -1
- package/dist/component/payoutRelease/mutations.js +6 -0
- package/dist/component/payoutRelease/mutations.js.map +1 -1
- package/dist/component/payoutRelease/observation.d.ts +18 -0
- package/dist/component/payoutRelease/observation.d.ts.map +1 -0
- package/dist/component/payoutRelease/observation.js +11 -0
- package/dist/component/payoutRelease/observation.js.map +1 -0
- package/dist/component/payoutRelease/queries.d.ts +3 -0
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/payoutRelease/schema.d.ts +8 -19
- package/dist/component/payoutRelease/schema.d.ts.map +1 -1
- package/dist/component/payoutRelease/schema.js +3 -9
- package/dist/component/payoutRelease/schema.js.map +1 -1
- package/dist/component/schema.d.ts +73 -2
- package/docs/settlement.md +121 -40
- package/package.json +1 -1
- package/src/client/connect/payoutActivationProof.ts +101 -0
- package/src/client/connect/payoutAdoption.test.ts +421 -0
- package/src/client/connect/payoutAdoption.ts +330 -0
- package/src/client/connect/payoutAdoptionAge.test.ts +89 -0
- package/src/client/connect/payoutAdoptionAge.ts +74 -0
- package/src/client/connect/payoutAdoptionAllocation.test.ts +132 -0
- package/src/client/connect/payoutAdoptionAllocation.ts +106 -0
- package/src/client/connect/payoutReconciliation.test.ts +16 -0
- package/src/client/connect/payoutReconciliation.ts +6 -2
- package/src/client/payoutPolicy.test.ts +16 -0
- package/src/client/payoutPolicy.ts +107 -5
- package/src/component/_generated/api.ts +6 -0
- package/src/component/_generated/component.ts +192 -1
- package/src/component/payout-adoption.test.ts +272 -0
- package/src/component/payout-policy-adoption.test.ts +131 -0
- package/src/component/payoutPolicy/adoption.ts +76 -0
- package/src/component/payoutPolicy/mutations.ts +46 -3
- package/src/component/payoutPolicy/schema.ts +9 -0
- package/src/component/payoutRelease/adoption.ts +262 -0
- package/src/component/payoutRelease/helpers.ts +21 -0
- package/src/component/payoutRelease/mutations.ts +10 -0
- package/src/component/payoutRelease/observation.ts +10 -0
- package/src/component/payoutRelease/schema.ts +3 -9
package/CHANGELOG.md
CHANGED
|
@@ -14,6 +14,12 @@
|
|
|
14
14
|
- Remove internal tracker references from public documentation and add a
|
|
15
15
|
regression check that prevents their reintroduction.
|
|
16
16
|
|
|
17
|
+
## [0.23.0](https://github.com/FantasticDevHQ/better-stripe/compare/v0.22.0...v0.23.0) (2026-10-15)
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
- **payouts:** adopt historical funding and activate funded policies ([#192](https://github.com/FantasticDevHQ/better-stripe/issues/192)) ([abc73dc](https://github.com/FantasticDevHQ/better-stripe/commit/abc73dccd6398bc6adfab58255e26bbeaa54ab96))
|
|
22
|
+
|
|
17
23
|
## [0.22.0](https://github.com/FantasticDevHQ/better-stripe/compare/v0.21.0...v0.22.0) (2026-10-15)
|
|
18
24
|
|
|
19
25
|
### Features
|
package/README.md
CHANGED
|
@@ -48,12 +48,14 @@ provider outcome, and books its net payout fees once.
|
|
|
48
48
|
The [combined release API](docs/settlement.md#combined-recipient-release) verifies
|
|
49
49
|
complete source and payout history, reserves mature unpaid funds, and creates an
|
|
50
50
|
idempotent bank payout. Install a host job to invoke it and expire due policies.
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
[Historical imports and explicit funded activation](docs/settlement.md#historical-import-and-funded-activation)
|
|
52
|
+
verify old payouts, source ownership and holding age before enabling a custom
|
|
53
|
+
schedule. Larger histories and unallocated money still require reconciliation;
|
|
54
|
+
schedule controls alone do not enforce a minimum.
|
|
53
55
|
|
|
54
56
|
## Status
|
|
55
57
|
|
|
56
|
-
**Version**: `0.
|
|
58
|
+
**Version**: `0.23.0` <!-- x-release-please-version --> (pre-1.0). API may change between minor versions until 1.0.
|
|
57
59
|
|
|
58
60
|
Used in production by its authors. Webhook pipeline is covered by unit tests and a live E2E harness (`npm run e2e:webhooks`) that fires real Stripe-signed events and asserts the ledger.
|
|
59
61
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import type { Infer } from "convex/values";
|
|
3
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
4
|
+
import type { adoptionProof } from "../../component/payoutPolicy/schema.js";
|
|
5
|
+
/** Complete read/reconcile proof. Unknown historical payouts require explicit import first. */
|
|
6
|
+
export declare function collectAdoptionProof(stripe: Stripe, component: Component, ctx: RunCtx, scope: {
|
|
7
|
+
accountId: string;
|
|
8
|
+
ownerKey: string;
|
|
9
|
+
}, expectedRevision: number, configuredCurrencies: string[]): Promise<{
|
|
10
|
+
status: "verified";
|
|
11
|
+
proof: Infer<typeof adoptionProof>;
|
|
12
|
+
} | {
|
|
13
|
+
status: "held";
|
|
14
|
+
reason: string;
|
|
15
|
+
}>;
|
|
16
|
+
//# sourceMappingURL=payoutActivationProof.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutActivationProof.d.ts","sourceRoot":"","sources":["../../../src/client/connect/payoutActivationProof.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AA+B5E,+FAA+F;AAC/F,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,EACpB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,EAC9C,gBAAgB,EAAE,MAAM,EACxB,oBAAoB,EAAE,MAAM,EAAE,GAC7B,OAAO,CACN;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC,OAAO,aAAa,CAAC,CAAA;CAAE,GAC1D;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CACrC,CAuDA"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { payoutClaimFingerprint } from "../../component/payoutRelease/helpers.js";
|
|
2
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
3
|
+
import { adoptRecipientPayouts } from "./payoutAdoption.js";
|
|
4
|
+
function balanceIdentity(b) {
|
|
5
|
+
return JSON.stringify([b.available, b.pending].map((rows) => rows
|
|
6
|
+
.map((t) => [
|
|
7
|
+
t.currency,
|
|
8
|
+
t.amount,
|
|
9
|
+
Object.entries(t.source_types ?? {}).sort(),
|
|
10
|
+
])
|
|
11
|
+
.sort((a, b) => String(a[0]).localeCompare(String(b[0])))));
|
|
12
|
+
}
|
|
13
|
+
function transactionIdentity(t) {
|
|
14
|
+
return t
|
|
15
|
+
? JSON.stringify([
|
|
16
|
+
t.id,
|
|
17
|
+
t.source,
|
|
18
|
+
t.amount,
|
|
19
|
+
t.net,
|
|
20
|
+
t.currency,
|
|
21
|
+
t.status,
|
|
22
|
+
t.available_on,
|
|
23
|
+
])
|
|
24
|
+
: "none";
|
|
25
|
+
}
|
|
26
|
+
/** Complete read/reconcile proof. Unknown historical payouts require explicit import first. */
|
|
27
|
+
export async function collectAdoptionProof(stripe, component, ctx, scope, expectedRevision, configuredCurrencies) {
|
|
28
|
+
const request = { stripeAccount: scope.accountId };
|
|
29
|
+
const initial = await stripe.balance.retrieve(undefined, request);
|
|
30
|
+
const first = await stripe.balanceTransactions.list({ limit: 1 }, request);
|
|
31
|
+
const queue = new Set([
|
|
32
|
+
...configuredCurrencies,
|
|
33
|
+
...initial.available.map((t) => t.currency),
|
|
34
|
+
...initial.pending.map((t) => t.currency),
|
|
35
|
+
...(first.data[0] ? [first.data[0].currency] : []),
|
|
36
|
+
]);
|
|
37
|
+
const completed = new Set();
|
|
38
|
+
let observations = [], oldestHeldAt;
|
|
39
|
+
for (const currency of queue) {
|
|
40
|
+
if (completed.size >= 50)
|
|
41
|
+
return { status: "held", reason: "currency_history_incomplete" };
|
|
42
|
+
const result = await adoptRecipientPayouts(stripe, component, ctx, { ...scope, currency, expectedRevision, payouts: [] }, true);
|
|
43
|
+
if (result.status !== "adopted")
|
|
44
|
+
return result;
|
|
45
|
+
completed.add(currency);
|
|
46
|
+
for (const c of result.historyCurrencies)
|
|
47
|
+
queue.add(c);
|
|
48
|
+
observations = result.observations;
|
|
49
|
+
if (result.oldestHeldAt !== undefined)
|
|
50
|
+
oldestHeldAt = Math.min(oldestHeldAt ?? Infinity, result.oldestHeldAt);
|
|
51
|
+
}
|
|
52
|
+
const balance = await stripe.balance.retrieve(undefined, request);
|
|
53
|
+
const latest = await stripe.balanceTransactions.list({ limit: 1 }, request);
|
|
54
|
+
if (balanceIdentity(initial) !== balanceIdentity(balance) ||
|
|
55
|
+
transactionIdentity(first.data[0]) !== transactionIdentity(latest.data[0]))
|
|
56
|
+
return { status: "held", reason: "provider_changed" };
|
|
57
|
+
const snapshot = (await ctx.runQuery(componentRef(component, "payoutRelease/queries/snapshot"), { accountId: scope.accountId, ownerKey: scope.ownerKey }));
|
|
58
|
+
if (snapshot.status !== "ready")
|
|
59
|
+
return snapshot;
|
|
60
|
+
return {
|
|
61
|
+
status: "verified",
|
|
62
|
+
proof: {
|
|
63
|
+
observations,
|
|
64
|
+
claims: snapshot.claims.map((c) => ({
|
|
65
|
+
operationKey: c.operationKey,
|
|
66
|
+
fingerprint: payoutClaimFingerprint(c),
|
|
67
|
+
})),
|
|
68
|
+
...(oldestHeldAt !== undefined ? { oldestHeldAt } : {}),
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=payoutActivationProof.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutActivationProof.js","sourceRoot":"","sources":["../../../src/client/connect/payoutActivationProof.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,SAAS,eAAe,CAAC,CAAiB;IACxC,OAAO,IAAI,CAAC,SAAS,CACnB,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CACpC,IAAI;SACD,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;QACV,CAAC,CAAC,QAAQ;QACV,CAAC,CAAC,MAAM;QACR,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE;KAC5C,CAAC;SACD,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,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,CAAC,CAAC,KAAK,mBAAmB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAE1E,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;IACxD,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,CAAC,QAAQ,CAClC,YAAY,CAAC,SAAS,EAAE,gCAAgC,CAAC,EACzD,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,CACzD,CAEqC,CAAC;IACvC,IAAI,QAAQ,CAAC,MAAM,KAAK,OAAO;QAAE,OAAO,QAAQ,CAAC;IACjD,OAAO;QACL,MAAM,EAAE,UAAU;QAClB,KAAK,EAAE;YACL,YAAY;YACZ,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBAClC,YAAY,EAAE,CAAC,CAAC,YAAY;gBAC5B,WAAW,EAAE,sBAAsB,CAAC,CAAC,CAAC;aACvC,CAAC,CAAC;YACH,GAAG,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACxD;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
import type { Infer } from "convex/values";
|
|
3
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
4
|
+
import type { claimDoc, observation } from "../../component/payoutRelease/schema.js";
|
|
5
|
+
type Claim = Infer<typeof claimDoc>;
|
|
6
|
+
export type PayoutAdoptionOptions = {
|
|
7
|
+
accountId: string;
|
|
8
|
+
ownerKey: string;
|
|
9
|
+
currency: string;
|
|
10
|
+
expectedRevision: number;
|
|
11
|
+
maxPages?: number;
|
|
12
|
+
/** Explicit authorized source accounting for historical payouts not already recorded by BetterStripe. */
|
|
13
|
+
payouts: {
|
|
14
|
+
stripePayoutId: string;
|
|
15
|
+
allocations: {
|
|
16
|
+
stripeTransferId: string;
|
|
17
|
+
amount: number;
|
|
18
|
+
}[];
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
export type PayoutAdoptionResult = {
|
|
22
|
+
status: "adopted";
|
|
23
|
+
claims: Claim[];
|
|
24
|
+
observedAt: number;
|
|
25
|
+
observations: Infer<typeof observation>[];
|
|
26
|
+
oldestHeldAt?: number;
|
|
27
|
+
historyCurrencies: string[];
|
|
28
|
+
} | {
|
|
29
|
+
status: "held";
|
|
30
|
+
reason: string;
|
|
31
|
+
};
|
|
32
|
+
/** Import verified terminal historical payouts. Does not change payout schedules or authorize activation. */
|
|
33
|
+
export declare function adoptRecipientPayouts(stripe: Stripe, component: Component, ctx: RunCtx, opts: PayoutAdoptionOptions, verifyOnly?: boolean): Promise<PayoutAdoptionResult>;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=payoutAdoption.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutAdoption.d.ts","sourceRoot":"","sources":["../../../src/client/connect/payoutAdoption.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAQvD,OAAO,KAAK,EACV,QAAQ,EACR,WAAW,EACZ,MAAM,yCAAyC,CAAC;AAejD,KAAK,KAAK,GAAG,KAAK,CAAC,OAAO,QAAQ,CAAC,CAAC;AACpC,MAAM,MAAM,qBAAqB,GAAG;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yGAAyG;IACzG,OAAO,EAAE;QACP,cAAc,EAAE,MAAM,CAAC;QACvB,WAAW,EAAE;YAAE,gBAAgB,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;KAC7D,EAAE,CAAC;CACL,CAAC;AACF,MAAM,MAAM,oBAAoB,GAC5B;IACE,MAAM,EAAE,SAAS,CAAC;IAClB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,KAAK,CAAC,OAAO,WAAW,CAAC,EAAE,CAAC;IAC1C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,GACD;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AACvC,6GAA6G;AAC7G,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,EACpB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,qBAAqB,EAC3B,UAAU,UAAQ,GACjB,OAAO,CAAC,oBAAoB,CAAC,CA6Q/B"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { oldestUnpaidFunding } from "./payoutAdoptionAge.js";
|
|
2
|
+
import { addSummaryAmounts as add } from "../../component/earnings/arithmetic.js";
|
|
3
|
+
import { runMutationOrThrow } from "../helpers.js";
|
|
4
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
5
|
+
import { validateScope } from "../../component/payoutPolicy/helpers.js";
|
|
6
|
+
import { transferFingerprint, payoutClaimFingerprint, } from "../../component/payoutRelease/helpers.js";
|
|
7
|
+
import { getTransferFundingReceipt, } from "./transferReceipt.js";
|
|
8
|
+
import { getRecipientPayoutReceipt } from "./payoutReceipt.js";
|
|
9
|
+
import { verifyPayoutHistory } from "./payoutHistory.js";
|
|
10
|
+
import { validateHistoricalFunding, } from "./payoutAdoptionAllocation.js";
|
|
11
|
+
import { reconcileRecipientPayout } from "./payoutReconciliation.js";
|
|
12
|
+
/** Import verified terminal historical payouts. Does not change payout schedules or authorize activation. */
|
|
13
|
+
export async function adoptRecipientPayouts(stripe, component, ctx, opts, verifyOnly = false) {
|
|
14
|
+
validateScope(opts);
|
|
15
|
+
const max = opts.maxPages ?? 10;
|
|
16
|
+
if (!/^[a-z]{3}$/.test(opts.currency) ||
|
|
17
|
+
!Number.isSafeInteger(opts.expectedRevision) ||
|
|
18
|
+
opts.expectedRevision < 0 ||
|
|
19
|
+
!Number.isInteger(max) ||
|
|
20
|
+
max < 1 ||
|
|
21
|
+
max > 100)
|
|
22
|
+
throw new Error("Invalid historical adoption scope or budget.");
|
|
23
|
+
if (!ctx.runMutation)
|
|
24
|
+
throw new Error("Historical adoption requires runMutation.");
|
|
25
|
+
const mappings = new Map();
|
|
26
|
+
for (const p of opts.payouts) {
|
|
27
|
+
if (mappings.has(p.stripePayoutId))
|
|
28
|
+
throw new Error("Duplicate historical payout mapping.");
|
|
29
|
+
mappings.set(p.stripePayoutId, p.allocations);
|
|
30
|
+
}
|
|
31
|
+
const scope = { accountId: opts.accountId, ownerKey: opts.ownerKey };
|
|
32
|
+
const policy = (await ctx.runQuery(componentRef(component, "payoutPolicy/queries/get"), scope));
|
|
33
|
+
if (!policy ||
|
|
34
|
+
policy.revision !== opts.expectedRevision ||
|
|
35
|
+
policy.mode !== "stripe_managed" ||
|
|
36
|
+
(policy.status !== "ready" && !(verifyOnly && policy.status === "changing")))
|
|
37
|
+
throw new Error("Historical adoption requires an owned automatic policy at this revision.");
|
|
38
|
+
const snapshot = (await ctx.runQuery(componentRef(component, "payoutRelease/queries/snapshot"), scope));
|
|
39
|
+
if (snapshot.status !== "ready")
|
|
40
|
+
return snapshot;
|
|
41
|
+
const known = new Map(snapshot.claims
|
|
42
|
+
.filter((c) => c.stripePayoutId)
|
|
43
|
+
.map((c) => [c.stripePayoutId, c]));
|
|
44
|
+
const historyCurrencies = new Set();
|
|
45
|
+
const payouts = [];
|
|
46
|
+
let cursor, head, complete = false;
|
|
47
|
+
for (let page = 0; page < max; page++) {
|
|
48
|
+
const batch = await stripe.payouts.list({ limit: 100, ...(cursor ? { starting_after: cursor } : {}) }, { stripeAccount: opts.accountId });
|
|
49
|
+
if (page === 0)
|
|
50
|
+
head = batch.data[0]?.id;
|
|
51
|
+
for (const p of batch.data)
|
|
52
|
+
historyCurrencies.add(p.currency);
|
|
53
|
+
payouts.push(...batch.data.filter((p) => p.currency === opts.currency));
|
|
54
|
+
if (!batch.has_more) {
|
|
55
|
+
complete = true;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
const next = batch.data[batch.data.length - 1]?.id;
|
|
59
|
+
if (!next || next === cursor)
|
|
60
|
+
return { status: "held", reason: "history_incomplete" };
|
|
61
|
+
cursor = next;
|
|
62
|
+
}
|
|
63
|
+
if (!complete)
|
|
64
|
+
return { status: "held", reason: "history_incomplete" };
|
|
65
|
+
if (new Set(payouts.map((p) => p.id)).size !== payouts.length)
|
|
66
|
+
return { status: "held", reason: "history_incomplete" };
|
|
67
|
+
if ([...mappings.keys()].some((id) => !payouts.some((p) => p.id === id)))
|
|
68
|
+
return { status: "held", reason: "historical_history_changed" };
|
|
69
|
+
const proofPayouts = [];
|
|
70
|
+
const transactions = [];
|
|
71
|
+
const imports = [];
|
|
72
|
+
for (const p of payouts) {
|
|
73
|
+
const old = known.get(p.id), supplied = mappings.get(p.id);
|
|
74
|
+
const allocations = old?.allocations ?? supplied;
|
|
75
|
+
if (!allocations)
|
|
76
|
+
return { status: "held", reason: "historical_allocation_required" };
|
|
77
|
+
const canonical = (a) => JSON.stringify(a
|
|
78
|
+
.map((x) => [x.stripeTransferId, x.amount])
|
|
79
|
+
.sort((a, b) => String(a[0]).localeCompare(String(b[0]))));
|
|
80
|
+
if (old && supplied && canonical(old.allocations) !== canonical(supplied))
|
|
81
|
+
throw new Error("Historical payout source allocations cannot be changed.");
|
|
82
|
+
const receipt = await getRecipientPayoutReceipt(stripe, {
|
|
83
|
+
accountId: opts.accountId,
|
|
84
|
+
stripePayoutId: p.id,
|
|
85
|
+
});
|
|
86
|
+
if (receipt.status !== "ready")
|
|
87
|
+
return { status: "held", reason: receipt.reason };
|
|
88
|
+
if (receipt.sourceType !== "card" ||
|
|
89
|
+
receipt.method !== "standard" ||
|
|
90
|
+
receipt.type !== "bank_account")
|
|
91
|
+
return { status: "held", reason: "source_allocation_required" };
|
|
92
|
+
if (!["paid", "failed", "canceled"].includes(receipt.providerStatus))
|
|
93
|
+
return { status: "held", reason: "historical_payout_pending" };
|
|
94
|
+
const returned = receipt.providerStatus !== "paid";
|
|
95
|
+
if (returned && !receipt.credit)
|
|
96
|
+
return { status: "held", reason: "return_unavailable" };
|
|
97
|
+
if (receipt.debit.fee + (receipt.credit?.fee ?? 0) < 0)
|
|
98
|
+
return { status: "held", reason: "fee_allocation_required" };
|
|
99
|
+
proofPayouts.push({
|
|
100
|
+
stripePayoutId: p.id,
|
|
101
|
+
currency: receipt.currency,
|
|
102
|
+
amount: receipt.amount,
|
|
103
|
+
created: receipt.created,
|
|
104
|
+
allocations,
|
|
105
|
+
...(returned
|
|
106
|
+
? {
|
|
107
|
+
returnedAt: Math.max(receipt.credit.created, receipt.credit.availableOn),
|
|
108
|
+
}
|
|
109
|
+
: {}),
|
|
110
|
+
});
|
|
111
|
+
transactions.push({ ...receipt.debit, sourceId: p.id });
|
|
112
|
+
if (receipt.credit)
|
|
113
|
+
transactions.push({ ...receipt.credit, sourceId: p.id });
|
|
114
|
+
imports.push({
|
|
115
|
+
old,
|
|
116
|
+
allocations,
|
|
117
|
+
payout: {
|
|
118
|
+
stripePayoutId: p.id,
|
|
119
|
+
amount: receipt.amount,
|
|
120
|
+
currency: receipt.currency,
|
|
121
|
+
created: receipt.created,
|
|
122
|
+
destinationId: receipt.destinationId,
|
|
123
|
+
status: returned ? "returned" : "paid",
|
|
124
|
+
debitTransactionId: receipt.debit.id,
|
|
125
|
+
debitFee: receipt.debit.fee,
|
|
126
|
+
...(returned
|
|
127
|
+
? {
|
|
128
|
+
returnTransactionId: receipt.credit.id,
|
|
129
|
+
returnFee: receipt.credit.fee,
|
|
130
|
+
}
|
|
131
|
+
: {}),
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const observations = [];
|
|
136
|
+
const sources = [];
|
|
137
|
+
for (const row of snapshot.transfers) {
|
|
138
|
+
const receipt = await getTransferFundingReceipt(stripe, component, ctx, {
|
|
139
|
+
accountId: opts.accountId,
|
|
140
|
+
stripeTransferId: row.stripeTransferId,
|
|
141
|
+
});
|
|
142
|
+
if (receipt.status !== "ready")
|
|
143
|
+
return { status: "held", reason: receipt.reason };
|
|
144
|
+
historyCurrencies.add(receipt.settlementCurrency);
|
|
145
|
+
sources.push({
|
|
146
|
+
stripeTransferId: row.stripeTransferId,
|
|
147
|
+
currency: receipt.settlementCurrency,
|
|
148
|
+
transactions: receipt.transactions,
|
|
149
|
+
});
|
|
150
|
+
observations.push({
|
|
151
|
+
stripeTransferId: row.stripeTransferId,
|
|
152
|
+
currency: receipt.settlementCurrency,
|
|
153
|
+
matureNet: receipt.matureNet,
|
|
154
|
+
pendingCredits: receipt.pendingCredits,
|
|
155
|
+
ledgerFingerprint: transferFingerprint(row),
|
|
156
|
+
observedAt: receipt.observedAt,
|
|
157
|
+
transactionIds: receipt.transactions.map((t) => t.id),
|
|
158
|
+
});
|
|
159
|
+
if (receipt.settlementCurrency === opts.currency)
|
|
160
|
+
transactions.push(...receipt.transactions);
|
|
161
|
+
}
|
|
162
|
+
validateHistoricalFunding(sources, proofPayouts);
|
|
163
|
+
const history = await verifyPayoutHistory(stripe, {
|
|
164
|
+
accountId: opts.accountId,
|
|
165
|
+
currency: opts.currency,
|
|
166
|
+
transactions,
|
|
167
|
+
maxPages: max,
|
|
168
|
+
});
|
|
169
|
+
if (history.status !== "verified")
|
|
170
|
+
return history;
|
|
171
|
+
const latest = await stripe.payouts.list({ limit: 1 }, { stripeAccount: opts.accountId });
|
|
172
|
+
if (latest.data[0]?.id !== head)
|
|
173
|
+
return { status: "held", reason: "provider_changed" };
|
|
174
|
+
const claims = [];
|
|
175
|
+
for (const item of imports.filter((i) => i.old)) {
|
|
176
|
+
const result = await reconcileRecipientPayout(stripe, component, ctx, {
|
|
177
|
+
...scope,
|
|
178
|
+
operationKey: item.old.operationKey,
|
|
179
|
+
stripePayoutId: item.payout.stripePayoutId,
|
|
180
|
+
maxPages: max,
|
|
181
|
+
});
|
|
182
|
+
if (result.status !== "updated")
|
|
183
|
+
return {
|
|
184
|
+
status: "held",
|
|
185
|
+
reason: result.status === "held"
|
|
186
|
+
? result.reason
|
|
187
|
+
: "historical_claim_terminal",
|
|
188
|
+
};
|
|
189
|
+
claims.push(result.claim);
|
|
190
|
+
}
|
|
191
|
+
const unknown = imports.filter((i) => !i.old);
|
|
192
|
+
if (unknown.length && verifyOnly)
|
|
193
|
+
return { status: "held", reason: "historical_allocation_required" };
|
|
194
|
+
if (unknown.length) {
|
|
195
|
+
const fresh = (await ctx.runQuery(componentRef(component, "payoutRelease/queries/snapshot"), scope));
|
|
196
|
+
if (fresh.status !== "ready")
|
|
197
|
+
return fresh;
|
|
198
|
+
const result = (await runMutationOrThrow(ctx, componentRef(component, "payoutRelease/mutations/adoptHistoricalBatch"), {
|
|
199
|
+
...scope,
|
|
200
|
+
expectedRevision: opts.expectedRevision,
|
|
201
|
+
observations,
|
|
202
|
+
expectedClaims: fresh.claims.map((c) => ({
|
|
203
|
+
operationKey: c.operationKey,
|
|
204
|
+
fingerprint: payoutClaimFingerprint(c),
|
|
205
|
+
})),
|
|
206
|
+
payouts: unknown.map((i) => ({
|
|
207
|
+
payout: i.payout,
|
|
208
|
+
allocations: i.allocations,
|
|
209
|
+
})),
|
|
210
|
+
}));
|
|
211
|
+
claims.push(...result.claims);
|
|
212
|
+
}
|
|
213
|
+
const paid = claims
|
|
214
|
+
.filter((c) => c.status === "paid")
|
|
215
|
+
.flatMap((c) => c.allocations);
|
|
216
|
+
const fees = claims.reduce((total, c) => add(total, c.providerFee ?? 0), 0);
|
|
217
|
+
const returned = proofPayouts
|
|
218
|
+
.filter((p) => p.returnedAt !== undefined)
|
|
219
|
+
.flatMap((p) => p.allocations.map((a) => ({ ...a, created: p.returnedAt })));
|
|
220
|
+
const oldestHeldAt = oldestUnpaidFunding(opts.currency, sources, paid, fees, returned);
|
|
221
|
+
return {
|
|
222
|
+
status: "adopted",
|
|
223
|
+
claims,
|
|
224
|
+
observations,
|
|
225
|
+
historyCurrencies: [...historyCurrencies],
|
|
226
|
+
...(oldestHeldAt !== undefined ? { oldestHeldAt } : {}),
|
|
227
|
+
observedAt: Date.now(),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=payoutAdoption.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutAdoption.js","sourceRoot":"","sources":["../../../src/client/connect/payoutAdoption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,IAAI,GAAG,EAAE,MAAM,wCAAwC,CAAC;AAIlF,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EACL,mBAAmB,EACnB,sBAAsB,GACvB,MAAM,0CAA0C,CAAC;AAOlD,OAAO,EACL,yBAAyB,GAE1B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EACL,yBAAyB,GAG1B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAA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MAAM,QAAQ,GAAG,YAAY;SAC1B,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,SAAS,CAAC;SACzC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CACb,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,UAAW,EAAE,CAAC,CAAC,CAC7D,CAAC;IACJ,MAAM,YAAY,GAAG,mBAAmB,CACtC,IAAI,CAAC,QAAQ,EACb,OAAO,EACP,IAAI,EACJ,IAAI,EACJ,QAAQ,CACT,CAAC;IACF,OAAO;QACL,MAAM,EAAE,SAAS;QACjB,MAAM;QACN,YAAY;QACZ,iBAAiB,EAAE,CAAC,GAAG,iBAAiB,CAAC;QACzC,GAAG,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACvD,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE;KACvB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type Source = {
|
|
2
|
+
stripeTransferId: string;
|
|
3
|
+
currency: string;
|
|
4
|
+
transactions: {
|
|
5
|
+
net: number;
|
|
6
|
+
created: number;
|
|
7
|
+
}[];
|
|
8
|
+
};
|
|
9
|
+
/** FIFO ownership age after historical paid principal, source debt and verified payout fees. */
|
|
10
|
+
export declare function oldestUnpaidFunding(currency: string, sources: Source[], paid: {
|
|
11
|
+
stripeTransferId: string;
|
|
12
|
+
amount: number;
|
|
13
|
+
}[], fees: number, returned?: {
|
|
14
|
+
stripeTransferId: string;
|
|
15
|
+
amount: number;
|
|
16
|
+
created: number;
|
|
17
|
+
}[]): number | undefined;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=payoutAdoptionAge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutAdoptionAge.d.ts","sourceRoot":"","sources":["../../../src/client/connect/payoutAdoptionAge.ts"],"names":[],"mappings":"AACA,KAAK,MAAM,GAAG;IACZ,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAClD,CAAC;AACF,gGAAgG;AAChG,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EAAE,EACjB,IAAI,EAAE;IAAE,gBAAgB,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAAE,EACpD,IAAI,EAAE,MAAM,EACZ,QAAQ,GAAE;IACR,gBAAgB,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB,EAAO,GACP,MAAM,GAAG,SAAS,CAwDpB"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { addSummaryAmounts as add } from "../../component/earnings/arithmetic.js";
|
|
2
|
+
/** FIFO ownership age after historical paid principal, source debt and verified payout fees. */
|
|
3
|
+
export function oldestUnpaidFunding(currency, sources, paid, fees, returned = []) {
|
|
4
|
+
if (!Number.isSafeInteger(fees) || fees < 0)
|
|
5
|
+
throw new Error("Invalid payout fee amount.");
|
|
6
|
+
const bySource = new Map();
|
|
7
|
+
for (const p of paid) {
|
|
8
|
+
if (!Number.isSafeInteger(p.amount) || p.amount < 0)
|
|
9
|
+
throw new Error("Invalid historical paid amount.");
|
|
10
|
+
bySource.set(p.stripeTransferId, add(bySource.get(p.stripeTransferId) ?? 0, p.amount));
|
|
11
|
+
}
|
|
12
|
+
sources = sources.map((s) => ({ ...s, transactions: [...s.transactions] }));
|
|
13
|
+
const returnedTotals = new Map();
|
|
14
|
+
for (const r of returned) {
|
|
15
|
+
const source = sources.find((s) => s.stripeTransferId === r.stripeTransferId);
|
|
16
|
+
if (!source || !Number.isSafeInteger(r.amount) || r.amount <= 0)
|
|
17
|
+
throw new Error("Invalid returned funding allocation.");
|
|
18
|
+
const total = add(returnedTotals.get(r.stripeTransferId) ?? 0, r.amount);
|
|
19
|
+
if (total > (bySource.get(r.stripeTransferId) ?? 0))
|
|
20
|
+
throw new Error("Returned funding exceeds historical paid principal.");
|
|
21
|
+
returnedTotals.set(r.stripeTransferId, total);
|
|
22
|
+
source.transactions.push({ net: r.amount, created: r.created });
|
|
23
|
+
}
|
|
24
|
+
const chunks = [];
|
|
25
|
+
let debt = fees;
|
|
26
|
+
for (const s of sources.filter((s) => s.currency === currency)) {
|
|
27
|
+
let consumed = bySource.get(s.stripeTransferId) ?? 0;
|
|
28
|
+
for (const t of s.transactions) {
|
|
29
|
+
if (!Number.isSafeInteger(t.net) ||
|
|
30
|
+
!Number.isSafeInteger(t.created) ||
|
|
31
|
+
t.created < 0 ||
|
|
32
|
+
!Number.isSafeInteger(t.created * 1000))
|
|
33
|
+
throw new Error("Invalid source funding age.");
|
|
34
|
+
if (t.net < 0)
|
|
35
|
+
consumed = add(consumed, -t.net);
|
|
36
|
+
}
|
|
37
|
+
for (const t of [...s.transactions]
|
|
38
|
+
.filter((t) => t.net > 0)
|
|
39
|
+
.sort((a, b) => a.created - b.created)) {
|
|
40
|
+
const used = Math.min(consumed, t.net);
|
|
41
|
+
consumed = add(consumed, -used);
|
|
42
|
+
const amount = add(t.net, -used);
|
|
43
|
+
if (amount)
|
|
44
|
+
chunks.push({ amount, created: t.created });
|
|
45
|
+
}
|
|
46
|
+
debt = add(debt, consumed);
|
|
47
|
+
}
|
|
48
|
+
for (const c of chunks.sort((a, b) => a.created - b.created)) {
|
|
49
|
+
const used = Math.min(debt, c.amount);
|
|
50
|
+
debt = add(debt, -used);
|
|
51
|
+
if (c.amount > used)
|
|
52
|
+
return c.created * 1000;
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=payoutAdoptionAge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutAdoptionAge.js","sourceRoot":"","sources":["../../../src/client/connect/payoutAdoptionAge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,IAAI,GAAG,EAAE,MAAM,wCAAwC,CAAC;AAMlF,gGAAgG;AAChG,MAAM,UAAU,mBAAmB,CACjC,QAAgB,EAChB,OAAiB,EACjB,IAAoD,EACpD,IAAY,EACZ,QAAQ,GAIF,EAAE;IAER,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC;YACjD,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACrD,QAAQ,CAAC,GAAG,CACV,CAAC,CAAC,gBAAgB,EAClB,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CACrD,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC;IAC5E,MAAM,cAAc,GAAG,IAAI,GAAG,EAAkB,CAAC;IACjD,KAAK,MAAM,CAAC,IAAI,QAAQ,EAAE,CAAC;QACzB,MAAM,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QAC7D,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QACtC,IAAI,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC;QACxB,IAAI,CAAC,CAAC,MAAM,GAAG,IAAI;YAAE,OAAO,CAAC,CAAC,OAAO,GAAG,IAAI,CAAC;IAC/C,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type HistoricalFundingSource = {
|
|
2
|
+
stripeTransferId: string;
|
|
3
|
+
currency: string;
|
|
4
|
+
transactions: {
|
|
5
|
+
net: number;
|
|
6
|
+
created: number;
|
|
7
|
+
availableOn: number;
|
|
8
|
+
status: string;
|
|
9
|
+
}[];
|
|
10
|
+
};
|
|
11
|
+
export type HistoricalFundingPayout = {
|
|
12
|
+
stripePayoutId: string;
|
|
13
|
+
currency: string;
|
|
14
|
+
amount: number;
|
|
15
|
+
created: number;
|
|
16
|
+
returnedAt?: number;
|
|
17
|
+
allocations: {
|
|
18
|
+
stripeTransferId: string;
|
|
19
|
+
amount: number;
|
|
20
|
+
}[];
|
|
21
|
+
};
|
|
22
|
+
/** Validate explicit historical cash provenance, not today's still-unpaid entitlement. */
|
|
23
|
+
export declare function validateHistoricalFunding(sources: HistoricalFundingSource[], payouts: HistoricalFundingPayout[]): void;
|
|
24
|
+
//# sourceMappingURL=payoutAdoptionAllocation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutAdoptionAllocation.d.ts","sourceRoot":"","sources":["../../../src/client/connect/payoutAdoptionAllocation.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,uBAAuB,GAAG;IACpC,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE;QACZ,GAAG,EAAE,MAAM,CAAC;QACZ,OAAO,EAAE,MAAM,CAAC;QAChB,WAAW,EAAE,MAAM,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;KAChB,EAAE,CAAC;CACL,CAAC;AACF,MAAM,MAAM,uBAAuB,GAAG;IACpC,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE;QAAE,gBAAgB,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAC7D,CAAC;AASF,0FAA0F;AAC1F,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,uBAAuB,EAAE,EAClC,OAAO,EAAE,uBAAuB,EAAE,GACjC,IAAI,CA0EN"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { addSummaryAmounts as add } from "../../component/earnings/arithmetic.js";
|
|
2
|
+
function integer(n) {
|
|
3
|
+
if (!Number.isSafeInteger(n))
|
|
4
|
+
throw new Error("Invalid historical funding arithmetic.");
|
|
5
|
+
return n;
|
|
6
|
+
}
|
|
7
|
+
function timestamp(n) {
|
|
8
|
+
if (integer(n) < 0)
|
|
9
|
+
throw new Error("Invalid historical funding timestamp.");
|
|
10
|
+
}
|
|
11
|
+
/** Validate explicit historical cash provenance, not today's still-unpaid entitlement. */
|
|
12
|
+
export function validateHistoricalFunding(sources, payouts) {
|
|
13
|
+
const byId = new Map();
|
|
14
|
+
for (const s of sources) {
|
|
15
|
+
if (byId.has(s.stripeTransferId))
|
|
16
|
+
throw new Error("Duplicate historical funding source.");
|
|
17
|
+
if (!/^tr_\w+$/.test(s.stripeTransferId) || !/^[a-z]{3}$/.test(s.currency))
|
|
18
|
+
throw new Error("Invalid historical funding source.");
|
|
19
|
+
for (const t of s.transactions) {
|
|
20
|
+
integer(t.net);
|
|
21
|
+
timestamp(t.created);
|
|
22
|
+
timestamp(t.availableOn);
|
|
23
|
+
if (!["available", "pending"].includes(t.status))
|
|
24
|
+
throw new Error("Invalid historical funding status.");
|
|
25
|
+
}
|
|
26
|
+
byId.set(s.stripeTransferId, s);
|
|
27
|
+
}
|
|
28
|
+
const ids = new Set();
|
|
29
|
+
for (const p of payouts) {
|
|
30
|
+
if (ids.has(p.stripePayoutId))
|
|
31
|
+
throw new Error("Duplicate historical payout.");
|
|
32
|
+
ids.add(p.stripePayoutId);
|
|
33
|
+
if (!/^po_\w+$/.test(p.stripePayoutId) ||
|
|
34
|
+
!/^[a-z]{3}$/.test(p.currency) ||
|
|
35
|
+
integer(p.amount) <= 0)
|
|
36
|
+
throw new Error("Invalid historical payout principal.");
|
|
37
|
+
timestamp(p.created);
|
|
38
|
+
if (p.returnedAt !== undefined) {
|
|
39
|
+
timestamp(p.returnedAt);
|
|
40
|
+
if (p.returnedAt < p.created)
|
|
41
|
+
throw new Error("Historical payout return predates the payout.");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const prior = [];
|
|
45
|
+
for (const p of [...payouts].sort((a, b) => a.created - b.created || a.stripePayoutId.localeCompare(b.stripePayoutId))) {
|
|
46
|
+
const seen = new Set();
|
|
47
|
+
let principal = 0;
|
|
48
|
+
for (const a of p.allocations) {
|
|
49
|
+
if (seen.has(a.stripeTransferId))
|
|
50
|
+
throw new Error("Duplicate historical source allocation.");
|
|
51
|
+
seen.add(a.stripeTransferId);
|
|
52
|
+
if (integer(a.amount) <= 0)
|
|
53
|
+
throw new Error("Invalid historical principal allocation.");
|
|
54
|
+
principal = add(principal, a.amount);
|
|
55
|
+
const s = byId.get(a.stripeTransferId);
|
|
56
|
+
if (!s)
|
|
57
|
+
throw new Error("Historical payout funding source is unavailable.");
|
|
58
|
+
if (s.currency !== p.currency)
|
|
59
|
+
throw new Error("Historical payout currency allocation mismatch.");
|
|
60
|
+
let capacity = 0;
|
|
61
|
+
for (const t of s.transactions)
|
|
62
|
+
if (t.status === "available" &&
|
|
63
|
+
Math.max(t.created, t.availableOn) <= p.created)
|
|
64
|
+
capacity = add(capacity, t.net);
|
|
65
|
+
for (const old of prior)
|
|
66
|
+
if (old.returnedAt === undefined || old.returnedAt > p.created)
|
|
67
|
+
for (const allocation of old.allocations)
|
|
68
|
+
if (allocation.stripeTransferId === s.stripeTransferId)
|
|
69
|
+
capacity = add(capacity, -allocation.amount);
|
|
70
|
+
if (a.amount > capacity)
|
|
71
|
+
throw new Error("Historical payout exceeds verified mature source funding.");
|
|
72
|
+
}
|
|
73
|
+
if (principal !== p.amount)
|
|
74
|
+
throw new Error("Historical allocations do not equal payout principal.");
|
|
75
|
+
prior.push(p);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=payoutAdoptionAllocation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payoutAdoptionAllocation.js","sourceRoot":"","sources":["../../../src/client/connect/payoutAdoptionAllocation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,IAAI,GAAG,EAAE,MAAM,wCAAwC,CAAC;AAmBlF,SAAS,OAAO,CAAC,CAAS;IACxB,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,OAAO,CAAC,CAAC;AACX,CAAC;AACD,SAAS,SAAS,CAAC,CAAS;IAC1B,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;AAC/E,CAAC;AACD,0FAA0F;AAC1F,MAAM,UAAU,yBAAyB,CACvC,OAAkC,EAClC,OAAkC;IAElC,MAAM,IAAI,GAAG,IAAI,GAAG,EAAmC,CAAC;IACxD,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC;YAC9B,MAAM,IAAI,KAAK,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