<?php 
session_start();
require('../../db.php'); 
include('../../inc/func.php');
include('../../inc/logo.php');
?> 
<?php include('../../inc/header2.php');?>
        <!-- ============================================================== -->
        <!-- end navbar -->
        <!-- ============================================================== -->
   
        <!-- ============================================================== -->
        <!-- left sidebar -->
        <!-- ============================================================== -->
        <?php include('../../inc/nav3.php'); ?>
        <!-- ============================================================== -->
        <!-- end left sidebar -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- wrapper  -->
        <!-- ============================================================== -->
        <div class="dashboard-wrapper">
            <div class="dashboard-ecommerce">
                <div class="container-fluid dashboard-content ">
                    <!-- ============================================================== -->
                   
                    <!-- pageheader  -->
                    <!-- ============================================================== -->
                    
                    <!-- ============================================================== -->
                    <!-- end pageheader  -->
                    <!-- ============================================================== -->
                    <div class="ecommerce-widget">

                        <div class="row"></div>
                        <div class="row">
                            <!-- ============================================================== -->
                      
                            <!-- ============================================================== -->

                                          <!-- recent orders  -->
                            <!-- ============================================================== -->
                            <div class="col-xl-9 col-lg-12 col-md-6 col-sm-12 col-12">
                            
                                <div class="card">
                                          
                                    <h5 class="card-header"></h5>
                                    <div class="card-body">
                                        
                                        
                                        
                                    <?php
							
							if(isset($_GET['ref'])){
								       echo'<div class="alert alert-success">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>Transaction successful</strong>  
									</div>'; 
									
									echo '<script>
									setTimeout(function(){ window.location.href="../../dashboard.php" }, 3000);</script>';
									      
								        }
								        
										$username = $_SESSION['user'];
										$amount = $_SESSION['amt'];
										$network = $_SESSION['carier'];
										$phone = $_SESSION['phone'];
										$requestId = $_SESSION['transid'];
										
									$variation = $_SESSION['variation_code'];
										
										$plan = $_SESSION['plan'];
									
									$customer = $_SESSION['customer'];
									
									$iuc = $_SESSION['iuc'];
									$buttonname = "Pay With Wallet";
									
