Files
agency/api/stripe-webhook.ts
T
2025-07-16 16:21:05 -05:00

65 lines
2.0 KiB
TypeScript

import { VercelRequest, VercelResponse } from '@vercel/node';
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || 'PURGED', {
apiVersion: '2023-10-16',
});
const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET || 'PURGED';
export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const sig = req.headers['stripe-signature'] as string;
let event: Stripe.Event;
try {
// Verify webhook signature
const rawBody = JSON.stringify(req.body);
event = stripe.webhooks.constructEvent(rawBody, sig, endpointSecret);
} catch (err: any) {
console.error('Webhook signature verification failed:', err.message);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the event
switch (event.type) {
case 'checkout.session.completed':
const session = event.data.object as Stripe.Checkout.Session;
// Process the successful payment
console.log('Payment successful:', {
sessionId: session.id,
customerId: session.customer,
amount: session.amount_total,
metadata: session.metadata
});
// Here you would typically:
// 1. Update user credits in your database
// 2. Send confirmation email
// 3. Log the transaction
// For now, we'll just log it
break;
case 'payment_intent.succeeded':
const paymentIntent = event.data.object as Stripe.PaymentIntent;
console.log('Payment intent succeeded:', paymentIntent.id);
break;
case 'payment_intent.payment_failed':
const failedPayment = event.data.object as Stripe.PaymentIntent;
console.log('Payment failed:', failedPayment.id);
break;
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a 200 response to acknowledge receipt of the event
res.status(200).json({ received: true });
}