This is a library for easy integration of Paystack with your Nodejs backend. This packages eliminates a lot of issues pertanining to making calls to Paystack endpoints.
- Within your Config file inside your .env file create the following key:
BEARER_SECRET_KEY=sk_test_20a4725e39fsssss9727292209w8w98w8ww3ba5d58
npm install paystack-solucien
// Require the library
var paystack = require('paystack-solucien')('secret_key');
Since you will be calling from your .env file
1. require dotenv
const dotenv = require("dotenv");
2. then require package
const paystack= require("paystack-solucien")(process.env.BEARER_SECRET_KEY);
The resource methods promisified
Calling the resources with try and catch
paystack.customer
.list()
.then(function(body) {
console.log(body);
})
.catch(function(error) {
console.log(error);
});
Calling the resources without try and catch
// paystack.{resource}.{method}
paystack.customer.list(function(error, body) {
console.log(error);
console.log(body);
});
exports.getPayments = asyncHandler(async (req, res, next) => {
const {
email,
amount,
plan,
reference,
start_date,
currency,
metadata,
callback_url,
cancel_url,
frequency,
duration,
name,
description,
send_invoices,
send_sms,
} = req.body;
paystack.transaction
.initialize({
//just pass the data that you need for your payment transaction
name,
email,
amount,
callback_url,
reference,
metadata,
plan,
start_date,
cancel_url,
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
});
exports.getPayments = asyncHandler(async (req, res, next) => {
const {
email,
amount,
plan,
reference,
start_date,
currency,
metadata,
callback_url,
cancel_url,
frequency,
duration,
name,
description,
send_invoices,
send_sms,
} = req.body;
paystack.transaction
.initialize({
//just pass the data that you need for your payment transaction
name,
email,
amount,
plan,
callback_url,
reference,
metadata,
plan,
start_date,
cancel_url,
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
});
const {
email,
amount,
plan,
reference,
start_date,
currency,
metadata,
callback_url,
cancel_url,
frequency,
duration,
name,
description,
send_invoices,
send_sms,
} = req.body;
paystack.transaction
.initialize({
//just pass the data that you need for your payment transaction
name,
email,
amount,
plan,
callback_url,
reference,
metadata,
plan,
start_date,
cancel_url,
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
const { email, id } = req.body;
//get get customer
paystack.customer
.get({
email, id
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
Let say you were using a complex controller.js file with a middleware that allowed you to run async functions on your endpoints
exports.getCustomer = asyncHandler(async (req, res, next) => {
//req,body it will take the data you will send via json
const { email, id } = req.body;
//get customer
paystack.customer
.get({
//it will then pass the request data from your api to paystack api
email, id
})
.then(async (transaction) => {
//you will get access to the transaction and you will use the transactions how you see fit
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
});
If you are new to Nodejs the above example is not foreign to this:
//get customer
paystack.customer
.get({
//send info as hardcorded json data
email: "useremail@email.com",
id: 933383930
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
exports.updatePayment = asyncHandler(async (req, res, next) => {
const { subscription_code } = req.body;
//get subscription
paystack.subscription
.update({
subscription_code
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
});
const { subscription_code } = req.body;
//get subscription
paystack.subscription
.update({
subscription_code
})
.then(async (transaction) => {
console.log(transaction);
res.status(200).json({ transaction });
})
.catch((error) => {
console.log(error);
res.status(400).json({ error });
});
- customer
- create
- get
- list
- update
- transaction
- initialize
- charge
- get
- list
- totals
- verify
- plan
- create
- get
- list
- update
- page
- create
- get
- list
- update
- subscription
- create
- disable
- enable
- get
- list
- update
- subaccount
- create
- get
- list
- listBanks
- update
- Miscellanous
- list_banks
- resolve_bin
Please do contribute
- [You can buy me a coffee] (https://www.buymeacoffee.com/solucien/)
For support, email info@solucien or find me on Twitter via lucien_mendela.