<?php

if($_SESSION['alslaksdjpAydaq'] == "laskd23#*(%kj23(sSMq"){
            
    
         if(isset($_POST['updatecart'])){
            $updatecart_item = $_POST['cartitems'];
            $updatecart_count = $_POST['cartitemscount'];  
            
            if(!empty(trim($updatecart_item)) && !empty(trim($updatecart_count))){
                
                
                //call db for cart update
                
                
                 $queries -> addtempfactors($_SESSION['uzi'], $updatecart_item, $updatecart_count, '', '', $_SESSION['ucid']);
                 
                
 
                header("Location: cart.php");
            }else{
                
            }
            
        } 
    
        }else{
           
        }
           


if($_SESSION['alslaksdjpAydaq'] == "laskd23#*(%kj23(sSMq"){
            
    
         if(isset($_POST['submitcart'])){
            $sendcart_note = $_POST['cartnote']; 
            
            if(empty(trim($sendcart_note))){$sendcart_note='no yaddasht';}
             
             $sendcart_note = str_replace('ی','ي',$sendcart_note);
             $sendcart_note = iconv( "UTF-8", "Windows-1256",$sendcart_note);
             $factorid = $queries -> getfid("Exec get_nf @p1 = '".$_SESSION['uzi']."', @p2 ='".$_SESSION['pzi']."'");
             
                for($i=0;$i<sizeof($cartdataset);$i++){
    
                        $pro_cart_img = $cartdataset[$i]['img'];
                        $pro_cart_name = $cartdataset[$i]['name'];
                        $pro_cart_price = $cartdataset[$i]['price'];
                        $pro_cart_count = $cartdataset[$i]['count'];
                        $pro_cart_id = $cartdataset[$i]['id'];
                        $pro_cart_total = floatval($cartdataset[$i]['count']) * floatval($cartdataset[$i]['price']);
                    
                        
                    
                        $queries -> addfactors(sizeof($cartdataset), $_SESSION['czi'], '0', '0', $pro_cart_count, $_SESSION['uzi'], $pro_cart_price, $factorid, $pro_cart_id, $sendcart_note ,$_SESSION['uzi'],$_SESSION['pzi'],$_SESSION['ucid']);
                        
                
                
                   }
             
                  $ftotal = $queries -> getFactorTotal("Exec calc_temp_factor_total @cod_cast = '".$_SESSION['uzi']."', @ucid = '".$_SESSION['ucid']."'");
             
                  $_SESSION['trco'] =  rand();     
                  $_SESSION['frco'] =  $factorid;     
                      
                  $queries -> addTransactions('NONE', $_SESSION['trco'] , 'NONE', '-123', $factorid, 'NONE', intval($ftotal), $_SESSION['uzi'], $_SESSION['ucid']);
             
                  $queries -> updateFactor("EXEC dbo.updatefactorflag @ucid =  ".$_SESSION['ucid'].", @factorid = ".$factorid.", @sync_flag = 100");
                  ///////$queries -> deletefactortemp("EXE  C deletetempfactorsall @p1= ".$_SESSION['uzi']." , @p2 = ".$_SESSION['pzi'].", @ucid = ".$_SESSION['ucid'].", @cid = ".$_SESSION['uzi']);
   
  $gatewayrandom =  rand();            
$data = array("merchant_id" => "493668a1-078d-4f3e-93ff-49061ba7e12e",
    "amount" => intval($ftotal),
    "callback_url" => "https://www.lajeh.ir/gwr.php?gr=".$gatewayrandom,
    "description" => "سفارش آنلاین",
    "metadata" => [ "email" => "info@lajeh.ir","mobile"=>"09995554466"],
    );
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
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);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);


$tr_res = "FAILED";
$tr_code = "-124";
$tr_state = true;
             
if ($err) {
//   echo "cURL Error #:" . $err;
    $tr_res = "FAILED ".$err;
     
} else {
    if (empty($result['errors'])) {
        if ($result['data']['code'] == 100) {
            $tr_state = false;
			$queries -> updateFactor("EXEC dbo.updatefactorAuth @ucid =  ".$_SESSION['ucid'].", @factorid = ".$factorid.", @t_auth = ".$result['data']["authority"]);
			
			 $queries -> updateFactor("EXEC handle_gateways @p1 = ".$factorid.", @p2 = ".$_SESSION['trco'].", @p3 = ".$gatewayrandom.", @p4 = '".$_SESSION['uzi']."', @p5 = ".$result['data']["authority"].", @p6 = 12");
			
			
            header('Location: https://www.zarinpal.com/pg/StartPay/' . $result['data']["authority"]);
        }
    } else {
         
         //echo'Error Code: ' . $result['errors']['code'];
        // echo'message: ' .  $result['errors']['message'];
        
        $tr_res = "FAILED ".  $result['errors']['message'];
        $tr_code = $result['errors']['code'];

    }
}
if($tr_state)   {
 $queries -> addTransactions('NONE', $_SESSION['trco'] , 'NONE', $tr_code, $factorid, $tr_res, $ftotal, $_SESSION['uzi'], $_SESSION['ucid']);            
 header("Location: chsta.php?stofrt=".$_SESSION['trco']);    
}          

            
        } 
    
        }else{
           
        }
           


?>