import { NextResponse } from 'next/server'; import { headers } from 'next/headers'; import { storeReceiptId } from '../../../lib/receipts'; export async function POST(req: Request) { try { const body = await req.text(); const headersList = await headers(); const signature = headersList.get('stripe-signature'); if (!signature) { console.error('No Stripe signature found'); return NextResponse.json({ error: 'No signature' }, { status: 400 }); } // For now, we'll process the webhook without signature verification // In production, you should verify the signature using Stripe's webhook secret const event = JSON.parse(body); console.log('Stripe webhook event:', event.type); if (event.type === 'checkout.session.completed') { const session = event.data.object; const sessionId = session.id; const paymentIntentId = session.payment_intent; const boxId = session.metadata?.boxId; console.log('Payment completed:', { sessionId, paymentIntentId, boxId, amount: session.amount_total, currency: session.currency, }); // Store the payment intent ID as the receipt ID if (paymentIntentId) { storeReceiptId(sessionId, paymentIntentId); console.log(`Stored receipt ID ${paymentIntentId} for session ${sessionId}`); } } return NextResponse.json({ received: true }); } catch (error) { console.error('Webhook error:', error); return NextResponse.json( { error: 'Webhook handler failed' }, { status: 400 } ); } }