@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,156 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
4
|
+
import { BetterStripe } from "../index.js";
|
|
5
|
+
import type { RunCtx } from "../helpers.js";
|
|
6
|
+
import { creditFixture } from "./creditTransport.fixtures.js";
|
|
7
|
+
afterEach(() => vi.useRealTimers());
|
|
8
|
+
async function ready() {
|
|
9
|
+
const f = creditFixture(),
|
|
10
|
+
client = new BetterStripe(f.component),
|
|
11
|
+
options = { stripeSecretKey: "sk_test_credit", httpClient: f.httpClient };
|
|
12
|
+
const view = await f.ready();
|
|
13
|
+
await f.t.action((ctx) =>
|
|
14
|
+
client.usage.purchaseCredits(ctx as RunCtx, f.purchase, options),
|
|
15
|
+
);
|
|
16
|
+
f.state.paid = true;
|
|
17
|
+
await f.t.action((ctx) =>
|
|
18
|
+
client.usage.observeCreditPurchase(ctx as RunCtx, f.purchase, options),
|
|
19
|
+
);
|
|
20
|
+
const event = {
|
|
21
|
+
scope: f.purchase.scope,
|
|
22
|
+
binding: f.purchase.binding,
|
|
23
|
+
sourceEventId: "work1",
|
|
24
|
+
quantity: "6",
|
|
25
|
+
occurredAt: 700,
|
|
26
|
+
},
|
|
27
|
+
subscription = view.bindings[0].subscription;
|
|
28
|
+
const reservation = await f.t.action((ctx) =>
|
|
29
|
+
client.usage.reserveCredits(ctx as RunCtx, { event, subscription }),
|
|
30
|
+
);
|
|
31
|
+
const receipt = await f.t.action((ctx) =>
|
|
32
|
+
client.usage.record(ctx as RunCtx, {
|
|
33
|
+
event,
|
|
34
|
+
subscription,
|
|
35
|
+
reservationId: reservation.reservationId,
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
const correction = {
|
|
39
|
+
scope: event.scope,
|
|
40
|
+
eventId: receipt.eventId,
|
|
41
|
+
correctionId: "fix1",
|
|
42
|
+
actor: "operator1",
|
|
43
|
+
reason: "duplicate activity",
|
|
44
|
+
operation: "cancel",
|
|
45
|
+
} as const;
|
|
46
|
+
return { f, client, options, correction };
|
|
47
|
+
}
|
|
48
|
+
it("restores original paid-grant quantity once for an unsent cancellation without minting a grant or refund", async () => {
|
|
49
|
+
const { f, client, options, correction } = await ready();
|
|
50
|
+
expect(
|
|
51
|
+
await f.t.action((ctx) =>
|
|
52
|
+
client.usage.requestCorrection(ctx as RunCtx, correction, options),
|
|
53
|
+
),
|
|
54
|
+
).toMatchObject({
|
|
55
|
+
state: "confirmed",
|
|
56
|
+
restoredQuantity: "6",
|
|
57
|
+
cashConsequence: "none",
|
|
58
|
+
});
|
|
59
|
+
expect(
|
|
60
|
+
await f.t.action((ctx) =>
|
|
61
|
+
client.usage.requestCorrection(ctx as RunCtx, correction, options),
|
|
62
|
+
),
|
|
63
|
+
).toMatchObject({ state: "confirmed", restoredQuantity: "6" });
|
|
64
|
+
expect(
|
|
65
|
+
await f.t.query((ctx) =>
|
|
66
|
+
client.usage.getCreditBalance(ctx as RunCtx, f.purchase),
|
|
67
|
+
),
|
|
68
|
+
).toMatchObject({
|
|
69
|
+
availableQuantity: "10",
|
|
70
|
+
consumedQuantity: "0",
|
|
71
|
+
status: "available",
|
|
72
|
+
});
|
|
73
|
+
expect(
|
|
74
|
+
f.calls.filter(
|
|
75
|
+
(c) => c.path === "/v1/billing/credit_grants" && c.method === "POST",
|
|
76
|
+
),
|
|
77
|
+
).toHaveLength(1);
|
|
78
|
+
expect(
|
|
79
|
+
f.calls.filter((c) => c.path.includes("refund") && c.method === "POST"),
|
|
80
|
+
).toHaveLength(0);
|
|
81
|
+
});
|
|
82
|
+
it("holds prepaid restoration if its original purchase was refunded or its native credit is unavailable", async () => {
|
|
83
|
+
for (const recovery of ["refund", "unavailable"]) {
|
|
84
|
+
const { f, client, options, correction } = await ready();
|
|
85
|
+
if (recovery === "refund") f.state.refunded = true;
|
|
86
|
+
else f.state.nativeAvailable = 0;
|
|
87
|
+
expect(
|
|
88
|
+
await f.t.action((ctx) =>
|
|
89
|
+
client.usage.requestCorrection(ctx as RunCtx, correction, options),
|
|
90
|
+
),
|
|
91
|
+
).toMatchObject({ state: "held", restoredQuantity: "0" });
|
|
92
|
+
expect(
|
|
93
|
+
await f.t.query((ctx) =>
|
|
94
|
+
client.usage.getCreditBalance(ctx as RunCtx, f.purchase),
|
|
95
|
+
),
|
|
96
|
+
).toMatchObject({ consumedQuantity: "6" });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
it("can verify and cancel provably unsent prepaid usage after a delayed credential recovery", async () => {
|
|
100
|
+
const { f, client, options, correction } = await ready();
|
|
101
|
+
expect(
|
|
102
|
+
await f.t.action((ctx) =>
|
|
103
|
+
client.usage.requestCorrection(ctx as RunCtx, correction),
|
|
104
|
+
),
|
|
105
|
+
).toMatchObject({ state: "held", restoredQuantity: "0" });
|
|
106
|
+
vi.advanceTimersByTime(24 * 3600 * 1000);
|
|
107
|
+
expect(
|
|
108
|
+
await f.t.action((ctx) =>
|
|
109
|
+
client.usage.requestCorrection(ctx as RunCtx, correction, options),
|
|
110
|
+
),
|
|
111
|
+
).toMatchObject({ state: "confirmed", restoredQuantity: "6" });
|
|
112
|
+
expect(
|
|
113
|
+
await f.t.query((ctx) =>
|
|
114
|
+
client.usage.getCreditBalance(ctx as RunCtx, f.purchase),
|
|
115
|
+
),
|
|
116
|
+
).toMatchObject({ availableQuantity: "10", consumedQuantity: "0" });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("restores a known rejected prepaid delivery only after fresh backing verification", async () => {
|
|
120
|
+
for (const refunded of [false, true]) {
|
|
121
|
+
const { f, client, options, correction } = await ready();
|
|
122
|
+
workflowTest.register(f.t, "betterStripe/workflow");
|
|
123
|
+
const delivery = { scope: correction.scope, eventId: correction.eventId };
|
|
124
|
+
await f.t.mutation(
|
|
125
|
+
componentRef(f.component, "usageDelivery/mutations/start"),
|
|
126
|
+
{
|
|
127
|
+
...delivery,
|
|
128
|
+
provider: "function://provider",
|
|
129
|
+
definitionVersion: 1,
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
const claim = (await f.t.mutation(
|
|
133
|
+
componentRef(f.component, "usageDelivery/steps/claim"),
|
|
134
|
+
delivery,
|
|
135
|
+
)) as { request: Record<string, unknown> };
|
|
136
|
+
await f.t.mutation(componentRef(f.component, "usageDelivery/steps/save"), {
|
|
137
|
+
...claim.request,
|
|
138
|
+
outcome: { status: "rejected" },
|
|
139
|
+
});
|
|
140
|
+
f.state.refunded = refunded;
|
|
141
|
+
expect(
|
|
142
|
+
await f.t.action((ctx) =>
|
|
143
|
+
client.usage.requestCorrection(ctx as RunCtx, correction, options),
|
|
144
|
+
),
|
|
145
|
+
).toMatchObject({
|
|
146
|
+
state: refunded ? "held" : "confirmed",
|
|
147
|
+
restoredQuantity: refunded ? "0" : "6",
|
|
148
|
+
});
|
|
149
|
+
expect(
|
|
150
|
+
f.calls.filter(
|
|
151
|
+
(c) =>
|
|
152
|
+
c.path.includes("meter_event_adjustments") && c.method === "POST",
|
|
153
|
+
),
|
|
154
|
+
).toHaveLength(0);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Doc } from "../../component/_generated/dataModel.js";
|
|
2
|
+
/** Local restoration requires proof that no native meter event was accepted. */
|
|
3
|
+
export function locallyCancelable(
|
|
4
|
+
outbox: Pick<
|
|
5
|
+
Doc<"usageOutbox">,
|
|
6
|
+
"attempts" | "firstSendAt" | "receipt" | "state" | "reason"
|
|
7
|
+
>,
|
|
8
|
+
): boolean {
|
|
9
|
+
return (
|
|
10
|
+
!outbox.receipt &&
|
|
11
|
+
((outbox.attempts === 0 && outbox.firstSendAt === undefined) ||
|
|
12
|
+
(outbox.state === "rejected" && outbox.reason === "provider_rejected"))
|
|
13
|
+
);
|
|
14
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
|
+
import type { RunCtx } from "../helpers.js";
|
|
3
|
+
import { setupCorrection } from "./correctionTransport.fixtures.js";
|
|
4
|
+
afterEach(() => vi.useRealTimers());
|
|
5
|
+
async function partial() {
|
|
6
|
+
const f = await setupCorrection();
|
|
7
|
+
await f.f.t.action((ctx) =>
|
|
8
|
+
f.client.usage.requestCorrection(
|
|
9
|
+
ctx as RunCtx,
|
|
10
|
+
{ ...f.correction, quantity: "2" },
|
|
11
|
+
f.options,
|
|
12
|
+
),
|
|
13
|
+
);
|
|
14
|
+
const input = {
|
|
15
|
+
scope: f.correction.scope,
|
|
16
|
+
eventId: f.correction.eventId,
|
|
17
|
+
correctionId: f.correction.correctionId,
|
|
18
|
+
invoiceId: "in_usage",
|
|
19
|
+
creditNoteId: "cn_fix",
|
|
20
|
+
amount: 4,
|
|
21
|
+
};
|
|
22
|
+
return { ...f, input };
|
|
23
|
+
}
|
|
24
|
+
it("verifies the original paid invoice, invoice payment, charge and succeeded refund before recording cash adjustment provenance", async () => {
|
|
25
|
+
const { f, client, options, state, input } = await partial();
|
|
26
|
+
state.paidAdjustment = true;
|
|
27
|
+
expect(
|
|
28
|
+
await f.t.action((ctx) =>
|
|
29
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
30
|
+
),
|
|
31
|
+
).toMatchObject({
|
|
32
|
+
state: "confirmed",
|
|
33
|
+
creditNoteId: "cn_fix",
|
|
34
|
+
refundId: "re_fix",
|
|
35
|
+
restoredQuantity: "0",
|
|
36
|
+
resolution: "invoice_credit",
|
|
37
|
+
cashCollection: "none",
|
|
38
|
+
invoiceAdjustment: {
|
|
39
|
+
amount: 4,
|
|
40
|
+
currency: "usd",
|
|
41
|
+
postPaymentAmount: 4,
|
|
42
|
+
paymentIntentId: "pi_invoice",
|
|
43
|
+
chargeId: "ch_invoice",
|
|
44
|
+
refundId: "re_fix",
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it("holds foreign, incomplete, pending-refund and wrong-line native credit evidence", async () => {
|
|
49
|
+
for (const invalid of [
|
|
50
|
+
"foreignNote",
|
|
51
|
+
"truncatedNote",
|
|
52
|
+
"foreignLine",
|
|
53
|
+
"pendingRefund",
|
|
54
|
+
] as const) {
|
|
55
|
+
const { f, client, options, state, input } = await partial();
|
|
56
|
+
if (invalid === "pendingRefund") {
|
|
57
|
+
state.paidAdjustment = true;
|
|
58
|
+
state.refundSucceeded = false;
|
|
59
|
+
} else state[invalid] = true;
|
|
60
|
+
expect(
|
|
61
|
+
await f.t.action((ctx) =>
|
|
62
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
63
|
+
),
|
|
64
|
+
).toMatchObject({ status: "held" });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
it("does not claim the same native credit note for a second correction operation", async () => {
|
|
68
|
+
const { f, client, options, correction, input } = await partial();
|
|
69
|
+
await f.t.action((ctx) =>
|
|
70
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
71
|
+
);
|
|
72
|
+
await f.t.action((ctx) =>
|
|
73
|
+
client.usage.requestCorrection(
|
|
74
|
+
ctx as RunCtx,
|
|
75
|
+
{ ...correction, correctionId: "fix2", quantity: "1" },
|
|
76
|
+
options,
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
await expect(
|
|
80
|
+
f.t.action((ctx) =>
|
|
81
|
+
client.usage.observeInvoiceCorrection(
|
|
82
|
+
ctx as RunCtx,
|
|
83
|
+
{ ...input, correctionId: "fix2" },
|
|
84
|
+
options,
|
|
85
|
+
),
|
|
86
|
+
),
|
|
87
|
+
).rejects.toThrow(/already claimed/i);
|
|
88
|
+
});
|
|
89
|
+
it("records a verified pre-payment credit-note void without refunding cash or restoring valid usage", async () => {
|
|
90
|
+
const { f, client, options, state, input } = await partial();
|
|
91
|
+
await f.t.action((ctx) =>
|
|
92
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
93
|
+
);
|
|
94
|
+
state.voidedNote = true;
|
|
95
|
+
expect(
|
|
96
|
+
await f.t.action((ctx) =>
|
|
97
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
98
|
+
),
|
|
99
|
+
).toMatchObject({
|
|
100
|
+
state: "confirmed",
|
|
101
|
+
nativeCreditNoteStatus: "void",
|
|
102
|
+
reason: "native_invoice_credit_voided",
|
|
103
|
+
cashConsequence: "none",
|
|
104
|
+
restoredQuantity: "0",
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("rejects unsafe native payment and charge amounts instead of normalizing a refund proof", async () => {
|
|
109
|
+
const { f, client, options, state, input } = await partial();
|
|
110
|
+
state.paidAdjustment = true;
|
|
111
|
+
state.invalidCash = true;
|
|
112
|
+
expect(
|
|
113
|
+
await f.t.action((ctx) =>
|
|
114
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
115
|
+
),
|
|
116
|
+
).toMatchObject({ status: "held" });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("does not treat missing native tax and page evidence as a known empty financial history", async () => {
|
|
120
|
+
const { f, client, options, state, input } = await partial();
|
|
121
|
+
state.missingEvidence = true;
|
|
122
|
+
expect(
|
|
123
|
+
await f.t.action((ctx) =>
|
|
124
|
+
client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
|
|
125
|
+
),
|
|
126
|
+
).toMatchObject({ status: "held" });
|
|
127
|
+
});
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
3
|
+
import { runMutationOrThrow } from "../helpers.js";
|
|
4
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
5
|
+
import type { Doc } from "../../component/_generated/dataModel.js";
|
|
6
|
+
import type { PreparedUsageRecord } from "./records.js";
|
|
7
|
+
import type { UsageDeliveryProviderOptions } from "./deliveryProvider.js";
|
|
8
|
+
import type { StripeApiVersion } from "../stripe-types.js";
|
|
9
|
+
import { verifyRealm } from "./catalogTypes.js";
|
|
10
|
+
import { subscriptionSnapshot } from "../../component/usage/helpers.js";
|
|
11
|
+
import {
|
|
12
|
+
invoiceCorrectionOptions,
|
|
13
|
+
correctionView,
|
|
14
|
+
type UsageInvoiceCorrectionOptions,
|
|
15
|
+
} from "./correctionTerms.js";
|
|
16
|
+
import { invoiceCorrectionProof } from "./correctionInvoiceProof.js";
|
|
17
|
+
import {
|
|
18
|
+
referenceId,
|
|
19
|
+
paymentReference,
|
|
20
|
+
} from "../billing/invoicePaymentShared.js";
|
|
21
|
+
const requireProof = (valid: unknown) => {
|
|
22
|
+
if (!valid)
|
|
23
|
+
throw new Error("Native invoice correction requires reconciliation");
|
|
24
|
+
};
|
|
25
|
+
/** Read-only provider adoption. The operator creates notes/refunds through existing authorized billing workflows. */
|
|
26
|
+
export async function observeInvoiceCorrection(
|
|
27
|
+
component: Component,
|
|
28
|
+
ctx: RunCtx,
|
|
29
|
+
input: UsageInvoiceCorrectionOptions,
|
|
30
|
+
options: UsageDeliveryProviderOptions,
|
|
31
|
+
) {
|
|
32
|
+
const opts = invoiceCorrectionOptions.parse(input);
|
|
33
|
+
const row = (await ctx.runQuery(
|
|
34
|
+
componentRef(component, "usageCorrections/queries/get"),
|
|
35
|
+
{
|
|
36
|
+
scope: opts.scope,
|
|
37
|
+
eventId: opts.eventId,
|
|
38
|
+
correctionId: opts.correctionId,
|
|
39
|
+
},
|
|
40
|
+
)) as Doc<"usageCorrections"> | null;
|
|
41
|
+
if (!row) throw new Error("Usage correction unavailable");
|
|
42
|
+
if (row.adjustmentJson) {
|
|
43
|
+
const pinned = invoiceCorrectionProof.parse(JSON.parse(row.adjustmentJson));
|
|
44
|
+
if (
|
|
45
|
+
pinned.creditNoteId !== opts.creditNoteId ||
|
|
46
|
+
pinned.invoiceId !== opts.invoiceId ||
|
|
47
|
+
pinned.amount !== opts.amount
|
|
48
|
+
)
|
|
49
|
+
throw new Error("Invoice adjustment replay conflict");
|
|
50
|
+
}
|
|
51
|
+
const record = JSON.parse(row.recordJson) as PreparedUsageRecord,
|
|
52
|
+
sub = subscriptionSnapshot(row.subscriptionJson);
|
|
53
|
+
if (!options.stripeSecretKey || record.binding.plan.model === "prepaid")
|
|
54
|
+
return {
|
|
55
|
+
status: "held" as const,
|
|
56
|
+
reason:
|
|
57
|
+
record.binding.plan.model === "prepaid"
|
|
58
|
+
? "prepaid_credit_reconciliation_required"
|
|
59
|
+
: "provider_unavailable",
|
|
60
|
+
};
|
|
61
|
+
const stripe = new Stripe(options.stripeSecretKey, {
|
|
62
|
+
apiVersion: record.binding.apiVersion as StripeApiVersion,
|
|
63
|
+
maxNetworkRetries: 0,
|
|
64
|
+
timeout: 15000,
|
|
65
|
+
...(options.httpClient ? { httpClient: options.httpClient } : {}),
|
|
66
|
+
});
|
|
67
|
+
let proof: ReturnType<typeof invoiceCorrectionProof.parse>;
|
|
68
|
+
try {
|
|
69
|
+
await verifyRealm(stripe, {
|
|
70
|
+
stripeAccountId: record.scope.stripeAccountId,
|
|
71
|
+
livemode: record.scope.livemode,
|
|
72
|
+
});
|
|
73
|
+
const [note, invoice, customer] = await Promise.all([
|
|
74
|
+
stripe.creditNotes.retrieve(opts.creditNoteId),
|
|
75
|
+
stripe.invoices.retrieve(opts.invoiceId),
|
|
76
|
+
stripe.customers.retrieve(record.scope.customerAccount),
|
|
77
|
+
]);
|
|
78
|
+
requireProof(
|
|
79
|
+
!customer.deleted &&
|
|
80
|
+
customer.id === record.binding.stripeCustomerId &&
|
|
81
|
+
customer.customer_account === record.scope.customerAccount &&
|
|
82
|
+
customer.livemode === record.scope.livemode,
|
|
83
|
+
);
|
|
84
|
+
requireProof(
|
|
85
|
+
note.id === opts.creditNoteId &&
|
|
86
|
+
note.object === "credit_note" &&
|
|
87
|
+
note.livemode === record.scope.livemode &&
|
|
88
|
+
note.customer_account === record.scope.customerAccount &&
|
|
89
|
+
referenceId(note.customer) === record.binding.stripeCustomerId &&
|
|
90
|
+
referenceId(note.invoice) === opts.invoiceId &&
|
|
91
|
+
(note.status === "issued" ||
|
|
92
|
+
(note.status === "void" &&
|
|
93
|
+
row.adjustmentJson &&
|
|
94
|
+
note.pre_payment_amount === note.amount &&
|
|
95
|
+
note.post_payment_amount === 0)) &&
|
|
96
|
+
note.currency === record.binding.plan.rate.currency &&
|
|
97
|
+
note.amount === opts.amount &&
|
|
98
|
+
note.total === note.amount &&
|
|
99
|
+
note.subtotal === note.amount &&
|
|
100
|
+
note.discount_amount === 0 &&
|
|
101
|
+
note.amount_shipping === 0 &&
|
|
102
|
+
Array.isArray(note.total_taxes) &&
|
|
103
|
+
note.total_taxes.length === 0 &&
|
|
104
|
+
note.customer_balance_transaction === null &&
|
|
105
|
+
(note.out_of_band_amount === null || note.out_of_band_amount === 0) &&
|
|
106
|
+
Array.isArray(note.pretax_credit_amounts) &&
|
|
107
|
+
note.pretax_credit_amounts.length === 0 &&
|
|
108
|
+
note.lines.has_more === false &&
|
|
109
|
+
note.lines.data.length === 1 &&
|
|
110
|
+
note.amount === note.pre_payment_amount + note.post_payment_amount,
|
|
111
|
+
);
|
|
112
|
+
requireProof(
|
|
113
|
+
invoice.id === opts.invoiceId &&
|
|
114
|
+
invoice.livemode === record.scope.livemode &&
|
|
115
|
+
invoice.customer_account === record.scope.customerAccount &&
|
|
116
|
+
referenceId(invoice.customer) === record.binding.stripeCustomerId &&
|
|
117
|
+
invoice.currency === record.binding.plan.rate.currency &&
|
|
118
|
+
["open", "paid"].includes(invoice.status ?? "") &&
|
|
119
|
+
referenceId(invoice.parent?.subscription_details?.subscription) ===
|
|
120
|
+
sub.subscriptionId &&
|
|
121
|
+
invoice.lines.has_more === false,
|
|
122
|
+
);
|
|
123
|
+
const noteLine = note.lines.data[0],
|
|
124
|
+
line = invoice.lines.data.find(
|
|
125
|
+
(line) => line.id === noteLine.invoice_line_item,
|
|
126
|
+
);
|
|
127
|
+
requireProof(
|
|
128
|
+
noteLine.type === "invoice_line_item" &&
|
|
129
|
+
noteLine.livemode === record.scope.livemode &&
|
|
130
|
+
noteLine.amount === opts.amount &&
|
|
131
|
+
noteLine.discount_amount === 0 &&
|
|
132
|
+
(noteLine.taxes === null ||
|
|
133
|
+
(Array.isArray(noteLine.taxes) && noteLine.taxes.length === 0)) &&
|
|
134
|
+
Array.isArray(noteLine.pretax_credit_amounts) &&
|
|
135
|
+
noteLine.pretax_credit_amounts.length === 0 &&
|
|
136
|
+
line &&
|
|
137
|
+
line.currency === record.binding.plan.rate.currency &&
|
|
138
|
+
line.amount >= opts.amount &&
|
|
139
|
+
line.pricing?.price_details?.price === record.binding.stripePriceId &&
|
|
140
|
+
line.parent?.subscription_item_details?.subscription_item ===
|
|
141
|
+
sub.subscriptionItemId &&
|
|
142
|
+
!line.parent?.subscription_item_details?.proration &&
|
|
143
|
+
line.period.start === sub.period.start &&
|
|
144
|
+
line.period.end === sub.period.end &&
|
|
145
|
+
Array.isArray(line.pretax_credit_amounts) &&
|
|
146
|
+
line.pretax_credit_amounts.length === 0,
|
|
147
|
+
);
|
|
148
|
+
let paymentIntentId: string | null = null,
|
|
149
|
+
chargeId: string | null = null,
|
|
150
|
+
refundId: string | null = null;
|
|
151
|
+
if (note.post_payment_amount > 0) {
|
|
152
|
+
requireProof(
|
|
153
|
+
note.refunds.length === 1 &&
|
|
154
|
+
note.refunds[0].type === "refund" &&
|
|
155
|
+
note.refunds[0].amount_refunded === note.post_payment_amount,
|
|
156
|
+
);
|
|
157
|
+
refundId = referenceId(note.refunds[0].refund) ?? null;
|
|
158
|
+
requireProof(refundId);
|
|
159
|
+
const refund = await stripe.refunds.retrieve(refundId!);
|
|
160
|
+
paymentIntentId = referenceId(refund.payment_intent) ?? null;
|
|
161
|
+
chargeId = referenceId(refund.charge) ?? null;
|
|
162
|
+
requireProof(
|
|
163
|
+
refund.id === refundId &&
|
|
164
|
+
refund.status === "succeeded" &&
|
|
165
|
+
refund.amount === note.post_payment_amount &&
|
|
166
|
+
refund.currency === note.currency &&
|
|
167
|
+
paymentIntentId &&
|
|
168
|
+
chargeId,
|
|
169
|
+
);
|
|
170
|
+
const [payment, charge, payments] = await Promise.all([
|
|
171
|
+
stripe.paymentIntents.retrieve(paymentIntentId!),
|
|
172
|
+
stripe.charges.retrieve(chargeId!),
|
|
173
|
+
stripe.invoicePayments.list({
|
|
174
|
+
invoice: invoice.id,
|
|
175
|
+
status: "paid",
|
|
176
|
+
limit: 100,
|
|
177
|
+
}),
|
|
178
|
+
]);
|
|
179
|
+
const references = payments.data.map(paymentReference);
|
|
180
|
+
requireProof(
|
|
181
|
+
payments.has_more === false &&
|
|
182
|
+
references.some(
|
|
183
|
+
({ invoicePayment: p }) =>
|
|
184
|
+
referenceId(p.invoice) === invoice.id &&
|
|
185
|
+
p.status === "paid" &&
|
|
186
|
+
p.currency === note.currency &&
|
|
187
|
+
p.amount_paid !== null &&
|
|
188
|
+
p.amount_paid >= refund.amount &&
|
|
189
|
+
p.payment.type === "payment_intent" &&
|
|
190
|
+
referenceId(p.payment.payment_intent) === paymentIntentId,
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
requireProof(
|
|
194
|
+
[
|
|
195
|
+
refund.amount,
|
|
196
|
+
payment.amount,
|
|
197
|
+
payment.amount_received,
|
|
198
|
+
charge.amount,
|
|
199
|
+
charge.amount_captured,
|
|
200
|
+
charge.amount_refunded,
|
|
201
|
+
].every((value) => Number.isSafeInteger(value) && value >= 0),
|
|
202
|
+
);
|
|
203
|
+
requireProof(
|
|
204
|
+
refund.object === "refund" &&
|
|
205
|
+
payment.object === "payment_intent" &&
|
|
206
|
+
charge.object === "charge" &&
|
|
207
|
+
charge.amount_captured === payment.amount_received &&
|
|
208
|
+
charge.amount_captured <= charge.amount &&
|
|
209
|
+
charge.amount_captured <= payment.amount &&
|
|
210
|
+
(!payment.customer ||
|
|
211
|
+
referenceId(payment.customer) ===
|
|
212
|
+
record.binding.stripeCustomerId) &&
|
|
213
|
+
referenceId(charge.customer) === referenceId(payment.customer),
|
|
214
|
+
);
|
|
215
|
+
requireProof(
|
|
216
|
+
payment.id === paymentIntentId &&
|
|
217
|
+
payment.livemode === record.scope.livemode &&
|
|
218
|
+
payment.customer_account === record.scope.customerAccount &&
|
|
219
|
+
payment.status === "succeeded" &&
|
|
220
|
+
payment.currency === note.currency &&
|
|
221
|
+
referenceId(payment.latest_charge) === chargeId &&
|
|
222
|
+
payment.amount_received >= refund.amount &&
|
|
223
|
+
charge.id === chargeId &&
|
|
224
|
+
charge.payment_intent === paymentIntentId &&
|
|
225
|
+
charge.livemode === record.scope.livemode &&
|
|
226
|
+
charge.currency === note.currency &&
|
|
227
|
+
charge.paid &&
|
|
228
|
+
charge.captured &&
|
|
229
|
+
charge.status === "succeeded" &&
|
|
230
|
+
!charge.disputed &&
|
|
231
|
+
charge.amount_captured >= refund.amount &&
|
|
232
|
+
charge.amount_refunded >= refund.amount &&
|
|
233
|
+
charge.amount_refunded <= charge.amount_captured,
|
|
234
|
+
);
|
|
235
|
+
} else requireProof(note.refunds.length === 0);
|
|
236
|
+
proof = invoiceCorrectionProof.parse({
|
|
237
|
+
creditNoteStatus: note.status,
|
|
238
|
+
stripeAccountId: record.scope.stripeAccountId,
|
|
239
|
+
customerAccount: record.scope.customerAccount,
|
|
240
|
+
livemode: record.scope.livemode,
|
|
241
|
+
stripeCustomerId: record.binding.stripeCustomerId,
|
|
242
|
+
subscriptionId: sub.subscriptionId,
|
|
243
|
+
subscriptionItemId: sub.subscriptionItemId,
|
|
244
|
+
stripePriceId: record.binding.stripePriceId,
|
|
245
|
+
period: sub.period,
|
|
246
|
+
creditNoteId: note.id,
|
|
247
|
+
invoiceId: invoice.id,
|
|
248
|
+
invoiceLineId: line!.id,
|
|
249
|
+
currency: note.currency,
|
|
250
|
+
amount: note.amount,
|
|
251
|
+
prePaymentAmount: note.pre_payment_amount,
|
|
252
|
+
postPaymentAmount: note.post_payment_amount,
|
|
253
|
+
paymentIntentId,
|
|
254
|
+
chargeId,
|
|
255
|
+
refundId,
|
|
256
|
+
});
|
|
257
|
+
} catch {
|
|
258
|
+
return {
|
|
259
|
+
status: "held" as const,
|
|
260
|
+
reason: "native_invoice_adjustment_unverified",
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const saved = (await runMutationOrThrow(
|
|
264
|
+
ctx,
|
|
265
|
+
componentRef(component, "usageCorrections/mutations/adopt"),
|
|
266
|
+
{ key: row.key, proofJson: JSON.stringify(proof) },
|
|
267
|
+
)) as Doc<"usageCorrections">;
|
|
268
|
+
return correctionView(saved);
|
|
269
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const invoiceCorrectionProof = z.strictObject({
|
|
3
|
+
creditNoteStatus: z.enum(["issued", "void"]),
|
|
4
|
+
stripeAccountId: z.string(),
|
|
5
|
+
livemode: z.boolean(),
|
|
6
|
+
customerAccount: z.string(),
|
|
7
|
+
stripeCustomerId: z.string(),
|
|
8
|
+
subscriptionId: z.string(),
|
|
9
|
+
subscriptionItemId: z.string(),
|
|
10
|
+
stripePriceId: z.string(),
|
|
11
|
+
period: z.strictObject({ start: z.number().int(), end: z.number().int() }),
|
|
12
|
+
creditNoteId: z
|
|
13
|
+
.string()
|
|
14
|
+
.regex(/^cn_[a-zA-Z0-9]+$/)
|
|
15
|
+
.max(100),
|
|
16
|
+
invoiceId: z
|
|
17
|
+
.string()
|
|
18
|
+
.regex(/^in_[a-zA-Z0-9]+$/)
|
|
19
|
+
.max(100),
|
|
20
|
+
invoiceLineId: z.string().max(100),
|
|
21
|
+
currency: z.string().regex(/^[a-z]{3}$/),
|
|
22
|
+
amount: z.number().int().positive().max(99999999),
|
|
23
|
+
prePaymentAmount: z.number().int().nonnegative(),
|
|
24
|
+
postPaymentAmount: z.number().int().nonnegative(),
|
|
25
|
+
paymentIntentId: z
|
|
26
|
+
.string()
|
|
27
|
+
.regex(/^pi_[a-zA-Z0-9]+$/)
|
|
28
|
+
.max(100)
|
|
29
|
+
.nullable(),
|
|
30
|
+
chargeId: z
|
|
31
|
+
.string()
|
|
32
|
+
.regex(/^ch_[a-zA-Z0-9]+$/)
|
|
33
|
+
.max(100)
|
|
34
|
+
.nullable(),
|
|
35
|
+
refundId: z
|
|
36
|
+
.string()
|
|
37
|
+
.regex(/^re_[a-zA-Z0-9]+$/)
|
|
38
|
+
.max(100)
|
|
39
|
+
.nullable(),
|
|
40
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const correctionOutcome = z.union([
|
|
3
|
+
z.strictObject({
|
|
4
|
+
status: z.enum(["pending", "complete"]),
|
|
5
|
+
eventName: z.string().min(1).max(100),
|
|
6
|
+
identifier: z.string().min(1).max(100),
|
|
7
|
+
livemode: z.boolean(),
|
|
8
|
+
nativeAdjustmentId: z.string().min(1).max(100).nullable(),
|
|
9
|
+
creditBackingVerified: z.boolean(),
|
|
10
|
+
billingSafe: z.boolean().default(false),
|
|
11
|
+
invoiceId: z
|
|
12
|
+
.string()
|
|
13
|
+
.regex(/^in_[a-zA-Z0-9]+$/)
|
|
14
|
+
.max(100)
|
|
15
|
+
.optional(),
|
|
16
|
+
}),
|
|
17
|
+
z.strictObject({
|
|
18
|
+
status: z.literal("local_complete"),
|
|
19
|
+
creditBackingVerified: z.literal(true),
|
|
20
|
+
}),
|
|
21
|
+
z.strictObject({
|
|
22
|
+
status: z.enum(["held", "invoice_adjustment_required", "unsupported"]),
|
|
23
|
+
reason: z.enum([
|
|
24
|
+
"native_cancellation_expired",
|
|
25
|
+
"original_delivery_unresolved",
|
|
26
|
+
"provider_unavailable",
|
|
27
|
+
"provider_outcome_unknown",
|
|
28
|
+
"billing_period_closed",
|
|
29
|
+
"native_invoice_finalized",
|
|
30
|
+
"native_invoice_history_incomplete",
|
|
31
|
+
"credit_backing_requires_reconciliation",
|
|
32
|
+
"native_cancellation_rejected",
|
|
33
|
+
]),
|
|
34
|
+
invoiceId: z
|
|
35
|
+
.string()
|
|
36
|
+
.regex(/^in_[a-zA-Z0-9]+$/)
|
|
37
|
+
.max(100)
|
|
38
|
+
.optional(),
|
|
39
|
+
}),
|
|
40
|
+
]);
|