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 β¦