<?php
// Retrieve POST data
$utr = $_POST['utr'];
$transactionId = $_POST['TransactionId'];

// Example code to connect to the database
$servername = "localhost";
$username = "nuahkbzh_upi";
$password = "nuahkbzh_upi";
$dbname = "nuahkbzh_upi";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Check if the UTR number already exists in the database
$existingUtr = "SELECT * FROM transactions WHERE utr = ?";
$stmtExistingUtr = $conn->prepare($existingUtr);
$stmtExistingUtr->bind_param("s", $utr);
$stmtExistingUtr->execute();
$result = $stmtExistingUtr->get_result();

if ($result->num_rows > 0) {
    // UTR number already exists, return error with HTTP status code 409
    http_response_code(409);
    echo json_encode(array("status" => "error", "message" => "UTR number already exists"));
} else {
    // Prepare SQL statement
    $sql = "INSERT INTO transactions (utr, transaction_id) VALUES (?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $utr, $transactionId);

    // Execute the statement
    if ($stmt->execute() === TRUE) {
        // Success
        echo json_encode(array("status" => "success"));
    } else {
        // Error
        echo json_encode(array("status" => "error", "message" => $conn->error));
    }
}

// Close statements
$stmtExistingUtr->close();
$stmt->close();
$conn->close();
?>
