# Check M-Pesa Repayment Status

## ✅ Transaction Processed Successfully!

The transaction was linked and processed:
- ✅ Transaction: TK1018ZAQC
- ✅ Borrower: Phin Client
- ✅ Loan: LOAN-000026
- ✅ Status: Processed

But the repayment is not showing on `/loans/repayments/` page.

---

## 🔍 Check if Repayment Exists

### Upload This File:
**`payments/management/commands/check_repayment.py`** (NEW)

### Run This Command:
```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
python manage.py check_repayment 3abe6f1f-c4c6-4772-a6af-ab8af54d5523
```

Or by Trans ID:
```bash
python manage.py check_repayment TK1018ZAQC
```

---

## 📊 What This Will Show

The command will check:
1. ✅ If transaction has a repayment_id
2. ✅ If repayment exists in database
3. ✅ Repayment details (receipt number, amount, etc.)
4. ✅ All repayments for this loan
5. ✅ Why it might not be showing on the page

---

## 🎯 Possible Issues

### Issue 1: Repayment Created But Not Linked
The repayment was created but the transaction's `repayment_id` field is NULL.

**Fix:** Link them manually in Django shell.

### Issue 2: Repayment Not Created
The `process_payment()` failed to create the repayment due to an error.

**Fix:** Check error logs and recreate repayment.

### Issue 3: Template Not Showing M-Pesa Payments
The `/loans/repayments/` template might be filtering out M-Pesa payments.

**Fix:** Check template filters.

### Issue 4: Cache Issue
The page is cached and not showing new data.

**Fix:** Clear browser cache, restart app.

---

## 🚀 Quick Verification

```bash
# 1. Check repayment
python manage.py check_repayment TK1018ZAQC

# 2. Check all M-Pesa repayments
python manage.py shell -c "
from loans.models import Repayment
mpesa_repayments = Repayment.objects.filter(payment_method='mpesa').order_by('-created_at')[:10]
print(f'Total M-Pesa repayments: {mpesa_repayments.count()}')
for r in mpesa_repayments:
    print(f'{r.receipt_number}: {r.loan.borrower.get_full_name()} - KES {r.amount} - {r.created_at}')
"

# 3. Check specific loan repayments
python manage.py shell -c "
from loans.models import Loan, Repayment
loan = Loan.objects.get(loan_number='LOAN-000026')
repayments = Repayment.objects.filter(loan=loan).order_by('-created_at')
print(f'Repayments for LOAN-000026: {repayments.count()}')
for r in repayments:
    print(f'{r.receipt_number}: KES {r.amount} - {r.payment_method} - {r.created_at}')
"
```

---

## 📝 Next Steps

1. **Upload** `check_repayment.py`
2. **Run** the check command
3. **Report** what it shows
4. Based on the output, we'll fix the specific issue

---

**Upload `check_repayment.py` → Run the command → See what's wrong!** 🔍
