Skip to content

This is a Node.Js project that inititiates an Mpesa STK popup on a user phone, and prompt them for their PIN, if entered successful, the status is saved to a JSON file.

Notifications You must be signed in to change notification settings

mohswell/mpesa-app

Repository files navigation

📱 Lipa na Mpesa Integration with Node.js

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.

🚀 Features

  • 🛡️ 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.

🛠️ Technologies Used

  • Node.js
  • Express.js
  • Request
  • Ngrok
  • Dotenv
  • Mpesa API

⚙️ Setup Instructions

1. Clone the repository

git clone https://github.com/mohswell/mpesa-app.git
cd mpesa-app

2. Install dependencies

npm install

3. Environment Setup

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

4. Start the application

npm start

📜 Endpoints

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

🔧 Project Structure

.
├── 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

📝 How It Works

Initiate STK Push

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.

Handle Callback

Safaricom sends a callback once the transaction is completed. The callback is processed via the /stkPushCallback/:Order_ID route, updating the transaction status.

Confirm Payment

You can confirm the payment status by querying Safaricom’s API through the /confirmPayment/:CheckoutRequestID route.


🎯 Future Improvements

  • 💼 Production Environment Setup
  • 📲 User Interface for Payment Initiation
  • 🖥️ Admin Dashboard for Viewing Transactions
  • 🛡️ Webhook Security

🏷️ License

This project is licensed under the MIT License.


👥 Contributors

About

This is a Node.Js project that inititiates an Mpesa STK popup on a user phone, and prompt them for their PIN, if entered successful, the status is saved to a JSON file.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published