Logo Lanfrica

kevin-sammy/mpesa-daraja-integration

Domaine:

digital infrastructure

Type de record:

software
Créateur:
kev
Hôte:
Flask app integrating Safaricom Daraja API — STK Push, C2B, B2C payments, callback handling & transaction tracking. Built for Kenyan fintech workflows # 🟢 M-Pesa Daraja API Integration > A full Flask web application integrating Safaricom's Daraja API — STK Push, C2B, B2C, callbacks, and real-time transaction tracking. --- ## ✨ Features | Feature | Description | |---|---| | 📲 **STK Push** | Prompt customer's phone to enter M-Pesa PIN and pay | | 🏦 **C2B** | Register Paybill/Till callbacks, validate & confirm payments | | 💸 **B2C** | Send money from business to customer (salaries, refunds) | | 🔔 **Callbacks** | Full Safaricom callback handling with parsed results | | 📋 **Transaction Log** | Real-time log of all payment attempts and outcomes | | 🧪 **Unit Tests** | Tests for phone formatting, callback parsing, password generation | --- ## 🚀 Quick Start ```bash git clone github.com cd mpesa-daraja-integration pip install -r requirements.txt # Set up credentials cp .env.example .env # Edit .env with your Daraja API keys python run.py ``` Open **http://127.0.0.1:5000** --- ## 🔑 Getting Daraja Credentials 1. Go to **developer.safaricom.co.ke** 2. Create an account and log in 3. Go to **My Apps → Add New App** 4. Select **Lipa Na M-Pesa Sandbox** 5. Copy your **Consumer Key** and **Consumer Secret** 6. Paste them into your `.env` file --- ## 🌐 Exposing Callbacks with ngrok (Testing) Safaricom needs a public URL to send callbacks. Use ngrok for local testing: ```bash # Install ngrok from ngrok.com, then: ngrok http 5000 ``` Copy the HTTPS URL (e.g. `abc123.ngrok.io`) and set in `.env`: ``` MPESA_CALLBACK_URL=abc123.ngrok.io ``` --- ## 📁 Project Structure ``` mpesa-daraja-integration/ ├── run.py # Entry point ├── .env.example # Credentials template ├── requirements.txt ├── app/ │ ├── __init__.py # Flask factory │ ├── mpesa.py # ⭐ Core Daraja API client │ └── routes/ │ ├── main.py # UI routes (STK, C2B, B2C) │ └ …

Languages