<?php
include 'common/config.php';

$payload = file_get_contents('php://input');
$data = json_decode($payload, true);

if (isset($data['orderid']) && isset($data['status'])) {
    $orderId = (int)$data['orderid'];
    $apiStatus = strtolower(trim($data['status']));
    
    $dbStatus = 'processing';
    if ($apiStatus === 'success') {
        $dbStatus = 'completed';
    } elseif ($apiStatus === 'error') {
        $dbStatus = 'cancelled';
    }

    if ($orderId > 0) {
        $stmt = $conn->prepare("UPDATE orders SET status = ? WHERE id = ?");
        if ($stmt) {
            $stmt->bind_param("si", $dbStatus, $orderId);
            $stmt->execute();
            $stmt->close();
        }
    }
    
    echo json_encode(["status" => "success"]);
} else {
    echo json_encode(["status" => "error", "message" => "Invalid Payload"]);
}
?>
