## Authentication Setup

### OAuth2
- Enter your client_id and client_secret in the settings screen
- Token request is made to /authorization/oauth/token
- Bearer token is used for API calls

### Basic Auth
- Enter your MerchantID and APIKey
- Basic Auth header is generated as MerchantID:APIKey (Base64)
- Used for endpoints that support Basic Auth

### Environment Selection
- Choose "sandbox" or "production" in the settings
- Endpoints switch automatically based on environment

### Secure Credential Storage
- Credentials are stored in ENV/config, not in code
- Secret keys and API keys are encrypted
# Computop Paygate Settings Configuration

This module provides a comprehensive web-based configuration interface for managing Computop Paygate payment gateway settings.

## Features

- **Web-Based Configuration**: Easy-to-use settings screen accessible through the application interface
- **Connection Testing**: Validate your PayGate configuration before processing live payments
- **Environment Management**: Separate sandbox and production environments
- **Secure Storage**: Sensitive data (secret keys) are encrypted in the configuration
- **Real-time Validation**: AJAX-based form validation for immediate feedback

## Accessing Settings

Navigate to: `/NGauge/paygate/settings/index`

Or use the application menu to access **PayGate Settings**.

## Configuration Fields

### API Connection Settings

| Field | Description | Required | Example |
|-------|-------------|----------|---------|
| **Enable Paygate Integration** | Toggle to enable/disable the module | Yes | ✓ |
| **Environment** | Select Sandbox or Production | Yes | Production |
| **Merchant ID** | Your Computop Merchant ID | Yes | MerchantXYZ |
| **API Key** | API key for Basic Auth (with Merchant ID) | Yes | *(encrypted)* |
| **Client ID** | OAuth2 client ID | Yes | client123 |
| **Client Secret** | OAuth2 client secret | Yes | *(encrypted)* |
| **Secret Key** | HMAC signing/webhook verification key | Yes | *(encrypted)* |

### Payment Settings

| Field | Description | Default |
|-------|-------------|---------|
| **Auto Capture Payments** | Automatically capture after authorization | Yes |
| **Enable Debug Logging** | Log detailed transaction information | No |

### URL Configuration

| Field | Description | Example |
|-------|-------------|---------|
| **Notification URL** | Webhook endpoint for payment notifications | https://example.com/paygate/notification |
| **Success Return URL** | Redirect URL after successful payment | https://example.com/payment/success |
| **Error Return URL** | Redirect URL after payment failure | https://example.com/payment/error |

## Currency Codes

- **EUR** - Euro
- **GBP** - British Pound Sterling
- **USD** - US Dollar

## Environment Setup

### Sandbox (Testing)

- Use sandbox credentials from your Computop merchant portal
- API URL: `https://test.computop-paygate.com/api/v2`
- Test card numbers available in the Computop developer documentation
- No real money is charged

### Production (Live)

- Use production credentials from your Computop merchant portal
- API URL: `https://www.computop-paygate.com/api/v2`
- Real transactions will be processed
- Real money will be charged to customers

## Testing Connection

1. Fill in all required configuration fields
2. Click the **Test Configuration** button
3. The system will validate:
   - API URL accessibility
   - Merchant code format
   - Terminal number format
   - Currency code format
   - Secret key validity

## Security Considerations

### Data Encryption

Sensitive configuration data is automatically encrypted:
- Secret keys are encrypted using `Security::encryptSensitiveData()`
- Encrypted values are stored in the params configuration file

### Access Control

- Settings page requires authentication
- Only authenticated users can access configuration
- Role-based access control can be added through Yii2's RBAC system

## Configuration Storage

Settings are stored in: `@common/config/params.php`

Example configuration structure:
```php
'paygate' => [
    'enabled' => true,
    'environment' => 'production',
    'merchant_id' => 'MerchantXYZ',
    'api_key' => '[ENCRYPTED]',
    'client_id' => 'client123',
    'client_secret' => '[ENCRYPTED]',
    'currency' => 'EUR',
    'secret_key' => '[ENCRYPTED]',
    'auto_capture' => true,
    'debug' => false,
    'notification_url' => 'https://example.com/paygate/notification',
    'success_url' => 'https://example.com/payment/success',
    'error_url' => 'https://example.com/payment/error',
]
```

## Usage in Code

### Accessing Configuration

```php
// Get PayGate settings
$settings = Yii::$app->params['paygate'];

// Check if enabled
if ($settings['enabled'] ?? false) {
    // PayGate is enabled
}

// Get merchant ID
$merchantId = $settings['merchant_id'];

// Get decrypted secret key
$secretKey = Security::decryptSensitiveData($settings['secret_key']);
```

### Getting API URL

```php
use PortalPeople\paygate\models\Paygate;

$apiUrl = Paygate::getApiUrl('api');
```

## API Endpoints

The settings controller provides the following endpoints:

- **GET/POST** `/paygate/settings/index` - Display and save settings
- **POST** `/paygate/settings/validate` - AJAX validation
- **POST** `/paygate/settings/test-connection` - Test configuration

## Troubleshooting

### Connection Test Fails

1. **Verify credentials**: Ensure merchant code and secret key are correct
2. **Check environment**: Make sure you're using the right environment (sandbox/production)
3. **Validate formats**:
   - Merchant ID: alphanumeric string from Computop portal
   - Currency: ISO 4217 alpha code (EUR, GBP, USD)
4. **Review firewall**: Ensure your server can connect to Computop servers

### Settings Not Saving

1. **File permissions**: Check write permissions on `@common/config/params.php`
2. **Validation errors**: Check for form validation messages
3. **Server logs**: Review application logs for error details

### Secret Key Issues

1. **Length**: Secret key should be at least 20 characters
2. **Format**: Typically Base64 encoded
3. **Source**: Obtain from your bank's merchant panel

## Integration with Existing Code

The configuration automatically integrates with the existing Module bootstrap:

```php
// The Module.php bootstrap can now read from params
$settings = Yii::$app->params['paygate'] ?? [];

$app->setComponents(["{$this->id}" => [
    'class' => 'PortalPeople\paygate\Module',
    'connection' => [
        'url' => Paygate::SANDBOX_URL,
        'merchant_id' => $settings['merchant_id'] ?? '',
        'api_key' => Security::decryptSensitiveData($settings['api_key'] ?? ''),
        'client_id' => $settings['client_id'] ?? '',
        'client_secret' => Security::decryptSensitiveData($settings['client_secret'] ?? ''),
        'secret_key' => Security::decryptSensitiveData($settings['secret_key'] ?? ''),
    ]
]]);
```

## Resources

- [Computop Developer Portal](https://developer.computop.com)
- [API Integration Guide](https://developer.computop.com/pages/viewpage.action?pageId=179963215)
- [Test Environment](https://test.computop-paygate.com)

## Support

For issues or questions:
- Check application logs: `@app/runtime/logs/`
- Review Computop developer documentation
- Contact Computop technical support for credential issues

## License

MIT License - See LICENSE.md file for details
