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
79 changes: 68 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ var url = require("url")

module.exports = function(api_secret, marketplace_id) {

if (api_secret === undefined){throw new Error("missing required api_secret")}
if (marketplace_id === undefined){throw new Error("missing required marketplace_id")}

var client = function(method, uri, json, cb) {

//make json param optional
Expand All @@ -31,6 +28,36 @@ module.exports = function(api_secret, marketplace_id) {
})
}

//needle should equal things like "account", "marketplace", "bank_account", etc.
var getId = function(haystack, needle) {

//It would appear that we were passed an actual ID, not a URI
if(haystack.indexOf("/") < 0){
return haystack
}

//All of Balanced's APIs end their resources with an s. Like "bank_accounts" instead of "bank_account"
if(needle.substr(needle.length - 1) !== "s") {
needle = needle + "s"
}

var patt = new RegExp("/"+needle+"/([^/]+)")
var match = patt.exec(haystack)

if(match !== null && match.length === 2){
return match[1]
} else {
return false
}
}


if (api_secret === undefined){throw new Error("missing required api_secret")}
if (marketplace_id === undefined){throw new Error("missing required marketplace_id")}

if(!(marketplace_id = getId(marketplace_id, 'marketplace'))){throw new Error('invalid marketplace_id')}


return {

account: {
Expand All @@ -47,8 +74,11 @@ module.exports = function(api_secret, marketplace_id) {

if(typeof card_info_or_id === "object"){
var card = {card:card_info_or_id}
}else{
}else if(card_info_or_id = getId(card_info_or_id, 'card')){
var card = {card_uri:"/v1/marketplaces/"+marketplace_id+"/cards/"+card_info_or_id}
}else{
if(typeof(cb) === "function"){cb("Invalid Card Id")}
return false
}

client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, card, cb)
Expand All @@ -58,14 +88,22 @@ module.exports = function(api_secret, marketplace_id) {
//debits the accounts card
debit: function(account_id, debit, cb){

client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/debits", debit, cb)
if(account_id = getId(account_id, "account")){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/debits", debit, cb)
}else{
if(typeof(cb) === "function"){cb("Invalid account id")}
}

},

//puts a hold on the accounts card
hold: function(account_id, hold, cb){

client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/holds", hold, cb)
if(account_id = getId(account_id, "account")){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/holds", hold, cb)
}else{
if(typeof(cb) === "function"){cb("Invalid account id")}
}

},

Expand All @@ -74,8 +112,11 @@ module.exports = function(api_secret, marketplace_id) {

if(typeof bank_info_or_id === "object"){
var bank = {bank_account:bank_info_or_id}
}else{
}else if(bank_info_or_id = getId(bank_info_or_id, "bank_account")){
var bank = {bank_account_uri:"/v1/marketplaces/"+marketplace_id+"/bank_accounts/"+bank_info_or_id}
}else{
if(typeof(cb) === "function"){cb("Invalid bank account")}
return false
}

client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, bank, cb)
Expand All @@ -85,28 +126,44 @@ module.exports = function(api_secret, marketplace_id) {
//credits accounts bank account
credit: function(account_id, credit, cb){

client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/credits", credit, cb)
if(account_id = getId(account_id, "account")){
client("POST", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/credits", credit, cb)
}else{
if(typeof(cb) === "function"){cb("Invalid account id")}
}

},

//adds extra deatils for underwriting purposes
underwrite: function(account_id, underwriting_info, cb){

client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, {merchant: underwriting_info}, cb)
if(account_id = getId(account_id, "account")){
client("PUT", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, {merchant: underwriting_info}, cb)
}else{
if(typeof(cb) === "function"){cb("Invalid account id")}
}

},

//returns account details
get: function(account_id, cb){

client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, cb)
if(account_id = getId(account_id, "account")){
client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id, cb)
}else{
if(typeof(cb) === "function"){cb("Invalid account id")}
}

},

//returns object of recent credits and debits for the account
transactions: function(account_id, cb){

client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/transactions", cb)
if(account_id = getId(account_id, "account")){
client("GET", "/v1/marketplaces/"+marketplace_id+"/accounts/"+account_id+"/transactions", cb)
}else{
if(typeof(cb) === "function"){cb("Invalid account id")}
}

}
},
Expand Down
51 changes: 51 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ var client = function(method, uri, json, cb) {
})
}

//needle should equal things like "account", "marketplace", "bank_account", etc.
var getId = function(haystack, needle) {

//It would appear that we were passed an actual ID, not a URI
if(haystack.indexOf("/") < 0){
return haystack
}

//All of Balanced's APIs end their resources with an s. Like "bank_accounts" instead of "bank_account"
if(needle.substr(needle.length - 1) !== "s") {
needle = needle + "s"
}

var patt = new RegExp("/"+needle+"/([^/]+)")
var match = patt.exec(haystack)

if(match !== null && match.length === 2){
return match[1]
} else {
return false
}
}


before(function(done){

Expand Down Expand Up @@ -98,6 +121,34 @@ before(function(done){
//TESTS
describe('balanced', function(){

//GLOBAL
describe('.getId', function(){

var testURI = "/v1/marketplaces/TEST-MP6cl1VoBnJWpjxzJspa6h4K/accounts/AC6cns438ARcVrzYKO71cw9Y/refunds/12345"

it('should get a marketplace ID', function(done){
var marketId = getId(testURI, 'marketplace')
assert.equal(marketId, "TEST-MP6cl1VoBnJWpjxzJspa6h4K", "marketplace id is incorrect")

done()
})

it('should not get a nonsense ID', function(done){
var nonsenseId = getId(testURI, 'nonsense')
assert.equal(nonsenseId, false, 'nonsense ID returned something')

done()
})

it('should return a base ID', function(done){
var baseId = getId("TEST-MP6cl1VoBnJWpjxzJspa6h4K", "marketplace")
assert.equal(baseId, "TEST-MP6cl1VoBnJWpjxzJspa6h4K", "base marketplace id is incorrect")

done()
})

})


//ACCOUNT
describe('.account', function(){
Expand Down