# Payment Gateway Selection System

## Overview
The system now allows the admin to select ONE payment gateway (either Paystack or Hubtel) that will be used for all transactions throughout the system. Users do not choose the gateway - it's controlled entirely by the admin.

## How It Works

### Admin Configuration
1. Navigate to Admin Dashboard → Settings
2. Scroll to "Payment Gateway Configuration"
3. Select your preferred gateway from the dropdown:
   - **Paystack** - Cards, Mobile Money, Bank Transfer
   - **Hubtel** - Mobile Money (MTN, Vodafone, AirtelTigo)
4. Enter the API credentials for the selected gateway
5. Save settings

### Gateway Selection Behavior

#### Paystack Selected
- Only Paystack fields are shown in admin settings
- All payments route through Paystack API
- Users see Paystack checkout page
- Supports: Cards, Mobile Money, Bank Transfer, USSD

#### Hubtel Selected
- Only Hubtel fields are shown in admin settings
- All payments route through Hubtel API
- Users receive mobile money prompt on their phone
- Supports: MTN Mobile Money, Vodafone Cash, AirtelTigo Money

### User Experience

1. **User initiates payment** (wallet top-up or exchange order)
2. **System automatically uses** the gateway selected by admin
3. **No gateway selection** shown to user
4. **Payment processed** through the configured gateway
5. **Transaction completed** with notifications sent

## Configuration Fields

### Paystack Configuration
```
- Paystack Public Key (pk_test_xxx or pk_live_xxx)
- Paystack Secret Key (sk_test_xxx or sk_live_xxx)
```

### Hubtel Configuration
```
- Hubtel Client ID
- Hubtel Client Secret
- Hubtel Merchant Account Number (HM-XXXXXXXXXX)
```

## Database Settings

The following settings are stored in the `settings` table:

```php
'payment_gateway' => 'paystack' or 'hubtel'
'paystack_public_key' => 'pk_xxx...'
'paystack_secret_key' => 'sk_xxx...'
'hubtel_client_id' => 'xxx...'
'hubtel_client_secret' => 'xxx...'
'hubtel_merchant_account' => 'HM-xxx...'
```

## Code Implementation

### Admin Settings View
- Dropdown selector for gateway type
- JavaScript toggles visibility of credential fields
- Only shows fields for selected gateway

### Payment Controller
```php
// Automatically gets active gateway from settings
$gateway = Setting::get('payment_gateway', 'paystack');

// Routes to appropriate gateway
if ($gateway === 'hubtel') {
    return $this->initializeHubtelPayment(...);
} else {
    return $this->initializePaystackPayment(...);
}
```

### Payment Views
- No gateway selection UI shown to users
- Single "Proceed to Payment" button
- System automatically uses configured gateway

## Switching Gateways

To switch from one gateway to another:

1. Go to Admin Settings
2. Change the "Active Payment Gateway" dropdown
3. Enter credentials for the new gateway
4. Save settings
5. All new payments will use the new gateway immediately

**Note:** Existing pending payments will still use the gateway they were initiated with.

## Benefits

1. **Simplified User Experience** - Users don't need to choose
2. **Centralized Control** - Admin controls payment method
3. **Easy Switching** - Change gateway anytime from settings
4. **Clean Interface** - No confusing options for users
5. **Consistent Processing** - All payments use same gateway

## Testing

### Test Paystack
1. Select "Paystack" in admin settings
2. Add Paystack test credentials
3. Create a test transaction
4. Verify payment routes through Paystack

### Test Hubtel
1. Select "Hubtel" in admin settings
2. Add Hubtel test credentials
3. Create a test transaction
4. Verify payment routes through Hubtel

## Files Modified

1. `resources/views/admin/settings.blade.php`
   - Changed to dropdown selector
   - Added field toggling JavaScript
   - Removed enable/disable checkboxes

2. `app/Http/Controllers/Admin/AdminController.php`
   - Changed from `paystack_enabled`/`hubtel_enabled` to `payment_gateway`
   - Updated validation rules
   - Simplified settings storage

3. `app/Http/Controllers/PaymentController.php`
   - Removed gateway parameter from request
   - Auto-detects gateway from settings
   - Routes to appropriate gateway automatically

4. `resources/views/payment-confirm.blade.php`
   - Removed gateway selection UI
   - Simplified to single payment button

5. `resources/views/wallet-topup-payment.blade.php`
   - Removed gateway selection UI
   - Simplified to single payment button

## Future Enhancements

1. Gateway-specific settings (e.g., Paystack channels)
2. Fallback gateway if primary fails
3. Gateway performance analytics
4. A/B testing between gateways
5. Multiple gateways for different transaction types
