<?php 
if(isset($_POST['status']) && !empty($_POST['message']) && !empty($_POST['hash'])){
include('database.php'); 


if($_POST['status']=="SUCCESS"){
require_once('upiapi_config.php');
$paramList = hashDecrypt($_POST['hash'],upiapi_secret);
$verifySignature = RechPayChecksum::verifySignature($paramList, upiapi_secret, $_POST['checksum']);

if($verifySignature){
$response = json_decode($paramList,true); 
if($response['txnStatus']=="TXN_SUCCESS"){
$o = explode("A",$response['orderId']);    
$id = $o[1];   
    
$sdata = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM users WHERE id='$id' "));
if($sdata['id']>0 && $sdata['id']==$id){
$old_order = mysqli_num_rows(mysqli_query($conn,"SELECT * FROM bal_point_transfer_history WHERE order_id='".$response['orderId']."' ")); 
if($old_order==0){

$amount = $response['txnAmount'];

$sql = "INSERT INTO `bal_point_transfer_history`(`order_id`, `from_user_id`, `status`, `to_user_id`, `transferred_point`, `createdate`) VALUES ('".$response['orderId']."','1','Online Customer Add','".$sdata['id']."','".$amount."','".date("Y-m-d H:i:s")."')";

if(mysqli_query($conn,$sql)){

if(mysqli_query($conn,"UPDATE `users` SET balance= (balance + $amount) WHERE id='".$sdata['id']."' ")){
echo "<script>alert('Transaction Successfully and Balance Updated')</script>"; 
}else{
echo "<script>alert('Server Error Balance Not Updated')</script>";    
}   
    
}else{
echo "<script>alert('Server Error')</script>";    
}   
   
    
}else{
echo "<script>alert('Duplicate Request')</script>";    
}
 
    
}else{
echo "<script>alert('User Not Found')</script>";    
}


}else{
echo "<script>alert('".$_POST['message']."')</script>";    
}

    
}else{
echo "<script>alert('Checksum Error')</script>";    
}


}else{
echo "<script>alert('".$_POST['message']."')</script>";    
}

echo '<script>location.href=""</script>';
exit();
}

include('config.php');
checkSession();
?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Add Child Balance | <?php echo $webdata['webname']; ?></title>
      <script>if(window.history.replaceState){window.history.replaceState(null,null,window.location.href);}</script>
      <link rel="icon" type="image/png" sizes="16x16" href="assets/img/icon.png">
      <!-- Common Plugins -->
      <link href="assets/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">
      <!-- Sweet Alerts -->
      <link href="assets/lib/datatables/responsive.bootstrap.min.css" rel="stylesheet" type="text/css">
      <link href="assets/lib/toast/jquery.toast.min.css" rel="stylesheet">
	  <!-- DataTimePicker -->
      <!-- Custom Css-->
      <link href="assets/scss/style.css" rel="stylesheet">
      <![endif]-->
	  <style>
	   @media print {
            html,body {
                display: none;
            }
        }
		.watermark img { width: 100%; }
		.watermark { position: relative; }
		.watermark::after {
		  content: "AADHARUPDATE";
		  position: absolute;
		  bottom: 42%;
		  right:10%;
		  font-size: 2.4em;
		  color: #f8c200;
		  font-weight:700;
		  transform: rotate(-42deg);
		  letter-spacing: 5px;
		}
		</style>
   </head>
   <body>
   <?php 
   include('nav.php');
   ?>
      <!-- ============================================================== -->     
      <!-- ============================================================== -->
      <!-- 						Content Start	 						-->
      <!-- ============================================================== -->
<div class="row page-header">
<div class="col-lg-6 align-self-center mt-5">

</div>
</div>

