npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

paystack-solucien

v1.0.9

Published

API wrapper for paystack endpoints with most functionality

Downloads

31

Readme

Paystack NODEJS

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.

Requirements

  1. Within your Config file inside your .env file create the following key:

BEARER_SECRET_KEY=sk_test_20a4725e39fsssss9727292209w8w98w8ww3ba5d58

Installation

npm install paystack-solucien

Usage

// Require the library
var paystack = require('paystack-solucien')('secret_key');

Notes on Usage (Best practices)

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);

Making calls to endpoints

The resource methods promisified

The Format is: paystack.{resource}.{method}

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

The Format is: paystack.{resource}

// paystack.{resource}.{method}
paystack.customer.list(function(error, body) {
  console.log(error);
  console.log(body);
});

Usage real world examples will require you to pass some params

Usage examples

initialize a payment

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 });
    });
});

initialize a payment with subscription

You just got to pass the plan_code and paystack will take care of the rest

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 });
    });
});

When you are not using any asyncHandler just use

  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 });
    });

More examples

Example 1 (simple):

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 });
  });

Example 2 (complex)

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 });
  });
});

Note

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: "[email protected]",
     id: 933383930
    })
    .then(async (transaction) => {
      console.log(transaction);
      res.status(200).json({ transaction });
    })
    .catch((error) => {
      console.log(error);
      res.status(400).json({ error });
    });

Update Subscription card on file

Generate a link for the user so they update their card

Example 1:

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 });
    });
  });

Example 2 without the complexity

 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 });
    });

Resources

  • 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

Contribution

Please do contribute

Resource example for all functions

Acknowledgements

Buy me a coffe

  • [You can buy me a coffee] (https://www.buymeacoffee.com/solucien/)

Support

For support, email info@solucien or find me on Twitter via lucien_mendela.