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

mpesapay

v4.4.1

Published

A module for processing MPESA payments

Downloads

190

Readme

Mpesa Pay

Mpesa Pay is a JavaScript library that enables developers to easily integrate Mpesa payments into their Node.js, Next.js, SvelteKit, and Nuxt.js applications. It provides an easy-to-use interface for initiating STK Push requests, facilitating Business-to-Business transactions, checking Account Balances, and verifying Transaction statuses.

Installation

You can install Mpesa Pay using Yarn, NPM & PNPM.

Yarn

yarn add mpesapay

NPM

npm install mpesapay

PNPM

pnpm install mpesapay

Usage

Import default MpesaPay from the mpesapay module in your Node.js, Nextjs, SveltKit, Nuxtjs application:

import MpesaPay from 'mpesapay';

Or use commonjs syntax:

const { MpesaPay } = require('mpesapay');

or;

const MpesaPay = require('mpesapay').default;

Create an instance of the MpesaPay class with your Mpesa API credentials:

const Consumer_Key = 'YOUR_CONSUMER_KEY';
const Consumer_Secret = 'YOUR_CONSUMER_SECRET';
const Business_Short_Code = 'YOUR_BUSINESS_SHORT_CODE';
const Passkey = 'YOUR_PASS_KEY';
const Transaction_Description = 'YOUR_TRANSACTION_DESCRIPTION';
const Account_Reference = 'YOUR_ACCOUNT_REFERENCE';
const PartyA = 'YOUR_MPESA_PARTYA';
const B2C_Security_Credential = 'YOUR MPESA B2C SECURITY CREDENTIAL';
const Initiator_Name = 'YOUR MPESA INITIATORS NAME';
const Environment = 'THE ENVIRONMENT i.e sandbox or live';
const Transaction_Type = 'YOUR SHORTCODE TYPE i.e paybill or till';

const mpesapay = new MpesaPay(
  Consumer_Key,
  Consumer_Secret,
  Business_Short_Code,
  Passkey,
  Account_Reference,
  Transaction_Description,
  PartyA,
  B2C_Security_Credential,
  Initiator_Name,
  Environment,
  Transaction_Type
);

Replace the placeholders with your actual Mpesa credentials.

To get your Mpesa credentials, you can follow the procedures outlined in our official documentation available at Getting Mpesa Credentials.

Initiating a Payment

To initiate a payment using Mpesa Pay, you can call the stkPush method:

async function initiatePayment(amount, phoneNumber, callbackUrl) {
  try {
    const response = await mpesapay.stkPush(amount, phoneNumber, callbackUrl);
    console.log(response);
    // Handle the response data
  } catch (error) {
    console.error(error);
    // Handle errors
  }
}

// Call the function to initiate a payment
const amount = '100';
const phoneNumber = '254712345678';
const callbackUrl = 'https://example.com/callback';
initiatePayment(amount, phoneNumber, callbackUrl);

Here is an explanation of the parameters:

  • amount: The amount of money to be paid. This should be a string value representing the amount in Kenyan shillings.
  • phoneNumber: The phone number of the person making the payment. This should be a string value starting with the country code (e.g., "254" for Kenya), followed by the phone number without any spaces or special characters.
  • callbackUrl: The URL that M-Pesa will use to send payment confirmation notifications to your server. This should be a string value containing a valid URL.

The stkPush method initiates the payment process and returns a Promise. The Promise resolves with the response data if the payment was successfully initiated, or rejects with an error if the payment initiation fails.

Make sure to handle the response data and errors accordingly in your application.

TypeScript Support

The Mpesa Pay module includes TypeScript type definitions, providing enhanced development experience and type checking capabilities when using the library in a TypeScript project.

That's it! You can now integrate Mpesa payments into your Node.js application using the Mpesa Pay library.

Please note that stkPush only initiates payments, and the results will be sent to the provided callback URL. Make sure to implement the necessary server-side logic to handle the payment confirmation notifications and update your database accordingly.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

All Contributors

Justine Gichana

Github URL

Full Documentation

Contributing

Support Via Paypal