← Back to blog

Integrate MercadoPago into your Laravel application

2026-04-19

Integrate MercadoPago into your Laravel application

In this article, we will explore the process of integrating MercadoPago into your Laravel application. MercadoPago is a popular payment gateway used in Latin America, and integrating it into your e-commerce application can help you expand your customer base and increase revenue.

Prerequisites

To integrate MercadoPago into your Laravel application, you will need the following:

Step 1: Install the MercadoPago SDK

To integrate MercadoPago into your Laravel application, you will need to install the MercadoPago SDK. You can do this by running the following command in your terminal:

composer require mercado-pago/mercadopago-php

Step 2: Configure the MercadoPago API credentials

To use the MercadoPago API, you will need to configure your API credentials. You can do this by creating a new file called `mercadopago.php` in the `config` directory of your Laravel application:

<?php

return [
    'sandbox' => [
        'client_id' => 'YOUR_CLIENT_ID',
        'client_secret' => 'YOUR_CLIENT_SECRET',
    ],
    'production' => [
        'client_id' => 'YOUR_CLIENT_ID',
        'client_secret' => 'YOUR_CLIENT_SECRET',
    ],
];

?>

Step 3: Create a payment method

To create a payment method, you will need to create a new model in your Laravel application. You can do this by running the following command in your terminal:

php artisan make:model PaymentMethod

Then, you can add the following code to the `PaymentMethod` model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PaymentMethod extends Model
{
    protected $fillable = [
        'name',
        'description',
        'type',
    ];
}

?>

Step 4: Create a payment

To create a payment, you will need to create a new controller in your Laravel application. You can do this by running the following command in your terminal:

php artisan make:controller PaymentController

Then, you can add the following code to the `PaymentController` controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PaymentMethod;
use MercadoPago\SDK;

class PaymentController extends Controller
{
    public function createPayment(Request $request)
    {
        $paymentMethod = PaymentMethod::find($request->input('payment_method_id'));

        $payment = new \MercadoPago\Payment();
        $payment->transaction_amount = $request->input('amount');
        $payment->description = $request->input('description');
        $payment->payment_method_id = $paymentMethod->id;
        $payment->payer = [
            'email' => $request->input('email'),
        ];

        $payment->save();

        return response()->json(['id' => $payment->id]);
    }
}

?>

Step 5: Handle payment status updates

To handle payment status updates, you will need to create a new event in your Laravel application. You can do this by running the following command in your terminal:

php artisan make:event PaymentStatusUpdated

Then, you can add the following code to the `PaymentStatusUpdated` event:

<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MercadoPago\SDK;

class PaymentStatusUpdated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $payment;

    public function __construct(\MercadoPago\Payment $payment)
    {
        $this->payment = $payment;
    }

    public function broadcastOn()
    {
        return new Channel('payment-status-updated');
    }

    public function broadcastWith()
    {
        return [
            'payment' => $this->payment,
        ];
    }
}

?>

Step 6: Handle payment status updates in the frontend

To handle payment status updates in the frontend, you will need to create a new JavaScript file. You can do this by running the following command in your terminal:

npm install pusher-js

Then, you can add the following code to the `payment-status-updated.js` file:

import Pusher from 'pusher-js';

const pusher = new Pusher('YOUR_PUSHER_APP_ID', {
    cluster: 'YOUR_PUSHER_APP_CLUSTER',
});

const channel = pusher.subscribe('payment-status-updated');

channel.bind('payment-status-updated', (data) => {
    console.log(data.payment);
});

Conclusion

In this article, we have explored the process of integrating MercadoPago into your Laravel application. We have covered the following steps:

By following these steps, you can integrate MercadoPago into your Laravel application and start accepting payments from your customers.