<section class="main-content">
<div><h4>Add Aadhaar Token Balance</h4></div>    
<div class="row">
<div class="col-sm-6">
<div class="card p-5">
<?php
if(isset($_POST['payment']) && !empty($_POST['amount']) && is_numeric($_POST['amount']) && $_POST['amount']>0){
require_once('upiapi_config.php');
$order_id = date("ymdhis")."A".$sdata['id'];

$request = [
   "token" => upiapi_token,
   "upiuid" => upiapi_upiuid,
   "orderId" => $order_id, 
   "txnAmount" => $_POST['amount'], 
   "txnNote" => $sdata['username'], 
   "customerName" => $sdata['name'], 
   "customerEmail" => $sdata['email'], 
   "customerMobile" => $sdata['mobile'], 
   "callbackUrl" => order_callback_url2
]; 
 

$checkSum = false;
$paramList = array();

$token = $request['token'];
$upiuid = $request['upiuid'];
$orderId = $request['orderId'];
$txnAmount = $request['txnAmount'];
$txnNote = $request['txnNote'];
$cust_Mobile = $request['customerMobile'];
$cust_Email = $request['customerEmail'];
$callback_url = $request['callbackUrl'];


$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;

$checkSum = RechPayChecksum::generateSignature($paramList,upiapi_secret);
?>
<html>
<head>
<title>Gateway Check Out Page</title>
</head>
<body>
	<center><h1>Please do not refresh this page...</h1></center>
		<form method="post" action="<?php echo upiapi_order_url ?>" name="f1">
		<table border="1">
			<tbody>
			<?php
			foreach($paramList as $name => $value) {
				echo '<input type="hidden" name="' . $name .'" value="' . $value . '">';
			}
			?>
			<input type="hidden" name="checksum" value="<?php echo $checkSum ?>">
			</tbody>
		</table>
		<script type="text/javascript">
			document.f1.submit();
		</script>
	</form>
</body>
</html>
<?php
}
?>    
    
<form action="" method="post">
<h4>Txn Amount:</h4>
<input type="number" name="amount" class="form-control" placeholder="Enter Amount" required><br>
<input type="submit" name="payment" value="Payment" class="btn btn-primary">
</form>

</div>  
</div> 
</div>
</section>

      <!-- ============================================================== -->
      <!-- 						Content End		 						-->
      <!-- ============================================================== -->
      <!-- Common Plugins -->
      <script src="assets/lib/jquery/dist/jquery.min.js"></script>
	  <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
      <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
	  <script src="model/js/mfs100-9.0.2.6.js"></script>
	  <script type="text/javascript" src="model/js/capture.js"></script>
	  <script src="assets/lib/formatter/jquery.formatter.min.js"></script>
    
      <script src="assets/lib/bootstrap/js/popper.min.js"></script>
      <script src="assets/lib/bootstrap/js/bootstrap.min.js"></script>
      <script src="assets/lib/pace/pace.min.js"></script>
      <script src="assets/lib/jasny-bootstrap/js/jasny-bootstrap.min.js"></script>
      <script src="assets/lib/slimscroll/jquery.slimscroll.min.js"></script>
      <script src="assets/lib/nano-scroll/jquery.nanoscroller.min.js"></script>
      <script src="assets/lib/metisMenu/metisMenu.min.js"></script>
      <script src="assets/js/custom.js"></script>
      <!--Chart Script-->
      <script src="assets/lib/chartjs/chart.min.js"></script>
      <script src="assets/lib/chartjs/chartjs-sass.js"></script>
      <!--Vetor Map Script-->
      <script src="assets/lib/vectormap/jquery-jvectormap-2.0.2.min.js"></script>
      <script src="assets/lib/vectormap/jquery-jvectormap-us-aea-en.js"></script>
      <!-- Chart C3 -->
      <script src="assets/lib/chart-c3/d3.min.js"></script>
      <script src="assets/lib/chart-c3/c3.min.js"></script>
      <!-- Datatables-->
      <script src="assets/lib/datatables/jquery.dataTables.min.js"></script>
      <script src="assets/lib/datatables/dataTables.responsive.min.js"></script>
      <script src="assets/lib/toast/jquery.toast.min.js"></script>
      <script src="assets/js/dashboard.js"></script>
      <!--Sweet Alerts-->
      <script src="assets/lib/sweet-alerts2/sweetalert2.min.js"></script>
	  
   </body>
</html>