<?php
session_start();
include "db.php";

require('razorpay-php/Razorpay.php');
use Razorpay\Api\Api;

/* Razorpay credentials */
$keyId = "rzp_test_RpUMYUDg3gBSOb";
$keySecret = "1239XYUaUCuHKUfDwdiU14Sr";

/* Correct API object */
$api = new Api($keyId, $keySecret);

/* Verify payment */
if (!empty($_POST['razorpay_payment_id']) && 
    !empty($_POST['razorpay_order_id']) && 
    !empty($_POST['razorpay_signature'])) {

    try {

        $attributes = [
            'razorpay_order_id'   => $_POST['razorpay_order_id'],
            'razorpay_payment_id' => $_POST['razorpay_payment_id'],
            'razorpay_signature'  => $_POST['razorpay_signature']
        ];

        /* Verify signature */
        $api->utility->verifyPaymentSignature($attributes);

        /* Update order to paid */
        $stmt = $pdo->prepare("
            UPDATE orders 
            SET payment_status='paid', payment_id=? 
            WHERE razorpay_order_id=?
        ");
        $stmt->execute([
            $_POST['razorpay_payment_id'], 
            $_POST['razorpay_order_id']
        ]);

        /* Clear cart session */
        unset($_SESSION['cart']);
        unset($_SESSION['current_order_id']);
        unset($_SESSION['order_amount']);

        /* Redirect after 3 seconds */
        header("refresh:3; url=index.php");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Payment Success</title>
    <style>
        :root {
            --brand: #002344;
            --brand-light: #e6edf5;
            --brand-mid: #3b5168;
        }

        body {
            font-family: Arial, sans-serif;
            background: var(--brand-light);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .success-box {
            background: #ffffff;
            padding: 35px;
            border-radius: 14px;
            box-shadow: 0 4px 18px rgba(0, 35, 68, 0.20);
            text-align: center;
            max-width: 420px;
            width: 100%;
            border-top: 6px solid var(--brand);
        }

        .success-box h2 {
            color: var(--brand);
            font-size: 28px;
            margin-bottom: 10px;
            font-weight: 700;
        }

        .success-box p {
            color: #333;
            font-size: 16px;
            margin: 8px 0;
        }

        .loader {
            margin: 20px auto;
            border: 4px solid var(--brand-light);
            border-top: 4px solid var(--brand);
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 0.9s linear infinite;
        }

        @keyframes spin {
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>

<div class="success-box">
    <h2>Payment Successful</h2>
    <p>Your payment has been completed successfully.</p>
    <p>Order ID: <strong><?= $_POST['razorpay_order_id']; ?></strong></p>

    <div class="loader"></div>

    <p style="color: var(--brand); font-weight:600;">
        Redirecting to homepage...
    </p>
</div>

</body>
</html>

<?php
        exit();

    } catch (Exception $e) {

        /* Update status as failed */
        $stmt = $pdo->prepare("
            UPDATE orders 
            SET payment_status='failed' 
            WHERE razorpay_order_id=?
        ");
        $stmt->execute([$_POST['razorpay_order_id']]);

        echo "<h2>Payment verification failed!</h2>";
        echo "<pre>".$e->getMessage()."</pre>";
        exit();
    }

} else {
    echo "Invalid Payment Details!";
    exit();
}
?>
