<?php
session_start();

$host = "127.0.0.1";      
$user = "root";           
$password = "root";           
$dbname = "pay"; 

$conn = mysqli_connect($host, $user, $password, $dbname);

if (!$conn) {
    die("Database connection failed: " . mysqli_connect_error());
}

if(!isset($_SESSION['alogin'])){
    die("Login required");
}

$email = $_SESSION['alogin'];

$amount = $_POST['amount'];
$sender = $_POST['sender'];
$trxid  = $_POST['trxid'];
$method = strtolower(trim($_POST['method'])); // ✅ FIX

// duplicate trx check
$check = mysqli_query($conn, "SELECT * FROM payment WHERE trxID='$trxid'");
if(mysqli_num_rows($check) > 0){
    $_SESSION['Error'] = "এই Transaction ID ইতিমধ্যে ব্যবহার হয়েছে!";
    header("Location: recharge.php");
    exit();
}

// insert pending request
$sql = "INSERT INTO payment (email, amount, customerMsisdn, trxID, method, status)
        VALUES ('$email','$amount','$sender','$trxid','$method','pending')";

mysqli_query($conn, $sql);

$_SESSION['Success'] = "রিকোয়েস্ট সাবমিট হয়েছে! Admin approval এর অপেক্ষা করুন।";
header("Location: recharge.php");
?>