Logo Lanfrica

E-ugine/flask-mpesa-integration

Domain:

digital infrastructure

Record type:

software
Creator:
E-u
Host:
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 …

Languages