mpesa paybill integrations to an ecommerce shop/ store
# E-Commerce M-Pesa Paybill Integration
## Requirements (requirements.txt)
```txt
Django>=4.2
python-decouple>=3.8
requests>=2.31.0
Pillow>=10.0.0
```
## Installation & Setup
### 1. Install Dependencies
```bash
pip install -r requirements.txt
```
### 2. Create `.env` File
Create a `.env` file in your project root with the following:
```env
SECRET_KEY=your-secret-key-here
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
MPESA_ENVIRONMENT=sandbox
MPESA_CONSUMER_KEY=your_consumer_key
MPESA_CONSUMER_SECRET=your_consumer_secret
MPESA_SHORTCODE=174379
MPESA_PASSKEY=bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919
MPESA_CALLBACK_URL=
your-ngrok-url.ngrok.io
```
### 3. Create Logs Directory
```bash
mkdir logs
```
### 4. Run Migrations
```bash
python manage.py makemigrations
python manage.py migrate
```
### 5. Create Superuser
```bash
python manage.py createsuperuser
```
### 6. Setup Ngrok (for local testing)
M-Pesa callback requires a public HTTPS URL. Use ngrok:
```bash
# Install ngrok from
ngrok.com
# Run your Django server
python manage.py runserver
# In another terminal, run ngrok
ngrok http 8000
# Copy the HTTPS URL (e.g.,
abc123.ngrok.io)
# Update .env file:
MPESA_CALLBACK_URL=
abc123.ngrok.io
```
### 7. Get M-Pesa Credentials
1. Go to Safaricom Daraja Portal
2. Create an account
3. Create a new app
4. Get your **Consumer Key** and **Consumer Secret**
5. Get your **Passkey** from the app settings
6. For sandbox testing, use:
- Shortcode: `174379`
- Test Phone: `254708374149`
### 8. Register Callback URLs
In the Daraja Portal:
1. Go to your app settings
2. Register your callback URL
3. Format: `
your-domain.com`
## Project Structure
```
your_project/
├── store/
│ ├── models.py # Database models
│ ├── views.py # Views with M-Pesa integration
│ ├── urls.py # URL patterns
│ ├── admin.py # Admin configur …