<?php
require 'db.php';
require 'functions.php';

$orderid = cleanInput($_POST['razorpay_order_id'] ?? '');
$paymentid = cleanInput($_POST['razorpay_payment_id'] ?? '');
$signature = cleanInput($_POST['razorpay_signature'] ?? '');

if ($orderid === '' || $paymentid === '' || $signature === '') {
    jsonOut(false, 'Missing payment verification data');
}

try {
    if (!razorpayVerifyPaymentSignature($orderid, $paymentid, $signature)) {
        logDebug('[ajax_payment_verify] Signature mismatch for order ' . $orderid);
        jsonOut(false, 'Payment verification failed. If money was deducted, it will be confirmed automatically.');
    }

    $result = markOrderPaidAndActivatePass($pdo, $orderid, $paymentid, $signature);
    jsonOut($result['success'], $result['message']);
} catch (Exception $e) {
    logDebug('[ajax_payment_verify] ' . $e->getMessage());
    jsonOut(false, 'Something went wrong while verifying payment');
}
