The official Mercado Pago Java client library.
Java 1.8 or later
First time using Mercado Pago? Create your Mercado Pago account, if you donβt have one already.
- Append MercadoPago dependencies to pom.xml
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk-java</artifactId>
<version>2.1.27</version>
</dependency>
-
Run
mvn install
and that's all, you have Mercado Pago SDK installed. -
Copy the access_token in the credentials section of the page and replace YOUR_ACCESS_TOKEN with it.
That's it! Mercado Pago SDK has been successfully installed.
Simple usage looks like:
import com.mercadopago.*;
import com.mercadopago.client.payment.PaymentClient;
import com.mercadopago.client.payment.PaymentCreateRequest;
import com.mercadopago.client.payment.PaymentPayerRequest;
import com.mercadopago.exceptions.MPApiException;
import com.mercadopago.exceptions.MPException;
import com.mercadopago.resources.payment.Payment;
import java.math.BigDecimal;
public class Example {
public static void main(String[] args) {
MercadoPagoConfig.setAccessToken("YOUR_ACCESS_TOKEN");
PaymentClient client = new PaymentClient();
PaymentCreateRequest createRequest =
PaymentCreateRequest.builder()
.transactionAmount(new BigDecimal(1000))
.token("your_cardtoken")
.description("description")
.installments(1)
.paymentMethodId("visa")
.payer(PaymentPayerRequest.builder().email("dummy_email").build())
.build();
try {
Payment payment = client.create(createRequest);
System.out.println(payment);
} catch (MPApiException ex) {
System.out.printf(
"MercadoPago Error. Status: %s, Content: %s%n",
ex.getApiResponse().getStatusCode(), ex.getApiResponse().getContent());
} catch (MPException ex) {
ex.printStackTrace();
}
}
}
All the request methods accept an optional MPRequestOptions
object. With this you can set a custom access token,
custom timeouts or even any custom headers you want, like an idempotency key for example.
public class Example {
public static void main(String[] args) {
PaymentClient client = new PaymentClient();
Map<String, String> customHeaders = new HashMap<>();
customHeaders.put("x-idempotency-key", "...");
MPRequestOptions requestOptions =
MPRequestOptions.builder()
.accessToken("custom_access_token")
.connectionRequestTimeout(2000)
.connectionTimeout(2000)
.socketTimeout(2000)
.customHeaders(customHeaders)
.build();
try {
Payment payment = client.create(createRequest, requestOptions);
System.out.println(payment);
} catch (MPException | MPApiException ex) {
ex.printStackTrace();
}
}
}
You can also set some customizations directly at MercadoPagoConfig class, for example set custom timeouts, a custom http client, log configurations, etc...
public class Example {
public static void main(String[] args) {
MercadoPagoConfig.setConnectionRequestTimeout(2000);
MercadoPagoConfig.setSocketTimeout(2000);
MercadoPagoConfig.setLoggingLevel(Level.FINEST);
}
}
You can use a custom http client instead of using the default MPDefaultHttpClient
by implementing the MPHttpClient
interface.
public class CustomHttpClient implements MPHttpClient {
//...
}
See our documentation for more details.
- Mercado Pago reference API. Portuguese / English / Spanish
All contributions are welcome, ranging from people wanting to triage issues, others wanting to write documentation, to people wanting to contribute code.
Please read and follow our contribution guidelines. Contributions not following these guidelines will be disregarded. The guidelines are in place to make all of our lives easier and make contribution a consistent process for everyone.
Since the release of version 2.0.0, version 1 is deprecated and will not be receiving new
features, only bug fixes. If you need to submit PRs for that version, please do so by using develop-v1
as your base
branch.
If you require technical support, please contact our support team at our developers site: English / Portuguese / Spanish
MIT license. Copyright (c) 2022 - Mercado Pago / Mercado Libre
For more information, see the LICENSE file.