Giter Site home page Giter Site logo

mpesapay's Introduction

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

Dev_riz
Dev_riz

๐Ÿ’ป
Justine Gichana
Justine Gichana

๐Ÿ’ป
Dre
Dre

๐Ÿ’ป

Justine Gichana

Github URL

Full Documentation

Contributing

Support Via Paypal

mpesapay's People

Contributors

justinelut avatar github-actions[bot] avatar allcontributors[bot] avatar hamisirizwan avatar odidaprotas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.