Overview
The webhook handler is implemented as a Supabase Edge Function that processes payment and subscription events from Dodo Payments. It validates webhook signatures, processes events, and syncs data to the database.Location
Webhook Handler
Setup and Configuration
Main Handler
Event Processing
Event Router
Payment Management
managePayment Function
Inserts or updates payment records in the database.- Upserts payment records (insert or update)
- Stores complete webhook data for audit trail
- Handles all payment statuses (succeeded, failed, processing, cancelled)
Subscription Management
manageSubscription Function
Syncs subscription data from webhooks to database.- Upserts subscription records
- Captures all subscription metadata
- Updates billing dates and status
User Tier Management
updateUserTier Function
Activates paid subscription for user.subscription.active- New subscription activatedsubscription.plan_changed- Plan upgraded/downgraded
downgradeToHobbyPlan Function
Reverts user to free tier.subscription.cancelled- User cancelled subscriptionsubscription.expired- Subscription expiredsubscription.failed- Payment failed
Webhook Events
Payment Events
| Event | Description | Action |
|---|---|---|
payment.succeeded | Payment completed successfully | Store payment record |
payment.failed | Payment failed | Store payment with error |
payment.processing | Payment being processed | Store payment status |
payment.cancelled | Payment cancelled | Store cancellation |
Subscription Events
| Event | Description | Actions |
|---|---|---|
subscription.active | New subscription activated | Store subscription, update user tier |
subscription.plan_changed | Plan upgraded/downgraded | Update subscription, update user tier |
subscription.renewed | Subscription renewed | Update subscription |
subscription.on_hold | Subscription paused | Update subscription status |
subscription.cancelled | Subscription cancelled | Update subscription, downgrade user |
subscription.expired | Subscription expired | Update subscription, downgrade user |
subscription.failed | Subscription payment failed | Update subscription, downgrade user |
Security
Signature Verification
All webhooks are verified using the Standard Webhooks library:webhook-id- Unique webhook IDwebhook-signature- HMAC signaturewebhook-timestamp- Timestamp for replay protection
Environment Variables
Required environment variables:DODO_WEBHOOK_SECRET- Webhook signing secret from Dodo PaymentsSUPABASE_URL- Supabase project URLSUPABASE_SERVICE_ROLE_KEY- Service role key for database access
Error Handling
- 400 - Invalid signature
- 405 - Method not allowed (non-POST)
- 500 - Server error or missing configuration
- 200 - Success or unhandled event type
Logging
All webhook events are logged:Testing Webhooks
Local Testing
- Start Supabase locally:
- Serve the function:
- Send test webhook:
Production Deployment
- Deploy to Supabase:
- Set environment variables:
- Configure webhook URL in Dodo Payments dashboard:
Workflow
Best Practices
- Idempotency - Use upsert operations to handle duplicate webhooks
- Validation - Always verify webhook signatures
- Logging - Log all events for debugging and audit trails
- Error Handling - Catch and log errors, return appropriate status codes
- Atomic Operations - Use database transactions for multi-step updates
- Payload Storage - Store complete webhook data for troubleshooting
