Skip to content

Commit

Permalink
added send, request, and register
Browse files Browse the repository at this point in the history
  • Loading branch information
nanek committed Dec 3, 2011
1 parent a2424a8 commit 360ed2c
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
env.sh
.DS_Store
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Requires your Dwolla application client_id and client_secret.

* basicAccountInfo(client_id, client_secret, id, callback)
* nearby(client_id, client_secret, lat, lon, [, params], callback)
* register(client_id, client_secret, userInfo, callback)

Requires a valid user OAuth2 token. Note tokens do not expire and may be
reused. See https://github.com/bnoguchi/everyauth for an example on how
Expand All @@ -18,6 +19,8 @@ to authorize a user and get a Dwolla OAuth2 token.
* transactions(oauth_token[, params], callback)
* transactionById(oauth_token, id, callback)
* transactionsStats(oauth_token[, params], callback)
* send(oauth_token, pin, destinationId, amount[, params], callback)
* request(oauth_token, pin, sourceId, amount[, params], callback)

All optional parameters are passed in as an optional object before the callback.

Expand All @@ -26,7 +29,7 @@ All optional parameters are passed in as an optional object before the callback.
$ npm install dwolla

## Example Usage
See examples.js.
See more examples in the examples folder.

var dwolla = require('dwolla');

Expand All @@ -51,7 +54,10 @@ See examples.js.
});

## Tests
Tests use mocha and should
Tests use mocha and should.js. Tests were made only for GET requests,
as tests of POST requests would be processed just like real requests.
Although working examples of each POST request can be found in the
examples folder.

$ npm test

Expand Down
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
token: process.env.DWOLLA_OAUTH_TOKEN || null
, client_id: process.env.DWOLLA_CLIENT_ID || null
, client_secret: process.env.DWOLLA_CLIENT_SECRET || null
, pin: process.env.DWOLLA_PIN || null
}
File renamed without changes.
14 changes: 14 additions & 0 deletions examples/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var dwolla = require('../lib/dwolla');
var c = require('../config');

if (c.pin === null) return 'Missing DWOLLA_PIN';
if (c.token === null) return 'Missing DWOLLA_TOKEN';

var sourceId = 'shiff2kl@yahoo.com';
var amount = 1;
var params = { sourceType: 'Email' };
dwolla.request(c.token, c.pin, sourceId, amount, params, function(err, tran) {
console.log(err);
console.log(tran);
});

14 changes: 14 additions & 0 deletions examples/send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var dwolla = require('../lib/dwolla');
var c = require('../config');

if (c.pin === null) return 'Missing DWOLLA_PIN';
if (c.token === null) return 'Missing DWOLLA_TOKEN';

var destinationId = 'shiff2kl@yahoo.com';
var amount = 1;
var params = { destinationType: 'Email' };
dwolla.send(c.token, c.pin, destinationId, amount, params, function(err, tran) {
console.log(err);
console.log(tran);
});

110 changes: 109 additions & 1 deletion lib/dwolla.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,44 @@ function _request(path, params, fn) {
fn(data.Message, null);
}
} catch (e) {
fn('Error parsing response from dwolla api.', data);
fn('Error parsing response from Dwolla API.', data);
}
});
});
req.end();
}

function _post(path, post_data, fn) {
var options = {
host: 'www.dwolla.com'
, path: API_PATH + path
, method: 'POST'
, headers: {
'Content-Type': 'application/json'
}
};
var req = https.request(options, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
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.', data);
}
});
});
req.write(JSON.stringify(post_data));
req.end();
}

/**
* Retrieves the basic account information for the Dwolla account associated with the account identifier.
* https://www.dwolla.com/developers/endpoints/users/basicinformation
Expand Down Expand Up @@ -200,3 +231,80 @@ exports.transactionsStats = function(oauth_token, params, fn) {
params.oauth_token = oauth_token;
_request('/transactions/stats', params, fn);
};

/**
* Send funds to a destination user for the user associated with the authorized access token.
* https://www.dwolla.com/developers/endpoints/transactions/send
*
* Optional params:
*
* - destinationType
* - facilitatorAmount
* - assumeCosts
* - notes
*
* @param {String} oauth_token
* @param {Number} pin
* @param {String} destinationId
* @param {String} amount
* @param {Function} fn
*/
exports.send = function(oauth_token, pin, destinationId, amount, params, fn) {
// params are optional
if (!fn || typeof params === 'function') {
fn = params;
params = {};
}
params = params || {};
params.oauth_token = oauth_token;
params.pin = pin;
params.destinationId = destinationId;
params.amount = amount;
_post('/transactions/send', params, fn);
};

/**
* Request funds from a source user for the user associated with the authorized access token.
* https://www.dwolla.com/developers/endpoints/transactions/request
*
* Optional params:
*
* - sourceType
* - facilitatorAmount
* - notes
*
* @param {String} oauth_token
* @param {Number} pin
* @param {String} sourceId
* @param {String} amount
* @param {Function} fn
*/
exports.request = function(oauth_token, pin, sourceId, amount, params, fn) {
// params are optional
if (!fn || typeof params === 'function') {
fn = params;
params = {};
}
params = params || {};
params.oauth_token = oauth_token;
params.pin = pin;
params.sourceId= sourceId;
params.amount = amount;
_post('/transactions/request', params, fn);
};

/**
* Register a new Dwolla user account.
* https://www.dwolla.com/developers/endpoints/register/user
*
* @param {String} client_id
* @param {String} client_secret
* @param {Object} userInfo
* @param {Function} fn
*/
exports.register = function(client_id, client_secret, userInfo, fn) {
var params = {};
params.client_id = client_id;
params.client_secret = client_secret;
_post('/register/?' + qs.stringify(params), userInfo, fn);
};
1 change: 1 addition & 0 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
token: process.env.DWOLLA_OAUTH_TOKEN || null
, client_id: process.env.DWOLLA_CLIENT_ID || null
, client_secret: process.env.DWOLLA_CLIENT_SECRET || null
, pin: process.env.DWOLLA_PIN || null
}
9 changes: 0 additions & 9 deletions test/register.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,4 @@ describe('Transactions', function() {
});
});
});
describe('Send', function() {
it('should return a transaction number');
});
describe('Request', function() {
it('should return a transaction number');
});
});

0 comments on commit 360ed2c

Please sign in to comment.