Skip to content

Latest commit

 

History

History
266 lines (209 loc) · 6.71 KB

README.md

File metadata and controls

266 lines (209 loc) · 6.71 KB

MercadoPago SDK module for Payments integration

Install

$ npm install mercadopago

Promises and Callbacks support

Every method supports either promises and callbacks. For example:

var at = mp.getAccessToken ();

at.then (
    function (accessToken) {
        console.log (accessToken);
    },
    function (error) {
        console.log (error);
    });

is the same as:

mp.getAccessToken(function (err, accessToken){
    if (err) {
        console.log (err);
    } else {
        console.log (accessToken);
    }
});

In order to use callbacks, simply pass a function as the last parameter.

Basic checkout

Configure your credentials

var MP = require ("mercadopago");

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

Preferences

Get an existent Checkout preference

mp.getPreference ("PREFERENCE_ID");

Create a Checkout preference

var preference = {
        "items": [
            {
                "title": "Test",
                "quantity": 1,
                "currency_id": "USD",
                "unit_price": 10.5
            }
        ]
    };

mp.createPreference (preference);

Update an existent Checkout preference:

var preference = {
        "items": [
            {
                "title": "Test Modified",
                "quantity": 1,
                "currency_id": "USD",
                "unit_price": 20.4
            }
        ]
    };

mp.updatePreference ("PREFERENCE_ID", preference);

Payments/Collections

Search for payments

var filters = {
        "id": null,
        "site_id": null,
        "external_reference": null
    };

mp.searchPayment (filters)
    .then (
        function success (data) {
            console.log (JSON.stringify (data, null, 4));
        },
        function error (err) {
            console.log (err);
        }
    });

Get payment data

mp.getPayment (qs["id"])
    .then (
        function success (data) {
            console.log (JSON.stringify (data, null, 4));
        },
        function error (err) {
            console.log (err);
        }
    });

Cancel (only for pending payments)

mp.cancelPayment ("ID");

Refund (only for accredited payments)

mp.refundPayment ("ID");

Customized checkout

Configure your credentials

var MP = require ("mercadopago");

var mp = new MP ("ACCESS_TOKEN");

Create payment

mp.post ({
    "uri": "/v1/payments",
    "data": payment_data
}).then (...);

Create customer

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

Get customer

mp.get ({
    "uri": "/v1/customers/CUSTOMER_ID"
}).then (...);

Generic methods

You can access any resource from the MercadoPago API using the generic methods. The basic structure is:

mp.method(request).then(...)

where request can be:

{
    "uri": "The resource URI, relative to https://api.mercadopago.com",
    "params": "Optional. Key:Value object with parameters to be appended to the URL",
    "data": "Optional. Object or String to be sent in POST and PUT requests",
    "headers": "Optional. Key:Value object with custom headers, like content-type: application/x-www-form-urlencoded",
    "authenticate": "Optional. Boolean to specify if the GET method has to authenticate with credentials before request. Set it to false when accessing public APIs"
}

Examples:

// Get a resource, with optional URL params. Also you can disable authentication for public APIs
mp.get ({
    "uri": "/resource/uri",
    "params": {params},
    "authenticate": true
});

// Create a resource with "data" and optional URL params.
mp.post ({
    "uri": "/resource/uri",
    "data": data,
    "params": {params}
});

// Update a resource with "data" and optional URL params.
mp.put ({
    "uri": "/resource/uri",
    "data": data,
    "params": {params}
});

// Delete a resource with optional URL params.
mp.delete ({
    "uri": "/resource/uri",
    "params": {params}
});

For example, if you want to get the Sites list (no params and no authentication):

mp.get ({
    "uri": "/sites",
    "authenticate": false
}).then (function (sites) {
    console.log (sites);
});