Overview
The payment webhook endpoint receives notifications from MercadoPago when payment events occur. It automatically activates user subscriptions, creates audit logs, and handles idempotent processing to prevent duplicate activations.This endpoint is implemented as a Cloudflare Worker for high reliability and low latency processing of payment notifications.
Endpoint
Authentication
The webhook is called directly by MercadoPago. No authentication headers are required from the client, but the worker:- Validates the payment ID with MercadoPago API
- Uses MercadoPago’s API to fetch payment details
- Implements idempotency checks to prevent duplicate processing
Webhook Payload
MercadoPago sends notifications in the following format:Notification type. Must be
payment for payment events.Event action. Typically
payment.created or payment.updated.Payment data object
Example Webhook Payload
Processing Flow
The webhook processes payments through the following steps:1. Receive & Log
Every webhook notification is immediately logged to thewebhook_logs table with status received.
2. Validate Event Type
Onlypayment type notifications with actions payment.created or payment.updated are processed. Other notifications are logged and ignored.
3. Idempotency Check
The webhook checks if the samepayment_id + action combination was already successfully processed. If yes, it returns success without creating duplicate subscriptions.
4. Fetch Payment Details
The worker fetches full payment details from MercadoPago API:5. Validate Payment Status
Only payments with statusapproved proceed to subscription activation. Other statuses are logged for monitoring.
6. User Lookup
The webhook attempts to find the user through multiple methods:7. Plan Detection
The plan type is determined from the payment amount:| Amount (COP) | Plan Type | Duration |
|---|---|---|
| 49,900 | PLAN_BASICO | 40 days |
| 89,900 | PLAN_PRO | 40 days |
| 149,900 | PLAN_PREMIUM | 40 days |
metadata.plan_type or defaults to PLAN_BASICO.
8. Subscription Creation
The webhook creates a new subscription in Supabase:9. Profile Update
Updates the user’s profile with active subscription information:10. Audit Log
Creates an entry insubscription_audit_log for compliance and debugging:
Response
Success Response
Always
true when webhook is receivedtrue if subscription was activated, false if ignoredSubscription activation details
UUID of the webhook log entry for debugging
Example Success Response
Ignored Response
Returned when the webhook is received but not processed:Error Handling
The webhook implements intelligent error handling with retry logic:Error Types
CONFIG_ERROR - Configuration Error
CONFIG_ERROR - Configuration Error
Should Retry: NoStatus Code: 500Cause: Missing environment variables (MP_ACCESS_TOKEN, SUPABASE_URL, SUPABASE_SERVICE_KEY)Action: Fix server configuration and redeploy
AUTH_ERROR - Authentication Error
AUTH_ERROR - Authentication Error
Should Retry: NoStatus Code: 200 (to prevent MercadoPago retries)Cause: Invalid or expired MercadoPago access tokenAction: Update MP_ACCESS_TOKEN in environment variables
NOT_FOUND - Payment Not Found
NOT_FOUND - Payment Not Found
Should Retry: NoStatus Code: 200 (to prevent MercadoPago retries)Cause: Payment ID doesn’t exist in MercadoPagoAction: Investigate if payment was deleted or notification is invalid
VALIDATION_ERROR - Data Validation Error
VALIDATION_ERROR - Data Validation Error
Should Retry: NoStatus Code: 200Cause: User not found, invalid payment dataAction: Check that user exists before payment or fix data mapping
NETWORK_ERROR - Network Error
NETWORK_ERROR - Network Error
Should Retry: YesStatus Code: 500Cause: DNS failure, timeout, connection refusedAction: MercadoPago will automatically retry
RATE_LIMIT - Rate Limit Exceeded
RATE_LIMIT - Rate Limit Exceeded
Should Retry: YesStatus Code: 500Cause: Too many requests to MercadoPago APIAction: Wait and retry, MercadoPago will handle retry logic
SERVER_ERROR - Server Error
SERVER_ERROR - Server Error
Should Retry: YesStatus Code: 500Cause: MercadoPago API or Supabase temporary errorAction: MercadoPago will automatically retry
Retry Strategy
MercadoPago automatically retries webhook deliveries for 48 hours when it receives a 5xx response or connection failure. The webhook returns 200 for permanent errors (AUTH, NOT_FOUND, VALIDATION) to prevent unnecessary retries.
Webhook Logs
All webhook notifications are logged to thewebhook_logs table:
| Column | Description |
|---|---|
id | UUID primary key |
status | received, processing, success, failed, ignored |
webhook_type | Event type from MercadoPago |
webhook_action | Action type (payment.created, payment.updated) |
payment_id | MercadoPago payment ID |
payment_status | Payment status (approved, pending, rejected) |
payment_amount | Payment amount |
user_email | Payer’s email |
user_id | JCV Fitness user UUID |
subscription_id | Created subscription UUID |
plan_type | Activated plan type |
is_duplicate | Boolean flag for duplicate webhooks |
error_message | Error message if failed |
processing_time_ms | Processing duration in milliseconds |
supabase_operations | JSON array of database operations |
raw_payload | Full webhook payload |
received_at | Webhook received timestamp |
processed_at | Processing completed timestamp |
Health Check
The webhook endpoint supports health checks:Webhook Flow Diagram
Configuring the Webhook in MercadoPago
Access MercadoPago Dashboard
Go to MercadoPago Developers
Testing
Manual Testing
You can manually trigger the webhook using curl:Monitoring Webhooks
Query the webhook logs to monitor webhook processing:Common Issues
User not found error
User not found error
Cause: User doesn’t exist in database when payment is madeSolution: Ensure users are created before initiating payment, or implement auto-registration based on payer email
Duplicate subscriptions
Duplicate subscriptions
Cause: Multiple webhooks for the same paymentSolution: The webhook has built-in idempotency - check
is_duplicate flag in logsWrong plan type
Wrong plan type
Cause: Payment amount doesn’t match PLAN_CONFIGSolution: Update PLAN_CONFIG in worker or ensure correct amount is sent
Security Considerations
IP Whitelisting: Consider adding Cloudflare Workers firewall rules to only accept webhooks from MercadoPago’s IP ranges.
Related Endpoints
Create Payment Preference
Create MercadoPago payment preferences