<?php
/**
 * @package     CMGiftCard
 * @subpackage  plg_cmgiftcard_stripe
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

require_once JPATH_ROOT . '/administrator/components/com_cmgiftcard/helpers/cmgiftcard.php';

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Language\Text;

/**
 * Base class for CMGiftCard payment plugin.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       2.1.0
 */
abstract class CMGiftCardPlugin extends CMSPlugin
{
	public $gatewayId;

	public $gatewayName;

	/**
	 * Constructor
	 *
	 * @param   object  &$subject  The object to observe
	 * @param   array   $config    An optional associative array of configuration settings.
	 *                             Recognized key values include 'name', 'group', 'params', 'language'
	 *                             (this list is not meant to be comprehensive).
	 *
	 * @since   2.1.0
	 */
	public function __construct(&$subject, $config = array())
	{
		$lang = Factory::getLanguage();
		$lang->load('plg_cmgiftcard_' . $this->gatewayId, JPATH_SITE . '/plugins/cmgiftcard/' . $this->gatewayId);

		parent::__construct($subject, $config);
	}

	/**
	 * Get payment plugin's information.
	 *
	 * @return  array
	 *
	 * @since   2.1.0
	 */
	public function onCMGCGetPaymentIdentity()
	{
		$name = $this->params->get('name', '');

		if (empty($name))
		{
			$name = Text::_($this->gatewayName);
		}

		$payment = [
			'id'	=> $this->gatewayId,
			'name'	=> $name,
		];

		return $payment;
	}

	/**
	 * Log callback data.
	 *
	 * @param   array    $data     Callback data.
	 * @param   boolean  $isValid  True if callback data is valid.
	 *
	 * @return  void
	 *
	 * @since   2.1.0
	 */
	public function logNotification($data, $isValid)
	{
		$logFolderPath = Factory::getConfig()->get('log_path');

		$logFilePath = $logFolderPath . '/plg_cmgiftcard_' . $this->gatewayId . '.php';

		if (!File::exists($logFilePath))
		{
			$dummy = "<?php die(); ?>\n";
			File::write($logFilePath, $dummy);
		}
		else
		{
			if (@filesize($logFilePath) > 1048756)
			{
				$altLogFilePath = $logFolderPath . '/plg_cmgiftcard_' . $this->gatewayId . '_1.php';

				if (File::exists($altLogFilePath))
				{
					File::delete($altLogFilePath);
				}

				File::copy($logFilePath, $altLogFilePath);
				File::delete($logFilePath);
				$dummy = "<?php die(); ?>\n";
				File::write($logFilePath, $dummy);
			}
		}

		$logData = file_get_contents($logFilePath);

		if ($logData === false)
			$logData = '';

		if ($isValid)
		{
			$logData .= "\n" . str_repeat('-', 40) . ' VALID ' . str_repeat('-', 40);
		}
		else
		{
			$logData .= "\n" . str_repeat('-', 10) . ' INVALID! FRAUD ATTEMPT OR INVALID NOTIFICATION! ' . str_repeat('-', 10);
		}

		$logData .= "\nDate/time : " . gmdate('Y-m-d H:i:s') . " GMT\n\n";

		foreach ($data as $key => $value)
		{
			$logData .= '  ' . str_pad($key, 30, ' ') . $value . "\n";
		}

		$logData .= "\n";

		File::write($logFilePath, $logData);
	}

	/**
	 * Generate payment form.
	 *
	 * @param   array  $data  Billing data.
	 *
	 * @return  array
	 *
	 * @since   2.1.0
	 */
	abstract public function onCMGCGetPaymentForm($data);

	/**
	 * Handle callback from payment service.
	 *
	 * @return  boolean
	 *
	 * @since   2.1.0
	 */
	abstract public function onCMGCValidateCallback();
}