<?php
ob_start();session_start();
if($_SESSION['lang']=='')
{
	$_SESSION['lang'] = "en";
}
include 'languages/lang.'.$_SESSION['lang'].'.php';
include 'languages/lang_common_'.$_SESSION['lang'].'.php';
include("include/db.inc.php");
include("include/bit_functions.inc.php");
include("include/functions.php");
$db1=new db;
$db1->open();
mysql_query("SET NAMES 'utf8'");
		 $currency_symbol_new=symbol_currency();
check_login();
$uid=$_SESSION['uid'];

$auth_rec = mysql_fetch_array(mysql_query("select auth_login,auth_key from admin where type='superadmin'"));
$auth_login_id = $auth_rec['auth_login'];
$auth_key = $auth_rec['auth_key'];

//foreach($_POST as $i=>$k){echo "$i=$k"."<br>";}die;
#CONFIGURATION
$x_login = $auth_login_id;  		//authorize.net login
$x_tran_key = $auth_key; 		//authorize.net transaction key
$x_delim_data="TRUE"; 			//Delimited response from the gateway
$x_delim_char=","; 				//Character that will be used to separate fields
$x_encap_char=""; 				//Character that will be used to encapsulate fields
$x_relay_response="FALSE";  	//relay response is desired. FALSE is desired
$x_type="AUTH_CAPTURE"; 		//Default tranaction type AUTH_CAPUTRE, AUTH_ONLY, PRIOR_AUTH_CAPTURE, CREDIT, CAPTURE_ONLY, VOID

//$x_test_request="TRUE"; 		//Make this a test transaction

#CUSTOMER INFORMATION

$x_method="CC";
$x_card_num=$_POST["x_card_num"];

$x_card_code = $_POST['x_card_code'];
$x_exp_date=$_POST["x_expiry_date"];
$x_amount=$_POST["x_amount"];

$x_company=$_POST["x_company"];
$x_name=$_POST["x_name"];
$x_first_name=$_POST["x_first_name"];
$x_last_name=$_POST["x_last_name"];
$x_cust_id=$_POST["x_cust_id"];

$x_email=$_POST["x_email"];

$x_order_id=$_POST["x_order_id"];
$x_invoice_num=$_POST["x_invoice_num"];
$x_description=$_POST["x_description"];
$x_address=$_POST["x_address"];
$x_city=$_POST["x_city"];
$x_state=$_POST["x_state"];
$x_zip=$_POST["x_zip"];
$x_country=$_POST["x_country"];
$x_phone=$_POST["x_phone"];
$x_ship_to_first_name=$_POST["x_ship_to_first_name"];
$x_ship_to_last_name=$_POST["x_ship_to_last_name"];
$x_ship_to_address=$_POST["x_ship_to_address"];
$x_ship_to_city=$_POST["x_ship_to_city"];
$x_ship_to_state=$_POST["x_ship_to_state"];
$x_ship_to_zip=$_POST["x_ship_to_zip"];
$x_ship_to_country=$_POST["x_ship_to_country"];
$x_ord_ref=$_POST["x_ord_ref"];

$fields="x_version=3.1&x_login=$x_login&x_delim_data=$x_delim_data&x_delim_char=$x_delim_char&x_encap_char=$x_encap_char&x_cust_id=$x_cust_id";
$fields.="&x_type=$x_type&x_test_request=$x_test_request&x_method=$x_method&x_amount=$x_amount";
$fields.="&x_name=$x_name&x_first_name=$x_first_name&x_last_name=$x_last_name&x_company=$x_company&x_email=$x_email&x_card_num=$x_card_num&x_card_code=$x_card_code&x_exp_date=$x_exp_date";
$fields.="&x_order_id=$x_order_id&x_invoice_num=$x_invoice_num&x_description=$x_description&x_address=$x_address&x_city=$x_city&x_state=$x_state&x_zip=$x_zip&x_country=$x_country&x_phone=$x_phone";
$fields.="&x_ship_to_first_name=$x_ship_to_first_name&x_ship_to_last_name=$x_ship_to_last_name&x_ship_to_address=$x_ship_to_address&x_ship_to_city=$x_ship_to_city&x_ship_to_state=$x_ship_to_state&x_ship_to_zip=$x_ship_to_zip&x_ship_to_country=$x_ship_to_country&x_ord_ref=$x_ord_ref";

if($x_tran_key!='')
{
 	$fields.="&x_tran_key=$x_tran_key";
}
//echo $fields;die;
#Start CURL session
$ch=curl_init("https://www.securepay.com/AuthSpayAdapter/process.aspx");
												//change https://secure.authorize.net/gateway/transact.dll   for live
                                                //to http://certification.authorize.net for test only
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields); 	//set the fields to post
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);		//make sure we get the response back
$buffer=curl_exec($ch);							//execute the post
curl_close($ch);								//close our session
//$details=explode($x_Delim_Char,$buffer); 		// create an array of the response values


$details=explode(",",$buffer);

//foreach($details as $i=>$k){echo "$i=$k"."<br>";}die;

//$details[0]="1";

if($details[0]==1)
{
      //echo "Transaction is approved (Payment Realized)!!";
      $orderid = $_POST["x_order_id"];
      $orderIdArr = explode("_",$orderid);
      for($i=0;$i<count($orderIdArr);$i++){

		  $sql_qry="UPDATE t123go_product_sold SET payment='1' WHERE id='".$orderIdArr[$i]."'";
		  $myrwd=mysql_query($sql_qry);
      }

 	  $user_add_sql = mysql_query("SELECT id FROM t123go_useradd WHERE uid='$uid'");
	  if(mysql_num_rows($user_add_sql)>0){
		  $qryup="UPDATE t123go_useradd SET city='$x_city',postcode='$x_zip',country='$x_country',phone_number='$x_phone' WHERE uid='$uid'";
		  mysql_query($qryup);
	  }else{
		  $qryup="INSERT INTO t123go_useradd SET uid='$uid' , city='$x_city',postcode='$x_zip',country='$x_country',phone_number='$x_phone'";
		  mysql_query($qryup);
	  }

      header("location:creditcard_product_payment.php?payment=success");
      exit();


} else if($details[0]==2) {

      //echo "Transaction is not approved (Declined)!!";
      $respreason=$details[3];

      header("location:creditcard_product_payment.php?payment=declined&respreason=$respreason");
      exit();


} else {

      header("location:creditcard_product_payment.php?payment=cancel");
      exit();

}

?>