<?php
session_start();
session_regenerate_id();
require_once('../system/function.php');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title><?php echo webdata('company_name');?> | Payments</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Questrial&amp;display=swap">
    <link rel="stylesheet" type="text/css" href="../assets/dist/css/adminx.css" media="screen" />
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"/>
    <link rel="icon" href="<?php echo webdata('web_fav');?>" type="image/*" />
	<!--script src="../assets/dist/js/historry.js"></script-->
	<script src="../assets/dist/js/sweetalert.min.js"></script>
<style>
html>body {
            font-family: Questrial, "sans-serif" !important;
            background: #24093b; /* Deep Purple background */
            min-height: 100vh;
            color: white; /* Text color to ensure visibility on dark background */
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
        }

        /* Wrapper for Flex Layout */
        .content-wrapper {
            display: flex;
            justify-content: space-between; /* Space between welcome section and sidebar */
            align-items: flex-start;
            width: 90%;
            max-width: 1200px;
            margin: 0 auto;
        }

        @media only screen and (max-width: 800px) {
    .welcome-section {
        width: 90%; /* Full width for smaller screens */
        padding: 20px; /* Reduced padding for smaller devices */
    }

    .welcome-section h1 {
        font-size: 2.5em; /* Smaller heading size */
    }

    .welcome-section p {
        font-size: 1em; /* Smaller paragraph text */
    }
}

 .valid-until {
            color: black;  /* Set "Valid until" text to black */
        }

        /* Animation for the welcome section */
        @keyframes welcomeAnimation {
            0% {
                transform: translateY(-10px);
                opacity: 0.7;
            }
            100% {
                transform: translateY(10px);
                opacity: 1;
            }
        }

        /* Sidebar Example */
        .example-sidebar {
            background: linear-gradient(135deg, #2d2b7f, #0e53b5);
            height: 100%;
            width: 30%;
            padding: 20px;
            border-radius: 20px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
            color: white;
        }

        .example-sidebar h2 {
            font-size: 2em;
            margin-bottom: 15px;
        }

        .example-sidebar p {
            font-size: 1.1em;
            line-height: 1.5;
        }

        /* Responsive Design */
        @media only screen and (max-width: 800px) {
            .content-wrapper {
                flex-direction: column; /* Stack the content vertically */
            }

            .welcome-section {
                width: 100%;
                margin-left: 0;
            }

            .example-sidebar {
                width: 100%;
                margin-top: 20px;
            }
        }

.text-center {
    color: red; /* Make the "Valid until" text red */
}

#upitimer {
    color: red; /* Make the text inside upitimer span red */
}
 </style>
</head>
<body>
    <div class="content-wrapper">
        <!-- Welcome Section -->
<div class="welcome-section">
    <h1>Welcome</h1>
    <p>Please remember to scan the QR card, after waiting 3 seconds payment will be successfully processed</p>
    <img src="https://t3.ftcdn.net/jpg/04/86/77/04/360_F_486770467_9nd0TjY0owEdwkoUCvi85VfIJQTvQFKi.jpg" alt="Payment Process" style="max-width: 300%; margin-top: 20px; border-radius: 10px;">
</div>
<body class="d-flex alifgn-items-center">
<div class="p-3 p-3 d-flex alifgn-items-center justify-content-center col-12">
<div class="container d-flex">	
<?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."'); 
var upilink = document.getElementById(\"qrcode\").title;
document.getElementById(\"upilink\").href = upilink;
}, 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();	
}
?>

<!--h4 class="text-white pb-3"><b><?php echo $result['company_name'];?></b></h4-->
 
<!--div class="col-md-12">
<div><a href="<?php echo $callback_url;?>" class="text-dark" style="text-decoration: none;"><i class="fa fa-reply"></i> Go Back</a></div>
<hr>
</div--> 
 <div class="text-center position-relative d-flex align-self-center mx-auto">
    <div class="card card-size border my-2 mx-auto" style="max-width: 350px; border-radius: 15px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);">
        <div class="card-body p-0">
            <div class="login-signin">
                <div class="mb-0" style="text-align: left; color: white; background: #424242; padding: 2.25rem 2.25rem 1rem 2.25rem; border-bottom: 1px solid #f2f4f9; border-top-left-radius: 15px; border-top-right-radius: 15px;">
                    <h3>Secure Payment</h3>
                    <p class="opacity-75 font-weight-bold" style="opacity:75%; font-size: 12px;">Scan to pay ₹<span class="rupee_symbol" style="margin: 0"><?php echo $txnAmount; ?></span></p>
                </div>

                <!-- Success message hidden initially -->
                <div class="d-none">
    <div class="d-flex flex-row fade show mb-5" role="alert" style="margin: 2rem 2rem 0 2rem; background-color: #4CAF50; color: white; border-radius: 10px; padding: 1rem;">
        <div class="alert-icon" style="font-size: 1.5rem; margin-right: 10px;">🚀</div>
        <div class="alert-text" style="font-size: 1.2rem; font-weight: bold;">Your transaction was successfully completed</div>
    </div>
    <button class="btn btn-xs btn-danger font-weight-bold" id="goback" style="margin-bottom: 2.25rem; background-color: #f44336; border-radius: 5px; padding: 10px 20px; color: white; font-size: 1rem; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2); transition: background-color 0.3s;">
        Go Back
    </button>
</div>

<div class="d-flex flex-column flex-center p-3" style="margin: 0 auto;">
    <div class="mt-2 mx-2 d-flex justify-content-center" id="qrcode">
        <img src="../assets/img/loading.cc387905.gif" alt="QR Code" style="max-width: 200px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); border: 3px solid #f44336;">
    </div>
</div>
<!--<a href="#" id="upilink" class="btn bg-primary text-white btn-sm mt-2 upilink" <?=$paybtn?> >Pay â‚¹<?php echo $txnAmount;?> using a UPI App</a><br>-->
<?php 
$paybtn = 'style="display:none;"'; 
if(isMobile()){
//$paybtn = 'style="display:inline-block;"';   
}
?>
<a href="#" id="upilink" class="btn bg-primary text-white btn-sm mt-2 upilink" <?=$paybtn?> >Pay ₹<?php echo $txnAmount;?> using a UPI App</a><br>
<span class="text-center mt-3">Valid until <span id="upitimer" class="font-weight-bold"></span></span> 
                           
                            </div>
                            </div>
                            </div>
</div>

    <!-- If you prefer jQuery these are the required scripts -->
    <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>	
	<script>upiCountdown("upitimer",005,00,'<?php echo $callback_url;?>?results=time out');</script>
	
  </body>
</html>