<?PHP
//echo mktime(0, 0, 0, date("m"), date("d"), date("Y")); die;
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/whm_functions.php";
include $server_inc."/client_functions.php";
include $server_inc."/login_functions.php";

if (!isset($xc)) { die("invalid entry"); }


include $server_admin_inc."/header.php";

echo("
	<table width='725' cellpadding='0' cellspacing='0' border='0'>
		<tr>
			<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
			<td width='99%' align='left' valign='top'>
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".$sid."'>
			<table width='100%' cellpadding='4' cellspacing='1' border='0'>
				<tr>
					<td align='left' valign='middle'>[&nbsp;<a href='".$http_admin."/manage_batch.php?sid=".$sid."'>Go Back</a>&nbsp;]&nbsp;&nbsp;<b>Processing Batch - Please be patient.</b></td>
				</tr>
				<tr>
					<td align='left' valign='middle'><hr color='#F0EFED'></td>
				</tr>
	");
$query0="select ";
$query0.="x_gateway, ";			// 0
$query0.="x_Login, ";			// 1
$query0.="x_Password, ";		// 2
$query0.="x_Currency_Code, ";	// 3
$query0.="demo ";				// 4
$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]));

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

$xc=base64_decode($xc);
$b_=explode("|", $xc);
foreach ($b_ as $line)
	{
	if (trim($line)=="") { continue; }

	list($ccid, $oid, $bid)=split("{{}}", $line);

	$query3="select ";
	$query3.="customer_first_name, ";	// 0
	$query3.="customer_last_name, ";	// 1
	$query3.="customer_address, ";		// 2
	$query3.="customer_city, ";			// 3
	$query3.="customer_state, ";		// 4
	$query3.="customer_zip, ";			// 5
	$query3.="customer_country, ";		// 6
	$query3.="customer_phone, ";		// 7
	$query3.="customer_fax, ";			// 8
	$query3.="customer_email, ";		// 9
	$query3.="card_info, ";				// 10
	$query3.="uid ";					// 11
	$query3.="from ";
	$query3.="authnet_master_cc ";
	$query3.="where ";
	$query3.="ccid='".addslashes(trim($ccid))."'";

	$rs3=mysql_Fetch_row(mysql_query($query3));
		
	$x_First_Name=stripslashes(trim($rs3[0]));
	$x_Last_Name=stripslashes(trim($rs3[1]));
	$x_Address=stripslashes(trim($rs3[2]));
	$x_City=stripslashes(trim($rs3[3]));
	$x_state=stripslashes(trim($rs3[4]));
	$x_Zip=stripslashes(trim($rs3[5]));
	$x_Country=stripslashes(trim($rs3[6]));
	$x_Phone=stripslashes(trim($rs3[7]));
	$x_Fax=stripslashes(trim($rs3[8]));

	$x_Email=stripslashes(trim($rs3[9]));
	$card_info=stripslashes(trim($rs3[10]));
	$card_info=d("576cb7f68040520768bf51c75f7f4c84", $card_info);
	list($x_Card_Num, $x_Exp_Date_x, $x_Card_Code)=explode("|", $card_info);

	$uid=stripslashes(trim($rs3[11]));

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

	$query1="select ";
	$query1.="total_due_today, ";	// 0
	$query1.="total_due_reoccur, ";	// 1
	$query1.="pid, ";				// 2
	$query1.="domain_name, ";		// 3
	$query1.="status, ";			// 4
	$query1.="ogcreate, ";			// 5
	$query1.="resolved_time, ";		// 6
	$query1.="ur_date, ";			// 7
	$query1.="cancel_date, ";		// 8
	$query1.="payment_term ";		// 9
	$query1.="from hosting_order ";
	$query1.="where oid='".addslashes(trim($oid))."'";

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

	$total_due_today=stripslashes(trim($rs1[0]));
	$total_due_reoccur=stripslashes(trim($rs1[1]));
	$pid=stripslashes(trim($rs1[2]));
	$domain_name=stripslashes(trim($rs1[3]));
	$status=stripslashes(trim($rs1[4]));
	$ogcreate=stripslashes(trim($rs1[5]));
	$resolved_time=stripslashes(trim($rs1[6]));
	$ur_date=stripslashes(trim($rs1[7]));
	$cancel_date=stripslashes(trim($rs1[8]));
	$payment_term=stripslashes(trim($rs1[9]));


	$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"; }
	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 { $processor="https://secure.authorize.net/gateway/transact.dll"; }

	$link_out="x_Login=".$x_Login;
	$link_out.="&x_Password=".$x_Password;
	if ($status==0) { $total=$total_due_today; } else { $total=$total_due_reoccur; }
	$link_out.="&x_Amount=".$total;
	$link_out.="&x_Customer_IP=".$_SERVER['REMOTE_ADDR'];
	$link_out.="&x_Description=".$package_name." [".$domain_name."]";

	$query4="select ";
	$query4.="invoice_number ";
	$query4.="from ";
	$query4.="authnet_batch ";
	$query4.="where ";
	$query4.="bid='".addslashes(trim($bid))."'";

	$rs4=mysql_fetch_row(mysql_query($query4));

	$link_out.="&x_invoice_num=".$rs4[0];
	$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=YES";
	$link_out.="&x_Email_Customer=True";
	$link_out.="&x_Method=CC";
	$link_out.="&x_Merchant_Email=".$email_admin;

	$link_out.="&x_First_Name=".$x_First_Name;
	$link_out.="&x_Last_Name=".$x_Last_Name;
	$link_out.="&x_Address=".$x_Address;
	$link_out.="&x_City=".$x_City;
	$link_out.="&x_State=".$x_state;
	$link_out.="&x_Zip=".$x_Zip;
	$link_out.="&x_Country=".$x_Country;
	$link_out.="&x_Phone=".$x_Phone;
	$link_out.="&x_Fax=".$x_Fax;
	$link_out.="&x_Email=".$x_Email;
	$link_out.="&x_Card_Num=".$x_Card_Num;
	$link_out.="&x_Exp_Date=".$x_Exp_Date_x;
	# $link_out.="&x_Card_Code=".$x_Card_Code;

	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]);
		}

	// 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]);

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

	$query4="update ";
	$query4.="authnet_batch ";
	$query4.="set ";
	$query4.="currency_code='".addslashes(trim($x_Currency_Code))."', ";
	$query4.="transaction_type='".addslashes(trim($transaction_type))."', ";
	$query4.="batch_status='".addslashes(trim($response_code))."', ";
	$query4.="response_reason_text='".addslashes(trim($response_reason_text))."' ";
	$query4.="where ";
	$query4.="bid='".addslashes(trim($bid))."'";

	mysql_query($query4);
	
	if ($response_code==1)
		{
		$query4="update ";
		$query4.="invoice ";
		$query4.="set ";
		$query4.="status='1', ";
		$query4.="date_paid='".time()."' ";
		$query4.="where ";
		# $query4.="oid='".addslashes(trim($oid))."' ";
		if ($oid!=0) { $query4.="oid='".addslashes(trim($oid))."' "; }
		else { $query4.="uid='".addslashes(trim($uid))."' "; }
		$query4.="and ";
		$query4.="master='1'";
	
		mysql_query($query4);
		}

	echo("
					<tr>
						<td align='left' valign='middle'>".$domain_name." -> [".(($response_code==1)?"Approved":"".(($response_code==2)?"Declined":"Error")."")."] ".$response_reason_text."</td>
					</tr>
					<tr>
						<td align='left' valign='middle'><hr color='#F0EFED'></td>
					</tr>
		");
	
	str_pad(" ", 220);
	}

echo("
				</table>
				</td>
			</tr>
		</table>
		");
include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>