Overview
Mercado Pago integration handles:- Payment preference creation
- Checkout redirect flow
- Webhook notifications for payment status
- Automatic class creation upon payment approval
- Payment record management in database
Environment Configuration
Required Environment Variables
Your Mercado Pago private access token. Obtain from:
- Log into Mercado Pago Developer Dashboard
- Navigate to “Your integrations” > “Credentials”
- Copy the “Access Token” (Production or Test mode)
Your Mercado Pago public key for client-side SDK initialization.
Found in the same credentials section as the access token.
Your application’s base URL including protocol and trailing slash:
- Development:
http://localhost:3000/ - Production:
https://yourdomain.com/
src/app/api/mercado-pago/create-preference/route.ts:24
Test vs Production Credentials
Mercado Pago provides separate credentials for testing:- Test Mode: Use test credentials for development
- Production Mode: Use production credentials for live payments
Payment Data Model
Payments are tracked using thePaymentMercadoPago schema:
PaymentMercadoPago Schema
MongoDB ObjectId - unique payment record identifier
Reference to the user making the payment
Mercado Pago preference ID (unique)
Payment amount in local currency (ARS)
Class type:
individual or grupalNumber of students for the class
Payment status:
approved, pending, or rejectedprisma/schema.prisma:182-193
Payment Status Types
Payment successfully processed
Payment initiated but not yet confirmed
Payment failed or was declined
prisma/schema.prisma:22-26
Payment Flow
The complete payment flow follows these steps:Step 1: User Initiates Booking
User selects class type, date, and time on the booking page.Step 2: Create Payment Preference
API Route:src/app/api/mercado-pago/create-preference/route.ts
Step 3: Redirect to Mercado Pago Checkout
User is redirected toresult.init_point where they complete payment.
Step 4: Payment Processing
Mercado Pago processes the payment and sends webhook notifications.Step 5: Webhook Handling
Webhook receives payment confirmation and creates the virtual class.Webhook Configuration
Webhook URL Setup
-
Configure in Mercado Pago Dashboard:
- Go to “Your integrations” > “Webhooks”
- Add webhook URL:
https://yourdomain.com/api/mercado-pago/webhook - Select events: “Payments” and “Merchant Orders”
-
Webhook Events:
payment- Individual payment notifications (logged but ignored)merchant_order- Order completion (triggers class creation)
Webhook Implementation
API Route:src/app/api/mercado-pago/webhook/route.ts
Mercado Pago sends webhook notifications multiple times. Your endpoint must be idempotent to handle duplicate requests safely.
Webhook Security
Unlike Stripe, Mercado Pago doesn’t sign webhook bodies. Security measures:- Verify Resource URL: Always fetch order details from Mercado Pago’s API
- Use HTTPS: Ensure webhook URL uses SSL
- Validate Data: Check payment status from official API response
- Idempotency: Handle duplicate webhook calls gracefully
Creating Virtual Class After Payment
When payment is approved, the system:1. Update Payment Record
Function:src/services/functions/index.ts:384-398
2. Find Virtual Class Record
The class was pre-created during booking withstatus: 'pending':
Function: src/services/functions/index.ts:401-419
3. Create Google Calendar Event
See Calendar Setup for details on this step.4. Update Virtual Class
Final update includes Google Meet link and access code: Function:src/services/functions/index.ts:185-246
Updates:
status→"scheduled"googleEventId→ Calendar event IDhtmlLink→ Google Meet linkaccessCode→ Random 8-character codesummary→ Class description
Payment Configuration Options
Payment Methods
Configure accepted payment methods:src/app/api/mercado-pago/create-preference/route.ts:39-43
Callback URLs
Users are redirected after payment:Redirect URL for successful payments:
${BASE_URL}/checkout/callback/successRedirect URL for failed payments:
${BASE_URL}/checkout/callback/failureRedirect URL for pending payments:
${BASE_URL}/checkout/callback/pendingAuto Return
Client-Side Integration
The checkout is initiated client-side using Mercado Pago SDK: Implementation:src/services/api/clients.ts
Testing Payments
Test Cards
Mercado Pago provides test cards for different scenarios: Approved Payment:- Card:
5031 7557 3453 0604 - Expiry: Any future date
- CVV: Any 3 digits
- Name: Any name
- Card:
5031 4332 1540 6351
- Card:
5031 4332 1540 6351
Webhook Testing
For local development, use a tunnel service to expose your webhook:-
Using ngrok:
-
Using srv.us:
-
Update webhook URL in Mercado Pago:
Troubleshooting
Common Issues
“Webhook not receiving notifications”- Verify
BASE_URLends with/ - Check webhook URL in Mercado Pago dashboard
- Ensure webhook endpoint is publicly accessible
- Check firewall/security group settings
- Check webhook logs for errors
- Verify
preferenceIdmatches between payment and class records - Check Google Calendar API is working
- Verify admin refresh token is stored
- Verify
MERCADO_PAGO_ACCESS_TOKENis correct - Check you’re using the right mode (test/production)
- Token may have been regenerated in dashboard
- Verify all required fields in request body
- Check currency_id is supported (ARS for Argentina)
- Ensure price is a valid number
- Verify user is authenticated
Security Best Practices
Related Documentation
Mercado Pago Documentation
Official Mercado Pago developer documentation
Calendar Setup
Configure Google Calendar integration for classes
Webhook Guide
Understanding webhooks and event-driven architecture
Class Management
Managing virtual classes after payment