<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>instant add payment by kgn </title>
    <!-- Include jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <!-- Include SweetAlert -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.14/dist/sweetalert2.all.min.js"></script>
</head>
<body>

<?php
session_start();
include '../includes/config.php';

if (isset($_SESSION['order_id'])) {
    $order_id = $_SESSION['order_id'];
    $customer_mobile = $_SESSION['customer_mobile'];

    if (!isset($_SESSION['wallet_updated'][$order_id])) {
        $url = "https://pginstant.gdrd.site/api/check-order-status";
        
        $minimum_amount="0";                                         // minimum amount add restriction from users...

        $postData = array(
            "user_token" => "035e3549df7ab69e29d5ffabe605988a",
            "order_id" => $order_id
        );

        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            echo "<script>alert('cURL Error: " . curl_error($ch) . "');</script>";
            exit;
        }

        curl_close($ch);

        $responseData = json_decode($response, true);

        if ($responseData["status"] === "COMPLETED") {
            $txnStatus = $responseData["result"]["txnStatus"];
            $status = $responseData["result"]["status"];
            $amount = $responseData["result"]["amount"];

            echo "PAYMENT SUCCESSFULLY";

            if ($status === "SUCCESS") {
                if ($amount > $minimum_amount) { 
                
                $sql = "UPDATE users SET status = '1' WHERE phone = '$customer_mobile'";

                if ($ahk_conn->query($sql) === TRUE) {
                    echo "<script>
                    Swal.fire({
                        title: 'SUCCESS',
                        text: 'YOUR REGISTERATION SUCCESSFULLY DONE',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://".$_SERVER['SERVER_NAME']."/admin/';
                    });
                </script>";

                    $_SESSION['wallet_updated'][$order_id] = true;
                } else {
                    echo "<script>
                    Swal.fire({
                        title: 'FAILED',
                        text: 'Update failed: " . $ahk_conn->error . "',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://".$_SERVER['SERVER_NAME']."';
                    });
                </script>";
                }
            } else {
            echo "<script>
                    Swal.fire({
                        title: 'You Are Trying To Cheat ! Amount Minimun ".($minimum_amount+0)."',
                        text: 'This Amount Not refunded or Added ! Sorry',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://".$_SERVER['SERVER_NAME']."/admin/wallet.php';
                    });
                </script>";
        }
            }
        } else {
            echo "<script>
                    Swal.fire({
                        title: 'PAYMENT GATEWAY API PROBLEM CONTACT TO ADMIN',
                        text: 'API call unsuccessful. Status: " . ($responseData["status"] ?? "Unknown") . "',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://".$_SERVER['SERVER_NAME']."';
                    });
                </script>";
        }
    } else {
        echo "<script>
                    Swal.fire({
                        title: 'PAYMENT FAILD',
                        text: 'YOUR PAYMENT WAS FAILED',
                        icon: 'info',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://".$_SERVER['SERVER_NAME']."';
                    });
                </script>";
    }
} else {
    echo "<script>Swal.fire('Error', 'Order ID not set in session', 'error');</script>";
}
?>


</body>
</html>
