<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var \Magento\Payment\Block\Transparent\Iframe $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */

$params = $block->getParams();
?>
<html>
    <head>
        <?php $scriptString = '' ?>
        <?php if (isset($params['redirect'])): ?>
            <?php $scriptString .= <<<script
            window.location="{$block->escapeJs($params['redirect'])}";
script;
            ?>
        <?php elseif (isset($params['redirect_parent'])): ?>
            <?php $scriptString .= <<<script
            var require = window.parent.require;
            require(
                [
                    'jquery'
                ],
                function($) {
                    var parent = window.parent;
                    $(parent).trigger('clearTimeout');
                    parent.location="{$block->escapeJs($params['redirect_parent'])}";
                }
            );
script;
            ?>
        <?php elseif (isset($params['error_msg'])): ?>
            <?php
            $encodedMsg = /* @noEscape */ json_encode($params['error_msg']);
            $scriptString .= <<<script
            var require = window.parent.require;
            require(
                [
                    'jquery',
                    'Magento_Ui/js/model/messageList',
                    'mage/translate',
                    'Magento_Checkout/js/model/full-screen-loader'
                ],
                function($, globalMessageList, \$t, fullScreenLoader) {
                    var parent = window.parent;
                    $(parent).trigger('clearTimeout');
                    fullScreenLoader.stopLoader();
                    globalMessageList.addErrorMessage({
                        message: \$t({$encodedMsg})
                    });
                }
            );
script;
            ?>
        <?php elseif (isset($params['multishipping'])): ?>
            <?php $scriptString .= <<<script
            var require = window.parent.require;
            require(
                [
                    'jquery'
                ],
                function($) {
                    var parent = window.parent;
                    $(parent).trigger('clearTimeout');
                    $(parent.document).find('#multishipping-billing-form').submit();
                }
            );
script;
            ?>
        <?php elseif (isset($params['order_success'])): ?>
            <?php $scriptString .= <<<script
            window.parent.location = "{$block->escapeJs($params['order_success'])}";
script;
            ?>
        <?php else: ?>
            <?php $scriptString .= <<<script
            var require = window.parent.require;
            require(
                [
                    'jquery',
                    'Magento_Checkout/js/model/quote',
                    'Magento_Checkout/js/action/place-order',
                    'Magento_Checkout/js/action/redirect-on-success',
                    'Magento_Checkout/js/model/full-screen-loader'
                ],
                function($, quote, placeOrderAction, redirectOnSuccessAction, fullScreenLoader) {
                    var parent = window.parent;

                    $(parent).trigger('clearTimeout');
                    $.when(
                        placeOrderAction({'method': quote.paymentMethod().method})
                    ).done(
                        function () {
                            redirectOnSuccessAction.execute();
                        }
                    ).fail(
                        function () {
                            var parent = window.parent;
                            $(parent).trigger('clearTimeout');
                            fullScreenLoader.stopLoader();
                        }
                    );
                }
            );
script;
            ?>
        <?php endif; ?>
        <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
    </head>
    <body></body>
</html>
