<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
	defined('_JEXEC') or die('Restricted access');

// Import Joomla predefined functions
	jimport( 'joomla.application.component.view' );
class jegroupbuyViewpaymentdetails  extends JView
{

	function display( $tpl = null )
	{
		$app		= JFactory::getApplication();
		$params 	= $app->getParams();
		$user	=& JFactory::getUser();
		$db	= JFactory::getDBO();
		$id = JRequest::getInt("pid","");
		$result = JRequest::getVar("result","");
		$action=JRequest::getVar('action','');
		$ptype=JRequest::getVar('ptype','');

		if($this->_layout != 'process')
		{
			if ( $user->get('guest'))
			{
				if($id > 0)
				{
					$url = base64_encode(JRoute::_('index.php?option=com_jegroupbuy&task=paymentDetails&pid='.$id));
				}else
				{
					$url = base64_encode(JRoute::_('index.php?option=com_jegroupbuy&view=jegroupbuy'));
				}
				$link = JRoute::_('index.php?option=com_user&view=login&return='.$url);
				$app->Redirect($link);
			}

			$val["id"] = $id;
			$options = array( $val);


			JPluginHelper::importPlugin('jegroupbuypayment');
			$dispatcher =& JDispatcher::getInstance();
			$PaymentMethodDetails = $dispatcher->trigger( 'onPaymentMethodList',$options);
			$plugin_info = JPluginHelper::getPlugin('jegroupbuypayment', $plugin=null);
			$PaymentMethodMessage="";
			$message="";
			if($action=="showresult")
			{
				$inv_arg["inv_id"] = $id;
				$arg = ($inv_arg);
				$PaymentMethodMessage = $dispatcher->trigger( 'onAfterFailedPayment',$arg);
				$message=$this->getPaymentPluginStatus($PaymentMethodMessage,$plugin_info,$ptype);
			}

			$this->assignRef("PaymentMethodDetails",$PaymentMethodDetails);
			$this->assignRef("plugin_info",$plugin_info);
			$this->assignRef("result",$result);

		}else
		{

			JPluginHelper::importPlugin("jegroupbuypayment");
			$dispatcher =& JDispatcher::getInstance();
			$results = $dispatcher->trigger( 'onProcessPayment');

			$text = trim(implode("\n", $results));
			echo $text;
		}




		parent::display( $tpl );
	}

	/**
	 * Prepares the document
	 */

	function limit_characters ( $str, $n )
	{
		if ( strlen ( $str ) <= $n )
		{
			return $str;
		}
		else {
			return substr ( $str, 0, $n ) . '...';
		}
	}

	function getPaymentPluginStatus($PaymentMethodMessage,$plugin_infos,$ptype)
	{
		$i = 0;
		foreach($PaymentMethodMessage as $PaymentMethodMessages)
		{

			if($plugin_infos[$i]->name == $ptype)
			{
				return $PaymentMethodMessages;
			}
			$i++;
		}
	}
}
