<?php
require_once 'config/database.php';
require_once 'includes/zashof.php';

echo "<h1>Payment Request Test</h1>";

try {
    $zashof = new ZashofPayment();
    
    if (!$zashof->isEnabled()) {
        throw new Exception("Zashof payment gateway is not enabled or properly configured");
    }
    
    // Prepare test student info
    $student_info = [
        'student_id' => 'TEST123',
        'name' => 'Test Student',
        'email' => 'test@example.com',
        'phone' => '01712345678'
    ];
    
    // URLs
    $redirect_url = 'http://' . $_SERVER['HTTP_HOST'] . '/fees/zashof_success.php';
    $cancel_url = 'http://' . $_SERVER['HTTP_HOST'] . '/fees/my_fees.php';
    
    echo "<h2>Sending Test Payment Request</h2>";
    echo "<p><strong>Amount:</strong> 100 BDT</p>";
    echo "<p><strong>Student:</strong> " . htmlspecialchars($student_info['name']) . "</p>";
    echo "<p><strong>Redirect URL:</strong> " . htmlspecialchars($redirect_url) . "</p>";
    echo "<p><strong>Cancel URL:</strong> " . htmlspecialchars($cancel_url) . "</p>";
    
    // Create charge
    echo "<h2>Creating Charge...</h2>";
    $response = $zashof->createCharge(100, $student_info, $redirect_url, $cancel_url);
    
    echo "<h3>Response:</h3>";
    echo "<pre>" . htmlspecialchars(json_encode($response, JSON_PRETTY_PRINT)) . "</pre>";
    
} catch (Exception $e) {
    echo "<h3>Error:</h3>";
    echo "<div style='color: red; padding: 10px; border: 1px solid red; border-radius: 5px;'>";
    echo "<strong>" . htmlspecialchars($e->getMessage()) . "</strong>";
    echo "</div>";
    
    // Check if it's the specific API key error
    if (strpos($e->getMessage(), 'Unauthorized request. Invalid API key') !== false) {
        echo "<div style='background-color: #fff3cd; border: 1px solid #ffeaa7; padding: 15px; border-radius: 5px; margin-top: 15px;'>";
        echo "<h4>Important Notice:</h4>";
        echo "<p>This error indicates that the Zashof API is rejecting your API key. This is not an issue with your system configuration.</p>";
        echo "<p><strong>What you need to do:</strong></p>";
        echo "<ol>";
        echo "<li>Contact Zashof support immediately</li>";
        echo "<li>Provide them with your API key: <code>73765654368fafdf094aa6841252168132430908268fafdf094aa82125124609</code></li>";
        echo "<li>Explain that you're getting 'Unauthorized request. Invalid API key' when trying to create charges</li>";
        echo "<li>Ask them to verify that your API key is activated and not restricted</li>";
        echo "</ol>";
        echo "</div>";
    }
}

echo "<p><a href='fees/my_fees.php' class='btn btn-primary'>Back to My Fees</a></p>";
?>