<?php  



class ControllerCheckoutPayment extends Controller {



	var $error = array();



  	function index() {



		$address   =& $this->locator->get('address');



		$cart      =& $this->locator->get('cart');



		$config    =& $this->locator->get('config');



		$customer  =& $this->locator->get('customer');



		$database  =& $this->locator->get('database');



		$language  =& $this->locator->get('language');



		$module    =& $this->locator->get('module');



		$payment   =& $this->locator->get('payment');



		$request   =& $this->locator->get('request');



		$response  =& $this->locator->get('response');



		$session   =& $this->locator->get('session');



		$shipping  =& $this->locator->get('shipping');



		$tax       =& $this->locator->get('tax');



		$template  =& $this->locator->get('template');



		$url       =& $this->locator->get('url');	


       
	    if (!$customer->isLogged()&&!isset($_POST)) {

	

				$session->set('redirect', $url->ssl('checkout_shipping'));

	

		  		$response->redirect($url->ssl('account_login'));

	

	    	} 

    	 elseif(isset($_POST)&&isset($_POST['cancel_return'])){



        	



        	echo '



        	       <form action="https://www.beststoreitems.com/index.php?controller=checkout_confirm" method="post"  id="form" name="form">



        	       



        	          <input type="hidden" name="cmd" value="'.$_POST['cmd'].'" />



        	          <input type="hidden" name="business" value="wutingjing@hotmail.com" />



        	          <input type="hidden" name="item_name" value="'.$_POST['item_name'].'" />



        	          <input type="hidden" name="currency_code" value="'.$_POST['currency_code'].'" />



        	           <input type="hidden" name="amount" value="'.$_POST['amount'].'" />



        	          <input type="hidden" name="first_name" value="'.$_POST['first_name'].'" />



        	          <input type="hidden" name="last_name" value="'.$_POST['last_name'].'" />



        	          <input type="hidden" name="address1" value="'.$_POST['address1'].'" />
        	          
        	           <input type="hidden" name="charset" value="utf-8" />
        	           
        	           <input type="hidden" name="lc" value="US" />

        	          <input type="hidden" name="city" value="'.$_POST['city'].'" />



        	          <input type="hidden" name="zip" value="'.$_POST['zip'].'" />



        	          <input type="hidden" name="country" value="'.$_POST['country'].'" />



        	          <input type="hidden" name="address_override" value="0" />



        	          <input type="hidden" name="notify_url" value="https://www.beststoreitems.com/payment/paypal/paypal_notify.php" />



        	          <input type="hidden" name="email" value="'.$_POST['email'].'" />



        	           <input type="hidden" name="invoice" value="'.$_POST['invoice'].'" />



        	          <input type="hidden" name="return" value="https://www.beststoreitems.com/paypal_sucess.html" />



        	          <input type="hidden" name="rm" value="2" />



        	          <input type="hidden" name="no_note" value="1" />



        	          <input type="hidden" name="cancel_return" value="https://www.beststoreitems.com/paypal_cancel.html" />



        	          



        	          <script language="javascript">



						   document.form.submit();



					  </script>



				   </form>	 



        	';



        	exit;



        }



    	if ((!$cart->hasProducts()) || ((!$cart->hasStock()) && (!$config->get('config_stock_checkout')))) {



      		$response->redirect($url->ssl('cart'));

    	}



    	if ($cart->hasShipping()) {



			if (!$address->has($session->get('shipping_address_id'))) {



	  			$response->redirect($url->ssl('checkout_address', 'shipping'));

    		}



		} else {



			//$session->delete('shipping_address_id');



			$session->delete('shipping_method');



		}



    	if (!$address->has($session->get('payment_address_id'))) {



      		$session->set('payment_address_id', $session->get('shipping_address_id'));

    	}



    	if (!$address->has($session->get('payment_address_id'))) {



	  		$session->set('payment_address_id', $customer->getAddressId());



    	}



    	if (!$session->get('payment_address_id')) {



	  		$response->redirect($url->ssl('checkout_address', 'payment'));

    	}





    	$language->load('controller/checkout_payment.php');



    	$template->set('title', $language->get('heading_title')); 



    	if (($request->isPost()) && ($this->validate())) {      	  	

    		

//    		print_r($_POST);exit;



			$session->set('payment_method', $request->get('payment', 'post'));	



	  		$response->redirect($url->ssl('checkout_confirm'));



    	}



    	$view = $this->locator->create('template');



    	$view->set('heading_title', $language->get('heading_title'));



    	$view->set('text_payment', $language->get('text_payment'));



    	$view->set('text_payment_to', $language->get('text_payment_to'));



    	$view->set('text_payment_address', $language->get('text_payment_address'));



    	$view->set('text_payment_method', $language->get('text_payment_method'));



    	$view->set('text_payment_secure', $language->get('text_payment_secure'));



    	$view->set('text_comments', $language->get('text_comments'));



    	$view->set('button_change_address', $language->get('button_change_address'));



    	$view->set('button_continue', $language->get('button_continue'));



    	$view->set('button_back', $language->get('button_back'));



    	$view->set('error', @$this->error['message']);



    	$view->set('action', $url->ssl('checkout_payment'));



    	$view->set('address', $address->getFormatted($session->get('payment_address_id'), '<br />'));



		$view->set('change_address', $url->ssl('checkout_address', 'payment'));



		$view->set('methods', $payment->getMethods());



    	$view->set('default', $session->get('payment_method'));



    	$view->set('comment', $session->get('comment'));



    	if ($cart->hasShipping()) {



      		$view->set('back', $url->ssl('checkout_shipping'));



    	} else {



      		$view->set('back', $url->ssl('cart'));



    	}



		$template->set('content', $view->fetch('content/checkout_payment.tpl'));



		$template->set($module->fetchPublic());



    	$response->set($template->fetch('layout.tpl'));



  	}



  	function validate() {



		$language =& $this->locator->get('language');



		$request  =& $this->locator->get('request');



    	if (!$request->has('payment', 'post')) {



	  		$this->error['message'] = $language->get('error_payment');



		}



		if (!$this->error) {



	  		return TRUE;



		} else {



	  		return FALSE;



		}



  	}  

}



?>















