Capturar pago autorizado
La finalización de un pago sucede después de la captura del pago autorizado, lo que significa que se puede debitar de la tarjeta el importe reservado para la compra.
Hay dos formas de capturar un pago autorizado:
- Captura del monto total de una reserva: en el que se captura el monto total del pago reservado.
- Captura del monto inferior al reservado: en la que se captura el monto parcial del pago reservado.
A continuación describimos en detalle cada una de las opciones y cómo ejecutarlas.
Capturar monto total
Para capturar el monto total de una reserva, envía el valor a capturar al parámetro transaction_amount
y ejecuta la solicitud a través de los códigos disponibles a continuación.
<?php
use MercadoPago\Client\Payment\PaymentClient;
MercadoPagoConfig::setAccessToken("YOUR_ACCESS_TOKEN");
$client = new PaymentClient();
$request_options = new RequestOptions();
$request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);
$client->capture($payment_id, $request_options);
?>
import { MercadoPagoConfig, Payment } from 'mercadopago';
const client = new MercadoPagoConfig({ accessToken: 'YOUR_ACCESS_TOKEN' });
const payment = new Payment(client);
payment.capture({
id: '<PAYMENT_ID>',
transaction_amount: 12.34,
requestOptions: {
idempotencyKey: '<IDEMPOTENCY_KEY>'
}
}).then(console.log).catch(console.log);
MercadoPagoConfig.setAccessToken("ENV_ACCESS_TOKEN");
Long paymentId = 123456789L;
PaymentClient client = new PaymentClient();
client.capture(paymentId);
require 'mercadopago'
sdk = Mercadopago::SDK.new('ENV_ACCESS_TOKEN')
request = {
capture: true
}
payment_response = sdk.payment.update(payment_id, request)
payment = payment_response[:response]
using MercadoPago.Client.Payment;
using MercadoPago.Config;
using MercadoPago.Resource.Payment;
MercadoPagoConfig.AccessToken = "ENV_ACCESS_TOKEN";
var client = new PaymentClient();
Payment payment = await client.CaptureAsync(paymentId);
import mercadopago
sdk = mercadopago.SDK("ENV_ACCESS_TOKEN")
payment_data = { "capture": True }
payment_response = sdk.payment().update(payment_id, payment_data)
payment = payment_response["response"]
package main
import (
"context"
"fmt"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/payment"
)
func main() {
accessToken := "{{ACCESS_TOKEN}}"
cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}
client := payment.NewClient(cfg)
// Create payment.
request := payment.Request{
TransactionAmount: 105.1,
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{CARD_TOKEN}}",
Installments: 1,
Capture: false,
}
resource, err := client.Create(context.Background(), request)
if err != nil {
fmt.Println(err)
return
}
// Capture.
resource, err = client.Capture(context.Background(), resource.ID)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resource)
}
curl -X PUT \
'https://api.mercadopago.com/v1/payments/PAYMENT_ID' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ENV_ACCESS_TOKEN' \
-H 'X-Idempotency-Key: SOME_UNIQUE_VALUE' \
-d '{"capture": true}'
La respuesta va a devolver que el pago se encuentra aprobado y acreditado.
{
...
"status": "approved",
"status_detail": "accredited",
...
"captured": true,
...
}
Capturar monto parcial
Para capturar un monto inferior al reservado, envía el valor a capturar al parámetro transaction_amount
y ejecuta la solicitud a través de los códigos disponibles a continuación.
<?php
MercadoPago\SDK::setAccessToken("ENV_ACCESS_TOKEN");
$payment = MercadoPago\Payment::find_by_id($payment_id);
$payment->transaction_amount = 75;
$payment->capture = true;
$payment->update();
?>
MercadoPagoConfig.setAccessToken("ENV_ACCESS_TOKEN");
Long paymentId = 123456789L;
PaymentClient client = new PaymentClient();
client.capture(paymentId, new BigDecimal("75"));
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'
sdk = Mercadopago::SDK.new('ENV_ACCESS_TOKEN')
request = {
transaction_amount: 75,
capture: true
}
payment_response = sdk.payment.update(payment_id, request)
payment = payment_response[:response]
using MercadoPago.Client.Payment;
using MercadoPago.Config;
using MercadoPago.Resource.Payment;
MercadoPagoConfig.AccessToken = "ENV_ACCESS_TOKEN";
var client = new PaymentClient();
Payment payment = await client.CaptureAsync(paymentId, 75);
import mercadopago
sdk = mercadopago.SDK("ENV_ACCESS_TOKEN")
payment_data = {
"transaction_amount": 75,
"capture": True
}
payment_response = sdk.payment().update(payment_id, payment_data)
payment = payment_response["response"]
package main
import (
"context"
"fmt"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/payment"
)
func main() {
accessToken := "{{ACCESS_TOKEN}}"
cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}
client := payment.NewClient(cfg)
// Create payment.
request := payment.Request{
TransactionAmount: 105.1,
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{CARD_TOKEN}}",
Installments: 1,
Capture: false,
}
resource, err := client.Create(context.Background(), request)
if err != nil {
fmt.Println(err)
return
}
// Capture.
resource, err = client.Capture(context.Background(), resource.ID)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resource)
}
curl -X PUT \
'https://api.mercadopago.com/v1/payments/PAYMENT_ID' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ENV_ACCESS_TOKEN' \
-H 'X-Idempotency-Key: SOME_UNIQUE_VALUE' \
-d '{
"transaction_amount": 75,
"capture": true
}'
La respuesta va a devolver que el pago se encuentra aprobado y acreditado.
{
...
"status": "approved",
"status_detail": "accredited",
...
"transaction_amount": 75,
...
"captured": true,
...
}