<commit_before><?php

/**
 * Checkout.com
 * Authorized and regulated as an electronic money institution
 * by the UK Financial Conduct Authority (FCA) under number 900816.
 *
 * PHP version 7
 *
 * @category  Magento2
 * @package   Checkout.com
 * @author    Platforms Development Team <<EMAIL>>
 * @copyright 2010-present Checkout.com
 * @license   https://opensource.org/licenses/mit-license.html MIT License
 * @link      https://docs.checkout.com/
 */

use CheckoutCom\Magento2\Block\Adminhtml\System\Config\Field\AbstractCallbackUrl;
use CheckoutCom\Magento2\Model\Config\Backend\Source\ConfigService;

/** @var AbstractCallbackUrl $block */
?>

<?php echo $block->getElementHtml(); ?>
<p id="webhook-message" class="webhook-message <?= $block->getMessageClass(); ?>"><?= $block->getMessage(); ?></p>
<script>
require([
    'jquery',
    'prototype',
], function(jQuery) {
    let label = document.getElementById('row_payment_us_checkoutcom_module_account_settings_private_shared_key').getElementsByClassName('label');
    let service = document.getElementsByName(
        'groups[checkoutcom][groups][module][groups][account_settings][fields][service][value]');
    let secretKey = document.getElementsByName(
        'groups[checkoutcom][groups][module][groups][account_settings][fields][secret_key][value]');
    let publicKey = document.getElementsByName(
        'groups[checkoutcom][groups][module][groups][account_settings][fields][public_key][value]');
    const LIVE_SECRET_KEY_REGEX = /^<KEY>;
    const SANDBOX_SECRET_KEY_REGEX = /^sk_test_?(\w{8})-(\w{4})-(\w{4})-(\w{4})-(\w{12})$/;

    function getService() {
        return document.getElementsByName(
            'groups[checkoutcom][groups][module][groups][account_settings][fields][service][value]')[0].value;
    }

    function processNAS() {
        label[0].innerText = "<?= __('Authorization Header Key') ?>";
        document.getElementById('webhook_auto_set').style.display = 'none';
        document.getElementById('webhook-message').style.display = 'none';
        document.getElementById('webhook_spinner').style.display = 'none';
        document.getElementById('webhook_nas').style.display = 'block';
    }

    function processABC() {
        label[0].innerText = "<?= __('Private Shared Key') ?>";
        document.getElementById('webhook_nas').style.display = 'none';
        if (checkFormFields()) {
            document.getElementById('webhook_auto_set').style.display = 'block';
        } else {
            document.getElementById('webhook_auto_set').style.display = 'none';
            document.getElementById('webhook-message').style.display = 'block';
        }
    }

    function checkService() {
        if (getService() === "<?= ConfigService::SERVICE_NAS ?>") {
            processNAS();
        } else if (getService() === "<?= ConfigService::SERVICE_ABC ?>") {
            processABC();
        }
    }

    function checkFormFields() {
        let environment = document.getElementsByName(
            'groups[checkoutcom][groups][module][groups][global_settings][fields][environment][value]')[0].value;
        if (environment === '1') {
            if (!secretKey[0].value.match(SANDBOX_SECRET_KEY_REGEX)) {
                return false;
            }
        } else {
            if (!secretKey[0].value.match(LIVE_SECRET_KEY_REGEX)) {
                return false;
            }
        }
        return true;
    }

    window.addEventListener('DOMContentLoaded', event => {
        checkService();
    });

    service[0].addEventListener('input', event => {
        checkService();
    });

    secretKey[0].addEventListener('input', event => {
        checkService();
    });

    jQuery('#webhook_button').click(function() {
        var params = {
            scope: '<?= $block->getScope(); ?>',
            scope_id: '<?= $block->getScopeId(); ?>',
            webhook_url: '<?= $block->getWebhookUrl(); ?>',
            secret_key: secretKey[0].value,
            public_key: publicKey[0].value,
        };
        new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
            parameters: params,
            loaderArea: false,
            asynchronous: true,
            onCreate: function() {
                document.getElementById('webhook_spinner').style.display = 'initial';
            },
            onSuccess: function(response) {
                if (response.responseJSON.success) {
                    let privateSharedKey = document.getElementsByName(
                        'groups[checkoutcom][groups][module][groups][account_settings][fields][private_shared_key][value]');
                    privateSharedKey[0].value = response.responseJSON.privateSharedKey;
                    document.getElementById('webhook-message').textContent = "<?= __('Your webhook is all set!'); ?>";
                    document.getElementById('webhook-message').classList.toggle('webhook-set');
                    document.getElementById('webhook_auto_set').style.display = 'none';
                } else {
                    document.getElementById('webhook_spinner').style.display = 'none';
                    alert("<?= __('Error! Could not set webhooks. Please check your secret key.'); ?>");
                }
            },
        });
    });
});
</script>
<div id="webhook_auto_set" style="display: <?= ($block->getHidden() == true) ? 'none' : 'block' ?> ">
    <?php echo $block->getButtonHtml() ?>
    <span id="webhook_span">
            <img id="webhook_spinner" style="display: none" alt="Processing Spinner" style="margin:0 5px"
                 src="<?php echo $block->getViewFileUrl('images/process_spinner.gif') ?>"/>
        </span>
