Add comprehensive logging to debug checkout issues
Added console.log statements to track: - Client: API response status, data, and errors - Server: Stripe configuration check, rate limiting, order creation, Stripe session creation This will help identify exactly where the checkout flow is failing. Check browser console for client-side errors and server logs for API errors. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
c9f5626984
commit
3d48e4d545
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user