Endpoint
Authentication
Stripe signs all webhook events with your webhook secret. The API verifies the signature using:Request Headers
Stripe webhook signature for request verification
Must be
application/jsonRequest Body
The request body is the raw Stripe event object. The API expects the raw body buffer, not parsed JSON.This endpoint uses
express.raw({ type: 'application/json' }) middleware to preserve the raw body for signature verification.Supported Events
payment_intent.succeeded
Triggered when a payment is successfully completed. Actions:- Updates order status to
paid - Sends order confirmation email to shop owner
order_id- Used to identify and update the orderuser_id- Customer user ID
payment_intent.payment_failed
Triggered when a payment attempt fails. Actions:- Updates order status to
failed
payment_intent.canceled
Triggered when a payment intent is canceled. Actions:- Updates order status to
canceled
Response
Always returns
true to acknowledge receipt of the webhookError Codes
| Status Code | Error Message | Description |
|---|---|---|
400 | Webhook Error: {message} | Signature verification failed or invalid payload |
500 | Internal webhook error | Error processing the webhook event |
Event Processing Flow
- Signature Verification: Validates the Stripe signature
- Event Type Check: Determines the event type
- Order Lookup: Extracts
order_idfrom payment intent metadata - Status Update: Updates the order status in Supabase
- Email Notification: Sends confirmation email (for succeeded events)
- Acknowledgment: Returns success response to Stripe
Order Confirmation Email
When a payment succeeds, an automated email is sent tosabbelshandmade@gmail.com containing:
- Order ID and date
- Customer name and email
- List of ordered items with sizes, colors, and quantities
- Total amount
- Payment status
- Responsive HTML design
- Sabbels Handmade branding
- Detailed product breakdown
- Customer contact information for follow-up
Example Webhook Event
Setting Up Webhooks in Stripe
1. Create Webhook Endpoint
In your Stripe Dashboard:- Go to Developers → Webhooks
- Click Add endpoint
- Enter your endpoint URL:
https://your-api-domain.com/api/stripe/webhook - Select events to listen to:
payment_intent.succeededpayment_intent.payment_failedpayment_intent.canceled
2. Get Webhook Secret
After creating the endpoint, Stripe will provide a webhook signing secret (starts withwhsec_). Add this to your environment variables:
3. Test Webhooks
Use the Stripe CLI to test webhooks locally:Error Handling
The webhook endpoint implements comprehensive error handling:Signature Verification Errors
STRIPE_WEBHOOK_SECRET is correctly configured.
Order Update Errors
If the order update fails, the error is logged but the webhook still returns success to Stripe:Email Sending Errors
Email errors are logged but don’t affect the webhook response:RESEND_API_KEY is configured correctly.
Logging
The webhook endpoint provides detailed logging:Security Considerations
Webhook Retry Logic
Stripe automatically retries failed webhook deliveries:- Retries for up to 3 days
- Uses exponential backoff
- Stops retrying after receiving a 2xx response
Testing Webhooks
Using Stripe CLI
Manual Testing
- Create a test payment in Stripe Dashboard
- Add metadata:
order_idanduser_id - Complete or fail the payment
- Verify order status updates in Supabase
- Check email delivery (if configured)
Notes
- Webhooks may arrive out of order or multiple times (idempotency is important)
- The endpoint processes events synchronously for simplicity
- Email sending errors don’t affect order status updates
- Unhandled event types are logged and ignored
- Raw body parsing is required for signature verification