<?php
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $method = $_POST["method"];
    $trxid = $_POST["trxid"];
    $date = date("d-M-Y");
    $time = date("h:i:s A");

    $query = "SELECT * FROM transaction WHERE trxid = '$trxid' AND gateway = '$method'";
    $result = $conn->query($query);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $amount = $row["amount"];
        $sender = $row["sender"];
        $status = $row["status"];

        if ($status === '0') {
            $update = "UPDATE transaction SET status = 1 WHERE trxid = '$trxid'";
            $sql = "INSERT INTO addmoney (name, email, method, number, amount, trxid, status, date, time) VALUES ('$name', '$email', '$method', '$sender', '$amount', '$trxid', 'Success', '$date', '$time')";
            $update2 = "UPDATE users SET balance = balance + '$amount' WHERE email = '$email'";

            if ($conn->query($update) === TRUE && $conn->query($sql) === TRUE && $conn->query($update2) === TRUE) {
                echo "Add Money Success";
            } else {
                echo "Add Money Failed";
            }
        } else {
            echo "Transaction ID Already Used";
        }
    } else {
        echo "Invalid Transaction ID";
    }

    $conn->close();
} else {
    echo "Invalid Request Method";
}
?>