Skip to content

Response & Error

Ariel Rey edited this page Jan 24, 2017 · 1 revision

MercadoPago Custom Responses and Errors

We implement two new objects that will help you make your implementation much more easier:

  • mercadopagoResponse
  • mercadopagoIpnResponse
  • mercadopagoError

mercadopagoResponse

This is the response object that you will receive from all the your resources responses. This is compose by the next variables and functions:

  • body - Object: Response Body
  • status - Integer: Status Code of the response
  • idempotency - String: Idempotency Header sended on the request
  • pagination - Object: Response object that comes from endpoints that have pagination like Payment Search
  • next - Function: Return next page
  • hasNext - Function: Check if has next Page (Boolean)
  • prev - Function: Return previous page
  • hasPrev - Function: Check if has previous page (Boolean)

Idempotency

The idempotency is a header send to MercadoPago to identify the operation. You will find more information about this on the Usage section here.

Pagination

The pagination is fully explain on the pagination section here

mercadopagoIpnResponse

This is the response that is going to come from the IPN Manager. This is compose by the next variables:

  • body - Object: Response body
  • status - Integer: Response status code
  • Id - String: id receive from the Webhook
  • Topic - String: topic receive from the Webhook

You will find more information about the IPN Manager here.

mercadopagoError

This is the error object you receive when a resource fail. It is compose by the following variables and functions:

  • name - String: Error name
  • message - String: Message receive from MercadoPago API
  • cause - Array (Object): Cause receive from MercadoPago API
  • stack - Stacktrace from error
  • status - Integer: Status code from response
  • idempotency - String: Idempotency Id Header
  • retry - Function

Retry

If a requests fail for time-out or other errors, we expose a method call retry to help you execute the same operation again re-using the idempotency and facilitating you the way of retrying it. If this method doesnt exists you will need to save the request object and get the idempotency from the error and construct the request again. Let's see a comparation:

Without retry
var payment = {
  description: 'Descripción',
  transaction_amount: 10,
  payment_method_id: 'rapipago',
  payer: {
    email: '[email protected]',
    identification: {
      type: 'DNI',
      number: '34214577'
    }
  }
};

mercadopago.payment.create(payment).then(function (response) {
  console.log(response);
}).catch(function (err) {
  // Manually retry
  mercadopago.payment.create(payment, {
    idempotency: err.idempotency
  }).then(function (response) {
    console.log(response);
  }).catch(function () {
    // Another Error
  });
});
With retry
mercadopago.payment.create({
  description: 'Descripción',
  transaction_amount: 10,
  payment_method_id: 'rapipago',
  payer: {
    email: '[email protected]',
    identification: {
      type: 'DNI',
      number: '34214577'
    }
  }
}).then(function (response) {
  console.log(response);
}).catch(function (err) {
  err.retry().then(function (response) {
    console.log(response);
  }).catch(function () {
    // Another Error
  });
});