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

pesapal3

v1.1.2

Published

pesapal api version 3 node library

Downloads

602

Readme

pesapal3

A nodejs implementation of the pesapal payment gateway API. A simple library for making payments with PesaPal.

Features

pesapal3 implements a secure pesapal payment gateway for nodejs. Its main features are:

Reliability

Connections are established even in the presence of:

  • proxies and load balancers.
  • personal firewall and antivirus software.

Minimalistic implementation

You do not need to understand the internal structures and operation of the systems.

Simple and convenient API

Sample code:

import { Pesapal, Iconfig } from "pesapal3";

const config: Iconfig = {
  pesapalEnvironment: "live", // or sandbox
  pesapalConsumerKey: "your-consumer-key",
  pesapalConsumerSecret: "your-consumer-secret",
  pesapalIpnUrl: "https://yourserverdomain/pesapal/ipn",
  pesapalCallbackUrl: "https://yoursitedomain/pesapal/callback",
};

const pesapal = new Pesapal(config);

// run the library with
const paymentInstance = pesapal.run();

// make order
const details: IpayDetails = {
  amount: 1000,
  currency: "UGX",
  description: "This is a test payment",
  // ...other_details
};

const ordered = await paymentInstance.submitOrder(
  details,
  "ProductId",
  "description"
);

if (ordered.success) {
  // The make payment request was successful.
} else {
  // The make payment request failed.
}

Installation

// with npm
npm install pesapal3

// with yarn
yarn add pesapal3

How to use

The following example inits pesapal3 and gets the chat client and chat controller instances.

import { Pesapal, Iconfig, IpayDetails } from "pesapal3";

const config: Iconfig = {
  pesapalEnvironment: "live", // or sandbox
  pesapalConsumerKey: "your-consumer-key",
  pesapalConsumerSecret: "your-consumer-secret",
  pesapalIpnUrl: "https://yourserverdomain/pesapal/ipn",
  pesapalCallbackUrl: "https://yoursitedomain/pesapal/callback",
};

const pesapal = new Pesapal(config);

// run the library with
const paymentInstance = pesapal.run();

// make order
const details: IpayDetails = {
  amount: 1000,
  currency: "UGX",
  description: "This is a test payment",
  // ...other_details
};

const ordered = await paymentInstance.submitOrder(
  details,
  "ProductId",
  "description"
);

if (ordered.success) {
  // The make payment request was successful.
} else {
  // The make payment request failed.
}

//check status using the order tracking id provided by pesapal

const checkStatus = await paymentInstance.getTransactionStatus(
  "orderTrackingId"
);

// provide an api ipn notfication call back
paymentRoutes.get("/ipn", async (req, res) => {
  const currntUrl = new URL(req.url);
  // get access to URLSearchParams object
  const searchParams = currntUrl.searchParams;

  // get url parameters
  const orderTrackingId = searchParams.get("OrderTrackingId") as string;
  const orderNotificationType = searchParams.get(
    "OrderNotificationType"
  ) as string;
  const orderMerchantReference = searchParams.get(
    "OrderMerchantReference"
  ) as string;

  // make sure you use main pesapal instance and not constrct another one

  if (!paymentInstance && !related) {
    return res
      .status(500)
      .send({ success: false, err: "internal server error" });
  }

  // return relegatePesaPalNotifications(orderTrackingId, orderNotificationType, orderMerchantReference);
  const response = await paymentInstance.getTransactionStatus(
    orderTrackingId,
    related._id
  );
  return response;
});

Register IPN

import { PesaPalController, IrefundRequestReq } from "pesapal3";
const config: Iconfig = {
  pesapalEnvironment: "live", // or sandbox
  pesapalConsumerKey: "your-consumer-key",
  pesapalConsumerSecret: "your-consumer-secret",
  pesapalIpnUrl: "https://yourserverdomain/pesapal/ipn",
  pesapalCallbackUrl: "https://yoursitedomain/pesapal/callback",
};

const pesapal = new Pesapal(config);

// run the library with
const paymentInstance = pesapal.run();

/** By default they are, as this uses the values provided in the config, but if you
 * want to specify additional ipn end points for transcation notification and response type
 * you can pass in the parameters as below
 */
const ipn = "https://myserverdomain.com";
const notificationMethodType = "GET";

const response = await registerIpn(pin, notificationMethodType);

Get Ipn Endpoints

If you want to get all the registered Ipn end point you would use the getIpnEndPoints

import { PesaPalController, IrefundRequestReq } from "pesapal3";
const config: Iconfig = {
  pesapalEnvironment: "live", // or sandbox
  pesapalConsumerKey: "your-consumer-key",
  pesapalConsumerSecret: "your-consumer-secret",
  pesapalIpnUrl: "https://yourserverdomain/pesapal/ipn",
  pesapalCallbackUrl: "https://yoursitedomain/pesapal/callback",
};

const pesapal = new Pesapal(config);

// run the library with
const paymentInstance = pesapal.run();

const response = await paymentInstance.getIpnEndPoints();

Get Transcation Status

import { PesaPalController, IrefundRequestReq } from "pesapal3";
const config: Iconfig = {
  pesapalEnvironment: "live", // or sandbox
  pesapalConsumerKey: "your-consumer-key",
  pesapalConsumerSecret: "your-consumer-secret",
  pesapalIpnUrl: "https://yourserverdomain/pesapal/ipn",
  pesapalCallbackUrl: "https://yoursitedomain/pesapal/callback",
};

const pesapal = new Pesapal(config);

// run the library with
const paymentInstance = pesapal.run();

const orderTrackingId = "15113631262323623734734";

const response = await paymentInstance.getTransactionStatus(orderTrackingId);

Recurring transactions

If you want to implement recurring subscriptions, you would do it as follows

Refund

Refund of users payment is made you would use the api below

import { PesaPalController, IrefundRequestReq } from 'pesapal3'
const config: Iconfig = {
  pesapalEnvironment: "live", // or sandbox
  pesapalConsumerKey: "your-consumer-key",
  pesapalConsumerSecret: "your-consumer-secret",
  pesapalIpnUrl: "https://yourserverdomain/pesapal/ipn",
  pesapalCallbackUrl: "https://yoursitedomain/pesapal/callback",
};

const pesapal = new Pesapal(config);

// run the library with
const paymentInstance = pesapal.run();

// provide refund objcet
const refundRequestReq: IrefundRequestReq {
  confirmation_code: 'confirmation_code'
  amount: '100',
  username: 'John Doe',
  remarks: 'product out of stock'
}


// refundRequest
const response = await paymentInstance.refundRequest()

Documentation

The full documentation for pesapal3 is available on here. Contributions are welcome!

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site.

License

pesapal3 is licensed under the MIT License. MIT