AI resources

Default rendering

Before rendering the Payment Brick, first execute the initialization steps shared among all Bricks. From there, see below the necessary information to configure and render the Payment Brick.

Note
To consult the types and specifications of the parameters and responses of the Brick functions, refer to the technical documentation.

Configure the Brick

Create Brick's startup configuration.

const renderPaymentBrick = async (bricksBuilder) => {
 const settings = {
   initialization: {
     /*
      "amount" is the total amount to be paid by all means of payment with the exception of the Mercado Pago Account and Installment without a credit card, which have their processing value determined in the backend through the "preferenceId"
     */
     amount: 100,
     preferenceId: "<PREFERENCE_ID>",
   },
   customization: {
     paymentMethods: {
       ticket: "all",
       bankTransfer: "all",
       creditCard: "all",
       prepaidCard: "all",
       debitCard: "all",
       mercadoPago: "all",
     },
   },
   callbacks: {
     onReady: () => {
       /*
        Callback called when Brick is ready.
        Here you can hide loadings from your site, for example.
       */
     },
     onSubmit: ({ selectedPaymentMethod, formData }) => {
       // callback called when clicking the submit data button
       return new Promise((resolve, reject) => {
         fetch("/process_payment", {
           method: "POST",
           headers: {
             "Content-Type": "application/json",
           },
           body: JSON.stringify(formData),
         })
           .then((response) => response.json())
           .then((response) => {
             // receive payment result
             resolve();
           })
           .catch((error) => {
             // handle error response when trying to create payment
             reject();
           });
       });
     },
     onError: (error) => {
       // callback called for all Brick error cases
       console.error(error);
     },
   },
 };
 window.paymentBrickController = await bricksBuilder.create(
   "payment",
   "paymentBrick_container",
   settings
 );
};
renderPaymentBrick(bricksBuilder);
Attention
Whenever the user leaves the screen where some Brick is displayed, it is necessary to destroy the current instance with the command window.paymentBrickController.unmount(). When entering again, a new instance must be generated.

To use a payment method (paymentMethods) of the "mercadoPago" type, a preference must be sent during Brick initialization, replacing the value <PREFERENCE_ID> by the ID of the preference created. Instructions for creating the preference are in the Enable payment with Mercado Pago section.

Render the Brick

Once the configurations are created, enter the code below to render the Brick.

Important
The id paymentBrick_container of the html div below should correspond to the value used in the method create() of the last step.
<div id="paymentBrick_container"></div>

The result of rendering the Brick should look like the image below.

payment-Brick-layout-mlb

Enable payment with Mercado Pago

To use a payment method (paymentMethods) of the "mercadoPago" type, a preference must be sent during Brick initialization, replacing the value <PREFERENCE_ID> by the ID of the preference created.

To create a preference in your backend, add the Mercado Pago SDK and the necessary credentials to your project to enable the preference usage:

<?php
// Mercado Pago SDK
require __DIR__ .  '/vendor/autoload.php';
// Add Your credentials
MercadoPago\SDK::setAccessToken('PROD_ACCESS_TOKEN');
?>

Then set the preference according to your product or service.

The code examples below set the purpose of preference to wallet_purchase, but it is also possible to set it to onboarding_credits. Understand the difference between the two:

  • wallet_purchase: the user must log in when redirected to his Mercado Pago account.

  • onboarding_credits: after logging in, the user will see the pre-selected credit payment option in his Mercado Pago account.

<?php
$client = new PreferenceClient();
$preference = $client->create([
  "items"=> array(
    array(
      "title" => "My product",
      "quantity" => 1,
      "unit_price" => 25
    )
  )
]);
?>
When a user chooses to make a payment through Mercado Pago Wallet, they are redirected to the Mercado Pago page to complete the payment. The back_urls allow them to be returned to the site after the payment is completed. Do not use local domains for this value (such as 'localhost/' or '127.0.0.1' with or without a specified port). We recommend using a server with a named domain (DNS) or development IPs to be able to return to the site after payment.

For more details on how to configure it, access the Preferences section.