This application allows you to integrate M-Pesa functionalities into your Flask application. It enables features like initiating STK push requests.
Before you begin, ensure you have the following:
- A Safaricom Developer Portal account. Safaricom Developer Portal
- Consumer Key and Consumer Secret obtained from the Safaricom Developer Portal.
- Test credentials assigned to you by Safaricom.
-
Environment Configuration:
-
Create a
.env
file in the root directory of your Flask project. -
Add the following credentials to your
.env
file:# Secret Key SECRET_KEY= # Your Safaricom Secret Key # Daraja API credentials CONSUMER_KEY= # Your Safaricom Consumer Key CONSUMER_SECRET= # Your Safaricom Consumer Secret SHORTCODE= # Your Safaricom Short Code PASSKEY= # Your Passkey # Database URI SQLALCHEMY_DATABASE_URI= # Your Database URI
-
-
Virtual Environment Setup:
-
It's a best practice to work within a virtual environment to manage package dependencies. Here's how to set it up based on your operating system:
- For Windows:
python -m venv venv venv\Scripts\activate
- For macOS/Linux:
python3 -m venv venv source venv/bin/activate
- For Windows:
-
-
Environment Activation:
-
Once the virtual environment is activated, depending on your operating system, run the appropriate command to source the
.env
file:- For Linux/MacOS:
source .env
- For Windows:
call setenv.bat
- For Linux/MacOS:
-
To manage database migrations, follow these steps:
-
Initialize the migration environment:
flask db init
-
Create an initial migration:
flask db migrate -m "Initial migration"
-
Apply the migration to the database:
flask db upgrade
-
Initiate M-Pesa STK Push
- Endpoint:
/initiate_mpesa_stk_push
- Methods: POST, GET
- Description: Initiate STK push for M-Pesa payment.
- Endpoint:
-
View Records
- Endpoint:
/view_records
- Method: GET
- Description: View M-Pesa transaction records.
- Endpoint: