# Production Deployment Guide

## Fixed Scripts
All scripts now use the correct settings module: `branch_system.settings`

## How to Run on Production Server

### Step 1: Upload Scripts to Server
Upload these files to your production directory:
```
/home/acbptxvs/public_html/branchbusinessadvance.co.ke/
├── diagnose_mpesa_setup.py
├── setup_branch_mpesa.py
└── register_mpesa_urls.py
```

### Step 2: Activate Virtual Environment
```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
source /home/acbptxvs/virtualenv/public_html/branchbusinessadvance.co.ke/3.13/bin/activate
```

### Step 3: Run Diagnostic Script
This checks your current M-Pesa configuration:
```bash
python diagnose_mpesa_setup.py
```

Expected output:
- Branch configurations
- Transaction counts by shortcode
- Callback statistics
- Recommendations

### Step 4: Run Setup Script
This configures both branches with correct credentials:
```bash
python setup_branch_mpesa.py
```

This will:
- Set Main Branch shortcode to 4086675
- Set Juja Office shortcode to 4159523
- Save all credentials to database

### Step 5: Register URLs with Safaricom (CRITICAL)

**Option A: Automatic Registration** (Recommended)
```bash
python register_mpesa_urls.py
```

When prompted:
- Enter your domain: `branchbusinessadvance.co.ke`
- Choose environment: `production`

**Option B: Manual Registration**
1. Go to https://org.ke.m-pesa.com/
2. Log in with your credentials
3. Navigate to API Management → C2B
4. For shortcode **4086675** (Main):
   - Validation URL: `https://branchbusinessadvance.co.ke/payments/callback/validation/`
   - Confirmation URL: `https://branchbusinessadvance.co.ke/payments/callback/confirmation/`
   - Response Type: Completed
5. For shortcode **4159523** (Juja):
   - Validation URL: `https://branchbusinessadvance.co.ke/payments/callback/validation/`
   - Confirmation URL: `https://branchbusinessadvance.co.ke/payments/callback/confirmation/`
   - Response Type: Completed

## Testing

### Test Main Branch (4086675)
1. Make a test payment:
   - Paybill: **4086675**
   - Account: Use a registered borrower's ID number
   - Amount: KES 10

2. Check the admin panel:
   - Go to Payments → M-Pesa Transactions
   - Look for transaction with business_short_code = 4086675
   - Verify it's processing correctly

### Test Juja Branch (4159523)
1. Make a test payment:
   - Paybill: **4159523**
   - Account: Use a registered borrower's ID number
   - Amount: KES 10

2. Check the admin panel:
   - Verify transaction appears
   - Confirm both branches are working

## Troubleshooting

### Error: ModuleNotFoundError
If you still get module errors, check:
```bash
# Verify you're in the right directory
pwd
# Should show: /home/acbptxvs/public_html/branchbusinessadvance.co.ke

# Verify virtual environment is activated
which python
# Should show: /home/acbptxvs/virtualenv/...
```

### Error: Permission Denied
```bash
chmod +x diagnose_mpesa_setup.py
chmod +x setup_branch_mpesa.py
chmod +x register_mpesa_urls.py
```

### Callbacks Not Received
1. Check server logs for errors
2. Verify SSL certificate is valid
3. Test callback URLs manually:
   ```bash
   curl https://branchbusinessadvance.co.ke/payments/callback/validation/
   ```
4. Contact Safaricom support if needed

## Post-Deployment Checklist

- [ ] Run `diagnose_mpesa_setup.py` successfully
- [ ] Run `setup_branch_mpesa.py` successfully
- [ ] Register URLs with Safaricom (automatic or manual)
- [ ] Test payment to Main branch (4086675)
- [ ] Test payment to Juja branch (4159523)
- [ ] Verify both appear in admin panel
- [ ] Check branch filtering works correctly
- [ ] Monitor for 24 hours for any issues

## Support

If issues persist after following this guide:
1. Check the callback logs in admin panel
2. Review server error logs
3. Verify paybill status with Safaricom
4. Ensure firewall allows Safaricom IPs

## Important Notes

- Both paybills use the **same callback URLs**
- The system filters by `business_short_code` to determine which branch
- Callbacks must be registered for **each shortcode separately**
- Test in sandbox first if possible
