diff --git a/src/app/api/checkout/route.ts b/src/app/api/checkout/route.ts index 775f94b..05cad72 100644 --- a/src/app/api/checkout/route.ts +++ b/src/app/api/checkout/route.ts @@ -29,23 +29,27 @@ type GuestInfo = { }; export async function POST(req: Request) { - if (!process.env.STRIPE_SECRET_KEY) { - return NextResponse.json( - { error: 'Stripe is not configured on this server yet. Add STRIPE_SECRET_KEY to .env.' }, - { status: 500 }, - ); - } + try { + if (!process.env.STRIPE_SECRET_KEY) { + console.error('STRIPE_SECRET_KEY is not configured'); + return NextResponse.json( + { error: 'Stripe is not configured on this server yet. Add STRIPE_SECRET_KEY to .env.' }, + { status: 500 }, + ); + } - const ip = getClientIp(req.headers); - const { allowed } = await checkRateLimit(`checkout:${ip}`, 20, 10 * 60 * 1000); - if (!allowed) { - return NextResponse.json( - { error: 'Too many requests — please wait a moment and try again.' }, - { status: 429 }, - ); - } + const ip = getClientIp(req.headers); + const { allowed } = await checkRateLimit(`checkout:${ip}`, 20, 10 * 60 * 1000); + if (!allowed) { + console.error('Rate limit exceeded for IP:', ip); + return NextResponse.json( + { error: 'Too many requests — please wait a moment and try again.' }, + { status: 429 }, + ); + } - const body = await req.json(); + const body = await req.json(); + console.log('Checkout request received with items:', body.items?.length, 'customerId:', body.customerId); const items: CheckoutCartItem[] = body.items ?? []; const guest: GuestInfo | undefined = body.guest; @@ -111,6 +115,7 @@ export async function POST(req: Request) { const siteUrl = process.env.NEXT_PUBLIC_SITE_URL ?? 'http://localhost:3000'; try { + console.log('Creating Stripe session for order:', order.id, 'total:', total); const session = await stripe.checkout.sessions.create({ mode: 'payment', line_items: pricedItems.map((i) => ({ @@ -132,6 +137,8 @@ export async function POST(req: Request) { metadata: { orderId: order.id }, }); + console.log('Stripe session created:', session.id, 'URL:', session.url); + await prisma.order.update({ where: { id: order.id }, data: { stripeSessionId: session.id }, @@ -140,8 +147,11 @@ export async function POST(req: Request) { return NextResponse.json({ url: session.url }); } catch (err) { // Stripe call failed — don't leave an orphaned PENDING order with no way to pay it. + console.error('Stripe session creation failed:', err); await prisma.order.delete({ where: { id: order.id } }); const message = err instanceof Error ? err.message : 'Could not start checkout.'; + console.error('Returning error to client:', message); return NextResponse.json({ error: message }, { status: 500 }); } } +} diff --git a/src/app/checkout/client.tsx b/src/app/checkout/client.tsx index 0ab6872..7a72d7b 100644 --- a/src/app/checkout/client.tsx +++ b/src/app/checkout/client.tsx @@ -123,12 +123,15 @@ export default function CheckoutPageClient({ isLoggedIn, customer }: CheckoutPag body: JSON.stringify({ items, customerId: customer.id }), }); const data = await res.json(); + console.log('Checkout response:', { status: res.status, ok: res.ok, data }); if (!res.ok || !data.url) { throw new Error(data.error ?? 'Something went wrong starting checkout.'); } window.location.href = data.url; } catch (err) { - setCheckoutError(err instanceof Error ? err.message : 'Something went wrong.'); + const errorMsg = err instanceof Error ? err.message : 'Something went wrong.'; + console.error('Checkout error:', errorMsg); + setCheckoutError(errorMsg); setCheckingOut(false); } }}