https://github.com/thatfiredev/mpesa-node-api
NodeJS library for M-Pesa API (🇲🇿 Mozambique)
https://github.com/thatfiredev/mpesa-node-api
mozambique mpesa mpesa-api
Last synced: 3 days ago
JSON representation
NodeJS library for M-Pesa API (🇲🇿 Mozambique)
- Host: GitHub
- URL: https://github.com/thatfiredev/mpesa-node-api
- Owner: thatfiredev
- License: mit
- Created: 2020-06-16T10:35:50.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-23T02:58:43.000Z (10 months ago)
- Last Synced: 2024-12-07T09:14:02.121Z (4 months ago)
- Topics: mozambique, mpesa, mpesa-api
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 27
- Watchers: 3
- Forks: 14
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - thatfiredev/mpesa-node-api - NodeJS library for M-Pesa API (🇲🇿 Mozambique) (JavaScript)
README
# M-Pesa Node API
Node.js wrapper for the M-Pesa Mozambique API.
## Using the API
1. Install it using npm:
```shell
npm install mpesa-node-api
```
1. Create the configuration `.env` file on your root directory based on [`.env.example`](.env.example).
1. 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](https://developer.mpesa.vm.co.mz/). See an example:
```shell
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
```
1. In your JavaScript file, import the package using `require()`:
```js
const mpesa = require('mpesa-node-api');
```
### Supported TransactionsAll transactions return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mpesa.vm.co.mz/apis/).Example C2B response:
```json
{
"output_ConversationID": "f02f957c19f4499faf6a6f19c0307e69",
"output_ResponseCode": "INS-0",
"output_ResponseDesc": "Request processed successfully",
"output_ThirdPartyReference": "ZXVM9H",
"output_TransactionID": "f449abol7j38"
}
```#### Customer to Business (C2B)
```js
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)
```js
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:
```js
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](https://nodejs.org/en/), which comes with `npm`.
### Installing
1. Fork the GitHub repository.
1. Clone it to your local machine using `git clone https://github.com//mpesa-node-api.git`
1. Navigate into the project's directory using `cd mpesa-node-api`;
1. Install dependencies using `npm run install`.## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process
for submitting pull requests to us.## License
This project is licensed under the [MIT License](LICENSE) - see the [LICENSE](LICENSE) file for
details.## Acknowledgments
Inspired by the [mpesa-php-api](https://github.com/abdulmueid/mpesa-php-api) created by
[Abdul Mueid](https://github.com/abdulmueid/).