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 🙏

© 2025 – Pkg Stats / Ryan Hefner

paympesa

v2.0.3

Published

A nodejs package to handle M-Pesa (Daraja-Api) operations such as authorization, STK Push, callbacks and more

Readme

Introduction

payMpesa is a Node.js library that simplifies the integration of Safaricom's M-Pesa API for seamless mobile payments in Kenya. It provides easy-to-use functions for STK Push transactions, payment confirmation, QR code generation, and more.

Features

  • Initiate STK Push transactions
  • Confirm payment status
  • Handle STK Push callbacks
  • Generate QR codes for M-Pesa payments
  • Easy configuration and extensibility

Installation

To install PayMpesa, run the following command:

npm install paympesa

or using pnpm:

pnpm add paympesa

or using Yarn:

yarn add paympesa

Configuration

Before using the package, you need to set up the necessary configurations.

STK Push Configuration

const { StkpushConfig } = require("paympesa");

const stkConfig = new StkpushConfig({
  consumerKey: "your_consumer_key",
  consumerSecret: "your_consumer_secret",
  shortCode: "your_shortcode",
  passkey: "your_passkey",
  transactionType: "CustomerPayBillOnline", // or "CustomerBuyGoodsOnline"
  accountReference: "TestTransaction",
  transactionDesc: "Payment for services",
  authUrl: "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",
  stkPushUrl: "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest",
  queryUrl: "https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query",
  callbackUrl: "https://your-callback-url.com/callback",
});

QR Code Configuration

const { QrCodeConfig } = require("paympesa");

const qrConfig = new QrCodeConfig({
  MerchantName: "TEST SUPERMARKET",
  merchantRequestID: "REQ002",
  RefNo: "Invoice Test",
  Amount: 1,
  TrxCode: "PB",
  CPI: "373132",
  Size: "300",
  qrCodeUrl: "https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate",
  consumerKey: "your_consumer_key",
  consumerSecret: "your_consumer_secret",
  saveLocally: true,
  amount: 500,
  refNumber: "REF500",
  shortCode: "654321",
  transactionType: "CustomerBuyGoodsOnline",
  authUrl: "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",
});

Usage

Initiate STK Push

const { initiateSTKPush } = require("paympesa");

async function sendSTKPush() {
  try {
    const response = await initiateSTKPush(stkConfig, {
      phoneNumber: "2547XXXXXXXX",
      amount: 100,
    });
    console.log("STK Push Response:", response);
  } catch (error) {
    console.error("Error initiating STK Push:", error.message || error);
  }
}

sendSTKPush();

Confirm Payment

const { confirmPayment } = require("paympesa");

async function checkPaymentStatus(CheckoutRequestID) {
  try {
    const response = await confirmPayment(stkConfig, CheckoutRequestID);
    console.log("Payment Confirmation Response:", response);
  } catch (error) {
    console.error("Error confirming payment:", error.message || error);
  }
}

checkPaymentStatus("your_checkout_request_id");

Handle STK Push Callback

const { stkPushCallback } = require("paympesa");

async function processCallback(callbackData) {
  try {
    const result = await stkPushCallback(callbackData);
    console.log("Callback Response:", result);
  } catch (error) {
    console.error("Error processing callback:", error.message || error);
  }
}

// Example callback data
const sampleCallbackData = {
  Body: {
    stkCallback: {
      MerchantRequestID: "123456",
      CheckoutRequestID: "789012",
      ResultCode: 0,
      ResultDesc: "Success",
    },
  },
};

processCallback(sampleCallbackData);

Generate QR Code

const { generateQRCode } = require("paympesa");

async function createQRCode() {
  try {
    const qrCodeResponse = await generateQRCode(qrConfig);
    console.log("QR Code Response:", qrCodeResponse);
  } catch (error) {
    console.error("Error generating QR Code:", error.message || error);
  }
}

createQRCode();

Developer

Paul
🚀 Web Developer & Open-Source Contributor
📧 Email: [email protected]
🐦 Twitter: @MarcochollaP
💼 LinkedIn: marcocholla
📖 GitHub: @Marcocholla01

License

This project is licensed under the MIT License.


Note:

  • generateQRCode() is not fully Completed hence might or not work properly.
  • Replace the sandbox with api if going live