<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Instant Balance Add By R.J DEVELOPER</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();

$hostname = "localhost";
 $username = "u459483528_tiranga1";
 $password ="|62Etg87>Dn";
 $database = "u459483528_tiranga1";


$conn = new mysqli($hostname, $username, $password, $database);

if ($conn->connect_error) {
    echo "<script>alert('Database connection failed: " . $conn->connect_error . "');</script>";
    die();
}

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

    if (!isset($_SESSION['wallet_updated'][$order_id])) {
        $url = "https://shivajigateway.com/api/check-order-status";

        $postData = array(
            "user_token" => "0f61e67c7465cc40d26726b6143d6731",                 //Api Token Click on developer option then get
            "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"];
            $transaction = $responseData["result"]["utr"];
            $HkbDate = $responseData["result"]["date"];

            echo "PAYMENT SUCCESSFULLY";

            if ($status === "SUCCESS") {
                $sql = "UPDATE usertable SET walletamount = walletamount + $amount WHERE phone = '$customer_mobile'";
                 // SQL query to insert into the wallet table
                $sql2 = "INSERT INTO wallet (phone, amount, txn_id, date, txn_date, PAYMENTMODE, RESPMSG, BANKTXNID, status) VALUES ('$customer_mobile', '$amount', '$transaction', '$HkbDate', '$HkbDate', 'PhonePe', '$transaction', '$order_id', '$status')";


                if ($conn->query($sql) === TRUE && $conn->query($sql2) === TRUE) {
                    echo "<script>
                    Swal.fire({
                        title: 'SUCCESS',
                        text: 'WALLET UPDATED SUCCESSFULLY',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://uclservices.online/admin/wallet5.php';
                    });
                </script>";

                    $_SESSION['wallet_updated'][$order_id] = true;
                } else {
                    echo "<script>
                    Swal.fire({
                        title: 'WALLET UPDATE FAILED',
                        text: ''Update failed: " . $conn->error . "',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://uclservices.online/admin/wallet5.php';
                    });
                </script>";
                }
            }
        } else {
            echo "<script>
                    Swal.fire({
                        title: 'PAYMMENT GATEWAY API PROBLEM',
                        text: 'API call unsuccessful. Status: " . ($responseData["status"] ?? "Unknown") . "',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://uclservices.online/admin/wallet5.php';
                    });
                </script>";
        }
    } else {
        echo "<script>
                    Swal.fire({
                        title: 'Dont even try to repeat the evening order this is your fathers code. Get Lost',
                        text: 'Wallet already updated for this order ID',
                        icon: 'info',
                        confirmButtonText: 'OK'
                    }).then(function() {
                        window.location.href = 'https://uclservices.online/admin/wallet5.php';
                    });
                </script>";
    }
} else {
    echo "<script>Swal.fire('Error', 'Order ID not set in session', 'error');</script>";
}
?>

</body>
</html>
