<?php
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;
use Joomla\CMS\Language\Text;
/**
 * @version		3.8
 * @package		Joomla
 * @subpackage	Joom Donation
 * @author  Tuan Pham Ngoc
 * @copyright	Copyright (C) 2009 - 2016 Ossolution Team
 * @license		GNU/GPL, see LICENSE.php
 */
defined( '_JEXEC' ) or die ;

class os_payments {	
	/**
	 * Get list of payment methods
	 *
	 * @return array
	 */
	public static function getPaymentMethods($campaignId = 0)
    {
		static $methods ;			
		if (!$methods)
		{
			define('JPAYMENT_METHODS_PATH', JPATH_ROOT.'/components/com_jdonation/payments/') ;
			$db = Factory::getDbo() ;
			$extraSql = '';
			if($campaignId > 0)
            {
                $db->setQuery("Select payment_plugins from #__jd_campaigns where id = '$campaignId'");
                $payment_plugins = $db->loadResult();
                if($payment_plugins != '')
                {
                    $extraSql = ' and id in ('.trim($payment_plugins).')';
                }
            }

			
			$sql = 'SELECT * FROM #__jd_payment_plugins WHERE published=1 '.$extraSql.' and `access` IN (' . implode(',', Factory::getUser()->getAuthorisedViewLevels()) . ') ORDER BY ordering' ;
			$db->setQuery($sql) ;
			$rows = $db->loadObjectList();
			foreach ($rows as $row)
			{
				$paymentPluginPath = JPAYMENT_METHODS_PATH.$row->name.'.php' ;
				if(file_exists($paymentPluginPath))
				{
					require_once JPAYMENT_METHODS_PATH.$row->name.'.php';
					$method = new $row->name(new Registry($row->params));
					$method->setTitle($row->title);
					$methods[] = $method ;					
				}					
			}
		}
				
		return $methods ;
	}
	/**
	 * Write the javascript objects to show the page
	 *
	 * @return string
	 */		
	public static function writeJavascriptObjects()
    {
		$methods =  os_payments::getPaymentMethods();
		$jsString = " methods = new PaymentMethods();\n" ;			
		if (count($methods))
		{
			foreach ($methods as $method)
			{
				$jsString .= " method = new PaymentMethod('".$method->getName()."',".$method->getCreditCard().",".$method->getCardType().",".$method->getCardCvv().",".$method->getCardHolderName().", ".$method->getEnableRecurring().");\n" ;
				$jsString .= " methods.Add(method);\n";								
			}
		}
		echo $jsString ;
	}
	/**
	 * Load information about the payment method
	 *
	 * @param string $name Name of the payment method
	 */
	public static  function loadPaymentMethod($name)
    {
		$db = Factory::getDbo() ;
		$sql = 'SELECT * FROM #__jd_payment_plugins WHERE name="'.$name.'"';
		$db->setQuery($sql) ;
		return $db->loadObject();
	}
	/**
	 * Get default payment gateway
	 *
	 * @return string
	 */
	public static function getDefautPaymentMethod($campaignId = 0)
    {
		$db                     = Factory::getDbo() ;
        if($campaignId > 0)
        {
            $extraSql           = '';
            $db->setQuery("Select payment_plugins from #__jd_campaigns where id = '$campaignId'");
            $payment_plugins    = $db->loadResult();
            if($payment_plugins != '')
            {
                $extraSql       = ' and id in ('.trim($payment_plugins).')';
            }
        }
		$sql                    = 'SELECT name FROM #__jd_payment_plugins WHERE published=1 '.$extraSql.' and `access` IN (' . implode(',', Factory::getUser()->getAuthorisedViewLevels()) . ') ORDER BY ordering LIMIT 1';
		$db->setQuery($sql) ;
		return $db->loadResult();	
	}
	/**
	 * Get the payment method object based on it's name
	 *
	 * @param string $name
	 * @return object
	 */		
	public static function getPaymentMethod($name, $campaignId = 0)
    {
		$methods = os_payments::getPaymentMethods($campaignId) ;
		foreach ($methods as $method)
		{
			if ($method->getName() == $name)
			{
				return $method ;		
			}
		}
		return null ;
	}

	/**
	 * Check to see whether the ideal payment plugin installed and activated
	 * @return boolean
	 */
	static public function sisowEnabled()
    {
		$db = & Factory::getDBO();
		$sql = 'SELECT COUNT(id) FROM #__jd_payment_plugins WHERE name="os_sisow" AND published=1';
		$db->setQuery($sql) ;
		$total = $db->loadResult() ;
		if ($total) {
			require_once JPATH_ROOT.'/components/com_jdonation/payments/sisow/sisow.cls5.php';
			return true ;
		} else {
			return false ;
		}
	}
	/**
	 * Get list of banks for ideal payment plugin
	 * @return array
	 */
	public static function getBankLists()
    {
		$sisowPlugin = self::loadPaymentMethod('os_sisow');		
		$params = new Registry($sisowPlugin->params) ;		
		$paymentType = $params->get('payment_type');
		$mode = $params->get('ideal_mode',0);
		if (!$paymentType)
		{
			?>
			<div class="control-group" id="tr_bank_lists" style="display:none;">
				<label class="control-label" for="card_holder_name">
					<?php echo Text::_('JD_SELECT_BANK'); ?><span class="required">*</span>
				</label>
				<div class="controls">
					<select name="issuer_id" id="issuer_id" class="input-large">
						<?php
						if ($mode)
						{
							?>
							<script type="text/javascript" src="https://www.sisow.nl/Sisow/iDeal/issuers2.js"></script>
						<?php
						}
						else
						{
							?>
							<option value="99">Sisow Bank (test)</option>
						<?php
						}
						?>
					</select>
				</div>
			</div>
			<?php
		}
	}
}
?>