</div>

<div id="webhook_nas" style="display: <?= ($block->getHidden() == true) ? 'none' : 'block' ?> ">
    <span id="webhook_span_nas">
        <br>
        <p> <?= __('In order to set your webhook for the NAS mode, please follow these steps:') ?></p>
        <br>
        <p> <?= __('1- Create your Notification in Checkout Dashboard: Notifications - Docs') ?></p>
        <p><?= __('2- Select all the events in the Notification') ?></p>
        <p><?= __('3- Fill the Notification URL from Magento "Webhook URL"') ?></p>
        <p><?= __(
                '4- Finally, copy the Authorization Header Key from your Notification into the "Authorization Header Key" field in Magento and save the configuration'
            ) ?></p>
        </span>
</div><commit_msg>feature/CHECMAG2003-11-41 : Add link to documentation<commit_after><?php

/**
 * Checkout.com
 * Authorized and regulated as an electronic money institution
 * by the UK Financial Conduct Authority (FCA) under number 900816.
 *
 * PHP version 7
 *
 * @category  Magento2
 * @package   Checkout.com
 * @author    Platforms Development Team <<EMAIL>>
 * @copyright 2010-present Checkout.com
 * @license   https://opensource.org/licenses/mit-license.html MIT License
 * @link      https://docs.checkout.com/
 */

use CheckoutCom\Magento2\Block\Adminhtml\System\Config\Field\AbstractCallbackUrl;
use CheckoutCom\Magento2\Model\Config\Backend\Source\ConfigService;

/** @var AbstractCallbackUrl $block */
?>

