- Analytics: <Analytics /> in root layout loads umami when NEXT_PUBLIC_UMAMI_* env are set - Subscription: free/pro tiers in Postgres, daily quotas (10msg/5tool free, 1000/500 pro) - Chat route: checkAndConsume(chat_message) gate before streamText, 429 when exhausted - Tool metering: getWeather wrapped with withLimit decorator that deducts tool_call quota - Pricing page at /pricing with upgrade button - /api/billing/checkout posts to EGBE payment gateway, /api/billing/webhook verifies HMAC and upgrades user on checkout.session.completed - UsageBadge in chat header polls /api/usage every 15s - Dropped now-unused entitlements.ts
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { auth } from "@/app/(auth)/auth";
|
|
import {
|
|
createCheckoutSession,
|
|
isBillingConfigured,
|
|
} from "@/lib/billing/gateway";
|
|
import { ChatbotError } from "@/lib/errors";
|
|
|
|
export async function POST(request: Request) {
|
|
const session = await auth();
|
|
if (!session?.user) {
|
|
return new ChatbotError("unauthorized:chat").toResponse();
|
|
}
|
|
|
|
if (!isBillingConfigured()) {
|
|
return Response.json(
|
|
{
|
|
error: "billing_not_configured",
|
|
message:
|
|
"Set EGBE_PAYMENT_GATEWAY_URL, EGBE_GATEWAYS_TOKEN, and PRO_STRIPE_PRICE_ID to enable upgrades.",
|
|
},
|
|
{ status: 501 }
|
|
);
|
|
}
|
|
|
|
const appSlug = process.env.APP_SLUG ?? "chatbot";
|
|
const baseUrl =
|
|
process.env.NEXT_PUBLIC_BASE_URL ?? new URL(request.url).origin;
|
|
|
|
try {
|
|
const result = await createCheckoutSession({
|
|
userId: session.user.id,
|
|
customerEmail: session.user.email ?? undefined,
|
|
appSlug,
|
|
baseUrl,
|
|
});
|
|
return Response.json({ url: result.url, id: result.id });
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "checkout failed";
|
|
return Response.json({ error: "checkout_failed", message }, { status: 502 });
|
|
}
|
|
}
|