website/app/api/checkout/route.ts

35 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
// Map price IDs to human-readable tier names
const TIER_NAMES: Record<string, string> = {
[process.env.STRIPE_PRICE_WEEKLY!]: "Weekly",
[process.env.STRIPE_PRICE_MONTHLY!]: "Monthly",
[process.env.STRIPE_PRICE_ANNUAL!]: "Annual",
[process.env.STRIPE_PRICE_LIFETIME!]: "Lifetime",
};
export async function POST(req: NextRequest) {
const { priceId } = await req.json();
if (!priceId || !TIER_NAMES[priceId]) {
return NextResponse.json({ error: "Invalid price" }, { status: 400 });
}
const session = await stripe.checkout.sessions.create({
mode: "payment",
line_items: [{ price: priceId, quantity: 1 }],
success_url: `${process.env.NEXT_PUBLIC_SITE_URL}/purchase/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.NEXT_PUBLIC_SITE_URL}/uit#pricing`,
metadata: { tier: TIER_NAMES[priceId] },
invoice_creation: { enabled: true },
custom_text: {
submit: { message: "Your license key will be emailed to you immediately after purchase." },
},
});
return NextResponse.json({ url: session.url });
}