Default rendering
Configure the Brick
Create Brick's startup configuration.
const renderWalletBrick = async (bricksBuilder) => {
const settings = {
callbacks: {
onReady: () => {
/*
Callback called when Brick is ready.
Here you can hide loadings from your site, for example.
*/
},
onSubmit: (formData) => {
// callback called when clicking Wallet Brick
// this is possible because the Brick is a button
// at this time of submit, you must create the preference
const yourRequestBodyHere = {
items: [
{
id: '202809963',
title: 'Dummy title',
description: 'Dummy description',
quantity: 1,
unit_price: 10,
},
],
purpose: 'wallet_purchase',
};
return new Promise((resolve, reject) => {
fetch('/create_preference', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
})
.then((response) => response.json())
.then((response) => {
// resolve the promise with the ID of the preference
resolve(response.preference_id);
})
.catch((error) => {
// handle error response when trying to create preference
reject();
});
});
},
},
};
window.walletBrickController = await bricksBuilder.create(
'wallet',
'walletBrick_container',
settings,
);
};
renderWalletBrick(bricksBuilder);
const onSubmit = async (formData) => {
// callback called when clicking Wallet Brick
// this is possible because the Brick is a button
// at this time of submit, you must create the preference
const yourRequestBodyHere = {
items: [
{
id: '202809963',
title: 'Dummy title',
description: 'Dummy description',
quantity: 1,
unit_price: 10,
},
],
purpose: 'wallet_purchase',
};
return new Promise((resolve, reject) => {
fetch('/create_preference', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(yourRequestBodyHere),
})
.then((response) => response.json())
.then((response) => {
// resolve the promise with the ID of the preference
resolve(response.preference_id);
})
.catch((error) => {
// handle error response when trying to create preference
reject();
});
});
};
const onError = async (error) => {
// callback called for all Brick error cases
console.log(error);
};
const onReady = async () => {
/*
Callback called when Brick is ready.
Here you can hide loadings from your site, for example.
*/
};
This preference onSubmit creation flow is designed for sellers who have one-click flows, if you want, you can also send preference on startup. See more information in the Preference on startup section.
Render the Brick
Once the configurations are created, enter the code below to render the brick.
<div id="walletBrick_container"></div>
import { Wallet } from '@mercadopago/sdk-react';
<Wallet
onSubmit={onSubmit}
onReady={onReady}
onError={onError}
/>
The result of rendering the Brick should blook like the image below, presenting a text and a standard visual.
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');
?>
// Mercado Pago SDK
const mercadopago = require ('mercadopago');
// Add Your credentials
mercadopago.configure({
access_token: 'PROD_ACCESS_TOKEN'
});
// Mercado Pago SDK
import com.mercadopago.MercadoPagoConfig;
// Add Your credentials
MercadoPagoConfig.setAccessToken("PROD_ACCESS_TOKEN");
# Mercado Pago SDK
require 'mercadopago'
# Add Your credentials
sdk = Mercadopago::SDK.new('PROD_ACCESS_TOKEN')
// Mercado Pago SDK
using MercadoPago.Config;
// Add Your credentials
MercadoPagoConfig.AccessToken = "PROD_ACCESS_TOKEN";
# Mercado Pago SDK
import mercadopago
# Add Your credentials
sdk = mercadopago.SDK("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
, where the user must log in when redirected to his Mercado Pago account.
<?php
// Create a preference object
$preference = new MercadoPago\Preference();
// Create an item in the preference
$item = new MercadoPago\Item();
$item->title = 'Meu produto';
$item->quantity = 1;
$item->unit_price = 75.56;
$preference->items = array($item);
// o $preference->purpose = 'wallet_purchase'; only allow logged in payments
// to allow guest payments you can omit this property
$preference->purpose = 'wallet_purchase';
$preference->save();
?>
// Create a preference object
let preference = {
// o "purpose": "wallet_purchase" only allows logged payments
// to allow guest payments you can omit this property
"purpose": "wallet_purchase",
"items": [
{
"id": "item-ID-1234",
"title": "Meu produto",
"quantity": 1,
"unit_price": 75.76
}
]
};
mercadopago.preferences.create(preference)
.then(function (response) {
// This value is the preferenceId that will be sent to the Brick at startup
const preferenceId = response.body.id;
}).catch(function (error) {
console.log(error);
});
// Create a preference object
PreferenceClient client = new PreferenceClient();
// Create an item in the preference
List<PreferenceItemRequest> items = new ArrayList<>();
PreferenceItemRequest item =
PreferenceItemRequest.builder()
.title("Meu produto")
.quantity(1)
.unitPrice(new BigDecimal("100"))
.build();
items.add(item);
PreferenceRequest request = PreferenceRequest.builder()
// o .purpose('wallet_purchase') only allows logged payments
// to allow guest payments you can omit this line
.purpose('wallet_purchase')
.items(items).build();
client.create(request);
# Create a preference object
preference_data = {
# the purpose: 'wallet_purchase', allows only logged payments
# to allow guest payments you can omit this property
purpose: 'wallet_purchase',
items: [
{
title: 'Meu produto',
unit_price: 75.56,
quantity: 1
}
]
}
preference_response = sdk.preference.create(preference_data)
preference = preference_response[:response]
# This value is the preferenceId you will use in the HTML on Brick startup
@preference_id = preference['id']
// Create the preference request object
var request = new PreferenceRequest
{
// the Purpose = 'wallet_purchase', allows only logged payments.
// to allow guest payments you can omit this property
Purpose = 'wallet_purchase',
Items = new List<PreferenceItemRequest>
{
new PreferenceItemRequest
{
Title = "Meu produto",
Quantity = 1,
CurrencyId = "BRL",
UnitPrice = 75.56m,
},
},
};
// Create the preference using the client
var client = new PreferenceClient();
Preference preference = await client.CreateAsync(request);
# Create an item in the preference
preference_data = {
# the "purpose": "wallet_purchase", allows only logged in payments
# to allow guest payments, you can omit this property
"purpose": "wallet_purchase",
"items": [
{
"title": "My Item",
"quantity": 1,
"unit_price": 75.76
}
]
}
preference_response = sdk.preference().create(preference_data)
preference = preference_response["response"]
curl -X POST \
'https://api.mercadopago.com/checkout/preferences' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-H 'Authorization: Bearer **PROD_ACCESS_TOKEN**' \
-d '{
"purpose": "wallet_purchase",
"items": [
{
"title": "My product",
"quantity": 1,
"unit_price": 75.76
}
]
}'