Mercadopago SDK module for Payments integration
Usage:
$ npm install mercadopago
var MP = require ("mercadopago");
var mp = new MP ("CLIENT_ID", "CLIENT_SECRET");
Using Mercadopago Checkout
Create a Checkout preference:
var preference = {
"items": [
{
"title": "Test",
"quantity": 1,
"currency_id": "USD",
"unit_price": 10.5
}
]
};
mp.createPreference (preference)
.on ("ok", function (pref){
console.log (JSON.stringify (pref, null, 4));
})
.on ("error", function (error){
console.log ("ERROR: " + error);
});
Get an existent Checkout preference:
mp.getPreference ("PREFERENCE_ID")
.on ("ok", function (pref){
console.log (JSON.stringify (pref, null, 4));
})
.on ("error", function (error){
console.log ("ERROR: " + error);
});
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)
.on ("ok", function (pref){
console.log (JSON.stringify (pref, null, 4));
})
.on ("error", function (error){
console.log ("ERROR: " + error);
});
Using Mercadopago Payment collection
Searching:
var filters = {
"id": null,
"site_id": null,
"external_reference": null
};
mp.searchCollection (filters)
.on ("ok", function (result){
console.log (JSON.stringify (result, null, 4));
})
.on ("error", function (error){
console.log ("ERROR: " + error);
});
Receiving IPN notification:
var MP = require ("mercadopago"),
http = require("http"),
url = require('url');
var mp = new MP ("CLIENT_ID", "CLIENT_SECRET");
function onRequest(request, response) {
var qs = url.parse (request.url, true).query;
mp.getPaymentInfo (qs["id"])
.on ("ok", function (result){
console.log (JSON.stringify (result, null, 4));
response.writeHead(200, {
'Content-Type' : 'application/json; charset=utf-8'
});
response.write (JSON.stringify (result));
response.end();
})
.on ("error", function (error){
console.log ("ERROR: " + error);
response.writeHead(200, {
'Content-Type' : 'application/json; charset=utf-8'
});
response.write (JSON.stringify (error));
response.end();
});
}
http.createServer(onRequest).listen(8888);