# M-Pesa Integration - Production Deployment Checklist

## ✅ Pre-Deployment Verification

### 1. Domain Configuration
- [x] **Domain:** `branchbusinessadvance.co.ke` configured
- [x] **HTTPS:** SSL certificate required for M-Pesa callbacks
- [x] **Callback URLs:** 
  - Confirmation: `https://branchbusinessadvance.co.ke/payments/mpesa/confirmation/`
  - Validation: `https://branchbusinessadvance.co.ke/payments/mpesa/validation/`

### 2. M-Pesa Credentials
- [x] **Consumer Key:** `9mD1A3H1qw5grqdqkZ4X1G9zbNxioydHXL5An4nkUGRlNRKr`
- [x] **Consumer Secret:** `C2dqBSaGFUIporfYYuyhQgnfPEqLvCS3GvfAJ91ENkXI2bhqptlVXAqMelsEpLQR`
- [x] **Shortcode:** `4159523`
- [x] **Environment:** Production

### 3. System Integration
- [x] **Automatic payments** integrated into existing repayments page
- [x] **Payment source filtering** (Automatic vs Manual)
- [x] **Branch configuration** for M-Pesa shortcodes
- [x] **Admin interface** enhanced for M-Pesa transactions
- [x] **Test customer** (Aaron Carlson, ID: 22812438) ready for testing

## 🚀 Deployment Steps

### Step 1: Upload Files to cPanel
Upload these modified files to your cPanel server:
```
loans/models.py
loans/views.py
loans/admin.py
users/models.py
users/views.py
users/admin.py
templates/users/branch_form.html
templates/loans/repayments.html
templates/loans/dashboard.html
templates/base.html
payments/branch_mpesa_service.py
payments/management/commands/setup_production_mpesa.py
payments/management/commands/simulate_mpesa_payment.py
deploy_mpesa_production.py
```

### Step 2: Fix Migration Conflicts (if needed)
If you get a "Duplicate column name" error, run this first:
```bash
python quick_fix_migration.py
```

### Step 3: Run Deployment Script
```bash
# SSH into your cPanel server
cd /home/yourusername/public_html/yourproject
python deploy_mpesa_production_auto.py
```

### Step 4: Register C2B URLs in M-Pesa Portal
Go to your M-Pesa Daraja portal and register:
- **Confirmation URL:** `https://branchbusinessadvance.co.ke/payments/mpesa/confirmation/`
- **Validation URL:** `https://branchbusinessadvance.co.ke/payments/mpesa/validation/`
- **Response Type:** `Completed`

## 🧪 Testing

### Test 1: Payment Simulation
```bash
python manage.py simulate_mpesa_payment --amount 1 --customer-id 22812438
```

### Test 2: Real Payment Test
1. Send **1 KSH** to paybill: `4159523`
2. Use account number: `22812438` (Aaron Carlson's ID Number)
3. Check repayments page: `https://branchbusinessadvance.co.ke/loans/repayments/`
4. Filter by "Payment Source: Automatic M-Pesa"

## 📊 Verification Points

### ✅ System Integration
- [ ] Automatic payments appear in repayments list
- [ ] Payment source shows "Automatic" for M-Pesa payments
- [ ] Branch M-Pesa configuration works
- [ ] Admin interface shows M-Pesa transactions

### ✅ M-Pesa Integration
- [ ] C2B URLs registered successfully
- [ ] Access token generation works
- [ ] Payment callbacks received
- [ ] Transactions processed automatically

### ✅ User Experience
- [ ] Customers can pay using paybill + customer ID
- [ ] Payments appear automatically in system
- [ ] Loan balances update correctly
- [ ] Receipts generated automatically

## 🚨 Important Notes

1. **HTTPS Required:** M-Pesa requires HTTPS for production callbacks
2. **Firewall:** Ensure server can receive incoming requests from M-Pesa
3. **Monitoring:** Check server logs for M-Pesa callback errors
4. **Backup:** Always backup database before deployment

## 📞 Support

If issues occur:
1. Check deployment script output
2. Review server error logs
3. Verify C2B URLs are accessible
4. Test with simulation before real payments

---

## 🎯 Ready for Production!

Your M-Pesa integration is configured for:
- **Domain:** `branchbusinessadvance.co.ke`
- **Paybill:** `4159523`
- **Test Customer:** Aaron Carlson (ID Number: `22812438`)

**Deploy when ready!** 🚀
