<?

/***************************************************************************
 *			shop_pays.php                   	           *
 *                     	          				           *
 *   File Info:		  Pay for cart                                     *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include ("./config.php");
include ("./payment.php");
include("./tests.php");
$PHPSESSID = session_id();
$pay_type = getenv("QUERY_STRING"); 
$old_type = $pay_type;

if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $full_name = addslashes($_POST["full_name"]);
        $email = addslashes($_POST["email"]);
        $address1 = addslashes($_POST["address1"]);
        $address2 = addslashes($_POST["address2"]);
        $city = addslashes($_POST["city"]);
        $postcode = addslashes($_POST["postcode"]);
        $phone = addslashes($_POST["phone"]);
        $fmt_subtotal = addslashes($_POST["fmt_subtotal"]);
        $order_id = addslashes($_POST["order_id"]);
        $shipzone = addslashes($_POST["shipzone"]);
	$msg = addslashes($_POST["msg"]);
}

$table= "myorders";
$sel_item_totalprice = $sel_item_qty * $sel_item_price;
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database 'mydata'...Please press 'Create Database' to create new database.");
$get_itemtot = "SELECT SUM(SEL_ITEM_TOTALPRICE) FROM $table
		WHERE USER_ID =  '$PHPSESSID'";

$itemtot_result = mysql_query($get_itemtot) 
	or die("SQL command failed - Couldn't get total");

$itemtot = mysql_result($itemtot_result,0,"SUM(SEL_ITEM_TOTALPRICE)");

$fmt_itemtot = sprintf("%0.2f",$itemtot);
	
############ normal currency/world currency ###########

if ($wcurrency == "NO" or $wcurrency != "NO") {

############# non-sql ##################
if ($itemtot < $min_cost && $usedata == "NO") 
{
	$shipping = $shipcost;
} else 
{
	$shipping = 0;
}

if ($itemtot < $mincost && $usedata == "YES") 
{
	$shipping = $shipcost;
} else 
{
	$shipping = 0;
}
$fmt_shipping = sprintf("%0.2f",$shipping);
$subtotal = $itemtot + $shipping;
$fmt_subtotal = sprintf("%0.2f",$subtotal);
}

$total = $fmt_subtotal;
$get_cart = "SELECT SEL_ITEM, SEL_ITEM_TITLE, SEL_ITEM_QTY, SEL_ITEM_PRICE, 
		SEL_ITEM_TOTALPRICE FROM $table
		WHERE USER_ID = '$PHPSESSID'";

$cart_result = mysql_query($get_cart)
	or die("SQL command failed - Couldn't get cart.");

$num_rows = mysql_numrows($cart_result);
$bad = "FALSE";
if ($num_rows == "0") {
	if ($phpcart == "NO"){
	if ($usecat == "NO")
		include_once("./sidebar.php");
	else
		include_once("./sidebars.php");}

################## display information box ####################

	echo "<center><br><table width=550 border=1 cellspacing=0 cellpadding=0 height=150>
<tr><td width=550 bgcolor=#CCCCCC height=10>EZ Shopper Information - Payment Processing</td></tr><tr><td width=550 bgcolor=#FFFFFF height=140>";
echo"<br><h1><font color = $highlight><center>Order Confirmation</center></font></h1>";
	echo "<P><strong><center><img src=./images/cart.gif border=0>There are no items in your cart - an order requires items added to the cart !</center></p></tr></td></table>";
} 
else 
{
# if user is registered , add the order to their order history
if ($phpcart == "NO") {
if (((session_is_registered("admin")) and ($_SESSION['admin'] != "admin")) or ((!session_is_registered("admin")) and ($_SESSION['admin'] == NULL)))
{
	$table = "history";
	if (strlen($phone) == 0)
		$phone = "n/a";
	$rtime = date("Y-m-d H:i dS F");
	$admins = $_SESSION['admin'];
	if ((session_is_registered("admin")) and ($_SESSION['admin'] != "admin"))
	$csql = "INSERT into $table values('$order_id','$admins','$full_name','$email','$address1','$address2','$city','$postcode','$phone','$pay_type','Awaiting payment','$fmt_subtotal','$rtime')";
	if ((!session_is_registered("admin")) and ($_SESSION['admin'] == NULL))
	$csql = "INSERT into $table values('$order_id','guest','$full_name','$email','$address1','$address2','$city','$postcode','$phone','$pay_type','Awaiting payment','$fmt_subtotal','$rtime')";
	$cres = mysql_query($csql,$conn)
	or die("SQL command failed - Couldn't insert history record.");
	$table = "myorders";
	$dsql = "SELECT * from $table where  user_id = \"$PHPSESSID\"";
	$count = 0;
	$qty = array();
	$product = array();
	$price = array();
	$totalpr = array();
	$dres = mysql_query($dsql,$conn)
	or die("SQL command failed - Couldn't extract customer order record details.");	
	$ncount = mysql_num_rows($dres);

	for ($count = 0; $count < $ncount; $count++)
	{
		$details = mysql_fetch_row($dres);
		$qty[$count] = $details[3];
		$product[$count] = $details[1];
		$price[$count] = $details[4];
		$totalpr[$count] = $details[5];
	}
	$table = "custdet";
	if ($wcurrency == "NO")
		$special = "NO";
	else
		$special = "YES";
	for ($count = 0; $count < $ncount;$count++)
	{
		$admins = $_SESSION['admin'];
		$esql = "INSERT into $table values('$order_id','$admins','$qty[$count]','$product[$count]','$price[$count]','$totalpr[$count]','$special')"; 
		
		mysql_query($esql,$conn)
			or die("SQL command failed - Couldn't insert customer order record.");
	}
	
}}
$table = "myorders";
$rm_item_sql = "DELETE FROM $table WHERE user_id = \"$PHPSESSID\"";
mysql_query($rm_item_sql) or die("Couldn't remove items.");

# take care of payments processing - add as many as you like #

if ($old_type == "2Checkout")
{
	if ($phpcart == "NO"){
	if ($usecat == "NO")
		include_once("./sidebar.php");
	else
		include_once("./sidebars.php");}

	################## display information box ####################

	echo "<center><br><table width=570 border=1 cellspacing=0 cellpadding=0 height=150>
<tr><td width=570 bgcolor=#CCCCCC height=10>$header - Payment Processing</td></tr><tr><td width=570 bgcolor=#FFFFFF height=140>";

	################# display order amount for user ###############

	if ($wcurrency != "NO")	
	{
		echo "<center><img src=images/online-order.gif border=0></center>";
		echo "<br><center><font face=verdana size=3 color=darkgreen>Your Order Details:-</font><br>";
		if ($scurrency == "cad") # canadian dollar
		{
			$totals = $total * $conv5;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Canadian Dollars) = $totals</font>";
		}
		if ($scurrency == "aud") # australian dollar
		{
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font>";
		}
		if ($scurrency == "usd") # usa dollar
		{
			$totals = $total * $conv2;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (US Dollars) = $totals</font>";
		}
		if ($scurrency == "gbp") # uk sterling
		{
			$totals = $total * $conv1;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (UK Sterling) = $totals</font>";
		}
		if ($scurrency == "jpy") # japanese yen
		{
			$totals = $total * $conv4;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Japanese Yen) = $totals</font>";
		}
		if ($scurrency == "eur") # euros
		{
			$totals = $total * $conv3;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Euros) = $totals</font>";
		}
		echo "</center><br>";
	}

	if ($wcurrency == "NO")
	{
		echo "<center><img src=images/online-order.gif border=0></center>";
		echo "<br><center><font face=verdana size=3 color=darkgreen>Your Order Details:-</font><br>";
		echo  "<font face=verdana size=2>Total Order Value ($currency) = $total</font>";
		echo "</center><br>";
	}

	echo("<br><center><font face=verdana size=2>To submit your order by <b>2Checkout</b>, press click on the <b>Pay By 2Checkout</b> button.<br>After you click the button your order be transferred securely to 2Checkout for authorisation.</font></center><br>");
	echo"<center><form target=\"_self\" action=\"https://www.2checkout.com/cgi-bin/sbuyers/cartpurchase.2c?\" name=\"SecureForm\" method=\"post\">
	<input type=\"hidden\" name=\"sid\" value=\"$sid\">
	<input type=\"hidden\" name=\"card_holder_name\" value=\"$full_name\">
	<input type=\"hidden\" name=\"ship_name\" value=\"$full_name\">
	<input type=\"hidden\" name=\"ship_street_address\" value=\"$address1 $address2\">
	<input type=\"hidden\" name=\"ship_city\" value=\"$city\">
	<input type=\"hidden\" name=\"ship_zip\" value=\"$postcode\">
	<input type=\"hidden\" name=\"cart_order_id\" value=\"$order_id\">
	<input type=\"hidden\" name=\"total\" value=\"$total\">
	<input type = \"submit\" value =\"Pay By 2Checkout\">
	</form></center></tr></td></table>";
}

if ($old_type == "PayPal")
{
	# include("./paypal.inc");
	if ($phpcart == "NO"){
	if ($usecat == "NO")
		include_once("./sidebar.php");
	else
		include_once("./sidebars.php");}

	################## display information box ####################
	
	echo "<center><br><table width=550 border=1 cellspacing=0 cellpadding=0 height=150>
<tr><td width=550 bgcolor=#CCCCCC height=10>$header - Payment Processing</td></tr><tr><td width=550 bgcolor=#FFFFFF height=140>";

	################# display order amount for user ###############

	if ($wcurrency != "NO")	
	{
		echo "<center><img src=images/online-order.gif border=0></center>";
		echo "<br><center><font face=verdana size=3 color=darkgreen>Your Order Details:-</font><br>";
if ($scurrency == "cad") # canadian dollar
		{
			$totals = $total * $conv5;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Canadian Dollars) = $totals</font>";
		}
		if ($scurrency == "aud") # australian dollar
		{
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font>";
		}
		if ($scurrency == "usd") # usa dollar
		{
			$totals = $total * $conv2;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (US Dollars) = $totals</font>";
		}
		if ($scurrency == "gbp") # uk sterling
		{
			$totals = $total * $conv1;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (UK Sterling) = $totals</font>";
		}
		if ($scurrency == "jpy") # japanese yen
		{
			$totals = $total * $conv4;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Japanese Yen) = $totals</font>";
		}
		if ($scurrency == "eur") # euros
		{
			$totals = $total * $conv3;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Euros) = $totals</font>";
		}
		echo "</center><br>";
	}

	if ($wcurrency == "NO")
	{
		echo "<center><img src=images/online-order.gif border=0></center>";
		echo "<br><center><font face=verdana size=3 color=darkgreen>Your Order Details:-</font><br>";
		echo  "<font face=verdana size=2>Total Order Value ($currency) = $total</font>";
		echo "</center><br>";
	}

	echo("<br><center><font face=verdana size=2>To submit your order by <b>PayPal</b>, press click on the <b>Pay By PayPal</b> button.<br>After you click the button your order be transferred securely to PayPal for authorisation.</font></center><br>");
	echo"<center><form target=\"_self\" action=\"https://www.paypal.com/xclick\" name=\"SecureForm\" method=\"post\">
	<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">
	<input type=\"hidden\" name=\"rm\" value=\"2\">
	<input type=\"hidden\" name=\"business\" value=\"$paypal_email\">
	<input type=\"hidden\" name=\"cart_order_id\" value=\"$order_id\">
	<input type=\"hidden\" name=\"item_name\" value=\"Shopping Basket Contents\">
	<input type=\"hidden\" name=\"item_number\" value=\"$order_id\">
	<input type=\"hidden\" name=\"amount\" value=\"$total\">
	<input type=\"hidden\" name=\"currency_code\" value=\"$paypal_currency\">
	<input type=\"hidden\" name=\"return\" value=\"$paypay_returnurl\">
	<input type = \"submit\" value =\"Pay By PayPal\">
	</form></center></tr></td></table>";
}
if ($old_type == "Nochex")
{
	#include("./nochex.inc");
	if ($phpcart == "NO"){
	if ($usecat == "NO")
		include_once("./sidebar.php");
	else
		include_once("./sidebars.php");}

	################## display information box ####################

	echo "<center><br><table width=550 border=1 cellspacing=0 cellpadding=0 height=150>
<tr><td width=550 bgcolor=#CCCCCC height=10>$header - Payment Processing</td></tr><tr><td width=550 bgcolor=#FFFFFF height=140>";

	################# display order amount for user ###############

	if ($wcurrency != "NO")	
	{
		echo "<center><img src=images/online-order.gif border=0></center>";
		echo "<br><center><font face=verdana size=3 color=darkgreen>Your Order Details:-</font><br>";
		if ($scurrency == "cad") # canadian dollar
		{
			$totals = $total * $conv5;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Canadian Dollars) = $totals</font>";
		}
		if ($scurrency == "aud") # australian dollar
		{
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font>";
		}
		if ($scurrency == "usd") # usa dollar
		{
			$totals = $total * $conv2;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (US Dollars) = $totals</font>";
		}
		if ($scurrency == "gbp") # uk sterling
		{
			$totals = $total * $conv1;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (UK Sterling) = $totals</font>";
		}
		if ($scurrency == "jpy") # japanese yen
		{
			$totals = $total * $conv4;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Japanese Yen) = $totals</font>";
		}
		if ($scurrency == "eur") # euros
		{
			$totals = $total * $conv3;
			$totals = number_format($totals,2);
			echo "<font face=verdana size=2>Total Order Value ($wcurrency) = $total</font><br>";
			echo "<font face=verdana size=2>Total Order Value (Euros) = $totals</font>";
		}
		echo "</center><br>";
	}

	if ($wcurrency == "NO")
	{
		echo "<center><img src=images/online-order.gif border=0></center>";
		echo "<br><center><font face=verdana size=3 color=darkgreen>Your Order Details:-</font><br>";
		echo  "<font face=verdana size=2>Total Order Value ($currency) = $total</font>";
		echo "</center><br>";
	}

	echo("<br><center><font face=verdana size=2>To submit your order by <b>Nochex</b>, press click on the <b>Pay By Noxhex</b> button.<br>After you click the button your order be transferred securely to Nochex for authorisation.</font></center><br>");
	echo"<center><form action=\"https://www.nochex.com/nochex.dll/checkout\" name=\"SecureForm\" method=POST> 
	<input type=\"hidden\" name=\"cart_order_id\" value=\"$order_id\">
	<input type=\"hidden\" name=\"total\" value=\"$total\">
	<input type=\"hidden\" name=\"amount\" value=\"$total\">   
	<input type=\"hidden\" name=\"logo\" value=\"$nochex_logo\">
	<input type=\"hidden\" name=\"from_email\" value=\"$email\">
	<input type=\"hidden\" name=\"description\" value=\"Shopping Basket Contents\">
	<input type= \"hidden\" name=\"returnurl\" value=\"$paypay_returnurl\">
	<input type= \"hidden\" name=\"email\" value=\"$nochex_email\">
	<input type=\"text\" name=\"ordernumber\" value=\"$order_id\"readonly> 
	<input type = \"submit\" value =\"Pay By Nochex\">
	</form></center></tr></td></table>";
}
}
// ############################## Developer's Note ##############################################

//# You can add as many payment agents inside the payments area. You just need to do the following.
//# Create a if...statement and the code to handle the processing e.g if ($old_type == 'xxxxxxxx') { code.....}
//# Note the code you can get from the internet forums such as hotscripts.com or any php developers forums.
//# You just need to add the extra option on the checkout page i.e the pull select box.

//# If you have any queries please direct them to technical@128concepts.com
?>



