Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
updated to use new Dwolla API endpoints and response format
  • Loading branch information
nanek committed Dec 1, 2011
1 parent c8e10c0 commit 21539cf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions 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);
Expand All @@ -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);
});
64 changes: 38 additions & 26 deletions lib/dwolla.js
Expand Up @@ -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);
}
Expand All @@ -24,43 +24,51 @@ 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();
}

/**
* 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
Expand All @@ -69,46 +77,50 @@ 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
* @param {int} range
* @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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Kenan Shifflett <kenan.shifflett@gmail.com>",
"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"
Expand Down

0 comments on commit 21539cf

Please sign in to comment.