├── .editorconfig ├── .gitignore ├── .travis.yml ├── README.md ├── index.js ├── package.json ├── resources ├── customer.js ├── misc.js ├── page.js ├── plan.js ├── settlements.js ├── subaccount.js ├── subscription.js └── transaction.js └── test ├── customer.js ├── misc.js ├── page.js ├── plan.js ├── settlements.js ├── subaccount.js ├── subscription.js └── transaction.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | responses.json 2 | demo.js 3 | /node_modules 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6.1" 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Paystack ![Build status](https://travis-ci.org/theslyone/node-paystack.svg?branch=master) 2 | 3 | Nodejs API wrapper for [Paystack](https://paystack.co/). 4 | 5 | ### Installation 6 | 7 | ``` 8 | npm install paystack 9 | ``` 10 | 11 | ### Usage 12 | 13 | ```js 14 | // Require the library 15 | var paystack = require('paystack')('secret_key'); 16 | ``` 17 | 18 | #### Making calls to the resources 19 | The resource methods accepts are promisified, but can receive optional callback as the last argument. 20 | 21 | ```js 22 | // First Option 23 | // paystack.{resource}.{method} 24 | paystack.customer.list(function(error, body) { 25 | console.log(error); 26 | console.log(body); 27 | }); 28 | ``` 29 | ```js 30 | // Second Option 31 | // paystack.{resource} 32 | paystack.customer.list() 33 | .then(function(body) { 34 | console.log(body); 35 | }) 36 | .catch(function(error) { 37 | console.log(error); 38 | }); 39 | ``` 40 | 41 | 42 | 43 | For resource methods that use POST or PUT, the JSON body can be passed as the first argument. 44 | 45 | ```js 46 | paystack.plan.create({ 47 | name: 'API demo', 48 | amount: 10000, 49 | interval: 'monthly' 50 | }) 51 | .then(function(error, body) { 52 | console.log(error); 53 | console.log(body); 54 | }); 55 | ``` 56 | 57 | For GET, you can pass the required ID as string and optional parameters as an optional object argument. 58 | 59 | ```js 60 | paystack.plan.get(90) 61 | .then(function(error, body) { 62 | console.log(error); 63 | console.log(body); 64 | }); 65 | ``` 66 | 67 | ```js 68 | paystack.transactions.list({perPage: 20}) 69 | .then(function(error, body) { 70 | console.log(error); 71 | console.log(body); 72 | }); 73 | ``` 74 | 75 | ### Resources 76 | 77 | - customer 78 | - create 79 | - get 80 | - list 81 | - update 82 | - transaction 83 | - initialize 84 | - charge 85 | - get 86 | - list 87 | - totals 88 | - verify 89 | - plan 90 | - create 91 | - get 92 | - list 93 | - update 94 | - page 95 | - create 96 | - get 97 | - list 98 | - update 99 | - subscription 100 | - create 101 | - disable 102 | - enable 103 | - get 104 | - list 105 | - subaccount 106 | - create 107 | - get 108 | - list 109 | - listBanks 110 | - update 111 | - Miscellanous 112 | - list_banks 113 | - resolve_bin 114 | 115 | 116 | 117 | ### Contributing 118 | - To ensure consistent code style, please follow the [editorconfig rules](http://obem.be/2015/06/01/a-quick-note-on-editorconfig.html) in .editorconfig 119 | 120 | ### Tests 121 | 122 | To run tests, add your Paystack test secret key to `package.json`. (The test line should look something like this: `env KEY=sk_test_1a68ac96a0171fb72111a24295d8d31d41c28eed ./node_modules/.bin/mocha...`). Now run: 123 | 124 | ``` 125 | npm test 126 | ``` 127 | 128 | If you are contributing to the repo, kindly update the necessary test file in `/test` or add a new one and ensure all tests are passed before sending a PR. 129 | 130 | ### Todo 131 | 132 | - Proper resource examples 133 | - ES6 support 134 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | Paystack API wrapper 3 | @author Obembe Opeyemi <@kehers> 4 | */ 5 | 6 | 'use strict'; 7 | 8 | var 9 | request = require('request'), 10 | root = 'https://api.paystack.co', 11 | Promise = require('promise') 12 | ; 13 | 14 | var resources = { 15 | customer: require('./resources/customer'), 16 | plan: require('./resources/plan'), 17 | transaction: require('./resources/transaction'), 18 | page: require('./resources/page'), 19 | subscription: require('./resources/subscription'), 20 | subaccount: require('./resources/subaccount'), 21 | settlements: require('./resources/settlements'), 22 | misc: require('./resources/misc') 23 | } 24 | 25 | function Paystack(key) { 26 | if (!(this instanceof Paystack)) { 27 | return new Paystack(key); 28 | } 29 | 30 | this.key = key; 31 | this.importResources(); 32 | } 33 | 34 | Paystack.prototype = { 35 | 36 | extend: function(params) { 37 | // This looks more sane. 38 | var self = this; 39 | return function(){ 40 | // Convert argument to array 41 | var args = new Array(arguments.length); 42 | var l = args.length; 43 | for(var i = 0; i < l; ++i) { 44 | args[i] = arguments[i]; 45 | } 46 | 47 | // Check for callback & Pull it out from the array 48 | var callback = l > 0 && typeof args.slice(l-1)[0] === "function" ? args.splice(l-1)[0] : undefined; 49 | 50 | var body, qs; 51 | 52 | // quick fix - method checking 53 | var method = params.method in {"get":'', "post":'', "put":''} 54 | ? params.method 55 | : (function () { throw new Error("Method not Allowed! - Resource declaration error") })() 56 | var endpoint = [root, params.endpoint].join(''); 57 | // Checking for required params; 58 | if(params.params) { 59 | var paramList = params.params; 60 | 61 | // Pull body passed 62 | var body = args.length === 2 ? args[1] : args[0]; 63 | paramList.filter(function(item, index, array) { 64 | if(item.indexOf("*") === -1) { 65 | // Not required 66 | return; 67 | } 68 | item = item.replace("*", ""); 69 | 70 | if(!(item in body)) { 71 | throw new Error("Required Parameters Ommited - " + item); 72 | } 73 | return; 74 | 75 | }); 76 | } 77 | 78 | // Get arguments in endpoint e.g {id} in customer/{id} and pull 79 | // out from array 80 | var argsInEndpoint = endpoint.match(/{[^}]+}/g); 81 | if (argsInEndpoint) { 82 | l = argsInEndpoint.length; 83 | 84 | // Do we have one or more? 85 | if (l > 0) { 86 | // Confirm resource declaration good 87 | if (!Array.isArray(params.args)) { 88 | // error 89 | throw new Error('Resource declaration error'); 90 | } 91 | 92 | // Confirm user passed the argument to method 93 | // and replace in endpoint 94 | 95 | var match, index; 96 | for (var i=0;i (http://obem.be/opeyemi)", 16 | "license": "MIT", 17 | "dependencies": { 18 | "promise": "^7.1.1", 19 | "request": "^2.79.0" 20 | }, 21 | "devDependencies": { 22 | "chai": "^3.5.0", 23 | "mocha": "^3.2.0" 24 | }, 25 | "repository": { 26 | "type": "git", 27 | "url": "https://github.com/kehers/paystack.git" 28 | }, 29 | "bugs": { 30 | "url": "https://github.com/kehers/paystack/issues" 31 | }, 32 | "homepage": "https://github.com/kehers/paystack" 33 | } 34 | -------------------------------------------------------------------------------- /resources/customer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/customer'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Create customer 9 | @param: first_name, last_name, email, phone 10 | */ 11 | create: { 12 | method: 'post', 13 | endpoint: root, 14 | params: ['first_name', 'last_name', 'email*', 'phone'] 15 | }, 16 | 17 | /* 18 | Get customer 19 | */ 20 | get: { 21 | method: 'get', 22 | endpoint: [root, '/{id}'].join(''), 23 | args: ['id'] 24 | }, 25 | 26 | /* 27 | List customers 28 | */ 29 | list: { 30 | method: 'get', 31 | endpoint: root 32 | }, 33 | 34 | /* 35 | Update customer 36 | @param: first_name, last_name, email, phone 37 | */ 38 | update: { 39 | method: 'put', 40 | endpoint: [root, '/{id}'].join(''), 41 | params: ['first_name', 'last_name', 'email', 'phone'], 42 | args: ['id'] 43 | }, 44 | 45 | /* 46 | White/Blacklist customer 47 | @param: customer, risk_action ('allow' to whitelist or 'deny' to blacklist) 48 | */ 49 | setRiskAction: { 50 | method: 'post', 51 | endpoint: [root, '/set_risk_action'].join(''), 52 | params: ['customer*', 'risk_action'] 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /resources/misc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Miscellanous functions have different endpoints 4 | 5 | module.exports = { 6 | 7 | /* 8 | List supported banks 9 | */ 10 | list_banks: { 11 | method: 'get', 12 | endpoint: '/bank', 13 | params: ['perPage', 'page'] 14 | }, 15 | 16 | resolve_bin: { 17 | method: 'get', 18 | endpoint: '/decision/bin/{id}', 19 | args: ['id'] 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /resources/page.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/page'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Create page 9 | */ 10 | create: { 11 | method: 'post', 12 | endpoint: root, 13 | params: ['name*', 'description', 'amount'] 14 | }, 15 | 16 | /* 17 | Get page 18 | */ 19 | get: { 20 | method: 'get', 21 | endpoint: [root, '/{id}'].join(''), 22 | args: ['id'] 23 | }, 24 | 25 | /* 26 | List page 27 | */ 28 | list: { 29 | method: 'get', 30 | endpoint: root 31 | }, 32 | 33 | /* 34 | Update page 35 | */ 36 | update: { 37 | method: 'put', 38 | endpoint: [root, '/{id}'].join(''), 39 | params: ['name', 'description', 'amount', 'active'], 40 | args: ['id'] 41 | }, 42 | 43 | /* 44 | Check Slug Avaliability 45 | */ 46 | slug: { 47 | method: 'get', 48 | endpoint: [root, '/check_slug_availability'].join(''), 49 | params: ['slug*'] 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /resources/plan.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/plan'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Create plan 9 | */ 10 | create: { 11 | method: 'post', 12 | endpoint: root, 13 | params: ['name*', 'description', 'amount*', 'interval*', 'send_invoices', 'send_sms', 'hosted_page', 'hosted_page_url', 'hosted_page_summary', 'currency'] 14 | }, 15 | 16 | /* 17 | Get plan 18 | */ 19 | get: { 20 | method: 'get', 21 | endpoint: [root, '/{id}'].join(''), 22 | args: ['id'] 23 | }, 24 | 25 | /* 26 | List plan 27 | */ 28 | list: { 29 | method: 'get', 30 | endpoint: root 31 | }, 32 | 33 | /* 34 | Update plan 35 | */ 36 | update: { 37 | method: 'put', 38 | endpoint: [root, '/{id}'].join(''), 39 | params: ['name', 'description', 'amount', 'interval', 'send_invoices', 'send_sms', 'hosted_page', 'hosted_page_url', 'hosted_page_summary', 'currency'], 40 | args: ['id'] 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /resources/settlements.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/settlement'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Fetch settlement 9 | */ 10 | fetch: { 11 | method: 'get', 12 | endpoint: root, 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /resources/subaccount.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/subaccount'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Create subaccount 9 | @param: business_name, settlement_bank, account_number, percentage_charge 10 | */ 11 | create: { 12 | method: 'post', 13 | endpoint: root, 14 | params: ['business_name*', 'settlement_bank*', 'account_number*', 'percentage_charge*'] 15 | }, 16 | 17 | /* 18 | Get subaccount 19 | */ 20 | get: { 21 | method: 'get', 22 | endpoint: [root, '/{id_or_slug}'].join(''), 23 | args: ['id_or_slug'] 24 | }, 25 | 26 | /* 27 | List subaccount 28 | */ 29 | list: { 30 | method: 'get', 31 | endpoint: root 32 | }, 33 | 34 | /* 35 | List supported banks 36 | */ 37 | listBanks: { 38 | method: 'get', 39 | endpoint: '/bank' 40 | }, 41 | 42 | /* 43 | Update subaccount 44 | @param: business_name, settlement_bank, account_number, percentage_charge 45 | */ 46 | update: { 47 | method: 'put', 48 | endpoint: [root, '/{id_or_slug}'].join(''), 49 | params: ['business_name', 'settlement_bank', 'account_number', 'percentage_charge'], 50 | args: ['id_or_slug'] 51 | } 52 | }; 53 | -------------------------------------------------------------------------------- /resources/subscription.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/subscription'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Create subscription 9 | */ 10 | create: { 11 | method: 'post', 12 | endpoint: root, 13 | params: ['customer*', 'plan*', 'authorization'] 14 | }, 15 | 16 | /* 17 | Disable subscription 18 | */ 19 | disable: { 20 | method: 'post', 21 | endpoint: root, 22 | params: ['code*', 'token*'] 23 | }, 24 | 25 | /* 26 | Enable subscription 27 | */ 28 | enable: { 29 | method: 'post', 30 | endpoint: root, 31 | params: ['code*', 'token*'] 32 | }, 33 | 34 | /* 35 | Get subscription 36 | */ 37 | get: { 38 | method: 'get', 39 | endpoint: [root, '/{id_or_subscription_code}'].join(''), 40 | args: ['id_or_subscription_code'] 41 | }, 42 | 43 | /* 44 | List subscription 45 | */ 46 | list: { 47 | method: 'get', 48 | endpoint: root 49 | } 50 | 51 | }; 52 | -------------------------------------------------------------------------------- /resources/transaction.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var root = '/transaction'; 4 | 5 | module.exports = { 6 | 7 | /* 8 | Initialize transaction 9 | */ 10 | initialize: { 11 | method: 'post', 12 | endpoint: [root, '/initialize'].join(''), 13 | params: ['reference', 'amount*', 'email*', 'plan'] 14 | }, 15 | 16 | /* 17 | Export transactions 18 | */ 19 | export: { 20 | method: 'get', 21 | endpoint: [root, '/export'].join(''), 22 | params: ['from', 'to', 'settled', 'payment_page'] 23 | }, 24 | 25 | /* 26 | Charge authorization 27 | */ 28 | charge: { 29 | method: 'post', 30 | endpoint: [root, '/charge_authorization'].join(''), 31 | params: ['reference', 'authorization_code*', 'email*', 'amount*'] 32 | }, 33 | 34 | /* 35 | Charge token 36 | */ 37 | chargeToken: { 38 | method: 'post', 39 | endpoint: [root, '/charge_token'].join(''), 40 | params: ['reference', 'token*', 'email', 'amount'] 41 | }, 42 | 43 | /* 44 | Get transaction 45 | */ 46 | get: { 47 | method: 'get', 48 | endpoint: [root, '/{id}'].join(''), 49 | args: ['id'] 50 | }, 51 | 52 | /* 53 | List transactions 54 | */ 55 | list: { 56 | method: 'get', 57 | endpoint: root 58 | }, 59 | 60 | /* 61 | Get totals 62 | */ 63 | totals: { 64 | method: 'get', 65 | endpoint: [root, '/totals'].join('') 66 | }, 67 | 68 | /* 69 | Verify transaction 70 | */ 71 | verify: { 72 | method: 'get', 73 | endpoint: [root, '/verify/{reference}'].join(''), 74 | args: ['reference'] 75 | }, 76 | 77 | }; 78 | -------------------------------------------------------------------------------- /test/customer.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Customers", function() { 7 | 8 | var customer_id; 9 | 10 | // New Customer 11 | it("should create a new customer", function(done) { 12 | paystack.customer.create({ 13 | first_name: 'Opeyemi', 14 | last_name: 'Obembe', 15 | email: 'kehers@gmail.com' 16 | }) 17 | .then(function(body){ 18 | expect(body).to.have.property('data'); 19 | expect(body.data).to.have.property('id'); 20 | customer_id = body.data.id; 21 | done(); 22 | }) 23 | .catch(function(error){ 24 | return done(error); 25 | }); 26 | }); 27 | 28 | // To test callback & then chaining 29 | it("create new customer, parse in callback and enter then handler", function(done) { 30 | paystack.customer.create({ 31 | first_name: 'Opeyemi', 32 | last_name: 'Obembe', 33 | email: 'kehers@gmail.com' 34 | }, function(error, body) { 35 | // callback should parse response and return an object 36 | return {'name': 'subomi'}; 37 | }).then(function(body) { 38 | // callback is called, but then handler does not show its processing, but returns initial api response 39 | expect(body).to.have.property('name') 40 | done(); 41 | }).catch(function(error) { 42 | return done(error); 43 | }); 44 | }); 45 | 46 | // Update Customer 47 | it("should update a customer", function(done) { 48 | paystack.customer.update(customer_id, {last_name: 'Kehers'}) 49 | .then(function(body){ 50 | expect(body).to.have.property('data'); 51 | expect(body.data).to.have.property('id'); 52 | done(); 53 | }) 54 | .catch(function(error){ 55 | return done(error); 56 | }); 57 | }); 58 | 59 | // Fetch Customer 60 | it("should get a customer's details", function(done) { 61 | paystack.customer.get(customer_id) 62 | .then(function(body){ 63 | expect(body).to.have.property('data'); 64 | expect(body.data).to.have.property('id'); 65 | done(); 66 | }) 67 | .catch(function(error){ 68 | return done(error); 69 | }); 70 | }); 71 | 72 | // List Customers 73 | it("should list customers", function(done) { 74 | paystack.customer.list() 75 | .then(function(body){ 76 | expect(body).to.have.property('data'); 77 | expect(body.data).to.be.instanceof(Array); 78 | done(); 79 | }) 80 | .catch(function(error){ 81 | return done(error); 82 | }); 83 | }); 84 | 85 | //Whitelist Customer 86 | /* 87 | setRiskAction integration not available 88 | on test integration 89 | it("should whitelist customer", function(done) { 90 | paystack.customer.setRiskAction({ 91 | "customer": customer_id, 92 | "risk_action": "allow" 93 | }, function(error, body) { 94 | 95 | if (error) 96 | return done(error); 97 | 98 | expect(body).to.have.property('data'); 99 | expect(body.data).to.have.property('risk_action'); 100 | 101 | done(); 102 | }); 103 | }); 104 | */ 105 | }); 106 | -------------------------------------------------------------------------------- /test/misc.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Miscellanous Functions", function() { 7 | 8 | // List Banks 9 | it("should list all supported banks", function(done) { 10 | paystack.misc.list_banks() 11 | .then(function(body){ 12 | expect(body).to.have.property('data'); 13 | expect(body.data[0]).to.have.property('name'); 14 | done(); 15 | }) 16 | .catch(function(error){ 17 | return done(error); 18 | }); 19 | }); 20 | 21 | // Resolve a Bin Card 22 | it("should resolve a bin card", function(done) { 23 | paystack.misc.resolve_bin(59983) 24 | .then(function(body){ 25 | expect(body).to.have.property('data'); 26 | expect(body.data).to.have.property('bin'); 27 | done(); 28 | }) 29 | .catch(function(error){ 30 | return done(error); 31 | }); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /test/page.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Pages", function() { 7 | 8 | var page_id; 9 | 10 | // New Page 11 | it("should create a new page", function(done) { 12 | paystack.page.create({ 13 | name: 'API Monthly', 14 | amount: 100000 15 | }) 16 | .then(function(body){ 17 | expect(body).to.have.property('data'); 18 | expect(body.data).to.have.property('id'); 19 | page_id = body.data.id; 20 | done(); 21 | }) 22 | .catch(function(error){ 23 | return done(error); 24 | }); 25 | }); 26 | 27 | // Update Page 28 | it("should update a page", function(done) { 29 | paystack.page.update(page_id, {'name': 'Monthly Subscription for API Course'}) 30 | .then(function(body){ 31 | expect(body).to.an('object'); 32 | done(); 33 | }) 34 | .catch(function(error){ 35 | return done(error); 36 | }); 37 | }); 38 | 39 | // Fetch Page 40 | it("should get details of a page", function(done) { 41 | paystack.page.get(page_id) 42 | .then(function(body){ 43 | expect(body).to.have.property('data'); 44 | expect(body.data).to.have.property('slug'); 45 | done(); 46 | }) 47 | .catch(function(error){ 48 | return done(error); 49 | }); 50 | }); 51 | 52 | // List Pages 53 | it("should list page", function(done) { 54 | paystack.page.list() 55 | .then(function(body){ 56 | expect(body).to.have.property('data'); 57 | expect(body.data).to.be.instanceof(Array); 58 | done(); 59 | }) 60 | .catch(function(error){ 61 | return done(error); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /test/plan.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Plan", function() { 7 | 8 | var plan_id, plan_code; 9 | 10 | 11 | // New Plan 12 | it("should create a new plan", function(done) { 13 | paystack.plan.create({ 14 | name: 'API Monthly', 15 | interval: 'monthly', 16 | amount: 100000 17 | }).then(function(body){ 18 | expect(body).to.have.property('data'); 19 | expect(body.data).to.have.property('id'); 20 | expect(body.data).to.have.property('plan_code'); 21 | 22 | plan_id = body.data.id; 23 | plan_code = body.data.plan_code; 24 | done(); 25 | }).catch(function(error){ 26 | return done(error); 27 | }); 28 | }); 29 | 30 | // Update Plan 31 | it("should update a plan", function(done) { 32 | paystack.plan.update(plan_id, {'name': 'Monthly Subscription for API Course'}).then(function(body){ 33 | expect(body).to.be.an('object'); 34 | done(); 35 | }).catch(function(error){ 36 | return done(error); 37 | }); 38 | }); 39 | 40 | // Fetch Plan 41 | it("should get details of a plan", function(done) { 42 | paystack.plan.get(plan_id).then(function(body){ 43 | expect(body).to.have.property('data'); 44 | expect(body.data).to.have.property('id'); 45 | 46 | done(); 47 | }).catch(function(error){ 48 | return done(error); 49 | }); 50 | }); 51 | 52 | // List Plans 53 | it("should list plan", function(done) { 54 | paystack.plan.list().then(function(body){ 55 | expect(body).to.have.property('data'); 56 | expect(body.data).to.be.instanceof(Array); 57 | done(); 58 | }).catch(function(error){ 59 | return done(error); 60 | }); 61 | }); 62 | 63 | }); 64 | -------------------------------------------------------------------------------- /test/settlements.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Settlements", function() { 7 | 8 | // Fetch Settlements 9 | it("should fetch settlements", function(done) { 10 | paystack.settlements.fetch() 11 | .then(function(body){ 12 | expect(body).to.have.property('status'); 13 | expect(body).to.have.property('message'); 14 | 15 | done(); 16 | }).catch(function(error){ 17 | return done(error); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/subaccount.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | 7 | describe("Paystack Subaccount", function() { 8 | 9 | var subaccount_id, subaccount_code; 10 | 11 | // New Subaccount 12 | it("should create a new subaccount", function(done) { 13 | paystack.subaccount.create({ 14 | business_name: 'Super Cool Inc', 15 | settlement_bank: 'Access Bank', 16 | account_number: '0193274682', 17 | percentage_charge: 18.2 18 | }) 19 | .then(function(body){ 20 | expect(body).to.have.property('data'); 21 | expect(body.data).to.have.property('id'); 22 | expect(body.data).to.have.property('subaccount_code'); 23 | 24 | subaccount_id = body.data.id; 25 | subaccount_code = body.data.subaccount_code; 26 | done(); 27 | }) 28 | .catch(function(error){ 29 | return done(error); 30 | }); 31 | }); 32 | 33 | // Update Subaccount 34 | it("should update a subaccount", function(done) { 35 | paystack.subaccount.update(subaccount_code, { 36 | primary_contact_email: 'wale@obo.com', 37 | percentage_charge: 98.9 38 | }) 39 | .then(function(body){ 40 | expect(body).to.have.property('data'); 41 | expect(body.data).to.have.property('id'); 42 | done(); 43 | }) 44 | .catch(function(error){ 45 | return done(error); 46 | }); 47 | }); 48 | 49 | // Fetch Subaccount 50 | it("should get details of a subaccount", function(done) { 51 | paystack.subaccount.get(subaccount_code) 52 | .then(function(body){ 53 | expect(body).to.have.property('data'); 54 | expect(body.data).to.have.property('id'); 55 | done(); 56 | }) 57 | .catch(function(error){ 58 | return done(error); 59 | }); 60 | }); 61 | 62 | // list Banks 63 | it("should list supported banks", function(done) { 64 | paystack.subaccount.listBanks() 65 | .then(function(body){ 66 | expect(body).to.have.property('data'); 67 | expect(body.data).to.be.instanceof(Array); 68 | done(); 69 | }) 70 | .catch(function(error){ 71 | return done(error); 72 | }); 73 | }); 74 | 75 | 76 | // List Subaccounts 77 | it("should list subaccounts", function(done) { 78 | paystack.subaccount.list() 79 | .then(function(body){ 80 | expect(body).to.have.property('data'); 81 | expect(body.data).to.be.instanceof(Array); 82 | done(); 83 | }) 84 | .catch(function(error){ 85 | return done(error); 86 | }); 87 | }); 88 | 89 | }); 90 | -------------------------------------------------------------------------------- /test/subscription.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Subscriptions", function() { 7 | 8 | var subscription_id, subscription_code, token; 9 | 10 | /* 11 | // New Subscription 12 | // [This will fail as no authorization is added to customer yet 13 | // Hence next 3 tests will fail as well 14 | // Test ideas welcomed] 15 | it("should create a new subscription", function(done) { 16 | paystack.subscription.create({ 17 | customer: 'kehers@gmail.com', 18 | plan: plan_code 19 | }, function(error, body) { 20 | 21 | if (error) 22 | return done(error); 23 | 24 | expect(body).to.have.property('data'); 25 | expect(body.data).to.have.property('id'); 26 | expect(body.data).to.have.property('subscription_code'); 27 | expect(body.data).to.have.property('email_token'); 28 | 29 | subscription_id = body.data.id; 30 | subscription_code = body.data.subscription_code; 31 | token = body.data.email_token; 32 | 33 | done(); 34 | }); 35 | }); 36 | 37 | // Fetch Subscription 38 | it("should get details of a subscription", function(done) { 39 | paystack.subscription.get(subscription_id, function(error, body) { 40 | 41 | if (error) 42 | return done(error); 43 | 44 | expect(body).to.have.property('data'); 45 | 46 | done(); 47 | }); 48 | }); 49 | 50 | // Enable Subscription 51 | it("should get enable subscription", function(done) { 52 | paystack.subscription.enable({code: subscription_code, token: token}, function(error, body) { 53 | 54 | if (error) 55 | return done(error); 56 | 57 | done(); 58 | }); 59 | }); 60 | 61 | // Disable Subscription 62 | it("should get disable subscription", function(done) { 63 | paystack.subscription.disable({code: subscription_code, token: token}, function(error, body) { 64 | 65 | if (error) 66 | return done(error); 67 | 68 | done(); 69 | }); 70 | }); 71 | //*/ 72 | 73 | // List Subscription 74 | it("should list subscription", function(done) { 75 | paystack.subscription.list() 76 | .then(function(body){ 77 | expect(body).to.have.property('data'); 78 | expect(body.data).to.be.instanceof(Array); 79 | done(); 80 | }) 81 | .catch(function(error){ 82 | return done(error); 83 | }); 84 | }); 85 | }); 86 | -------------------------------------------------------------------------------- /test/transaction.js: -------------------------------------------------------------------------------- 1 | var paystack = require('../index')(process.env.KEY) 2 | , mocha = require('mocha') 3 | , expect = require('chai').expect 4 | ; 5 | 6 | describe("Paystack Transaction", function() { 7 | 8 | var reference; 9 | 10 | // Init Transaction 11 | it("should initialize a transaction", function(done) { 12 | paystack.transaction.initialize({ 13 | email: 'theslyguy@icloud.com', 14 | amount: 500000 15 | }) 16 | .then(function(body){ 17 | expect(body).to.have.property('data'); 18 | expect(body.data).to.have.property('authorization_url'); 19 | expect(body.data).to.have.property('access_code'); 20 | expect(body.data).to.have.property('reference'); 21 | reference = body.data.reference; 22 | done(); 23 | }) 24 | .catch(function(error){ 25 | return done(error); 26 | }); 27 | }); 28 | 29 | // Verify Transaction 30 | it("should verify a transaction", function(done) { 31 | paystack.transaction.verify(reference) 32 | .then(function(body){ 33 | expect(body).to.have.property('data'); 34 | expect(body.data).to.be.an('object'); 35 | done(); 36 | }) 37 | .catch(function(error){ 38 | return done(error); 39 | }); 40 | }); 41 | 42 | // Fetch Transaction 43 | // No transaction id :/ 44 | /* 45 | it("should get details of a transaction", function(done) { 46 | paystack.transaction.get(transaction_id, function(error, body) { 47 | 48 | if (error) 49 | return done(error); 50 | 51 | expect(body).to.have.property('data'); 52 | 53 | done(); 54 | }); 55 | }); 56 | //*/ 57 | 58 | // List Transactions 59 | it("should list transaction", function(done) { 60 | paystack.transaction.list() 61 | .then(function(body){ 62 | expect(body).to.have.property('data'); 63 | expect(body.data).to.be.instanceof(Array); 64 | 65 | done(); 66 | }) 67 | .catch(function(error){ 68 | return done(error); 69 | }); 70 | }); 71 | 72 | // Export Transactions 73 | it("should export transaction", function(done) { 74 | paystack.transaction.export() 75 | .then(function(body){ 76 | expect(body).to.have.property('data'); 77 | done(); 78 | }) 79 | .catch(function(error){ 80 | return done(error); 81 | }); 82 | }); 83 | }); 84 | --------------------------------------------------------------------------------