app/shop/actions.ts
"use server";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
// product id in the cart data -> Stripe Price id (create the prices in your Stripe dashboard)
const PRICES: Record<string, string> = {
orbit: "price_xxx",
case: "price_yyy",
cable: "price_zzz",
};
export async function createCheckout(items: { id: string; qty: number }[]) {
const session = await stripe.checkout.sessions.create({
mode: "payment",
line_items: items.map((i) => ({ price: PRICES[i.id], quantity: i.qty })),
success_url: `${process.env.NEXT_PUBLIC_SITE_URL}/thanks?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.NEXT_PUBLIC_SITE_URL}/shop`,
});
if (!session.url) throw new Error("Stripe returned no checkout URL");
return session.url;
}