<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2022-2024 The OpenMage Contributors (https://openmage.org)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Paypal_Block_Iframe $this */
?>
<div id="checkout-paypaliframe-load" class="authentication"></div>
<script type="text/javascript">
    Review.prototype.save = function() {
        if (checkout.loadWaiting !== false) return;
        checkout.setLoadWaiting('review');

        const formData = new FormData(payment.form);
        if (this.agreementsForm) {
            const agreementFormData = new FormData(this.agreementsForm);
            for (let pair of agreementFormData.entries()) {
                formData.append(pair[0], pair[1]);
            }
        }
        formData.append('save', true);

        const params = new URLSearchParams(formData).toString();
        fetch(this.saveUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: params
        })
            .then(response => {
                return response.json().catch(() => response.text().then(text => {
                    try {
                        return JSON.parse(text);
                    } catch(e) {
                        return {};
                    }
                }));
            })
            .then(response => {
                if (response.error_messages) {
                    checkout.setLoadWaiting(false);
                }
                this.onSave({ responseJSON: response });
            })
            .catch(error => {
                checkout.ajaxFailure.call(checkout, error);
            });
    }
</script>
