<?php
/**
 * Stripe Checkout success landing page.
 *
 * Stripe will redirect here with ?session_id={CHECKOUT_SESSION_ID}.
 * We fetch the session to show a simple "successful / pending" message.
 */

require __DIR__ . '/stripe-config.php';
require __DIR__ . '/includes/stripe-api.php';

header('Content-Type: text/html; charset=utf-8');

$sessionId = isset($_GET['session_id']) ? trim((string) $_GET['session_id']) : '';
if ($sessionId === '') {
    http_response_code(400);
    echo "<h2>Missing session.</h2>";
    exit;
}

$res = stripe_get_checkout_session($sessionId);
if (!$res['ok'] || !isset($res['session']) || !is_array($res['session'])) {
    http_response_code(200);
    echo "<h2>Payment received.</h2>";
    echo "<p>We are verifying your payment. If you need help, contact support.</p>";
    exit;
}

$session = $res['session'];
$paymentStatus = (string) ($session['payment_status'] ?? '');
$status = (string) ($session['status'] ?? '');

if ($paymentStatus === 'paid' || $status === 'complete') {
    echo "<h2>Payment successful.</h2>";
    echo "<p>Thank you — your payment has been received.</p>";
    exit;
}

echo "<h2>Payment processing.</h2>";
echo "<p>Your payment is not marked as paid yet. If you used a bank method, it may take time to confirm.</p>";

