Payment authorization and capture
Add specific features to your integration based on your business needs.
Offer the possibility of granting an authorization before payment capture. This allows you to make a fund reserve in your buyer's card without making a payment.
For example, to grant an authorization when you reserve a car, or for an estimated purchase price prior to confirmation.
Reserve Funds
For fund reserve authorization, you just need to add the capture=false
attribute like this:
<?php
MercadoPago\SDK::setAccessToken("ENV_ACCESS_TOKEN");
$payment = new MercadoPago\Payment();
$payment->transaction_amount = 100;
$payment->token = "ff8080814c11e237014c1ff593b57b4d";
$payment->description = "Title of what you are paying for";
$payment->installments = 1;
$payment->payment_method_id = "visa";
$payment->payer = array(
"email" => "test_user_19653727@testuser.com"
);
$payment->capture=false;
$payment->save();
?>
import com.mercadopago.*;
MercadoPago.SDK.configure("ENV_ACCESS_TOKEN");
Payment payment = new Payment();
payment.setTransactionAmount(100f)
.setToken('ff8080814c11e237014c1ff593b57b4d')
.setDescription('Title of what you are paying for')
.setInstallments(1)
.setPaymentMethodId("visa")
.setPayer(new Payer("test_user_19653727@testuser.com"))
.setCapture(false);
payment.save();
var mercadopago = require('mercadopago');
mercadopago.configurations.setAccessToken(config.access_token);
var payment_data = {
transaction_amount: 100,
token: 'ff8080814c11e237014c1ff593b57b4d'
description: 'Title of what you are paying for',
installments: 1,
payment_method_id: 'visa',
payer: {
email: 'test_user_3931694@testuser.com'
},
capture: false
};
mercadopago.payment.create(payment_data).then(function (data) {
}).catch(function (error) {
});
require 'mercadopago'
MercadoPago::SDK.configure(ACCESS_TOKEN: ENV_ACCESS_TOKEN)
payment = MercadoPago::Payment.new()
payment.transaction_amount = 100
payment.token = 'ff8080814c11e237014c1ff593b57b4d'
payment.description = 'Title of what you are paying for'
payment.installments = 1
payment.payment_method_id = "visa"
payment.payer = {
email: "test_user_19653727@testuser.com"
}
payment.capture = false
payment.save()
The response indicates that the payment is authorized and pending to capture.
json
{
"id": PAYMENT_ID,
...
"status": "authorized",
"status_detail": "pending_capture",
...
"captured": false,
...
}
It can be rejected or remain pending. Take into account that authorized funds cannot be used by your customer until captured. You need to make the capture as soon as possible.
Capture an authorized payment
To complete the payment, you need to capture the funds reserved for your customer. You can capture the amount, entirely or partially.
Capture the entire amount
To capture the full amount, you just need to submit the capture
attribute as true
.
<?php
MercadoPago\SDK::setAccessToken("ENV_ACCESS_TOKEN");
$payment = MercadoPago\Payment::find_by_id($payment_id);
$payment->capture = true;
$payment->update();
?>
import com.mercadopago.*;
MercadoPago.SDK.configure("ENV_ACCESS_TOKEN");
Payment payment = Payment.load(paymentId);
payment.capture = true;
payment.update();
var mercadopago = require('mercadopago');
mercadopago.configurations.setAccessToken(config.access_token);
let paymentId = 123;
mercadopago.payment.capture(paymentId, mercadopago, (error, response) => {
if (error){
console.log(error);
}else{
console.log(response)
}
});
require 'mercadopago'
MercadoPago::SDK.configure(ACCESS_TOKEN: ENV_ACCESS_TOKEN)
payment = MercadoPago::Payment.load(paymentId)
payment.capture=true
payment.update()
curl -X PUT \
'https://api.mercadopago.com/v1/payments/PAYMENT_ID' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ENV_ACCESS_TOKEN' \
-d '{"capture": true}'
The answer will return that the payment is approved and accredited.
json
{
...
"status": "approved",
"status_detail": "accredited",
...
"captured": true,
...
}
Capture payments for less than the reserved amount
To capture an amount lower than the reserve, you need to submit the transaction_amount
attribute with the new value.
<?php
MercadoPago\SDK::setAccessToken("ENV_ACCESS_TOKEN");
$payment = MercadoPago\Payment::find_by_id($payment_id);
$payment->transaction_amount = 75;
$payment->capture = true;
$payment->update();
?>
import com.mercadopago.*;
MercadoPago.SDK.configure("ENV_ACCESS_TOKEN");
Payment payment = Payment.load(paymentId);
payment.transaction_amount = 75;
payment.capture = true;
payment.update();
var mercadopago = require('mercadopago');
mercadopago.configurations.setAccessToken(config.access_token);
let captureInfo = {id: 123, transaction_amount: 5}
mercadopago.payment.capturePartial(captureInfo, mercadopago, (error, response) => {
if (error){
console.log(error);
}else{
console.log(response)
}
});
require 'mercadopago'
MercadoPago::SDK.configure(ACCESS_TOKEN: ENV_ACCESS_TOKEN)
payment = MercadoPago::Payment.load(paymentId)
payment.transaction_amount = 75
payment.capture=true
payment.update()
curl -X PUT \
'https://api.mercadopago.com/v1/payments/PAYMENT_ID' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ENV_ACCESS_TOKEN' \
-d '{
"transaction_amount": 75,
"capture": true
}'
Response
json
{
...
"status": "approved",
"status_detail": "accredited",
...
"transaction_amount": 75,
...
"captured": true,
...
}
Cancel a reserve
If you update payment status
to cancelled
, you can cancel a reserve and release the card money.
<?php
MercadoPago\SDK::setAccessToken("ENV_ACCESS_TOKEN");
$payment = MercadoPago\Payment::find_by_id($payment_id);
$payment->status = "cancelled";
$payment->update();
?>
import com.mercadopago.*;
MercadoPago.SDK.configure("ENV_ACCESS_TOKEN");
Payment payment = Payment.load(paymentId);
payment.status = "canceled";
payment.update();
var mercadopago = require('mercadopago');
mercadopago.configurations.setAccessToken(config.access_token);
let paymentToBeCanceled = 123;
mercadopago.payment.cancel(paymentToBeCanceled, mercadopago, (error, response) => {
if (error){
console.log(error);
}else{
console.log(response)
}
});
require 'mercadopago'
MercadoPago::SDK.configure(ACCESS_TOKEN: ENV_ACCESS_TOKEN)
payment = MercadoPago::Payment.load(paymentId)
payment.status = "canceled"
payment.update()
curl -X PUT \
'https://api.mercadopago.com/v1/payments/PAYMENT_ID' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ENV_ACCESS_TOKEN' \
-d '{"status": "cancelled"}'
Response
json
{
...
"status": "cancelled",
"status_detail": "by_collector",
...
"captured": false,
...
}