# Link M-Pesa Transaction to Borrower

## ✅ Transaction Created Successfully!

**Transaction ID:** `3abe6f1f-c4c6-4772-a6af-ab8af54d5523`  
**M-Pesa Trans ID:** `TK1018ZAQC`  
**Amount:** KES 1.00  
**Status:** Created but not matched to borrower

From the M-Pesa message:
- **Account:** 1 (Bill Ref Number)
- **Customer Name:** PHINEAS

---

## 🚀 Link the Transaction (3 Options)

### Option 1: By Phone Number (Recommended)
```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
python manage.py link_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523 --phone +254114565176
```

Replace `+254114565176` with the actual phone number.

### Option 2: By ID Number
```bash
python manage.py link_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523 --id-number 1
```

This will link to the borrower with ID number "1" (from Bill Ref).

### Option 3: Interactive (Choose from List)
```bash
python manage.py link_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523
```

This will show you a list of borrowers to choose from.

---

## 📊 Expected Output

```
======================================================================
Link M-Pesa Transaction to Borrower
======================================================================

Transaction: TK1018ZAQC
Amount: KES 1.00
Current Status: failed
Current Borrower: Not matched

✓ Found borrower by phone (+254114565176): Phineas Client

======================================================================
Linking:
  Transaction: TK1018ZAQC (KES 1.00)
  To Borrower: Phineas Client
  Phone: +254114565176
  ID Number: 40178864
======================================================================

Proceed? (yes/no): yes

----------------------------------------------------------------------
Linking transaction to borrower...
----------------------------------------------------------------------
✓ Transaction linked to borrower

----------------------------------------------------------------------
Processing payment...
----------------------------------------------------------------------

======================================================================
✓ Payment Processed Successfully!
======================================================================

Transaction Status: processed
Matched Borrower: Phineas Client
Matched Loan: LOAN-000123
Repayment Created: Yes
Receipt Number: RCP-000456

✓ Payment successfully applied to loan!

You can now view it at:
  • /loans/repayments/ - Repayment record
  • /payments/transactions/ - Transaction details
  • /loans/ - Updated loan balance
```

---

## 🔍 Find the Borrower's Phone Number

If you don't know the phone number, check the database:

```bash
python manage.py shell -c "
from users.models import User
# Search by first name
borrowers = User.objects.filter(
    first_name__icontains='PHINEAS',
    role='borrower'
)
for b in borrowers:
    print(f'{b.get_full_name()}: {b.phone_number} (ID: {b.id_number})')
"
```

Or search by the phone number you used to make the payment:

```bash
python manage.py shell -c "
from users.models import User
# Replace with the actual phone
borrower = User.objects.filter(
    phone_number__contains='114565176',
    role='borrower'
).first()
if borrower:
    print(f'Found: {borrower.get_full_name()}')
    print(f'Phone: {borrower.phone_number}')
    print(f'User ID: {borrower.id}')
else:
    print('Not found')
"
```

---

## 📤 Files to Upload

1. **`payments/management/commands/link_transaction.py`** (NEW)
2. **`payments/management/commands/process_callback.py`** (UPDATED)

---

## ✅ Complete Workflow

```bash
# 1. Upload the files

# 2. Restart application
touch /home/acbptxvs/public_html/branchbusinessadvance.co.ke/passenger_wsgi.py

# 3. Link the transaction (use the phone number that made the payment)
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
python manage.py link_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523 --phone +254114565176

# 4. Verify
python manage.py check_mpesa_status
python manage.py check_callbacks
```

---

## 🎯 What Happens Next

After linking:
1. ✅ Transaction linked to borrower
2. ✅ System finds borrower's active loan
3. ✅ Creates repayment record
4. ✅ Updates loan balance
5. ✅ Payment appears in `/loans/repayments/` with M-Pesa badge
6. ✅ Payment appears in `/loans/` (updated balance)
7. ✅ Borrower receives notification

---

## 🆘 If Borrower Has No Active Loans

If the borrower doesn't have an active loan, you'll see:

```
⚠ Payment Processing Completed with Issues
Matched Loan: Not matched
Repayment Created: No
Processing Notes: No active loans found for this borrower
```

**Solution:** Create a loan for the borrower first, then reprocess:

```bash
python manage.py reprocess_mpesa_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523
```

---

## 📝 Quick Reference

```bash
# Link by phone
python manage.py link_transaction <TRANSACTION_ID> --phone <PHONE>

# Link by ID number
python manage.py link_transaction <TRANSACTION_ID> --id-number <ID_NUMBER>

# Link by user ID
python manage.py link_transaction <TRANSACTION_ID> --user-id <USER_ID>

# Interactive mode
python manage.py link_transaction <TRANSACTION_ID>
```

---

**Upload `link_transaction.py` → Run the command with the borrower's phone → Payment will be processed!** 🚀
