# Virtual Account Implementation with PocketFi API

## Overview
Added virtual account as a deposit method with automatic account creation using PocketFi API.

## Files Created/Modified

### Database
- **create_virtual_accounts.sql**: Creates virtual_accounts table
- **add_pocketfi_gateway.sql**: Adds PocketFi gateway configuration

### Models
- **VirtualAccount.php**: Model for virtual accounts
- **User.php**: Added virtualAccount() relationship
- **Payment.php**: Added gateway_response cast

### Controllers
- **WalletPaymentController.php**: 
  - showVirtualAccount() - Shows account or creates if doesn't exist
  - createVirtualAccount() - Calls PocketFi API to create account
  - pocketfiWebhook() - Handles automatic wallet crediting

### Views
- **frontend/user/payment/index.blade.php**: Added virtual account option
- **frontend/user/payment/virtual.blade.php**: Virtual account details page

### Routes
- **routes/user.php**: 
  - GET /payment/virtual - Show/create virtual account
  - POST /webhook/pocketfi - Webhook for auto-credit

## Setup Instructions

1. **Run SQL Files**:
   ```sql
   -- In phpMyAdmin, run these files:
   create_virtual_accounts.sql
   add_pocketfi_gateway.sql
   ```

2. **Configure PocketFi API Key**:
   - Update the gateway record in database:
   ```sql
   UPDATE gateways 
   SET gateway_parameters = '{"api_key":"YOUR_ACTUAL_API_KEY","default_bvn":"22222222222"}'
   WHERE gateway_name = 'pocketfi';
   ```

3. **Configure PocketFi Webhook**:
   - In PocketFi dashboard, set webhook URL to:
   - `https://yourdomain.com/webhook/pocketfi`

## How It Works

1. User clicks "Virtual Account" on payment page
2. System checks if user has virtual account
3. If not, automatically calls PocketFi API to create one
4. Account details are saved and displayed to user
5. User transfers money from any bank
6. PocketFi webhook notifies your system
7. Wallet is credited automatically
8. User receives email confirmation

## PocketFi API Integration

**Endpoint**: `https://api.pocketfi.ng/api/v1/virtual-accounts/create`

**Request**:
```json
{
  "email": "user@example.com",
  "first_name": "John",
  "last_name": "Doe",
  "phone": "08012345678",
  "bvn": "22222222222"
}
```

**Response**:
```json
{
  "data": {
    "account_number": "1234567890",
    "account_name": "John Doe",
    "bank_name": "Wema Bank",
    "reference": "VA_REF_123"
  }
}
```

## Features
- ✅ Automatic account creation via API
- ✅ Instant wallet credit (via webhook)
- ✅ Dedicated account per user
- ✅ Copy account details with one click
- ✅ Works with any Nigerian bank
- ✅ No manual approval needed
- ✅ Transaction logging
- ✅ Email notifications

## Webhook URL
- **PocketFi**: `https://yourdomain.com/webhook/pocketfi`

## Testing
1. Login as a user
2. Go to Payment page
3. Click "Virtual Account"
4. Account will be created automatically
5. Transfer money to the account
6. Wallet should be credited instantly
