Endpoint
Overview
Themercadopago-payment edge function handles two operations:
- Create Payment Preference - Generate Mercado Pago checkout link
- Webhook Handler - Process payment notifications and credit user accounts
This function is used exclusively for the B2C model (direct user purchases).
Create Payment Preference
Request
Headers
Bearer token with Supabase anon key
Must be
application/jsonBody Parameters
User ID purchasing credits
Number of credits to purchaseExample:
500Price in ARS (Argentine Pesos)Example:
2500.00Display name for the credit packExample:
"Pack Premium"URL to redirect after payment completion
Response
Success
Mercado Pago preference ID
Production checkout URL - redirect user here
Test checkout URL (for sandbox testing)
Error
Code Example
Webhook Handler
Webhook URL
Configure this URL in Mercado Pago dashboard:Request (from Mercado Pago)
Mercado Pago sends POST notifications when payment status changes.Body Structure
Notification typeValues:
payment, merchant_order, payPayment ID to fetch details
Response
Always return
200 OK to acknowledge webhook receipt, even if processing fails.Implementation Details
Creating Preference
The function builds a Mercado Pago preference object:supabase/functions/mercadopago-payment/index.ts:35-59
External Reference Format
Theexternal_reference field stores metadata:
Webhook Processing
When a payment is approved:supabase/functions/mercadopago-payment/index.ts:95-128
Idempotency
Webhooks may be sent multiple times. The function checkspayment_notifications table to prevent duplicate credits:
Mercado Pago Setup
1. Get Credentials
- Go to Mercado Pago Developers
- Navigate to Your integrations → Credentials
- Copy Access Token (Production or Test)
2. Configure Webhook
- Go to Your integrations → Webhooks
- Click Add webhook
- Set URL:
https://<project-ref>.supabase.co/functions/v1/mercadopago-payment?webhook=true - Select events:
payment.created,payment.updated - Save
3. Set Edge Function Secret
Testing
Test Mode
Use Mercado Pago test credentials:- In Mercado Pago dashboard, switch to Test mode
- Use test access token in
MP_ACCESS_TOKEN - Use test cards from Mercado Pago docs
Local Testing
Webhook Testing
Use ngrok to expose local function:Environment Variables
Auto-injected by Supabase
Auto-injected by Supabase
Mercado Pago access tokenTest:
Production:
TEST-1234...Production:
APP_USR-1234...Set via: supabase secrets set MP_ACCESS_TOKEN=your-tokenError Handling
MP_ACCESS_TOKEN is invalid or expiredSolution: Regenerate token in Mercado Pago dashboard
Price or credits value is invalidSolution: Ensure price is positive number, credits is integer
Access token doesn’t have required permissionsSolution: Use access token from “Credentials” page, not client ID
Security Considerations
Validate webhook origin
Optionally verify webhook IP is from Mercado Pago
Idempotency is critical
Always check
payment_notifications before creditingNever trust external_reference
Validate user_id exists before crediting
Log all webhooks
Store full payload for debugging and auditing
Next Steps
Mercado Pago Integration
Complete integration guide
Edge Functions
General edge function docs