<?php
/**------------------------------------------------------------------------
 * com_vikbooking - VikBooking
 * ------------------------------------------------------------------------
 * author    Alessio Gaggii - e4j - Extensionsforjoomla.com
 * copyright Copyright (C) 2015 e4j - Extensionsforjoomla.com. All Rights Reserved.
 * @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * Websites: http://www.extensionsforjoomla.com
 * Technical Support:  tech@extensionsforjoomla.com
 * ------------------------------------------------------------------------
*/

defined('_JEXEC') OR die('Restricted Area');

class VikBookingPayment {
	
	private $order_info;
	private $params;
	
	public static function getAdminParameters() {
		$logo_img = JURI::root().'administrator/components/com_vikbooking/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'),
			),
		);
	}
	
	public function __construct ($order, $params=array()) {
		$this->order_info = $order;
		$this->params = $params;
	}
	
	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'].'"/>'; //Payment response will be analyzed by the validatePayment()
		$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;

	}
	
	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 logs will be sent via email to the admin */
		$array_result['log'] = '';
		
		$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 result to VikBooking */
		return $array_result;
	}

	public function afterValidation ($esit = 0) {
	
		$mainframe = JFactory::getApplication();
		//URL to order details page
		$redirect_url = 'index.php?option=com_vikbooking&task=vieworder&sid='.$this->order_info['sid'].'&ts='.$this->order_info['ts'];
		
		if($esit < 1) {
			JError::raiseWarning('', 'The payment was not verified, please try again.');
			$mainframe->redirect($redirect_url);
		} else {
			$mainframe->enqueueMessage('Thank you! The payment was verified successfully.');
			$mainframe->redirect($redirect_url);
		}
		
		exit;
		//No page rendering
	}
}
?>