<?php
//ob_start();
//error_reporting(E_ALL);
//ini_set("display_errors", 0);
session_start();
define("INSITE", true);
require_once('../core/class.db.php');
require_once('../core/class.users.fun.php');

 if($_GET['do']  == "succeededPaid"){
     $order_id= $_GET['order_id'];
     $return_url=urldecode($_GET['return_url']);
     $amount=$_GET['amount'];
     $currency_code=$_GET['currency_code'];
     
     $status=1;
     header('Location:'.$return_url.'?order_id='.$order_id.'&amount='.$amount.'&currency_code='.$currency_code.'&status=1');
  exit;

}
 if($_GET['do']  == "failurePaid"){
         $order_id= $_GET['order_id'];
     $return_url=urldecode($_GET['return_url']);
     $amount=$_GET['amount'];
     $currency_code=$_GET['currency_code'];
     $status=0;
     header('Location:'.$return_url.'?order_id='.$order_id.'&amount='.$amount.'&currency_code='.$currency_code.'&status=0');
  exit; 

}
if($_POST['do'] or $_GET['getaction']){

$do = $_POST['do'];

if($do == "payCheckout"){
$db = new Db();
$tokenid=(int)$_SESSION['tokenid'];
$order_id=$_SESSION['order_id'];
$amount=$_SESSION['amount'];
$account_id=$_SESSION['account_id'];
$currency_id=$_SESSION['currency_code'];
$return_url=$_SESSION['return_url'];
$user_id=$_SESSION['usid'];
$invoice_carrancy=$_POST['invoice_carrancy'];
if($invoice_carrancy==''){
$_SESSION['note_msg_arr']=array(

         "type" => 0,
         "msg" => 'Select a payment account  '


       );


  header('Location:homeCheckout.php');
  exit;  
}else{
    $currency= $db -> select("SELECT * FROM `system_currencies` where 	symbol='$currency_id' ");
    $currency_id=(int) $currency[0]['id'];
    $account= $db -> select("SELECT * FROM `users_accounts` where 	id='$invoice_carrancy' ");
    $currency_account=$account[0]['currency_id'];
    $balance_account=$account[0]['balance'];
    $userFun = new UsersFunctions();
    if((int)$currency_id == (int)$currency_account){
       if($balance_account>=$amount){
          $result = $userFun -> checkout_transfare($account_id,$tokenid,$invoice_carrancy,$amount);
           $status=1;
         header('Location:'.$return_url.'?order_id='.$order_id.'&amount='.$amount.'&currency_code='.$currency_id.'&status=1');
          exit;
          
       }else{
         $_SESSION['note_msg_arr']=array(

         "type" => 0,
         "msg" => 'There is insufficient account balance, choose another account'
       );
  header('Location: homeCheckout.php');
  exit;  
       } 
    }else{
     
 $ex_rows = $db -> select("SELECT * FROM `currencies_ex` WHERE `from_c` = $currency_account AND `to_c` = $currency_id");

     $rate = (float) $ex_rows[0]['rate'];

     $final_amount = $amount*$rate;   
    //echo $final_amount; 
     if($balance_account>=$final_amount){
          $result = $userFun -> checkout_transfare($account_id,$tokenid,$invoice_carrancy,$final_amount);
           $status=1;
         header('Location:'.$return_url.'?order_id='.$order_id.'&amount='.$amount.'&currency_code='.$currency_id.'&status=1');
          exit;
          
       }else{
         $_SESSION['note_msg_arr']=array(

         "type" => 0,
         "msg" => 'There is insufficient account balance, choose another account'
       );
  header('Location: homeCheckout.php');
  exit; 
       }
        
    }

}
}

if($do == "loginChecout" and $xWAF->verifyCSRF($_POST['csrf'])){
if(isset($_SESSION['error_login'])){

$captcha=$_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$secretkey = "6LfjXaEUAAAAAFXKdV4uZ0arBTgF0qlo04tMYfQT";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);

if(intval($responseKeys["success"]) !== 1) {


       $_SESSION['note_msg_arr']=array(

         "type" => 0,
         "msg" => 'قم بالتاكد من التحقق البشري'


       );


  header('Location: ../login.php?capatsha-error&email='.$_POST['email']);
  exit;

} else {
  //  echo '<h2>Success!</h2>';
}

}

    $userFun = new UsersFunctions();
  $email=$_POST['email'];
  $password=$_POST['password'];


  $email_fommobile = $userFun -> getEmailByMobile($email);

  if($email_fommobile){


    $email = $email_fommobile;

  }

 $order_id= $_POST['order_id'];
     $return_url=urldecode($_POST['return_url']);
     $amount=$_POST['amount'];
     $currency_code=$_POST['currency_code'];
     $account_id=$_POST['account_id'];
     $tokenid=$_POST['tokenid'];
  $result = $userFun -> loginCheck($email,$password,$order_id,$amount,$currency_code,$account_id,$tokenid,$return_url);

  header('Location: homeCheckout.php');
  exit;  
    
    
}
}

?>