<?php


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve the data sent by the webhook
    $status = $_POST['status'];
    $order_id = $_POST['order_id'];
    $remark1 = $_POST['remark1'];
    $email=$remark1;

    // Check if the status is "SUCCESS"
    if ($status === 'SUCCESS') {
        // Process the data here as needed
        // For example, log it or perform other actions

        // Respond to the webhook with a success message
        
// API endpoint URL
$url = "https://upi.clusterwebsolution.com/api/check-order-status";

// POST data
$postData = array(
    "user_token" => "2d147974d89c914f857db9adef8ffd6f",
    "order_id" => $order_id
);

// Initialize cURL session
$ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

// Execute cURL session and POST the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo "cURL Error: " . curl_error($ch);
    exit;
}

// Close cURL session
curl_close($ch);

// Decode the JSON response
$responseData = json_decode($response, true);

// Check if the API call was successful
if ($responseData["status"] === "COMPLETED") {
    // API call was successful
    // Access the response data as needed
    $txnStatus = $responseData["result"]["txnStatus"];
    $orderId = $responseData["result"]["orderId"];
    $status = $responseData["result"]["status"];
    $amount = $responseData["result"]["amount"];
    $date = $responseData["result"]["date"];
    $utr = $responseData["result"]["utr"];
 $conn = mysqli_connect("localhost", "nuahkbzh_upi", "nuahkbzh_upi", "nuahkbzh_upi");
 $charge=2;
  $amountc=$amount-(($amount*$charge)/100);
  $amounts= round($amountc);
 
   $quertyed="UPDATE `users` SET `wbal` = wbal+'$amounts' WHERE `users`.`user_token` = '$remark1'";
     
$hfjg=mysqli_query($conn,$quertyed);
} else {
    // API call failed
    $errorMessage = $responseData["message"];
    echo "API Error: $errorMessage";
}

    } else {
        // Respond with an error message if the status is not "SUCCESS"
        http_response_code(400); // Bad Request
        echo "Invalid status: " . $status;
    }

    // You may want to add additional error handling and security measures here
} else {
    http_response_code(400); // Bad Request
    echo "Invalid request method";
}
?>