@aizon/node-paystack
v1.3.3
Published
A Node.JS API wrapper for paystack
Downloads
75
Maintainers
Readme
@aizon/node-paystack 💫
This is a simple Paystack API wrapper for Node.js designed to be easy to use. Use this npm package in your Node.js backend servers to seamlessly integrate paystack payments. View all supported routes here
Features
- Full support for all Paystack API routes as at 2023/06/23 🚀
- VSCode intellisense support for all routes 🚀
- Built with TypeScript for improved type safety 🚀
Getting started
Installation
To install, simply run the command npm i @aizon/node-paystack
in your terminal
Usage
Quick tips
To use this package, you simply use any of the provided methods, e.g:
transaction.initialize()
: Initialize a transactioncustomer.list()
: List customersproduct.fetch()
: Fetch a product
All methods in this library will always return a JSON response containing the following fields
status
(boolean, always present)message
(string, always present)data
(object, only present on a SUCCESSFUL response)httpStatus
(objedt, only present on an UNSUCCESSFUL response
Initializing and verifying transactions
Let's take a quick example to see the package in action
- Import the package:
//Firstly import the installed library into your project
//CommonJS
const paystack = require("@aizon/node-paystack")("<api-key>")
//or alternatively
const node_paystack = require("@aizon/node-paystack")
const paystack = node_paystack("<api-key">)
//EJS
import node_paystack from "@aizon/node-paystack";
const paystack = node_paystack("<api-key>");
//TS
import node_paystack from "@aizon/node-paystack";
(optional) Configure the package:
Because this package uses
axios
under the hood, two(2) configuration options useful for debugging have been provided, which are:- showRaw (boolean)
- hideHttpErrorStatus (boolean)
Note: all config options are set to false by default
You can use them like so:
const node_paystack = require("@aizon/node-paystack")("<api-key>", {showRaw: true})
// By default, the axios reponse is transformed to display just the data from the server.
// `showRaw` config option displays the raw, unaltered axios response
const node_paystack = require("@aizon/node-paystack")("<api-key>", {hideHttpErrorStatus: true})
// By default, when an error is received from the server, its HTTP status is displayed.
// `hideHttpErrorStatus` config option, hides this, and displays just the server's response
Make transaction:
To complete transactions, you will first need to
initialize
a transaction, thenverify
the transaction using thereference
returned- With async/await
async function makePayment() {
// Initializing a transaction using the `transaction.initialize()` method and passing the transaction information
let result = await paystack.transaction.initialize({
email: "[email protected]",
amount: 100000 // Amount should be in kobo if currency is NGN, pesewas, if currency is GHS, and cents, if currency is ZAR
})
// Getting the transaction reference from the JSON data
let reference = result.data.reference
// Verifying the transaction using the `transaction.verify()` method and passing the `reference`
let verification = await paystack.transaction.verify(reference)
// The transaction is successful if the `status` of `verification` is true
let isSuccessful = verification.status
if(isSuccessful){
console.log("Yay, transaction successful")
}
}
- Without async/await
paystack.transaction.initialize({
email: "[email protected]",
amount: 100000
}).then( result => {
let reference = result.data.reference;
paystack.transaction.verify(reference).then( result => {
if(status == true) {
console.log("Transaction verified")
}
})
})
- Full Code sample
const paystack = require("@aizon/node-paystack")("<api-key>")
async function makePayment() {
let result = await paystack.transaction.initialize({
email: "[email protected]",
amount: 100000
})
let reference = result.data.reference;
let verification = await paystack.transaction.verify(reference)
let isSuccessful = verification.status;
if(isSuccessful){
console.log("Yay, transaction successful")
}
}
makePayment();
Routes:
Below are all the paystack routes supported by this package hyperlinked to their section on the official Paystack API documentation: