

<?php
    
    include'connection.php';
    
    date_default_timezone_set("asia/Kolkata");
	$datetime= Date("Y-m-d - h:i:s A");
	$time= Date("h:i:s A");
	$date= Date("Y-m-d");
    
    $id=$_GET['rchid'];
    $status=$_GET['Status'];
    $orderid=$_GET['rechargeno'];
    $txid=$_GET['operatorid'];
    $LapuBal=$_GET['lapubal'];
    $IdeaUserId=$_GET['lapuno'];
    
	$quGetRechData="select * from recharge where Id='$id'";
	$reGetRechData=mysqli_query($con,$quGetRechData);
	$dataGetRech=mysqli_fetch_array($reGetRechData);
	$userid=$dataGetRech['Userid'];
	$mobile=$dataGetRech['Mobile'];
	$amount=$dataGetRech['Amount'];
	$operator=$dataGetRech['Companyname'];
	$CheckStatus=$dataGetRech['Status'];
	$Partnerid=$dataGetRech['Partnerid'];
	
	$UpdateCBS="update recharge set CallBack='1' where Id='$id'";
	mysqli_query($con,$UpdateCBS);
	
		
	    $GetLapuOldBal="select * from recharge where LapuNo='$IdeaUserId' and status='SUCCESS' order by Id desc";
    	$reGetLapuOldBal=mysqli_query($con,$GetLapuOldBal);
	    $dataGetLapuOldBal=mysqli_fetch_array($reGetLapuOldBal);
    	$LapuOldBal=$dataGetLapuOldBal['ApiBal'];
	    $remaiBal=$LapuOldBal-$amount;
	
    if(empty($LapuBal) || $LapuBal==0)
    {
        $IdeaBal= $LapuOldBal-$amount;
    }
    else
    {
        $IdeaBal = $LapuBal;
    }
	
	$LapuDiff=$IdeaBal-$remaiBal;
	
	if($txid=="You can only select other than prime plan for recharge.")
	{
	    $msg=$txid;
	}
	elseif(
	    $txid=="Sorry! This recharge is not available any more"
	    || $txid=="Recharge plan not available" || $txid=="Transaction Failed" || $txid=="Card gorup set ID not found." || $txid=="Please try again after checking ETOP balance, Sorry! This recharge is not available any more. To continue using your mobile services, dial *121# and recharge with the best offers.	")
	{
	    $msg="Invalid Amount !";
	}
	elseif($txid=="Receivers mobile number is from un-supported network" || $txid=="Receiver's mobile number is from un-supported network." || $txid=="IN Bad MSISDN" || $txid=="Please try again after checking ETOP balance, Receivers mobile number is from un-supported network." || $txid=="Customer not found, please check number")
	{
	    $msg="Invalid Number !";
	}
	else
	{
	    $msg="Failed";
	}
	
	
    	$query8="select * from wallet where Userid='$userid' order by Id desc";
		$result8=mysqli_query($con,$query8);
		$data8=mysqli_fetch_array($result8);
		$currentbal= $data8['Balance'];
		
		$query2="select * from retailer where Userid='$userid'";
		$result2=mysqli_query($con,$query2);
		$databal=mysqli_fetch_array($result2);
		$username=$databal['Name'];
		$parentid=$databal['Parentid'];
		$grandparentid=$databal['Grandparentid'];
		$CallBackUrl=$databal['CallBackUrl'];
		$ApiActive=$databal['ApiActive'];
	    
	    if($ApiActive==1)
	    {
	        $ch = curl_init();
	        $url="$CallBackUrl?partnertid=$Partnerid&status=$status&orderid=$id&opid=$txid&errmsg=$txid";
    		curl_setopt($ch, CURLOPT_URL, "$CallBackUrl?partnertid=$Partnerid&status=$status&orderid=$id&opid=$txid&errmsg=$txid");
			
	    	curl_setopt($ch, CURLOPT_POST, false );
		    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    		curl_setopt($ch, CURLOPT_TIMEOUT, 0);
	    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
		    $result = curl_exec($ch);
		    curl_close($ch);
		    
		    $insertTest="insert into Test(Testdata) values('$url')";
		    mysqli_query($con,$insertTest);
	    }
	
	
	        $opcodequery="select * from operator where Operator='$operator'";
			$opcoderesult=mysqli_query($con,$opcodequery);
			$opcodedata=mysqli_fetch_array($opcoderesult);
			$opcode=$opcodedata['Value'];
			$columName=$opcodedata['Commision_Column'];
			
			$querygetcom="select * from commision where Userid='$userid'";
			$rsultgetcom=mysqli_query($con,$querygetcom);
			$data=mysqli_fetch_array($rsultgetcom);
			$usercommvalue=$data[$columName];
			
			$commision=$amount*$usercommvalue/100;
			$actual=$amount-$commision;
	
	if($status=="PENDING"){
	$pendingquery="update recharge set Status='$status', Transactionid='$orderid', Operatorid='$txid', API='My_API1' where Id='$id'";
	mysqli_query($con,$pendingquery);
	echo "Your Recharge Request Pending Now !";
	
	}
	elseif($status=="SUCCESS")
	{
		$sucessquery="update recharge set Status='$status', Transactionid='$orderid', Operatorid='$txid', API='My_API1', AdminOpId='S'  where Id='$id'";
		mysqli_query($con,$sucessquery);
	    	$quUpdatebal="update myapi set LapuBal='$IdeaBal' where LapuNumber='$IdeaUserId'";
	    	if(mysqli_query($con,$quUpdatebal))
	    	{
	    	    $UpdateRechApibal="update recharge set ApiBal='$IdeaBal' where Id='$id'";
	    		mysqli_query($con,$UpdateRechApibal);
	    	}
		$updateLapuNo="update recharge set LapuNo='$IdeaUserId', LapuOldBal='$LapuOldBal', Diff='$LapuDiff' where Id='$id'";
    	mysqli_query($con,$updateLapuNo);
	}
	else
	{
	    
		$quGetAutoFailedStatus="select * from Auto_Failed where Id='1'";
		$reGetAutoFailedStatus=mysqli_query($con,$quGetAutoFailedStatus);
		$dataGetAutoFailedStatus=mysqli_fetch_array($reGetAutoFailedStatus);
		$AutoFailedStatus=$dataGetAutoFailedStatus['Status'];
		
	    if($CheckStatus=="FAILED" || $CheckStatus=="Failed")
	    {
	        
	    }
	    elseif($txid=="Card gorup set ID not found." || $txid=="Transaction Failed" || $txid=="You can only select other than prime plan for recharge." || $txid=="Recharge plan not available" || $txid=="Receivers mobile number is from un-supported network" || $txid=="IN Bad MSISDN" || $txid=="Sorry! This recharge is not available any more" || $txid=="Customer not found, please check number")
	    {
	        //$UpdateRechApibal="update recharge set ReHit='1' where Id='$id'";
	    	//mysqli_query($con,$UpdateRechApibal);
	    	
	    	$refundquery="select * from wallet where Userid='$userid' order by Id desc";
    		$refundresult=mysqli_query($con,$refundquery);
    		$refunddata=mysqli_fetch_array($refundresult);
    		$refundoldbal=$refunddata['Balance'];
    		$refundnewbal=$refundoldbal+$actual;
		
    		$refundaddbalquery="insert into wallet(Userid,Openingbalance,Credit,Remarks,Balance,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount,Status)
    		values('$userid','$refundoldbal','$actual','Recharge_Refund','$refundnewbal','$date','$time','Recharge_Refund','$id','$operator','$mobile','$amount','$status')";
    		if(mysqli_query($con,$refundaddbalquery))
	    	{
	    		$getrefundewalletid="select * from wallet where Userid='$userid' order by Id desc";
	    		$resultrefundewalletid=mysqli_query($con,$getrefundewalletid);
	    		$datarefundewalletid=mysqli_fetch_array($resultrefundewalletid);
	    		$refundewalletid=$datarefundewalletid['Id'];
			
		    	$failedquery="update recharge set Creditewalletid='$refundewalletid', Status='FAILED', Transactionid='$orderid', Operatorid='$msg', API='My_API1'  where Id='$id'";
			
    			mysqli_query($con,$failedquery);
			
	    		$deletecomm="delete from commwallet where Rechargeid='$id'";
            	mysqli_query($con,$deletecomm);
        	
	    		$query9="update retailer set Balance='$refundnewbal' where Userid='$userid'";
		    	mysqli_query($con,$query9);
			    
			    echo "Your Recharge Request Has Been Failed Now !"; 
		    }
	    }
	    elseif($AutoFailedStatus=="Deactive")
	    {
	        $UpdateRechApibal="update recharge set ReHit='1', ApiBal='$IdeaBal', AdminOpId='$txid' where Id='$id'";
	    	mysqli_query($con,$UpdateRechApibal);
	    	
	    }
	    else
	    {
	    	$refundquery="select * from wallet where Userid='$userid' order by Id desc";
    		$refundresult=mysqli_query($con,$refundquery);
    		$refunddata=mysqli_fetch_array($refundresult);
    		$refundoldbal=$refunddata['Balance'];
    		$refundnewbal=$refundoldbal+$actual;
		
    		$refundaddbalquery="insert into wallet(Userid,Openingbalance,Credit,Remarks,Balance,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount,Status)
    		values('$userid','$refundoldbal','$actual','Recharge_Refund','$refundnewbal','$date','$time','Recharge_Refund','$id','$operator','$mobile','$amount','$status')";
    		if(mysqli_query($con,$refundaddbalquery))
	    	{
	    		$getrefundewalletid="select * from wallet where Userid='$userid' order by Id desc";
	    		$resultrefundewalletid=mysqli_query($con,$getrefundewalletid);
	    		$datarefundewalletid=mysqli_fetch_array($resultrefundewalletid);
	    		$refundewalletid=$datarefundewalletid['Id'];
			
		    	$failedquery="update recharge set Creditewalletid='$refundewalletid', Status='FAILED', Transactionid='$orderid', AdminOpId='F', Operatorid='$msg', API='My_API1'  where Id='$id'";
			
    			mysqli_query($con,$failedquery);
			
	    		$deletecomm="delete from commwallet where Rechargeid='$id'";
            	mysqli_query($con,$deletecomm);
        	
	    		$query9="update retailer set Balance='$refundnewbal' where Userid='$userid'";
		    	mysqli_query($con,$query9);
			    echo "Your Recharge Request Has Been Failed Now !"; 
		    }
	    }
	}
?>