This project demonstrates how to integrate MercadoPago payments with webhooks for automatic notifications.
- User accesses
buytest.html
and clicks "Buy Now" - Frontend (
checkout.js
) makes POST request to/criar-pagamento
- Backend creates preference in MercadoPago and returns checkout URL
- User is redirected to MercadoPago checkout
- After payment, user returns to success/failure page
- MercadoPago sends webhook to
/webhook
when payment status changes - Backend processes the notification and verifies payment status
- Detailed logs are displayed in server console
- Automatic actions can be executed (add to group, send email, etc.)
- MercadoPago account with production or sandbox credentials
- Node.js installed
- ngrok or similar to expose localhost (for webhooks)
Create a .env
file in the project root:
MP_ACCESS_TOKEN=TEST-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
npm install
ngrok http 3000
Copy the ngrok URL and update it in Server.js
:
notification_url: 'https://your-ngrok-url.ngrok-free.app/webhook'
npm run dev
- Open:
http://localhost:3000
- Click "Buy Now"
- Complete payment on MercadoPago
node test-webhook.js
├── Server.js # Main server
├── public/
│ ├── buytest.html # Purchase page
│ ├── checkout.js # Frontend JavaScript
│ ├── pagamentoconfirmado.html # Success page
│ ├── pagamentofalhou.html # Failure page
│ └── pagamentopendente.html # Pending page
├── test-webhook.js # Test script
└── README.md # This file
- Function: Creates payment preference in MercadoPago
- Returns: Checkout URL
- Function: Receives notifications from MercadoPago
- Processes: Payment status automatically
- Function: Approved payment page
- Function: Failed payment page
- Function: Pending payment page
- Function: Adding user to a group after payment-success
The webhook processes the following statuses:
approved
: ✅ Payment approvedpending
: ⏳ Payment pendingrejected
: ❌ Payment rejectedcancelled
: 🚫 Payment cancelled
The server displays detailed logs in the console:
📩 Webhook received: { ... }
📋 Notification type: payment
🆔 Payment ID: 123456789
💰 Payment status: approved
💵 Amount: 50.00
👤 Payer: [email protected]
✅ Payment approved - Processing...
In Server.js
, change the product data:
const preference = {
items: [
{
title: 'Your Product',
quantity: 1,
unit_price: 99.99,
},
],
// ...
};
node test-webhook.js
- Make a real purchase
- Check the console logs
- Confirm if the webhook was received
- ✅ Webhook validation implemented
- ✅ Status verification via MercadoPago API
- ✅ Detailed logs for auditing
⚠️ Configure HTTPS in production⚠️ Validate webhook signature in production
For questions about the integration:
🎉 Now you have a complete payment system with automatic notifications!