Adapters
Manus
Checkpoint: Complete Impulso Digital website with:
da20cd7
import Stripe from 'stripe';
import { Request, Response } from 'express';
import { ENV } from '../_core/env';
const stripe = new Stripe(ENV.stripeSecretKey);
/**
* Handle Stripe webhook events
* This endpoint receives events from Stripe when payments are processed
*/
export async function handleStripeWebhook(req: Request, res: Response) {
const sig = req.headers['stripe-signature'] as string;
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
req.body,
sig,
ENV.stripeWebhookSecret
);
} catch (err) {
console.error('[Stripe Webhook] Signature verification failed:', err);
return res.status(400).send(`Webhook Error: ${err}`);
}
// Handle test events
if (event.id.startsWith('evt_test_')) {
console.log('[Stripe Webhook] Test event detected, returning verification response');
return res.json({
verified: true,
});
}
try {
switch (event.type) {
case 'checkout.session.completed': {
const session = event.data.object as Stripe.Checkout.Session;
console.log('[Stripe Webhook] Checkout session completed:', session.id);
// Extract metadata
const userId = session.metadata?.user_id;
const productId = session.metadata?.product_id;
const customerEmail = session.customer_email;
console.log('[Stripe Webhook] Order details:', {
sessionId: session.id,
userId,
productId,
customerEmail,
amount: session.amount_total,
currency: session.currency,
});
// TODO: Update database with order information
// - Save order record
// - Update user subscription status if applicable
// - Send confirmation email
// - Trigger fulfillment workflow
break;
}
case 'payment_intent.succeeded': {
const paymentIntent = event.data.object as Stripe.PaymentIntent;
console.log('[Stripe Webhook] Payment intent succeeded:', paymentIntent.id);
// TODO: Handle payment success
// - Update order status
// - Send receipt email
// - Trigger fulfillment
break;
}
case 'payment_intent.payment_failed': {
const paymentIntent = event.data.object as Stripe.PaymentIntent;
console.log('[Stripe Webhook] Payment intent failed:', paymentIntent.id);
// TODO: Handle payment failure
// - Update order status
// - Send failure notification
// - Trigger retry logic
break;
}
case 'customer.subscription.created': {
const subscription = event.data.object as Stripe.Subscription;
console.log('[Stripe Webhook] Subscription created:', subscription.id);
// TODO: Handle subscription creation
// - Save subscription record
// - Grant access to features
// - Send welcome email
break;
}
case 'customer.subscription.updated': {
const subscription = event.data.object as Stripe.Subscription;
console.log('[Stripe Webhook] Subscription updated:', subscription.id);
// TODO: Handle subscription update
// - Update subscription record
// - Adjust feature access if plan changed
break;
}
case 'customer.subscription.deleted': {
const subscription = event.data.object as Stripe.Subscription;
console.log('[Stripe Webhook] Subscription deleted:', subscription.id);
// TODO: Handle subscription cancellation
// - Mark subscription as cancelled
// - Revoke access to features
// - Send cancellation confirmation
break;
}
case 'invoice.paid': {
const invoice = event.data.object as Stripe.Invoice;
console.log('[Stripe Webhook] Invoice paid:', invoice.id);
// TODO: Handle invoice payment
// - Update invoice status
// - Send receipt
// - Trigger renewal logic
break;
}
case 'invoice.payment_failed': {
const invoice = event.data.object as Stripe.Invoice;
console.log('[Stripe Webhook] Invoice payment failed:', invoice.id);
// TODO: Handle invoice payment failure
// - Update invoice status
// - Send retry notification
// - Trigger dunning process
break;
}
default:
console.log('[Stripe Webhook] Unhandled event type:', event.type);
}
// Acknowledge receipt of event
res.json({ received: true });
} catch (error) {
console.error('[Stripe Webhook] Error processing event:', error);
res.status(500).json({ error: 'Webhook processing failed' });
}
}