From acac0ebd3e9af4c68a8ca073e1bdb7a0440085b1 Mon Sep 17 00:00:00 2001 From: itswisdomagain Date: Fri, 29 Sep 2017 08:27:46 +0100 Subject: [PATCH 01/11] 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 02/11] 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 03/11] 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 04/11] 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 05/11] 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 06/11] 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 07/11] 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; From 28b69115d2d53f79bff37b2b641f31f86432aea1 Mon Sep 17 00:00:00 2001 From: subomi Date: Thu, 12 Oct 2017 14:33:28 +0100 Subject: [PATCH 08/11] Made huge change with promstream.js --- index.js | 40 ++++++++------------------ lib/promstream.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++ resources/bank.js | 5 ++++ test/bank.js | 29 +++++++++++-------- test/customer.js | 5 ++-- 5 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 lib/promstream.js diff --git a/index.js b/index.js index 4a1e739..695743e 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,14 @@ Paystack API wrapper */ 'use strict'; +const process = require('process'); var request = require('request'), baseUrl = 'https://api.paystack.co', acceptedMethods = [ "get", "post", "put" ], - Promise = require('promise') + root = 'https://api.paystack.co', + promstream = require('./lib/promstream') ; var resources = { @@ -21,7 +23,13 @@ var resources = { subaccount: require('./resources/subaccount'), settlements: require('./resources/settlements'), misc: require('./resources/misc'), - bank: require('./resources/bank') + bank: require('./resources/bank'), + transfer: require('./resources/transfer'), + transferrecipient: require('./resources/transferRecipient'), + transfercontrol: require('./resources/transferControl'), + bulkCharge: require('.resources/bulkCharge'), + charge: require('.resources/charge') + controlPanel: require('.resources/controlPanel') } function Paystack(key) { @@ -138,33 +146,7 @@ Paystack.prototype = { if (qs) options.qs = qs; - return new Promise(function (fulfill, reject){ - request(options, function(error, response, body) { - // return body - if (error){ - reject(error); - } - else if(!body.status){ - // Error from API?? - error = body; - body = null; - reject(error); - } - else{ - fulfill(body); - } - }); - }).then(function(value) { - if(callback) { - return callback(null, value); - } - return value; - }).catch(function(reason) { - if(callback) { - return callback(reason, null); - } - return reason; - }); + return new promstream(options, callback); } }, diff --git a/lib/promstream.js b/lib/promstream.js new file mode 100644 index 0000000..f48951b --- /dev/null +++ b/lib/promstream.js @@ -0,0 +1,71 @@ +"use strict"; +// Customized interface for promisifying & streaming a http data source with requests. +const request = require('request') +const Promise = require('promise') + +module.exports = promstream + +function promstream(options, callback) { + + options.encoding = "utf8" + + function promstream() {} + + var httpRequest; + + let datasource = new Promise(function(fulfill, reject){ + httpRequest = request(options, function callback(error, response, body) { + if (error){ + // Network Error + reject(error); + } + else if(!body.status){ + // API Error + error = body; + body = null; + reject(error); + } + else{ + fulfill(body); + } + }) + }) + + // Promise Interface + promstream.prototype.then = function (onFulfillment, onRejection) { + return datasource.then(onFulfillment, onRejection) + } + + promstream.prototype.catch = function (fn) { + return datasource.catch(fn) + } + + promstream.prototype.reject = function (error) { + return new Promise.reject(error) + } + + promstream.prototype.resolve = function (data) { + return new Promise.resolve(data) + } + + // Stream Interface + promstream.prototype.on = function (eventName, callback) { + httpRequest.on(eventName, callback) + } + + promstream.prototype.pipe = function(destSrc) { + httpRequest.pipe(destSrc) + } + + promstream.prototype.unpipe = function() { + httpRequest.unpipe() + } + + ///let mypromstream = new promstream(); + + if(callback) { + return datasource.then(callback, undefined) + } + + return new promstream(); //mypromstream; +} \ No newline at end of file diff --git a/resources/bank.js b/resources/bank.js index 6c39305..326d84a 100644 --- a/resources/bank.js +++ b/resources/bank.js @@ -21,6 +21,11 @@ module.exports = { resolveBin: { method: 'get', path: '/decision/bin/{bin}' + }, + + resolveBvn: { + method: 'get', + path: [root, '/resolve_bvn/{bvn}'].join('') } } diff --git a/test/bank.js b/test/bank.js index 90a0db5..24b1932 100644 --- a/test/bank.js +++ b/test/bank.js @@ -21,19 +21,14 @@ describe("Paystack Bank Related Functions", function() { // Resolve a Bin Card it("should resolve an account number", function(done) { var queryParams = { - account_number: '0022728151', - bank_code: '063' + account_number: '2009687327', + bank_code: '057' }; paystack.bank.resolveAccountNumber(queryParams) - .then(function(body){ - /** uncomment following 3 lines if above details are valid **/ - // expect(body).to.have.property('data'); - // expect(body.data).to.have.property('account_number'); - // expect(body.data).to.have.property('account_name'); - - /** comment out following 2 lines if above details are valid **/ - expect(body).to.have.property('status'); - expect(body).to.have.property('message'); + .then(function(body){ + expect(body).to.have.property('data'); + expect(body.data).to.have.property('account_number'); + expect(body.data).to.have.property('account_name'); done(); }) .catch(function(error){ @@ -55,4 +50,16 @@ describe("Paystack Bank Related Functions", function() { }); }); + // Resolve a Bvn + it("should resolve a bvn number", function(done) { + paystack.bank.resolveBvn(21212917741) + .then(function(body){ + expect(body.data).to.have.property('bvn'); + done(); + }) + .catch(function(error){ + return done(error); + }); + }); + }); diff --git a/test/customer.js b/test/customer.js index df73cff..384082b 100644 --- a/test/customer.js +++ b/test/customer.js @@ -3,7 +3,7 @@ var paystack = require('../index')(process.env.KEY) , expect = require('chai').expect ; -describe("Paystack Customers", function() { +describe("Paystack Customers using promises", function() { var customer_id; @@ -31,7 +31,7 @@ describe("Paystack Customers", function() { first_name: 'Opeyemi', last_name: 'Obembe', email: 'kehers@gmail.com' - }, function(error, body) { + }, function(body) { // callback should parse response and return an object return {'name': 'subomi'}; }).then(function(body) { @@ -102,4 +102,5 @@ describe("Paystack Customers", function() { }); }); */ + }); From 86d86a89cbf909f8357ba363970fb78f4d978482 Mon Sep 17 00:00:00 2001 From: subomi Date: Thu, 12 Oct 2017 14:35:19 +0100 Subject: [PATCH 09/11] added new resources, but not tested yet --- resources/bulkCharge.js | 55 ++++++++++++++++++++ resources/charge.js | 71 ++++++++++++++++++++++++++ resources/controlPanel.js | 23 +++++++++ resources/transfer.js | 52 +++++++++++++++++++ resources/transferControl.js | 47 ++++++++++++++++++ resources/transferRecipient.js | 23 +++++++++ test/transfer.js | 91 ++++++++++++++++++++++++++++++++++ test/transferRecipient.js | 71 ++++++++++++++++++++++++++ 8 files changed, 433 insertions(+) create mode 100644 resources/bulkCharge.js create mode 100644 resources/charge.js create mode 100644 resources/controlPanel.js create mode 100644 resources/transfer.js create mode 100644 resources/transferControl.js create mode 100644 resources/transferRecipient.js create mode 100644 test/transfer.js create mode 100644 test/transferRecipient.js diff --git a/resources/bulkCharge.js b/resources/bulkCharge.js new file mode 100644 index 0000000..d8714b3 --- /dev/null +++ b/resources/bulkCharge.js @@ -0,0 +1,55 @@ +'use strict'; + +var root = '/bulkcharge'; + +module.exports = { + + /* + Initiate a bulk charge + */ + initiate: { + method: 'post', + path: root + }, + + /* + List Batches + */ + list: { + method: 'get', + path: root + }, + + /* + Get Batch + */ + getBatch: { + method: 'get', + path: [root, '/{id}'].join('') + }, + + /* + Get Charges in Batch + */ + getCharges: { + method: 'get', + path: [root, '/{id}/charges'].join('') + } + + /* + Pause Batch + */ + pauseBatch: { + method: 'get', + path: [root, '/pause/{id}'].join('') + }, + + /* + Resume Batch + */ + resumeBatch: { + method: 'get', + path: [root, '/resume/{id}'].join('') + } + +}; diff --git a/resources/charge.js b/resources/charge.js new file mode 100644 index 0000000..000d2b0 --- /dev/null +++ b/resources/charge.js @@ -0,0 +1,71 @@ +'use strict'; + +var root = '/charge'; + +module.exports = { + + /* + Tokenize payment instrument before a charge + */ + tokenize: { + method: 'post', + path: [root, '/tokenize'].join(''), + params: ['email*', 'card*', 'card.number*', 'card.cvv*', 'card.expiry_month*', 'card.expiry_year*'] + }, + + /* + Charge a bank/card/authorization_code + */ + charge: { + method: 'get', + path: root, + params: ['email*', 'amount*', + [ + ['card*', 'card.number*', 'card.cvv*', 'card.expiry_month*', 'card.expiry_year*'], + ['bank*', 'bank.code*', 'bank.account_number*'], + "authorization_code*" + ] + ] + }, + + /* + Submit Pin + */ + submitPin: { + method: 'post', + path: [root, '/submit_pin'].join(''), + params: ['pin*', 'reference*'] + }, + + /* + Submit OTP + */ + submitOtp: { + method: 'post', + path: [root, '/submit_otp'].join('') + } + + /* + Submit Phone + */ + submitPhone: { + method: 'post', + path: [root, '/submit_phone'].join('') + }, + + /* + Submit Birthday + */ + submitBirthday: { + method: 'post', + path: [root, '/submit_birthday'].join('') + } + + /* + Check charge status + */ + chargeStatus: { + method: 'get', + path: [root, '/{id}'].join('') + } +}; diff --git a/resources/controlPanel.js b/resources/controlPanel.js new file mode 100644 index 0000000..de9d9d3 --- /dev/null +++ b/resources/controlPanel.js @@ -0,0 +1,23 @@ +'use strict'; + +var root = '/integration'; + +module.exports = { + + /* + Fetch Payment Session Timeout + */ + fetchPaymentTimeout: { + method: 'get', + path: [root, '/payment_session_timeout'].join('') + }, + + /* + Update Payment Session Timeout + */ + updatePaymentTimeout: { + method: 'put', + path: [root, '/payment_session_timeout'].join('') + } + +}; diff --git a/resources/transfer.js b/resources/transfer.js new file mode 100644 index 0000000..fbeb197 --- /dev/null +++ b/resources/transfer.js @@ -0,0 +1,52 @@ +'use strict'; + +var root = '/transfer'; + +module.exports = { + + /* + Initiate a transfer + @param: source, amount, recipient + */ + initiate: { + method: 'post', + path: root, + params: ['source*', 'amount*', 'recipient*'] + }, + + /* + List transfers + */ + list: { + method: 'get', + path: root + }, + + /* + Get transfers + */ + get: { + method: 'get', + path: [root, '/{id}'].join('') + }, + + /* + Finalize transfer + @param: transfer_code, otp + */ + finalize: { + method: 'post', + path: [root, '/finalize_transfer'].join(''), + params: ['transfer_code*', 'otp*'] + }, + + /* + White/Blacklist customer + @param: customer, risk_action ('allow' to whitelist or 'deny' to blacklist) + */ + initiateBulkTransfer: { + method: 'post', + path: [root, '/bulk'].join(''), + params: ['source*'] + } +}; diff --git a/resources/transferControl.js b/resources/transferControl.js new file mode 100644 index 0000000..8d8a1ef --- /dev/null +++ b/resources/transferControl.js @@ -0,0 +1,47 @@ +'use strict'; + +var root = '/transfer'; + +module.exports = { + /* + check your balance + */ + checkBalance: { + method: 'get', + path: root, + }, + + /* + Resend OTP for transfer + */ + resendOtp: { + method: 'post', + path: [root, '/resend_otp'].join(''), + params: ['transfer_code*', 'reason*'] + }, + + /* + Disable OTP + */ + disableOtp: { + method: 'post', + path: [root, '/disable_otp'].join('') + }, + + /* + Finalize Disabling OTP + */ + finalizeDisableOtp: { + method: 'post', + path: [root, '/disable_otp_finalize'], + params: ['otp*'] + }, + + /* + Enable OTP + */ + enableOtp: { + method: 'post', + path: [root, '/enable_otp'].join('') + } +}; \ No newline at end of file diff --git a/resources/transferRecipient.js b/resources/transferRecipient.js new file mode 100644 index 0000000..65b8e0f --- /dev/null +++ b/resources/transferRecipient.js @@ -0,0 +1,23 @@ +'use strict'; + +var root = '/transferrecipient'; + +module.exports = { + /* + Create a New recipient + */ + create: { + method: 'post', + path: root, + params: ['type*', 'name*', 'account_number*', 'bank_code*'] + }, + + /* + List Recipients + */ + list: { + method: 'get', + endpoint: root, + } + +}; diff --git a/test/transfer.js b/test/transfer.js new file mode 100644 index 0000000..38331df --- /dev/null +++ b/test/transfer.js @@ -0,0 +1,91 @@ +var paystack = require('../index')(process.env.KEY) + , mocha = require('mocha') + , expect = require('chai').expect + ; + +describe("Paystack Transfers", function() { + + let reference; + + // Init Transaction + it("should initialize a transfer", function(done) { + paystack.transfer.initiate({ + source: 'balance', + reason: 'Calm down', + recipient: 'reference', + + amount: 500000 + }) + .then(function(body) { + expect(body).to.have.property('data'); + expect(body.data).to.have.property('authorization_url'); + expect(body.data).to.have.property('access_code'); + expect(body.data).to.have.property('reference'); + + reference = body.data.reference; + + done(); + }) + .catch(function(error) { + done(error); + }) + }); + + // Verify Transaction + it("should verify a transaction", function(done) { + paystack.transaction.verify(reference, function(error, body) { + + if (error) + return done(error); + + expect(body).to.have.property('data'); + expect(body.data).to.be.an('object'); + + done(); + }); + }); + + // Fetch Transaction + // No transaction id :/ + /* + it("should get details of a transaction", function(done) { + paystack.transaction.get(transaction_id, function(error, body) { + + if (error) + return done(error); + + expect(body).to.have.property('data'); + + done(); + }); + }); + //*/ + + // List Transactions + it("should list transaction", function(done) { + paystack.transaction.list(function(error, body) { + + if (error) + return done(error); + + expect(body).to.have.property('data'); + expect(body.data).to.be.instanceof(Array); + + done(); + }); + }); + + // Export Transactions + it("should export transaction", function(done) { + paystack.transaction.export(function(error, body) { + + if (error) + return done(error); + + expect(body).to.have.property('data'); + + done(); + }); + }); +}); + diff --git a/test/transferRecipient.js b/test/transferRecipient.js new file mode 100644 index 0000000..1ceb4a6 --- /dev/null +++ b/test/transferRecipient.js @@ -0,0 +1,71 @@ +var paystack = require('../index')(process.env.KEY) + , mocha = require('mocha') + , expect = require('chai').expect + ; + +describe("Paystack Transfer Recipients", function() { + + var reference; + + // Create Transaction + it("should create a transfer recipient", function(done) { + paystack.transfer_recipient.create({ + type: 'nuban', + name: "Subomi Oluwalana", + account_number: "2009687727", + bank_code: "057", + currency: "NGN" + }, function(error, body) { + + if (error) { + reference = null; + return done(error); + } + reference = body.data.recipient_code; + + expect(body).to.have.property('data'); + expect(body.data).to.have.property('integration'); + done(); + }); + }); + + it("should list all recipients", function(done) { + paystack.transfer_recipient.list(function(error, body) { + if(error) + return done(error); + + expect(body).to.have.property('data'); + expect(body.data).to.be.an("array"); + done(); + }); + }); + + it("should fetch a particular transfer recipients", function(done) { + + paystack.transfer_recipient.fetch(reference, function(error, body) { + if(error) { + return done(error); + } + expect(body).to.have.property('data'); + expect(reference).to.equal(body.data.recipient_code); + done(); + }); + + + }); + + it("should update a transfer recipient record", function(done) { + paystack.transfer_recipient.update(reference, { + type: 'nuban', + name: "Shubby", + account_number: "2009687327", + bank_code: "057", + currency: "NGN" + }, function(error, body) { + done(); + }); + }); + +}); + + From 8efe75221aae9eac9d97d242b8cc9b56ee0dd333 Mon Sep 17 00:00:00 2001 From: subomi Date: Thu, 12 Oct 2017 14:39:03 +0100 Subject: [PATCH 10/11] Added error parameters in test/customer.js on line 34 Added '/' in the path resources in index.js on lines 29-32 --- index.js | 6 +++--- test/customer.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 695743e..2226bc3 100644 --- a/index.js +++ b/index.js @@ -27,9 +27,9 @@ var resources = { transfer: require('./resources/transfer'), transferrecipient: require('./resources/transferRecipient'), transfercontrol: require('./resources/transferControl'), - bulkCharge: require('.resources/bulkCharge'), - charge: require('.resources/charge') - controlPanel: require('.resources/controlPanel') + bulkCharge: require('./resources/bulkCharge'), + charge: require('./resources/charge') + controlPanel: require('./resources/controlPanel') } function Paystack(key) { diff --git a/test/customer.js b/test/customer.js index 384082b..87d1495 100644 --- a/test/customer.js +++ b/test/customer.js @@ -31,7 +31,7 @@ describe("Paystack Customers using promises", function() { first_name: 'Opeyemi', last_name: 'Obembe', email: 'kehers@gmail.com' - }, function(body) { + }, function(error, body) { // callback should parse response and return an object return {'name': 'subomi'}; }).then(function(body) { From d49c5feb9c810f1afc6584b78c83375d16883668 Mon Sep 17 00:00:00 2001 From: subomi Date: Thu, 12 Oct 2017 14:50:03 +0100 Subject: [PATCH 11/11] Fixed minor bugs, but transfers still need fixing --- index.js | 2 +- resources/bulkCharge.js | 8 ++++---- resources/charge.js | 7 ++++--- test/transfer.js | 5 ++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 2226bc3..8102212 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,7 @@ var resources = { transferrecipient: require('./resources/transferRecipient'), transfercontrol: require('./resources/transferControl'), bulkCharge: require('./resources/bulkCharge'), - charge: require('./resources/charge') + charge: require('./resources/charge'), controlPanel: require('./resources/controlPanel') } diff --git a/resources/bulkCharge.js b/resources/bulkCharge.js index d8714b3..91eb0b7 100644 --- a/resources/bulkCharge.js +++ b/resources/bulkCharge.js @@ -10,7 +10,7 @@ module.exports = { initiate: { method: 'post', path: root - }, + }, /* List Batches @@ -18,7 +18,7 @@ module.exports = { list: { method: 'get', path: root - }, + }, /* Get Batch @@ -34,7 +34,7 @@ module.exports = { getCharges: { method: 'get', path: [root, '/{id}/charges'].join('') - } + }, /* Pause Batch @@ -42,7 +42,7 @@ module.exports = { pauseBatch: { method: 'get', path: [root, '/pause/{id}'].join('') - }, + }, /* Resume Batch diff --git a/resources/charge.js b/resources/charge.js index 000d2b0..5605bb8 100644 --- a/resources/charge.js +++ b/resources/charge.js @@ -43,7 +43,7 @@ module.exports = { submitOtp: { method: 'post', path: [root, '/submit_otp'].join('') - } + }, /* Submit Phone @@ -51,7 +51,7 @@ module.exports = { submitPhone: { method: 'post', path: [root, '/submit_phone'].join('') - }, + }, /* Submit Birthday @@ -59,7 +59,7 @@ module.exports = { submitBirthday: { method: 'post', path: [root, '/submit_birthday'].join('') - } + }, /* Check charge status @@ -68,4 +68,5 @@ module.exports = { method: 'get', path: [root, '/{id}'].join('') } + }; diff --git a/test/transfer.js b/test/transfer.js index 38331df..76d140f 100644 --- a/test/transfer.js +++ b/test/transfer.js @@ -12,8 +12,7 @@ describe("Paystack Transfers", function() { paystack.transfer.initiate({ source: 'balance', reason: 'Calm down', - recipient: 'reference', - + recipient: '', amount: 500000 }) .then(function(body) { @@ -87,5 +86,5 @@ describe("Paystack Transfers", function() { done(); }); }); -}); +});