<?php include ("../serive/samparka.php"); ?>

<?php
 $date = date_default_timezone_set('Asia/Kolkata');
        //Day,Date with time AM/PM
        $today = date("F j, Y, g:i a");
    

$rawPostData = file_get_contents("php://input");

// Parse the URL-encoded data from the callback
$notificationData = [];
parse_str($rawPostData, $notificationData);


// Log the incoming data to a file
$logFilePath = './sunpay_transaction_log.txt';  // Adjust the path as necessary

$logData = "Date: " . $today . "\n";
$logData .= "Raw Data: " . $rawPostData . "\n";
$logData .= "Parsed Data: " . print_r($notificationData, true) . "\n";
$logData .= "---------------------------------------------\n";

// Append the log data to the log file
file_put_contents($logFilePath, $logData, FILE_APPEND);


    // Extract data from the notification
    $tradeResult = $notificationData['tradeResult'];
    $mchId = $notificationData['mchId'];
    $mchOrderNo = $notificationData['mchOrderNo'];
    $oriAmount = $notificationData['oriAmount'];
    $amount = $notificationData['amount'];
    $orderDate = $notificationData['orderDate'];
    $orderNo = $notificationData['orderNo'];

    
    echo 'success';




// Function to log errors
function logError($message) {
    file_put_contents('woompibabylogs.txt', date('Y-m-d H:i:s') . " - " . $message . "\n", FILE_APPEND);
}

// Log the callback data for debugging
logError("Received data: " . print_r($_POST, true));

// Load the configuration
$config = require 'config.php';


$data = $_POST;


// Order processing logic
$mchOrderNo = $mchOrderNo;

// Check if the order exists and is not already processed (status = 0)
$checkamt = mysqli_query($conn, "SELECT motta, balakedara FROM thevani WHERE dharavahi = '".$mchOrderNo."' AND sthiti = '0'");

if (!$checkamt) {
    logError("Database query error: " . mysqli_error($conn));
    echo json_encode([
        "message" => "fail(database error)",
        "status" => false,
    ]);
    exit;
}

$checkamtrow = mysqli_num_rows($checkamt);

if ($checkamtrow >= 1) {
    $checkamtar = mysqli_fetch_array($checkamt);
    $motta = $checkamtar['motta'];
    $shonuid = $checkamtar['balakedara'];

    // Update the user's balance
    $nabikarana = "UPDATE shonu_kaichila
                   SET motta = ROUND(motta + '".$motta."', 2)
                   WHERE balakedara = '".$shonuid."'";
    
    if (!$conn->query($nabikarana)) {
        logError("Database update error: " . mysqli_error($conn));
        echo json_encode([
            "message" => "fail(update error)",
            "status" => false,
        ]);
        exit;
    }

    // Update the order status to processed (status = 1)
    $sql2 = mysqli_query($conn, "UPDATE thevani SET sthiti = '1' WHERE dharavahi = '".$mchOrderNo."'");

    if (!$sql2) {
        logError("Database update error: " . mysqli_error($conn));
        echo json_encode([
            "message" => "fail(update error)",
            "status" => false,
        ]);
        exit;
    }
} else {
    echo "ok"; // Response back to the webhook
exit;
}

echo "ok"; // Response back to the webhook
exit;
?>
