<?PHP
if(phpversion() <= "4.0.6") { $_POST=$HTTP_POST_VARS; }
$sid=$_POST['sid'];

$cksid=mysql_fetch_row(mysql_query("select count(*) from session_history where sid='".addslashes(trim($sid))."'"));

if ($cksid[0]<=0) 
	{	
	# added 6/2/2004
	raw_post_details($email_admin, "ERROR[10] SID was not in the database!", "unknown", "Authorize.net Public Order System");

	header("Location: ".$http_web."/step_one.php");	
	exit; 
	}
else if (!isset($sid)) 
	{ 
	# added 6/2/2004
	raw_post_details($email_admin, "ERROR[18] Sid was not set!", "unknown", "Authorize.net Public Order System");

	header("Location: ".$http_web."/step_one.php"); 
	exit; 
	}

if (isset($_POST['x_Submit']))
	{
	if ($_POST['ccid']!=0)
		{
		$query="select ";
		$query.="customer_first_name, ";	// 0
		$query.="customer_last_name, ";		// 1
		$query.="customer_address, ";		// 2
		$query.="customer_city, ";			// 3
		$query.="customer_state, ";			// 4
		$query.="customer_zip, ";			// 5
		$query.="customer_country, ";		// 6
		$query.="customer_phone, ";			// 7
		$query.="customer_fax, ";			// 8
		$query.="customer_email, ";			// 9
		$query.="card_info ";				// 10
		$query.="from ";
		$query.="authnet_master_cc ";
		$query.="where ccid='".addslashes(trim($ccid))."' ";
		$query.="group by customer_first_name";

		$rs=mysql_fetch_row(mysql_query($query));

		$_POST['x_First_Name']=stripslashes(trim($rs[0]));
		$_POST['x_Last_Name']=stripslashes(trim($rs[1]));
		$_POST['x_Address']=stripslashes(trim($rs[2]));
		$_POST['x_City']=stripslashes(trim($rs[3]));
		$_POST['x_state']=stripslashes(trim($rs[4]));
		$_POST['x_Zip']=stripslashes(trim($rs[5]));
		$_POST['x_Country']=stripslashes(trim($rs[6]));
		$_POST['x_Phone']=stripslashes(trim($rs[7]));
		$_POST['x_Fax']=stripslashes(trim($rs[8]));
		$_POST['x_Email']=stripslashes(trim($rs[9]));
		$card_info=stripslashes(trim($rs[10]));
		
		$xcard_info=d("576cb7f68040520768bf51c75f7f4c84", $card_info);
		$var=explode("|", $xcard_info);
		$_POST['x_Card_Num']=$var[0];
		$_POST['x_Exp_Date_m']=substr($var[1], 0, 2);
		$_POST['x_Exp_Date_y']=substr($var[1], -2);
		}
	else
		{ 
		if (strlen(trim($_POST['x_First_Name']))==0) { $e0=true; }
		if (strlen(trim($_POST['x_Last_Name']))==0) { $e1=true; }
		if (strlen(trim($_POST['x_Address']))==0) { $e2=true; }
		if (strlen(trim($_POST['x_City']))==0) { $e3=true; }
		if (strlen(trim($_POST['x_Zip']))==0) { $e4=true; }
		if (strlen(trim($_POST['x_Phone']))<10) { $e5=true; }
		if (strlen(trim($_POST['x_Card_Num']))==0) { $e6=true; }
		if (strlen(trim($_POST['x_Exp_Date_m']))==0||strlen(trim($_POST['x_Exp_Date_y']))==0) { $e7=true; }
		if (strlen(trim($_POST['x_Card_Code']))==0) { $e8=true; }
		if (strlen(trim($_POST['x_Card_Code']))==6) { $e9=true; }
		}
	
	if (!isset($e0)&&!isset($e1)&&!isset($e2)&&!isset($e3)&&!isset($e4)&&!isset($e5)&&!isset($e6)&&!isset($e7)&&!isset($e8)&&!isset($e9))
		{
		$query0="select ";
		$query0.="x_gateway, ";			// 0
		$query0.="x_Login, ";			// 1
		$query0.="x_Password, ";		// 2
		$query0.="x_Currency_Code, ";	// 3
		$query0.="demo, ";				// 4
		$query0.="x_tran_key ";			// 5
		$query0.="from ";
		$query0.="payment_process ";
		$query0.="where pid='8'";

		$rs0=mysql_fetch_row(mysql_query($query0));

		$authnet_gateway=stripslashes(trim($rs0[0]));
		$x_Login=d("576cb7f68040520768bf51c75f7f4c84", $rs0[1]);
		$x_Password=d("576cb7f68040520768bf51c75f7f4c84", $rs0[2]);
		$x_Currency_Code=stripslashes(trim($rs0[3]));
		$demo=stripslashes(trim($rs0[4]));
		$x_tran_key=stripslashes(trim($rs0[5]));

		# ---------------------------------------------------------

		$query1="select ";
		$query1.="total_due_today, ";	// 0
		$query1.="pid, ";				// 1
		$query1.="domain_name ";		// 2
		$query1.="from session_history ";
		$query1.="where sid='".$_POST['sid']."'";

		$rs1=mysql_fetch_row(mysql_query($query1));

		$total_due_today=stripslashes(trim($rs1[0]));
		$pid=stripslashes(trim($rs1[1]));
		$domain_name=stripslashes(trim($rs1[2]));
		
		# ---------------------------------------------------------

		$query2="select ";
		$query2.="package_name ";	// 0
		$query2.="from plan_specs ";
		$query2.="where pid='".addslashes(trim($pid))."'";

		$rs2=mysql_fetch_row(mysql_query($query2));

		$package_name=stripslashes(trim($rs2[0]));

		# ---------------------------------------------------------

		if ($authnet_gateway=="authorize") { $processor="https://secure.authorize.net/gateway/transact.dll"; }
		else if ($authnet_gateway=="planetpayment") { $processor="https://secure.planetpayment.com/gateway/transact.dll"; }
		else if ($authnet_gateway=="ecx") { $processor="https://secure.quickcommerce.net/gateway/transact.dll"; }
		else if ($authnet_gateway=="epn") { $processor="https://www.eprocessingnetwork.com/cgi-bin/an/order.pl"; /*$parse_return=true;*/ }
		else if ($authnet_gateway=="rtware") { $processor="https://www.rtware.com/cgi-bin/an/order.pl"; }
		else if ($authnet_gateway=="mcps") { $processor="https://secure.merchantcommerce.net/gateway/transact.dll"; }
		else if ($authnet_gateway=="mplus") { $processor="https://gateway.merchantplus.com/cgi-bin/PAWebClient.cgi"; }
		else if ($authnet_gateway=="cdgcommerce") { $processor="https://www.quantumgateway.com/cgi/authnet.php"; }
		else { $processor="https://secure.authorize.net/gateway/transact.dll"; }

		if ($authnet_gateway=="cdgcommerce")
			{
			$link_out=array();

			$link_out['x_login']=$x_Login;
			$link_out['x_password']=$x_Password;
			$link_out['x_tran_key']=$x_tran_key;
			$link_out['x_amount']=$total_due_today;
			$link_out['x_customer_ip']=$_SERVER['REMOTE_ADDR'];
			$link_out['x_description']=$package_name." [".$domain_name."]";
			$link_out['x_currency_code']=$x_Currency_Code;
			$link_out['x_test_request']=(($demo==0)?"False":"True");

			$link_out['x_version']="3.1";
			$link_out['x_delim_data']="True";
			$link_out['x_delim_char']="|";
			$link_out['x_encap_char']="";
			$link_out['x_type']="AUTH_CAPTURE";
			$link_out['x_recurring_billing']="NO";
			$link_out['x_email_customer']="True";
			$link_out['x_method']="CC";
			$link_out['x_merchant_email']=$email_admin;
			
			$link_out['x_invoice_num']=$invoice_number;
			$link_out['x_first_name']=$_POST['x_First_Name'];
			$link_out['x_jast_name']=$_POST['x_Last_Name'];
			$link_out['x_address']=$_POST['x_Address'];
			$link_out['x_city']=$_POST['x_City'];
			$link_out['x_state']=$_POST['x_state'];
			$link_out['x_zip']=$_POST['x_Zip'];
			$link_out['x_country']=$_POST['x_Country'];
			$link_out['x_phone']=$_POST['x_Phone'];
			$link_out['x_fax']=$_POST['x_Fax'];
			$link_out['x_email']=$_POST['x_Email'];
			$link_out['x_card_num']=$_POST['x_Card_Num'];
			$link_out['x_exp_date']=$_POST['x_Exp_Date_m'].$_POST['x_Exp_Date_y'];
			$link_out['x_card_code']=$_POST['x_Card_Code'];
			}
		else
			{
			$link_out="x_Login=".$x_Login;
			$link_out.="&x_Password=".$x_Password;
			$link_out.="&x_Amount=".$total_due_today;
			$link_out.="&x_Customer_IP=".$_SERVER['REMOTE_ADDR'];
			$link_out.="&x_Description=".$package_name." [".$domain_name."]";
			$link_out.="&x_Currency_Code=".$x_Currency_Code;
			$link_out.="&x_Test_Request=".(($demo==0)?"False":"True")."";

			$link_out.="&x_Version=3.1";
			$link_out.="&x_Delim_Data=True";
			$link_out.="&x_Delim_Char=|";
			$link_out.="&x_Encap_Char=";
			$link_out.="&x_Type=AUTH_CAPTURE";
			$link_out.="&x_Recurring_Billing=NO";
			$link_out.="&x_Email_Customer=True";
			$link_out.="&x_Method=CC";
			$link_out.="&x_Merchant_Email=".$email_admin;
			
			$link_out.="&x_invoice_num=".$invoice_number;
			$link_out.="&x_First_Name=".$_POST['x_First_Name'];
			$link_out.="&x_Last_Name=".$_POST['x_Last_Name'];
			$link_out.="&x_Address=".$_POST['x_Address'];
			$link_out.="&x_City=".$_POST['x_City'];
			$link_out.="&x_State=".$_POST['x_state'];
			$link_out.="&x_Zip=".$_POST['x_Zip'];
			$link_out.="&x_Country=".$_POST['x_Country'];
			$link_out.="&x_Phone=".$_POST['x_Phone'];
			$link_out.="&x_Fax=".$_POST['x_Fax'];
			$link_out.="&x_Email=".$_POST['x_Email'];
			$link_out.="&x_Card_Num=".$_POST['x_Card_Num'];
			$link_out.="&x_Exp_Date=".$_POST['x_Exp_Date_m'].$_POST['x_Exp_Date_y'];
			$link_out.="&x_Card_Code=".$_POST['x_Card_Code'];
			}
		
		# $curl_root="/usr/bin/curl";
		
		if ($curl_root=="") 
			{
			$link=curl_init();
			curl_setopt($link, CURLOPT_URL, $processor);
			curl_setopt($link, CURLOPT_VERBOSE, 0);
			curl_setopt($link, CURLOPT_POST, 1);
			curl_setopt($link, CURLOPT_POSTFIELDS, $link_out);
			curl_setopt($link, CURLOPT_RETURNTRANSFER,1);
			$xresult=curl_exec($link);
			curl_close($link);
			$result=split("\,", $xresult);
			} 
		else 
			{
			@exec($curl_root." -d '".$link_out."' ".$processor, $xresult, $return);
			$result = split("\,", $xresult[0]);
			}
		
		if (isset($parse_return))
			{
			$res=strip_tags($result[0]);
			if (eregi("successfully processed", $res)) 
				{ 
				$response_code=1;
				$response_reason_text="Charged Successfully";

				list($junk, $good)=explode(":", $res);
				$res1=explode(" ", $good);
				$transaction_id=$res1[0]." ";
				$transaction_id.=str_replace("This", "", $res1[2]);
				}
			else 
				{ 
				$response_code=0;
				$response_reason_text="Transaction Failed or the card was declined. Please contact support";
				}
			}
		else
			{
			// list out result
			list($response_code, 
				$response_subcode, 
				$response_reason_code,
				$response_reason_text,
				$approval_code,
				$avs_result_code,
				$transaction_id,
				$invoice_number,
				$xdescription,
				$xamount,
				$method,
				$transaction_type,
				$customer_id,
				$cardholder_first_name,
				$cardholder_last_name,
				$company,
				$xaddress,
				$xcity,
				$xstate,
				$xzip,
				$xcountry,
				$xphone,
				$xfax,
				$xemail,
				$ship_to_first_name,
				$ship_to_last_name,
				$ship_to_company,
				$ship_to_address,
				$ship_to_city,
				$ship_to_state,
				$ship_to_zip,
				$ship_to_country,
				$tax_amount,
				$duty_amount,
				$freight_amount,
				$tax_exempt_flag,
				$po_number,
				$md5_hash,
				$xcard_code)=explode("|", $result[0]);

			# added 6/2/2004
			# -------------------------------------------------------------
			
			$response_code=str_replace("'", "", $response_code);
			
			$post_data="response_code=".$response_code."\n";
			$post_data.="response_subcode=".$response_subcode."\n";
			$post_data.="response_reason_code=".$response_reason_code."\n";
			$post_data.="response_reason_text=".$response_reason_text."\n";
			$post_data.="approval_code=".$approval_code."\n";
			$post_data.="avs_result_code=".$avs_result_code."\n";
			$post_data.="transaction_id=".$transaction_id."\n";
			$post_data.="invoice_number=".$invoice_number."\n";
			$post_data.="xdescription=".$xdescription."\n";
			$post_data.="xamount=".$xamount."\n";
			$post_data.="method=".$method."\n";
			$post_data.="transaction_type=".$transaction_type."\n";
			$post_data.="customer_id=".$customer_id."\n";
			$post_data.="cardholder_first_name=".$cardholder_first_name."\n";
			$post_data.="cardholder_last_name=".$cardholder_last_name."\n";
			$post_data.="company=".$company."\n";
			$post_data.="xaddress=".$xaddress."\n";
			$post_data.="xcity=".$xcity."\n";
			$post_data.="xstate=".$xstate."\n";
			$post_data.="xzip=".$xzip."\n";
			$post_data.="xcountry=".$xcountry."\n";
			$post_data.="xphone=".$xphone."\n";
			$post_data.="xfax=".$xfax."\n";
			$post_data.="xemail=".$xemail."\n";
			$post_data.="ship_to_first_name=".$ship_to_first_name."\n";
			$post_data.="ship_to_last_name=".$ship_to_last_name."\n";
			$post_data.="ship_to_company=".$ship_to_company."\n";
			$post_data.="ship_to_address=".$ship_to_address."\n";
			$post_data.="ship_to_city=".$ship_to_city."\n";
			$post_data.="ship_to_state=".$ship_to_state."\n";
			$post_data.="ship_to_zip=".$ship_to_zip."\n";
			$post_data.="ship_to_country=".$ship_to_country."\n";
			$post_data.="tax_amount=".$tax_amount."\n";
			$post_data.="duty_amount=".$duty_amount."\n";
			$post_data.="freight_amount=".$freight_amount."\n";
			$post_data.="tax_exempt_flag=".$tax_exempt_flag."\n";
			$post_data.="po_number=".$po_number."\n";
			$post_data.="md5_hash=".$md5_hash."\n";
			$post_data.="xcard_code=".$xcard_code."\n\n\n";
			$post_data.="Raw Data:\n\n".$result[0]."\n\n\n";
			
			raw_post_details($email_admin, $post_data, $domain_name, "Authorize.net");
			}
	
		if ($response_code==1)
			{
			// build vars
			if ($_POST['ccid']==0)
				{
				$card_info=$_POST['x_Card_Num']."|".$_POST['x_Exp_Date_m'].$_POST['x_Exp_Date_y']."|0|";
				$card_info=e("576cb7f68040520768bf51c75f7f4c84", $card_info);
				}

			$desc=$package_name." [".$domain_name."]{}".$package_name."{}";

			$batch_info=$_SERVER['REMOTE_ADDR']."|";	// customer_ip_address|
			$batch_info.=$desc."|";						// description|
			$batch_info.=$x_Currency_Code."|";			// currency_code|
			$batch_info.=$transaction_type."|";			// transaction_type|
			$batch_info.="YES"."|";						// recurring_billing|
			$batch_info.="CC"."|";						// payment_method|
			$batch_info.=$cardholder_first_name."|";	// customer_first_name|
			$batch_info.=$cardholder_last_name."|";		// customer_last_name|
			$batch_info.=$xaddress."|";					// customer_address|
			$batch_info.=$xcity."|";					// customer_city|
			$batch_info.=$xstate."|";					// customer_state|
			$batch_info.=$xzip."|";						// customer_zip|
			$batch_info.=$xcountry."|";					// customer_country|
			$batch_info.=$xphone."|";					// customer_phone|
			$batch_info.=$xfax."|";						// customer_fax|
			$batch_info.=$xemail."|";					// customer_email|
			$batch_info.=$card_info."|";				// card_info|
			$batch_info.=$response_code."|";			// batch_status|
			$batch_info.=$response_reason_text."|";		// response_reason_text|
			$batch_info.=$transaction_id."|";			// invoice_number|
			$batch_info.=$_POST['ccid']."|";			// ccid|

			// write marker
			$c=md5(microtime());

			// sid|batch_info
			$data=$sid."|".base64_encode($batch_info)."|";

			// write the file
			$read=fopen($below_public."/".$c, "w");
			$result=fwrite($read, $data);
			fclose($read);
			
			// chmod the file
			chmod($below_public."/".$c, 0777);

			// redirect out!
			header("Location: ".$http_web."/authnet_success.php?c=".$c);
			exit;
			}
		else
			{
			$e69=$response_reason_text.".";
			}
		}
	}
?>