Default rendering
Before rendering the Wallet Brick, first execute the initialization steps shared among all Bricks. From there, see below the necessary information to configure and render the Wallet Brick.
Configure the Brick
Create Brick's startup configuration.
const renderWalletBrick = async (bricksBuilder) => {
await bricksBuilder.create('wallet', 'walletBrick_container', {
initialization: {
preferenceId: "<PREFERENCE_ID>",
},
customization: {
texts: {
valueProp: 'smart_option'
},
...
},
});
};
renderWalletBrick(bricksBuilder);
const initialization = {
preferenceId: '<PREFERENCE_ID>',
}
const customization = {
texts: {
valueProp: 'smart_option',
},
}
const onSubmit = async (formData) => {
// callback called when clicking on Wallet Brick
// this is possible because Brick is a button
};
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 on your website, for example.
};
This flow is designed for stores that use Wallet Brick at the end of the checkout process and already have the preference created when rendering the Brick, sending it during initialization. If desired, you can use the Brick by creating the preference at the time of submission (onSubmit
). See more information in the Preference on submit 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
initialization={initialization}
customization={customization}
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
$client = new PreferenceClient();
$preference = $client->create([
"items"=> array(
array(
"title" => "My product",
"quantity" => 1,
"unit_price" => 25
)
)
]);
?>
// 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
}
]
};
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,
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,
},
},
};
// 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
}
]
}
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
}
]
}'
Test your integration
With the integration completed, you will be able to test payment reception. For more information, access the section Make test purchase.