From acac0ebd3e9af4c68a8ca073e1bdb7a0440085b1 Mon Sep 17 00:00:00 2001 From: itswisdomagain Date: Fri, 29 Sep 2017 08:27:46 +0100 Subject: [PATCH 1/7] added bank resource to , optimized variable names --- index.js | 141 ++++++++++++++++++++------------------ resources/bank.js | 26 +++++++ resources/customer.js | 14 ++-- resources/misc.js | 11 ++- resources/page.js | 22 +++--- resources/plan.js | 12 ++-- resources/settlements.js | 2 +- resources/subaccount.js | 14 ++-- resources/subscription.js | 11 ++- resources/transaction.js | 18 +++-- test/bank.js | 58 ++++++++++++++++ 11 files changed, 204 insertions(+), 125 deletions(-) create mode 100644 resources/bank.js create mode 100644 test/bank.js diff --git a/index.js b/index.js index 529eaf0..8f152d2 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,8 @@ Paystack API wrapper var request = require('request'), - root = 'https://api.paystack.co', + baseUrl = 'https://api.paystack.co', + acceptedMethods = [ "get", "post", "put" ], Promise = require('promise') ; @@ -19,7 +20,8 @@ var resources = { subscription: require('./resources/subscription'), subaccount: require('./resources/subaccount'), settlements: require('./resources/settlements'), - misc: require('./resources/misc') + misc: require('./resources/misc'), + bank: require('./resources/bank') } function Paystack(key) { @@ -33,9 +35,10 @@ function Paystack(key) { Paystack.prototype = { - extend: function(params) { + extend: function(endpoint) { // This looks more sane. - var self = this; + var secretKey = this.key; + return function(){ // Convert argument to array var args = new Array(arguments.length); @@ -44,90 +47,88 @@ Paystack.prototype = { args[i] = arguments[i]; } - // Check for callback & Pull it out from the array + // Check if last argument is supplied and is a valid callback function & Pull it out from the array var callback = l > 0 && typeof args.slice(l-1)[0] === "function" ? args.splice(l-1)[0] : undefined; - var body, qs; - - // quick fix - method checking - var method = params.method in {"get":'', "post":'', "put":''} - ? params.method - : (function () { throw new Error("Method not Allowed! - Resource declaration error") })() - var endpoint = [root, params.endpoint].join(''); - // Checking for required params; - if(params.params) { - var paramList = params.params; - - // Pull body passed - var body = args.length === 2 ? args[1] : args[0]; - paramList.filter(function(item, index, array) { - if(item.indexOf("*") === -1) { - // Not required - return; - } - item = item.replace("*", ""); - - if(!(item in body)) { - throw new Error("Required Parameters Ommited - " + item); - } - return; - - }); + // method checking + if (acceptedMethods.indexOf(endpoint.method) < 0) { + throw new Error("Method - " + endpoint.method + " - not Allowed! - Resource declaration error") } - // Get arguments in endpoint e.g {id} in customer/{id} and pull - // out from array - var argsInEndpoint = endpoint.match(/{[^}]+}/g); + var method = endpoint.method; + var url = [baseUrl, endpoint.path].join(''); + + // First check path parameters (e.g {id} in customer/{id}) before checking post body or query string paramters + // Pull out all path parameters from url into array + var argsInEndpoint = url.match(/{[^}]+}/g); if (argsInEndpoint) { l = argsInEndpoint.length; // Do we have one or more? if (l > 0) { - // Confirm resource declaration good - if (!Array.isArray(params.args)) { - // error - throw new Error('Resource declaration error'); - } - // Confirm user passed the argument to method // and replace in endpoint - - var match, index; for (var i=0;i Date: Fri, 29 Sep 2017 08:50:26 +0100 Subject: [PATCH 2/7] updated readme --- README.md | 76 ++++++++++++++++++++++++++++++++++------------- resources/bank.js | 4 +-- test/bank.js | 6 ++-- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3c12195..8cc32df 100644 --- a/README.md +++ b/README.md @@ -19,16 +19,16 @@ var paystack = require('paystack')('secret_key'); The resource methods accepts are promisified, but can receive optional callback as the last argument. ```js -// First Option -// paystack.{resource}.{method} +// First Option (with callback) +// paystack.{resource}.{method}(callback) paystack.customer.list(function(error, body) { console.log(error); console.log(body); }); ``` ```js -// Second Option -// paystack.{resource} +// Second Option (as promise) +// paystack.{resource}.{method}.then().catch() paystack.customer.list() .then(function(body) { console.log(body); @@ -40,7 +40,28 @@ paystack.customer.list() -For resource methods that use POST or PUT, the JSON body can be passed as the first argument. +For GET endpoints with url path parameters (e.g. https://api.paystack.co/plan/{id_or_plan_code}), pass path parameter as string or number +Separate path parameter values by comma if more than 1 path parameter and place them in order as they appear in the url path. + +```js +paystack.plan.get(90) + .then(function(error, body) { + console.log(error); + console.log(body); + }); +``` + +For GET endpoints with query string parameters (e.g. https://api.paystack.co/bank/resolve?account_number=0022728151&bank_code=063), pass paramaters as object. + +```js +paystack.bank.resolve_account_number({account_number: '0022778151', bank_code: '063'}) + .then(function(error, body) { + console.log(error); + console.log(body); + }); +``` + +For POST or PUT endpoints, the JSON body should be passed as the first argument. ```js paystack.plan.create({ @@ -54,25 +75,18 @@ paystack.plan.create({ }); ``` -For GET, you can pass the required ID as string and optional parameters as an optional object argument. - -```js -paystack.plan.get(90) - .then(function(error, body) { - console.log(error); - console.log(body); - }); -``` +For POST or PUT endpoints, if the endpoint also has path parameters (e.g. https://api.paystack.co/customer/{id_or_customer_code}), pass the path parameters as explained above, before passing the JSON body object. ```js -paystack.transactions.list({perPage: 20}) - .then(function(error, body) { - console.log(error); - console.log(body); - }); +var customer_id = 100; +paystack.customer.update(customer_id, {last_name: 'Kehers'}) + .then(function(error, body) { + console.log(error); + console.log(body); + }); ``` -### Resources +### Resources and Methods - customer - create @@ -108,11 +122,31 @@ paystack.transactions.list({perPage: 20}) - list - listBanks - update +- bank + - list + - resolveAccountNumber + - resolveBin - Miscellanous - list_banks - resolve_bin - +To use any endpoint, call +```js +//using callback function +paystack.{resource}.{method}(function(err, body){ + console.log(error); + console.log(body); +}); + +//or as promise +paystack.{resource}.{method} + .then(function(body) { + console.log(body); + }) + .catch(function(error) { + console.log(error); + }); +``` ### Contributing - To ensure consistent code style, please follow the [editorconfig rules](http://obem.be/2015/06/01/a-quick-note-on-editorconfig.html) in .editorconfig diff --git a/resources/bank.js b/resources/bank.js index 90045f2..6c39305 100644 --- a/resources/bank.js +++ b/resources/bank.js @@ -12,13 +12,13 @@ module.exports = { params: ['perPage', 'page'] }, - resolve_account_number: { + resolveAccountNumber: { method: 'get', path: [root, '/resolve'].join(''), params: ['account_number*', 'bank_code*'] }, - resolve_bin: { + resolveBin: { method: 'get', path: '/decision/bin/{bin}' } diff --git a/test/bank.js b/test/bank.js index 33b5f87..90a0db5 100644 --- a/test/bank.js +++ b/test/bank.js @@ -24,7 +24,7 @@ describe("Paystack Bank Related Functions", function() { account_number: '0022728151', bank_code: '063' }; - paystack.bank.resolve_account_number(queryParams) + paystack.bank.resolveAccountNumber(queryParams) .then(function(body){ /** uncomment following 3 lines if above details are valid **/ // expect(body).to.have.property('data'); @@ -42,8 +42,8 @@ describe("Paystack Bank Related Functions", function() { }); // Resolve a Bin Card - it("should resolve a bin card", function(done) { - paystack.bank.resolve_bin(539983) + it("should resolve a card bin", function(done) { + paystack.bank.resolveBin(539983) .then(function(body){ expect(body).to.have.property('data'); expect(body.data).to.have.property('bin'); From 028ca86fea65d74ff8f6154d9d4b633b1f897f3d Mon Sep 17 00:00:00 2001 From: subomi Date: Wed, 11 Oct 2017 13:52:48 +0100 Subject: [PATCH 3/7] checking customer.js tests --- test/customer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/customer.js b/test/customer.js index 2a7e41c..0c979fd 100644 --- a/test/customer.js +++ b/test/customer.js @@ -15,6 +15,7 @@ describe("Paystack Customers", function() { email: 'kehers@gmail.com' }) .then(function(body){ + console.log(body) expect(body).to.have.property('data'); expect(body.data).to.have.property('id'); customer_id = body.data.id; @@ -60,9 +61,9 @@ describe("Paystack Customers", function() { it("should get a customer's details", function(done) { paystack.customer.get(customer_id) .then(function(body){ + done(); expect(body).to.have.property('data'); expect(body.data).to.have.property('id'); - done(); }) .catch(function(error){ return done(error); From 9787122bb1bf951883dda6fc2c05dac8aaf19037 Mon Sep 17 00:00:00 2001 From: subomi Date: Wed, 11 Oct 2017 14:11:28 +0100 Subject: [PATCH 4/7] still testing customer.js --- package.json | 2 +- test/customer.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1dc55d0..169aaa0 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Paystack API wrapper", "main": "index.js", "scripts": { - "test": "./node_modules/.bin/mocha ./test/*.js --reporter spec --timeout 8000" + "test": "./node_modules/.bin/mocha ./test/customer.js --reporter spec --timeout 8000" }, "keywords": [ "payment", diff --git a/test/customer.js b/test/customer.js index 0c979fd..10c2929 100644 --- a/test/customer.js +++ b/test/customer.js @@ -8,7 +8,7 @@ describe("Paystack Customers", function() { var customer_id; // New Customer - it("should create a new customer", function(done) { + it("create a new customer", function(done) { paystack.customer.create({ first_name: 'Opeyemi', last_name: 'Obembe', @@ -27,16 +27,16 @@ describe("Paystack Customers", function() { }); // To test callback & then chaining - it("create new customer, parse in callback and enter then handler", function(done) { + it("create a new customer, parse in callback and enter then handler", function(done) { paystack.customer.create({ first_name: 'Opeyemi', last_name: 'Obembe', email: 'kehers@gmail.com' }, function(error, body) { // callback should parse response and return an object - return {'name': 'subomi'}; + return {'name': 'subomi'}; }).then(function(body) { - // callback is called, but then handler does not show its processing, but returns initial api response + // callback is called, and it returns a processed that to the then handler expect(body).to.have.property('name') done(); }).catch(function(error) { From e5f1e276b51afa99d0361ed42387123ba85cc2e8 Mon Sep 17 00:00:00 2001 From: subomi Date: Wed, 11 Oct 2017 14:13:35 +0100 Subject: [PATCH 5/7] console.log in index.js --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8f152d2..faed4eb 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,7 @@ function Paystack(key) { return new Paystack(key); } - this.key = key; + this.key = key || process.env["PAYSTACK_SECRET_KEY"]; this.importResources(); } @@ -140,6 +140,7 @@ Paystack.prototype = { return new Promise(function (fulfill, reject){ request(options, function(error, response, body) { + console.log(body) // return body if (error){ reject(error); From 638a81de1967fd206915fa549c2a418da2776a4a Mon Sep 17 00:00:00 2001 From: subomi Date: Wed, 11 Oct 2017 14:18:45 +0100 Subject: [PATCH 6/7] debugging from index.js --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index faed4eb..1bc1485 100644 --- a/index.js +++ b/index.js @@ -140,12 +140,13 @@ Paystack.prototype = { return new Promise(function (fulfill, reject){ request(options, function(error, response, body) { - console.log(body) // return body if (error){ + console.log("From index == > error") reject(error); } else if(!body.status){ + console.log("From index == > API error") // Error from API?? error = body; @@ -153,6 +154,7 @@ Paystack.prototype = { reject(error); } else{ + console.log("From index == > successful") fulfill(body); } }); From 3be59ccce3d8454b1218caca281e3f197b1a08f5 Mon Sep 17 00:00:00 2001 From: subomi Date: Wed, 11 Oct 2017 15:10:15 +0100 Subject: [PATCH 7/7] restored tests --- index.js | 6 +----- package.json | 2 +- test/customer.js | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 1bc1485..4a1e739 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ Paystack API wrapper 'use strict'; -var +var request = require('request'), baseUrl = 'https://api.paystack.co', acceptedMethods = [ "get", "post", "put" ], @@ -142,19 +142,15 @@ Paystack.prototype = { request(options, function(error, response, body) { // return body if (error){ - console.log("From index == > error") reject(error); } else if(!body.status){ - console.log("From index == > API error") - // Error from API?? error = body; body = null; reject(error); } else{ - console.log("From index == > successful") fulfill(body); } }); diff --git a/package.json b/package.json index 169aaa0..1dc55d0 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Paystack API wrapper", "main": "index.js", "scripts": { - "test": "./node_modules/.bin/mocha ./test/customer.js --reporter spec --timeout 8000" + "test": "./node_modules/.bin/mocha ./test/*.js --reporter spec --timeout 8000" }, "keywords": [ "payment", diff --git a/test/customer.js b/test/customer.js index 10c2929..df73cff 100644 --- a/test/customer.js +++ b/test/customer.js @@ -14,8 +14,7 @@ describe("Paystack Customers", function() { last_name: 'Obembe', email: 'kehers@gmail.com' }) - .then(function(body){ - console.log(body) + .then(function(body){ expect(body).to.have.property('data'); expect(body.data).to.have.property('id'); customer_id = body.data.id;