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

defined('_JEXEC') or die();

use Joomla\CMS\Session\Session;
?>
<?php if (!empty($token)) : ?>
	<script type="text/javascript">
	function parseQueryString(str) {
		var vars = [];
		var arr = str.split('&');
		var pair;

		for (var i = 0; i < arr.length; i++) {
			pair = arr[i].split('=');
			vars.push(pair[0]);
			vars[pair[0]] = unescape(pair[1]);
		}

		return vars;
	}

	jQuery(document).ready(function() {
		jQuery("#tokenForm").submit();

		window.AuthorizeNetIFrame = {};

		AuthorizeNetIFrame.onReceiveCommunication = function (querystr) {
			var params = parseQueryString(querystr);

			switch (params["action"]) {
				case "successfulSave":
					break;
				case "cancel":
					window.location.href = '<?php echo $cancelUrl; ?>';
					break;
				case "resizeWindow":
					//var w = parseInt(params["width"]);
					var h = parseInt(params["height"]);
					var ifrm = document.getElementById("paymentIframe");
					//ifrm.style.width = w.toString() + "px";
					ifrm.style.height = h.toString() + "px";
					break;
				case "transactResponse":
					var transResponse = JSON.parse(params['response']);
					var transId = transResponse.transId;

					jQuery.ajax({
						url: '<?php echo $notifyUrl . '&' . Session::getFormToken() . '=1'; ?>&transaction_id=' + transId,
						success: function(r) {
							window.location.href = '<?php echo $returnUrl; ?>';
						}
					});

					break;
				}
			};
	});
	</script>

	<iframe id="paymentIframe" name="paymentIframe" width="100%" style="min-height: 600px" frameborder="0"></iframe>

	<form id="tokenForm" action="<?php echo $url; ?>" method="post" target="paymentIframe" >
		<input type="hidden" name="token" value="<?php echo $token; ?>" />
	</form>
	<?php
	// Empty the cart
	//Factory::getSession()->clear('cart', 'CMGiftCard');
	?>
<?php elseif (!empty($apiError)) : ?>
	<p><?php echo $apiError; ?></p>
<?php endif; ?>
