Go to Scriptcase store

# Mercado Pago

This API is available for use in the following countries:

It is also necessary to check the payment methods available in each country, check the updated list on the Mercado Pago website.

To use this API, you need a Mercado Pago account. Access [https://www.mercadopago.com.br/hub/registration/landing][link_mercado_pago_site]{:target=”_blank”} to log in or create an account if you don’t already have one.

## Setting up a Profile

In the menu Tools > API you can create API profiles for use in your projects. This way, you will only need to provide your credentials once to use the API in your project.

See how to obtain the credentials needed to configure the profile.

> When deploying your project, you will need to configure the API credentials in Prod, by accessing the API menu.

To create the profile, we must add the following information:

###### Name

Defines the name to identify the API profile. This name is unique and will be used to reference the API profile in your project’s applications.

###### Mode

The mode defines the availability of the Profile in Scriptcase.

- Public - Users from all projects will be able to view and use the created profile.

- Project - Users will be able to view and use the profile in the project where it was configured.

- User - Only the user who created the profile will be able to view and use it in the applications.

> Regardless of the mode used for creation, profiles can only be edited by users with administrator access in the project. See how to define user access levels in Scriptcase.

###### Gateway

Defines the API that will be used to create the profile.

###### Auth Token

Enter the Access Token generated in the Mercado Pago settings. If you do not have this information, see our article on how to obtain Mercado Pago credentials.

## Usage Example

It is possible to offer payment by Card, PIX, Mercado Pago Wallet, bank slip and payment at lottery outlets

See below some examples of using the API with PIX and Bank Slip

### Example of payment via PIX

> For payments using PIX, it is necessary that the keys are registered with Mercado Pago. See how to register your PIX key with Mercado Pago.

    // Calling the API directly in the macro.

    $token = 'public_token';

    $settings = array('settings' => ['gateway' => 'mercadopago', 'token' => $token] );

    $payment = sc_call_api('', $settings);

    // Passing the values ​​to the methods: values, description, and payment type.

    $payment->transaction_amount = 0.01;

    $payment->description = "test";

    $payment->payment_method_id = "pix";

    // Passing the customer information.

    // The "type" parameter can be the email or the CPF (Brazilian tax identification number).

    // "type" => "CPF"
    // "type" => "email"
    // The email, first_name, and last_name parameters cannot be the same as the account administrator's.

    $payment->payer = array(
    "email" => "contato@scriptcase.com.br",
    "first_name" => "Netmake",
    "last_name" => "Scriptcase",
    "identification" => array(

    "type" => "email",
    "number" => "contato@scriptcase.com.br"

    ),
    "address"=> array(

    "zip_code" => "",
    "street_name" => "Av. dos Lusíadas, 23",

    "street_number" => "",
    "neighborhood" => "",
    "city" => "São Paulo" ,

    "federal_unit" => "SP"

    )
    );

    //Call to execute the payment method.

    $payment->save();

#### API Response

The response will show the pending payment status and all the information you need to show the buyer.

The `transaction_data` value will return the data for the QR code.

> For more information, access the Mercado Pago documentation for PIX payment integration.

See below for an example of the response.

    //ID is the code transaction.
    "id": 5466310457,
    "status": "pending",
    "status_detail": "pending_waiting_transfer",
    ...,
    "transaction_details": {
        "net_received_amount": 0,
        "total_paid_amount": 100,
        "overpaid_amount": 0,
        "external_resource_url": null,
        "installment_amount": 0,
        "financial_institution": null
    },
    "point_of_interaction": {
        "type": "PIX",
        "sub_type": null,
        "application_data": {
        "name": "NAME_SDK",
        "version": "VERSION_NUMBER"
        },
        "transaction_data": {
        "qr_code_base64": "iVBORw0KGgoAAAANSUhEUgAABRQAAAUUCAYAAACu5p7oAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAIABJREFUeJzs2luO3LiWQNFmI+Y/Zd6vRt36KGNXi7ZOBtcagHD4kNLeiLX33v8DAAAAABD879sDAAAAAAA/h6AIAAAAAGSCIgAAAACQCYoAAAAAQCYoAgAAAACZoAgAAAAAZIIiAAAAAJAJigAAAABAJigCAAAAAJmgCAAAAABkgiIAAAAAkAmKAAAAAEAmKAIAAAAAmaAIAAAAAGSCIgAAAACQCYoAAAAAQCYoAgAAAACZoAgAAAAAZIIiAAAAAJAJigAAAABAJigCA...",
        "qr_code": "00020126600014br.gov.bcb.pix0117john@yourdomain.com0217additional data520400005303986540510.005802BR5913Maria Silva6008Brasilia62070503***6304E2CA",
        "ticket_url": "https://www.mercadopago.com.br/payments/1234544589/ticket?caller_id=123456&hash=123e4567-e89b-12d3-a456-426655440000"

### Example of Payment by Boleto (bank slip)

    // Chamada da Macro utilizando perfil da API
    $payment = sc_call_api('mercadpago');

    // Passando os valores para os métodos: valores, descrição e o tipo de pagamento.
    $payment->transaction_amount = 100;
    $payment->description = "Título do produto";

    // Tipo de pagamento para ser usado.
    // Pagamento por boleto = $payment->payment_method_id = "bolbradesco";
    // Pagamento em lotérica = $payment->payment_method_id = "pec";
    $payment->payment_method_id = "bolbradesco";

    // Passando as informações do cliente.
    // O parâmetro  “type” pode ser o email ou o cpf.
    // "type" => "CPF"
    // "type" => "email"
    // Os parâmetros email, first_name e last_name não podem ser os mesmos do administrador da conta.
    " $payment->payer = array(
         "email" => "test@test.com",
         "first_name" => "Test",
         "last_name" => "User",
         "identification" => array(
             "type" => "CPF",
             "number" => "19119119100"
          ),
         "address"=>  array(
             "zip_code" => "06233200",
             "street_name" => "Av. das Nações Unidas",
             "street_number" => "3003",
             "neighborhood" => "Bonfim",
             "city" => "Osasco",
             "federal_unit" => "SP"
          )
       );"

    // Chamada da execução do método de pagamento.
    $payment->save();

    // Retornando a URL gerada e abrindo diretamente na aplicação.
    $page = $payment->transaction_details->external_resource_url;
    header("Location: $page");

#### API Response

The response will show the pending status until the buyer makes the payment.

Additionally, in the response to the request, the `external_resource_url` parameter will return a URL containing instructions for the buyer to make the payment.

You can redirect them to this same link to complete the payment flow.

> For more information, access the Mercado Pago documentation for integration of payments via boleto.

See below an example of the response.

    // ID é código da transação.
    "id": 5466310457,
        "status": "pending",
        "status_detail": "pending_waiting_payment",
        ...,
        "transaction_details": {
            "net_received_amount": 0,
            "total_paid_amount": 100,
            "overpaid_amount": 0,
            "external_resource_url": "https://www.mercadopago.com/mlb/payments/ticket/helper?payment_id=123456789&payment_method_reference_id= 123456789&caller_id=123456",
            "installment_amount": 0,
            "financial_institution": null,
            "payment_method_reference_id": "1234567890"
        }