Rocky_Mountain_Vending/app/api/stripe/webhook/route.ts
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

72 lines
2.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { getStripeClient } from '@/lib/stripe/client'
import Stripe from 'stripe'
// This is your Stripe CLI webhook secret
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET
async function handler(req: NextRequest) {
const body = await req.text()
const signature = req.headers.get('stripe-signature') || ''
const stripe = getStripeClient()
let event: Stripe.Event
try {
event = stripe.webhooks.constructEvent(body, signature, webhookSecret!)
} catch (err) {
console.error('Webhook signature verification failed:', err)
return NextResponse.json(
{ error: 'Invalid signature' },
{ status: 400 }
)
}
// Handle the event
switch (event.type) {
case 'checkout.session.completed':
const checkoutSession = event.data.object as Stripe.Checkout.Session
console.log('Payment received for session:', checkoutSession.id)
console.log('Customer email:', checkoutSession.customer_email)
console.log('Amount:', checkoutSession.amount_total)
console.log('Items:', checkoutSession.display_items)
// TODO: Create order record in database
// TODO: Send confirmation email to customer
// TODO: Update inventory for physical products
break
case 'payment_intent.succeeded':
const paymentIntent = event.data.object as Stripe.PaymentIntent
console.log('Payment succeeded for intent:', paymentIntent.id)
// TODO: Handle any post-payment logic
break
case 'payment_intent.payment_failed':
const failedPayment = event.data.object as Stripe.PaymentIntent
console.log('Payment failed for intent:', failedPayment.id)
console.log('Failure reason:', failedPayment.last_payment_error?.message)
// TODO: Handle failed payment (notify customer, etc.)
break
case 'invoice.payment_succeeded':
const invoice = event.data.object as Stripe.Invoice
console.log('Invoice payment succeeded for invoice:', invoice.id)
// TODO: Handle recurring payment success
break
case 'invoice.payment_failed':
const failedInvoice = event.data.object as Stripe.Invoice
console.log('Invoice payment failed for invoice:', failedInvoice.id)
// TODO: Handle recurring payment failure
break
default:
console.log(`Unhandled event type: ${event.type}`)
}
return NextResponse.json({ received: true })
}
export { handler as POST }