From 21539cf94107ef058fa3f7c76398ac956a04adc4 Mon Sep 17 00:00:00 2001 From: Kenan Shifflett Date: Thu, 1 Dec 2011 13:14:51 -0500 Subject: [PATCH] updated to use new Dwolla API endpoints and response format --- README.md | 2 +- example.js | 4 ++-- lib/dwolla.js | 64 ++++++++++++++++++++++++++++++--------------------- package.json | 2 +- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index f4deaaa..88732c0 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ client_secret to make API requests. Set these as env variables. // get oauth_token, be sure to set the proper scope // use oauth lib or everyauth to setup OAuth2 - // see nanek/everyauth for example Dwolla authentication + // see everyauth for example Dwolla authentication var token = req.session.oauth_token; dwolla.fullAccountInfo(token, function(err, data) { diff --git a/example.js b/example.js index fea7359..314baff 100644 --- a/example.js +++ b/example.js @@ -1,6 +1,6 @@ var dwolla = require('./lib/dwolla'); -var accountId = '812-111-1111'; //use valid id +var accountId = '812-728-8151'; //'812-111-1111'; //use valid id dwolla.basicAccountInfo(accountId, function(err, data) { if (err) { console.log(err); } console.log(data); @@ -10,5 +10,5 @@ var lat = '41.58454600'; var lon = '-93.63416700'; dwolla.nearby(lat, lon, null, function(err, data) { if (err) { console.log(err); } - console.log(data); + console.log(data.length); }); diff --git a/lib/dwolla.js b/lib/dwolla.js index fed5f22..9639f3a 100644 --- a/lib/dwolla.js +++ b/lib/dwolla.js @@ -2,19 +2,19 @@ var https = require('https'); var qs = require('querystring'); var keys = { - client_id: process.env.DWOLLA_CLIENT_ID - , client_secret: process.env.DWOLLA_CLIENT_SECRET + client_id: process.env.DWOLLA_CLIENT_ID || null + , client_secret: process.env.DWOLLA_CLIENT_SECRET || null }; var defaultOptions = { host: 'www.dwolla.com', }; -var ACCOUNT_API_PATH = '/oauth/rest/accountapi'; +var API_PATH = '/oauth/rest'; function _request(path, params, fn) { var options = defaultOptions; - options.path = ACCOUNT_API_PATH + path; + options.path = API_PATH + path; if (params) { options.path += '?' + qs.stringify(params); } @@ -24,8 +24,16 @@ function _request(path, params, fn) { data += chunk; }); res.on('end', function() { - data = JSON.parse(data); - fn(null, data); + try { + data = JSON.parse(data); + if (data.Success) { + fn(null, data.Response); + } else { + fn(data.Message, null); + } + } catch (e) { + fn('Error parsing response from dwolla api.', null); + } }); }); req.end(); @@ -33,34 +41,34 @@ function _request(path, params, fn) { /** * Retrieves the basic account information for the Dwolla account associated with the account identifier. - * https://www.dwolla.com/developers/endpoints/accountapi/accountinformation + * https://www.dwolla.com/developers/endpoints/users/basicinformation **/ exports.basicAccountInfo = function(id, fn) { - var path = '/accountinformation/' + id; + var path = '/users/' + id; _request(path, keys, fn); }; /** * Retrieves the account information for the user assoicated with the authorized access token. - * https://www.dwolla.com/developers/endpoints/accountapi/accountinformation + * https://www.dwolla.com/developers/endpoints/users/accountinformation **/ exports.fullAccountInfo = function(oauth_token, fn) { var params = { oauth_token: oauth_token }; - _request('/accountinformation', params, fn); + _request('/users/', params, fn); }; /** * Retrieves the account balance for the user for the authorized access token. - * https://www.dwolla.com/developers/endpoints/accountapi/balance + * https://www.dwolla.com/developers/endpoints/balance/account * */ exports.balance = function(oauth_token, fn) { var params = { oauth_token: oauth_token }; - _request('/balance', params, fn); + _request('/balance/', params, fn); }; /** * Retrieves contacts for the user for the authorized access token. - * https://www.dwolla.com/developers/endpoints/accountapi/contacts + * https://www.dwolla.com/developers/endpoints/contacts/user * * @param {String} search * @param {String} types @@ -69,14 +77,14 @@ exports.balance = function(oauth_token, fn) { exports.contacts = function(oauth_token, params, fn) { params = params || {}; params.oauth_token = oauth_token; - _request('/contacts', params, fn); + _request('/contacts/', params, fn); }; /** * Retrieves nearby Dwolla spots within the range of the provided latitude and longitude. * Half of the limit are returned as spots with closest proximity. The other half of the spots * are returned as random spots within the range. - * https://www.dwolla.com/developers/endpoints/accountapi/nearby + * https://www.dwolla.com/developers/endpoints/contacts/nearby * * @param {String} lat * @param {String} long @@ -84,31 +92,35 @@ exports.contacts = function(oauth_token, params, fn) { * @param {int} limit **/ exports.nearby = function(lat, lon, params, fn) { - params = params || {}; - params.client_id = keys.client_id; - params.client_secret = keys.client_secret; - params.latitude = lat; - params.longitude = lon; + if (keys.client_id !== null) { + params = params || {}; + params.client_id = keys.client_id; + params.client_secret = keys.client_secret; + params.latitude = lat; + params.longitude = lon; - _request('/nearby', params, fn); + _request('/contacts/nearby', params, fn); + } else { + console.log('Missing client_id and client_secret'); + } }; /** * Retrieves transactions for the user for the authorized access token. * Transactions are returned in descending order by transaction date. - * https://www.dwolla.com/developers/endpoints/accountapi/transactions + * https://www.dwolla.com/developers/endpoints/transactions/details * * @param {int} transactionId **/ exports.transactionById = function(oauth_token, id, fn) { var params = { oauth_token: oauth_token }; - _request('/transactions' + id, params, fn); + _request('/transactions/' + id, params, fn); }; /** * Retrieves transactions for the user for the authorized access token. * Transactions are returned in descending order by transaction date. - * https://www.dwolla.com/developers/endpoints/accountapi/transactions + * https://www.dwolla.com/developers/endpoints/transactions/list * * @param {String} sinceDate * @param {String} types @@ -118,12 +130,12 @@ exports.transactionById = function(oauth_token, id, fn) { exports.transactions = function(oauth_token, params, fn) { params = params || {}; params.oauth_token = oauth_token; - _request('/transactions', params, fn); + _request('/transactions/', params, fn); }; /** * Retrieves transactions stats for the user for the authorized access token. - * https://www.dwolla.com/developers/endpoints/accountapi/transactions + * https://www.dwolla.com/developers/endpoints/transactions/stats * * @param {String} types * @param {String} startDate diff --git a/package.json b/package.json index 3056dec..a516308 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Kenan Shifflett ", "name": "dwolla", "description": "Dwolla API for node.js", - "version": "0.0.1", + "version": "0.0.2", "repository": { "type": "git", "url": "git@github.com:nanek/node-dwolla.git"