- Copy lib/mercadopago.jar and lib/jettison-1.0.1.jar to your project desired folder.
- Add these libs in your build path project.
- Get your CLIENT_ID and CLIENT_SECRET in the following address:
import mercadopago.MP;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
MP mp = new MP ("CLIENT_ID", "CLIENT_SECRET");
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
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:
// Get the payment reported by the IPN. Glossary of attributes response in https://developers.mercadopago.com
JSONObject payment_info = mp.getPaymentInfo(request.getParameter("ID"));
// Show payment information
if (Integer.parseInt (payment_info.get("status").toString()) == 200) {
out.print(payment_info.get("response"));
}
JSONObject result = mp.cancelPayment(request.getParameter("ID"));
// Show result
out.print(result);
JSONObject result = mp.refundPayment(request.getParameter("ID"));
// Show result
out.print(result);