Skip to content

mercadopago/sdk-java

Repository files navigation

Mercado Pago SDK for Java

Maven Central APM

The official Mercado Pago Java client library.

💡 Requirements

Java 1.8 or later

📲 Installation

First time using Mercado Pago? Create your Mercado Pago account, if you don’t have one already.

  1. Append MercadoPago dependencies to pom.xml
<dependency>
  <groupId>com.mercadopago</groupId>
  <artifactId>sdk-java</artifactId>
  <version>2.1.29</version>
</dependency>
  1. Run mvn install and that's all, you have Mercado Pago SDK installed.

  2. 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.

🌟 Getting Started

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();
    }
  }
}

Per-request Configuration

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();
    }
  }
}

SDK configurations

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);
  }
}

Custom Http Client

You can use a custom http client instead of using the default MPDefaultHttpClient by implementing the MPHttpClient interface.

public class CustomHttpClient implements MPHttpClient {
  //...
}

📚 Documentation

See our documentation for more details.

🤝 Contributing

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.

Patches to version 1.x.x

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.

❤️ Support

If you require technical support, please contact our support team at our developers site: English / Portuguese / Spanish

🏻 License

MIT license. Copyright (c) 2022 - Mercado Pago / Mercado Libre
For more information, see the LICENSE file.