A Nodejs implementation of Safaricom's Lipa na Mpesa Online Payment Api. Use this as a starting point template for your app.
Credentials are required including:
- Consumer Key
- Consumer Secret
- Passkey
The consumer key & secret can be obtained by registering for an account here https://developer.safaricom.co.ke/ and once you have an account, the passkey can be got here https://developer.safaricom.co.ke/test_credentials
To automatically get the correct dependencies used in this project, ensure you have Node.js installed then:
- Clone the project by running
git clone https://github.com/petekmunz/Lipa-Na-Mpesa_Nodejs.git
in your Git terminal. - In your terminal ensure you are in the directory
Lipa-Na-Mpesa_Nodejs
then runnpm install
- Create a .env file in the root directory with the following variables.
NODE_ENV=development
MYPORT=3000 //You can set to your preferrable port
CONSUMER_KEY=******************
CONSUMER_SECRET=***************
PASSKEY=***************
SHORTCODE=174379 //This is the test shortcode
- In the app.js file, replace the url here
"CallBackURL": "http://example.com/api/v1/c2bconfirmation"
with your actual callback url. The endpoint needs to be exposed to the internet if it is to receive a payload from Safaricom.
- Make a POST request to the endpoint
http://localhost:3000/mpesa
with a JSON with the parameters as in the example below
{
"phoneNumber":"254722xxxxxx",
"amount":11
}
When all the required variables have been input, a succesful request will result in a STK push as shown below