<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

  include('includes/application_top.php');
 require(DIR_WS_CLASSES . 'currencies.php');
 $currencies=new currencies();
  $server_date = getServerDate(true);	

  if ( (tep_not_null(MODULE_PAYMENT_INSTALLED)) && (!tep_session_is_registered('payment')) ) tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));  

 // include(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_PROCESS);
include(DIR_WS_LANGUAGES . $language . '/' . FILENAME_EDIT_ORDERS_PROCESS);
// load selected payment module
 require(DIR_WS_CLASSES . 'payment.php');
  $payment_modules = new payment($payment);

// load the selected shipping module
 // require(DIR_WS_CLASSES . 'shipping.php');
 // $shipping_modules = new shipping($shipping);

  require(DIR_WS_CLASSES . 'order.php');
  $oID=(int)$edit_order;

 	require(DIR_WS_CLASSES . 'ot_coupon.php');
 	$coupon_class= new ot_coupon();
		$edit_cart=new editOrderCart($oID);
		$update_string=$edit_cart->store_final_contents();

		if ($update_string!=""){
			$order_status_query=tep_db_query("SELECT orders_status from " . TABLE_ORDERS . " where orders_id='" . (int)$oID . "'");
			$order_status_result=tep_db_fetch_array($order_status_query);
			$status=$order_status_result["orders_status"];
			$add_comments="<b>Order Updated</b><br>" . $update_string;
			$user_name=tep_admin_name($login_id);
			tep_db_query("insert into " . TABLE_ORDERS_STATUS_HISTORY . " 
			(orders_id, orders_status_id, date_added, customer_notified, comments,user_added) 
			values ('" . $oID . "', '" . $status . "','" .  $server_date . "', '" . $customer_notify . "', '" . tep_db_input($add_comments)  . "','" . tep_db_input($user_name) . "')");

		}
		$UpdateOrders = "update " . TABLE_ORDERS . " set ";
		

	$update_info_cc_type=(isset($GLOBALS['cc_type']) ? tep_db_input(stripslashes($GLOBALS['cc_type'])) : '');
	$update_info_cc_owner=(isset($GLOBALS['cc_owner']) ? tep_db_input(stripslashes($GLOBALS['cc_owner'])) : '');
	$update_info_cc_number=(isset($GLOBALS['cc_number']) ? tep_db_input(stripslashes($GLOBALS['cc_number'])) : '');
	$update_info_cc_cvv='';
	$update_info_cc_expires=(isset($GLOBALS['cc_expires']) ? tep_db_input(stripslashes($GLOBALS['cc_expires'])) : '');
	$update_info_payment=(isset($GLOBALS['payment_info']) ? tep_db_input(stripslashes($GLOBALS['payment_info'])) : '');
	$UpdateOrders .="payment_method = '" . tep_db_input(stripslashes($GLOBALS[$payment]->title)) . "',
					 cc_type = '" . tep_db_input(stripslashes($update_info_cc_type)) . "',
					 cc_owner = '" . tep_db_input(stripslashes($update_info_cc_owner)) . "',
					 cc_cvv_number='" . tep_db_input(stripslashes($update_info_cc_cvv)) . "',";
	
	$UpdateOrders .= "cc_number = '" . tep_db_input(stripslashes($update_info_cc_number)) . "',";
	$UpdateOrders .= "payment_info = '" . tep_db_input(stripslashes($GLOBALS[$payment]->email_footer)) . "',";
	
	$UpdateOrders .= "cc_expires = '" . tep_db_input(stripslashes($update_info_cc_expires)) . "'";
if ($UpdateOrders!=''){
		$UpdateOrders .= ",last_modified = '" .  $server_date . "', orders_status='-1'";
		$UpdateOrders .= " where orders_id = '" . tep_db_prepare_input($oID) . "';";
		tep_db_query($UpdateOrders);
	}
		
// load the before_process function from the payment modules
  $payment_modules->before_process();
 $order = new order($oID);
 
 $order_totals=$order->totals;

// load the after_process function from the payment modules
  $payment_modules->after_process();
  if (ereg($order->info['currency'],MODULE_PAYMENT_PAYPALIPN_ALLOWED_CURRENCIES)) {
    $paypal_ipn_currency = $order->info['currency'];
  } else {
    $paypal_ipn_currency = MODULE_PAYMENT_PAYPALIPN_DEFAULT_CURRENCY;
  };
 
  $order_total_amount=$net_amount;
  $order_tax_amount=$updated_tax;
  $paypal_ipn_order_amount = $order_total_amount;

  if (MODULE_PAYMENT_PAYPALIPN_ADD_TAX_TO_AMOUNT=='True') {
    $paypal_ipn_tax_amount = 0.00;
  } else {
    $paypal_ipn_order_amount -= $order_tax_amount;
	$paypal_ipn_tax_amount = number_format(($order_tax_amount * $currencies->get_value($paypal_ipn_currency)), 2);
  };

  $paypal_ipn_order_amount = number_format(($paypal_ipn_order_amount * $currencies->get_value($paypal_ipn_currency)), 2);

 
  
 // tep_redirect("https://www.paypal.com/cgi-bin/webscr?cmd=_ext-enter&redirect_cmd=_xclick&business=".MODULE_PAYMENT_PAYPALIPN_ID."&item_name=".urlencode(STORE_NAME)."&item_number=".$insert_id."&currency_code=".$paypal_ipn_currency."&amount=".$paypal_ipn_order_amount."&shipping=".$paypal_ipn_shipping_amount."&tax=".$paypal_ipn_tax_amount."&first_name=".urlencode($order->customer['firstname'])."&last_name=".urlencode($order->customer['lastname'])."&address1=".urlencode($order->customer['street_address'])."&city=".urlencode($order->customer['city'])."&state=".urlencode($order->customer['state'])."&zip=".urlencode($order->customer['postcode'])."&email=".$order->customer['email_address']."&bn=oscommerce-osmosis-0.981&return=".tep_href_link(FILENAME_CHECKOUT_SUCCESS, '', 'SSL')."&cancel_return=".tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL')."&notify_url=".MODULE_PAYMENT_PAYPALIPN_NOTIFY_URL);
tep_redirect("https://www.paypal.com/cgi-bin/webscr?cmd=_ext-enter&redirect_cmd=_xclick&business=".MODULE_PAYMENT_PAYPALIPN_ID."&item_name=".urlencode(STORE_NAME)."&item_number=".$insert_id."&currency_code=".$paypal_ipn_currency."&amount=".$paypal_ipn_order_amount."&tax=".$paypal_ipn_tax_amount."&first_name=".urlencode($order->customer['firstname'])."&last_name=".urlencode($order->customer['lastname'])."&address1=".urlencode($order->customer['street_address'])."&city=".urlencode($order->customer['city'])."&state=".urlencode($order->customer['state'])."&zip=".urlencode($order->customer['postcode'])."&email=".$order->customer['email_address']."&bn=oscommerce-osmosis-0.981&return=".tep_href_link(FILENAME_EDIT_ORDERS_SUCCESS, '', 'SSL')."&cancel_return=".tep_href_link(FILENAME_EDIT_ORDERS, '', 'SSL')."&notify_url=".MODULE_PAYMENT_PAYPALIPN_NOTIFY_URL);  
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>