GitHub Repository
Complete source code and setup guide
Quick Setup
1. Prerequisites
- Netlify account
- Neon database account
- DodoPayments API key from dashboard
2. Install Dependencies
3. Database Setup
- Sign up for Neon
- Create a new project
- Open the SQL Editor
- Copy and paste the contents of
schema.sql - Run the query
- Get your connection string from Neon → Connection Details
4. Set Initial Environment Variables
Note: We’ll set DODO_PAYMENTS_WEBHOOK_KEY after deployment once you have your webhook URL.
5. Initialize & Deploy
6. Get Your Webhook URL
Your webhook URL is:7. Register Webhook in DodoPayments Dashboard
- Go to DodoPayments Dashboard → Developer → Webhooks
- Create a new webhook endpoint
- Configure your webhook URL as the endpoint
- Enable these subscription events:
subscription.activesubscription.cancelledsubscription.renewed
- Copy the Signing Secret
8. Set Webhook Key & Redeploy
What It Does
Processes subscription events and stores them in PostgreSQL:- subscription.active - Creates/updates customer and subscription records
- subscription.cancelled - Marks subscription as cancelled
- subscription.renewed - Updates next billing date
Key Features
✅ Signature verification - Using the dodopayments library✅ Idempotency - Prevents duplicate processing with webhook IDs
✅ Event logging - Complete audit trail in
webhook_events table✅ Error handling - Logged and retryable
Note: This implementation demonstrates handling three core subscription events (subscription.active,subscription.cancelled,subscription.renewed) with minimal fields. You can easily extend it to support additional event types and fields based on your requirements.
Configuration Files
Database Schema
- customers - Email, name, dodo_customer_id
- subscriptions - Status, amount, next_billing_date, linked to customers
- webhook_events - Event log with webhook_id for idempotency
Implementation Code
How It Works
The webhook handler:- Verifies the signature - Ensures the request is from DodoPayments using HMAC-SHA256
- Checks for duplicates - Uses webhook ID to prevent processing the same event twice
- Logs the event - Stores raw webhook in
webhook_eventstable for audit trail - Processes the event - Creates or updates customers and subscriptions in Neon
- Handles errors - Logs failures and marks event as unprocessed for retry
Testing
Local development:- Select your site → Functions tab
- Click on the
webhookfunction - View real-time logs and invocation history
- Go to Developers → Webhooks
- Add endpoint with your Netlify Functions URL
- Enable: subscription.active, subscription.cancelled, subscription.renewed