<?php
if (!defined('ADDFUNDS')) {
    http_response_code(404);
    die();
}

$orderId = $_GET['order_id'] ?? null;

if (!$orderId) {
    echo "Invalid Request.";
    exit;
}

$verifyPayload = json_encode(["md5" => $orderId]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://khqr-nang--ten.vercel.app/api/check');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjoiYzlmZmRiYjQ0YzM0NDNkNiJ9LCJpYXQiOjE3NDI2MTYzNDcsImV4cCI6MTc1MDM5MjM0N30.f_PDHWG83DOUo36zFUQQeMle2LUGV_4OoS59E1CoMOA'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $verifyPayload);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

if ($data && $data["verified"] && $data["responseCode"] === 0) {
    $update = $conn->prepare("UPDATE payments SET payment_status='Completed' WHERE payment_extra=:md5");
    $update->execute(["md5" => $orderId]);
    echo "✅ Payment Verified!";
} else {
    echo "❌ Not Paid Yet or Failed";
}
?>
