Install it using maven:
Just add to your pom.xml the following repository
<repositories>
...
<repository>
<id>mercadopago</id>
<url>https://github.com/mercadopago/sdk-java/raw/master/releases</url>
</repository>
...
</repositories>
Then add the dependency
<dependencies>
...
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk</artifactId>
<version>0.3.4</version>
</dependency>
...
</dependencies>
And that's it!
- Get your CLIENT_ID and CLIENT_SECRET in the following address:
- Argentina: https://www.mercadopago.com/mla/herramientas/aplicaciones
- Brazil: https://www.mercadopago.com/mlb/ferramentas/aplicacoes
- México: https://www.mercadopago.com/mlm/herramientas/aplicaciones
- Venezuela: https://www.mercadopago.com/mlv/herramientas/aplicaciones
- Colombia: https://www.mercadopago.com/mco/herramientas/aplicaciones
import mercadopago.MP;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
MP mp = new MP ("CLIENT_ID", "CLIENT_SECRET");
import mercadopago.MP;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
MP mp = new MP ("LL_ACCESS_TOKEN");
String accessToken = mp.getAccessToken();
System.out.println(accessToken);
JSONObject preference = mp.getPreference("PREFERENCE_ID");
System.out.println(preference.toString());
JSONObject createPreferenceResult = mp.createPreference("{'items':[{'title':'Prueba','quantity':1,'currency_id':'ARS','unit_price':10.5}]}");
System.out.println(createPreferenceResult.toString());
JSONObject updatePreferenceResult = mp.updatePreference("PREFERENCE_ID", "{'items':[{'title':'Prueba','quantity':1,'currency_id':'USD','unit_price':2}]}");
System.out.println(updatePreferenceResult.toString());
// Sets the filters you want
Map<String, Object> filters = new HashMap<String, Object> ();
filters.put("site_id", "MLA"); // Argentina: MLA; Brasil: MLB; Mexico: MLM; Venezuela: MLV; Colombia: MCO
filters.put("external_reference", "Bill001");
// Search payment data according to filters
JSONObject searchResult = mp.searchPayment (filters);
JSONArray results = searchResult.getJSONObject("response").getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
System.out.println(results.getJSONObject(i).getJSONObject("collection").getString("id"));
System.out.println(results.getJSONObject(i).getJSONObject("collection").getString("external_reference"));
System.out.println(results.getJSONObject(i).getJSONObject("collection").getString("status"));
}
- Go to Mercadopago IPN configuration:
- Argentina: https://www.mercadopago.com/mla/herramientas/notificaciones
- Brasil: https://www.mercadopago.com/mlb/ferramentas/notificacoes
- México: https://www.mercadopago.com/mlm/herramientas/notificaciones
- Venezuela: https://www.mercadopago.com/mlv/herramientas/notificaciones
- Colombia: https://www.mercadopago.com/mco/herramientas/notificaciones
//Get your access token
Map<String, Object> params = new HashMap<String, Object>();
params.put("access_token", mp.getAccessToken());
JSONObject merchantOrderInfo=null;
// Get the merchant_order reported by the IPN. Glossary of attributes response in https://developers.mercadopago.com
if(request.getParameter("topic").equals("merchant_order")){
merchantOrderInfo = mp.get("/merchant_orders/" + request.getParameter("id"), params, false);
// Get the payment reported by the IPN. Glossary of attributes response in https://developers.mercadopago.com
}else if(request.getParameter("topic").equals("payment")){
JSONObject paymentInfo = mp.get("/collections/notifications/" + request.getParameter("id"), params, false);
merchantOrderInfo = mp.get("/merchant_orders/" + paymentInfo.getJSONObject("response").getJSONObject("collection").getString("merchant_order_id"), params, false);
}
//If the payment's transaction amount is equal (or bigger) than the merchant order's amount you can release your items
if (merchantOrderInfo.getInt("status") == 200) {
Double transactionAmountPayments = 0D;
Double transactionAmountOrder = merchantOrderInfo.getJSONObject("response").getDouble("total_amount");
JSONArray payments=merchantOrderInfo.getJSONObject("response").getJSONArray("payments");
for (int i=0; i < payments.length(); i++) {
JSONObject payment = payments.getJSONObject(i);
if(payment.getString("status").equals("approved")){
transactionAmountPayments += payment.getDouble("transaction_amount");
}
}
if(transactionAmountPayments >= transactionAmountOrder){
System.out.print("release your items");
}else{
System.out.print("dont release your items");
}
}
JSONObject result = mp.cancelPayment(request.getParameter("ID"));
// Show result
out.print(result);
JSONObject result = mp.refundPayment(request.getParameter("ID"));
// Show result
out.print(result);
You can access any other resource from the MercadoPago API using the generic methods:
// Get a resource, with optional URL params. Also you can disable authentication for public APIs
mp.get ("/resource/uri", [params], [authenticate=true]);
// Create a resource with "data" and optional URL params.
mp.post ("/resource/uri", data, [params]);
// Update a resource with "data" and optional URL params.
mp.put ("/resource/uri", data, [params]);
// Delete a resource with optional URL params.
mp.delete ("/resource/uri", [params]);
For example, if you want to get the Sites list (no params and no authentication):
JSONObject result = mp.get ("/sites", null, false);
out.print(result);