<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";
include "classes/cls_utils.php";
if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
	$_SESSION = array_map('stripslashes_deep', $_SESSION);
}
if(isset( $_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= ""){
	$userid = $_SESSION["sess_userid"];
}
$paymentMethod="PP";

//$totaltopay = getCartTotal($userid);

//getting payment info
$sql = "SELECT * FROM ".$tableprefix."settings ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
$txtPaypalEmail = $row["paypalemail"];
$txtPaypalSandbox = $row["enablepaypalsandbox"];

/*pass transaction id*/
$uique_sessionid=$userid.time().session_id();
$_SESSION['sess_unique_sessid']=$uique_sessionid;
$uique_sessionid_md5encrypted=md5($uique_sessionid);

$adminCurrency	= GetAdminCurrency();
/*****************/



$referrer =  $_SERVER["HTTP_REFERER"];

/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";

/* Top File Name */

include_once ("includes/".$active_template[0]);

//----------------Direct buyer to seller payment----------------------------//
/*$row = mysql_fetch_assoc(mysql_query("SELECT enable_buyerToseller FROM ".$tableprefix."settings "));
if($row["enable_buyerToseller"]=="Y")
{
	if($_SESSION['SESS_ARTISTID']!='')
	{
		$row = mysql_fetch_assoc(mysql_query("SELECT  paypalemail,paypalidentitytoken ,enablepaypalsandbox FROM ".$tableprefix."artists WHERE artist_id=".$_SESSION['SESS_ARTISTID']));
		$txtPaypalEmail		=$row['paypalemail'];
		$auth_token 		= $row["paypalidentitytoken"];
		$txtPaypalSandbox 	= $row["enablepaypalsandbox"];
		
	}	
}*/

if($txtPaypalSandbox == "Y"){
	$paypalurl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
	$paypalbuttonurl = "https://www.sandbox.paypal.com/en_US/i/btn/x-click-but23.gif" ;
}else{
	$paypalurl = "https://www.paypal.com/cgi-bin/webscr";
	$paypalbuttonurl = "https://www.paypal.com/en_US/i/btn/x-click-but23.gif" ;
}
//----------------Direct buyer to seller payment ends here----------------------------//

/*echo $paypalurl;

echo "<br>";

echo $paypalbuttonurl;

echo "<br>";

echo $txtPaypalEmail;

exit;*/

?>


<!--mainbody area start -->
<div class="main_body">
	<div class="container">
		<div class="row">	
			
		<!--center content area start-->		
		
		<div id="item_details">
	
	
		<!--items display area start -->
	
		<section class="sec_marg">
		<div class="col-sm-12 col-lg-12 col-md-12 ">
			<h3><?php echo PAYPAL_GATEWAY_TEXT; ?></h3>
		</div>
		</section>
		
		
		<div class="text_information" align="center"><?php echo $message?></div>
		
		<div class="col-sm-12 col-lg-12 col-md-12 ">

		
		<table cellpadding="0" cellspacing="0" width="100%">
		

<?php

	if(isset($_SESSION["TAX_TOTAL"]) && $_SESSION["TAX_TOTAL"]!='')
	{
		  $taxrate=number_format($_SESSION["TAX_TOTAL"],2,".","");
	} 
	
	
	
	$currency_attributes = displayCurrencyType();
	
/*-------function call to check whether cart contains digital or gift coupon products so as to hide shipping---*/

	$hide_shipping_division = hideShipping();

/*-------function call to check whether cart contains digital or gift coupon products so as to hide shipping------*/
	
	$check_shipping_enable = checkShippingEnable();
	
	if($check_shipping_enable == "Y" && $hide_shipping_division == 1)
	{ 
	 
		$shipping_cost = 	$_SESSION['SHIPPING_TOTAL'];
		
		$shipping_cost=number_format($shipping_cost,2,".","");
		 
		$shipping_form = "<input type='hidden' name='handling_cart' value='$shipping_cost'>";
		$shipping_form .= "<input type='hidden' name='no_shipping' value='1'>";	
	}
	else
	{
	
		$shipping_form = "<input type='hidden' name='no_shipping' value='1'>";																							
	
	}	
        $cart_coupon_discount =0;
        
         $totaltopay = number_format((getCartTotal($userid)),2,".","");

       /*() if(isset($_SESSION['sess_giftcertificate_available_amt']) && $_SESSION['sess_giftcertificate_available_amt']>0)
         {

                        $cart_coupon_discount = number_format($_SESSION['sess_giftcertificate_available_amt'],2,".","");

         }
					 
					 
		
        if(isset($_SESSION['couponCode_det']) && $_SESSION['couponCode_det']!='')
        {
                $totalpay = number_format((getCartTotalcouponCode($userid,$_SESSION['couponCode_det'])),2,".","");

                $cart_coupon_discount	= number_format((getCartTotalcouponCodeDicountamount($userid,$_SESSION['couponCode_det'])),2,".","");

        }else{
                $totalpay = number_format((getCartTotal($userid)),2,".","");
        }*/

        if(isset($_SESSION['couponCode_det']) && $_SESSION['couponCode_det']!='' || $_SESSION['sess_giftcertificate_available_amt']>0)
        {

                //$couponCodeDiscountAmt	= getCartTotalcouponCodeDicountamount($userid,$_SESSION['couponCode_det']);
                $couponCodeDiscountAmt          = $_SESSION['couponCode_det']['discount_amount'];
                $total_discount_amount          = $_SESSION['sess_giftcertificate_available_amt'] + $couponCodeDiscountAmt;
                $cart_coupon_discount           =  number_format($total_discount_amount,2,".","");
                $totaltopay                     =  $totaltopay -$total_discount_amount;

        }

        
			
				
	//$totaltopay += $shipping_cost;
	
	//passing custom field for paypal ipn updation
                        $customValues=$_SESSION["sess_userid"].'-'.$_SESSION["sessBillingFAX"].'-'.$_SESSION["sessBillingEmail"].'-'.$_SESSION["sessBillingPhone"].'-'.
                        $_SESSION["sessShippingFirstName"].'-'.$_SESSION["sessShippingLastName"].'-'.$_SESSION["sessShippingAddress1"].'-'.
                        $_SESSION["sessShippingAddress2"].'-'.$_SESSION["sessShippingCity"].'-'.$_SESSION["sessShippingState"].'-'.
                        $_SESSION["sessShippingCountry"].'-'.$_SESSION["sessShippingZIP"].'-'.$_SESSION["sessShippingFAX"].'-'.
                        $_SESSION["sessShippingEmail"].'-'.$_SESSION["sessShippingPhone"].'-'.$_SESSION["sessShippingMethod"].'-'.
                        $_SESSION['SESS_currency_type'].'-'.$_SESSION['couponCode_det'];
	
?>			


			<tr>
                            <td><img src="<?php echo str_replace("https://", "//", SECURE_SITE_URL); ?>/images/loading.gif" border="0"> &nbsp;&nbsp;<?php echo WAIT_INTERFACE_TEXT; ?></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td align="center">
					<form action="<?php echo $paypalurl;?>" method="post" name="frmPayment">
					<div style="display:none;">&nbsp;&nbsp;&nbsp;<?php echo TEXT_PAYPAL_NAVIGATE; ?></div>
					<p>
						<input type="hidden" name="cmd" value="_cart">
						<input type="hidden" name="business" value="<?php echo $txtPaypalEmail?>">
						<input type="hidden" name="upload" value="1">


<?php

		$sql_cart = "SELECT c.*,(p.product_price+po.additional_price) AS product_price,p.product_name,p.product_description,p.product_code,p.product_artist_id,po.discount,p.vdigital_product
				FROM " . $tableprefix . "cart c
                                INNER JOIN " . $tableprefix . "product_options po ON po.product_option_id = c.product_option_id
                                INNER JOIN " . $tableprefix . "products p ON p.product_id = po.product_id
				WHERE c.user_id = '".$userid."' ";
				
		$res_cart = mysql_query($sql_cart) or die(mysql_error());
		
		$total_product_amt = 0;
		if(mysql_num_rows($res_cart)>0)
		{
			$i=1;
			
			while($row_cart	=mysql_fetch_array($res_cart))
			{
			
				$cart_productid         = 	$row_cart["product_id"];
				
				$cart_quantity          =       $row_cart["quantity"];
				
				$productprice           =       number_format($row_cart['product_price'],2,".","");
				
				$cart_productname       =       generalMethods::html_entity($row_cart["product_name"]);
                                $optionsStr             =       getProductOptionsName($row_cart["product_option_id"]);
                                $product_description	= 	Utils::html_entity($row_cart["product_description"]);
				$productcode 		=  	stripslashes($row_cart["product_code"]);
				$productdiscount 	= 	$row_cart["discount"];
				
				$unitprice		= $productprice - (($productdiscount/100)*$productprice);
				
				$unitprice		= number_format($unitprice,2,".","");
				
				$total_product_amt      +=  $unitprice;//discounted amt
                                $itemName = strip_tags($cart_productname).$optionsStr.' - '.strip_tags($product_description);
                                $itemName = (string) html_entity_decode ( (string) $itemName , (int) $quote_style = ENT_HTML5 , (string) $charset = 'UTF-8'  );
?>					
                                                <input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $itemName; ?>">
						<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $unitprice; ?>">
						<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $cart_quantity; ?>">
						<!--<input type="hidden" name="discount_rate_<?php echo $i; ?>" value="<?php echo $productdiscount; ?>">-->
<?php

			$i++;	
			}		
	}					
						/*Shipping Settings*/
						echo $shipping_form;
?>						
<?php if($taxrate>0)
{
?>		
                                <input type="hidden" name="tax_cart" value="<?php echo $taxrate;?>">
<?php
}
?>			
				<input type="hidden" name="discount_amount_cart" value="<?php echo $cart_coupon_discount;?>"> 
                                <!--<input type="hidden" name="notify_url" value="<?php //echo SECURE_SITE_URL ."/success_payipn.php";?>">-->
                                <!--<input type="hidden" name="custom" value="<?php// echo $customValues;?>">-->
                                <input type="hidden" name="item_number" value="<?php echo $uique_sessionid;?>">
                                <input type="hidden" name="bn" value="armiasystems_shoppingcart_wps_us">
                                <input type="hidden" name="return" value="<?php echo SECURE_SITE_URL ."/success.php"; ?>">
                                <input type="hidden" name="cancel_return" value="<?php echo SITE_URL ."/failure.php"; ?>">
                                <input type="hidden" name="no_note" value="1">
                                <input type="hidden" name="currency_code" value="<?php echo $adminCurrency['currency_code']?>">
				</form>
<?php
/*--------------Register a session to store the amount-------------*/

//(isset($_SESSION['SESS_pay_amount']))?session_unregister('SESS_pay_amount'):session_register('SESS_pay_amount');

$totalpayamt= $totaltopay + $shipping_cost+$taxrate;

$_SESSION['SESS_pay_amount']=number_format($totalpayamt,2,".","");
/*--------------Register a session to store the amount-------------*/
	
?>				

				</td>
			</tr>	
	</table>		
	</div>	
	<div align="left" class="float_clear1"></div>
	 <!--items display area end -->
	  </div>
	<!--center content area end-->	
	<div align="left" class="clear"></div>		
	</div>
	</div>
</div>		
<!--mainbody area end -->
<div align="left" class="clear"></div>
<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																										
<script>
	document.frmPayment.submit();		    
</script>