<?php echo $block->getElementHtml(); ?>
<p id="webhook-message" class="webhook-message <?= $block->getMessageClass(); ?>"><?= $block->getMessage(); ?></p>
<script>
require([
    'jquery',
    'prototype',
], function(jQuery) {
    let label = document.getElementById('row_payment_us_checkoutcom_module_account_settings_private_shared_key').getElementsByClassName('label');
    let service = document.getElementsByName(
        'groups[checkoutcom][groups][module][groups][account_settings][fields][service][value]');
    let secretKey = document.getElementsByName(
        'groups[checkoutcom][groups][module][groups][account_settings][fields][secret_key][value]');
    let publicKey = document.getElementsByName(
        'groups[checkoutcom][groups][module][groups][account_settings][fields][public_key][value]');
    const LIVE_SECRET_KEY_REGEX = /^<KEY>(\w{4})-(\w{4})-(\w{12})$/;
    const SANDBOX_SECRET_KEY_REGEX = /^sk_test_?(\w{8})-(\w{4})-(\w{4})-(\w{4})-(\w{12})$/;

    function getService() {
        return document.getElementsByName(
            'groups[checkoutcom][groups][module][groups][account_settings][fields][service][value]')[0].value;
    }

    function processNAS() {
        label[0].innerText = "<?= __('Authorization Header Key') ?>";
        document.getElementById('webhook_auto_set').style.display = 'none';
        document.getElementById('webhook-message').style.display = 'none';
        document.getElementById('webhook_spinner').style.display = 'none';
        document.getElementById('webhook_nas').style.display = 'block';
    }

    function processABC() {
        label[0].innerText = "<?= __('Private Shared Key') ?>";
        document.getElementById('webhook_nas').style.display = 'none';
        if (checkFormFields()) {
            document.getElementById('webhook_auto_set').style.display = 'block';
        } else {
            document.getElementById('webhook_auto_set').style.display = 'none';
            document.getElementById('webhook-message').style.display = 'block';
        }
    }

    function checkService() {
        if (getService() === "<?= ConfigService::SERVICE_NAS ?>") {
            processNAS();
        } else if (getService() === "<?= ConfigService::SERVICE_ABC ?>") {
            processABC();
        }
    }

    function checkFormFields() {
        let environment = document.getElementsByName(
            'groups[checkoutcom][groups][module][groups][global_settings][fields][environment][value]')[0].value;
        if (environment === '1') {
            if (!secretKey[0].value.match(SANDBOX_SECRET_KEY_REGEX)) {
                return false;
            }
        } else {
            if (!secretKey[0].value.match(LIVE_SECRET_KEY_REGEX)) {
                return false;
            }
        }
        return true;
    }

    window.addEventListener('DOMContentLoaded', event => {
        checkService();
    });

    service[0].addEventListener('input', event => {
        checkService();
    });

    secretKey[0].addEventListener('input', event => {
        checkService();
    });

    jQuery('#webhook_button').click(function() {
        var params = {
            scope: '<?= $block->getScope(); ?>',
            scope_id: '<?= $block->getScopeId(); ?>',
            webhook_url: '<?= $block->getWebhookUrl(); ?>',
            secret_key: secretKey[0].value,
            public_key: publicKey[0].value,
        };
        new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
            parameters: params,
            loaderArea: false,
            asynchronous: true,
            onCreate: function() {
                document.getElementById('webhook_spinner').style.display = 'initial';
            },
            onSuccess: function(response) {
                if (response.responseJSON.success) {
                    let privateSharedKey = document.getElementsByName(
                        'groups[checkoutcom][groups][module][groups][account_settings][fields][private_shared_key][value]');
                    privateSharedKey[0].value = response.responseJSON.privateSharedKey;
                    document.getElementById('webhook-message').textContent = "<?= __('Your webhook is all set!'); ?>";
                    document.getElementById('webhook-message').classList.toggle('webhook-set');
                    document.getElementById('webhook_auto_set').style.display = 'none';
                } else {
                    document.getElementById('webhook_spinner').style.display = 'none';
                    alert("<?= __('Error! Could not set webhooks. Please check your secret key.'); ?>");
                }
            },
        });
    });
});
</script>
<div id="webhook_auto_set" style="display: <?= ($block->getHidden() == true) ? 'none' : 'block' ?> ">
    <?php echo $block->getButtonHtml() ?>
    <span id="webhook_span">
            <img id="webhook_spinner" style="display: none" alt="Processing Spinner" style="margin:0 5px"
                 src="<?php echo $block->getViewFileUrl('images/process_spinner.gif') ?>"/>
        </span>
</div>

<div id="webhook_nas" style="display: <?= ($block->getHidden() == true) ? 'none' : 'block' ?> ">
    <span id="webhook_span_nas">
        <br>
        <p> <?= __('In order to set your webhook for the NAS mode, please follow these steps:') ?></p>
        <br>
        <p> <?= __('1- Create your Notification in Checkout Dashboard: ') ?> <a
                href="https://www.checkout.com/docs/four/dashboard/notifications#Create_a_notification"><?= __('Notifications - Docs') ?></a></p>
        <p><?= __('2- Select all the events in the Notification') ?></p>
        <p><?= __('3- Fill the Notification URL from Magento "Webhook URL"') ?></p>
        <p><?= __(
                '4- Finally, copy the Authorization Header Key from your Notification into the "Authorization Header Key" field in Magento and save the configuration'
            ) ?></p>
        </span>
</div>
{'fraction_non_alphanumeric': 0.16186032280272317, 'fraction_numerical': 0.006731431194064102, 'mean_word_length': 2.8657599053814313, 'pattern_counts': {'":': 0, '<': 108, '<?xml version=': 0, '>': 136, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2766392', 'n_tokens_mistral': 3758, 'n_tokens_neox': 3701, 'n_words': 665}