# Django Mpesa Integration (STK Push, Query, and Callback)
This Django app integrates **Safaricom M-Pesa Daraja API** to perform **STK Push**, **Query Transaction Status**, and handle **Callback Responses**.
It provides a clean, modular implementation for M-Pesa payments in Django-based systems.
---
## Features
- Automatic OAuth Access Token generation
- **STK Push** payment initiation
- **Transaction Status Query** support
- **Callback** response handling
- Built for the **Sandbox** environment and easily configurable for production
---
## đź§± Project Structure
```
mpesa_app/
│
├── stkPush.py # Handles M-Pesa STK Push request
├── query_stk.py # Handles STK push query status
├── callback.py # Handles M-Pesa callback responses
└── generateAccessToken.py # Fetches OAuth access token from Safaricom Daraja API
```
---
## ⚙️ Requirements
- Python 3.10+
- Django 4+
- `requests` library
Install dependencies:
```bash
pip install django requests
```
---
## 🔑 Configuration
### 1. Safaricom Developer Setup
- Sign up on Safaricom Developer Portal
- Create an app and obtain:
- **Consumer Key**
- **Consumer Secret**
- Use the **Sandbox** environment during testing.
### 2. Environment Variables (Recommended)
Create a `.env` file or use Django’s settings to securely store credentials:
```bash
MPESA_CONSUMER_KEY=your_consumer_key
MPESA_CONSUMER_SECRET=your_consumer_secret
MPESA_SHORTCODE=174379
MPESA_PASSKEY=bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919
MPESA_CALLBACK_URL=
yourdomain.com
```
Then load them using `os.environ.get()` within your Python files.
---
## đź§© Endpoints Overview
| Endpoint | Method | Description |
| ---------------- | ------ | ----------------------------------- |
| `/access-token/` | `GET` | Generates OAuth token |
| `/stk-push/` | `POST` | Initiates an STK push request |
| `/stk-query/` | `POST` | Queries STK …