2026-05-25 17:27:08 +04:00
|
|
|
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;
|
|
|
|
|
|
2026-05-25 18:36:12 +04:00
|
|
|
const rawEmail = session.user.email ?? "";
|
|
|
|
|
const looksLikeEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(rawEmail);
|
|
|
|
|
const customerEmail = looksLikeEmail ? rawEmail : undefined;
|
|
|
|
|
|
2026-05-25 17:27:08 +04:00
|
|
|
try {
|
|
|
|
|
const result = await createCheckoutSession({
|
|
|
|
|
userId: session.user.id,
|
2026-05-25 18:36:12 +04:00
|
|
|
customerEmail,
|
2026-05-25 17:27:08 +04:00
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|