GithubHelp home page GithubHelp logo

trinomio / sdk-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matiascompiano/sdk-java

0.0 2.0 0.0 6.85 MB

MercadoPago Java SDK

Home Page: http://developers.mercadopago.com/

Makefile 1.41% Java 98.59%

sdk-java's Introduction

MercadoPago SDK module for Payments integration

Install

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!

Basic checkout

Configure your credentials

import com.mercadopago.MP;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

MP mp = new MP ("CLIENT_ID", "CLIENT_SECRET");

Preferences

Get an existent Checkout preference

JSONObject preference = mp.getPreference("PREFERENCE_ID");

System.out.println(preference.toString());

Create a Checkout preference

JSONObject createPreferenceResult = mp.createPreference("{'items':[{'title':'Prueba','quantity':1,'currency_id':'ARS','unit_price':10.5}]}");
System.out.println(createPreferenceResult.toString());

Update an existent Checkout preference

JSONObject updatePreferenceResult = mp.updatePreference("PREFERENCE_ID", "{'items':[{'title':'Prueba','quantity':1,'currency_id':'USD','unit_price':2}]}");
System.out.println(updatePreferenceResult.toString());

Payments/Collections

Search for payments

// Sets the filters you want
Map<String, Object> filters = new HashMap<String, Object> ();
   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).getString("id"));
    System.out.println(results.getJSONObject(i).getString("external_reference"));
    System.out.println(results.getJSONObject(i).getString("status"));
}

Get payment data

// Get the payment reported by the IPN. Glossary of attributes response in https://developers.mercadopago.com
JSONObject payment_info = mp.getPayment("ID");

// Show payment information
if (Integer.parseInt (payment_info.get("status").toString()) == 200) {
    out.print(payment_info.get("response"));
}

Cancel (only for pending payments)

JSONObject result = mp.cancelPayment(request.getParameter("ID"));

// Show result
out.print(result);

Refund (only for accredited payments)

JSONObject result = mp.refundPayment(request.getParameter("ID"));

// Show result
out.print(result);

Customized checkout

Configure your credentials

import com.mercadopago.MP;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

MP mp = new MP ("ACCESS_TOKEN");

Create payment

mp.post ("/v1/payments", paymentData);

Create customer

mp.post ("/v1/customers", "{'email': '[email protected]'}");

Get customer

mp.get ("/v1/customers/CUSTOMER_ID");

Generic methods

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

sdk-java's People

Contributors

hcasatti avatar fpiruzi avatar sebagun avatar matiascompiano avatar nrubio avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.