<?php
    session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	
	date_default_timezone_set("asia/Kolkata");
	$date= Date("Y-m-d");
    $time= Date("h:i:s A");
    
	if(isset($_POST['submit']))
	{
	    $msg="Failed";
	    $rehit=$_POST['rehit'];
	    
		$queryPR="select * from recharge where Status='PENDING' and ReHit='$rehit' and Rechargedate='$date'";
		$resultPR=mysqli_query($con,$queryPR);
		while($data=mysqli_fetch_array($resultPR))
		{
	        $id=$data['Id'];
			$userid=$data['Userid'];
			$debitamount=$data['Debitamount'];
			$ewalletid=$data['Ewalletid'];
			$oper=$data['Companyname'];
			$mobile=$data['Mobile'];
			$amount=$data['Amount'];
			$Partnerid=$data['Partnerid'];
    	    $Status=$data['Status'];
	    
	
	$quGetEwalletId="select * from RetBal where Userid='$userid'";
    $reGetEwalletId=mysqli_query($con,$quGetEwalletId);
    $dataGetEwalletId=mysqli_fetch_array($reGetEwalletId);
    $EwalletId=$dataGetEwalletId['Walletid'];
    
	        if($Status=="FAILED")
	        {
	            echo "<h1 align='center'><font color='red'> &#x2714; Transaction Failed Already !</font></h1><br>";
	        }
			else
			{
    			$query2="select * from retailer where Userid='$userid'";
	    		$result2=mysqli_query($con,$query2);
		    	$databal=mysqli_fetch_array($result2);
			    $CallBackUrl=$databal['CallBackUrl'];
    			$ApiActive=$databal['ApiActive'];
	    	
		    	$query2="insert into wallet(Userid,Txn,Credit,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount,Status)
			    values('$userid','$debitamount','$debitamount','$date','$time','Recharge_Refund','$id','$oper','$mobile','$amount','FAILED')";
    			if(mysqli_query($con,$query2))
	    		{
		    	    $refundewalletid=mysqli_insert_id($con);
			    												            
		            $queryNewBal3="select * from wallet where Userid='$userid' and Id>=$EwalletId";
                	$resultNewBal3=mysqli_query($con,$queryNewBal3);
	                $newbal3=0;
	                while($dataNewBal3=mysqli_fetch_array($resultNewBal3))
	                {
	                    $Retbal3= $dataNewBal3['Txn'];
	                    $newbal3=$newbal3+$Retbal3;
            	    }
    	            $Openingbalance=$newbal3-$debitamount;
	                $queryW="update wallet set Openingbalance='$Openingbalance', Balance='$newbal3' where Id='$refundewalletid'";
		    		mysqli_query($con,$queryW);
				
			    	$query12="update retailer set Balance='$newbal3' where Userid='$userid'";
				    mysqli_query($con,$query12);
			
    				$query4="update recharge set Creditewalletid='$refundewalletid', Operatorid='$msg', AdminOpId='F', ReHit='0', Creditamount='$debitamount', Status='FAILED' where Id='$id' ";
	    			mysqli_query($con, $query4);
			
		    		$query5="delete from commwallet where Rechargeid='$id'";
			    	mysqli_query($con,$query5);
				
				    if($ApiActive==1)
    				{
	    				$url = "$CallBackUrl?partnertid=$Partnerid&status=$status&orderid=$id&opid=$msg&errmsg=$msg";
                        $curl = curl_init();
            
                        curl_setopt_array($curl, array(
                        CURLOPT_URL => $url,
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_ENCODING => "",
                        CURLOPT_CONNECTTIMEOUT => 60,
                        CURLOPT_TIMEOUT => 60,
                        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                        CURLOPT_CUSTOMREQUEST => "GET",
                        CURLOPT_HTTPHEADER => array(
                        "cache-control: no-cache"
                        ),
                        ));

                        $response = curl_exec($curl);
                        $hhtpCode =curl_getinfo($curl, CURLINFO_HTTP_CODE);
                        $err = curl_error($curl);

                        curl_close($curl);
                
                        if ($err) {
                            //$do=mysql_query("INSERT INTO kwk_cbhis (oid,uid,status_code,msg,url)VALUES(paste your values)");
                        } else {
                            //$do=mysql_query("INSERT INTO kwk_cbhis (oid,uid,status_code,msg,url)VALUES(paste your values)");
                    }
                    $insertCBH="INSERT INTO cbhis (oid,uid,status_code,msg,url)VALUES('$id','$userid','$hhtpCode','$response','$url')";
                    mysqli_query($con,$insertCBH);
		    		}
				
			    }
			}
		}
		
		echo "<center><h3>All Transaction Failed Successfully !</h3></center>";
	}

?>
<html>
    <head>
       <style>
        #msg{width:200px; height:40px; font-size:22px; margin:10px;}
       </style>
    </head>
    <body>
        <form method="post">
        <select name="rehit" id="msg">
            <option value="1">FAILDED</option>
            <option value="0">PENDINGED</option>
        </select>
        <input type="submit" name="submit" id="msg">
        </form>
    </body>
</html>