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

mpesa-node-api

v1.1.0

Published

Node.js library for M-Pesa API (Mozambique)

Downloads

145

Readme

M-Pesa Node API

Node.js wrapper for the M-Pesa Mozambique API.

Using the API

  1. Install it using npm:

    npm install mpesa-node-api
  2. Create the configuration .env file on your root directory based on .env.example.

  3. Use your favorite text editor to edit the .env file and fill in the blank lines with configuration you got from the M-Pesa Developer Portal. See an example:

    MPESA_PUBLIC_KEY=example_public_key
    MPESA_API_HOST=api.sandbox.vm.co.mz
    MPESA_API_KEY=example_api_key
    MPESA_ORIGIN=developer.mpesa.vm.co.mz
    MPESA_SERVICE_PROVIDER_CODE=171717
  4. In your JavaScript file, import the package using require():

    const mpesa = require('mpesa-node-api');

Supported Transactions

All transactions return a Promise. So you can either use then/catch or async/await to get the transaction response.

Responses match the ones specified on the M-Pesa API Documentation.

Example C2B response:

{
  "output_ConversationID": "f02f957c19f4499faf6a6f19c0307e69",
  "output_ResponseCode": "INS-0",
  "output_ResponseDesc": "Request processed successfully",
  "output_ThirdPartyReference": "ZXVM9H",
  "output_TransactionID": "f449abol7j38"
}

Customer to Business (C2B)

const mpesa = require('mpesa-node-api');

mpesa.initiate_c2b(/* amount */ 10, /* msisdn */ 258843330333, /* transaction ref */ 'T12344C', /*3rd party ref*/ 'ref1')
    .then(function(response) {
      // logging the response
      console.log(response);
    })
    .catch(function(error) {
      // TODO: handle errors
    });

Business to Customer (B2C)

const mpesa = require('mpesa-node-api');

mpesa.initiate_b2c(/* amount */ 10, /* msisdn */ 258843330333, /* transaction ref */ 'T12344C', /*3rd party ref*/ 'ref1')
    .then(function(response) {
      // logging the response
      console.log(response);
    })
    .catch(function(error) {
      // TODO: handle errors
    });

Planned Support:

  • [ ] B2B
  • [ ] Reversal
  • [ ] Query Transaction Status

Using custom configuration

Optionally, you can also use custom configuration to initialize the API:

const mpesa = require('mpesa-node-api');

mpesa.initializeApi({
    baseUrl: "YOUR_MPESA_API_HOST",
    apiKey: "YOUR_MPESA_API_KEY",
    publicKey: "YOUR_MPESA_PUBLIC_KEY",
    origin: "YOUR_MPESA_ORIGIN",
    serviceProviderCode: "YOUR_MPESA_SERVICE_PROVIDER_CODE"
});
mpesa.initiate_c2b( 10, 258843330333, 'T12344C', 'ref1');

Getting a copy for development

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

Make sure you have installed Node.js, which comes with npm.

Installing

  1. Fork the GitHub repository.
  2. Clone it to your local machine using git clone https://github.com/<YOUR-USERNAME>/mpesa-node-api.git
  3. Navigate into the project's directory using cd mpesa-node-api;
  4. Install dependencies using npm run install.

Contributing

Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Inspired by the mpesa-php-api created by Abdul Mueid.