import { NextResponse } from "next/server"; import { stripeClient } from "../../../../../lib/stripeClient"; export async function POST(req: Request) { try { const body = await req.json(); const accountId = body.accountId as string; if (!accountId) { return NextResponse.json( { error: "accountId is required." }, { status: 400 } ); } const baseUrl = process.env.NEXT_PUBLIC_BASE_URL; if (!baseUrl) { return NextResponse.json( { error: "Missing NEXT_PUBLIC_BASE_URL for billing portal return URL." }, { status: 500 } ); } const session = await stripeClient.billingPortal.sessions.create({ customer_account: accountId, return_url: `${baseUrl}/connect`, }); return NextResponse.json({ url: session.url }); } catch (err: any) { return NextResponse.json( { error: err?.message || "Failed to create billing portal session." }, { status: 500 } ); } }