Mpesa Integration
# M-Pesa STK Push Integration
This project is a full-stack application for integrating Safaricom's M-Pesa STK Push (Lipa Na M-Pesa Online) into a web application using Node.js/Express for the backend and React (Vite) for the frontend.
## Features
- **STK Push Initiation**: Send payment requests directly to a user's phone.
- **Real-time Updates**: Uses Socket.io to provide instant feedback to the user when the payment is completed or failed.
- **Transaction Storage**: Saves all transaction details in MongoDB for future reference.
- **Secure Authentication**: Handles OAuth token generation for Safaricom's Daraja API.
## Prerequisites
- Node.js installed on your machine.
- A MongoDB database (local or MongoDB Atlas).
- Safaricom Daraja API credentials (Consumer Key, Consumer Secret, Passkey, Shortcode).
- Ngrok or a similar tool to expose your local server for the Safaricom Callback URL.
## Setup Instructions
### 1. Backend Setup
1. Navigate to the root directory.
2. Install dependencies:
```bash
npm install
```
3. Create a `.env` file in the root directory and add the following environment variables:
```env
PORT=8080
MONGO_URI=your_mongodb_connection_string
MPESA_CONSUMER_KEY=your_daraja_consumer_key
MPESA_SECRET_KEY=your_daraja_consumer_secret
MPESA_SHORTCODE=your_daraja_shortcode
MPESA_PASSKEY=your_daraja_passkey
CALLBACK_URL=
your-public-url.ngrok-free.…
```
4. Start the backend server:
```bash
npm run dev
```
### 2. Frontend Setup
1. Navigate to the `client` directory:
```bash
cd client
```
2. Install dependencies:
```bash
npm install
```
3. Start the Vite development server:
```bash
npm run dev
```
## How the App Works
1. **Frontend**: The user enters their phone number (in the format `2547xxxxxxxx`) and the amount they wish to pay.
2. **Initiating Payment**: When the "Pay Now" button is clicked, the frontend sends a POST request to the backend at `/api/stk/push`.
3. **Backend Processing**:
- The backend generate …