Skip to content

Commit

Permalink
release(v0.2.3)
Browse files Browse the repository at this point in the history
  • Loading branch information
isocroft committed Feb 4, 2020
1 parent 828814a commit 788b7e7
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 25 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<a name="0.2.3"></a>
# 0.2.3 (2020-02-04)

### Bug Fixes
- Fixed errors on **listBanks()** Endpoint method

### Feature Added
- Updated _Miscellaneous_ API Endpoint methods
- Updated _Pages_ API Endpoint methods
- Updated _Verifications_ API Endpoint methods
- Added _Disputes_ API Endpoint methods by [@isocroft](https://github.com/isocroft)

<a name="0.2.2"></a>
# 0.2.2 (2019-23-2019)
# 0.2.2 (2019-11-23)

### Feature Added
- Added _Transfer Recipients_ API Endpoint methods by [@adekoyejoakinhanmi](https://github.com/adekoyejoakinhanmi)
Expand Down
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,48 @@ const feeCharge = feesCalculator.calculateFor(250000) // 2,500 Naira
using `toJSON()` method for date instances/objects
*/

const promise1 = paystack.getSettlements({
const promise0 = paystack.getSettlements({
from:new Date("2017-02-09"),
to:new Date()
})

promise1.then(function(response){
var data = response.body
promise0.then(function(response){
var data = response.body.data;
}).catch(function (error){
// deal with error
})

// listBanks

try {
let { body: { status, message, data } } = await paystack.listBanks({
currency: 'NGN'
});

if(status === false){
throw new Error(message);
}
}catch(ex){
console.error(ex.message);
}

// addPageProduct

const promise1 = paystack.addPageProduct({
id: '0826739',
products: [473, 292]
})

promise1.then(function(response){
// Error Handling
if(response.body.status === false){
console.error(response.body.message);
}
var data = response.body.data;
}).catch(function (error) {
// deal with error
})

// getCustomer

const promise2 = paystack.getCustomer({
Expand Down Expand Up @@ -176,6 +207,8 @@ app.use(async function verifications(req, res, next){
- paystack.updateCustomer()
- paystack.deactivateAuthOnCustomer()
- paystack.setRiskActionOnCustomer()
- disputes
- paystack.listDisputes()
- invoices
- paystack.createInvoice()
- paystack.getMetricsForInvoices()
Expand All @@ -197,6 +230,7 @@ app.use(async function verifications(req, res, next){
- paystack.getPage()
- paystack.updatePage()
- paystack.checkSlugAvailability()
- paystack.addPageProduct()
- products
- paystack.createProduct()
- paystack.listProduct()
Expand Down Expand Up @@ -235,6 +269,7 @@ app.use(async function verifications(req, res, next){
- paystack.updateSubaccount()
- verifications
- paystack.resolveBVN()
- paystack.matchBVN()
- paystack.resolveAccountNumber()
- paystack.resolveCardBin()
- paystack.resolvePhoneNumber()
Expand All @@ -259,6 +294,7 @@ app.use(async function verifications(req, res, next){
- paystack.checkPendingCharge()
- miscellanous
- paystack.listBanks()
- paystack.listCountries()

# License

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "paystack-node",
"version": "0.2.2",
"version": "0.2.3",
"description": "A NodeJS wrapper for the Paystack API",
"main": "index.js",
"files": [
Expand Down
93 changes: 81 additions & 12 deletions src/PayStack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const querystring = require('querystring')
const _ = require('lodash')

const customers = require('../endpoints/customers.js')
const disputes = require('../endpoints/disputes.js')
const transactions = require('../endpoints/transactions.js')
const subaccounts = require('../endpoints/subaccounts.js')
const plans = require('../endpoints/plans.js')
const pages = require('../endpoints/pages.js')
const products = require('../endpoints/products.js')
const transferRecipients = require('../endpoints/transfers_recipients.js');
const balanceHistory = require('../endpoints/balance_history.js')
const transferRecipients = require('../endpoints/transfers_recipients.js')
const refunds = require('../endpoints/refunds.js')
const charges = require('../endpoints/charges.js')
const invoices = require('../endpoints/invoices.js')
Expand All @@ -25,11 +27,13 @@ const controlPanelForSessions = require('../endpoints/control_panel_for_sessions
const apiEndpoints = Object.assign(
{},
customers,
disputes,
transactions,
subaccounts,
plans,
pages,
products,
balanceHistory,
refunds,
charges,
invoices,
Expand Down Expand Up @@ -67,9 +71,61 @@ _.mixin(function () {
}
}())

const isTypeOf = (_value, type) => {
let value = Object(_value)
return (value instanceof type)
const isLiteralFalsey = (variable) => {
return (variable === '' || variable === false || variable === 0)
}

const checkTypeName = (target, type) => {
let typeName = ''
if (isLiteralFalsey(target)) {
typeName = (typeof target)
} else {
typeName = ('' + (target && target.constructor.name))
}
return !!(typeName.toLowerCase().indexOf(type) + 1)
}

const isTypeOf = (value, type) => {
let result = false

type = type || []

if (typeof type === 'object') {
if (typeof type.length !== 'number') {
return result
}

let bitPiece = 0
type = [].slice.call(type)

type.forEach(_type => {
if (typeof _type === 'function') {
_type = (_type.name || _type.displayName).toLowerCase()
}
bitPiece |= (1 * (checkTypeName(value, _type)))
})

result = !!(bitPiece)
} else {
if (typeof type === 'function') {
type = (type.name || type.displayName).toLowerCase()
}

result = checkTypeName(value, type)
}

return result
}

const isNullOrUndefined = (value) => {
return isTypeOf(value, ['undefined', 'null'])
}

const isNumeric = (value) => {
if (isTypeOf(value, ['string', 'number'])) {
return isTypeOf(Math.abs(-value), 'number')
}
return false
}

const setPathName = (config, values) => {
Expand All @@ -78,6 +134,11 @@ const setPathName = (config, values) => {
string,
offset) {
let _value = values[string]
if (config.route_params_numeric === true) {
if (!isNumeric(_value)) {
return null
}
}
return isTypeOf(
_value,
config.route_params[string]
Expand All @@ -88,7 +149,7 @@ const setPathName = (config, values) => {
}

const _jsonify = (data) => {
return !data ? 'null'
return isNullOrUndefined(data) ? 'null'
: (typeof data === 'object'
? (data instanceof Date ? data.toISOString().replace(/Z$/, '') : (('toJSON' in data) ? data.toJSON().replace(/Z$/, '') : JSON.stringify(data)))
: String(data))
Expand Down Expand Up @@ -130,7 +191,7 @@ const setInputValues = (config, inputs) => {
_required = true
}

if (_input === void 0 || _input === '' || _input === null) {
if (isNullOrUndefined(_input) || _input === '') {
if (_required) { throw new Error(`param: "${param}" is required but not provided; please provide as needed`) }
} else {
httpReqOptions[label][param] = isTypeOf(_input, _type)
Expand Down Expand Up @@ -244,21 +305,29 @@ class PayStack {
],
afterResponse: [
(response, retryWithMergedOptions) => {
let errorMessage = null
let errorMessage = ''
switch (response.statusCode) {
case 400: // Bad Request
errorMessage = 'Request was badly formed | Bad Request (400)'
break
case 401: // Unauthorized
errorMessage = 'Bearer Authorization header may not have been set: Unauthorized (401)'
errorMessage = 'Bearer Authorization header may not have been set | Unauthorized (401)'
break
case 404: // Not Found
errorMessage = 'Request endpoint does not exist: Not Found (404)'
errorMessage = 'Request endpoint does not exist | Not Found (404)'
break
case 403: // Forbidden
errorMessage = 'Request endpoint requires further priviledges to be accessed: Forbidden (403)'
errorMessage = 'Request endpoint requires further priviledges to be accessed | Forbidden (403)'
break
}

if (errorMessage !== null) {
;// throw new Error(errorMessage)
if (response.body && response.body.status === false) {
errorMessage += '; ' + response.body.message
}

if (errorMessage !== '') {
// console.error('PayStack Error: ', errorMessage);
throw new Error(errorMessage)
}

return response
Expand Down
16 changes: 16 additions & 0 deletions src/endpoints/balance_history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

module.exports = {
/*
Get Balance History
@params: from, to, type
*/
getBalanceHistory: {
method: 'GET',
path: '/balance/ledger',
send_json: false,
params: { from: Date, to: Date, type: String },
param_defaults: { type: 'Transaction' },
route_params: null
}
}
1 change: 1 addition & 0 deletions src/endpoints/charges.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.exports = {
checkPendingCharge: {
method: 'GET',
path: '/charge/{:reference}',
send_json: false,
params: null,
param_defaults: null,
route_params: { reference: String }
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
getCustomer: {
method: 'GET',
path: '/customer/{:customer_id}',
send_json: false,
params: null,
route_params: { customer_id: String }
},
Expand Down
16 changes: 16 additions & 0 deletions src/endpoints/disputes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

module.exports = {
/*
List Disputes
@params: from, to, page, perPage, transaction
*/
listDisputes: {
method: 'GET',
path: '/dispute',
send_json: false,
params: { from: Date, to: Date, page: Number, perPage: Number, transaction: Number },
param_defaults: { page: 1, perPage: 10 },
route_params: null
}
}
2 changes: 1 addition & 1 deletion src/endpoints/invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
verifyInvoice: {
method: 'GET',
path: '/paymentrequest/verify/{:invoice_code}',
send_json: true,
send_json: false,
params: null,
param_defaults: null,
route_params: { invoice_code: String }
Expand Down
21 changes: 17 additions & 4 deletions src/endpoints/miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

module.exports = {
/*
List Card
List Banks
@param: perPage, page
*/
listBanks: {
method: 'GET',
path: '/bank',
send_json: true,
params: { perPage: Number, page: Number },
param_defaults: { perPage: 50, page: 1 },
send_json: false,
params: { perPage: Number, page: Number, type: String, currency: String, country: String },
param_defaults: { perPage: 50, page: 1, currency: 'NGN', country: 'Nigeria' },
route_params: null
},

/*
List Countries
@param: -
*/

listCountries: {
method: 'GET',
path: '/country',
send_json: false,
params: null,
route_params: null
}
}
14 changes: 14 additions & 0 deletions src/endpoints/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ module.exports = {
checkSlugAvailability: {
method: 'GET',
path: '/page/check_slug_availability/{:slug}',
send_json: false,
params: null,
route_params: { slug: String }
},

/*
Add Page Product
@params: id, products
*/
addPageProduct: {
method: 'POST',
path: '/page/{:id}/product',
send_json: true,
params: { products$: Array },
route_params_numeric: true,
route_params: { id: String }
}
}
Loading

0 comments on commit 788b7e7

Please sign in to comment.