Integrate payment flow
The Mercado Pago SDK offers a comprehensive set of features designed to simplify and optimize the entire payment process.
You will be able to configure, adapt and automate the payment flow according to the specific needs of your application. This includes actions such as managing payment methods, optimizing payment flows, handling errors and even processing approved payments.
Below, see how to configure each of these features.
To facilitate the integration of payment methods in your application, our SDK provides an instance of PaymentsMethodsTools. From this, using the getPaymentMethods function, it is possible to obtain a list of available payment methods, which are presented in PaymentMethod.
This resource allows you to retrieve supported payment methods according to the specifications of each country. This information is essential when starting the payment flow.
Below, see an example of how to access this function through the MPManager object and get the available payment methods according to the country.
MPManager.paymentMethodsTools.getPaymentMethods { response ->
response
.doIfSuccess { result ->
// you can render the list of payment methods in a recycler view
}.doIfError { exception ->
// error handling
}
}
/**
* Checks the status of a payment using the payment reference.
* @param paymentReference Reference of the payment (description entered in the field)
*/
public static void getPaymentStatus(String paymentReference) {
PaymentStatus paymentStatus = MPManager.INSTANCE.getPaymentStatus();
paymentStatus.getPaymentStatus(paymentReference, response -> {
doIfSuccess(response, result -> {
Log.i("PaymentStatusManager", "Payment status: " + result);
return null;
});
doIfError(response, error -> {
Log.e("PaymentStatusManager", "Payment status error: " + error.getMessage());
return null;
});
return null;
});
}
To start the payment flow in your SmartApp with the Mercado Pago SDK, use the PaymentFlow function, available in the launchPaymentFlow class. This function allows you to manage responses through callbacks, simplifying both implementation and subsequent control.
When implementing the PaymentFlowRequestData object, pay attention to the definition of the paymentMethod parameter, which may vary depending on the integration country. Use the resource to check available payment methods and configure them according to each country.
See below an example of how to access this function through the MPManager object and start the payment flow.
val paymentFlow = MPManager.paymentFlow
val paymentFlowRequestData = PaymentFlowRequestData(
amount = 10.0,
description = "test description",
paymentMethod = paymentMethod, // this parameter is obtained through the getPaymentMethods method
printOnTerminal = false, // optional field if launching payment without printing on terminal
taxes = listOf(
Tax(
payerCondition = PayerCondition.PAYMENT_TAXABLE_IVA
)
), // optional field if launching payment with taxes
)
paymentFlow.launchPaymentFlow(
paymentFlowRequestData = paymentFlowRequestData
) { response ->
response.doIfSuccess { result ->
// handle success using a message
}.doIfError { error ->
// handle the error
}
}
final PaymentFlow paymentFlow = MPManager.INSTANCE.getPaymentFlow();
final String amount = "2.0";
final String description = "Payment description";
final ArrayList<Tax> taxes = new ArrayList<>();
taxes.add(new Tax(
PayerCondition.PAYMENT_TAXABLE_IVA
));
final PaymentFlowData paymentFlowData = new PaymentFlowData(
amount,
description,
PaymentMethod, // this parameter is obtained through the getPaymentMethods method
6, // optional field if launching payment with installments
false, // optional field if launching payment without printing on terminal
taxes, // optional field if launching payment with taxes
);
final Function1<MPResponse<PaymentResponse>, Unit> callback = (final MPResponse<PaymentResponse> response) -> {
if (response.getStatus() == ResponseStatus.SUCCESS) {
// success handling using a message
} else {
// error handling
}
return Unit.INSTANCE;
};
paymentFlow.launchPaymentFlow(paymentFlowData, callback);
| Parameter | Type | Description | Required |
amount | String | Payment amount. | Yes |
description | String | Payment flow description. This field must contain unique information per transaction because it will be used as a basis to query the payment status. | Yes |
paymentMethod | String | The payment method to use. | No |
printOnTerminal | Boolean | Flag for automatic printing of payment receipt on terminal (default: true). | No |
taxes | List | List of indicators for inclusion of taxes in the sale, being: - PAYMENT_EXEMPT_IVA: payment exempt from VAT. - PAYMENT_TAXABLE_IVA: payment with VAT tax. | No |
In case of success, the response will look like the example below.
kotlin
paymentFlow.launchPaymentFlow( paymentFlowRequestData = paymentFlowRequestData ) { response -> response.doIfSuccess { result -> // result is a PaymentResponse object with the following information: println("Payment reference: ${result.paymentReference}") println("Payment method: ${result.paymentMethod}") println("Amount: ${result.paymentAmount}") println("Creation date: ${result.paymentCreationDate}") println("Last four digits: ${result.paymentLastFourDigits}") println("Device: ${result.paymentSnDevice}") println("User: ${result.paymentBrandName}") println("Tip amount: ${result.tipAmount}") println("External reference: ${result.externalReference}") }.doIfError { error -> // error contains the error message println("Error: ${error.message}") } }
| Parameter | Type | Description |
paymentReference | String | Number that serves as the unique identifier of the transaction. |
paymentMethod | PaymentMethod | Payment method used to process the payment (e.g.: CREDIT_CARD, DEBIT_CARD, etc.). |
paymentAmount | Number | Amount of the processed payment. |
paymentCreationDate | String | Date when the transaction was created. |
paymentInstallments | String | Number of installments the user chose at the time of payment. |
paymentLastFourDigits | String | Last 4 digits of the card used by the customer for payment. |
paymentSnDevice | String | Serial number of the device that processed the transaction. |
paymentBrandName | String | Name of the user registered on the Point Smart terminal. |
paymentStatusError | String | Field for recording transaction problems and errors. Empty if the payment was successful. |
tipAmount | String | Tip amount charged in the payment. |
externalReference | String | External reference of the payment. |
Our SDK presents a method to query the status of an approved payment at the end of the flow on the payment completion screen.
The query is performed based on the payment description. Therefore, this field must contain unique information per transaction. If multiple payments use the same description, the system will update the status based on the most recent payment and will only show the data of the last associated record.
Implement status query
With the getPaymentStatus method, which receives as a parameter the description (description) corresponding to the payment, a query is executed in our database and an object of type PaymentResponse is returned with all the information related to the approved payment.
The parseResponse function, available in PaymentFlow, allows you to analyze the responses of each transaction. By converting the URI data into a PaymentResponse object, you can access information such as the payment reference, the method used and possible associated errors.
Below, see an example of how to implement this resource.
// We initialize the payment status through MPManager
private val paymentStatus = MPManager.paymentStatus
// This is the result of what is written in the description field
private val paymentReference = ""
paymentStatus.getPaymentStatus(paymentReference) { response ->
response.doIfSuccess {
// handling success case of PaymentResponse object result
}.doIfError {
// Handling error case
}
}
// We initialize the payment status through MPManager
PaymentStatus paymentStatus = MPManager.getPaymentStatus();
// This is the result of what is written in the description field
String paymentReference = "";
paymentStatus.getPaymentStatus(paymentReference, new PaymentStatusCallback() {
@Override
public void onSuccess(PaymentResponse result) {
// Handling success case of PaymentResponse object result
}
@Override
public void onError(Throwable error) {
// Handling error case
}
});
If successful, the response will be similar to the example below.
kotlin
// We initialize the payment status through MPManager private val paymentStatus = MPManager.paymentStatus // This is the value from the description field used to fetch the status private val paymentReference = "12345678" paymentStatus.getPaymentStatus(paymentReference) { response -> response.doIfSuccess { result -> // result is an ApprovedPaymentData object with the following information: println("Payment ID: ${result.paymentId}") println("Payment type: ${result.type}") println("Amount: ${result.amount}") println("Creation date: ${result.creationDate}") println("Terminal serial number: ${result.snDevice}") println("Status: ${result.status}") println("Brand name: ${result.brandName}") println("Last four digits: ${result.lastFourDigits}") println("External reference: ${result.externalReference}") println("Description: ${result.description}") println("Tip: ${result.tip}") }.doIfError { error -> // Handle error case println("Error: ${error.message}") } }
| Parameter | Type | Description |
paymentId | String | Payment ID. |
type | String | Payment type. |
amount | Number | Amount of the successful payment. |
creationDate | String | Payment creation date. |
snDevice | String | Serial number of the Point Smart terminal that processed the transaction. |
status | String | Payment status. |
brandName | String | Name of the user registered on the Point Smart terminal. |
lastFourDigits | String | Last 4 digits of the customer's card used for the payment. |
externalReference | String | External reference of the payment. |
description | String | Transaction description. |
tip | String | Tip amount charged in the payment. |
paymentStatusError | String | Field for recording transaction issues and errors. Empty if the payment was successful. |