<?php
session_start();
session_regenerate_id();
require_once('../system/function.php');
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Premium Checkout |⚡AK47💀</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;800&display=swap" rel="stylesheet">
    
<script disable-devtool-auto src="https://cdn.jsdelivr.net/npm/disable-devtool@latest"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        :root {
            --bg-dark: #0f172a;
            --outline-blue: #3b82f6;
            --outline-green: #10b981;
            --outline-yellow: #f59e0b;
            --outline-paytm: #00baf2;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Plus Jakarta Sans', sans-serif; }

        body {
            background-color: var(--bg-dark);
            background-image: radial-gradient(at 0% 0%, rgba(59, 130, 246, 0.1) 0px, transparent 50%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            padding: 20px;
        }

        .checkout-card {
            background: rgba(30, 41, 59, 0.8);
            backdrop-filter: blur(20px);
            border-radius: 28px;
            width: 100%;
            max-width: 400px;
            padding: 25px;
            text-align: center;
            border: 1px solid rgba(255,255,255,0.1);
            box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);
        }

        .box-blue {
            border: 2px solid var(--outline-blue);
            box-shadow: 0 0 15px rgba(59, 130, 246, 0.3);
            border-radius: 20px;
            padding: 20px;
            margin: 20px 0;
            background: rgba(59, 130, 246, 0.05);
        }
        .box-blue h2 { font-size: 2.5rem; font-weight: 800; }

        /* QR Container with Loading System */
        .box-green {
            border: 2px solid var(--outline-green);
            box-shadow: 0 0 15px rgba(16, 185, 129, 0.3);
            background: white;
            padding: 12px;
            border-radius: 20px;
            display: inline-block;
            margin-bottom: 20px;
            position: relative;
            min-width: 244px;
            min-height: 244px;
        }
        
        #qr-image { 
            width: 220px; 
            height: 220px; 
            display: block; 
            border-radius: 10px; 
            opacity: 0; /* Initially hidden */
            transition: opacity 0.3s ease;
        }

        /* QR Loader Styles */
        .qr-loader {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #334155;
        }
        .spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid var(--outline-green);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-bottom: 10px;
        }
        @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

        .btn-download {
            border: 2px solid var(--outline-yellow) !important;
            box-shadow: 0 0 15px rgba(245, 158, 11, 0.3);
            background: #f1f5f9;
            color: #000;
            width: 100%;
            padding: 15px;
            border-radius: 14px;
            font-weight: 700;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            margin-bottom: 15px;
            text-transform: uppercase;
        }

        .paytm-intent-btn {
            background: #002e6e;
            border: 2px solid var(--outline-paytm);
            box-shadow: 0 0 15px rgba(0, 186, 242, 0.4);
            border-radius: 16px;
            padding: 14px;
            text-decoration: none;
            color: #fff;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
        }

        .timer-sec { margin-top: 20px; color: #f87171; font-weight: 600; font-size: 0.9rem; }
    </style>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(isset($_POST['token'])&& 
!empty($_POST['token'])&& 
!empty($_POST['orderId'])&& 
!empty($_POST['txnAmount'])&& 
!empty($_POST['txnNote'])&&
!empty($_POST['cust_Mobile']) &&
!empty($_POST['cust_Email']) &&
!empty($_POST['callback_url'])&& 
!empty($_POST['checksum']) &&
safe_str($_POST['txnAmount'])>0  &&
is_numeric($_POST['cust_Mobile']) &&
strlen($_POST['cust_Mobile'])==10 && 
filter_var($_POST['cust_Email'], FILTER_VALIDATE_EMAIL)
){
    

$token = strip_tags($_POST['token']);
$orderId = safe_str($_POST['orderId']);
$txnAmount = safe_str($_POST['txnAmount']);
$txnNote = safe_str($_POST['txnNote']);
$cust_Mobile = safe_str($_POST['cust_Mobile']);
$cust_Email= strip_tags($_POST['cust_Email']);
$callback_url = strip_tags($_POST['callback_url']);
$checksum = $_POST['checksum'];

$res = sql_query("SELECT * FROM tb_partner WHERE token='".$token."' AND status='active' ");
$result = sql_fetch_array($res);

$upiuid = strip_tags(get_paytm_business($result['id'],"upi_id"));

if(sql_num_rows($res)>0 && $result['token']==$token){

$txn_query = sql_query("SELECT * FROM tb_virtualtxn WHERE client_orderid='".$orderId."' ");
if(sql_num_rows($txn_query)=='' && sql_num_rows($query)==0){
$paramList = array();
$paramList["upiuid"] = $upiuid;
$paramList["token"] = $token;
$paramList["orderId"] = $orderId;
$paramList["txnAmount"] = $txnAmount;
$paramList["txnNote"] = $txnNote;
$paramList["cust_Mobile"] = $cust_Mobile;
$paramList["cust_Email"] = $cust_Email;
$paramList["callback_url"] = $callback_url;	
require_once('../system/checksum.php');
$verifySignature = RechPayChecksum::verifySignature($paramList, $result['secret'], $checksum);
if($verifySignature){
	
$upi_id = get_paytm_business($result['id'],"upi_id");
$upi_name = get_paytm_business($result['id'],"upi_name");

if(isset($upi_id) && !empty($upi_id) && isset($upi_name) && !empty($upi_name) && $upiuid==$upi_id){
$txn_ref_id = GenRandomString().time();	
$_SESSION['muid'] = $result['id'];
$_SESSION['auth_token'] = $result['token'];
$_SESSION['txn_ref_id'] = $txn_ref_id;
$_SESSION['upi_id'] = $upi_id;
$_SESSION['client_orderid'] = $orderId;	
$_SESSION['txnAmount'] = $txnAmount;	
$_SESSION["txnNote"] = $txnNote;
$_SESSION['cust_Mobile'] = $cust_Mobile;
$_SESSION['cust_Email'] = $cust_Email;
echo "<script>
setTimeout(function(){ 
  document.getElementById(\"qrcode\").innerHTML = '';
  GenerateQR('".$upi_id."', '".$upi_name."', '".$txnAmount."', '".$txn_ref_id."', '".$txnNote."'); 
  
  // After generating, remove the title to prevent tooltip display
  setTimeout(function() {
    var qrElement = document.getElementById(\"qrcode\");
    qrElement.removeAttribute('title'); // Remove tooltip
    var upilink = qrElement.title || ''; // Optional: backup in case needed
    document.getElementById(\"upilink\").href = upilink;
  }, 500); // Slight delay to wait for QR code generation
}, 1500);
</script>";
	
$html = '
<form id="formSubmit" action="'.$callback_url.'" method="post" style="display:none;">
<input type="hidden" id="status" name="status">
<input type="hidden" id="message" name="message">
<input type="hidden" name="cust_Mobile" value="'.$cust_Mobile.'">
<input type="hidden" name="cust_Email" value="'.$cust_Email.'">
<input type="hidden" id="hash" name="hash">
<input type="hidden" id="checksum" name="checksum">
</form>
<script type="text/javascript" src="../assets/dist/js/paytmTxnStatus.js"></script>
';
echo $html;
}else{
$html = '
<h2 style="color:white" class="text-center">Please do not refresh this page...</h2>
<form id="formSubmit" action="'.$callback_url.'" method="post" style="display:none;">
<input type="hidden" name="status" value="FAILED" >
<input type="hidden" name="message" value="upi id is invalid or not updated" >
<input type="hidden" name="cust_Mobile" value="'.$cust_Mobile.'">
<input type="hidden" name="cust_Email" value="'.$cust_Email.'">
<input type="hidden" name="hash" value="false">
<input type="hidden" name="checksum" value="false">
</form>
<script type="text/javascript">
setTimeout(function(){ 
document.getElementById("formSubmit").submit();
}, 1500);
</script>
';	
echo $html;
exit();	
}
	
	
}else{
$html = '
<h2 style="color:white" class="text-center">Please do not refresh this page...</h2>
<form id="formSubmit" action="'.$callback_url.'" method="post" style="display:none;">
<input type="hidden" name="status" value="FAILED" >
<input type="hidden" name="message" value="Checksum Mismatch" >
<input type="hidden" name="cust_Mobile" value="'.$cust_Mobile.'">
<input type="hidden" name="cust_Email" value="'.$cust_Email.'">
<input type="hidden" name="hash" value="false">
<input type="hidden" name="checksum" value="false">
</form>
<script type="text/javascript">
setTimeout(function(){ 
document.getElementById("formSubmit").submit();
}, 1000);
</script>
';	
echo $html;	
exit();	
}


}else{
$html = '
<h2 style="color:white" class="text-center">Please do not refresh this page...</h2>
<form id="formSubmit" action="'.$callback_url.'" method="post" style="display:none;">
<input type="hidden" name="status" value="FAILED" >
<input type="hidden" name="message" value="This Order Id is Already Taken" >
<input type="hidden" name="cust_Mobile" value="'.$cust_Mobile.'">
<input type="hidden" name="cust_Email" value="'.$cust_Email.'">
<input type="hidden" name="hash" value="false">
<input type="hidden" name="checksum" value="false">
</form>
<script type="text/javascript">
setTimeout(function(){ 
document.getElementById("formSubmit").submit();
}, 1000);
</script>
';	
echo $html;		
exit();	
}



}else{
$html = '
<h2 style="color:white" class="text-center">Please do not refresh this page...</h2>
<form id="formSubmit" action="'.$callback_url.'" method="post" style="display:none;">
<input type="hidden" name="status" value="FAILED" >
<input type="hidden" name="message" value="Unauthorized Access or Token Is Invalid" >
<input type="hidden" name="cust_Mobile" value="'.$cust_Mobile.'">
<input type="hidden" name="cust_Email" value="'.$cust_Email.'">
<input type="hidden" name="hash" value="false">
<input type="hidden" name="checksum" value="false">
</form>
<script type="text/javascript">
setTimeout(function(){ 
document.getElementById("formSubmit").submit();
}, 1000);
</script>
';	
echo $html;		
exit();	
}

	
}else{
echo '<h2 style="color:white" class="text-center">Parameter Missing<br>Redirect...</h2>';
redirect($_POST['callback_url'],'2000');		
exit();	
}

}else{
echo '<h2 style="color:white" class="text-center">Unauthorized Access<br>Redirect...</h2>';
redirect(webdata('socket').base_url(),'2000');		
exit();	
}
?>




<div class="checkout-card">
    <div class="header">
        <h2 style="text-transform: uppercase;"><?php echo $result['company_name'];?></h2>
        <p style="font-size: 0.7rem; color: var(--outline-green); letter-spacing: 1px;">SECURE PAYMENT GATEWAY</p>
    </div>

    <div class="box-blue">
        <p style="font-size: 0.75rem; color: #94a3b8; margin-bottom: 5px;">PAYING AMOUNT</p>
        <h2>₹<?php echo $txnAmount;?></h2>
    </div>

    <div class="box-green" id="qrcode">
        <div class="qr-loader" id="qr-loader">
            <div class="spinner">   </div>
            <p style="font-size: 0.7rem; font-weight: 600;">LOADING QR...</p>
         </div>
        <img src="" id="qr-image" crossorigin="anonymous" onload="showQR()">
    </div>

    <button onclick="downloadQR()" class="btn-download">
        <i class="fas fa-file-download"></i> DOWNLOAD QR CODE
    </button>

    <a  class="paytm-intent-btn">
        <img src=" " width="22" style="border-radius: 4px;" onerror="this.style.display='none'"> 
        PAY WITH PAYTM APP
    </a>

    <p class="timer-sec"><i class="far fa-clock"></i> Expires in  <span id="upitimer" class="font-weight-bold"></span></p>
    
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script>
    // 1. QR Loading Logic: Hide loader and show image
    function showQR() {
        document.getElementById('qr-loader').style.display = 'none';
        document.getElementById('qr-image').style.opacity = '1';
    }

    

    // 3. Timer & Polling Logic
    function upiCountdown(elm, minute, second) {
        var display = document.getElementById(elm);
        var timer = (minute * 60) + second;
        var interval = setInterval(function () {
            var m = parseInt(timer / 60, 10);
            var s = parseInt(timer % 60, 10);
            display.textContent = (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
            if (--timer < 0) {
                clearInterval(interval);
                location.reload();
            }
        }, 1000);
    }

    

    window.onload = function () {
        upiCountdown("timeout", 5, 0);
        setInterval(checkPaymentStatus, 4000);
    };
</script>
	<script>upiCountdown("upitimer",005,00,'<?php echo $callback_url;?>?results=time out');</script>
</body>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
    <script src="../assets/dist/js/vendor.js"></script>
    <script src="../assets/dist/js/adminx.js"></script>
	<script src="../assets/dist/js/custom-new.js?<?=time()?>"></script>
	<script src="../assets/dist/js/qrcode.min.js"></script>	
</html>

      