@fantastic.dev/better-stripe 0.59.0 → 0.60.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +5 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +676 -50
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +322 -9
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +644 -18
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +338 -25
- 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/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.js +2 -2
- package/dist/client/usage/creditCapacity.d.ts.map +1 -1
- package/dist/client/usage/creditCapacity.js +7 -1
- package/dist/client/usage/creditCapacity.js.map +1 -1
- package/dist/client/usage/creditClient.d.ts +3 -0
- package/dist/client/usage/creditClient.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.js +4 -1
- package/dist/client/usage/creditDelivery.js.map +1 -1
- package/dist/client/usage/creditLifecycle.d.ts.map +1 -1
- package/dist/client/usage/creditLifecycle.js +2 -1
- package/dist/client/usage/creditLifecycle.js.map +1 -1
- package/dist/client/usage/creditNative.d.ts +3 -0
- package/dist/client/usage/creditNative.d.ts.map +1 -1
- package/dist/client/usage/creditNative.js +3 -0
- package/dist/client/usage/creditNative.js.map +1 -1
- package/dist/client/usage/creditProvider.d.ts.map +1 -1
- package/dist/client/usage/creditProvider.js +2 -1
- package/dist/client/usage/creditProvider.js.map +1 -1
- package/dist/client/usage/creditRead.d.ts +1 -0
- package/dist/client/usage/creditRead.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.js +11 -3
- package/dist/client/usage/creditReconciliation.js.map +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 +216 -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 +8 -1
- package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.js +27 -5
- package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
- package/dist/client/usage/index.d.ts +4 -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 +2 -2
- package/dist/client/usage/observationClient.d.ts.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/subscriptionCancellation.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCancellation.js +3 -2
- package/dist/client/usage/subscriptionCancellation.js.map +1 -1
- package/dist/client/usage/subscriptionCheckout.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCheckout.js +3 -2
- package/dist/client/usage/subscriptionCheckout.js.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.d.ts +1 -0
- package/dist/client/usage/subscriptionLifecycle.d.ts.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.js +3 -2
- package/dist/client/usage/subscriptionLifecycle.js.map +1 -1
- package/dist/client/usage/subscriptionProvider.d.ts +2 -0
- package/dist/client/usage/subscriptionProvider.d.ts.map +1 -1
- package/dist/client/usage/subscriptionProvider.js +9 -2
- package/dist/client/usage/subscriptionProvider.js.map +1 -1
- package/dist/component/_generated/component.d.ts +146 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/earnings/triggers.d.ts +172 -12
- 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 +322 -9
- 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/allowance.fixtures.d.ts +4212 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -0
- package/dist/component/usage/allowance.fixtures.js +67 -0
- package/dist/component/usage/allowance.fixtures.js.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts +4235 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.js +89 -0
- package/dist/component/usageCredits/accounting.fixtures.js.map +1 -0
- package/dist/component/usageCredits/helpers.d.ts +7 -0
- package/dist/component/usageCredits/helpers.d.ts.map +1 -1
- package/dist/component/usageCredits/helpers.js +5 -0
- package/dist/component/usageCredits/helpers.js.map +1 -1
- package/dist/component/usageCredits/mutations.d.ts +6 -0
- package/dist/component/usageCredits/mutations.d.ts.map +1 -1
- package/dist/component/usageCredits/mutations.js +70 -7
- package/dist/component/usageCredits/mutations.js.map +1 -1
- package/dist/component/usageCredits/purchase.d.ts +1 -0
- package/dist/component/usageCredits/purchase.d.ts.map +1 -1
- package/dist/component/usageCredits/queries.d.ts +10 -0
- package/dist/component/usageCredits/queries.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.d.ts +24 -4
- package/dist/component/usageCredits/schema.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.js +4 -0
- package/dist/component/usageCredits/schema.js.map +1 -1
- package/dist/component/usageSubscriptions/history.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/history.js +3 -1
- package/dist/component/usageSubscriptions/history.js.map +1 -1
- package/dist/component/usageSubscriptions/mutations.d.ts +5 -0
- package/dist/component/usageSubscriptions/mutations.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/mutations.js +19 -0
- package/dist/component/usageSubscriptions/mutations.js.map +1 -1
- package/dist/component/usageSubscriptions/queries.d.ts +2 -0
- package/dist/component/usageSubscriptions/queries.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.d.ts +7 -2
- package/dist/component/usageSubscriptions/schema.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.js +1 -0
- package/dist/component/usageSubscriptions/schema.js.map +1 -1
- package/docs/README.md +4 -0
- package/docs/settlement.md +4 -0
- package/docs/usage-credits.md +6 -1
- package/docs/usage-security.md +130 -0
- 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/creditBackingIsolation.test.ts +110 -0
- package/src/client/usage/creditCapacity.test.ts +80 -1
- package/src/client/usage/creditCapacity.ts +10 -1
- package/src/client/usage/creditDelivery.ts +4 -0
- package/src/client/usage/creditLifecycle.ts +2 -0
- package/src/client/usage/creditNative.ts +5 -0
- package/src/client/usage/creditProofBounds.test.ts +187 -0
- package/src/client/usage/creditProvider.test.ts +74 -0
- package/src/client/usage/creditProvider.ts +2 -0
- package/src/client/usage/creditReconciliation.ts +18 -3
- 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 +31 -5
- package/src/client/usage/index.ts +9 -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/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/subscriptionCancellation.ts +3 -2
- package/src/client/usage/subscriptionCheckout.ts +7 -2
- package/src/client/usage/subscriptionClient.test.ts +60 -1
- package/src/client/usage/subscriptionLifecycle.ts +3 -2
- package/src/client/usage/subscriptionProvider.ts +27 -3
- package/src/component/_generated/component.ts +150 -1
- package/src/component/financialObservations/settlementHelpers.ts +2 -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/allowance.fixtures.ts +77 -0
- package/src/component/usage/allowance.test.ts +3 -66
- package/src/component/usage/allowanceBounds.test.ts +79 -0
- package/src/component/usage/historyIsolation.test.ts +124 -0
- package/src/component/usageCredits/accounting.fixtures.ts +96 -0
- package/src/component/usageCredits/accounting.test.ts +176 -82
- package/src/component/usageCredits/freshness.test.ts +33 -0
- package/src/component/usageCredits/helpers.ts +8 -0
- package/src/component/usageCredits/mutations.ts +83 -5
- package/src/component/usageCredits/quotas.test.ts +63 -0
- package/src/component/usageCredits/schema.ts +10 -0
- package/src/component/usageSubscriptions/history.test.ts +57 -0
- package/src/component/usageSubscriptions/history.ts +3 -1
- package/src/component/usageSubscriptions/mutations.ts +23 -0
- package/src/component/usageSubscriptions/schema.ts +1 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { makeFunctionReference } from "convex/server";
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import { setupAllowance } from "./allowance.fixtures.js";
|
|
4
|
+
import {
|
|
5
|
+
request,
|
|
6
|
+
nativeSubscription,
|
|
7
|
+
} from "../../client/usage/subscription.fixtures.js";
|
|
8
|
+
const prepare = makeFunctionReference<"mutation">(
|
|
9
|
+
"usageSubscriptions/mutations:prepare",
|
|
10
|
+
);
|
|
11
|
+
const save = makeFunctionReference<"mutation">(
|
|
12
|
+
"usageSubscriptions/mutations:save",
|
|
13
|
+
);
|
|
14
|
+
afterEach(() => vi.useRealTimers());
|
|
15
|
+
it("keeps later subscription and allowance history pages inside the requested tenant when cursors are reused", async () => {
|
|
16
|
+
const { t, meter, subscription, configure, bindingJson } =
|
|
17
|
+
await setupAllowance();
|
|
18
|
+
await configure();
|
|
19
|
+
await configure(
|
|
20
|
+
{
|
|
21
|
+
reset: "billing_period",
|
|
22
|
+
rollover: "none",
|
|
23
|
+
entitled: true,
|
|
24
|
+
overage: "allow",
|
|
25
|
+
limit: { mode: "hard", quantity: "13" },
|
|
26
|
+
},
|
|
27
|
+
1,
|
|
28
|
+
);
|
|
29
|
+
const originalIntent = await t.mutation(prepare, {
|
|
30
|
+
termsJson: JSON.stringify({ ...request, meters: [meter] }),
|
|
31
|
+
method: "direct",
|
|
32
|
+
});
|
|
33
|
+
const renewed = nativeSubscription();
|
|
34
|
+
renewed.cancel_at_period_end = true;
|
|
35
|
+
await t.mutation(save, {
|
|
36
|
+
key: originalIntent.key,
|
|
37
|
+
nativeJson: JSON.stringify(renewed),
|
|
38
|
+
startedAt: Date.now() + 1,
|
|
39
|
+
});
|
|
40
|
+
const foreign = {
|
|
41
|
+
...meter,
|
|
42
|
+
scope: { ...meter.scope, customerAccount: "acct_other" },
|
|
43
|
+
binding: { ...meter.binding, stripeCustomerId: "cus_other" },
|
|
44
|
+
};
|
|
45
|
+
const other = await t.mutation(prepare, {
|
|
46
|
+
termsJson: JSON.stringify({ ...request, meters: [foreign] }),
|
|
47
|
+
method: "direct",
|
|
48
|
+
});
|
|
49
|
+
const native = nativeSubscription();
|
|
50
|
+
native.id = "sub_other";
|
|
51
|
+
native.customer = "cus_other";
|
|
52
|
+
native.customer_account = "acct_other";
|
|
53
|
+
await t.mutation(save, {
|
|
54
|
+
key: other.key,
|
|
55
|
+
nativeJson: JSON.stringify(native),
|
|
56
|
+
startedAt: Date.now(),
|
|
57
|
+
});
|
|
58
|
+
const otherBinding = JSON.stringify({
|
|
59
|
+
...foreign,
|
|
60
|
+
subscription: { ...subscription, subscriptionId: "sub_other" },
|
|
61
|
+
});
|
|
62
|
+
await t.mutation(
|
|
63
|
+
makeFunctionReference<"mutation">("usageAllowance/mutations:configure"),
|
|
64
|
+
{
|
|
65
|
+
bindingJson: otherBinding,
|
|
66
|
+
policyJson: JSON.stringify({
|
|
67
|
+
reset: "billing_period",
|
|
68
|
+
rollover: "none",
|
|
69
|
+
entitled: true,
|
|
70
|
+
overage: "allow",
|
|
71
|
+
limit: { mode: "none" },
|
|
72
|
+
}),
|
|
73
|
+
expectedRevision: 0,
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
for (const kind of ["subscription", "allowance"]) {
|
|
77
|
+
const query = makeFunctionReference<"query">(
|
|
78
|
+
kind === "subscription"
|
|
79
|
+
? "usageSubscriptions/queries:history"
|
|
80
|
+
: "usageAllowance/queries:readHistory",
|
|
81
|
+
);
|
|
82
|
+
const args =
|
|
83
|
+
kind === "subscription"
|
|
84
|
+
? {
|
|
85
|
+
scope: {
|
|
86
|
+
stripeAccountId: meter.scope.stripeAccountId,
|
|
87
|
+
customerAccount: meter.scope.customerAccount,
|
|
88
|
+
livemode: false,
|
|
89
|
+
},
|
|
90
|
+
requestId: "signup1",
|
|
91
|
+
kind: "subscription",
|
|
92
|
+
}
|
|
93
|
+
: { bindingJson };
|
|
94
|
+
const otherArgs =
|
|
95
|
+
kind === "subscription"
|
|
96
|
+
? {
|
|
97
|
+
...args,
|
|
98
|
+
scope: {
|
|
99
|
+
stripeAccountId: meter.scope.stripeAccountId,
|
|
100
|
+
customerAccount: "acct_other",
|
|
101
|
+
livemode: false,
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
: { bindingJson: otherBinding };
|
|
105
|
+
const first = await t.query(query, {
|
|
106
|
+
...args,
|
|
107
|
+
paginationOpts: { numItems: 1, cursor: null },
|
|
108
|
+
});
|
|
109
|
+
expect(first.isDone).toBe(false);
|
|
110
|
+
const paginationOpts = { numItems: 1, cursor: first.continueCursor };
|
|
111
|
+
const next = await t.query(query, { ...args, paginationOpts });
|
|
112
|
+
expect(next).toEqual(await t.query(query, { ...args, paginationOpts }));
|
|
113
|
+
expect(next.page).toHaveLength(1);
|
|
114
|
+
try {
|
|
115
|
+
const otherPage = await t.query(query, { ...otherArgs, paginationOpts });
|
|
116
|
+
for (const row of otherPage.page) {
|
|
117
|
+
if (kind === "subscription") expect(row.intentKey).toBe(other.key);
|
|
118
|
+
else expect(row.key).not.toBe(first.page[0].key);
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
expect(String(error)).toMatch(/cursor|range|index|position|pagination/i);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { vi } from "vitest";
|
|
4
|
+
import { schema, modules } from "../../test.js";
|
|
5
|
+
import {
|
|
6
|
+
request,
|
|
7
|
+
nativeSubscription,
|
|
8
|
+
} from "../../client/usage/subscription.fixtures.js";
|
|
9
|
+
const ref = (name: string) =>
|
|
10
|
+
makeFunctionReference<"mutation">(`usageCredits/mutations:${name}`);
|
|
11
|
+
const prepare = makeFunctionReference<"mutation">(
|
|
12
|
+
"usageSubscriptions/mutations:prepare",
|
|
13
|
+
);
|
|
14
|
+
const save = makeFunctionReference<"mutation">(
|
|
15
|
+
"usageSubscriptions/mutations:save",
|
|
16
|
+
);
|
|
17
|
+
export async function setupCredits() {
|
|
18
|
+
vi.useFakeTimers();
|
|
19
|
+
vi.setSystemTime(700000);
|
|
20
|
+
const t = convexTest(schema, modules);
|
|
21
|
+
const meter = {
|
|
22
|
+
scope: request.meters[0].scope,
|
|
23
|
+
binding: {
|
|
24
|
+
...request.meters[0].binding,
|
|
25
|
+
plan: {
|
|
26
|
+
key: "calls",
|
|
27
|
+
version: "v1",
|
|
28
|
+
model: "prepaid",
|
|
29
|
+
unit: { name: "call", precision: 0 },
|
|
30
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
31
|
+
insufficientBalance: "reject",
|
|
32
|
+
} as const,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const terms = { ...request, meters: [meter] };
|
|
36
|
+
const intent = await t.mutation(prepare, {
|
|
37
|
+
termsJson: JSON.stringify(terms),
|
|
38
|
+
method: "direct",
|
|
39
|
+
});
|
|
40
|
+
await t.mutation(save, {
|
|
41
|
+
key: intent.key,
|
|
42
|
+
nativeJson: JSON.stringify(nativeSubscription()),
|
|
43
|
+
startedAt: Date.now(),
|
|
44
|
+
});
|
|
45
|
+
const purchase = {
|
|
46
|
+
...meter,
|
|
47
|
+
purchaseId: "topup1",
|
|
48
|
+
quantity: "10",
|
|
49
|
+
ownership: "issuer",
|
|
50
|
+
rollover: "retain_original_terms",
|
|
51
|
+
};
|
|
52
|
+
const row = await t.mutation(ref("preparePurchase"), {
|
|
53
|
+
inputJson: JSON.stringify(purchase),
|
|
54
|
+
});
|
|
55
|
+
await t.mutation(ref("pinPayment"), {
|
|
56
|
+
key: row.key,
|
|
57
|
+
paymentIntentId: "pi_topup1",
|
|
58
|
+
});
|
|
59
|
+
const proof = {
|
|
60
|
+
paymentIntentId: "pi_topup1",
|
|
61
|
+
chargeId: "ch_topup1",
|
|
62
|
+
amount: 20,
|
|
63
|
+
currency: "usd",
|
|
64
|
+
customerAccount: "acct_buyer",
|
|
65
|
+
livemode: false,
|
|
66
|
+
};
|
|
67
|
+
await t.mutation(ref("verifyPayment"), {
|
|
68
|
+
key: row.key,
|
|
69
|
+
proofJson: JSON.stringify(proof),
|
|
70
|
+
});
|
|
71
|
+
await t.mutation(ref("pinGrant"), {
|
|
72
|
+
key: row.key,
|
|
73
|
+
proofJson: JSON.stringify({
|
|
74
|
+
...proof,
|
|
75
|
+
creditGrantId: "credgr_topup1",
|
|
76
|
+
stripePriceId: "price_calls",
|
|
77
|
+
createdAt: 700,
|
|
78
|
+
effectiveAt: 700,
|
|
79
|
+
expiresAt: null,
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
const subscription = {
|
|
83
|
+
subscriptionId: "sub_calls",
|
|
84
|
+
subscriptionItemId: "si_calls",
|
|
85
|
+
stripePriceId: "price_calls",
|
|
86
|
+
period: { start: 600, end: 1200 },
|
|
87
|
+
interval: "month",
|
|
88
|
+
};
|
|
89
|
+
const event = {
|
|
90
|
+
...meter,
|
|
91
|
+
sourceEventId: "work1",
|
|
92
|
+
quantity: "6",
|
|
93
|
+
occurredAt: 700,
|
|
94
|
+
};
|
|
95
|
+
return { t, meter, purchase, row, proof, subscription, event };
|
|
96
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { setupCredits } from "./accounting.fixtures.js";
|
|
2
2
|
import { makeFunctionReference } from "convex/server";
|
|
3
3
|
import { afterEach, expect, it, vi } from "vitest";
|
|
4
|
-
import { schema, modules } from "../../test.js";
|
|
5
4
|
import {
|
|
6
5
|
request,
|
|
7
6
|
nativeSubscription,
|
|
@@ -16,86 +15,7 @@ const save = makeFunctionReference<"mutation">(
|
|
|
16
15
|
);
|
|
17
16
|
const record = makeFunctionReference<"mutation">("usage/mutations:record");
|
|
18
17
|
const balance = makeFunctionReference<"query">("usageCredits/queries:balance");
|
|
19
|
-
|
|
20
|
-
vi.useFakeTimers();
|
|
21
|
-
vi.setSystemTime(700000);
|
|
22
|
-
const t = convexTest(schema, modules);
|
|
23
|
-
const meter = {
|
|
24
|
-
scope: request.meters[0].scope,
|
|
25
|
-
binding: {
|
|
26
|
-
...request.meters[0].binding,
|
|
27
|
-
plan: {
|
|
28
|
-
key: "calls",
|
|
29
|
-
version: "v1",
|
|
30
|
-
model: "prepaid",
|
|
31
|
-
unit: { name: "call", precision: 0 },
|
|
32
|
-
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
33
|
-
insufficientBalance: "reject",
|
|
34
|
-
} as const,
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
const terms = { ...request, meters: [meter] };
|
|
38
|
-
const intent = await t.mutation(prepare, {
|
|
39
|
-
termsJson: JSON.stringify(terms),
|
|
40
|
-
method: "direct",
|
|
41
|
-
});
|
|
42
|
-
await t.mutation(save, {
|
|
43
|
-
key: intent.key,
|
|
44
|
-
nativeJson: JSON.stringify(nativeSubscription()),
|
|
45
|
-
startedAt: Date.now(),
|
|
46
|
-
});
|
|
47
|
-
const purchase = {
|
|
48
|
-
...meter,
|
|
49
|
-
purchaseId: "topup1",
|
|
50
|
-
quantity: "10",
|
|
51
|
-
ownership: "issuer",
|
|
52
|
-
rollover: "retain_original_terms",
|
|
53
|
-
};
|
|
54
|
-
const row = await t.mutation(ref("preparePurchase"), {
|
|
55
|
-
inputJson: JSON.stringify(purchase),
|
|
56
|
-
});
|
|
57
|
-
await t.mutation(ref("pinPayment"), {
|
|
58
|
-
key: row.key,
|
|
59
|
-
paymentIntentId: "pi_topup1",
|
|
60
|
-
});
|
|
61
|
-
const proof = {
|
|
62
|
-
paymentIntentId: "pi_topup1",
|
|
63
|
-
chargeId: "ch_topup1",
|
|
64
|
-
amount: 20,
|
|
65
|
-
currency: "usd",
|
|
66
|
-
customerAccount: "acct_buyer",
|
|
67
|
-
livemode: false,
|
|
68
|
-
};
|
|
69
|
-
await t.mutation(ref("verifyPayment"), {
|
|
70
|
-
key: row.key,
|
|
71
|
-
proofJson: JSON.stringify(proof),
|
|
72
|
-
});
|
|
73
|
-
await t.mutation(ref("pinGrant"), {
|
|
74
|
-
key: row.key,
|
|
75
|
-
proofJson: JSON.stringify({
|
|
76
|
-
...proof,
|
|
77
|
-
creditGrantId: "credgr_topup1",
|
|
78
|
-
stripePriceId: "price_calls",
|
|
79
|
-
createdAt: 700,
|
|
80
|
-
effectiveAt: 700,
|
|
81
|
-
expiresAt: null,
|
|
82
|
-
}),
|
|
83
|
-
});
|
|
84
|
-
const subscription = {
|
|
85
|
-
subscriptionId: "sub_calls",
|
|
86
|
-
subscriptionItemId: "si_calls",
|
|
87
|
-
stripePriceId: "price_calls",
|
|
88
|
-
period: { start: 600, end: 1200 },
|
|
89
|
-
interval: "month",
|
|
90
|
-
};
|
|
91
|
-
const event = {
|
|
92
|
-
...meter,
|
|
93
|
-
sourceEventId: "work1",
|
|
94
|
-
quantity: "6",
|
|
95
|
-
occurredAt: 700,
|
|
96
|
-
};
|
|
97
|
-
return { t, meter, purchase, row, proof, subscription, event };
|
|
98
|
-
}
|
|
18
|
+
|
|
99
19
|
afterEach(() => vi.useRealTimers());
|
|
100
20
|
it("atomically reserves payment-backed units, commits with ledger acceptance and rejects concurrent overspend", async () => {
|
|
101
21
|
const { t, meter, event, subscription } = await setupCredits();
|
|
@@ -387,3 +307,177 @@ it("allocates by provider creation time when native grants are mirrored out of o
|
|
|
387
307
|
).toBe("0");
|
|
388
308
|
expect(reservation.state).toBe("reserved");
|
|
389
309
|
});
|
|
310
|
+
|
|
311
|
+
it("does not replace credit backing with conflicting same-time evidence or resume through that ambiguity", async () => {
|
|
312
|
+
const { t, purchase, meter } = await setupCredits();
|
|
313
|
+
const proof = {
|
|
314
|
+
creditGrantId: "credgr_topup1",
|
|
315
|
+
paymentIntentId: "pi_topup1",
|
|
316
|
+
currency: "usd",
|
|
317
|
+
ledgerAmount: 8,
|
|
318
|
+
availableAmount: 8,
|
|
319
|
+
granted: 20,
|
|
320
|
+
applied: 12,
|
|
321
|
+
expired: 0,
|
|
322
|
+
voided: 0,
|
|
323
|
+
reinstated: 0,
|
|
324
|
+
invoicesVerified: true,
|
|
325
|
+
complete: true,
|
|
326
|
+
transactions: [],
|
|
327
|
+
};
|
|
328
|
+
const saveProof = (value: typeof proof, startedAt = Date.now()) =>
|
|
329
|
+
t.mutation(ref("saveObservation"), {
|
|
330
|
+
inputJson: JSON.stringify(purchase),
|
|
331
|
+
proofJson: JSON.stringify(value),
|
|
332
|
+
startedAt,
|
|
333
|
+
});
|
|
334
|
+
await saveProof(proof);
|
|
335
|
+
const result = JSON.parse(
|
|
336
|
+
await saveProof({
|
|
337
|
+
...proof,
|
|
338
|
+
ledgerAmount: 20,
|
|
339
|
+
availableAmount: 20,
|
|
340
|
+
applied: 0,
|
|
341
|
+
}),
|
|
342
|
+
);
|
|
343
|
+
expect(result.backing).toBe("held");
|
|
344
|
+
const grant = await t.run((ctx) => ctx.db.query("usageCreditGrants").first());
|
|
345
|
+
expect(JSON.parse(grant!.nativeObservationJson!).ledgerAmount).toBe(8);
|
|
346
|
+
await expect(
|
|
347
|
+
t.mutation(ref("resumeAccount"), { bindingJson: JSON.stringify(meter) }),
|
|
348
|
+
).rejects.toThrow(/reconciliation|ambigu|funding/i);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("keeps later credit history and grant cursors inside the requested original tenant binding", async () => {
|
|
352
|
+
const { t, meter } = await setupCredits();
|
|
353
|
+
const seed = async (binding: typeof meter, suffix: string) => {
|
|
354
|
+
const row = await t.mutation(ref("preparePurchase"), {
|
|
355
|
+
inputJson: JSON.stringify({
|
|
356
|
+
...binding,
|
|
357
|
+
purchaseId: suffix,
|
|
358
|
+
quantity: "10",
|
|
359
|
+
ownership: "issuer",
|
|
360
|
+
rollover: "retain_original_terms",
|
|
361
|
+
}),
|
|
362
|
+
});
|
|
363
|
+
await t.mutation(ref("pinPayment"), {
|
|
364
|
+
key: row.key,
|
|
365
|
+
paymentIntentId: `pi_${suffix}`,
|
|
366
|
+
});
|
|
367
|
+
const proof = {
|
|
368
|
+
paymentIntentId: `pi_${suffix}`,
|
|
369
|
+
chargeId: `ch_${suffix}`,
|
|
370
|
+
amount: 20,
|
|
371
|
+
currency: "usd",
|
|
372
|
+
customerAccount: binding.scope.customerAccount,
|
|
373
|
+
livemode: false,
|
|
374
|
+
};
|
|
375
|
+
await t.mutation(ref("verifyPayment"), {
|
|
376
|
+
key: row.key,
|
|
377
|
+
proofJson: JSON.stringify(proof),
|
|
378
|
+
});
|
|
379
|
+
await t.mutation(ref("pinGrant"), {
|
|
380
|
+
key: row.key,
|
|
381
|
+
proofJson: JSON.stringify({
|
|
382
|
+
...proof,
|
|
383
|
+
creditGrantId: `credgr_${suffix}`,
|
|
384
|
+
stripePriceId: binding.binding.stripePriceId,
|
|
385
|
+
createdAt: 700,
|
|
386
|
+
effectiveAt: 700,
|
|
387
|
+
expiresAt: null,
|
|
388
|
+
}),
|
|
389
|
+
});
|
|
390
|
+
return row;
|
|
391
|
+
};
|
|
392
|
+
const original = await seed(meter, "topup2");
|
|
393
|
+
const foreign = {
|
|
394
|
+
...meter,
|
|
395
|
+
scope: { ...meter.scope, customerAccount: "acct_other" },
|
|
396
|
+
binding: { ...meter.binding, stripeCustomerId: "cus_other" },
|
|
397
|
+
};
|
|
398
|
+
const intent = await t.mutation(prepare, {
|
|
399
|
+
termsJson: JSON.stringify({ ...request, meters: [foreign] }),
|
|
400
|
+
method: "direct",
|
|
401
|
+
});
|
|
402
|
+
const native = nativeSubscription();
|
|
403
|
+
native.id = "sub_other";
|
|
404
|
+
native.customer = "cus_other";
|
|
405
|
+
native.customer_account = "acct_other";
|
|
406
|
+
await t.mutation(save, {
|
|
407
|
+
key: intent.key,
|
|
408
|
+
nativeJson: JSON.stringify(native),
|
|
409
|
+
startedAt: Date.now(),
|
|
410
|
+
});
|
|
411
|
+
const other = await seed(foreign, "other1");
|
|
412
|
+
for (const name of ["readHistory", "readGrants"]) {
|
|
413
|
+
const query = makeFunctionReference<"query">(
|
|
414
|
+
`usageCredits/queries:${name}`,
|
|
415
|
+
);
|
|
416
|
+
const args = {
|
|
417
|
+
bindingJson: JSON.stringify(meter),
|
|
418
|
+
paginationOpts: { numItems: 1, cursor: null },
|
|
419
|
+
};
|
|
420
|
+
const first = await t.query(query, args);
|
|
421
|
+
expect(first.isDone).toBe(false);
|
|
422
|
+
const next = {
|
|
423
|
+
...args,
|
|
424
|
+
paginationOpts: { numItems: 1, cursor: first.continueCursor },
|
|
425
|
+
};
|
|
426
|
+
expect(await t.query(query, next)).toEqual(await t.query(query, next));
|
|
427
|
+
try {
|
|
428
|
+
const page = await t.query(query, {
|
|
429
|
+
...next,
|
|
430
|
+
bindingJson: JSON.stringify(foreign),
|
|
431
|
+
});
|
|
432
|
+
expect(
|
|
433
|
+
page.page.every(
|
|
434
|
+
(row: { accountKey: string }) =>
|
|
435
|
+
row.accountKey === other.accountKey &&
|
|
436
|
+
row.accountKey !== original.accountKey,
|
|
437
|
+
),
|
|
438
|
+
).toBe(true);
|
|
439
|
+
} catch (error) {
|
|
440
|
+
expect(String(error)).toMatch(/cursor|range|index|position|pagination/i);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("retains open credit liabilities when a dispute freezes a fully consumed grant after a zero-balance observation", async () => {
|
|
446
|
+
const { t, meter, purchase, row, event, subscription } = await setupCredits();
|
|
447
|
+
const args = {
|
|
448
|
+
inputJson: JSON.stringify({ ...event, quantity: "10" }),
|
|
449
|
+
subscriptionJson: JSON.stringify(subscription),
|
|
450
|
+
};
|
|
451
|
+
const reserved = await t.mutation(ref("reserve"), args);
|
|
452
|
+
await t.mutation(record, { ...args, reservationId: reserved.reservationId });
|
|
453
|
+
await t.mutation(ref("saveObservation"), {
|
|
454
|
+
inputJson: JSON.stringify(purchase),
|
|
455
|
+
startedAt: Date.now(),
|
|
456
|
+
proofJson: JSON.stringify({
|
|
457
|
+
creditGrantId: "credgr_topup1",
|
|
458
|
+
paymentIntentId: "pi_topup1",
|
|
459
|
+
currency: "usd",
|
|
460
|
+
ledgerAmount: 0,
|
|
461
|
+
availableAmount: 0,
|
|
462
|
+
granted: 20,
|
|
463
|
+
applied: 20,
|
|
464
|
+
expired: 0,
|
|
465
|
+
voided: 0,
|
|
466
|
+
reinstated: 0,
|
|
467
|
+
invoicesVerified: true,
|
|
468
|
+
complete: true,
|
|
469
|
+
transactions: [],
|
|
470
|
+
}),
|
|
471
|
+
});
|
|
472
|
+
await t.mutation(ref("freezePurchase"), {
|
|
473
|
+
key: row.key,
|
|
474
|
+
paymentIntentId: "pi_topup1",
|
|
475
|
+
reason: "payment_disputed",
|
|
476
|
+
});
|
|
477
|
+
await expect(
|
|
478
|
+
t.mutation(ref("closeAccount"), { bindingJson: JSON.stringify(meter) }),
|
|
479
|
+
).rejects.toThrow(/dispute|recovery|reconcil/i);
|
|
480
|
+
expect(
|
|
481
|
+
await t.run((ctx) => ctx.db.query("usageCreditAccounts").first()),
|
|
482
|
+
).toMatchObject({ closed: false, frozenReason: "payment_disputed" });
|
|
483
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { makeFunctionReference } from "convex/server";
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import { setupCredits } from "./accounting.fixtures.js";
|
|
4
|
+
const ref = (name: string) =>
|
|
5
|
+
makeFunctionReference<"mutation">(`usageCredits/mutations:${name}`);
|
|
6
|
+
afterEach(() => vi.useRealTimers());
|
|
7
|
+
for (const age of [60000, 60001])
|
|
8
|
+
it(`uses native proof wall time rather than a future logical order at age ${age}ms`, async () => {
|
|
9
|
+
const { t, purchase, row } = await setupCredits();
|
|
10
|
+
await t.mutation(ref("saveObservation"), {
|
|
11
|
+
inputJson: JSON.stringify(purchase),
|
|
12
|
+
startedAt: Date.now() + 100000,
|
|
13
|
+
proofJson: JSON.stringify({
|
|
14
|
+
creditGrantId: "credgr_topup1",
|
|
15
|
+
paymentIntentId: "pi_topup1",
|
|
16
|
+
currency: "usd",
|
|
17
|
+
ledgerAmount: 20,
|
|
18
|
+
availableAmount: 20,
|
|
19
|
+
granted: 20,
|
|
20
|
+
applied: 0,
|
|
21
|
+
expired: 0,
|
|
22
|
+
voided: 0,
|
|
23
|
+
reinstated: 0,
|
|
24
|
+
invoicesVerified: true,
|
|
25
|
+
complete: true,
|
|
26
|
+
transactions: [],
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
vi.setSystemTime(Date.now() + age);
|
|
30
|
+
const prepare = t.mutation(ref("prepareRefund"), { key: row.key });
|
|
31
|
+
if (age === 60000) await expect(prepare).resolves.toBeNull();
|
|
32
|
+
else await expect(prepare).rejects.toThrow(/current.*reconciliation/i);
|
|
33
|
+
});
|
|
@@ -158,3 +158,11 @@ export async function history(
|
|
|
158
158
|
historyCount: row.historyCount + 1,
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
|
+
|
|
162
|
+
/** Observation order never extends the wall-clock lifetime of native financial evidence. */
|
|
163
|
+
export function freshCreditProof(grant: Doc<"usageCreditGrants">) {
|
|
164
|
+
const start = grant.proofStartedAt ?? grant.observedAt;
|
|
165
|
+
return (
|
|
166
|
+
start !== undefined && start <= Date.now() && Date.now() - start <= 60000
|
|
167
|
+
);
|
|
168
|
+
}
|