项目作者: rosariopfernandes

项目描述 :
NodeJS library for M-Pesa API (🇲🇿 Mozambique)
高级语言: JavaScript
项目地址: git://github.com/rosariopfernandes/mpesa-node-api.git
创建时间: 2020-06-16T10:35:50Z
项目社区:https://github.com/rosariopfernandes/mpesa-node-api

开源协议:MIT License

下载


M-Pesa Node API

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

Using the API

  1. Install it using npm:

    1. 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:

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

    1. 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:

  1. {
  2. "output_ConversationID": "f02f957c19f4499faf6a6f19c0307e69",
  3. "output_ResponseCode": "INS-0",
  4. "output_ResponseDesc": "Request processed successfully",
  5. "output_ThirdPartyReference": "ZXVM9H",
  6. "output_TransactionID": "f449abol7j38"
  7. }

Customer to Business (C2B)

  1. const mpesa = require('mpesa-node-api');
  2. mpesa.initiate_c2b(/* amount */ 10, /* msisdn */ 258843330333, /* transaction ref */ 'T12344C', /*3rd party ref*/ 'ref1')
  3. .then(function(response) {
  4. // logging the response
  5. console.log(response);
  6. })
  7. .catch(function(error) {
  8. // TODO: handle errors
  9. });

Business to Customer (B2C)

  1. const mpesa = require('mpesa-node-api');
  2. mpesa.initiate_b2c(/* amount */ 10, /* msisdn */ 258843330333, /* transaction ref */ 'T12344C', /*3rd party ref*/ 'ref1')
  3. .then(function(response) {
  4. // logging the response
  5. console.log(response);
  6. })
  7. .catch(function(error) {
  8. // TODO: handle errors
  9. });

Planned Support:

  • B2B
  • Reversal
  • Query Transaction Status

Using custom configuration

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

  1. const mpesa = require('mpesa-node-api');
  2. mpesa.initializeApi({
  3. baseUrl: "YOUR_MPESA_API_HOST",
  4. apiKey: "YOUR_MPESA_API_KEY",
  5. publicKey: "YOUR_MPESA_PUBLIC_KEY",
  6. origin: "YOUR_MPESA_ORIGIN",
  7. serviceProviderCode: "YOUR_MPESA_SERVICE_PROVIDER_CODE"
  8. });
  9. 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.