<?php
/**
 * @version		3.0.0
 * @package		JomLand TicketCenter
 * @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
 * @link 		www.jomland.com
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.html.pane');

require 'jlticketpayment.php';
class JLTicketCenterESelect extends JLTicketPayment

{

	public $payment_params = array();

	private $payment_data = array();

        public $eSelect_URL = 'https://esqa.moneris.com/HPPDP/index.php';
        

	function __construct(){
	
		if (session_id() == "")
		session_start();
	}

	function buildPanel($payment_class){

		$payment_config = $this->getConfig($payment_class);

        $output = JHtml::_('tabs.start', 'slider-', array('useCookie' => 1));
        $output .= JHtml::_('tabs.panel', JText::_('Parameters'), 'panel1');
        $output .='<div id="payment_params">'."\n";
		$output .='<div id="payment_params">'."\n";

                $output .='<tr>'."\n";
		$output .='<td width="100" class="key">'."\n";
		$output .='<label for="published">'.JText::_('eSelect Test Mode').'</label>'."\n";
		$output .='</td>'."\n";
		$output .='<td>'."\n";
		$output .= JHTML::_('select.genericlist', array(0=>JText::_('JNO'),1=>JText::_('JYES')), 'params[test_mode]', null, $optKey = 'value', $optText = 'text',$payment_config->test_mode);
		//$output .= JHTML::_('select.booleanlist','params[paypal_debug]', '', $payment_config->paypal_debug);
		$output .='</td>'."\n";
		$output .='</tr>'."\n";
		
                $output .='<tr>'."\n";
		$output .='<td width="100" class="key">'."\n";
		$output .='<label for="published">'.JText::_('Test URL').'</label>'."\n";
		$output .='</td>'."\n";
		$output .='<td>'."\n";
		$output .= '<input name="params[test_url]" size="75" value="'.$payment_config->test_url.'" />'." (Don't change it unless you know the url)\n";
		$output .='</td>'."\n";
		$output .='</tr>'."\n";
                
                $output .='<tr>'."\n";
		$output .='<td width="100" class="key">'."\n";
		$output .='<label for="published">'.JText::_('Production url').'</label>'."\n";
		$output .='</td>'."\n";
		$output .='<td>'."\n";
		$output .= '<input name="params[production_url]" size="75" value="'.$payment_config->production_url.'" />'." (Don't change it unless you know the url) \n";
		$output .='</td>'."\n";
		$output .='</tr>'."\n";
                
                
		$output .='<tr>'."\n";
		$output .='<td width="100" class="key">'."\n";
		$output .='<label for="published">'.JText::_('Store Id').'</label>'."\n";
		$output .='</td>'."\n";
		$output .='<td>'."\n";
		$output .= '<input name="params[ps_store_id]" value="'.$payment_config->ps_store_id.'" />'."\n";
		$output .='</td>'."\n";
		$output .='</tr>'."\n";
                
                $output .='<tr>'."\n";
		$output .='<td width="100" class="key">'."\n";
		$output .='<label for="published">'.JText::_('HPP Key').'</label>'."\n";
		$output .='</td>'."\n";
		$output .='<td>'."\n";
		$output .= '<input name="params[hpp_key]" value="'.$payment_config->hpp_key.'" />'."\n";
		$output .='</td>'."\n";
		$output .='</tr>'."\n";
                
                $output .='<tr>'."\n";
		$output .='<td width="100" class="key">'."\n";
		$output .='<label for="published">'.JText::_('Order Prefix').'</label>'."\n";
		$output .='</td>'."\n";
		$output .='<td>'."\n";
		$output .= '<input name="params[order_prefix]" value="'.$payment_config->order_prefix.'" />'."\n";
		$output .='</td>'."\n";
		$output .='</tr>'."\n";
                

		$output .='</div>';
        $output .= JHtml::_('tabs.end');

		return $output;
	}//EOF buildPanel

        
        public function eSelectConfig(){
            $payment_config = $this->getConfig('eselect');
            $this->payment_params  = $payment_config;
            
        }

	function pross_payment($data){
            $pparams = new JLTicketCenterHelperSetup;

            $mainframe = JFactory::getApplication();
            $url = '/index.php?option=com_jlticketcenter&view=cart&format=raw&task=eselect&order_id='.$data['order_id'].'&total='.number_format(round($data['order_total'], 2),2).'&email='.$data['user_email'];

            $mainframe->redirect( $url);

	}// EOF pross

        
        
	
	public function post_payment_proccess(){

            // http://www.yousite.com/index.php?option=com_jlticketcenter&view=cart&format=raw&&task=eselectpaymentprocess
            
            $eSelect_data = $_POST;
            $eSelect_data  = JArrayHelper::toObject($eSelect_data);

            
            
            //$timestr=strtotime($eSelect_data->date_stamp. ' '.$eSelect_data->time_stamp);
            //$tran_time =strftime("%Y-%m-%d %H:%M:%S",$timestr);
            $tran_time = $eSelect_data->date_stamp. ' '.$eSelect_data->time_stamp;

            $this->eSelectConfig();
            
            if ($this->payment_params->order_prefix !==''){
                $order_id = (int)str_replace($this->payment_params->order_prefix, '',$eSelect_data->response_order_id);
            }else{
                $order_id = (int)$eSelect_data->response_order_id;
            }
            
            $data = array();
            
            require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'models'.DS.'paymentlog.php');

            $data['payment_time']   = $tran_time ;
            $data['order_id']       = $order_id;
            $data['payment_method'] = 'eSelect PayPage';
            $data['amount']         = $eSelect_data->charge_total;

            if ($eSelect_data->result != 1){

                    $data['status'] = 0;
                    $data['transactionid'] = '';

                    $model = JModelLegacy::getInstance('PaymentLog', 'JLTicketCenterModel');
                    $model->store($data);

                    return ;
            }

            // Success write to log and proccess the order
            $data['status'] = 1;
            $data['transactionid'] = $eSelect_data->bank_transaction_id;

            $model = JModelLegacy::getInstance('PaymentLog', 'JLTicketCenterModel');
            $model->store($data);


            require_once (JPATH_COMPONENT_ADMINISTRATOR.DS.'models'.DS.'order.php');
            require_once (JPATH_COMPONENT_ADMINISTRATOR.DS.'controllers'.DS.'order.php');

            $order=array();
            $order['order_id'] 		= $order_id;
            $order['order_status']	= 0;
            $order['new_status']	= 'OrderPaid';
            $order['payment_pross']	= true;
            $order['no_redirect']	= true;
            $order[JUtility::getToken()]= "1";

            JRequest::set($order,'POST');

            JLTicketCenterControllerOrder::orderProcess();
            
            //redirect to order completed
            
            $mainframe = JFactory::getApplication();
            $url = '/index.php?option=com_jlticketcenter&view=order';

            $mainframe->redirect($url);

            die;
	}//EOF
}//EOC