Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/IuguMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function iuguMethod(spec) {

for (var i = 0, l = urlParams.length; i < l; ++i) {
var arg = args[0];
if (urlParams[i] && !arg) {
if (urlParams[i] && (!arg && arg !== 0)) {
throw new Error('Iugu: I require argument "' + urlParams[i] + '", but I got: ' + arg);
}
urlData[urlParams[i]] = args.shift();
Expand Down
190 changes: 115 additions & 75 deletions lib/IuguResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var http = require('http');
var https = require('https');
var path = require('path');
var when = require('when');

var request = require('request');
var utils = require('./utils');
var Error = require('./Error');

Expand Down Expand Up @@ -46,10 +46,10 @@ IuguResource.prototype = {

createFullPath: function(commandPath, urlData) {
return path.join(
this.basePath(urlData),
this.path(urlData),
typeof commandPath == 'function' ?
commandPath(urlData) : commandPath
this.basePath(urlData),
this.path(urlData),
typeof commandPath == 'function' ?
commandPath(urlData) : commandPath
).replace(/\\/g, '/'); // ugly workaround for Windows
},

Expand All @@ -65,19 +65,19 @@ IuguResource.prototype = {
},

createDeferred: function(callback) {
var deferred = when.defer();

if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
setTimeout(function(){ callback(null, res) }, 0);
}, function(err) {
setTimeout(function(){ callback(err, null); }, 0);
});
}
var deferred = when.defer();

if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
setTimeout(function(){ callback(null, res) }, 0);
}, function(err) {
setTimeout(function(){ callback(err, null); }, 0);
});
}

return deferred;
return deferred;
},

_timeoutHandler: function(timeout, req, callback) {
Expand All @@ -90,12 +90,12 @@ IuguResource.prototype = {
req.abort();

callback.call(
self,
new Error.IuguConnectionError({
message: 'Request aborted due to timeout being reached (' + timeout + 'ms)',
detail: timeoutErr
}),
null
self,
new Error.IuguConnectionError({
message: 'Request aborted due to timeout being reached (' + timeout + 'ms)',
detail: timeoutErr
}),
null
);
}
},
Expand Down Expand Up @@ -123,13 +123,13 @@ IuguResource.prototype = {
}
} catch (e) {
return callback.call(
self,
new Error.IuguAPIError({
message: 'Invalid JSON received from the Iugu API',
response: response,
exception: e
}),
null
self,
new Error.IuguAPIError({
message: 'Invalid JSON received from the Iugu API',
response: response,
exception: e
}),
null
);
}
callback.call(self, null, response);
Expand All @@ -142,71 +142,111 @@ IuguResource.prototype = {
return function(error) {
if (req._isAborted) return; // already handled
callback.call(
self,
new Error.IuguConnectionError({
message: 'An error occurred with our connection to Iugu',
detail: error
}),
null
self,
new Error.IuguConnectionError({
message: 'An error occurred with our connection to Iugu',
detail: error
}),
null
);
}
},

