This project provides a Python-based M-Pesa Daraja API Payment Module for managing vendor subscription payments and handling edge cases like branch limits, payment failures, and subscription downgrades.
- Overview
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Edge Cases Handled
- Error Handling
- Testing Locally
This module integrates with Safaricom's Daraja API to facilitate Customer-to-Business (C2B) payments using the STK Push method (Lipa na M-Pesa). Vendors can pay for subscriptions based on tiers and add branches with additional fees.
- Subscription Tiers with branch limits and pricing.
- STK Push Payment Integration using M-Pesa Daraja API.
- Edge Case Handling:
- Branch limits enforcement.
- Payment failure retries with a grace period.
- Subscription downgrades with data compliance.
- Grace Period for payment retries.
- Detailed Debugging and Error Handling.
Before using this module, ensure you have:
- Python 3.x installed.
- Daraja API Credentials:
- Consumer Key
- Consumer Secret
- Shortcode
- Passkey
- Flask (for handling callbacks):
pip install Flask
- ngrok (to expose your local server to the internet):
Download ngrok
-
Clone the Repository:
git clone https://github.com/your-username/payment-module.git cd payment-module -
Install Dependencies:
pip install requests
-
Run Flask Server:
python callback_server.py
-
Expose Local Server via ngrok:
ngrok http 5000
Copy the ngrok public URL (e.g.,
https://1234abcd.ngrok.io) for use in thecallback_url.
Create a .env file to store your Daraja API credentials securely:
{
"consumer_key": "your_consumer_key",
"consumer_secret": "your_consumer_secret",
"shortcode": "your_shortcode",
"passkey": "your_passkey",
"callback_url": "https://1234abcd.ngrok.io/callback"
}from payment_module import PaymentModulepayment_module = PaymentModule(
consumer_key="your_consumer_key",
consumer_secret="your_consumer_secret",
shortcode="your_shortcode",
passkey="your_passkey",
callback_url="https://1234abcd.ngrok.io/callback"
)phone_number = "254712345678"
subscription_tier = "pro"
num_branches = 5
payment_module.initiate_payment(phone_number, subscription_tier, num_branches)- Starter Tier: Max 10 branches
- Pro Tier: Max 100 branches
- Enterprise Tier: Unlimited branches
If a vendor attempts to add more than 10 branches at once, an error is returned:
Error: You cannot add more than 10 branches at once.
If a payment fails due to insufficient funds, the system starts a 7-day grace period for the vendor to retry the payment.
Payment failed. A grace period of 7 days has started.
When downgrading to a lower tier, vendors must reduce their branches to comply with the new tier's limits.
The following errors are handled gracefully:
-
Invalid Phone Numbers:
Invalid phone number format. Please use Kenyan international format (e.g., 254712345678). -
Invalid Access Token:
Error fetching access token: <error details> -
Payment Processing Errors:
Error initiating payment: <error details>
-
Run Flask Server:
python callback_server.py
-
Expose via ngrok:
ngrok http 5000
-
Set
callback_urlto the ngrok URL. -
Run Payment Module:
python payment_module.py
-
Check Logs:
View logs in your terminal or the ngrok web interface (http://127.0.0.1:4040).
Feel free to fork this repository and contribute by submitting a pull request. For major changes, please open an issue first to discuss what you'd like to change.
For any inquiries, contact me at:
- Email: masayaelvin@gmail.com
- GitHub: Masayaelvin