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

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Plugin\PluginHelper;

/**
 * Model for checkout.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardModelCheckout extends BaseDatabaseModel
{
	/**
	 * Method to get checkout form.
	 *
	 * @return  array()
	 *
	 * @since   1.0.0
	 */

	public function getForm()
	{
		$app = Factory::getApplication();
		$session = Factory::getSession();
		$cart = $session->get('cart', array(), 'CMGiftCard');
		$billing = $app->getUserState('com_cmgiftcard.shopping_cart.data');

		if (empty($cart['items']))
			return array();

		$data = $cart;
		$data['total'] = 0;
		$data['billing'] = $billing;

		foreach ($data['items'] as $item)
		{
			$data['total'] += $item['amount'];
		}

		PluginHelper::importPlugin('cmgiftcard');
		$payments = $app->triggerEvent('onCMGCGetPaymentForm', array($data));

		$form = '';

		foreach ($payments as $payment)
		{
			if ($payment === false)
				continue;

			$form = $payment;
		}

		return $form;
	}
}
