<?php
/** 
 * @package   	VikRestaurants
 * @subpackage 	com_vikrestaurants
 * @author    	Matteo Galletti - e4j
 * @copyright 	Copyright (C) 2017 e4j - Extensionsforjoomla.com. All Rights Reserved.
 * @license  	http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link 		https://extensionsforjoomla.com
 */

// No direct access to this file
defined('_JEXEC') OR die('Restricted Area');

/**
 * An example payment gateway integration for VikRestaurants.
 *
 * @since 1.0
 */
class VikRestaurantsPayment
{
	/**
	 * The order information needed to complete the payment process.
	 *
	 * @var array
	 */
	private $order_info;

	/**
	 * The payment configuration.
	 *
	 * @var array
	 */
	private $params;
	
	/**
	 * Return the fields that should be filled in from the details of the payment.
	 *
	 * @return 	array 	The fields array.
	 */
	public static function getAdminParameters()
	{
		$logo_img = JURI::root().'administrator/components/com_vikrestaurants/payments/mypay/mypay-logo.jpg';

		return array(	
			'logo' => array(
				'label' => '',
				'type' => 'custom',
				'html' => '<img src="'.$logo_img.'"/>',
			),
			'merchantid' => array(
				'label' => 'Merchant ID',
				'type' => 'text',
			),
			'testmode' => array(
				'label' => 'Test Mode',
				'type' => 'select',
				'options' => array('Yes', 'No'),
			),
		);
	}
	
	/**
	 * Class constructor.
	 *
	 * @param 	array 	$order 	 The order info array.
	 * @param 	array 	$params  The payment configuration. These fields are the 
	 * 							 same of the getAdminParameters() function.
	 */
	public function __construct($order, $params = array())
	{
		$this->order_info = $order;
		$this->params = $params;
	}
	
	/**
	 * This method is invoked every time a user visits the page of a reservation with PENDING Status.
	 *
	 * @return 	void
	 */
	public function showPayment()
	{
		$merchant_id = $this->params['merchantid'];
		
		$action_url = "https://yourbankgateway.com/";
		if ($this->params['testmode'] == 'Yes') {
			$action_url = "https://test.yourbankgateway.com/";
		}

		$form = '<form action="'.$action_url.'" method="post">';
		// put here all the required fields of your gateway
		$form .= '<input type="hidden" name="your_post_data_merchantid" value="'.$merchant_id.'"/>';
		$form .= '<input type="hidden" name="your_post_data_amount" value="'.$this->order_info['total_to_pay'].'"/>';
		$form .= '<input type="hidden" name="your_post_data_notifyurl" value="'.$this->order_info['notify_url'].'"/>';
		$form .= '<input type="hidden" name="your_post_data_description" value="'.$this->order_info['transaction_name'].'"/>';
		// print a button to submit the payment form
		$form .= '<input type="submit" name="_submit" value="Pay Now!" />';
		$form .= '</form>';
		
		echo $form;
	}
	
	/**
	 * Validate the transaction details sent from the bank. 
	 * This method is invoked by the system every time the Notify URL 
	 * is visited (the one used in the showPayment() method). 
	 *
	 * @return 	array 	The array result, which MUST contain the "verified" key (1 or 0).
	 */
	public function validatePayment()
	{
		$array_result = array();
		$array_result['verified'] = 0;
		$array_result['tot_paid'] = ''; /** This value will be stored in the DB */
		
		/** In case of error the log will be sent via email to the admin */
		
		$status = $_POST['status'];
		/** Process your gateway response here */
		if ($status == 'success') {
			$array_result['verified'] = 1;
			/** Set a value for $array_result['tot_paid'] */
			$array_result['tot_paid'] = $_POST['amount'];
		} else {
			$array_result['log'] = "Transaction Error!\n".$_POST['error_msg'];
		}
		
		/** Return the array to VikRestaurants */
		return $array_result;
	}

	/**
	 * This function is called once the payment has been validated for redirect actions.
	 *
	 * @param 	boolean 	$esit 	The esit of the transaction.
	 *
	 * @return 	void
	 */
	public function afterValidation($esit = 0)
	{
		$app = JFactory::getApplication();
		
		if ($esit < 1) {
			$app->enqueueMessage('The payment was not verified, please try again.', 'error');
			$app->redirect($this->order_info['error_url']);
		} else {
			$app->enqueueMessage('Thank you! The payment was verified successfully.');
			$app->redirect($this->order_info['return_url']);
		}
		
		exit;
	}
}