_request: function(method, path, data, auth, callback) {

var requestData = utils.stringifyRequestData(data || {});
var self = this;
console.log(requestData);

var host = self._iugu.getApiField('host');
var protocol = self._iugu.getApiField('protocol') === 'http' ? 'http' : 'https';
var port = self._iugu.getApiField('port');
var apiVersion = this._iugu.getApiField('version');


var headers = {
// Use specified auth token or use default from this stripe instance:
'Authorization': auth ?
'Basic ' + new Buffer(auth + ':').toString('base64') :
this._iugu.getApiField('auth'),
'Basic ' + new Buffer(auth + ':').toString('base64') :
this._iugu.getApiField('auth'),
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': requestData.length,
'User-Agent': 'Iugu/v1 NodeBindings/' + this._iugu.getConstant('PACKAGE_VERSION')
'Content-Type': 'application/json'
};

if (apiVersion) {
headers['Iugu-Version'] = apiVersion;
}
var uri = protocol + "://" + host + ':' + port + '/' + path;

// Grab client-user-agent before making the request:
this._iugu.getClientUserAgent(function(cua) {
headers['X-Iugu-Client-User-Agent'] = cua;
makeRequest();
});

function makeRequest() {
var options = {
uri: uri,
method: method,
json: true,
headers: headers
};

var timeout = self._iugu.getApiField('timeout');
if ((method === 'POST' || method === 'PUT')&& data){
options.body = data;
}

var req = (
self._iugu.getApiField('protocol') == 'http' ? http : https
).request({
host: self._iugu.getApiField('host'),
port: self._iugu.getApiField('port'),
path: path,
method: method,
headers: headers
});

req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
req.on('response', self._responseHandler(req, callback));
req.on('error', self._errorHandler(req, callback));
request(options,callback);
console.log(data);

req.on('socket', function(socket) {
socket.on('secureConnect', function() {
req.write(requestData);
req.end();
});
});
}

}

}
//_request: function(method, path, data, auth, callback) {
//
// //var requestData = utils.stringifyRequestData(data || {});
// var requestData = data;
// var self = this;
// console.log(requestData);
// var apiVersion = this._iugu.getApiField('version');
// var headers = {
// // Use specified auth token or use default from this stripe instance:
// 'Authorization': auth ?
// 'Basic ' + new Buffer(auth + ':').toString('base64') :
// this._iugu.getApiField('auth'),
// 'Accept': 'application/json',
// 'Content-Type': 'application/json',
// //'Content-Type': 'application/x-www-form-urlencoded',
// 'Content-Length': requestData.length,
// 'User-Agent': 'Iugu/v1 NodeBindings/' + this._iugu.getConstant('PACKAGE_VERSION')
// };
//
// if (apiVersion) {
// headers['Iugu-Version'] = apiVersion;
// }
//
// // Grab client-user-agent before making the request:
// this._iugu.getClientUserAgent(function(cua) {
// headers['X-Iugu-Client-User-Agent'] = cua;
// makeRequest();
// });
//
// function makeRequest() {
//
// var timeout = self._iugu.getApiField('timeout');
//
// var req = (
// self._iugu.getApiField('protocol') == 'http' ? http : https
// ).request({
// host: self._iugu.getApiField('host'),
// port: self._iugu.getApiField('port'),
// path: path,
// method: method,
// headers: headers
// });
//
// req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
// req.on('response', self._responseHandler(req, callback));
// req.on('error', self._errorHandler(req, callback));
//
// req.on('socket', function(socket) {
// socket.on('secureConnect', function() {
// req.write(requestData);
// req.end();
// });
// });
//
// }
//
//}

};

Expand Down
1 change: 1 addition & 0 deletions lib/iugu.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var resources = {
Subscriptions: require('./resources/Subscriptions'),
PaymentToken: require('./resources/PaymentToken'),
Transfers: require('./resources/Transfers'),
WithDrawRequests: require('./resources/WithDrawRequests'),

// The following rely on pre-filled customer IDs:
CustomerPaymentMethods: require('./resources/CustomerPaymentMethods'),
Expand Down
6 changes: 0 additions & 6 deletions lib/resources/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ module.exports = IuguResource.extend({
method: 'POST',
path: '{accountId}/request_verification',
urlParams: ['accountId']
}),

request_withdraw: iuguMethod({
method: 'POST',
path: '{accountId}/request_withdraw',
urlParams: ['accountId']
})

});
8 changes: 8 additions & 0 deletions lib/resources/Invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ module.exports = IuguResource.extend({
urlParams: ['invoiceId']
}),


listPaginated : iuguMethod({
method: 'GET',
path: '?limit={limit}&start={start}&created_at_from={created_at_from}&created_at_to={created_at_to}',
urlParams: ['limit','start','created_at_from','created_at_to']
})


});
14 changes: 14 additions & 0 deletions lib/resources/WithDrawRequests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var IuguResource = require('../IuguResource');
var iuguMethod = IuguResource.method;

module.exports = IuguResource.extend({

path: 'withdraw_requests',
includeBasic: ['list' , 'retrieve'],



});

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iugu",
"version": "0.0.6",
"version": "0.0.8",
"description": "Biblioteca Iugu para Node.js",
"homepage": "https://github.com/iugu/iugu-node",
"author": {
Expand All @@ -24,6 +24,9 @@
"mocha-as-promised": "~1.4.0"
},
"dependencies": {
"continuation-local-storage": "^3.1.4",
"request": "^2.67.0",
"request-promise": "^1.0.2",
"when": "~3.1.0"
},
"scripts": {
Expand Down
Loading