# Product Purchase Fix

## Issue
Product purchase was failing with database error:
```
Column 'msisdn' cannot be null
```

## Root Cause
The `purchaseProduct()` method was trying to save a transaction record with:
- `msisdn` = `null` ❌
- `amount` = `0` ❌

This happened because the purchase endpoint only receives `validation_reference`, but doesn't have access to customer phone number or amount.

## Solution
**Before making the purchase API call**, we now:
1. Look up the validation transaction from the database
2. Extract `msisdn`, `amount`, and `description` from the validation record
3. Use those values when saving the purchase transaction

## Code Changes

### Added Validation Transaction Lookup
```php
// Look up validation transaction to get msisdn and amount
$validationTransaction = $this->paymentTransactionModel
    ->where('gateway_reference', $json->validation_reference)
    ->orWhere('transaction_id LIKE', '%' . $json->validation_reference . '%')
    ->first();
```

### Use Values from Validation
```php
// Get msisdn and amount from validation transaction if available
$msisdn = $validationTransaction['msisdn'] ?? '+000000000000'; // Fallback to avoid null
$amount = $validationTransaction['amount'] ?? 0;
$description = $validationTransaction['description'] ?? 'Product Purchase';
```

## Testing

Try the complete flow again:

### Step 1: Validate Product
```bash
POST /api/mobile-money/validate-product
{
    "msisdn": "256709713160",
    "amount": 5000,
    "product_code": "DSTV",
    "contact_phone": "256709713160"
}
```

**Response:**
```json
{
    "success": true,
    "validation_reference": "abc123xyz",
    "customer_name": "John Doe",
    "msisdn": "+256709713160",
    "amount": 5000
}
```

### Step 2: Purchase Product
```bash
POST /api/mobile-money/purchase-product
{
    "validation_reference": "abc123xyz"
}
```

**Expected Response:**
```json
{
    "success": true,
    "message": "Product purchase in progress",
    "internal_reference": "xyz789..."
}
```

## What Gets Saved Now

When purchase succeeds, the transaction record includes:
- ✅ `msisdn`: Retrieved from validation transaction
- ✅ `amount`: Retrieved from validation transaction
- ✅ `description`: Retrieved from validation transaction
- ✅ `validation_reference`: Stored in metadata
- ✅ `validation_transaction_id`: Linked to original validation

## Files Modified
- `app/Controllers/MobileMoney.php` - Fixed `purchaseProduct()` method

## Benefits
1. ✅ No more null constraint errors
2. ✅ Complete transaction records with all data
3. ✅ Linkage between validation and purchase transactions
4. ✅ Proper webhook notifications with actual amounts

