mpesa-online
A lightweight library for node.js that simplifies integrating your website/web app to mpesa using the Lipa na mpesa online payment API . The API documentation is quite adequate and contains sufficient code samples for several languages, you can check it out here. If you don't have a developers' account , please register and create an app. This step is very important in order to access test short codes and credentials required by the API.
This library provides a quick and painless means to integrate to MPESA. It will be useful to developers that want to get up and running as fast as possible and also presents an opportunity to effortlessly test the payment experience for those who are unfamiliar with the process.
Installation
$ npm i mpesa-online
Initiate mpesa transaction (processRequest)
The mpesaRequest() method expects 2 arguments - a params object and the request type (in this case processRequest). This request type will initiate an mpesa transaction and a USSD payment prompt (STK push) will be sent to your mobile device.
const MpesaOnline = const mpesa = mpesa
A sample of the params object:
const params = 'BusinessShortCode': '' // The organization shortcode used to receive the transaction. 'TransactionType': 'CustomerPayBillOnline' // The transaction type to be used for this request. 'Amount': '1' // Amount to be charged / paid 'PartyA': '' // The mobile number sending the funds. 'PartyB': '' // The organization shortcode receiving the funds 'PhoneNumber': '' // The mobile number sending the funds. 'CallBackURL': '' // The url to where responses from M-Pesa will be sent to. 'AccountReference': '' // Used with M-Pesa PayBills 'TransactionDesc': 'Testing mpesa online' 'consumerKey': '' 'consumerSecret': '' 'passKey': '' // Used to create a password for use when making a Lipa Na M-Pesa Online Payment API calls 'authenticationURL': 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials' // MPESA authentication end point 'processRequestURL': 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest' // MPESA request processing end point
here. A link to the test credentials is provided in the instructions.
You can retrieve the BusinessShortCode (Lipa Na Mpesa Online Shortcode), PartyB and passKey (Lipa Na Mpesa Online Passkey) valuesTo obtain the consumerKey and consumerSecret:
- Login to the developers' portal
- Click on your app.
- The 'Keys' tab contains both keys.
When you run your code, you should receive a USSD payment prompt(STK push) on your mobile device:
Check payment status(queryRequest)
The queryRequest request type checks the status of the payment.
const params = 'BusinessShortCode': '' 'CheckoutRequestID': '' 'consumerKey': '' 'consumerSecret': '' 'passKey': '' 'authenticationURL': '' 'queryRequestURL': 'https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query'
mpesa
Connecting the dots...
A snippet demonstrating how to make an mpesa payment request and query for the payment status.
// Add the mpesa-online moduleconst MpesaOnline = const mpesa = // The params required to make a processRequestconst processRequestParams = 'BusinessShortCode': '' 'TransactionType': 'CustomerPayBillOnline' 'Amount': '1' 'PartyA': '' 'PartyB': '' 'PhoneNumber': '' 'CallBackURL': '' 'AccountReference': '4002' 'TransactionDesc': 'Testing mpesa online' 'consumerKey': '' 'consumerSecret': '' 'passKey': '' 'authenticationURL': 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials' 'processRequestURL': 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest' // Use processRequest to receive the USSD payment prompt(STK push) on your mobile devicempesa const poll = { const endTime = Number + timeout const query = { } return query}