<?php


include("tools/header.php");?>
<?php
$inst_ch = true;
$hs_err = false;
$hs_aut = false;
$tr_id = 'NONE';
$msgboxof = ' <div class="col-12 text-center text-danger mb-1"><div class="p-3"> <h5>تراکنش با مشکل روبرو گردید</h5></div></div>';
?>
<?php

if(isset($_SESSION['trco']) || isset($_SESSION['frco'])){
    
      $wwr_s = $_SESSION['trco'];
      $factorid = $_SESSION['frco'];
    
     if(empty(trim($wwr_s)) || empty(trim($factorid))){
         
         $inst_ch = false;
         
     }
    
}else{
    $inst_ch = false;
}
?>

<?php

 if(isset($_GET['stofrt'])){
    $hs_err = true;        
    $wwr_s_g = $_GET['stofrt'];
            
    if($wwr_s != $wwr_s_g){
                
       $inst_ch = false;
                
     }else{
           $_SESSION['trco'] = null;
           $_SESSION['frco'] = null;
           unset($_SESSION['trco']);
           unset($_SESSION['frco']);
     }
            
}

 


if(!$inst_ch){
    
  $_SESSION['trco'] = null;
  $_SESSION['frco'] = null;
  unset($_SESSION['trco']);
  unset($_SESSION['frco']);
    
  header("Location: logout.php"); 
     exit();
}


?>
 

<?php



if(isset($_GET['Authority'])){
    
if($hs_err){
  $_SESSION['trco'] = null;
  $_SESSION['frco'] = null;
  unset($_SESSION['trco']);
  unset($_SESSION['frco']);
    
  header("Location: logout.php"); 
    exit();
}    
    
    
$Authority = $_GET['Authority']; 
 
if(!empty(trim($Authority))){
    
$hs_aut = true;
    
    
$ftotal = $queries -> getFactorTotal("Exec calc_newsales_factor_total @cod_cast = '".$_SESSION['czi']."', @ucid = '".$_SESSION['ucid']."', @fid = ".$factorid);
$ftotal = intval($ftotal);
  
    
$data = array("merchant_id" => "493668a1-078d-4f3e-93ff-49061ba7e12e", "authority" => $Authority, "amount" => $ftotal);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData)
));

$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);


$tr_res = "FAILED";
$tr_code = "-125";
$tr_state = false;

if ($err) { 
    $tr_res = "FAILED ".$err;
} else {
    if ($result['data']['code'] == 100) { 
        
        $tr_id = $result['data']['ref_id'];
        $tr_res = "PAID";
        $tr_code = $result['data']['code'];
        $tr_state = true;
         
        $msgboxof = '<div class="col-12 text-center text-success mb-1 d-none">
                                    
                                    <div class="p-3">
                                        
                                        <h5>خرید شما با موفقیت ثبت گردید</h5>
                                        
                                    </div>
                                    
                                </div>
 
                                                                
                                <div class="col-12 text-center text-dark mb-3 d-none">
                                    
                                    <div class="p-3">
                                        
                                        <h6>شماره تراکنش</h6>
                                        <h6>'.$tr_id.'</h6>
                                        
                                    </div>
                                    
                                </div>
                                ';
        
        
    }else { 
        $tr_res = "FAILED ".  $result['errors']['message'];
        $tr_code = $result['errors']['code'];
		 
    }
}

if($tr_state){
    $queries -> updateFactor("EXEC dbo.updatefactorflag @ucid =  ".$_SESSION['ucid'].", @factorid = ".$factorid.", @sync_flag = 1");
	$queries -> deletefactortemp("EXEC deletetempfactorsall @p1= ".$_SESSION['uzi']." , @p2 = ".$_SESSION['pzi'].", @ucid = ".$_SESSION['ucid'].", @cid = ".$_SESSION['uzi']);
}else{
    $queries -> updateFactor("EXEC dbo.updatefactorflag @ucid =  ".$_SESSION['ucid'].", @factorid = ".$factorid.", @sync_flag = 404");
}
 $queries -> addTransactions($Authority, $_SESSION['trco'] , $tr_id, $tr_code, $factorid, $tr_res, $ftotal, $_SESSION['uzi'], $_SESSION['ucid']);   

    
}
 
}


$_SESSION['trco'] = null;
$_SESSION['frco'] = null;
unset($_SESSION['trco']);
unset($_SESSION['frco']);

if(!$hs_aut){
      
   header("Location: logout.php");   
     exit();
}

?>
 

<body>

    <div class="container-fluid bg-white">

        <div class="row">

            <?php include("tools/cartnav.php");?>
 
            <div class="col-12 mt-5 ">
                
                
                <div class="d-flex flex-row align-items-center justify-content-center">
                    
                    <div class="col-xl-9 col-lg-10 col-11">
                        
                        
                            
                            <div class="row border border-secondary rounded">
                                
                                <?php echo $msgboxof;?>   
                                                            

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

            

        </div>
    </div>



 <?php include("tools/notmodal.php");?>
 


        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="assets/js/jq.js"></script>
        <script src="assets/js/bootstrap.bundle.js"></script>
 

</body>

</html>
