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-mz-nodejs-lib

v0.8.1

Published

A node.js library for the M-Pesa Mozambique API

Downloads

148

Readme

mpesa-mz-nodejs-lib

Github Github Maintainability Test Coverage Build Status GitHub

A Node.js library for the M-Pesa Mozambique API

Initially, a port of mpesa-php-api to Node.js

Read documentation here

Version 0.7.11

Currently in beta version. Stable but not production-ready (v1.0.0)

Features

  • Simple syntax
  • Promise-based
  • Agnostic library. Treats config and transaction details as data, rather than dependencies
  • Validation of transaction parameters to ensure only valid requests get sent to the MPesa API

Status

  • [x] C2B Transaction
  • [x] B2C Transaction
  • [x] Transaction status query
  • [x] Transaction reversal

Roadmap

  • [x] Unit tests passing for all features (v0.4.x)
  • [x] Continuous Integration (v0.5.x)
  • [x] Documentation (code coverage, features) (v0.6.x)
  • [x] Linting, code quality (v0.7.x)
  • [ ] Code refactoring (entire codebase) (v0.8.x)
  • [ ] Rename library (v0.9.x)
  • [ ] All tests passing, functionalities stable (v1.0.0)

Installation

npm install mpesa-mz-nodejs-lib

Examples

All parameters used in the examples correspond to the parameters required by the MPesa API. See the documentation on the MPesa Developer Portal for more information.

Customer to Business (C2B) transaction

// include the library
Transaction = require('mpesa-mz-nodejs-lib')

// create the config object
var config = {
    public_key: '<Public key>',
    api_host: 'api.sandbox.vm.co.mz',
    api_key: '<API key>',
    origin: '<Origin>',
    service_provider_code: '<Service provider code>',
    initiator_identifier: '<Initiator Identifier>',
    security_credential: '<Security Credential>'
}

// instantiate the Transaction object, initializing it with valid config
transaction = new Transaction(config)

// initiate a promise-based C2B transaction
transaction.c2b({
    amount: <floating-point number>,
    msisdn: '<valid/invalid MSISDN>',
    reference: '<Transaction Reference>',
    third_party_reference: '<Third-party reference>'
  })
  // handle success
  .then(function(response){
      console.log(response)
  })
  // handle error
  .catch(function(error){
      console.log(error)
  })

Querying status of an existing transaction

// ... Assuming an initialized Transaction object

// query the status of an existing transaction
transaction
  .query({
    query_reference: '<Transaction reference>',
    third_party_reference: '<Third-party reference>'
  })
  // handle success
  .then(function (response) {
    console.log(response)
  })
  // handle error
  .catch(function (error) {
    console.log(error)
  })

Reversal of an existing transaction

// ... Assuming an initialized Transaction object

// Reverse a committed transaction
transaction
  .reverse({
    amount: '<floating-point number>',
    transaction_id: '<Transaction ID>',
    third_party_reference: '<Third-party reference>'
  })
  // handle success
  .then(function (response) {
    console.log(response)
  })
  // handle error
  .catch(function (error) {
    console.log(error)
  })

Error-handling

The parameters for the initialization of the Transaction object as well as for the c2b, query and reversal methods are validated by the library.

If any parameter is non-existent, empty or invalid, the library throws the error:

Missing or invalid Config/C2B/Query/Reversal parameters

The names of the missing or invalid parameters are also appended to the error message.

Example

Transaction = require('mpesa-mz-nodejs-lib')

// create the config object, missing api_host
var config = {
  public_key: '<Public key>',
  api_key: '<API key>',
  origin: '<Origin>',
  service_provider_code: '<Service provider code>',
  initiator_identifier: '<Initiator Identifier>',
  security_credential: '<Security Credential>'
}

// instantiate the Transaction object, initializing it with incomplete config
transaction = new Transaction(config)

Will throw a Missing or invalid Configuration parameter: API Host error

And

Transaction = require('mpesa-mz-nodejs-lib')

// create the config object, missing api_host
var config = {
    public_key: '<Public key>',
    api_host: '<API host>',
    api_key: '<API key>',
    origin: '<Origin>',
    service_provider_code: '<Service provider code>',
    initiator_identifier: '<Initiator Identifier>',
    security_credential: '<Security Credential>'
}

// instantiate the Transaction object, initializing it with incomplete config
transaction = new Transaction(config)

// initiate a C2B transaction, missing reference parameter
transaction.c2b({
    amount: <floating-point number>,
    msisdn: '<valid/invalid MSISDN>',
    third_party_reference: '<Third-party reference>'
  })
  // handle success
  .then(function(response){
      console.log(response)
  })
  // handle error
  .catch(function(error){
      console.log(error)
  })
})

Will throw a Missing or invalid C2B parameter: C2B Reference error

Responses

Response format from MPesa API:

{
  output_ResponseCode: 'INS-0',
  output_ResponseDesc: 'Request processed successfully',
  output_ResponseTransactionStatus: 'Completed',
  output_ConversationID: '3b46f68931324acb857ae4fe52b826b5',
  output_ThirdPartyReference: 'XXXXX'
}

The response format provided by the HTTP client library used, (Axios), is structured as:

{
  status: '',
  statusText: '',
  headers: {},
  config: {},
  data: {}
}

In the current version of the library, all returned objects correspond to the data property (data returned from MPesa API). Future versions will distinguish response (full Axios response object) from response.data in returned messages according to the environment (dev/prod)

Issue templates

Bug report

Feature request

License

MIT License © 2020 Ivan Ruby