57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { headers } from 'next/headers';
|
|
|
|
// In-memory storage for demo purposes - in production, use a database
|
|
const paymentReceipts = new Map<string, string>();
|
|
|
|
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) {
|
|
paymentReceipts.set(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 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// Helper function to get receipt ID for a session
|
|
export function getReceiptId(sessionId: string): string | null {
|
|
return paymentReceipts.get(sessionId) || null;
|
|
}
|