Skip to main content

Overview

PC Fix integrates with MercadoPago to provide a seamless checkout experience for customers. The integration handles preference creation, payment callbacks, and webhook notifications for automatic payment status updates.

Prerequisites

Before setting up MercadoPago, you’ll need:
  • A MercadoPago account (production or sandbox)
  • Access token from your MercadoPago dashboard
  • Backend URL configured for callbacks and webhooks

Environment Variables

Add the following environment variables to your .env file:
For testing, you can use MercadoPago’s test credentials. Production credentials are required for live transactions.

Configuration

1

Install Dependencies

The MercadoPago SDK is already included in the project:
2

Configure Service

The MercadoPagoService is located at /packages/api/src/shared/services/MercadoPagoService.ts:
3

Set Up Webhooks

Configure your MercadoPago webhook URL in the dashboard:
Webhooks are only configured for production environments (HTTPS). Local development uses callback URLs only.

Creating Payment Preferences

When a customer selects MercadoPago as their payment method, the system creates a payment preference:

Usage in Sales Controller

The preference is created through the sales controller at /packages/api/src/modules/sales/sales.controller.ts:

Payment Flow

1

Customer Initiates Payment

Customer selects MercadoPago and proceeds to checkout. The system creates a sale record with PENDIENTE_PAGO status.
2

Preference Creation

Frontend calls POST /api/sales/:id/mp-preference to generate the MercadoPago checkout URL.
3

Customer Completes Payment

Customer is redirected to MercadoPago’s checkout page and completes the payment.
4

Callback Handling

After payment, MercadoPago redirects to the callback URL:
5

Webhook Confirmation

MercadoPago sends a webhook notification for final confirmation:

Webhook Processing

The webhook handler fetches payment details and updates the sale status:

Price Calculation

Products in PC Fix have different pricing for MercadoPago vs. other payment methods. When not using MercadoPago, a 8% discount is applied (multiplied by 0.92).

API Endpoints

Testing

Test User Creation

PC Fix includes a script to create test users in MercadoPago sandbox:

Test Cards

Use these test cards in sandbox mode:

Troubleshooting

If payments are not being updated automatically, verify:
  • MP_ACCESS_TOKEN is correctly set
  • Webhook URL is configured in MercadoPago dashboard
  • Your backend is accessible via HTTPS (webhooks require HTTPS)
  • The external_reference matches your sale ID

Common Issues

Preference creation fails:
  • Check that MP_ACCESS_TOKEN is valid
  • Verify item prices are positive numbers
  • Ensure currency_id is set to ‘ARS’
Webhooks not received:
  • Webhooks only work in production (HTTPS)
  • Check MercadoPago dashboard for delivery errors
  • Verify your endpoint returns 200 OK
Payment approved but sale not updated:
  • Check that external_reference is being passed correctly
  • Verify database connection is stable
  • Review application logs for errors

Additional Resources