A Node.JS API wrapper for paystack
npm install @aizon/node-paystackThis 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
npm i @aizon/node-paystack in your terminaltransaction.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
*
Let's take a quick example to see the package in action
- Import the package:
``js
//Firstly import the installed library into your project
//CommonJS
const paystack = require("@aizon/node-paystack")("
//or alternatively
const node_paystack = require("@aizon/node-paystack")
const paystack = node_paystack("
//EJS
import node_paystack from "@aizon/node-paystack";
const paystack = node_paystack("
//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:
`jsshowRaw
const node_paystack = require("@aizon/node-paystack")("
// By default, the axios reponse is transformed to display just the data from the server.
// config option displays the raw, unaltered axios response
const node_paystack = require("@aizon/node-paystack")("
// 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, then verify the transaction using the referencereturned`
1. With async/awaitjs
async function makePayment() {
// Initializing a transaction using the transaction.initialize() method and passing the transaction information
let result = await paystack.transaction.initialize({
email: "customer@email.com",
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")
}
}
2. Without async/await
`js`
paystack.transaction.initialize({
email: "customer@email.com",
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
`js
const paystack = require("@aizon/node-paystack")("
async function makePayment() {
let result = await paystack.transaction.initialize({
email: "customer@email.com",
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();
``