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
39 changes: 33 additions & 6 deletions lib/services/Invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,49 @@ function InvoiceService(config) {
this.config = config;
}

/* http://developers.freshbooks.com/docs/invoices/#invoice.sendByEmail */
InvoiceService.prototype.sendByEmail = function(id, cb) { //cb(err, client)
var data = {
invoice_id: id
},
options = {
method: 'invoice.sendByEmail',
url: this.config.url,
token: this.config.token
};

utils.callAPI(data, options, function(err, result) {

if (err) {
cb(err, null);
}
else if (!result) {
cb(null, null);
}
else {
cb(null, result.client || null);
}
});
};

/* http://developers.freshbooks.com/docs/invoices/#invoice.list */
InvoiceService.prototype.list = function(cb) { //cb(err, invoice, metaData)
InvoiceService.prototype.list = function(data, cb) { //cb(err, invoice, metaData)

var options = {
method: 'invoice.list',
url: this.config.url,
token: this.config.token
};

var filters;
if (typeof arguments[1] === 'function') {
cb = arguments[1];
filters = arguments[0];
var filters = data;
var callback = cb;

if (typeof data === 'function') {
callback = data;
filters = {};
}

utils.callAPI(null, options, function(err, result) {
utils.callAPI(filters, options, function(err, result) {

if (err) {
cb(err, null);
Expand Down
11 changes: 5 additions & 6 deletions lib/services/Payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ function PaymentService(config) {
}

/* http://developers.freshbooks.com/docs/payments/#payment.list */
PaymentService.prototype.list = function(cb) { //cb(err, payment, metaData)
PaymentService.prototype.list = function(filters, cb) { //cb(err, payment, metaData)

var options = {
method: 'payment.list',
url: this.config.url,
token: this.config.token
};

var filters;
if (typeof arguments[1] === 'function') {
cb = arguments[1];
filters = arguments[0];
if (typeof arguments[0] === 'function') {
cb = arguments[0];
filters = {};
}

utils.callAPI(null, options, function(err, result) {
utils.callAPI(filters, options, function(err, result) {

if (err) {
cb(err, null);
Expand Down
23 changes: 9 additions & 14 deletions lib/services/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

var xml2js = require('xml2js'),
request = require('request'),
builder = require('xmlbuilder'),
fs = require('fs');

function callAPI(data, options, cb) {
Expand All @@ -13,24 +14,18 @@ function callAPI(data, options, cb) {
options = {};
}

data.$ = {
method: options.method
};

var buildOpts = {
rootName: 'request',
xmldec: {
version: '1.0',
encoding: 'UTF-8'
}
};

var builder = new xml2js.Builder(buildOpts);
data['@method'] = options.method;

var xmlStr;
try {
xmlStr = builder.buildObject(data);
xmlStr = builder.create({
request: data
}, {
version: '1.0',
encoding: 'UTF-8'
}).toString();
}

catch(e) {
cb(e, null);
return;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/hashdog/node-freshbooks-api#readme",
"dependencies": {
"request": "^2.60.0",
"xmlbuilder": "4.2.1",
"xml2js": "^0.4.10"
},
"devDependencies": {
Expand Down