Full-stack M-PESA payment integration tutorial. Covers authentication, STK Push, webhook callbacks, and SQLite storage. Built with Flask, ready for production deployment.
# 🚀 M-PESA Flask Starter
Complete M-PESA Daraja API integration with Flask. Implements STK Push, callback handling, and SQLite persistence. Perfect starter template for donation apps, e-commerce, and payment processing.
---
## ✨ Features
- ✅ **OAuth 2.0 Authentication** - Automatic token generation and caching
- ✅ **STK Push Implementation** - Trigger M-PESA payment prompts
- ✅ **Webhook Callback Handling** - Process payment confirmations in real-time
- ✅ **SQLite Database** - Persist transactions with SQLAlchemy ORM
- ✅ **Transaction Tracking** - Query by status, phone number, or date
- ✅ **Error Handling** - Comprehensive logging and validation
- ✅ **Sandbox Ready** - Test with Safaricom sandbox environment
- ✅ **Production Ready** - Easy migration to production credentials
---
## 🎯 Use Cases
- 💰 Donation platforms
- 🛒 E-commerce checkout
- 💳 Payment processing systems
- 📱 Mobile money integrations
- 🎫 Event ticketing
- 📚 Subscription services
---
## 📋 Prerequisites
- Python 3.8 or higher
- M-PESA Daraja API credentials (Get them here)
- Ngrok or Serveo account for webhook testing (optional for local development)
---
## 🚀 Quick Start
### 1. Clone the Repository
```bash
git clone
github.com
cd flask-mpesa-intergration
```
### 2. Create Virtual Environment
```bash
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# Mac/Linux:
source venv/bin/activate
```
### 3. Install Dependencies
```bash
pip install -r requirements.txt
```
### 4. Configure Environment Variables
Create a `.env` file in the project root:
```env
# M-PESA Daraja API Credentials (from developer portal)
MPESA_CONSUMER_KEY=your_consumer_key_here
MPESA_CONSUMER_SECRET=your_consumer_secret_here
# Sandbox Credentials (Standard - DO NOT CHANGE for testing)
MPESA_SHORTCODE=174379
MPESA_PASSKEY=bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919
# Environment
MPESA_ENV=sandbox
# Callback URL …