This project demonstrates how to integrate with the Safaricom M-Pesa Daraja API to perform Lipa na M-Pesa Online payments.
- Sandbox and production environment support
- Proper error handling and timeouts
- Python 3.6+
requestslibrary- Safaricom Daraja API credentials
-
Clone the repository:
git clone <your-repository-url> cd daraja-mpesa-integration
-
Install the required dependencies:
pip install requests
-
Setup configuration:
# Copy the configuration template cp config.example.py config.py -
Edit
config.pywith your actual Safaricom API credentials:# Sandbox credentials (get from Safaricom Developer Portal) SANDBOX_CONSUMER_KEY = 'your_actual_sandbox_consumer_key' SANDBOX_CONSUMER_SECRET = 'your_actual_sandbox_consumer_secret' # Production credentials (for live environment) PRODUCTION_CONSUMER_KEY = 'your_production_consumer_key' PRODUCTION_CONSUMER_SECRET = 'your_production_consumer_secret' # Business shortcode and passkey BUSINESS_SHORT_CODE = '174379' # Sandbox test code LIPA_NA_MPESA_PASSKEY = 'your_actual_passkey' # Callback URL for payment notifications CALLBACK_URL = 'https://yourdomain.com/callback'
daraja-mpesa-integration/
├── .gitignore # Git ignore rules for security
├── mpesa_integration.py # Main integration code
├── config.example.py # Configuration template (safe to commit)
├── config.py # Actual configuration (DO NOT COMMIT)
└── README.md # This file
from mpesa_integration import get_access_token, lipa_na_mpesa_online
# Get access token
token = get_access_token(environment='sandbox') # Use 'production' for live environment
if token:
# Initiate payment
response = lipa_na_mpesa_online(
access_token=token,
amount=100, # Amount in KES
phone_number="254712345678", # Customer phone number
account_reference="Order123", # Your internal reference
environment='sandbox' # Use 'production' for live
)
print("Payment Response:", response)
else:
print("Failed to authenticate")Execute the main script directly:
python mpesa_integration.pyThis will:
- Authenticate with the Daraja API
- Initiate a test payment of KES 100
- Display the API response
Sandbox Environment
- Base URL:
https://sandbox.safaricom.co.ke - Test Credentials: Available from Safaricom Developer Portal
Production Environment
- Base URL:
https://api.safaricom.co.ke - Live Credentials: Obtain from Safaricom after approval
The code includes comprehensive error handling for:
- Network timeouts
- Authentication failures
- Invalid responses
- Missing configuration
| Variable | Description | Example |
|---|---|---|
| SANDBOX_CONSUMER_KEY | Sandbox app consumer key | 'your_sandbox_key' |
| SANDBOX_CONSUMER_SECRET | Sandbox app consumer secret | 'your_sandbox_secret' |
| PRODUCTION_* | Production environment credentials | - |
| BUSINESS_SHORT_CODE | Your business PayBill/till number | '174379' (sandbox) |
| LIPA_NA_MPESA_PASSKEY | Lipa na M-Pesa passkey | 'your_passkey' |
| CALLBACK_URL | Payment confirmation webhook | 'https://yourdomain.com/callback' |
For API-related issues, refer to the Safaricom Daraja API Documentation.
This project is for educational purposes. Ensure you comply with Safaricom's terms of service when using in production.