@fantastic.dev/better-stripe 0.58.0 → 0.60.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 +12 -0
- package/README.md +3 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +888 -62
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +428 -15
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +856 -30
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +444 -31
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/paymentSource/resolver.d.ts +12 -1
- package/dist/client/paymentSource/resolver.d.ts.map +1 -1
- package/dist/client/paymentSource/resolver.fixtures.d.ts +220 -0
- package/dist/client/paymentSource/resolver.fixtures.d.ts.map +1 -0
- package/dist/client/paymentSource/resolver.fixtures.js +189 -0
- package/dist/client/paymentSource/resolver.fixtures.js.map +1 -0
- package/dist/client/paymentSource/resolver.js +28 -3
- package/dist/client/paymentSource/resolver.js.map +1 -1
- package/dist/client/settlement/execution.d.ts +25 -0
- package/dist/client/settlement/execution.d.ts.map +1 -1
- package/dist/client/settlement/execution.js +2 -0
- package/dist/client/settlement/execution.js.map +1 -1
- package/dist/client/usage/allowanceClient.d.ts +1 -1
- package/dist/client/usage/catalogPrices.d.ts +1 -1
- package/dist/client/usage/catalogPrices.d.ts.map +1 -1
- package/dist/client/usage/catalogPrices.js +4 -4
- package/dist/client/usage/catalogPrices.js.map +1 -1
- package/dist/client/usage/client.d.ts +2 -2
- package/dist/client/usage/client.d.ts.map +1 -1
- package/dist/client/usage/client.js +2 -2
- package/dist/client/usage/client.js.map +1 -1
- package/dist/client/usage/correctionClient.d.ts +33 -0
- package/dist/client/usage/correctionClient.d.ts.map +1 -0
- package/dist/client/usage/correctionClient.js +27 -0
- package/dist/client/usage/correctionClient.js.map +1 -0
- package/dist/client/usage/correctionEligibility.d.ts +4 -0
- package/dist/client/usage/correctionEligibility.d.ts.map +1 -0
- package/dist/client/usage/correctionEligibility.js +7 -0
- package/dist/client/usage/correctionEligibility.js.map +1 -0
- package/dist/client/usage/correctionInvoice.d.ts +9 -0
- package/dist/client/usage/correctionInvoice.d.ts.map +1 -0
- package/dist/client/usage/correctionInvoice.js +218 -0
- package/dist/client/usage/correctionInvoice.js.map +1 -0
- package/dist/client/usage/correctionInvoiceProof.d.ts +29 -0
- package/dist/client/usage/correctionInvoiceProof.d.ts.map +1 -0
- package/dist/client/usage/correctionInvoiceProof.js +41 -0
- package/dist/client/usage/correctionInvoiceProof.js.map +1 -0
- package/dist/client/usage/correctionProofs.d.ts +36 -0
- package/dist/client/usage/correctionProofs.d.ts.map +1 -0
- package/dist/client/usage/correctionProofs.js +41 -0
- package/dist/client/usage/correctionProofs.js.map +1 -0
- package/dist/client/usage/correctionProvider.d.ts +6 -0
- package/dist/client/usage/correctionProvider.d.ts.map +1 -0
- package/dist/client/usage/correctionProvider.js +243 -0
- package/dist/client/usage/correctionProvider.js.map +1 -0
- package/dist/client/usage/correctionRead.d.ts +27 -0
- package/dist/client/usage/correctionRead.d.ts.map +1 -0
- package/dist/client/usage/correctionRead.js +36 -0
- package/dist/client/usage/correctionRead.js.map +1 -0
- package/dist/client/usage/correctionTerms.d.ts +64 -0
- package/dist/client/usage/correctionTerms.d.ts.map +1 -0
- package/dist/client/usage/correctionTerms.js +65 -0
- package/dist/client/usage/correctionTerms.js.map +1 -0
- package/dist/client/usage/correctionTransport.fixtures.d.ts +37 -0
- package/dist/client/usage/correctionTransport.fixtures.d.ts.map +1 -0
- package/dist/client/usage/correctionTransport.fixtures.js +322 -0
- package/dist/client/usage/correctionTransport.fixtures.js.map +1 -0
- package/dist/client/usage/creditClient.d.ts +1 -1
- package/dist/client/usage/creditRevenue.d.ts +22 -0
- package/dist/client/usage/creditRevenue.d.ts.map +1 -0
- package/dist/client/usage/creditRevenue.js +118 -0
- package/dist/client/usage/creditRevenue.js.map +1 -0
- package/dist/client/usage/creditSettlement.d.ts +29 -0
- package/dist/client/usage/creditSettlement.d.ts.map +1 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts +215 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -0
- package/dist/client/usage/creditSettlement.fixtures.js +59 -0
- package/dist/client/usage/creditSettlement.fixtures.js.map +1 -0
- package/dist/client/usage/creditSettlement.js +70 -0
- package/dist/client/usage/creditSettlement.js.map +1 -0
- package/dist/client/usage/creditTransport.fixtures.d.ts +7 -1
- package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.js +25 -4
- package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
- package/dist/client/usage/index.d.ts +6 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/client/usage/invoiceSale.d.ts +22 -0
- package/dist/client/usage/invoiceSale.d.ts.map +1 -0
- package/dist/client/usage/invoiceSale.fixtures.d.ts +285 -0
- package/dist/client/usage/invoiceSale.fixtures.d.ts.map +1 -0
- package/dist/client/usage/invoiceSale.fixtures.js +104 -0
- package/dist/client/usage/invoiceSale.fixtures.js.map +1 -0
- package/dist/client/usage/invoiceSale.js +117 -0
- package/dist/client/usage/invoiceSale.js.map +1 -0
- package/dist/client/usage/observationClient.d.ts +20 -0
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/client/usage/observationClient.js +20 -6
- package/dist/client/usage/observationClient.js.map +1 -1
- package/dist/client/usage/records.d.ts +1 -1
- package/dist/client/usage/records.d.ts.map +1 -1
- package/dist/client/usage/records.js.map +1 -1
- package/dist/client/usage/settlement.d.ts +32 -0
- package/dist/client/usage/settlement.d.ts.map +1 -0
- package/dist/client/usage/settlement.js +60 -0
- package/dist/client/usage/settlement.js.map +1 -0
- package/dist/client/usage/settlementClient.d.ts +51 -0
- package/dist/client/usage/settlementClient.d.ts.map +1 -0
- package/dist/client/usage/settlementClient.js +35 -0
- package/dist/client/usage/settlementClient.js.map +1 -0
- package/dist/client/usage/summary.d.ts +3 -0
- package/dist/client/usage/summary.d.ts.map +1 -1
- package/dist/client/usage/summary.js +9 -4
- package/dist/client/usage/summary.js.map +1 -1
- package/dist/client/usageExports.d.ts +2 -0
- package/dist/client/usageExports.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +12 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +380 -14
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/accountClosure/mutations.d.ts.map +1 -1
- package/dist/component/accountClosure/mutations.js +13 -0
- package/dist/component/accountClosure/mutations.js.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +368 -32
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.js +2 -0
- package/dist/component/financialObservations/settlementHelpers.js.map +1 -1
- package/dist/component/recoveryWorkflow/helpers.d.ts +25 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/mutations.d.ts +25 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/queries.d.ts +50 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/schema.d.ts +566 -10
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
- package/dist/component/schema.d.ts +428 -15
- package/dist/component/schema.js +4 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/settlement/mutations.d.ts +50 -0
- package/dist/component/settlement/mutations.d.ts.map +1 -1
- package/dist/component/settlement/mutations.js +8 -1
- package/dist/component/settlement/mutations.js.map +1 -1
- package/dist/component/settlement/queries.d.ts +25 -0
- package/dist/component/settlement/queries.d.ts.map +1 -1
- package/dist/component/settlement/schema.d.ts +128 -1
- package/dist/component/settlement/schema.d.ts.map +1 -1
- package/dist/component/settlement/schema.js +22 -0
- package/dist/component/settlement/schema.js.map +1 -1
- package/dist/component/usage/queries.d.ts +2 -2
- package/dist/component/usage/schema.d.ts +18 -13
- package/dist/component/usage/schema.d.ts.map +1 -1
- package/dist/component/usage/schema.js +2 -1
- package/dist/component/usage/schema.js.map +1 -1
- package/dist/component/usageAllowance/queries.d.ts +1 -1
- package/dist/component/usageAllowance/queries.d.ts.map +1 -1
- package/dist/component/usageAllowance/queries.js +10 -1
- package/dist/component/usageAllowance/queries.js.map +1 -1
- package/dist/component/usageAllowance/schema.d.ts +5 -5
- package/dist/component/usageAllowance/schema.d.ts.map +1 -1
- package/dist/component/usageAllowance/schema.js +1 -1
- package/dist/component/usageAllowance/schema.js.map +1 -1
- package/dist/component/usageCorrections/accounting.d.ts +5 -0
- package/dist/component/usageCorrections/accounting.d.ts.map +1 -0
- package/dist/component/usageCorrections/accounting.js +55 -0
- package/dist/component/usageCorrections/accounting.js.map +1 -0
- package/dist/component/usageCorrections/cancellation.d.ts +6 -0
- package/dist/component/usageCorrections/cancellation.d.ts.map +1 -0
- package/dist/component/usageCorrections/cancellation.js +29 -0
- package/dist/component/usageCorrections/cancellation.js.map +1 -0
- package/dist/component/usageCorrections/helpers.d.ts +60 -0
- package/dist/component/usageCorrections/helpers.d.ts.map +1 -0
- package/dist/component/usageCorrections/helpers.js +52 -0
- package/dist/component/usageCorrections/helpers.js.map +1 -0
- package/dist/component/usageCorrections/invoice.d.ts +38 -0
- package/dist/component/usageCorrections/invoice.d.ts.map +1 -0
- package/dist/component/usageCorrections/invoice.js +82 -0
- package/dist/component/usageCorrections/invoice.js.map +1 -0
- package/dist/component/usageCorrections/mutations.d.ts +121 -0
- package/dist/component/usageCorrections/mutations.d.ts.map +1 -0
- package/dist/component/usageCorrections/mutations.js +256 -0
- package/dist/component/usageCorrections/mutations.js.map +1 -0
- package/dist/component/usageCorrections/queries.d.ts +115 -0
- package/dist/component/usageCorrections/queries.d.ts.map +1 -0
- package/dist/component/usageCorrections/queries.js +84 -0
- package/dist/component/usageCorrections/queries.js.map +1 -0
- package/dist/component/usageCorrections/schema.d.ts +248 -0
- package/dist/component/usageCorrections/schema.d.ts.map +1 -0
- package/dist/component/usageCorrections/schema.js +74 -0
- package/dist/component/usageCorrections/schema.js.map +1 -0
- package/dist/component/usageCredits/queries.d.ts +1 -1
- package/dist/component/usageCredits/queries.d.ts.map +1 -1
- package/dist/component/usageCredits/queries.js +6 -0
- package/dist/component/usageCredits/queries.js.map +1 -1
- package/dist/component/usageCredits/schema.d.ts +5 -5
- package/dist/component/usageCredits/schema.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.js +1 -1
- package/dist/component/usageCredits/schema.js.map +1 -1
- package/dist/component/usageDelivery/fields.d.ts +2 -2
- package/dist/component/usageDelivery/fields.d.ts.map +1 -1
- package/dist/component/usageDelivery/fields.js +1 -1
- package/dist/component/usageDelivery/fields.js.map +1 -1
- package/dist/component/usageDelivery/helpers.d.ts +7 -6
- package/dist/component/usageDelivery/helpers.d.ts.map +1 -1
- package/dist/component/usageDelivery/mutations.d.ts +6 -6
- package/dist/component/usageDelivery/mutations.d.ts.map +1 -1
- package/dist/component/usageDelivery/mutations.js +9 -1
- package/dist/component/usageDelivery/mutations.js.map +1 -1
- package/dist/component/usageDelivery/queries.d.ts +8 -6
- package/dist/component/usageDelivery/queries.d.ts.map +1 -1
- package/dist/component/usageDelivery/schema.d.ts +4 -4
- package/dist/component/usageDelivery/steps.d.ts +2 -2
- package/dist/component/usageDelivery/steps.js +1 -1
- package/dist/component/usageDelivery/steps.js.map +1 -1
- package/dist/component/usageObservations/queries.d.ts.map +1 -1
- package/dist/component/usageObservations/queries.js +4 -2
- package/dist/component/usageObservations/queries.js.map +1 -1
- package/dist/component/usageSubscriptions/retirement.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/retirement.js +24 -2
- package/dist/component/usageSubscriptions/retirement.js.map +1 -1
- package/docs/README.md +4 -0
- package/docs/settlement.md +4 -0
- package/docs/usage-corrections.md +249 -0
- package/docs/usage-limits.md +1 -1
- package/docs/usage-settlement.md +162 -0
- package/package.json +3 -1
- package/src/client/index.ts +9 -0
- package/src/client/paymentSource/resolver.fixtures.ts +216 -0
- package/src/client/paymentSource/resolver.test.ts +11 -207
- package/src/client/paymentSource/resolver.ts +54 -2
- package/src/client/settlement/execution.ts +24 -0
- package/src/client/usage/catalogPrices.ts +4 -2
- package/src/client/usage/client.ts +2 -2
- package/src/client/usage/correctionClient.test.ts +153 -0
- package/src/client/usage/correctionClient.ts +62 -0
- package/src/client/usage/correctionCredits.test.ts +156 -0
- package/src/client/usage/correctionEligibility.ts +14 -0
- package/src/client/usage/correctionInvoice.test.ts +127 -0
- package/src/client/usage/correctionInvoice.ts +269 -0
- package/src/client/usage/correctionInvoiceProof.ts +40 -0
- package/src/client/usage/correctionProofs.ts +40 -0
- package/src/client/usage/correctionProvider.ts +326 -0
- package/src/client/usage/correctionRead.ts +73 -0
- package/src/client/usage/correctionRecovery.test.ts +113 -0
- package/src/client/usage/correctionTerms.ts +98 -0
- package/src/client/usage/correctionTransport.fixtures.ts +372 -0
- package/src/client/usage/creditRevenue.test.ts +105 -0
- package/src/client/usage/creditRevenue.ts +163 -0
- package/src/client/usage/creditSettlement.fixtures.ts +70 -0
- package/src/client/usage/creditSettlement.test.ts +66 -0
- package/src/client/usage/creditSettlement.ts +94 -0
- package/src/client/usage/creditSettlementRecovery.test.ts +173 -0
- package/src/client/usage/creditTransport.fixtures.ts +29 -4
- package/src/client/usage/index.ts +20 -0
- package/src/client/usage/invoiceSale.fixtures.ts +103 -0
- package/src/client/usage/invoiceSale.test.ts +184 -0
- package/src/client/usage/invoiceSale.ts +155 -0
- package/src/client/usage/observationClient.ts +20 -6
- package/src/client/usage/records.ts +7 -1
- package/src/client/usage/settlement.test.ts +179 -0
- package/src/client/usage/settlement.ts +81 -0
- package/src/client/usage/settlementClient.ts +84 -0
- package/src/client/usage/summary.ts +12 -4
- package/src/client/usageExports.ts +11 -0
- package/src/component/_generated/api.ts +12 -0
- package/src/component/_generated/component.ts +440 -2
- package/src/component/accountClosure/mutations.ts +17 -0
- package/src/component/financialObservations/settlementHelpers.ts +2 -0
- package/src/component/schema.ts +8 -0
- package/src/component/settlement/mutations.ts +10 -1
- package/src/component/settlement/schema.ts +26 -0
- package/src/component/settlement/settlement-claims.test.ts +54 -0
- package/src/component/usage/schema.ts +2 -0
- package/src/component/usageAllowance/queries.ts +8 -1
- package/src/component/usageAllowance/schema.ts +1 -0
- package/src/component/usageCorrections/accounting.ts +79 -0
- package/src/component/usageCorrections/cancellation.ts +32 -0
- package/src/component/usageCorrections/corrections.test.ts +389 -0
- package/src/component/usageCorrections/helpers.ts +57 -0
- package/src/component/usageCorrections/invoice.ts +103 -0
- package/src/component/usageCorrections/mutations.ts +278 -0
- package/src/component/usageCorrections/queries.ts +94 -0
- package/src/component/usageCorrections/schema.ts +90 -0
- package/src/component/usageCredits/queries.ts +4 -0
- package/src/component/usageCredits/schema.ts +1 -0
- package/src/component/usageDelivery/fields.ts +1 -0
- package/src/component/usageDelivery/mutations.ts +8 -3
- package/src/component/usageDelivery/steps.ts +1 -1
- package/src/component/usageObservations/queries.ts +5 -2
- package/src/component/usageSubscriptions/retirement.ts +31 -2
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
|
+
import type { RunCtx } from "../helpers.js";
|
|
3
|
+
import { fixture } from "./creditSettlement.fixtures.js";
|
|
4
|
+
afterEach(() => vi.useRealTimers());
|
|
5
|
+
it("pays an issuer-service purchase affiliate once and never pays again for credit consumption", async () => {
|
|
6
|
+
const f = await fixture(),
|
|
7
|
+
{ native, client, options, view, input, settle } = f;
|
|
8
|
+
const first = await settle();
|
|
9
|
+
expect(first).toMatchObject({
|
|
10
|
+
status: "settled",
|
|
11
|
+
gross: 20,
|
|
12
|
+
processingFee: 1,
|
|
13
|
+
platformNet: 18,
|
|
14
|
+
creditSource: { purchaseId: "topup1", creditGrantId: "credgr_topup1" },
|
|
15
|
+
transfers: [{ role: "affiliate", amount: 1 }],
|
|
16
|
+
});
|
|
17
|
+
const event = {
|
|
18
|
+
scope: f.purchase.scope,
|
|
19
|
+
binding: f.purchase.binding,
|
|
20
|
+
sourceEventId: "consume1",
|
|
21
|
+
quantity: "6",
|
|
22
|
+
occurredAt: 700,
|
|
23
|
+
},
|
|
24
|
+
subscription = view.bindings[0].subscription;
|
|
25
|
+
const reservation = await f.t.action((ctx) =>
|
|
26
|
+
client.usage.reserveCredits(ctx as RunCtx, { event, subscription }),
|
|
27
|
+
);
|
|
28
|
+
await f.t.action((ctx) =>
|
|
29
|
+
client.usage.record(ctx as RunCtx, {
|
|
30
|
+
event,
|
|
31
|
+
subscription,
|
|
32
|
+
reservationId: reservation.reservationId,
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
expect(await settle()).toEqual(first);
|
|
36
|
+
expect(native).toHaveLength(1);
|
|
37
|
+
f.state.processingFee = 2;
|
|
38
|
+
expect(await settle()).toMatchObject({
|
|
39
|
+
status: "conflict",
|
|
40
|
+
reason: "identity_changed",
|
|
41
|
+
});
|
|
42
|
+
expect(native).toHaveLength(1);
|
|
43
|
+
f.state.processingFee = 1;
|
|
44
|
+
f.state.refunded = true;
|
|
45
|
+
expect(await settle()).toMatchObject({
|
|
46
|
+
status: "rejected",
|
|
47
|
+
reason: "payment_not_payable",
|
|
48
|
+
});
|
|
49
|
+
expect(native).toHaveLength(1);
|
|
50
|
+
for (const unsupported of [
|
|
51
|
+
{
|
|
52
|
+
...input,
|
|
53
|
+
policy: { ...input.policy, creatorAccountId: "acct_reseller" },
|
|
54
|
+
},
|
|
55
|
+
{ ...input, ownership: "reseller" },
|
|
56
|
+
])
|
|
57
|
+
expect(
|
|
58
|
+
await f.t.action((ctx) =>
|
|
59
|
+
client.usage.settleCreditPurchase(
|
|
60
|
+
ctx as RunCtx,
|
|
61
|
+
unsupported as typeof input,
|
|
62
|
+
options,
|
|
63
|
+
),
|
|
64
|
+
),
|
|
65
|
+
).toMatchObject({ status: "rejected", reason: "unsupported_source" });
|
|
66
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
2
|
+
import type { ResolvedSettlement } from "../settlement/execution.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { getCreditPurchaseRevenue } from "./creditRevenue.js";
|
|
5
|
+
import { allocateSale } from "../settlement/allocation.js";
|
|
6
|
+
import { executeResolvedTransfers } from "../settlement/execution.js";
|
|
7
|
+
import type Stripe from "stripe";
|
|
8
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
9
|
+
import type { UsageCreditPurchaseOptions } from "./creditTerms.js";
|
|
10
|
+
import type { SaleAllocationOptions } from "../settlement/allocation.js";
|
|
11
|
+
export type UsageCreditSettlementOptions = UsageCreditPurchaseOptions & {
|
|
12
|
+
policy: {
|
|
13
|
+
affiliate?: SaleAllocationOptions["affiliate"];
|
|
14
|
+
feeBasisPoints?: number;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const policySchema = z.strictObject({
|
|
18
|
+
feeBasisPoints: z.number().int().min(0).max(10000).optional(),
|
|
19
|
+
affiliate: z
|
|
20
|
+
.strictObject({
|
|
21
|
+
destinationAccountId: z.string().min(1).max(100),
|
|
22
|
+
basisPoints: z.number().int().min(0).max(10000).optional(),
|
|
23
|
+
fixedAmount: z
|
|
24
|
+
.number()
|
|
25
|
+
.int()
|
|
26
|
+
.nonnegative()
|
|
27
|
+
.max(Number.MAX_SAFE_INTEGER)
|
|
28
|
+
.optional(),
|
|
29
|
+
})
|
|
30
|
+
.optional(),
|
|
31
|
+
});
|
|
32
|
+
/** Retain issuer revenue; an authorized affiliate is paid from the original purchase only. No reseller redemption. */
|
|
33
|
+
export async function settleCreditPurchase(
|
|
34
|
+
stripe: Stripe,
|
|
35
|
+
component: Component,
|
|
36
|
+
ctx: RunCtx,
|
|
37
|
+
input: UsageCreditSettlementOptions,
|
|
38
|
+
) {
|
|
39
|
+
const { policy, ...purchase } = input;
|
|
40
|
+
const parsed = policySchema.safeParse(policy);
|
|
41
|
+
if (!parsed.success || purchase.ownership !== "issuer")
|
|
42
|
+
return {
|
|
43
|
+
status: "rejected" as const,
|
|
44
|
+
reason: "unsupported_source" as const,
|
|
45
|
+
retryable: false as const,
|
|
46
|
+
};
|
|
47
|
+
const source = await getCreditPurchaseRevenue(
|
|
48
|
+
stripe,
|
|
49
|
+
component,
|
|
50
|
+
ctx,
|
|
51
|
+
purchase,
|
|
52
|
+
);
|
|
53
|
+
if (source.status !== "ready") return source;
|
|
54
|
+
const value = source.value;
|
|
55
|
+
const prior = (await ctx.runQuery(
|
|
56
|
+
componentRef(component, "settlement/queries/getSettlementAllocation"),
|
|
57
|
+
{ sourceChargeId: value.chargeId },
|
|
58
|
+
)) as ResolvedSettlement | null;
|
|
59
|
+
if (
|
|
60
|
+
prior &&
|
|
61
|
+
(prior.gross !== value.gross ||
|
|
62
|
+
prior.currency !== value.currency ||
|
|
63
|
+
prior.processingFee !== value.processingFee)
|
|
64
|
+
)
|
|
65
|
+
return {
|
|
66
|
+
status: "conflict" as const,
|
|
67
|
+
reason: "identity_changed" as const,
|
|
68
|
+
retryable: false as const,
|
|
69
|
+
};
|
|
70
|
+
const allocation = allocateSale({
|
|
71
|
+
...parsed.data,
|
|
72
|
+
creatorAccountId: value.stripeAccountId,
|
|
73
|
+
gross: value.gross,
|
|
74
|
+
processingFee: value.processingFee,
|
|
75
|
+
});
|
|
76
|
+
return executeResolvedTransfers(stripe, component, ctx, {
|
|
77
|
+
sourceChargeId: value.chargeId,
|
|
78
|
+
paymentIntentId: value.paymentIntentId,
|
|
79
|
+
creditSource: {
|
|
80
|
+
stripeAccountId: value.stripeAccountId,
|
|
81
|
+
customerAccount: value.customerAccount,
|
|
82
|
+
livemode: value.livemode,
|
|
83
|
+
purchaseId: value.purchaseId,
|
|
84
|
+
creditGrantId: value.creditGrantId,
|
|
85
|
+
stripePriceId: value.stripePriceId,
|
|
86
|
+
stripeProductId: value.stripeProductId,
|
|
87
|
+
},
|
|
88
|
+
gross: value.gross,
|
|
89
|
+
currency: value.currency,
|
|
90
|
+
processingFee: value.processingFee,
|
|
91
|
+
platformNet: allocation.platformNet + allocation.creatorNet,
|
|
92
|
+
transfers: allocation.transfers.filter((leg) => leg.role !== "store"),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
3
|
+
import type { RunCtx } from "../helpers.js";
|
|
4
|
+
import { fixture } from "./creditSettlement.fixtures.js";
|
|
5
|
+
afterEach(() => vi.useRealTimers());
|
|
6
|
+
it("preserves original prepaid affiliate recovery through partial consumption/refund, dispute, won reinstatement and a held correction", async () => {
|
|
7
|
+
const f = await fixture({ fixedAmount: 7 });
|
|
8
|
+
expect(await f.settle()).toMatchObject({
|
|
9
|
+
status: "settled",
|
|
10
|
+
platformNet: 12,
|
|
11
|
+
});
|
|
12
|
+
const event = {
|
|
13
|
+
scope: f.purchase.scope,
|
|
14
|
+
binding: f.purchase.binding,
|
|
15
|
+
sourceEventId: "consume1",
|
|
16
|
+
quantity: "6",
|
|
17
|
+
occurredAt: 700,
|
|
18
|
+
},
|
|
19
|
+
subscription = f.view.bindings[0].subscription;
|
|
20
|
+
const reservation = await f.t.action((ctx) =>
|
|
21
|
+
f.client.usage.reserveCredits(ctx as RunCtx, { event, subscription }),
|
|
22
|
+
);
|
|
23
|
+
const receipt = await f.t.action((ctx) =>
|
|
24
|
+
f.client.usage.record(ctx as RunCtx, {
|
|
25
|
+
event,
|
|
26
|
+
subscription,
|
|
27
|
+
reservationId: reservation.reservationId,
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
const claim = (state: "refund" | "open" | "won") =>
|
|
31
|
+
f.t.mutation(
|
|
32
|
+
componentRef(f.component, "connect/mutations/claimReversalSlices"),
|
|
33
|
+
{
|
|
34
|
+
sourceChargeId: "ch_topup1",
|
|
35
|
+
operationId: state === "refund" ? "re_partial" : "dp_credit",
|
|
36
|
+
mode:
|
|
37
|
+
state === "refund"
|
|
38
|
+
? { kind: "fraction", chargeAmount: 20, amountRefunded: 10 }
|
|
39
|
+
: state === "won"
|
|
40
|
+
? { kind: "fraction", chargeAmount: 20, amountRefunded: 0 }
|
|
41
|
+
: { kind: "amount", amount: 20 },
|
|
42
|
+
commissionExposure:
|
|
43
|
+
state === "refund"
|
|
44
|
+
? {
|
|
45
|
+
kind: "refund",
|
|
46
|
+
chargeAmount: 20,
|
|
47
|
+
principal: 10,
|
|
48
|
+
finalized: true,
|
|
49
|
+
refundId: "re_partial",
|
|
50
|
+
}
|
|
51
|
+
: {
|
|
52
|
+
kind: "dispute",
|
|
53
|
+
chargeAmount: 20,
|
|
54
|
+
principal: 20,
|
|
55
|
+
finalized: false,
|
|
56
|
+
disputeState: state,
|
|
57
|
+
disputeId: "dp_credit",
|
|
58
|
+
refundedPrincipal: 10,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
const rows = async () =>
|
|
63
|
+
(await f.t.query(
|
|
64
|
+
componentRef(f.component, "settlement/queries/listAffiliateObligations"),
|
|
65
|
+
{
|
|
66
|
+
affiliateAccountId: "acct_affiliate",
|
|
67
|
+
currency: "usd",
|
|
68
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
69
|
+
},
|
|
70
|
+
)) as {
|
|
71
|
+
page: Array<{
|
|
72
|
+
_id: string;
|
|
73
|
+
finalizedAmount: number;
|
|
74
|
+
provisionalAmount: number;
|
|
75
|
+
recoveredAmount: number;
|
|
76
|
+
restoredAmount: number;
|
|
77
|
+
}>;
|
|
78
|
+
};
|
|
79
|
+
f.state.refundedAmount = 10;
|
|
80
|
+
await claim("refund");
|
|
81
|
+
await f.t.mutation(
|
|
82
|
+
componentRef(f.component, "connect/mutations/recordTransferReversal"),
|
|
83
|
+
{
|
|
84
|
+
stripeTransferId: "tr_0",
|
|
85
|
+
reversedAmount: 4,
|
|
86
|
+
receipt: {
|
|
87
|
+
stripeReversalId: "trr_partial",
|
|
88
|
+
operationId: "re_partial",
|
|
89
|
+
amount: 4,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
await claim("refund");
|
|
94
|
+
expect((await rows()).page[0]).toMatchObject({
|
|
95
|
+
finalizedAmount: 4,
|
|
96
|
+
recoveredAmount: 4,
|
|
97
|
+
});
|
|
98
|
+
await claim("open");
|
|
99
|
+
const obligationId = (await rows()).page[0]._id;
|
|
100
|
+
const recover = (await f.t.mutation(
|
|
101
|
+
componentRef(f.component, "affiliateRecovery/mutations/reserveOperation"),
|
|
102
|
+
{ obligationId, kind: "recovery", now: 700000 },
|
|
103
|
+
)) as { _id: string; amount: number };
|
|
104
|
+
expect(recover.amount).toBe(3);
|
|
105
|
+
await f.t.mutation(
|
|
106
|
+
componentRef(f.component, "affiliateRecovery/mutations/beginOperation"),
|
|
107
|
+
{ operationId: recover._id },
|
|
108
|
+
);
|
|
109
|
+
await f.t.mutation(
|
|
110
|
+
componentRef(f.component, "affiliateRecovery/mutations/completeOperation"),
|
|
111
|
+
{
|
|
112
|
+
operationId: recover._id,
|
|
113
|
+
state: "succeeded",
|
|
114
|
+
receiptId: "trr_dispute",
|
|
115
|
+
confirmedTransferReversed: 7,
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
await claim("won");
|
|
119
|
+
const restore = (await f.t.mutation(
|
|
120
|
+
componentRef(f.component, "affiliateRecovery/mutations/reserveOperation"),
|
|
121
|
+
{ obligationId, kind: "restoration", now: 700000 },
|
|
122
|
+
)) as { _id: string; amount: number };
|
|
123
|
+
expect(restore.amount).toBe(3);
|
|
124
|
+
await f.t.mutation(
|
|
125
|
+
componentRef(f.component, "affiliateRecovery/mutations/beginOperation"),
|
|
126
|
+
{ operationId: restore._id },
|
|
127
|
+
);
|
|
128
|
+
const confirmation = {
|
|
129
|
+
operationId: restore._id,
|
|
130
|
+
state: "succeeded",
|
|
131
|
+
receiptId: "tr_restore",
|
|
132
|
+
};
|
|
133
|
+
await f.t.mutation(
|
|
134
|
+
componentRef(f.component, "affiliateRecovery/mutations/completeOperation"),
|
|
135
|
+
confirmation,
|
|
136
|
+
);
|
|
137
|
+
await f.t.mutation(
|
|
138
|
+
componentRef(f.component, "affiliateRecovery/mutations/completeOperation"),
|
|
139
|
+
confirmation,
|
|
140
|
+
);
|
|
141
|
+
expect((await rows()).page[0]).toMatchObject({
|
|
142
|
+
finalizedAmount: 4,
|
|
143
|
+
provisionalAmount: 0,
|
|
144
|
+
recoveredAmount: 7,
|
|
145
|
+
restoredAmount: 3,
|
|
146
|
+
});
|
|
147
|
+
expect(await f.settle()).toMatchObject({
|
|
148
|
+
status: "rejected",
|
|
149
|
+
reason: "payment_not_payable",
|
|
150
|
+
});
|
|
151
|
+
expect(
|
|
152
|
+
await f.t.action((ctx) =>
|
|
153
|
+
f.client.usage.requestCorrection(
|
|
154
|
+
ctx as RunCtx,
|
|
155
|
+
{
|
|
156
|
+
scope: event.scope,
|
|
157
|
+
eventId: receipt.eventId,
|
|
158
|
+
correctionId: "after_refund",
|
|
159
|
+
actor: "operator1",
|
|
160
|
+
reason: "duplicate activity",
|
|
161
|
+
operation: "cancel",
|
|
162
|
+
},
|
|
163
|
+
f.options,
|
|
164
|
+
),
|
|
165
|
+
),
|
|
166
|
+
).toMatchObject({ state: "held", restoredQuantity: "0" });
|
|
167
|
+
expect(
|
|
168
|
+
await f.t.query((ctx) =>
|
|
169
|
+
f.client.usage.getCreditBalance(ctx as RunCtx, f.purchase),
|
|
170
|
+
),
|
|
171
|
+
).toMatchObject({ consumedQuantity: "6" });
|
|
172
|
+
expect(f.native).toHaveLength(1);
|
|
173
|
+
});
|
|
@@ -7,7 +7,12 @@ const response = (value: unknown) =>
|
|
|
7
7
|
new Response(JSON.stringify(value), {
|
|
8
8
|
headers: { "content-type": "application/json" },
|
|
9
9
|
});
|
|
10
|
-
export function creditFixture(
|
|
10
|
+
export function creditFixture(
|
|
11
|
+
intercept?: (
|
|
12
|
+
input: string,
|
|
13
|
+
init?: RequestInit,
|
|
14
|
+
) => Promise<Response | undefined>,
|
|
15
|
+
) {
|
|
11
16
|
vi.useFakeTimers();
|
|
12
17
|
vi.setSystemTime(700000);
|
|
13
18
|
const calls: Array<{
|
|
@@ -25,6 +30,12 @@ export function creditFixture() {
|
|
|
25
30
|
incomplete: false,
|
|
26
31
|
canceled: false,
|
|
27
32
|
refunded: false,
|
|
33
|
+
refundedAmount: 0,
|
|
34
|
+
disputed: false,
|
|
35
|
+
feePending: false,
|
|
36
|
+
processingFee: 1,
|
|
37
|
+
paymentCustomer: "cus_buyer",
|
|
38
|
+
chargeCustomer: "cus_buyer",
|
|
28
39
|
failGrant: false,
|
|
29
40
|
wrongPayer: false,
|
|
30
41
|
paymentId: "pi_topup1",
|
|
@@ -32,6 +43,8 @@ export function creditFixture() {
|
|
|
32
43
|
metadata: {} as Record<string, string>,
|
|
33
44
|
};
|
|
34
45
|
const f = fixture(false, false, async (input, init) => {
|
|
46
|
+
const override = await intercept?.(input, init);
|
|
47
|
+
if (override) return override;
|
|
35
48
|
const path = new URL(input).pathname,
|
|
36
49
|
method = init?.method ?? "GET",
|
|
37
50
|
body = new URLSearchParams(String(init?.body ?? ""));
|
|
@@ -219,7 +232,7 @@ export function creditFixture() {
|
|
|
219
232
|
id: state.paymentId,
|
|
220
233
|
object: "payment_intent",
|
|
221
234
|
customer_account: state.wrongPayer ? "acct_other" : "acct_buyer",
|
|
222
|
-
customer:
|
|
235
|
+
customer: state.paymentCustomer,
|
|
223
236
|
livemode: false,
|
|
224
237
|
currency: "usd",
|
|
225
238
|
amount: 20,
|
|
@@ -244,8 +257,20 @@ export function creditFixture() {
|
|
|
244
257
|
currency: "usd",
|
|
245
258
|
livemode: false,
|
|
246
259
|
refunded: state.refunded,
|
|
247
|
-
amount_refunded: state.refunded ? 20 :
|
|
248
|
-
disputed:
|
|
260
|
+
amount_refunded: state.refunded ? 20 : state.refundedAmount,
|
|
261
|
+
disputed: state.disputed,
|
|
262
|
+
customer: state.chargeCustomer,
|
|
263
|
+
balance_transaction: state.feePending
|
|
264
|
+
? null
|
|
265
|
+
: {
|
|
266
|
+
id: "txn_topup1",
|
|
267
|
+
object: "balance_transaction",
|
|
268
|
+
source: "ch_topup1",
|
|
269
|
+
currency: "usd",
|
|
270
|
+
amount: 20,
|
|
271
|
+
fee: state.processingFee,
|
|
272
|
+
net: 20 - state.processingFee,
|
|
273
|
+
},
|
|
249
274
|
}
|
|
250
275
|
: null,
|
|
251
276
|
};
|
|
@@ -88,3 +88,23 @@ export type {
|
|
|
88
88
|
UsageLimitPolicy,
|
|
89
89
|
} from "./allowanceTerms.js";
|
|
90
90
|
export type { UsageAllowanceReservation } from "./allowanceClient.js";
|
|
91
|
+
|
|
92
|
+
export type {
|
|
93
|
+
UsageCorrectionOptions,
|
|
94
|
+
UsageCorrectionView,
|
|
95
|
+
UsageInvoiceCorrectionOptions,
|
|
96
|
+
UsageInvoiceCorrectionEvidence,
|
|
97
|
+
} from "./correctionTerms.js";
|
|
98
|
+
export type {
|
|
99
|
+
UsageCorrectionHistoryPage,
|
|
100
|
+
UsageCorrectionHistoryRecord,
|
|
101
|
+
} from "./correctionRead.js";
|
|
102
|
+
|
|
103
|
+
export type {
|
|
104
|
+
UsageInvoiceSaleOptions,
|
|
105
|
+
VerifiedUsageInvoiceSale,
|
|
106
|
+
} from "./invoiceSale.js";
|
|
107
|
+
export type { UsageInvoiceSettlementOptions } from "./settlement.js";
|
|
108
|
+
export type { UsageCreditRevenue } from "./creditRevenue.js";
|
|
109
|
+
|
|
110
|
+
export type { UsageCreditSettlementOptions } from "./creditSettlement.js";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { fixture as sourceFixture } from "../paymentSource/resolver.fixtures.js";
|
|
2
|
+
import { prepareUsageRecord } from "./records.js";
|
|
3
|
+
export function fixture() {
|
|
4
|
+
const f = sourceFixture();
|
|
5
|
+
const scope = {
|
|
6
|
+
stripeAccountId: "acct_platform",
|
|
7
|
+
customerAccount: "acct_customer",
|
|
8
|
+
meterId: "mtr_calls",
|
|
9
|
+
livemode: false,
|
|
10
|
+
};
|
|
11
|
+
const period = { start: 600, end: 1200 };
|
|
12
|
+
const binding = prepareUsageRecord({
|
|
13
|
+
scope,
|
|
14
|
+
binding: {
|
|
15
|
+
stripeCustomerId: "cus_buyer",
|
|
16
|
+
stripePriceId: "price_original",
|
|
17
|
+
eventName: "calls",
|
|
18
|
+
plan: {
|
|
19
|
+
key: "calls",
|
|
20
|
+
version: "v1",
|
|
21
|
+
model: "pay_as_you_go",
|
|
22
|
+
unit: { name: "call", precision: 0 },
|
|
23
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
sourceEventId: "proof",
|
|
27
|
+
quantity: "0",
|
|
28
|
+
occurredAt: 600,
|
|
29
|
+
}).binding;
|
|
30
|
+
const meter = {
|
|
31
|
+
id: "mtr_calls",
|
|
32
|
+
livemode: false,
|
|
33
|
+
status: "active",
|
|
34
|
+
event_name: "calls",
|
|
35
|
+
default_aggregation: { formula: "sum" },
|
|
36
|
+
customer_mapping: {
|
|
37
|
+
type: "by_id",
|
|
38
|
+
event_payload_key: "stripe_customer_id",
|
|
39
|
+
},
|
|
40
|
+
value_settings: { event_payload_key: "value" },
|
|
41
|
+
event_time_window: null,
|
|
42
|
+
};
|
|
43
|
+
const price = {
|
|
44
|
+
id: "price_original",
|
|
45
|
+
active: true,
|
|
46
|
+
livemode: false,
|
|
47
|
+
currency: "usd",
|
|
48
|
+
type: "recurring",
|
|
49
|
+
product: "prod_original",
|
|
50
|
+
lookup_key: "calls",
|
|
51
|
+
billing_scheme: "per_unit",
|
|
52
|
+
unit_amount_decimal: "2",
|
|
53
|
+
transform_quantity: null,
|
|
54
|
+
recurring: {
|
|
55
|
+
interval: "month",
|
|
56
|
+
interval_count: 1,
|
|
57
|
+
usage_type: "metered",
|
|
58
|
+
meter: "mtr_calls",
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
f.extra.set("/v1/account", { id: "acct_platform" });
|
|
62
|
+
f.extra.set("/v1/balance", { livemode: false });
|
|
63
|
+
f.extra.set("/v1/billing/meters/mtr_calls", meter);
|
|
64
|
+
f.extra.set("/v1/customers/acct_customer", {
|
|
65
|
+
id: "cus_buyer",
|
|
66
|
+
customer_account: "acct_customer",
|
|
67
|
+
livemode: false,
|
|
68
|
+
});
|
|
69
|
+
f.extra.set("/v1/prices/price_original", price);
|
|
70
|
+
Object.assign(f.payment, { customer: "cus_buyer" });
|
|
71
|
+
Object.assign(f.payment.latest_charge, {
|
|
72
|
+
customer: "cus_buyer",
|
|
73
|
+
refunded: false,
|
|
74
|
+
});
|
|
75
|
+
Object.assign(f.subscription, { customer: "cus_buyer" });
|
|
76
|
+
Object.assign(f.line, {
|
|
77
|
+
period,
|
|
78
|
+
quantity: null,
|
|
79
|
+
quantity_decimal: "500",
|
|
80
|
+
pretax_credit_amounts: [],
|
|
81
|
+
});
|
|
82
|
+
Object.assign(f.invoice, {
|
|
83
|
+
customer: "cus_buyer",
|
|
84
|
+
status_transitions: { finalized_at: 1200 },
|
|
85
|
+
lines: { data: [f.line], has_more: false },
|
|
86
|
+
});
|
|
87
|
+
const input = {
|
|
88
|
+
scope,
|
|
89
|
+
binding,
|
|
90
|
+
subscription: {
|
|
91
|
+
subscriptionId: "sub_source",
|
|
92
|
+
subscriptionItemId: "si_source",
|
|
93
|
+
stripePriceId: "price_original",
|
|
94
|
+
period,
|
|
95
|
+
interval: "month" as const,
|
|
96
|
+
},
|
|
97
|
+
period,
|
|
98
|
+
invoiceId: "in_source",
|
|
99
|
+
paymentIntentId: "pi_source",
|
|
100
|
+
gross: 1000,
|
|
101
|
+
};
|
|
102
|
+
return { ...f, meter, price, input };
|
|
103
|
+
}
|