<?php
/**
 * Test page for Stripe integration
 *
 * @package    Subscription_System
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Get Stripe API keys
$publishable_key = get_option('subscription_stripe_publishable_key');
$secret_key = get_option('subscription_stripe_secret_key');

// Check if keys are configured
$keys_configured = !empty($publishable_key) && !empty($secret_key);
?>

<div class="stripe-test-container">
    <h2>Stripe Integration Test</h2>
    
    <div class="test-results">
        <h3>Configuration Check</h3>
        <ul>
            <li>Publishable Key: <?php echo !empty($publishable_key) ? '✓ Configured' : '✗ Missing'; ?></li>
            <li>Secret Key: <?php echo !empty($secret_key) ? '✓ Configured' : '✗ Missing'; ?></li>
            <li>PHP cURL Extension: <?php echo function_exists('curl_version') ? '✓ Enabled' : '✗ Disabled'; ?></li>
        </ul>
        
        <?php if ($keys_configured): ?>
            <div class="test-actions">
                <button id="test-stripe-connection" class="button">Test Stripe Connection</button>
                <div id="connection-result"></div>
            </div>
        <?php else: ?>
            <div class="error-message">
                <p>Please configure your Stripe API keys in the plugin settings before testing.</p>
            </div>
        <?php endif; ?>
    </div>
    
    <?php if ($keys_configured): ?>
        <div class="manual-checkout-test">
            <h3>Manual Checkout Test</h3>
            <p>This will create a simple checkout session directly using the Stripe API.</p>
            
            <form id="manual-checkout-form">
                <label for="amount">Amount (USD):</label>
                <input type="number" id="amount" name="amount" value="10.00" min="1" step="0.01">
                
                <button type="submit" class="button">Create Checkout Session</button>
            </form>
            
            <div id="manual-checkout-result"></div>
        </div>
    <?php endif; ?>
</div>

<script>
jQuery(document).ready(function($) {
    // Test Stripe connection
    $('#test-stripe-connection').on('click', function() {
        var button = $(this);
        button.prop('disabled', true).text('Testing...');
        
        $('#connection-result').html('');
        
        $.ajax({
            url: '<?php echo esc_url(admin_url('admin-ajax.php')); ?>',
            type: 'POST',
            data: {
                action: 'test_stripe_connection',
                nonce: '<?php echo esc_attr(wp_create_nonce('subscription_checkout_nonce')); ?>'
            },
            success: function(response) {
                if (response.success) {
                    $('#connection-result').html('<div class="success-message">' + response.data.message + '</div>');
                } else {
                    $('#connection-result').html('<div class="error-message">' + response.data.message + '</div>');
                }
                button.prop('disabled', false).text('Test Stripe Connection');
            },
            error: function() {
                $('#connection-result').html('<div class="error-message">Error connecting to server. Please check PHP error logs.</div>');
                button.prop('disabled', false).text('Test Stripe Connection');
            }
        });
    });
    
    // Manual checkout test
    $('#manual-checkout-form').on('submit', function(e) {
        e.preventDefault();
        
        var amount = $('#amount').val();
        var button = $(this).find('button');
        
        button.prop('disabled', true).text('Creating...');
        $('#manual-checkout-result').html('');
        
        // Direct API call using fetch
        fetch('https://api.stripe.com/v1/checkout/sessions', {
            method: 'POST',
            headers: {
                'Authorization': 'Bearer <?php echo esc_js($secret_key); ?>',
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: new URLSearchParams({
                'payment_method_types[]': 'card',
                'line_items[0][price_data][currency]': 'usd',
                'line_items[0][price_data][product_data][name]': 'Test Product',
                'line_items[0][price_data][unit_amount]': Math.round(amount * 100),
                'line_items[0][quantity]': 1,
                'mode': 'payment',
                'success_url': '<?php echo esc_js(home_url('/stripe-test-success')); ?>',
                'cancel_url': '<?php echo esc_js(home_url('/stripe-test-cancel')); ?>'
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.url) {
                $('#manual-checkout-result').html(
                    '<div class="success-message">Checkout session created successfully!</div>' +
                    '<p>Session ID: ' + data.id + '</p>' +
                    '<p><a href="' + data.url + '" target="_blank" class="button">Open Checkout</a></p>'
                );
            } else if (data.error) {
                $('#manual-checkout-result').html('<div class="error-message">Error: ' + data.error.message + '</div>');
            }
            button.prop('disabled', false).text('Create Checkout Session');
        })
        .catch(error => {
            $('#manual-checkout-result').html('<div class="error-message">Error: ' + error.message + '</div>');
            button.prop('disabled', false).text('Create Checkout Session');
        });
    });
});
</script>

<style>
.stripe-test-container {
    max-width: 800px;
    margin: 20px auto;
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.test-results, .manual-checkout-test {
    margin-bottom: 30px;
    padding: 15px;
    background: #f9f9f9;
    border-radius: 4px;
}

.test-actions, #manual-checkout-form {
    margin-top: 15px;
}

.success-message {
    color: #4CAF50;
    font-weight: bold;
    padding: 10px;
    background: #E8F5E9;
    border-radius: 4px;
    margin: 10px 0;
}

.error-message {
    color: #F44336;
    font-weight: bold;
    padding: 10px;
    background: #FFEBEE;
    border-radius: 4px;
    margin: 10px 0;
}

#manual-checkout-form label {
    display: block;
    margin-bottom: 5px;
}

#manual-checkout-form input {
    margin-bottom: 10px;
    padding: 8px;
    width: 100%;
    max-width: 200px;
}

button {
    cursor: pointer;
}
</style> 