<?php
$conn = new mysqli("localhost", "kkfxoaad", "Bitok@2025", "kkfxoaad_bella");
$data = json_decode(file_get_contents("php://input"), true);

$transactionRef = $data['transaction_reference'];
$status = strtolower($data['status']); // success or failed

$stmt = $conn->prepare("UPDATE kopokopo_transactions SET status=? WHERE transaction_reference=?");
$stmt->bind_param("ss", $status, $transactionRef);
$stmt->execute();

if ($status === "success") {
    // also mark checkout_attempts as paid
    $result = $conn->query("SELECT attempt_id FROM kopokopo_transactions WHERE transaction_reference='$transactionRef'");
    $row = $result->fetch_assoc();
    $attemptId = $row['attempt_id'];

    $conn->query("UPDATE checkout_attempts SET status='paid' WHERE id=$attemptId");
}

http_response_code(200);
echo "Callback received";
?>
