This library provides developers with a simple set of bindings to help you integrate Mercado Pago API to a website and start receiving payments.
The SDK Supports PHP version 8.2 or higher.
If you already use another version of MercadoPago PHP SDK, take a look at our migration guide from version 2 to version 3.
First time using Mercado Pago? Create your Mercado Pago account, if you donβt have one already.
-
Download Composer if not already installed.
-
Install PHP SDK for MercadoPago running in command line:
composer require "mercadopago/dx-php:3.0.0"
You can also run composer require "mercadopago/dx-php:2.6.1" for PHP7.1 or composer require "mercadopago/dx-php:1.12.5" for PHP5.6.
- Copy the access_token in the credentials section of the page and replace YOUR_ACCESS_TOKEN with it.
That's it! Mercado Pago SDK has been successfully installed.
Simple usage looks like:
<?php
// Step 1: Require the library from your Composer vendor folder
require_once 'vendor/autoload.php';
use MercadoPago\Client\Payment\PaymentClient;
use MercadoPago\Exceptions\MPApiException;
use MercadoPago\MercadoPagoConfig;
// Step 2: Set production or sandbox access token
MercadoPagoConfig::setAccessToken("<ACCESS_TOKEN>");
// Step 3: Initialize the API client
$client = new PaymentClient();
try {
// Step 4: Create the request array
$request = [
"transaction_amount" => 100,
"token" => "YOUR_CARD_TOKEN",
"description" => "description",
"installments" => 1,
"payment_method_id" => "visa",
"payer" => [
"email" => "user@test.com",
]
];
// Step 5: Make the request
$payment = $client->create($request);
echo $payment->id;
// Step 6: Handle exceptions
} catch (MPApiException $e) {
echo "Status code: " . $e->getApiResponse()->getStatusCode() . "\n";
echo "Content: " . $e->getApiResponse()->getContent() . "\n";
} catch (\Exception $e) {
echo $e->getMessage();
}
require_once 'vendor/autoload.php';
use MercadoPago\Client\Payment\PaymentClient;
use MercadoPago\Exceptions\MPApiException;
use MercadoPago\MercadoPagoConfig;
MercadoPagoConfig::setAccessToken("<ACCESS_TOKEN>");
You can also set another properties as quantity of retries, tracking headers, timeouts and a custom http client.
$client = new PaymentClient();
$request = [
"transaction_amount" => 100,
"token" => "YOUR_CARD_TOKEN",
"description" => "description",
"installments" => 1,
"payment_method_id" => "visa",
"payer" => [
"email" => "user@test.com",
]
];
$payment = $client->create($request);
...
// Handle API exceptions
} catch (MPApiException $e) {
echo "Status code: " . $e->getApiResponse()->getStatusCode() . "\n";
echo "Content: " . $e->getApiResponse()->getContent() . "\n";
// Handle all other exceptions
} catch (\Exception $e) {
echo $e->getMessage();
}
See our documentation for more details.
- Mercado Pago reference API. Portuguese / English / Spanish
All contributions are welcome, ranging from people wanting to triage issues, others wanting to write documentation, to people wanting to contribute code.
Please read and follow our contribution guidelines. Contributions not following these guidelines will be disregarded. The guidelines are in place to make all of our lives easier and make contribution a consistent process for everyone.
Since the release of version 3.0.0, version 2 is deprecated and will not be receiving new features, only bug fixes. If you need to submit PRs for that version, please do so by using master-v2 as your base branch.
If you require technical support, please contact our support team at our developers site: English / Portuguese / Spanish
MIT license. Copyright (c) 2023 - Mercado Pago / Mercado Libre
For more information, see the LICENSE file.