diff --git a/lib/services/Invoice.js b/lib/services/Invoice.js index 55dd16e..add5b61 100644 --- a/lib/services/Invoice.js +++ b/lib/services/Invoice.js @@ -15,8 +15,33 @@ 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', @@ -24,13 +49,15 @@ InvoiceService.prototype.list = function(cb) { //cb(err, invoice, metaData) 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); diff --git a/lib/services/Payment.js b/lib/services/Payment.js index c2013c2..7475b36 100644 --- a/lib/services/Payment.js +++ b/lib/services/Payment.js @@ -16,7 +16,7 @@ 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', @@ -24,13 +24,12 @@ PaymentService.prototype.list = function(cb) { //cb(err, payment, metaData) 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); diff --git a/lib/services/utils.js b/lib/services/utils.js index 99a4ffa..7f0fdcb 100644 --- a/lib/services/utils.js +++ b/lib/services/utils.js @@ -1,6 +1,7 @@ var xml2js = require('xml2js'), request = require('request'), + builder = require('xmlbuilder'), fs = require('fs'); function callAPI(data, options, cb) { @@ -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; diff --git a/package.json b/package.json index a30b703..3f3dcf5 100644 --- a/package.json +++ b/package.json @@ -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": {