# Link M-Pesa Transaction - FINAL COMMAND

## ✅ Fixed Command (No More EOFError!)

I've updated the command to work in non-interactive environments (cPanel).

---

## 🚀 Upload & Run

### 1. Upload Fixed File
**`payments/management/commands/link_transaction.py`** (UPDATED - added --yes flag)

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

### 3. Run Command (with --yes flag)
```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
python manage.py link_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523 --phone +254114565176 --yes
```

**The `--yes` flag skips the confirmation prompt!**

---

## 📊 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): Phin Client

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

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

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

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

Transaction Status: processed
Matched Borrower: Phin 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
```

---

## 🎯 Command Options

```bash
# By phone number (recommended)
python manage.py link_transaction <TRANSACTION_ID> --phone <PHONE> --yes

# By ID number
python manage.py link_transaction <TRANSACTION_ID> --id-number <ID_NUMBER> --yes

# By user ID
python manage.py link_transaction <TRANSACTION_ID> --user-id <USER_ID> --yes
```

**Always add `--yes` flag when running in cPanel!**

---

## ✅ Complete Workflow

```bash
# 1. Upload link_transaction.py

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

# 3. Link transaction
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
python manage.py link_transaction 3abe6f1f-c4c6-4772-a6af-ab8af54d5523 --phone +254114565176 --yes

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

---

## 🔍 Verify Payment

After running the command, check:

### 1. Callback Status
```bash
python manage.py check_callbacks
```

Should show:
```
✓ Linked to Transaction: 3abe6f1f-c4c6-4772-a6af-ab8af54d5523
✓ Processed: Yes
```

### 2. Transaction Status
```bash
python manage.py check_mpesa_status
```

Should show:
```
✓ Borrower: Phin Client
✓ Loan: LOAN-000123
✓ Repayment: RCP-000456
```

### 3. Web Pages
- `/payments/callbacks/` - Should show "Processed"
- `/payments/transactions/` - Should show linked borrower and loan
- `/loans/repayments/` - Should show payment with M-Pesa badge
- `/loans/` - Should show updated loan balance

---

## 🆘 If No Active Loan

If the borrower doesn't have an active loan:

```
⚠ 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
```

---

## 📝 Summary

**Problem:** EOFError because command tried to prompt for confirmation in non-interactive environment  
**Fix:** Added `--yes` flag to skip confirmation  
**Command:** `python manage.py link_transaction <ID> --phone <PHONE> --yes`

---

**Upload the fixed file → Run the command with --yes flag → Payment will be processed!** 🚀
