@fantastic.dev/better-stripe 0.60.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 +6 -0
- package/README.md +3 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +26 -6
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +13 -3
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +26 -6
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +13 -3
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- 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/creditSettlement.fixtures.d.ts +1 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.d.ts +1 -0
- package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.js +2 -1
- package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
- 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 +21 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +16 -6
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/schema.d.ts +13 -3
- 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 +2 -0
- package/docs/usage-credits.md +6 -1
- package/docs/usage-security.md +130 -0
- package/package.json +2 -1
- 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/creditTransport.fixtures.ts +2 -1
- 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 +40 -1
- 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
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "./subscriptionTerms.js";
|
|
12
12
|
import {
|
|
13
13
|
subscriptionCall,
|
|
14
|
+
beginSubscriptionObservation,
|
|
14
15
|
UsageSubscriptionError,
|
|
15
16
|
verifySubscriptionRealm,
|
|
16
17
|
} from "./subscriptionProvider.js";
|
|
@@ -21,8 +22,8 @@ export async function changeManagedCancellation(
|
|
|
21
22
|
row: Doc<"usageSubscriptionIntents">,
|
|
22
23
|
input: { operationId: string; cancelAtPeriodEnd: boolean },
|
|
23
24
|
): Promise<UsageSubscriptionView> {
|
|
24
|
-
const terms = subscriptionTerms(JSON.parse(row.termsJson))
|
|
25
|
-
|
|
25
|
+
const terms = subscriptionTerms(JSON.parse(row.termsJson));
|
|
26
|
+
const startedAt = await beginSubscriptionObservation(component, ctx, row.key);
|
|
26
27
|
return subscriptionCall(async () => {
|
|
27
28
|
if (!row.providerId)
|
|
28
29
|
throw new UsageSubscriptionError(
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
verifySubscriptionPrices,
|
|
16
16
|
discoverSubscription,
|
|
17
17
|
saveSubscription,
|
|
18
|
+
beginSubscriptionObservation,
|
|
18
19
|
} from "./subscriptionProvider.js";
|
|
19
20
|
export function managedCheckoutParams(
|
|
20
21
|
terms: UsageSubscriptionTerms,
|
|
@@ -79,12 +80,16 @@ export async function ensureManagedCheckout(
|
|
|
79
80
|
ctx: RunCtx,
|
|
80
81
|
input: UsageSubscriptionOptions,
|
|
81
82
|
) {
|
|
82
|
-
const terms = subscriptionTerms(input)
|
|
83
|
-
startedAt = Date.now();
|
|
83
|
+
const terms = subscriptionTerms(input);
|
|
84
84
|
const params = managedCheckoutParams(terms, "pending");
|
|
85
85
|
return subscriptionCall(async () => {
|
|
86
86
|
await verifySubscriptionRealm(stripe, terms);
|
|
87
87
|
const row = await prepareSubscription(component, ctx, terms, "checkout");
|
|
88
|
+
const startedAt = await beginSubscriptionObservation(
|
|
89
|
+
component,
|
|
90
|
+
ctx,
|
|
91
|
+
row.key,
|
|
92
|
+
);
|
|
88
93
|
await discoverSubscription(stripe, terms, row.key);
|
|
89
94
|
let session = row.checkoutId
|
|
90
95
|
? await stripe.checkout.sessions.retrieve(row.checkoutId)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { expect, it } from "vitest";
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
2
|
import { BetterStripe } from "../index.js";
|
|
3
3
|
import type { RunCtx } from "../helpers.js";
|
|
4
4
|
import { fixture } from "./subscriptionTransport.fixtures.js";
|
|
@@ -314,3 +314,62 @@ it("records terminal cancellation refusal without leaving a lock that prevents c
|
|
|
314
314
|
),
|
|
315
315
|
).toMatchObject({ retired: true });
|
|
316
316
|
});
|
|
317
|
+
|
|
318
|
+
afterEach(() => vi.useRealTimers());
|
|
319
|
+
it("orders concurrent same-clock native refreshes and returns the retained newer projection", async () => {
|
|
320
|
+
vi.useFakeTimers();
|
|
321
|
+
vi.setSystemTime(700000);
|
|
322
|
+
let block = false,
|
|
323
|
+
signal!: () => void,
|
|
324
|
+
release!: () => void;
|
|
325
|
+
const entered = new Promise<void>((resolve) => {
|
|
326
|
+
signal = resolve;
|
|
327
|
+
}),
|
|
328
|
+
gate = new Promise<void>((resolve) => {
|
|
329
|
+
release = resolve;
|
|
330
|
+
});
|
|
331
|
+
const f = fixture(false, false, async (input, init) => {
|
|
332
|
+
if (
|
|
333
|
+
block &&
|
|
334
|
+
new URL(input).pathname === "/v1/subscriptions/sub_calls" &&
|
|
335
|
+
(init?.method ?? "GET") === "GET"
|
|
336
|
+
) {
|
|
337
|
+
block = false;
|
|
338
|
+
signal();
|
|
339
|
+
await gate;
|
|
340
|
+
return new Response(JSON.stringify(nativeSubscription()), {
|
|
341
|
+
headers: { "content-type": "application/json" },
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
const client = new BetterStripe(f.component),
|
|
346
|
+
options = {
|
|
347
|
+
stripeSecretKey: "sk_test_subscription",
|
|
348
|
+
httpClient: f.httpClient,
|
|
349
|
+
};
|
|
350
|
+
await f.t.action((ctx) =>
|
|
351
|
+
client.usage.ensureSubscription(ctx as RunCtx, request, options),
|
|
352
|
+
);
|
|
353
|
+
block = true;
|
|
354
|
+
const older = f.t.action((ctx) =>
|
|
355
|
+
client.usage.observeSubscription(ctx as RunCtx, ref, options),
|
|
356
|
+
);
|
|
357
|
+
await entered;
|
|
358
|
+
f.state.native!.items.data.forEach((item) => {
|
|
359
|
+
item.current_period_start = 1200;
|
|
360
|
+
item.current_period_end = 1800;
|
|
361
|
+
});
|
|
362
|
+
const newer = await f.t.action((ctx) =>
|
|
363
|
+
client.usage.observeSubscription(ctx as RunCtx, ref, options),
|
|
364
|
+
);
|
|
365
|
+
release();
|
|
366
|
+
expect(newer.bindings[0].subscription.period.start).toBe(1200);
|
|
367
|
+
expect((await older).bindings[0].subscription.period.start).toBe(1200);
|
|
368
|
+
expect(
|
|
369
|
+
await f.t.query((ctx) => client.usage.getSubscription(ctx as RunCtx, ref)),
|
|
370
|
+
).toMatchObject({
|
|
371
|
+
value: {
|
|
372
|
+
bindings: [{ subscription: { period: { start: 1200, end: 1800 } } }],
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
});
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "./subscriptionTerms.js";
|
|
12
12
|
import {
|
|
13
13
|
saveSubscription,
|
|
14
|
+
beginSubscriptionObservation,
|
|
14
15
|
subscriptionCall,
|
|
15
16
|
UsageSubscriptionError,
|
|
16
17
|
verifySubscriptionRealm,
|
|
@@ -51,8 +52,8 @@ export async function refreshManagedSubscription(
|
|
|
51
52
|
ctx: RunCtx,
|
|
52
53
|
row: Doc<"usageSubscriptionIntents">,
|
|
53
54
|
) {
|
|
54
|
-
const terms = subscriptionTerms(JSON.parse(row.termsJson))
|
|
55
|
-
|
|
55
|
+
const terms = subscriptionTerms(JSON.parse(row.termsJson));
|
|
56
|
+
const startedAt = await beginSubscriptionObservation(component, ctx, row.key);
|
|
56
57
|
return subscriptionCall(async () => {
|
|
57
58
|
if (!row.providerId)
|
|
58
59
|
throw new UsageSubscriptionError(
|
|
@@ -159,6 +159,17 @@ export async function discoverSubscription(
|
|
|
159
159
|
"Native subscription inventory exceeds bounded history",
|
|
160
160
|
);
|
|
161
161
|
}
|
|
162
|
+
export function beginSubscriptionObservation(
|
|
163
|
+
component: Component,
|
|
164
|
+
ctx: RunCtx,
|
|
165
|
+
key: string,
|
|
166
|
+
) {
|
|
167
|
+
return runMutationOrThrow(
|
|
168
|
+
ctx,
|
|
169
|
+
componentRef(component, "usageSubscriptions/mutations/beginObservation"),
|
|
170
|
+
{ key },
|
|
171
|
+
) as Promise<number>;
|
|
172
|
+
}
|
|
162
173
|
export async function saveSubscription(
|
|
163
174
|
component: Component,
|
|
164
175
|
ctx: RunCtx,
|
|
@@ -174,7 +185,16 @@ export async function saveSubscription(
|
|
|
174
185
|
componentRef(component, "usageSubscriptions/mutations/save"),
|
|
175
186
|
{ key, nativeJson: JSON.stringify(evidence), startedAt },
|
|
176
187
|
);
|
|
177
|
-
|
|
188
|
+
const retained = (await ctx.runQuery(
|
|
189
|
+
componentRef(component, "usageSubscriptions/queries/lookup"),
|
|
190
|
+
{ key },
|
|
191
|
+
)) as Doc<"usageSubscriptionIntents"> | null;
|
|
192
|
+
if (!retained?.viewJson || retained.providerId !== view.subscriptionId)
|
|
193
|
+
throw new UsageSubscriptionError(
|
|
194
|
+
"incompatible_terms",
|
|
195
|
+
"Retained subscription projection unavailable",
|
|
196
|
+
);
|
|
197
|
+
return JSON.parse(retained.viewJson) as ReturnType<typeof subscriptionView>;
|
|
178
198
|
}
|
|
179
199
|
export async function prepareSubscription(
|
|
180
200
|
component: Component,
|
|
@@ -194,8 +214,7 @@ export async function ensureManagedSubscription(
|
|
|
194
214
|
ctx: RunCtx,
|
|
195
215
|
input: UsageSubscriptionOptions,
|
|
196
216
|
) {
|
|
197
|
-
const terms = subscriptionTerms(input)
|
|
198
|
-
startedAt = Date.now();
|
|
217
|
+
const terms = subscriptionTerms(input);
|
|
199
218
|
return subscriptionCall(async () => {
|
|
200
219
|
if (terms.collection.method === "checkout")
|
|
201
220
|
throw new UsageSubscriptionError(
|
|
@@ -204,6 +223,11 @@ export async function ensureManagedSubscription(
|
|
|
204
223
|
);
|
|
205
224
|
await verifySubscriptionRealm(stripe, terms);
|
|
206
225
|
const row = await prepareSubscription(component, ctx, terms, "direct");
|
|
226
|
+
const startedAt = await beginSubscriptionObservation(
|
|
227
|
+
component,
|
|
228
|
+
ctx,
|
|
229
|
+
row.key,
|
|
230
|
+
);
|
|
207
231
|
if (!row.providerId) await verifySubscriptionPrices(stripe, terms);
|
|
208
232
|
let sub = row.providerId
|
|
209
233
|
? await stripe.subscriptions.retrieve(row.providerId)
|
|
@@ -8978,6 +8978,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
8978
8978
|
};
|
|
8979
8979
|
usageCredits: {
|
|
8980
8980
|
mutations: {
|
|
8981
|
+
beginObservation: FunctionReference<
|
|
8982
|
+
"mutation",
|
|
8983
|
+
"internal",
|
|
8984
|
+
{ inputJson: string },
|
|
8985
|
+
number,
|
|
8986
|
+
Name
|
|
8987
|
+
>;
|
|
8981
8988
|
cancelPurchase: FunctionReference<
|
|
8982
8989
|
"mutation",
|
|
8983
8990
|
"internal",
|
|
@@ -9051,6 +9058,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9051
9058
|
paymentIntentId?: string;
|
|
9052
9059
|
paymentKey?: string;
|
|
9053
9060
|
paymentProofJson?: string;
|
|
9061
|
+
recoveryReason?:
|
|
9062
|
+
"payment_refunded" | "payment_disputed" | "native_state_changed";
|
|
9054
9063
|
refundedAmount?: number;
|
|
9055
9064
|
state: "pending" | "ready" | "frozen" | "canceled";
|
|
9056
9065
|
termsJson: string;
|
|
@@ -9100,7 +9109,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9100
9109
|
saveObservation: FunctionReference<
|
|
9101
9110
|
"mutation",
|
|
9102
9111
|
"internal",
|
|
9103
|
-
{
|
|
9112
|
+
{
|
|
9113
|
+
inputJson: string;
|
|
9114
|
+
proofJson: string;
|
|
9115
|
+
proofStartedAt?: number;
|
|
9116
|
+
startedAt: number;
|
|
9117
|
+
},
|
|
9104
9118
|
string,
|
|
9105
9119
|
Name
|
|
9106
9120
|
>;
|
|
@@ -9152,6 +9166,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9152
9166
|
historyCount?: number;
|
|
9153
9167
|
key: string;
|
|
9154
9168
|
method: "direct" | "checkout";
|
|
9169
|
+
observationStartedAt?: number;
|
|
9155
9170
|
providerId?: string;
|
|
9156
9171
|
retiredAt?: number;
|
|
9157
9172
|
retirementInvoiceId?: string;
|
|
@@ -9191,9 +9206,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9191
9206
|
nativeExpired?: boolean;
|
|
9192
9207
|
nativeObservationJson?: string;
|
|
9193
9208
|
nativeUnused: boolean;
|
|
9209
|
+
observationConflictAt?: number;
|
|
9210
|
+
observationStartedAt?: number;
|
|
9194
9211
|
observedAt?: number;
|
|
9195
9212
|
paymentIntentId: string;
|
|
9196
9213
|
proofJson: string;
|
|
9214
|
+
proofStartedAt?: number;
|
|
9197
9215
|
purchaseKey: string;
|
|
9198
9216
|
reserved: string;
|
|
9199
9217
|
}>;
|
|
@@ -9212,6 +9230,10 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9212
9230
|
paymentIntentId?: string;
|
|
9213
9231
|
paymentKey?: string;
|
|
9214
9232
|
paymentProofJson?: string;
|
|
9233
|
+
recoveryReason?:
|
|
9234
|
+
| "payment_refunded"
|
|
9235
|
+
| "payment_disputed"
|
|
9236
|
+
| "native_state_changed";
|
|
9215
9237
|
refundedAmount?: number;
|
|
9216
9238
|
state: "pending" | "ready" | "frozen" | "canceled";
|
|
9217
9239
|
termsJson: string;
|
|
@@ -9237,6 +9259,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9237
9259
|
paymentIntentId?: string;
|
|
9238
9260
|
paymentKey?: string;
|
|
9239
9261
|
paymentProofJson?: string;
|
|
9262
|
+
recoveryReason?:
|
|
9263
|
+
"payment_refunded" | "payment_disputed" | "native_state_changed";
|
|
9240
9264
|
refundedAmount?: number;
|
|
9241
9265
|
state: "pending" | "ready" | "frozen" | "canceled";
|
|
9242
9266
|
termsJson: string;
|
|
@@ -9261,6 +9285,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9261
9285
|
paymentIntentId?: string;
|
|
9262
9286
|
paymentKey?: string;
|
|
9263
9287
|
paymentProofJson?: string;
|
|
9288
|
+
recoveryReason?:
|
|
9289
|
+
"payment_refunded" | "payment_disputed" | "native_state_changed";
|
|
9264
9290
|
refundedAmount?: number;
|
|
9265
9291
|
state: "pending" | "ready" | "frozen" | "canceled";
|
|
9266
9292
|
termsJson: string;
|
|
@@ -9313,9 +9339,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9313
9339
|
nativeExpired?: boolean;
|
|
9314
9340
|
nativeObservationJson?: string;
|
|
9315
9341
|
nativeUnused: boolean;
|
|
9342
|
+
observationConflictAt?: number;
|
|
9343
|
+
observationStartedAt?: number;
|
|
9316
9344
|
observedAt?: number;
|
|
9317
9345
|
paymentIntentId: string;
|
|
9318
9346
|
proofJson: string;
|
|
9347
|
+
proofStartedAt?: number;
|
|
9319
9348
|
purchaseKey: string;
|
|
9320
9349
|
reserved: string;
|
|
9321
9350
|
}>;
|
|
@@ -9838,6 +9867,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9838
9867
|
null,
|
|
9839
9868
|
Name
|
|
9840
9869
|
>;
|
|
9870
|
+
beginObservation: FunctionReference<
|
|
9871
|
+
"mutation",
|
|
9872
|
+
"internal",
|
|
9873
|
+
{ key: string },
|
|
9874
|
+
number,
|
|
9875
|
+
Name
|
|
9876
|
+
>;
|
|
9841
9877
|
completeCancellation: FunctionReference<
|
|
9842
9878
|
"mutation",
|
|
9843
9879
|
"internal",
|
|
@@ -9866,6 +9902,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9866
9902
|
historyCount?: number;
|
|
9867
9903
|
key: string;
|
|
9868
9904
|
method: "direct" | "checkout";
|
|
9905
|
+
observationStartedAt?: number;
|
|
9869
9906
|
providerId?: string;
|
|
9870
9907
|
retiredAt?: number;
|
|
9871
9908
|
retirementInvoiceId?: string;
|
|
@@ -9941,6 +9978,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
9941
9978
|
historyCount?: number;
|
|
9942
9979
|
key: string;
|
|
9943
9980
|
method: "direct" | "checkout";
|
|
9981
|
+
observationStartedAt?: number;
|
|
9944
9982
|
providerId?: string;
|
|
9945
9983
|
retiredAt?: number;
|
|
9946
9984
|
retirementInvoiceId?: string;
|
|
@@ -10003,6 +10041,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
10003
10041
|
historyCount?: number;
|
|
10004
10042
|
key: string;
|
|
10005
10043
|
method: "direct" | "checkout";
|
|
10044
|
+
observationStartedAt?: number;
|
|
10006
10045
|
providerId?: string;
|
|
10007
10046
|
retiredAt?: number;
|
|
10008
10047
|
retirementInvoiceId?: string;
|
|
@@ -0,0 +1,77 @@
|
|
|
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 mutation = (name: string) =>
|
|
10
|
+
makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
|
|
11
|
+
const policy = {
|
|
12
|
+
reset: "billing_period",
|
|
13
|
+
rollover: "none",
|
|
14
|
+
entitled: true,
|
|
15
|
+
overage: "allow",
|
|
16
|
+
limit: { mode: "hard", quantity: "12" },
|
|
17
|
+
};
|
|
18
|
+
export async function setupAllowance() {
|
|
19
|
+
vi.useFakeTimers();
|
|
20
|
+
vi.setSystemTime(700000);
|
|
21
|
+
const t = convexTest(schema, modules);
|
|
22
|
+
const meter = {
|
|
23
|
+
...request.meters[0],
|
|
24
|
+
binding: {
|
|
25
|
+
...request.meters[0].binding,
|
|
26
|
+
plan: {
|
|
27
|
+
key: "calls",
|
|
28
|
+
version: "v1",
|
|
29
|
+
model: "included_overage",
|
|
30
|
+
unit: { name: "call", precision: 0 },
|
|
31
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
32
|
+
includedQuantity: "10",
|
|
33
|
+
reset: "billing_period",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const intent = await t.mutation(
|
|
38
|
+
makeFunctionReference<"mutation">("usageSubscriptions/mutations:prepare"),
|
|
39
|
+
{
|
|
40
|
+
termsJson: JSON.stringify({ ...request, meters: [meter] }),
|
|
41
|
+
method: "direct",
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
await t.mutation(
|
|
45
|
+
makeFunctionReference<"mutation">("usageSubscriptions/mutations:save"),
|
|
46
|
+
{
|
|
47
|
+
key: intent.key,
|
|
48
|
+
nativeJson: JSON.stringify(nativeSubscription()),
|
|
49
|
+
startedAt: Date.now(),
|
|
50
|
+
},
|
|
51
|
+
);
|
|
52
|
+
const subscription = {
|
|
53
|
+
subscriptionId: "sub_calls",
|
|
54
|
+
subscriptionItemId: "si_calls",
|
|
55
|
+
stripePriceId: "price_calls",
|
|
56
|
+
period: { start: 600, end: 1200 },
|
|
57
|
+
interval: "month",
|
|
58
|
+
};
|
|
59
|
+
const bindingJson = JSON.stringify({ ...meter, subscription });
|
|
60
|
+
const event = {
|
|
61
|
+
...meter,
|
|
62
|
+
sourceEventId: "work1",
|
|
63
|
+
quantity: "6",
|
|
64
|
+
occurredAt: 700,
|
|
65
|
+
};
|
|
66
|
+
const args = {
|
|
67
|
+
inputJson: JSON.stringify(event),
|
|
68
|
+
subscriptionJson: JSON.stringify(subscription),
|
|
69
|
+
};
|
|
70
|
+
const configure = (value = policy, expectedRevision = 0) =>
|
|
71
|
+
t.mutation(mutation("configure"), {
|
|
72
|
+
bindingJson,
|
|
73
|
+
policyJson: JSON.stringify(value),
|
|
74
|
+
expectedRevision,
|
|
75
|
+
});
|
|
76
|
+
return { t, meter, event, subscription, bindingJson, args, configure };
|
|
77
|
+
}
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { setupAllowance as setup } from "./allowance.fixtures.js";
|
|
2
2
|
import { makeFunctionReference } from "convex/server";
|
|
3
3
|
import { afterEach, expect, it, vi } from "vitest";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
request,
|
|
7
|
-
nativeSubscription,
|
|
8
|
-
} from "../../client/usage/subscription.fixtures.js";
|
|
4
|
+
import { nativeSubscription } from "../../client/usage/subscription.fixtures.js";
|
|
9
5
|
const mutation = (name: string) =>
|
|
10
6
|
makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
|
|
11
7
|
const status = makeFunctionReference<"query">("usageAllowance/queries:status");
|
|
@@ -17,66 +13,7 @@ const policy = {
|
|
|
17
13
|
overage: "allow",
|
|
18
14
|
limit: { mode: "hard", quantity: "12" },
|
|
19
15
|
};
|
|
20
|
-
|
|
21
|
-
vi.useFakeTimers();
|
|
22
|
-
vi.setSystemTime(700000);
|
|
23
|
-
const t = convexTest(schema, modules);
|
|
24
|
-
const meter = {
|
|
25
|
-
...request.meters[0],
|
|
26
|
-
binding: {
|
|
27
|
-
...request.meters[0].binding,
|
|
28
|
-
plan: {
|
|
29
|
-
key: "calls",
|
|
30
|
-
version: "v1",
|
|
31
|
-
model: "included_overage",
|
|
32
|
-
unit: { name: "call", precision: 0 },
|
|
33
|
-
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
34
|
-
includedQuantity: "10",
|
|
35
|
-
reset: "billing_period",
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
const intent = await t.mutation(
|
|
40
|
-
makeFunctionReference<"mutation">("usageSubscriptions/mutations:prepare"),
|
|
41
|
-
{
|
|
42
|
-
termsJson: JSON.stringify({ ...request, meters: [meter] }),
|
|
43
|
-
method: "direct",
|
|
44
|
-
},
|
|
45
|
-
);
|
|
46
|
-
await t.mutation(
|
|
47
|
-
makeFunctionReference<"mutation">("usageSubscriptions/mutations:save"),
|
|
48
|
-
{
|
|
49
|
-
key: intent.key,
|
|
50
|
-
nativeJson: JSON.stringify(nativeSubscription()),
|
|
51
|
-
startedAt: Date.now(),
|
|
52
|
-
},
|
|
53
|
-
);
|
|
54
|
-
const subscription = {
|
|
55
|
-
subscriptionId: "sub_calls",
|
|
56
|
-
subscriptionItemId: "si_calls",
|
|
57
|
-
stripePriceId: "price_calls",
|
|
58
|
-
period: { start: 600, end: 1200 },
|
|
59
|
-
interval: "month",
|
|
60
|
-
};
|
|
61
|
-
const bindingJson = JSON.stringify({ ...meter, subscription });
|
|
62
|
-
const event = {
|
|
63
|
-
...meter,
|
|
64
|
-
sourceEventId: "work1",
|
|
65
|
-
quantity: "6",
|
|
66
|
-
occurredAt: 700,
|
|
67
|
-
};
|
|
68
|
-
const args = {
|
|
69
|
-
inputJson: JSON.stringify(event),
|
|
70
|
-
subscriptionJson: JSON.stringify(subscription),
|
|
71
|
-
};
|
|
72
|
-
const configure = (value = policy, expectedRevision = 0) =>
|
|
73
|
-
t.mutation(mutation("configure"), {
|
|
74
|
-
bindingJson,
|
|
75
|
-
policyJson: JSON.stringify(value),
|
|
76
|
-
expectedRevision,
|
|
77
|
-
});
|
|
78
|
-
return { t, meter, event, subscription, bindingJson, args, configure };
|
|
79
|
-
}
|
|
16
|
+
|
|
80
17
|
afterEach(() => vi.useRealTimers());
|
|
81
18
|
it("reserves a hard cap atomically and commits included usage and overage exactly once", async () => {
|
|
82
19
|
const { t, args, configure, bindingJson, event } = await setup();
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { makeFunctionReference } from "convex/server";
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import { setupAllowance } from "./allowance.fixtures.js";
|
|
4
|
+
const mutation = (name: string) =>
|
|
5
|
+
makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
|
|
6
|
+
const reconciliation = makeFunctionReference<"query">(
|
|
7
|
+
"usageAllowance/queries:reconciliation",
|
|
8
|
+
);
|
|
9
|
+
const record = makeFunctionReference<"mutation">("usage/mutations:record");
|
|
10
|
+
afterEach(() => vi.useRealTimers());
|
|
11
|
+
for (const commit of [false, true])
|
|
12
|
+
it(`matches exactly 500 ${commit ? "accepted events" : "open reservations"} and holds 501 without repairing`, async () => {
|
|
13
|
+
const { t, event, subscription, bindingJson, configure } =
|
|
14
|
+
await setupAllowance();
|
|
15
|
+
await configure({
|
|
16
|
+
reset: "billing_period",
|
|
17
|
+
rollover: "none",
|
|
18
|
+
entitled: true,
|
|
19
|
+
overage: "allow",
|
|
20
|
+
limit: { mode: "hard", quantity: "1000" },
|
|
21
|
+
});
|
|
22
|
+
const add = async (i: number) => {
|
|
23
|
+
const args = {
|
|
24
|
+
inputJson: JSON.stringify({
|
|
25
|
+
...event,
|
|
26
|
+
sourceEventId: `bound_${i}`,
|
|
27
|
+
quantity: "1",
|
|
28
|
+
}),
|
|
29
|
+
subscriptionJson: JSON.stringify(subscription),
|
|
30
|
+
};
|
|
31
|
+
const reserved = await t.mutation(mutation("reserve"), args);
|
|
32
|
+
expect(reserved.state).toBe("reserved");
|
|
33
|
+
if (commit)
|
|
34
|
+
await t.mutation(record, {
|
|
35
|
+
...args,
|
|
36
|
+
reservationId: reserved.reservationId,
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
for (let i = 0; i < 500; i++) await add(i);
|
|
40
|
+
expect(await t.query(reconciliation, { bindingJson })).toMatchObject({
|
|
41
|
+
status: "matched",
|
|
42
|
+
consumedScaledQuantity: commit ? "500" : "0",
|
|
43
|
+
reservedScaledQuantity: commit ? "0" : "500",
|
|
44
|
+
repaired: false,
|
|
45
|
+
});
|
|
46
|
+
await add(500);
|
|
47
|
+
expect(await t.query(reconciliation, { bindingJson })).toMatchObject({
|
|
48
|
+
status: "held",
|
|
49
|
+
reason: "local_evidence_incomplete",
|
|
50
|
+
consumedScaledQuantity: commit ? "501" : "0",
|
|
51
|
+
reservedScaledQuantity: commit ? "0" : "501",
|
|
52
|
+
repaired: false,
|
|
53
|
+
});
|
|
54
|
+
}, 30000);
|
|
55
|
+
it("commits the 100000th allowance audit entry and rolls back one over the retained quota", async () => {
|
|
56
|
+
const { t, configure, args } = await setupAllowance();
|
|
57
|
+
await configure();
|
|
58
|
+
await t.run(async (ctx) => {
|
|
59
|
+
const row = (await ctx.db.query("usageAllowancePolicies").first())!;
|
|
60
|
+
await ctx.db.patch(row._id, { historyCount: 99999 });
|
|
61
|
+
});
|
|
62
|
+
const reservation = await t.mutation(mutation("reserve"), args);
|
|
63
|
+
expect(reservation.state).toBe("reserved");
|
|
64
|
+
await expect(
|
|
65
|
+
t.mutation(record, { ...args, reservationId: reservation.reservationId }),
|
|
66
|
+
).rejects.toThrow(/history quota/i);
|
|
67
|
+
await t.run(async (ctx) => {
|
|
68
|
+
expect(await ctx.db.query("usageEvents").first()).toBeNull();
|
|
69
|
+
expect(await ctx.db.query("usageOutbox").first()).toBeNull();
|
|
70
|
+
expect(await ctx.db.query("usageReservations").first()).toMatchObject({
|
|
71
|
+
state: "reserved",
|
|
72
|
+
});
|
|
73
|
+
expect(await ctx.db.query("usageAllowancePolicies").first()).toMatchObject({
|
|
74
|
+
historyCount: 100000,
|
|
75
|
+
consumed: "0",
|
|
76
|
+
reserved: "6",
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -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
|
+
});
|