From 7b9cee0396374a656e1aa80185afa8e0941f3137 Mon Sep 17 00:00:00 2001 From: Opeyemi Date: Fri, 29 Jan 2016 12:16:40 +0100 Subject: [PATCH] first commit --- .editorconfig | 12 ++++ .gitignore | 3 + README.md | 82 +++++++++++++++++++++ index.js | 149 +++++++++++++++++++++++++++++++++++++++ package.json | 20 ++++++ resources/customer.js | 44 ++++++++++++ resources/plan.js | 42 +++++++++++ resources/transaction.js | 59 ++++++++++++++++ 8 files changed, 411 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 resources/customer.js create mode 100644 resources/plan.js create mode 100644 resources/transaction.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4a7ea30 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f9544e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +responses.json +demo.js +/node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b9453d --- /dev/null +++ b/README.md @@ -0,0 +1,82 @@ +## paystack-node + +A Nodejs API wrapper for [Paystack](https://paystack.co/). + +### Installation + +``` +npm install paystack-node +``` + +### Usage + +```js +// Require the library +var paystack = require('./index')('secret_key'); + +// Make a call to the resource/method +// paystack.{resource}.{method} +paystack.customer.list(function(error, body) { + console.log(error); + console.log(body); +}); +``` + +The resource method accepts an optional callback as the last argument. The callback returns two JSON objects - `error`, which will be null for successful calls, and `body`, the response from the API call. + +For resource methods that use POST or PUT, the JSON body can be passed as the first argument. + +```js +paystack.plan.create({ + name: 'API demo', + amount: 10000, + interval: 'monthly' +},function(error, body) { + console.log(error); + console.log(body); +}); +``` + +For GET, you can pass the required ID as string and optional parameters as an optioal object argument. + +```js +paystack.plan.get(90, function(error, body) { + console.log(error); + console.log(body); +}); +``` + +```js +paystack.transactions.list({ + perPage: 20 +}, function(error, body) { + console.log(error); + console.log(body); +}); +``` + +### Resources + +- customer + - create + - get + - list + - update +- transaction + - initialize + - charge + - get + - list + - totals + - verify +- plan + - create + - get + - list + - update + +### Todo + +- Proper resource examples +- Tests +- ES6 support diff --git a/index.js b/index.js new file mode 100644 index 0000000..7af8877 --- /dev/null +++ b/index.js @@ -0,0 +1,149 @@ +/* +Paystack API wrapper +@author Obembe Opeyemi <@kehers> +*/ + +'use strict'; + +var + request = require('request') + , root = 'https://api.paystack.co' +; + +var resources = { + customer: require('./resources/customer'), + plan: require('./resources/plan'), + transaction: require('./resources/transaction') +} + +function Paystack(key) { + if (!(this instanceof Paystack)) { + return new Paystack(key); + } + + this.key = key; + this.importResources(); +} + +Paystack.prototype = { + + extend: function(params) { + + return (function() { + + // Convert argument to array + var args = new Array(arguments.length); + var l = args.length; + for(var i = 0; i < l; ++i) { + args[i] = arguments[i]; + } + + // Check for callback + var callback; + if (l > 0) { + l--; + // I expect it to be the last argument + if (args[l].constructor === Function) + callback = args[l]; + args.splice(l, 1); + } + + var body, qs; + var method = params.method; + // todo: Validate possible values of method (get, post, put...) + var endpoint = [root, params.endpoint].join(''); + + // Get arguments in endpoint => {id} in customer/{id} + var argsInEndpoint = endpoint.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 (http://obem.be/opeyemi)", + "license": "MIT", + "dependencies": { + "request": "^2.67.0" + } +} diff --git a/resources/customer.js b/resources/customer.js new file mode 100644 index 0000000..98a1db5 --- /dev/null +++ b/resources/customer.js @@ -0,0 +1,44 @@ +'use strict'; + +var root = '/customer'; + +module.exports = { + + /* + Create customer + @param: first_name, last_name, email, phone + */ + create: { + method: 'post', + endpoint: root, + params: ['first_name', 'last_name', 'email', 'phone'] + }, + + /* + Get customer + */ + get: { + method: 'get', + endpoint: [root, '/{id}'].join(''), + args: ['id'] + }, + + /* + List customers + */ + list: { + method: 'get', + endpoint: root + }, + + /* + Update customer + @param: first_name, last_name, email, phone + */ + update: { + method: 'put', + endpoint: [root, '/{id}'].join(''), + params: ['first_name', 'last_name', 'email', 'phone'], + args: ['id'] + } +}; diff --git a/resources/plan.js b/resources/plan.js new file mode 100644 index 0000000..67b04b0 --- /dev/null +++ b/resources/plan.js @@ -0,0 +1,42 @@ +'use strict'; + +var root = '/plan'; + +module.exports = { + + /* + Create plan + */ + create: { + method: 'post', + endpoint: root, + params: ['name', 'description', 'amount', 'interval', 'send_invoices', 'send_sms', 'hosted_page', 'hosted_page_url', 'hosted_page_summary', 'currency'] + }, + + /* + Get plan + */ + get: { + method: 'get', + endpoint: [root, '/{id}'].join(''), + args: ['id'] + }, + + /* + List plan + */ + list: { + method: 'get', + endpoint: root + }, + + /* + Update plan + */ + update: { + method: 'put', + endpoint: [root, '/{id}'].join(''), + params: ['name', 'description', 'amount', 'interval', 'send_invoices', 'send_sms', 'hosted_page', 'hosted_page_url', 'hosted_page_summary', 'currency'], + args: ['id'] + } +}; diff --git a/resources/transaction.js b/resources/transaction.js new file mode 100644 index 0000000..cbf4326 --- /dev/null +++ b/resources/transaction.js @@ -0,0 +1,59 @@ +'use strict'; + +var root = '/transaction'; + +module.exports = { + + /* + Initialize transaction + */ + initialize: { + method: 'post', + endpoint: [root, '/initialize'].join(''), + params: ['reference', 'amount', 'email', 'plan'] + }, + + /* + Charge authorization + */ + charge: { + method: 'post', + endpoint: [root, '/charge_authorization'].join(''), + params: ['authorization_code', 'email', 'amount'] + }, + + /* + Get transaction + */ + get: { + method: 'get', + endpoint: [root, '/{id}'].join(''), + args: ['id'] + }, + + /* + List transactions + */ + list: { + method: 'get', + endpoint: root + }, + + /* + Get totals + */ + totals: { + method: 'get', + endpoint: [root, '/totals'].join('') + }, + + /* + Verify transaction + */ + verify: { + method: 'get', + endpoint: [root, '/verify/{reference}'].join(''), + args: ['reference'] + }, + +};