This Node.js application integrates Lipa na Mpesa STK Push functionality, allowing users to make payments seamlessly using Safaricom's Mpesa API. It provides secure access to Safaricom's sandbox environment for testing, and handles requests, callbacks, and payment confirmation.
- 🛡️ STK Push Integration: Securely initiate STK push requests to Mpesa.
- 🔄 Payment Confirmation: Confirm the status of the Mpesa payment.
- 📞 Callback Handling: Efficiently process Mpesa callbacks.
- 🔑 Access Token Generation: Securely generate and use access tokens for Safaricom API.
- 🌐 Ngrok Integration: Use Ngrok for creating secure tunnels for testing callbacks.
git clone https://github.com/mohswell/mpesa-app.git
cd mpesa-app
npm install
Create a .env
file in the root directory of your project and fill in the following details:
PORT=3000
BUSINESS_SHORT_CODE=123456
PASS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SAFARICOM_CONSUMER_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SAFARICOM_CONSUMER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
npm start
HTTP Method | Endpoint | Description |
---|---|---|
POST |
/api/stkPush |
Initiates an STK push to a given phone number |
POST |
/api/stkPushCallback/:Order_ID |
Handles callback data from Safaricom |
POST |
/api/confirmPayment/:CheckoutRequestID |
Confirms the status of a payment |
.
├── controllers
│ └── controllers.lipanampesa.js # Contains Lipa Na Mpesa logic
├── middlewares
│ └── middlewares.generateAccessToken.js # Handles access token generation
├── routes
│ └── routes.lipanampesa.js # Defines API routes for Mpesa
├── utils
│ └── utils.timestamp.js # Utility function for generating timestamps
├── .env # Example environment configuration
├── server.js # Main application file
└── README.md # Project documentation
The user triggers an STK push request via the /stkPush
endpoint. The application sends the necessary details to Safaricom's API, initiating the push request on the user's phone.
Safaricom sends a callback once the transaction is completed. The callback is processed via the /stkPushCallback/:Order_ID
route, updating the transaction status.
You can confirm the payment status by querying Safaricom’s API through the /confirmPayment/:CheckoutRequestID
route.
- 💼 Production Environment Setup
- 📲 User Interface for Payment Initiation
- 🖥️ Admin Dashboard for Viewing Transactions
- 🛡️ Webhook Security
This project is licensed under the MIT License.