This Django app offers a comprehensive solution for managing M-Pesa transactions within Django applications. It encompasses functionalities such as initiating STK push requests, processing payment notifications, and storing transaction records in the database.
- STK Push Requests: Initiate payments using Safaricom's STK push functionality.
- Transaction Record Keeping: Store transaction details in the database for future reference.
- Easy Integration: Simple setup and configuration for seamless integration.
Before using this app, ensure you have the following:
- Django installed in your environment.
- A Safaricom Developer Portal account.
- Consumer Key and Consumer Secret obtained from the Safaricom Developer Portal.
- Test credentials assigned to you by Safaricom.
-
Create a virtual environment:
python -m venv myenv
-
Activate the virtual environment:
- For Linux/MacOS:
source myenv/bin/activate
- For Windows:
myenv\Scripts\activate
-
Install Django and other dependencies by running:
pip install -r requirements.txt
-
Add
'django_daraja'
and'mpesa_transactions'
to yourINSTALLED_APPS
in Django settings. -
Create a
.env
file in the root directory of your Django project and add the following Mpesa Daraja credentials:MPESA_ENVIRONMENT= # Set to 'sandbox' or 'production' MPESA_CONSUMER_KEY= # Your Safaricom Consumer Key MPESA_CONSUMER_SECRET= # Your Safaricom Consumer Secret MPESA_SHORTCODE= # Your Mpesa Paybill or Till Number MPESA_EXPRESS_SHORTCODE= # Your Mpesa Express Paybill or Till Number MPESA_SHORTCODE_TYPE= # Set to 'paybill' or 'till_number' MPESA_PASSKEY= # Your Mpesa Passkey MPESA_INITIATOR_USERNAME= # Your Mpesa Initiator Username MPESA_INITIATOR_SECURITY_CREDENTIAL= # Your Mpesa Initiator Security Credential
-
Update your Django settings to load environment variables from the
.env
file. For example:import os from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() # Mpesa Daraja credentials MPESA_ENVIRONMENT = os.getenv('MPESA_ENVIRONMENT') MPESA_CONSUMER_KEY = os.getenv('MPESA_CONSUMER_KEY') MPESA_CONSUMER_SECRET = os.getenv('MPESA_CONSUMER_SECRET') MPESA_SHORTCODE = os.getenv('MPESA_SHORTCODE') MPESA_EXPRESS_SHORTCODE = os.getenv('MPESA_EXPRESS_SHORTCODE') MPESA_SHORTCODE_TYPE = os.getenv('MPESA_SHORTCODE_TYPE') MPESA_PASSKEY = os.getenv('MPESA_PASSKEY') MPESA_INITIATOR_USERNAME = os.getenv('MPESA_INITIATOR_USERNAME') MPESA_INITIATOR_SECURITY_CREDENTIAL = os.getenv('MPESA_INITIATOR_SECURITY_CREDENTIAL')
-
Depending on your operating system, run the appropriate command to source the
.env
file:- For Linux/MacOS:
source .env
- For Windows:
call setenv.bat
-
Run migrations to create the necessary database tables.
-
Sending STK Push Requests:
- Visit the homepage of the Django application
localhost:8000
. - Fill out the form with the required details: phone number, amount, account reference, and transaction description.
- Click on the "Pay Now" button to initiate the payment.
- Visit the homepage of the Django application
-
Retrieving Transaction Records:
- To view transaction records, visit
localhost:8000/records
. - All transaction records will be displayed in a tabular format.
- To view transaction records, visit
- Thanks to Safaricom for providing the M-Pesa API.
- This project is inspired by the need for seamless M-Pesa integration in Django applications.