45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
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;
|
|
const priceId = process.env.PRICE_ID;
|
|
if (!baseUrl || !priceId) {
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
"Missing NEXT_PUBLIC_BASE_URL or PRICE_ID. Set both before creating subscriptions.",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
const session = await stripeClient.checkout.sessions.create({
|
|
customer_account: accountId,
|
|
mode: "subscription",
|
|
line_items: [{ price: priceId, quantity: 1 }],
|
|
success_url: `${baseUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
|
|
cancel_url: `${baseUrl}/cancel`,
|
|
});
|
|
|
|
return NextResponse.json({ url: session.url });
|
|
} catch (err: any) {
|
|
return NextResponse.json(
|
|
{ error: err?.message || "Failed to create subscription checkout." },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|