$variation = strtolower($network);
$plan = strtoupper($network);
$pnt = "$plan Account Funding";
									

								$tranDetail = "Please Confirm your Transaction Details:";
								
								$stat = "Pending";
										// replace comma
					$xamount = str_replace( array( '+', ',', '-', '/', '(', ')',   '_',   '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);
			
						
						
						$trans = mysqli_query($conn,"SELECT * FROM transactions WHERE ref='$requestId' ");					
					$rowTrans = mysqli_fetch_array($trans);	
					
				$img = "bet.png";


		


if($data['level'] === 'paid'){
$charge = $bill['bet'];
		

}elseif($data['level'] === 'free'){
$charge = $regbil['bet'];

}else{
$charge = 100; 
}



if($network == "Bet9ja-Agent"){
$bet = 0; //100;
}else{
$bet = 0; }



             $per = 0;

			$debit = $xamount + $charge + $bet;
            $chargeAmt = $debit;
			$payable = $chargeAmt;
			$balanceA = $bal - $debit;
			$balA = $bal - $debit;

	
	

						
	
			
		 
				
			if(isset($_POST['pay'])){
				
$xamount = str_replace( array( '+', ',', '-', '/', '(', ')',   '_',  '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);
$per = str_replace( array( '+', ',', '-', '/', '(', ')',   '_',  '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $per);
$charge = str_replace( array( '+', ',', '-', '/', '(', ')',   '_',  '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $charge);			
				
			//$payko = ($affpay/100)*$charge;
				
			$comi = ($per/100);
			$debit = $xamount + $charge + $bet;
            $chargeAmt = $debit;
			$payable = $chargeAmt;
		

		
									

											
		$ret = mysqli_query($conn,"SELECT * FROM transactions WHERE ref='$requestId' ");
		$da = mysqli_fetch_array($ret);
		$tref = $da['ref'];


$ins = mysqli_query($conn,"SELECT * FROM users WHERE email='$user' ");
$data = mysqli_fetch_array($ins);
$bal = $data['bal'];
$userRef = $data['action'];


if($userRef === $tref){
		
if($da['refer'] === $da['token']){
			
	if($debit <= $bal){		
			
   if($debit >= '100'){
	   
$updateuser = mysqli_query($conn,"UPDATE users SET bal=bal-$debit WHERE email='$user' ") or die(mysqli_error());
$updatetrans = mysqli_query($conn,"UPDATE transactions SET status='Processing' WHERE ref='$requestId' ") or die(mysqli_error());


$qryApi = mysqli_query($conn,"SELECT * FROM api_setting");
$apidata = mysqli_fetch_array($qryApi);

$apikey = $apidata['APIkey']; 
$url = $apidata['baseurl'];



$host = "https://vtuafrica.com.ng/portal/api/betpay/?apikey=$apikey&service=$network&userid=$iuc&phone=$phone&amount=$xamount&ref=$requestId";




//Initialize cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$dataR = curl_exec($ch);

//Close the cURL handle.
curl_close($ch);

//echo $dataR;


$result = json_decode($dataR);

$APImessage = $result->description->message;
$stat = $result->description->Status;
$APImessage = str_replace( array( '+', '-', '/', '(', ')',   '_', "'",  '{',   '}',   '<',  '>',  '=', '   '), '', $APImessage);

if($result->code =='101'){	
    
$balanceA = $bal - $debit;
$balA = $bal - $debit;


$qsel = "UPDATE transactions SET status='$stat',debit='$debit',balanceA='$balA',token='0',refer='N/A',channel='Wallet',message='$APImessage' WHERE ref='$tref' ";	
$sav = $conn->query($qsel);


echo'<div class="alert alert-success">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>Transaction Successful.</strong>  
									</div>'; 
									
//include('../affiliatepay.php');

}else {

$balanceA = $bal;


$bilacc = "UPDATE users SET bal=bal+$debit WHERE email='$user'";
$doDebit = $conn->query($bilacc);

$qrfel = "UPDATE transactions SET status='$stat' WHERE ref='$requestId'";
$Qrevs = $conn->query($qrfel);

echo'<div class="alert alert-danger">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>Error Occur: </strong><br> '.$APImessage.' <br>Contact '.$sitephone.' </div>';
									 

}		


   	} else{
$balanceA = $bal;
$stat = "Success";
$qrfel = "UPDATE transactions SET status='Fraud' WHERE ref='$requestId'";
  			$Qrevs = $conn->query($qrfel);
			
echo'<div class="alert alert-success">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>Invalid Amount Entered.</strong>  
									</div>';  }

		
		
}else{

$balanceA = $bal;
$stat = "Insufficient Balance";


echo'<div class="alert alert-warning">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>Insufficient Balance</strong>,  <a href="../buy_credit.php">Click here to credit wallet </a> 
									</div>';  } 

									
}else{

$balanceA = $bal - $debit;
$stat = "Completed";
$tranDetail = "Your Transaction Details are as Follows:";

echo'<div class="alert alert-danger">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>You Have Already Sent This Request.</strong>  
									</div>';  }
		
			

	}else{
$updateuser = mysqli_query($conn,"UPDATE users SET blockstat='Blocked' WHERE email='$user' ") or die(mysqli_error());

session_destroy();
unset($_SESSION["user"]);
echo '<script> setTimeout(function(){ window.location.href="../../index.php" }, 10);</script>';
exit;		

  }

									
                                        
								
                                }	
                                
                       


?>

<?php $tranDetail = "Please Confirm your Transaction Details:";?>

<form method="post" action="" >
                                            
    <h2 class='margin-tp-10'><?php echo $tranDetail; ?> </h2>                                            
                    
                                      
                                        
             <table class="table  margin-tp-10" id="transTable">
                      <td width="30%">Service</td>
                            <td id="mainService"><?php echo $pnt;?>  </td>
                        </tr>
                        
                         <!--<tr>
                            <td width="30%">Product</td>
                            <td id="mainService"><?php echo $network;?>  </td>
                        </tr>-->
                               
                         
						
						<tr>
                            <td width="30%">Customer/User ID:</td>
                            <td><?php echo $_SESSION['iuc'];?></td>
                        </tr>

						<tr>
                            <td width="30%">Customer Name:</td>
                            <td><?php echo $customer;?></td>
                        </tr>                   
                                                            <tr>
                        <td width="30%">Amount To Fund:</td>
                        <td>₦<?php echo $_SESSION['amt'];?>.00 </td>
                    </tr>
                     
                      <td width="30%">Transaction Charge:</td>  
                        <td>₦<?php echo $charge;?> </td></tr>                     
                    <tr>
                        <td width="30%">Total Payable:</td>
                        <td>₦<d id="total_amount"><?php echo $payable;?></d></td>
                    </tr>  

                   <tr>
                        <td width="30%"><stro>Current Balance: </h4></td>
                        <td id="transactionId">₦<?php echo $bal;?></td>
                    </tr>                    
                           
                     <tr>
                        <td width="30%"><stro>Balance After: </h4></td>
                        <td id="transactionId">₦<?php echo $balanceA;?></td>
                    </tr> 
					
                    <tr>
                        <td width="30%"><stro>Transaction ID</h4></td>
                        <td id="transactionId"><?php echo $_SESSION['transid'];?></td>
                    </tr>                    
                    <tr>
                        <td width="30%">Status</td>
                        <td><?php echo $stat;?></td>
                    </tr>       
                    
                            
                                         
                       
                                                                                                                                                                                                                                
                                </div>
                              							               </td>
                        </tr>
                                    </table>              
                                         
                                        
                                     <div class="col-sm-6 pl-0">
                                         
                                                <p class="text-center">
 <button type="submit" name="pay" class="btn btn-rounded btn-primary"><i class="fas fa-fw fa-money-bill-alt"></i> Pay With Wallet</button>
                     
                           </form>
                    
                                                
                       
                                                </p>
                                                
                                                <p align="center">
                                                    
                                                
                                                     </p>
                                            
                                       
							
                                        
                                        </div> 

                                        

                                        
                                    </div>
                                </div>
                            </div>
                            <!-- ============================================================== -->
                            <!-- end recent orders  -->
 
                            <!-- ============================================================== -->

                            <!-- ============================================================== -->
                            <!-- customer acquistion  -->
                            <!-- ============================================================== -->
                            <div class="col-xl-3 col-lg-6 col-md-6 col-sm-12 col-12">
                                <div class="card">
                                    <h5 class="card-header">Account Status</h5>
                                    <div class="card-body">
                                   <div class="d-inline-block">
                                        <h5 class="text-muted">Current Balance</h5>
                                        <h2 class="mb-0"> &#x20A6;<?php echo number_format($bal,2,'.',',');?></h2>
                                    </div>
                                    <div class="float-right icon-circle-medium  icon-box-lg  bg-brand-light mt-1">
                                        <i class="fa fa-money-bill-alt fa-fw fa-sm text-brand"></i>
                                    </div>
                                   
                                    </div>
                                </div>
                                
                                <div class="card">
                                <div class="card-body">
                                    <div class="d-inline-block">
                                        <h5 class="text-muted">Affiliate:</h5>
                                       
                                        
                                    </div>
                                    <div class="float-left"><p class="text-muted"><strong>Total Referred:</strong> <?php echo $data['refcount']; ?> <br> 
                                    
                                   <strong> Earning:</strong> &#x20A6;<?php echo number_format($data['refwallet'],2,'.',','); ?>
                                    </p> </div>
                                    
                                    
                                    <div class="float-right icon-circle-medium  icon-box-lg  bg-primary-light mt-1">
                                        
                                        <i class="fa fa-users fa-fw fa-sm text-primary"></i>
                                    </div>
                                </div>
                            </div>
                            </div>
                            <!-- ============================================================== -->
                            
                            
                            
                            <!-- end customer acquistion  -->
                      <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                        <div class="card">
                            <h5 class="card-header">Recent Transactions</h5>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered first">
                                        <thead>
                                            <tr>
                                                <th>Transaction ID</th>
                                                
                                                <th>Amount</th>
                                                <th>Status</th>
                                                <th>Date</th>
                                               
                                            </tr>
                                        </thead>
                                        <tbody>
                                         <?php 
							$sql_trans = "SELECT * FROM transactions WHERE email='$user' ORDER BY `serial` DESC LIMIT 3 ";
									
$Show_trans = $conn->query($sql_trans);
										
										while($trow = $Show_trans->fetch_assoc())
											
											{
										
											?>
                                            <tr>
                                           
                                                <td><?php echo $trow['ref']; ?></td>
                                                
                                                <td><?php echo '&#x20A6;'.$trow['amount'].' ';?></td>
                                                 <td><?php echo $trow['status'];?></td>
                                                <td><?php echo $trow['date'];?></td>
                                                
                                            </tr>
                                           
                                           <?php } ?>
                                          
                                           
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Transaction ID</th>
                                                
                                                <th>Amount</th>
                                                <th>Status</th>
                                                <th>Date</th>
                                                
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>      
                          
                            <!-- ============================================================== -->
                        </div>
                        <div class="row">
                            <!-- ============================================================== -->
              				                        <!-- product category  -->
                            <!-- ============================================================== -->
                            <!-- ============================================================== -->
                            <!-- end product category  -->
                          

                                   <!-- product sales  -->
                            <!-- ============================================================== -->
                            <!-- ============================================================== -->
                            <!-- end product sales  -->
                            <!-- ============================================================== -->
                        </div>

                        <div class="row">
                            <!-- ============================================================== -->
                            <!-- sales  -->
                            <!-- ============================================================== -->
                            <!-- ============================================================== -->
                            <!-- end sales  -->
                            <!-- ============================================================== -->
                            <!-- ============================================================== -->
                         
                        </div>
                        <div class="row">
                            <!-- ============================================================== -->
                            <!-- total revenue  -->
                            <!-- ============================================================== -->
  
                        
                        </div>
                        <div class="row">
                          <!-- ============================================================== -->
                            <!-- end sales traffice source  -->
                            <!-- ============================================================== -->
                          
                        </div>
                    </div>
                </div>
            </div>
            <!-- ============================================================== -->
            <!-- footer -->
            <!-- ============================================================== -->
           <?php include('../../inc/footer1.php');?>
		   
            </div>
        <!-- ============================================================== -->
        <!-- end wrapper  -->
        <!-- ============================================================== -->
    </div>
    <!-- ============================================================== -->
    <!-- end main wrapper  -->
    <!-- ============================================================== -->
    <!-- Optional JavaScript -->
    <!-- jquery 3.3.1 -->
    <script src="../assets/vendor/jquery/jquery-3.3.1.min.js"></script>
    <!-- bootstap bundle js -->
    <script src="../assets/vendor/bootstrap/js/bootstrap.bundle.js"></script>
    <!-- slimscroll js -->
    <script src="../assets/vendor/slimscroll/jquery.slimscroll.js"></script>
    <!-- main js -->
    <script src="../assets/libs/js/main-js.js"></script>
    <!-- chart chartist js -->
    <script src="../assets/vendor/charts/chartist-bundle/chartist.min.js"></script>
    <!-- sparkline js -->
    <script src="../assets/vendor/charts/sparkline/jquery.sparkline.js"></script>
    <!-- morris js -->
    <script src="../assets/vendor/charts/morris-bundle/raphael.min.js"></script>
    <script src="../assets/vendor/charts/morris-bundle/morris.js"></script>
    <!-- chart c3 js -->
    <script src="../assets/vendor/charts/c3charts/c3.min.js"></script>
    <script src="../assets/vendor/charts/c3charts/d3-5.4.0.min.js"></script>
    <script src="../assets/vendor/charts/c3charts/C3chartjs.js"></script>
    <script src="../assets/libs/js/dashboard-ecommerce.js"></script>
</body>
 
</html>