mercadopago
Advanced tools
Comparing version 0.3.3 to 0.5.0
"use strict"; | ||
var p = require("../package"), | ||
var p = require("../package"), | ||
request = require ("request"), | ||
@@ -13,420 +13,524 @@ Q = require ("q"); | ||
function MercadoPagoError(message, status) { | ||
this.name = "MercadoPagoError"; | ||
this.message = message || "MercadoPago Unknown error"; | ||
this.stack = (new Error()).stack; | ||
this.status = status || 500; | ||
} | ||
MercadoPagoError.prototype = Object.create(Error.prototype); | ||
MercadoPagoError.prototype.constructor = MercadoPagoError; | ||
var MP = function () { | ||
var __llAccessToken, | ||
__clientId, | ||
__clientSecret, | ||
__sandbox = false; | ||
if (arguments.length > 2 || arguments.length < 1) { | ||
throw "Invalid arguments. Use CLIENT_ID and CLIENT SECRET, or ACCESS_TOKEN"; | ||
throw new MercadoPagoError("Invalid arguments. Use CLIENT_ID and CLIENT SECRET, or ACCESS_TOKEN", 400); | ||
} | ||
if (arguments.length == 1) { | ||
this.__llAccessToken = arguments[0]; | ||
__llAccessToken = arguments[0]; | ||
} | ||
if (arguments.length == 2) { | ||
this.__clientId = arguments[0]; | ||
this.__clientSecret = arguments[1]; | ||
__clientId = arguments[0]; | ||
__clientSecret = arguments[1]; | ||
} | ||
this.__sandbox = false; | ||
}; | ||
// Instance creation | ||
var mp = {}; | ||
MP.version = p.version; | ||
/** | ||
* Switch or get Sandbox Mode for Basic Checkout | ||
*/ | ||
mp.sandboxMode = function (enable) { | ||
if (enable !== null && enable !== undefined) { | ||
__sandbox = enable === true; | ||
} | ||
MP.prototype.sandboxMode = function (enable) { | ||
if (enable !== null) { | ||
this.__sandbox = enable === true; | ||
} | ||
return __sandbox; | ||
}; | ||
return this.__sandbox; | ||
}; | ||
/** | ||
* Get Access Token for API use | ||
*/ | ||
mp.getAccessToken = function () { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
MP.prototype.getAccessToken = function () { | ||
var __self = this; | ||
if (__llAccessToken) { | ||
next && next(null, __llAccessToken); | ||
deferred.resolve (__llAccessToken); | ||
} else { | ||
MPRestClient.post({ | ||
"uri": "/oauth/token", | ||
"data": { | ||
"client_id": __clientId, | ||
"client_secret": __clientSecret, | ||
"grant_type": "client_credentials" | ||
}, | ||
"headers": { | ||
"Content-type": config.MIME_FORM | ||
} | ||
}).then ( | ||
function success (data) { | ||
next && next(null, data.response.access_token); | ||
deferred.resolve (data.response.access_token); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject (err); | ||
} | ||
); | ||
} | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
return deferred.promise; | ||
}; | ||
if (__self.__llAccessToken) { | ||
next && next(null, __self.__llAccessToken); | ||
deferred.resolve (__self.__llAccessToken); | ||
} else { | ||
MP.restClient.post( | ||
"/oauth/token", | ||
{ | ||
"client_id": __self.__clientId, | ||
"client_secret": __self.__clientSecret, | ||
"grant_type": "client_credentials" | ||
}, | ||
config.MIME_FORM | ||
).then ( | ||
function success (data) { | ||
next && next(null, data.response.access_token); | ||
deferred.resolve (data.response.access_token); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject (err); | ||
/** | ||
Generic resource get | ||
@param req | ||
@param params (deprecated) | ||
@param authenticate = true (deprecated) | ||
*/ | ||
mp.get = function (req) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
if (typeof req == "string") { | ||
req = { | ||
"uri": req, | ||
"params": arguments[1], | ||
"authenticate": arguments[2] | ||
}; | ||
} | ||
req.authenticate = req.authenticate !== false; | ||
var auth = Q.Promise(function(resolve, reject) { | ||
if (req.authenticate) { | ||
resolve(mp.getAccessToken()); | ||
} else { | ||
resolve(); | ||
} | ||
); | ||
} | ||
}); | ||
return deferred.promise; | ||
}; | ||
auth.then(function success(at) { | ||
if (at) { | ||
req.params || (req.params = {}); | ||
req.params.access_token = at; | ||
} | ||
/** | ||
Get information for specific payment | ||
@param id | ||
@return json | ||
*/ | ||
MP.prototype.getPayment = MP.prototype.getPaymentInfo = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
MPRestClient.get(req).then ( | ||
function success (data) { | ||
next && next(null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject (err); | ||
} | ||
); | ||
}, | ||
deferred.reject); | ||
var uriPrefix = this.__sandbox ? "/sandbox" : ""; | ||
return deferred.promise; | ||
}; | ||
return this.get (uriPrefix+"/collections/notifications/"+id, next); | ||
}; | ||
/** | ||
Generic resource post | ||
@param req | ||
@param data (deprecated) | ||
@param params (deprecated) | ||
*/ | ||
mp.post = function (req) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
/** | ||
Get information for specific authorized payment | ||
@param id | ||
@return json | ||
*/ | ||
MP.prototype.getAuthorizedPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
if (typeof req == "string") { | ||
req = { | ||
"uri": req, | ||
"data": arguments[1], | ||
"params": arguments[2] | ||
}; | ||
} | ||
return this.get ("/authorized_payments/"+id, next); | ||
}; | ||
req.authenticate = req.authenticate !== false; | ||
/** | ||
Refund accredited payment | ||
@param id | ||
@return json | ||
*/ | ||
MP.prototype.refundPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var auth = Q.Promise(function(resolve, reject) { | ||
if (req.authenticate) { | ||
resolve(mp.getAccessToken()); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
return this.put ("/collections/"+id, {"status": "refunded"}, next); | ||
}; | ||
auth.then(function success(at) { | ||
if (at) { | ||
req.params || (req.params = {}); | ||
req.params.access_token = at; | ||
} | ||
/** | ||
Cancel pending payment | ||
@param id | ||
@return json | ||
*/ | ||
MP.prototype.cancelPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
MPRestClient.post(req).then ( | ||
function success (data) { | ||
next && next(null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject (err); | ||
} | ||
); | ||
}, | ||
deferred.reject); | ||
return this.put ("/collections/"+id, {"status": "cancelled"}, next); | ||
}; | ||
return deferred.promise; | ||
}; | ||
/** | ||
Cancel preapproval payment | ||
@param id | ||
@return json | ||
*/ | ||
MP.prototype.cancelPreapprovalPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
/** | ||
Generic resource put | ||
@param req | ||
@param data (deprecated) | ||
@param params (deprecated) | ||
*/ | ||
mp.put = function (req) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
return this.put ("/preapproval/"+id, {"status": "cancelled"}, next); | ||
}; | ||
if (typeof req == "string") { | ||
req = { | ||
"uri": req, | ||
"data": arguments[1], | ||
"params": arguments[2] | ||
}; | ||
} | ||
/** | ||
Search payments according to filters, with pagination | ||
@param filters | ||
@param offset | ||
@param limit | ||
@return json | ||
*/ | ||
MP.prototype.searchPayment = function (filters, offset, limit) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
req.authenticate = req.authenticate !== false; | ||
if (!isNaN(offset)) { | ||
filters.offset = offset; | ||
} | ||
if (!isNaN(limit)) { | ||
filters.limit = limit; | ||
} | ||
var auth = Q.Promise(function(resolve, reject) { | ||
if (req.authenticate) { | ||
resolve(mp.getAccessToken()); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
var uriPrefix = this.__sandbox ? "/sandbox" : ""; | ||
auth.then(function success(at) { | ||
if (at) { | ||
req.params || (req.params = {}); | ||
req.params.access_token = at; | ||
} | ||
return this.get (uriPrefix+"/collections/search", filters, next); | ||
}; | ||
MPRestClient.put(req).then ( | ||
function success (data) { | ||
next && next(null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject (err); | ||
} | ||
); | ||
}, | ||
deferred.reject); | ||
/** | ||
Create a checkout preference | ||
@param preference | ||
@return json | ||
*/ | ||
MP.prototype.createPreference = function (preference){ | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
return deferred.promise; | ||
}; | ||
return this.post ("/checkout/preferences", preference, next); | ||
}; | ||
/** | ||
Generic resource delete | ||
@param req | ||
@param params (deprecated) | ||
*/ | ||
mp.delete = function (req) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
/** | ||
Update a checkout preference | ||
@param id | ||
@param preference | ||
@return json | ||
*/ | ||
MP.prototype.updatePreference = function (id, preference) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
if (typeof req == "string") { | ||
req = { | ||
"uri": req, | ||
"params": arguments[1] | ||
}; | ||
} | ||
return this.put ("/checkout/preferences/"+id, preference, next); | ||
}; | ||
req.authenticate = req.authenticate !== false; | ||
/** | ||
Get a checkout preference | ||
@param id | ||
@param preference | ||
@return json | ||
*/ | ||
MP.prototype.getPreference = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var auth = Q.Promise(function(resolve, reject) { | ||
if (req.authenticate) { | ||
resolve(mp.getAccessToken()); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
return this.get ("/checkout/preferences/"+id, next); | ||
}; | ||
auth.then(function success(at) { | ||
if (at) { | ||
req.params || (req.params = {}); | ||
req.params.access_token = at; | ||
} | ||
/** | ||
Create a preapproval payment | ||
@param preference | ||
@return json | ||
*/ | ||
MP.prototype.createPreapprovalPayment = function (preapprovalPayment){ | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
MPRestClient.delete(req).then ( | ||
function success (data) { | ||
next && next(null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject (err); | ||
} | ||
); | ||
}, | ||
deferred.reject); | ||
return this.post ("/preapproval", preapprovalPayment) | ||
}; | ||
return deferred.promise; | ||
}; | ||
/** | ||
Get a preapproval payment | ||
@param id | ||
@param preference | ||
@return json | ||
*/ | ||
MP.prototype.getPreapprovalPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
// Backward compatibility | ||
/** | ||
Create a checkout preference | ||
@param preference | ||
@return json | ||
*/ | ||
mp.createPreference = function (preference){ | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
return this.get ("/preapproval/"+id, next); | ||
}; | ||
return mp.post ({ | ||
"uri": "/checkout/preferences", | ||
"data": preference | ||
}, next); | ||
}; | ||
/* Generic resource call methods */ | ||
/** | ||
Update a checkout preference | ||
@param id | ||
@param preference | ||
@return json | ||
*/ | ||
mp.updatePreference = function (id, preference) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
/** | ||
Generic resource get | ||
@param uri | ||
@param params | ||
@param authenticate = true | ||
*/ | ||
MP.prototype.get = function (uri, params, authenticate) { | ||
var _self = this; | ||
return mp.put ({ | ||
"uri": "/checkout/preferences/"+id, | ||
"data": preference | ||
}, next); | ||
}; | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
/** | ||
Get a checkout preference | ||
@param id | ||
@return json | ||
*/ | ||
mp.getPreference = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
params = params && typeof params == "object" ? params : {}; | ||
return mp.get ({ | ||
"uri": "/checkout/preferences/"+id | ||
},next); | ||
}; | ||
if (authenticate !== false) { | ||
this.getAccessToken () | ||
.then ( | ||
function success (accessToken) { | ||
params.access_token = accessToken; | ||
/** | ||
Create a preapproval payment | ||
@param preapprovalPayment | ||
@return json | ||
*/ | ||
mp.createPreapprovalPayment = function (preapprovalPayment){ | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
if (Object.keys(params).length > 0) { | ||
uri += (uri.indexOf ("?") >= 0 ? "&" : "?") + _self.__build_query(params); | ||
} | ||
return mp.post ({ | ||
"uri": "/preapproval", | ||
"data": preapprovalPayment | ||
}, next) | ||
}; | ||
MP.restClient.get(uri, config.MIME_JSON) | ||
.then ( | ||
function success (data) { | ||
next && next (null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
} else { | ||
if (Object.keys(params).length > 0) { | ||
uri += (uri.indexOf ("?") >= 0 ? "&" : "?") + _self.__build_query(params); | ||
} | ||
/** | ||
Update a preapproval payment | ||
@param preapprovalPayment | ||
@return json | ||
*/ | ||
mp.updatePreapprovalPayment = function (id, preapprovalPayment){ | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
MP.restClient.get(uri, config.MIME_JSON) | ||
.then ( | ||
function success (data) { | ||
next && next (null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
} | ||
return mp.put ({ | ||
"uri": "/preapproval/"+id, | ||
"data": preapprovalPayment | ||
}, next) | ||
}; | ||
return deferred.promise; | ||
}; | ||
/** | ||
Get a preapproval payment | ||
@param id | ||
@return json | ||
*/ | ||
mp.getPreapprovalPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
/** | ||
Generic resource post | ||
@param uri | ||
@param data | ||
@param params | ||
*/ | ||
MP.prototype.post = function (uri, data, params) { | ||
var _self = this; | ||
return mp.get ({ | ||
"uri": "/preapproval/"+id | ||
}, next); | ||
}; | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
/** | ||
Search payments according to filters, with pagination | ||
@param filters | ||
@param offset | ||
@param limit | ||
@return json | ||
*/ | ||
mp.searchPayment = function (filters, offset, limit) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
params = params && typeof params == "object" ? params : {}; | ||
if (!isNaN(offset)) { | ||
filters.offset = offset; | ||
} | ||
if (!isNaN(limit)) { | ||
filters.limit = limit; | ||
} | ||
this.getAccessToken () | ||
.then ( | ||
function success (accessToken) { | ||
params.access_token = accessToken; | ||
var uriPrefix = this.__sandbox ? "/sandbox" : ""; | ||
if (Object.keys(params).length > 0) { | ||
uri += (uri.indexOf ("?") >= 0 ? "&" : "?") + _self.__build_query(params); | ||
} | ||
return mp.get ({ | ||
"uri": uriPrefix+"/collections/search", | ||
"params": filters | ||
}, next); | ||
}; | ||
MP.restClient.post(uri, data, config.MIME_JSON) | ||
.then ( | ||
function success (data) { | ||
next && next (null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
/** | ||
Get information for specific payment | ||
@param id | ||
@return json | ||
*/ | ||
mp.getPayment = mp.getPaymentInfo = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
return deferred.promise; | ||
}; | ||
var uriPrefix = this.__sandbox ? "/sandbox" : ""; | ||
/** | ||
Generic resource put | ||
@param uri | ||
@param data | ||
@param params | ||
*/ | ||
MP.prototype.put = function (uri, data, params) { | ||
var _self = this; | ||
return mp.get ({ | ||
"uri": uriPrefix+"/collections/notifications/"+id | ||
}, next); | ||
}; | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
/** | ||
Get information for specific authorized payment | ||
@param id | ||
@return json | ||
*/ | ||
mp.getAuthorizedPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
params = params && typeof params == "object" ? params : {}; | ||
return mp.get ({ | ||
"uri": "/authorized_payments/"+id | ||
}, next); | ||
}; | ||
this.getAccessToken () | ||
.then ( | ||
function success (accessToken) { | ||
params.access_token = accessToken; | ||
/** | ||
Refund accredited payment | ||
@param id | ||
@return json | ||
*/ | ||
mp.refundPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
if (Object.keys(params).length > 0) { | ||
uri += (uri.indexOf ("?") >= 0 ? "&" : "?") + _self.__build_query(params); | ||
} | ||
return mp.put ({ | ||
"uri": "/collections/"+id, | ||
"data": { | ||
"status": "refunded" | ||
} | ||
}, next); | ||
}; | ||
MP.restClient.put(uri, data, config.MIME_JSON) | ||
.then ( | ||
function success (data) { | ||
next && next (null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
/** | ||
Cancel pending payment | ||
@param id | ||
@return json | ||
*/ | ||
mp.cancelPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
return deferred.promise; | ||
}; | ||
return mp.put ({ | ||
"uri": "/collections/"+id, | ||
"data": { | ||
"status": "cancelled" | ||
} | ||
}, next); | ||
}; | ||
/** | ||
Generic resource delete | ||
@param uri | ||
@param params | ||
*/ | ||
MP.prototype.delete = function (uri, params) { | ||
var _self = this; | ||
/** | ||
Cancel preapproval payment | ||
@param id | ||
@return json | ||
*/ | ||
mp.cancelPreapprovalPayment = function (id) { | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var next = typeof (arguments[arguments.length -1]) == "function" ? arguments[arguments.length -1] : null; | ||
var deferred = Q.defer(); | ||
return mp.put ({ | ||
"uri": "/preapproval/"+id, | ||
"data": { | ||
"status": "cancelled" | ||
} | ||
}, next); | ||
}; | ||
params = params && typeof params == "object" ? params : {}; | ||
// Instance return | ||
return mp; | ||
}; | ||
this.getAccessToken () | ||
.then ( | ||
function success (accessToken) { | ||
params.access_token = accessToken; | ||
MP.version = p.version; | ||
if (Object.keys(params).length > 0) { | ||
uri += (uri.indexOf ("?") >= 0 ? "&" : "?") + _self.__build_query(params); | ||
} | ||
// /*************************************************************************/ | ||
MP.restClient.delete(uri, config.MIME_JSON) | ||
.then ( | ||
function success (data) { | ||
next && next (null, data); | ||
deferred.resolve (data); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
}, | ||
function error (err) { | ||
next && next(err); | ||
deferred.reject(err); | ||
}); | ||
var MPRestClient = (function() { | ||
function buildRequest (req) { | ||
var request = {}; | ||
return deferred.promise; | ||
}; | ||
request.uri = config.API_BASE_URL + req.uri; | ||
request.method = req.method || "GET"; | ||
/*************************************************************************/ | ||
MP.prototype.__build_query = function (params) { | ||
var elements = [] | ||
req.headers || (req.headers = {}); | ||
for (var key in params) { | ||
if (params[key] == null) { | ||
params[key] = ""; | ||
request.headers = { | ||
"user-agent": "MercadoPago Node.js SDK v"+MP.version, | ||
"accept": config.MIME_JSON, | ||
"content-type": config.MIME_JSON | ||
}; | ||
Object.keys(req.headers).map(function (h) { | ||
request.headers[h.toLowerCase()] = req.headers[h]; | ||
}); | ||
if (req.data) { | ||
if (request.headers["content-type"] == config.MIME_JSON) { | ||
request.json = req.data; | ||
} else { | ||
request.form = req.data; | ||
} | ||
} | ||
elements.push(key+"="+escape(params[key])); | ||
if (req.params) { | ||
request.qs = req.params; | ||
} | ||
request.strictSSL = true; | ||
return request; | ||
} | ||
return elements.join("&"); | ||
}; | ||
MP.restClient = { | ||
__exec: function (uri, req) { | ||
function exec (req) { | ||
var deferred = Q.defer(); | ||
req.uri = config.API_BASE_URL + uri; | ||
req.headers = { | ||
"User-Agent": "MercadoPago Node.js SDK v"+MP.version, | ||
"Accept": config.MIME_JSON | ||
}; | ||
req = buildRequest(req); | ||
req.strictSSL = true; | ||
request(req, function(error, response, body) { | ||
(typeof body == "string") && (body = JSON.parse(body)); | ||
if (error) { | ||
deferred.reject (error); | ||
deferred.reject (new MercadoPagoError(error)); | ||
} else if (response.statusCode < 200 || response.statusCode >= 300) { | ||
deferred.reject (body); | ||
deferred.reject (new MercadoPagoError(body ? body.message || body : "Unknown", response.statusCode)); | ||
} else { | ||
try { | ||
(typeof body == "string") && (body = JSON.parse(body)); | ||
} catch (e) { | ||
deferred.reject(new MercadoPagoError ("Bad response")); | ||
} | ||
deferred.resolve ({ | ||
@@ -440,44 +544,36 @@ "status": response.statusCode, | ||
return deferred.promise; | ||
}, | ||
get: function (uri, contentType) { | ||
var req = { | ||
"method": "GET" | ||
} | ||
contentType == config.MIME_JSON && (req.json = true); | ||
} | ||
return this.__exec (uri, req); | ||
}, | ||
// Instance creation | ||
var restclient = {}; | ||
post: function (uri, data, contentType) { | ||
var req = { | ||
"method": "POST" | ||
} | ||
restclient.get = function (req) { | ||
req.method = "GET"; | ||
contentType == config.MIME_JSON && (req.json = data); | ||
contentType == config.MIME_FORM && (req.form = data); | ||
return exec(req); | ||
}; | ||
return this.__exec (uri, req); | ||
}, | ||
restclient.post = function (req) { | ||
req.method = "POST"; | ||
put: function (uri, data, contentType) { | ||
var req = { | ||
"method": "PUT" | ||
} | ||
contentType == config.MIME_JSON && (req.json = data); | ||
contentType == config.MIME_FORM && (req.form = data); | ||
return exec(req); | ||
}; | ||
return this.__exec (uri, req); | ||
}, | ||
restclient.put = function (req) { | ||
req.method = "PUT"; | ||
delete: function (uri, contentType) { | ||
var req = { | ||
"method": "DELETE" | ||
} | ||
contentType == config.MIME_JSON && (req.json = true); | ||
return exec(req); | ||
}; | ||
return this.__exec (uri, req); | ||
} | ||
}; | ||
restclient.delete = function (req) { | ||
req.method = "DELETE"; | ||
return exec(req); | ||
}; | ||
return restclient; | ||
})(); | ||
module.exports = MP; | ||
module.exports.MercadoPagoError = MercadoPagoError; |
{ | ||
"name" : "mercadopago", | ||
"version" : "0.3.3", | ||
"author" : "Horacio Casatti <horacio.casatti@mercadolibre.com>", | ||
"description" : "Mercadopago SDK module for Payments integration", | ||
"name": "mercadopago", | ||
"version": "0.5.0", | ||
"author": "Horacio Casatti <horacio.casatti@mercadolibre.com>", | ||
"description": "Mercadopago SDK module for Payments integration", | ||
"main": "./lib/mercadopago.js", | ||
"scripts":{ | ||
"test": "node ./tests/unitTest" | ||
"scripts": { | ||
"test": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- -R spec" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mercadopago/sdk-nodejs.git" | ||
"type": "git", | ||
"url": "https://github.com/mercadopago/sdk-nodejs.git" | ||
}, | ||
"keywords": [ | ||
"api" | ||
, "mercadopago" | ||
, "checkout" | ||
, "payment" | ||
, "ipn" | ||
, "sdk" | ||
, "integration" | ||
"api", | ||
"mercadopago", | ||
"checkout", | ||
"payment", | ||
"ipn", | ||
"sdk", | ||
"integration" | ||
], | ||
"dependencies" : { | ||
"q": ">=1.1.2", | ||
"request": ">=2.48.0" | ||
"dependencies": { | ||
"q": ">=1.1.2", | ||
"request": ">=2.48.0" | ||
}, | ||
"devDependencies": { | ||
"vows": ">=0.8.1" | ||
"istanbul": "^0.3.17", | ||
"mocha": "^2.2.5" | ||
}, | ||
"engine" : { | ||
"node" : ">=0.6" | ||
"engine": { | ||
"node": ">=0.6" | ||
} | ||
} | ||
} |
@@ -174,4 +174,6 @@ # MercadoPago SDK module for Payments integration | ||
```javascript | ||
mp.post ("/v1/payments", payment_data) | ||
.then (...); | ||
mp.post ({ | ||
"uri": "/v1/payments", | ||
"data": payment_data | ||
}).then (...); | ||
``` | ||
@@ -182,4 +184,8 @@ | ||
```javascript | ||
mp.post ("/v1/customers", {"email": "email@test.com"}) | ||
.then (...); | ||
mp.post ({ | ||
"uri": "/v1/customers", | ||
"data": { | ||
"email": "email@test.com" | ||
} | ||
}).then (...); | ||
``` | ||
@@ -190,4 +196,5 @@ | ||
```javascript | ||
mp.get ("/v1/customers/CUSTOMER_ID") | ||
.then (...); | ||
mp.get ({ | ||
"uri": "/v1/customers/CUSTOMER_ID" | ||
}).then (...); | ||
``` | ||
@@ -205,16 +212,48 @@ | ||
You can access any other resource from the MercadoPago API using the generic methods: | ||
You can access any resource from the [MercadoPago API](https://api.mercadopago.com) using the generic methods. | ||
The basic structure is: | ||
`mp.method(request).then(...)` | ||
where `request` can be: | ||
```javascript | ||
{ | ||
"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: | ||
```javascript | ||
// Get a resource, with optional URL params. Also you can disable authentication for public APIs | ||
mp.get ("/resource/uri", [params], [authenticate=true]); | ||
mp.get ({ | ||
"uri": "/resource/uri", | ||
"params": {params}, | ||
"authenticate": true | ||
}); | ||
// Create a resource with "data" and optional URL params. | ||
mp.post ("/resource/uri", data, [params]); | ||
mp.post ({ | ||
"uri": "/resource/uri", | ||
"data": data, | ||
"params": {params} | ||
}); | ||
// Update a resource with "data" and optional URL params. | ||
mp.put ("/resource/uri", data, [params]); | ||
mp.put ({ | ||
"uri": "/resource/uri", | ||
"data": data, | ||
"params": {params} | ||
}); | ||
// Delete a resource with optional URL params. | ||
mp.delete ("/resource/uri", [params]); | ||
mp.delete ({ | ||
"uri": "/resource/uri", | ||
"params": {params} | ||
}); | ||
``` | ||
@@ -225,6 +264,8 @@ | ||
```javascript | ||
mp.get ("/sites", null, false) | ||
.then (function (sites) { | ||
console.log (sites); | ||
}); | ||
mp.get ({ | ||
"uri": "/sites", | ||
"authenticate": false | ||
}).then (function (sites) { | ||
console.log (sites); | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37270
24
985
267
2
1