@fantastic.dev/better-stripe 0.50.3 → 0.52.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 +33 -0
- package/README.md +5 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +166 -32
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +83 -16
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +134 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +67 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +4 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/usage/boundaries.d.ts +132 -0
- package/dist/client/usage/boundaries.d.ts.map +1 -0
- package/dist/client/usage/boundaries.js +43 -0
- package/dist/client/usage/boundaries.js.map +1 -0
- package/dist/client/usage/client.d.ts +37 -0
- package/dist/client/usage/client.d.ts.map +1 -0
- package/dist/client/usage/client.js +59 -0
- package/dist/client/usage/client.js.map +1 -0
- package/dist/client/usage/contracts.d.ts +59 -0
- package/dist/client/usage/contracts.d.ts.map +1 -0
- package/dist/client/usage/contracts.js +63 -0
- package/dist/client/usage/contracts.js.map +1 -0
- package/dist/client/usage/index.d.ts +7 -0
- package/dist/client/usage/index.d.ts.map +1 -0
- package/dist/client/usage/index.js +4 -0
- package/dist/client/usage/index.js.map +1 -0
- package/dist/client/usage/records.d.ts +50 -0
- package/dist/client/usage/records.d.ts.map +1 -0
- package/dist/client/usage/records.js +54 -0
- package/dist/client/usage/records.js.map +1 -0
- package/dist/component/_generated/api.d.ts +6 -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 +79 -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/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +178 -16
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/schema.d.ts +67 -0
- package/dist/component/schema.js +5 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/helpers.d.ts +13 -0
- package/dist/component/usage/helpers.d.ts.map +1 -0
- package/dist/component/usage/helpers.js +34 -0
- package/dist/component/usage/helpers.js.map +1 -0
- package/dist/component/usage/mutations.d.ts +14 -0
- package/dist/component/usage/mutations.d.ts.map +1 -0
- package/dist/component/usage/mutations.js +124 -0
- package/dist/component/usage/mutations.js.map +1 -0
- package/dist/component/usage/queries.d.ts +62 -0
- package/dist/component/usage/queries.d.ts.map +1 -0
- package/dist/component/usage/queries.js +65 -0
- package/dist/component/usage/queries.js.map +1 -0
- package/dist/component/usage/schema.d.ts +110 -0
- package/dist/component/usage/schema.d.ts.map +1 -0
- package/dist/component/usage/schema.js +54 -0
- package/dist/component/usage/schema.js.map +1 -0
- package/docs/README.md +7 -0
- package/docs/development.md +26 -8
- package/docs/metered-usage.md +289 -37
- package/docs/metering-setup.md +184 -0
- package/docs/usage-ledger.md +62 -0
- package/package.json +8 -2
- package/src/client/index.ts +5 -0
- package/src/client/transferContinuation.test.ts +2 -6
- package/src/client/usage/boundaries.test.ts +136 -0
- package/src/client/usage/boundaries.ts +182 -0
- package/src/client/usage/client.ts +97 -0
- package/src/client/usage/contracts.test.ts +145 -0
- package/src/client/usage/contracts.ts +79 -0
- package/src/client/usage/index.ts +34 -0
- package/src/client/usage/ledger-client.test.ts +74 -0
- package/src/client/usage/records.ts +76 -0
- package/src/component/_generated/api.ts +6 -0
- package/src/component/_generated/component.ts +106 -0
- package/src/component/schema.ts +10 -0
- package/src/component/usage/helpers.ts +33 -0
- package/src/component/usage/ledger.test.ts +310 -0
- package/src/component/usage/mutations.ts +139 -0
- package/src/component/usage/queries.ts +69 -0
- package/src/component/usage/schema.ts +65 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const unit = z.strictObject({
|
|
4
|
+
name: z.string().min(1).max(100),
|
|
5
|
+
precision: z.number().int().min(0).max(6),
|
|
6
|
+
});
|
|
7
|
+
const rate = z.strictObject({
|
|
8
|
+
currency: z.string().regex(/^[a-z]{3}$/),
|
|
9
|
+
// Decimal minor currency units per whole billable unit, never a JS float.
|
|
10
|
+
unitAmountDecimal: z.string().regex(/^(0|[1-9]\d{0,11})(\.\d{1,12})?$/),
|
|
11
|
+
});
|
|
12
|
+
const common = {
|
|
13
|
+
key: z.string().min(1).max(100),
|
|
14
|
+
version: z.string().min(1).max(100),
|
|
15
|
+
unit,
|
|
16
|
+
rate,
|
|
17
|
+
};
|
|
18
|
+
const planSchema = z.discriminatedUnion("model", [
|
|
19
|
+
z.strictObject({ ...common, model: z.literal("pay_as_you_go") }),
|
|
20
|
+
z.strictObject({
|
|
21
|
+
...common,
|
|
22
|
+
model: z.literal("included_overage"),
|
|
23
|
+
includedQuantity: z.string(),
|
|
24
|
+
reset: z.literal("billing_period"),
|
|
25
|
+
}),
|
|
26
|
+
z.strictObject({
|
|
27
|
+
...common,
|
|
28
|
+
model: z.literal("prepaid"),
|
|
29
|
+
insufficientBalance: z.literal("reject"),
|
|
30
|
+
}),
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
/** Configuration contracts only. Validating a plan does not enable billing. */
|
|
34
|
+
export type UsagePlan = z.infer<typeof planSchema>;
|
|
35
|
+
export type UsageBillingModel = UsagePlan["model"];
|
|
36
|
+
export type ExactUsageQuantity = {
|
|
37
|
+
quantity: string;
|
|
38
|
+
scaledQuantity: string;
|
|
39
|
+
precision: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parse a nonnegative decimal quantity without rounding. The shared contract
|
|
44
|
+
* supports 0–6 decimal places and at most 18 scaled digits. A provider adapter
|
|
45
|
+
* must additionally enforce its own supported bounds before accepting usage.
|
|
46
|
+
*/
|
|
47
|
+
export function parseUsageQuantity(
|
|
48
|
+
value: unknown,
|
|
49
|
+
precision: number,
|
|
50
|
+
): ExactUsageQuantity {
|
|
51
|
+
if (!Number.isInteger(precision) || precision < 0 || precision > 6)
|
|
52
|
+
throw new Error("Usage precision must be an integer between 0 and 6");
|
|
53
|
+
if (
|
|
54
|
+
typeof value !== "string" ||
|
|
55
|
+
value.length > 25 ||
|
|
56
|
+
!/^(0|[1-9]\d*)(\.\d+)?$/.test(value)
|
|
57
|
+
)
|
|
58
|
+
throw new Error("Usage quantity must be a nonnegative decimal string");
|
|
59
|
+
const [whole, fraction = ""] = value.split(".");
|
|
60
|
+
if (fraction.length > precision)
|
|
61
|
+
throw new Error("Usage quantity exceeds configured precision");
|
|
62
|
+
const scaled = BigInt(whole + fraction.padEnd(precision, "0"));
|
|
63
|
+
if (scaled > 999_999_999_999_999_999n)
|
|
64
|
+
throw new Error("Usage quantity exceeds the shared scaled bound");
|
|
65
|
+
const normalizedFraction = fraction.replace(/0+$/, "");
|
|
66
|
+
return {
|
|
67
|
+
quantity: normalizedFraction ? `${whole}.${normalizedFraction}` : whole,
|
|
68
|
+
scaledQuantity: scaled.toString(),
|
|
69
|
+
precision,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Validate and copy terms; provider provisioning and host authorization are separate. */
|
|
74
|
+
export function defineUsagePlan(input: unknown): UsagePlan {
|
|
75
|
+
const plan = planSchema.parse(input);
|
|
76
|
+
if (plan.model === "included_overage")
|
|
77
|
+
parseUsageQuantity(plan.includedQuantity, plan.unit.precision);
|
|
78
|
+
return plan;
|
|
79
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export { defineUsagePlan, parseUsageQuantity } from "./contracts.js";
|
|
2
|
+
export type {
|
|
3
|
+
UsagePlan,
|
|
4
|
+
UsageBillingModel,
|
|
5
|
+
ExactUsageQuantity,
|
|
6
|
+
} from "./contracts.js";
|
|
7
|
+
export { prepareUsageRecord } from "./records.js";
|
|
8
|
+
export type {
|
|
9
|
+
UsageScope,
|
|
10
|
+
PreparedUsageRecord,
|
|
11
|
+
UsageDeliveryState,
|
|
12
|
+
} from "./records.js";
|
|
13
|
+
export {
|
|
14
|
+
assertUsageReplay,
|
|
15
|
+
validateUsagePageRequest,
|
|
16
|
+
authorizeUsageRecord,
|
|
17
|
+
} from "./boundaries.js";
|
|
18
|
+
export type {
|
|
19
|
+
UsagePeriod,
|
|
20
|
+
UsagePageRequest,
|
|
21
|
+
UsageAvailability,
|
|
22
|
+
UsageAuthorization,
|
|
23
|
+
UsageObservations,
|
|
24
|
+
UsageCorrectionRequest,
|
|
25
|
+
UsageRetentionPolicy,
|
|
26
|
+
UsageTransitionRequest,
|
|
27
|
+
UsageSubscriptionBinding,
|
|
28
|
+
UsageCreditProvenance,
|
|
29
|
+
UsageCorrectionOutcome,
|
|
30
|
+
UsageReceipt,
|
|
31
|
+
UsageHistoryPage,
|
|
32
|
+
UsageProviderCapabilities,
|
|
33
|
+
UsageContractPort,
|
|
34
|
+
} from "./boundaries.js";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { componentsGeneric, type FunctionReference } from "convex/server";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { initConvexTest } from "../setup.test.js";
|
|
4
|
+
import schema from "../../component/schema.js";
|
|
5
|
+
import { modules } from "../../test.js";
|
|
6
|
+
import { BetterStripe } from "../index.js";
|
|
7
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
8
|
+
it("records and reads through the public facade without resolving a Stripe secret", async () => {
|
|
9
|
+
const t = initConvexTest();
|
|
10
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
11
|
+
const stripe = new BetterStripe(
|
|
12
|
+
componentsGeneric().betterStripe as unknown as Component,
|
|
13
|
+
);
|
|
14
|
+
const ctx = {
|
|
15
|
+
runQuery: (
|
|
16
|
+
ref: FunctionReference<"query">,
|
|
17
|
+
args: Record<string, unknown>,
|
|
18
|
+
) => t.query(ref, args),
|
|
19
|
+
runMutation: (
|
|
20
|
+
ref: FunctionReference<"mutation">,
|
|
21
|
+
args: Record<string, unknown>,
|
|
22
|
+
) => t.mutation(ref, args),
|
|
23
|
+
} as RunCtx;
|
|
24
|
+
const now = Math.floor(Date.now() / 1000);
|
|
25
|
+
const scope = {
|
|
26
|
+
stripeAccountId: "acct_platform",
|
|
27
|
+
customerAccount: "acct_buyer",
|
|
28
|
+
meterId: "mtr_calls",
|
|
29
|
+
livemode: false,
|
|
30
|
+
};
|
|
31
|
+
const event = {
|
|
32
|
+
scope,
|
|
33
|
+
binding: {
|
|
34
|
+
stripeCustomerId: "cus_buyer",
|
|
35
|
+
stripePriceId: "price_calls",
|
|
36
|
+
eventName: "calls",
|
|
37
|
+
plan: {
|
|
38
|
+
key: "calls",
|
|
39
|
+
version: "v1",
|
|
40
|
+
model: "pay_as_you_go",
|
|
41
|
+
unit: { name: "call", precision: 0 },
|
|
42
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
sourceEventId: "activity_1",
|
|
46
|
+
quantity: "1",
|
|
47
|
+
occurredAt: now,
|
|
48
|
+
};
|
|
49
|
+
const result = await stripe.usage.record(ctx, {
|
|
50
|
+
event,
|
|
51
|
+
subscription: {
|
|
52
|
+
subscriptionId: "sub_calls",
|
|
53
|
+
subscriptionItemId: "si_calls",
|
|
54
|
+
stripePriceId: "price_calls",
|
|
55
|
+
period: { start: now - 1, end: now + 100 },
|
|
56
|
+
interval: "month",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
expect(result).toMatchObject({ delivery: "queued" });
|
|
60
|
+
expect(
|
|
61
|
+
await stripe.usage.get(ctx, { scope, eventId: result.eventId }),
|
|
62
|
+
).toMatchObject({
|
|
63
|
+
record: { sourceEventId: "activity_1" },
|
|
64
|
+
delivery: "queued",
|
|
65
|
+
});
|
|
66
|
+
const page = await stripe.usage.read(ctx, {
|
|
67
|
+
scope,
|
|
68
|
+
page: { period: { start: now - 1, end: now + 2 }, limit: 10, cursor: null },
|
|
69
|
+
});
|
|
70
|
+
expect(page).toMatchObject({
|
|
71
|
+
isDone: true,
|
|
72
|
+
events: [{ record: { quantity: "1" }, delivery: "queued" }],
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { STRIPE_API_VERSION } from "../constants.js";
|
|
3
|
+
import { STRIPE_CUSTOMER_PREVIEW_API_VERSION } from "../apiVersion.js";
|
|
4
|
+
import {
|
|
5
|
+
defineUsagePlan,
|
|
6
|
+
parseUsageQuantity,
|
|
7
|
+
type UsagePlan,
|
|
8
|
+
} from "./contracts.js";
|
|
9
|
+
|
|
10
|
+
export const scopeSchema = z.strictObject({
|
|
11
|
+
stripeAccountId: z.string().regex(/^acct_[a-zA-Z0-9]+$/),
|
|
12
|
+
customerAccount: z.string().regex(/^acct_[a-zA-Z0-9]+$/),
|
|
13
|
+
meterId: z.string().regex(/^mtr_[a-zA-Z0-9_]+$/),
|
|
14
|
+
livemode: z.boolean(),
|
|
15
|
+
});
|
|
16
|
+
const recordSchema = z.strictObject({
|
|
17
|
+
scope: scopeSchema,
|
|
18
|
+
binding: z.strictObject({
|
|
19
|
+
stripeCustomerId: z.string().regex(/^cus_[a-zA-Z0-9]+$/),
|
|
20
|
+
stripePriceId: z.string().regex(/^price_[a-zA-Z0-9]+$/),
|
|
21
|
+
eventName: z.string().min(1).max(100),
|
|
22
|
+
apiVersion: z
|
|
23
|
+
.enum([STRIPE_API_VERSION, STRIPE_CUSTOMER_PREVIEW_API_VERSION])
|
|
24
|
+
.default(STRIPE_API_VERSION),
|
|
25
|
+
plan: z.unknown(),
|
|
26
|
+
}),
|
|
27
|
+
sourceEventId: z.string().min(1).max(200),
|
|
28
|
+
quantity: z.unknown(),
|
|
29
|
+
occurredAt: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/** The exact Stripe realm, Accounts v2 payer, meter and test/live scope. */
|
|
33
|
+
export type UsageScope = z.infer<typeof scopeSchema>;
|
|
34
|
+
export type PreparedUsageRecord = Omit<
|
|
35
|
+
z.infer<typeof recordSchema>,
|
|
36
|
+
"quantity" | "binding"
|
|
37
|
+
> & {
|
|
38
|
+
schemaVersion: 1;
|
|
39
|
+
identity: string;
|
|
40
|
+
quantity: string;
|
|
41
|
+
scaledQuantity: string;
|
|
42
|
+
binding: Omit<z.infer<typeof recordSchema>["binding"], "plan"> & {
|
|
43
|
+
plan: UsagePlan;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export type UsageDeliveryState =
|
|
47
|
+
"queued" | "sending" | "provider_accepted" | "rejected" | "unknown" | "held";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Validate and snapshot a record before acceptance. This pure helper does not
|
|
51
|
+
* authorize an actor, verify provider ownership, persist or deliver anything.
|
|
52
|
+
* Occurrence time is Unix seconds; native provider windows are enforced by the
|
|
53
|
+
* acceptance/delivery implementation, not by changing historical event time.
|
|
54
|
+
*/
|
|
55
|
+
export function prepareUsageRecord(input: unknown): PreparedUsageRecord {
|
|
56
|
+
const record = recordSchema.parse(input);
|
|
57
|
+
const plan = defineUsagePlan(record.binding.plan);
|
|
58
|
+
const quantity = parseUsageQuantity(record.quantity, plan.unit.precision);
|
|
59
|
+
const scope = record.scope;
|
|
60
|
+
return {
|
|
61
|
+
...record,
|
|
62
|
+
schemaVersion: 1,
|
|
63
|
+
quantity: quantity.quantity,
|
|
64
|
+
scaledQuantity: quantity.scaledQuantity,
|
|
65
|
+
binding: { ...record.binding, plan },
|
|
66
|
+
// A plan/version/quantity change must conflict with the original source
|
|
67
|
+
// event, so these are deliberately outside the deduplication identity.
|
|
68
|
+
identity: JSON.stringify([
|
|
69
|
+
scope.livemode,
|
|
70
|
+
scope.stripeAccountId,
|
|
71
|
+
scope.customerAccount,
|
|
72
|
+
scope.meterId,
|
|
73
|
+
record.sourceEventId,
|
|
74
|
+
]),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -162,6 +162,9 @@ import type * as settlement_clawbackSchema from "../settlement/clawbackSchema.js
|
|
|
162
162
|
import type * as settlement_clawbacks from "../settlement/clawbacks.js";
|
|
163
163
|
import type * as settlement_mutations from "../settlement/mutations.js";
|
|
164
164
|
import type * as settlement_queries from "../settlement/queries.js";
|
|
165
|
+
import type * as usage_helpers from "../usage/helpers.js";
|
|
166
|
+
import type * as usage_mutations from "../usage/mutations.js";
|
|
167
|
+
import type * as usage_queries from "../usage/queries.js";
|
|
165
168
|
import type * as webhooks_mutations from "../webhooks/mutations.js";
|
|
166
169
|
import type * as webhooks_queries from "../webhooks/queries.js";
|
|
167
170
|
import type * as webhooks_validators from "../webhooks/validators.js";
|
|
@@ -328,6 +331,9 @@ const fullApi: ApiFromModules<{
|
|
|
328
331
|
"settlement/clawbacks": typeof settlement_clawbacks;
|
|
329
332
|
"settlement/mutations": typeof settlement_mutations;
|
|
330
333
|
"settlement/queries": typeof settlement_queries;
|
|
334
|
+
"usage/helpers": typeof usage_helpers;
|
|
335
|
+
"usage/mutations": typeof usage_mutations;
|
|
336
|
+
"usage/queries": typeof usage_queries;
|
|
331
337
|
"webhooks/mutations": typeof webhooks_mutations;
|
|
332
338
|
"webhooks/queries": typeof webhooks_queries;
|
|
333
339
|
"webhooks/validators": typeof webhooks_validators;
|
|
@@ -8270,6 +8270,112 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
8270
8270
|
>;
|
|
8271
8271
|
};
|
|
8272
8272
|
};
|
|
8273
|
+
usage: {
|
|
8274
|
+
mutations: {
|
|
8275
|
+
record: FunctionReference<
|
|
8276
|
+
"mutation",
|
|
8277
|
+
"internal",
|
|
8278
|
+
{
|
|
8279
|
+
dispatch?: string;
|
|
8280
|
+
inputJson: string;
|
|
8281
|
+
quota?: number;
|
|
8282
|
+
reservationId?: string;
|
|
8283
|
+
subscriptionJson: string;
|
|
8284
|
+
},
|
|
8285
|
+
{
|
|
8286
|
+
acceptedAt: number;
|
|
8287
|
+
delivery: "queued";
|
|
8288
|
+
eventId: string;
|
|
8289
|
+
identity: string;
|
|
8290
|
+
},
|
|
8291
|
+
Name
|
|
8292
|
+
>;
|
|
8293
|
+
};
|
|
8294
|
+
queries: {
|
|
8295
|
+
get: FunctionReference<
|
|
8296
|
+
"query",
|
|
8297
|
+
"internal",
|
|
8298
|
+
{
|
|
8299
|
+
eventId: string;
|
|
8300
|
+
scope: {
|
|
8301
|
+
customerAccount: string;
|
|
8302
|
+
livemode: boolean;
|
|
8303
|
+
meterId: string;
|
|
8304
|
+
stripeAccountId: string;
|
|
8305
|
+
};
|
|
8306
|
+
},
|
|
8307
|
+
{
|
|
8308
|
+
delivery:
|
|
8309
|
+
| "queued"
|
|
8310
|
+
| "sending"
|
|
8311
|
+
| "provider_accepted"
|
|
8312
|
+
| "rejected"
|
|
8313
|
+
| "unknown"
|
|
8314
|
+
| "held";
|
|
8315
|
+
event: {
|
|
8316
|
+
_creationTime: number;
|
|
8317
|
+
_id: string;
|
|
8318
|
+
acceptedAt: number;
|
|
8319
|
+
identity: string;
|
|
8320
|
+
occurredAt: number;
|
|
8321
|
+
recordJson: string;
|
|
8322
|
+
reservationId?: string;
|
|
8323
|
+
scopeKey: string;
|
|
8324
|
+
subscriptionJson: string;
|
|
8325
|
+
};
|
|
8326
|
+
} | null,
|
|
8327
|
+
Name
|
|
8328
|
+
>;
|
|
8329
|
+
read: FunctionReference<
|
|
8330
|
+
"query",
|
|
8331
|
+
"internal",
|
|
8332
|
+
{
|
|
8333
|
+
paginationOpts: {
|
|
8334
|
+
cursor: string | null;
|
|
8335
|
+
endCursor?: string | null;
|
|
8336
|
+
id?: number;
|
|
8337
|
+
maximumBytesRead?: number;
|
|
8338
|
+
maximumRowsRead?: number;
|
|
8339
|
+
numItems: number;
|
|
8340
|
+
};
|
|
8341
|
+
period: { end: number; start: number };
|
|
8342
|
+
scope: {
|
|
8343
|
+
customerAccount: string;
|
|
8344
|
+
livemode: boolean;
|
|
8345
|
+
meterId: string;
|
|
8346
|
+
stripeAccountId: string;
|
|
8347
|
+
};
|
|
8348
|
+
},
|
|
8349
|
+
{
|
|
8350
|
+
continueCursor: string;
|
|
8351
|
+
isDone: boolean;
|
|
8352
|
+
page: Array<{
|
|
8353
|
+
delivery:
|
|
8354
|
+
| "queued"
|
|
8355
|
+
| "sending"
|
|
8356
|
+
| "provider_accepted"
|
|
8357
|
+
| "rejected"
|
|
8358
|
+
| "unknown"
|
|
8359
|
+
| "held";
|
|
8360
|
+
event: {
|
|
8361
|
+
_creationTime: number;
|
|
8362
|
+
_id: string;
|
|
8363
|
+
acceptedAt: number;
|
|
8364
|
+
identity: string;
|
|
8365
|
+
occurredAt: number;
|
|
8366
|
+
recordJson: string;
|
|
8367
|
+
reservationId?: string;
|
|
8368
|
+
scopeKey: string;
|
|
8369
|
+
subscriptionJson: string;
|
|
8370
|
+
};
|
|
8371
|
+
}>;
|
|
8372
|
+
pageStatus?: "SplitRecommended" | "SplitRequired" | null;
|
|
8373
|
+
splitCursor?: string | null;
|
|
8374
|
+
},
|
|
8375
|
+
Name
|
|
8376
|
+
>;
|
|
8377
|
+
};
|
|
8378
|
+
};
|
|
8273
8379
|
webhooks: {
|
|
8274
8380
|
mutations: {
|
|
8275
8381
|
insertWebhookEvent: FunctionReference<
|
package/src/component/schema.ts
CHANGED
|
@@ -69,6 +69,12 @@ import {
|
|
|
69
69
|
commissionDecisionsTable,
|
|
70
70
|
} from "./settlement/schema";
|
|
71
71
|
import { defineSchema } from "convex/server";
|
|
72
|
+
import {
|
|
73
|
+
usageEventsTable,
|
|
74
|
+
usageOutboxTable,
|
|
75
|
+
usageScopeCountersTable,
|
|
76
|
+
usageReservationsTable,
|
|
77
|
+
} from "./usage/schema.js";
|
|
72
78
|
|
|
73
79
|
import {
|
|
74
80
|
checkoutSessionsTable,
|
|
@@ -89,6 +95,10 @@ import { pricesTable, productsTable } from "./products/schema";
|
|
|
89
95
|
import { webhookEventsTable } from "./webhooks/schema";
|
|
90
96
|
|
|
91
97
|
export default defineSchema({
|
|
98
|
+
usageEvents: usageEventsTable,
|
|
99
|
+
usageOutbox: usageOutboxTable,
|
|
100
|
+
usageScopeCounters: usageScopeCountersTable,
|
|
101
|
+
usageReservations: usageReservationsTable,
|
|
92
102
|
refundSyncClaims: refundSyncClaimsTable,
|
|
93
103
|
processingCostReconciliationOperations,
|
|
94
104
|
recoveryWorkflowOperations,
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { scopeSchema } from "../../client/usage/records.js";
|
|
3
|
+
import type { UsageScope } from "../../client/usage/records.js";
|
|
4
|
+
export function scopeKey(input: UsageScope) {
|
|
5
|
+
const key = JSON.stringify(scopeSchema.parse(input));
|
|
6
|
+
if (key.length > 512) throw new Error("Usage scope exceeds size limit");
|
|
7
|
+
return key;
|
|
8
|
+
}
|
|
9
|
+
const seconds = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER);
|
|
10
|
+
const subscriptionSchema = z.strictObject({
|
|
11
|
+
subscriptionId: z
|
|
12
|
+
.string()
|
|
13
|
+
.regex(/^sub_[a-zA-Z0-9]+$/)
|
|
14
|
+
.max(100),
|
|
15
|
+
subscriptionItemId: z
|
|
16
|
+
.string()
|
|
17
|
+
.regex(/^si_[a-zA-Z0-9]+$/)
|
|
18
|
+
.max(100),
|
|
19
|
+
stripePriceId: z
|
|
20
|
+
.string()
|
|
21
|
+
.regex(/^price_[a-zA-Z0-9]+$/)
|
|
22
|
+
.max(100),
|
|
23
|
+
period: z.strictObject({ start: seconds, end: seconds }),
|
|
24
|
+
interval: z.literal("month"),
|
|
25
|
+
});
|
|
26
|
+
export function subscriptionSnapshot(json: string) {
|
|
27
|
+
if (new TextEncoder().encode(json).length > 2048)
|
|
28
|
+
throw new Error("Usage subscription exceeds size limit");
|
|
29
|
+
const subscription = subscriptionSchema.parse(JSON.parse(json));
|
|
30
|
+
if (subscription.period.end <= subscription.period.start)
|
|
31
|
+
throw new Error("Invalid usage subscription period");
|
|
32
|
+
return subscription;
|
|
33
|
+
}
|