POST /api/mercado-pago/webhook
Receives and processes webhook notifications from Mercado Pago when payment events occur. This endpoint automatically updates payment status and creates virtual class events when payments are approved.Webhook Events
Mercado Pago sends different types of webhook notifications:Event Processing Flow
- Receive Webhook: Mercado Pago sends POST request with event data
- Parse Event Type: Check if it’s a
merchant_orderevent - Fetch Order Details: Query the resource URL for complete order information
- Check Payment Status: Verify if payment status is
approved - Update Database: Update
PaymentMercadoPagorecord status - Create Virtual Class: Automatically create Google Calendar event for the class
Request Body
Merchant Order Response
When fetching the resource URL, Mercado Pago returns:The preference ID that links to the payment record
Array of payment objects associated with the order
Payment status:
approved, pending, or rejectedExample Merchant Order
Payment Status Values
Payment was successfully processed and accepted
Payment is awaiting confirmation or processing
Payment was declined or failed
Automated Actions
When a payment is approved, the webhook automatically:- Updates Payment Status: Changes
PaymentMercadoPago.statustoapproved - Creates Virtual Class: Calls
/api/calendarendpoint to create Google Meet event - Links Preference: Associates the virtual class with the payment preference ID
Example Webhook Flow
Response
Indicates whether the webhook was processed successfully
HTTP status code (200 for success, 500 for error)
Success Response
Error Handling
The webhook gracefully handles various scenarios:- Missing resource URL: Returns
ok: trueto acknowledge receipt - No payments in order: Returns
ok: trueand logs warning - Non-approved payments: Acknowledges but doesn’t process
- Database errors: Returns
ok: falsewith 500 status
Security Considerations
Unlike Stripe, Mercado Pago does not sign webhook requests. Ensure your webhook URL is kept secure and validate payment status by fetching data directly from Mercado Pago’s API using the resource URL.
Configuration
Set up your webhook URL in the Mercado Pago dashboard:- Go to your Mercado Pago Dashboard
- Navigate to Webhooks section
- Add your webhook URL:
https://your-domain.com/api/mercado-pago/webhook - Select event type:
merchant_order
Testing Webhooks
For local development, use a tunnel service like ngrok:Retry Logic
Mercado Pago automatically retries webhook deliveries if:- Your server returns a non-2xx status code
- The request times out
- Connection fails
Best Practices
- Idempotency: Ensure your webhook handler can process the same event multiple times safely
- Quick Response: Return 200 status quickly, process heavy operations asynchronously
- Logging: Log all webhook events for debugging and audit trails
- Validation: Always fetch fresh data from Mercado Pago’s API using the resource URL