svb-ach
Version:
Helper API to issue ACH money transfers
104 lines (92 loc) • 2.47 kB
JavaScript
//
// svb-ach
// index.js
//
// For full documentation: http://docs.svbplatform.com/authentication/
// Email apisupport@svb.com for help!
//
const _genericGet = function(client, type, id, callback) {
let url = ['/v1', type, id];
if (!id) {
url.pop();
}
client.get(url.join('/'), '', (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
};
const _genericPatch = function(client, type, id, data, callback) {
client.patch(['/v1', type, id].join('/'), data, (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
};
const _genericPost = function(client, type, data, callback) {
client.post(['/v1', type].join('/'), data, (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
};
function SVBach(client) {
if (!client) {
throw Error('SVB ACH helper requires an SVB Client object');
}
this.client = client;
}
SVBach.prototype = {
create: function(achData, callback) {
_genericPost(this.client, 'ach', achData, callback);
},
get: function(achID, callback) {
_genericGet(this.client, 'ach', achID, callback);
},
updateStatus: function(achID, achStatus, callback) {
if (typeof achStatus === 'object') {
if (achStatus.status) {
achStatus = achStatus.status;
} else {
throw Error('send an ACH status string or { status: status } parameter');
}
}
_genericPatch(this.client, 'ach', achID, { status: achStatus }, callback);
},
find: function(filters, callback) {
let filterURL = [];
if (filters.status) {
filterURL.push('filter%5Bstatus%5D=' + filters.status);
}
if (filters.effective_date) {
filterURL.push('filter%5Beffective_date%5D=' + filters.effective_date);
}
if (!filterURL.length) {
throw Error('send an ACH filter object ({ status: status } or { effective_date: "YYYY-MM-DD" })');
}
this.client.get('/v1/ach', filterURL.join('&'), (err, body) => {
if (err) {
return callback(err);
}
if (body && body.data) {
callback(null, body.data);
}
});
},
filtered: function(filters, callback) {
this.find(filters, callback);
},
all: function(callback) {
this.get('', callback);
}
};
module.exports = SVBach;