/*
 * ==========================================================
 * ADMINISTRATION SCRIPT
 * ==========================================================
 *
 * Main JavaScript admin file. © 2022-2024 cryptopayment.network. All rights reserved.
 *  
 */

'use strict';
(function () {

    var body;
    var main;
    var timeout;
    var areas;
    var active_area;
    var active_area_id;
    var transactions_table;
    var transactions_filters = [false, false, false, false, false];
    var transaction_statuses = { P: 'Pending', C: 'Completed', R: 'Refunded', X: 'Underpayment' };
    var pagination = 0;
    var pagination_count = true;
    var today = new Date();
    var CRPAY_CHECKOUTS = false;
    var responsive = window.innerWidth < 769;
    var datepicker = false;
    var datepicker_loaded = false;
    var date_picker_settings = {
        format: 'dd/mm/yyyy',
        maxDate: new Date(),
        dateDelimiter: ' - ',
        separator: ' - ',
        clearBtn: true,
    };
    var WP;
    var _ = window._query;
    var refund_timeout;
    var upload_input;
    var upload_target;
    var confirm_on_success;

    /*
    * ----------------------------------------------------------
    * CRPAYTransactions
    * ----------------------------------------------------------
    */

    var CRPAYTransactions = {

        get: function (onSuccess, search = false, status = false, cryptocurrency = false, date_range = false, checkout_id = false) {
            ajax('get-transactions', { pagination: pagination, search: search, status: status, cryptocurrency: cryptocurrency, date_range: date_range, checkout_id: checkout_id }, (response) => {
                pagination++;
                onSuccess(response);
            });
        },

        print: function (onSuccess, search = transactions_filters[0], status = transactions_filters[1], cryptocurrency = transactions_filters[2], date_range = transactions_filters[3], checkout_id = transactions_filters[4]) {
            this.get((response) => {
                let code = '';
                pagination_count = response.length;
                for (var i = 0; i < pagination_count; i++) {
                    let transaction = response[i];
                    let from = transaction.from ? `<a href="${CRPAYAdmin.explorer(transaction.cryptocurrency, transaction.from)}" target="_blank" class="crpay-link">${transaction.from}</a>` : '';
                    let amount = transaction.currency.toUpperCase() + ' ' + transaction.amount_fiat;
                    let vat = '';
                    let notes = transaction.description.replace(/\[|\]|"/g, '');
                    let responsive_labels = ['', '', '', '', '', ''];
                    let code_shop = '';
                    let amounts = [transaction.amount ? transaction.amount + ' ' + CRYPTOPayment.baseCode(transaction.cryptocurrency.toUpperCase()) : (transaction.amount_fiat ? amount : crpay_('Free')), transaction.amount ? amount : (transaction.amount_fiat ? slugToString(CRYPTOPayment.baseCode(transaction.cryptocurrency)) : '')];
                    if (responsive) {
                        responsive_labels = ['ID', 'Date', 'From', 'To', 'Status', 'Amount'];
                        for (var y = 0; y < responsive_labels.length; y++) {
                            responsive_labels[y] = '<div class="crpay-label">' + crpay_(responsive_labels[y]) + '</div>';
                        }
                    }
                    if (transaction.vat_details) {
                        vat = JSON.parse(transaction.vat_details);
                        vat = ` data-vat="${transaction.currency} ${vat.amount} (${vat.percentage}%, ${vat.country})"`;
                    }
                    if (CRPAY_ADMIN_SETTINGS.apps.includes('shop')) {
                        code_shop = ` data-license-key="${transaction.license_key ? transaction.license_key : ''}" data-license-key-status="${transaction.license_key_status ? transaction.license_key_status : ''}" data-customer-id="${transaction.customer_id ? transaction.customer_id : ''}"`;
                    }
                    code += `<tr data-id="${transaction.id}"${code_shop} data-checkout-id="${transaction.checkout_id}" data-title="${transaction.title}" data-currency="${transaction.currency}" data-cryptocurrency="${transaction.cryptocurrency}" data-hash="${transaction.hash}" data-notes="${notes.replace(/,/g, '<br>')}" data-status="${transaction.status}"${transaction.billing ? ' data-invoice="true"' : ''}${vat} data-discount-code="${transaction.discount_code ? transaction.discount_code : ''}" data-amount="${transaction.amount_fiat ? transaction.amount_fiat : transaction.amount}" data-type="${transaction.type ? transaction.type : 1}"><td class="crpay-td-id">${responsive_labels[0]}${transaction.id}</td><td class="crpay-td-time">${responsive_labels[1]}<div class="crpay-title">${CRYPTOPayment.beautifyTime(transaction.creation_time, true)}</div></td><td class="crpay-td-from">${from ? responsive_labels[2] : ''}${from}</td><td class="crpay-td-to">${transaction.to ? responsive_labels[3] : ''}${transaction.to ? `<a href="${CRPAYAdmin.explorer(transaction.cryptocurrency, transaction.to)}" target="_blank" class="crpay-link">${transaction.to}</a>` : ''}</td><td class="crpay-td-status">${responsive_labels[4]}<span class="crpay-status-${transaction.status}">${crpay_(transaction_statuses[transaction.status])}</span></td><td class="crpay-td-amount">${responsive_labels[5]}<div class="crpay-title"><div>${amounts[0]}</div><div>${!transaction.cryptocurrency || !amounts[1].includes(transaction.cryptocurrency.toUpperCase()) ? amounts[1] : ''}</div></div></td><td><i class="crpay-transaction-menu-btn crpay-icon-menu"></i></td></tr>`;
                }
                print(transactions_table.find('tbody'), code, true);
                if (onSuccess) {
                    onSuccess(response);
                }
            }, search, status, cryptocurrency, date_range, checkout_id);
        },

        query: function (icon = false) {
            if (loading(transactions_table)) return;
            transactions_table.find('tbody').html('');
            pagination = 0;
            transactions_filters[0] = _(main).find('#crpay-search-transactions').val().toLowerCase().trim();
            transactions_filters[1] = _(main).find('.crpay-filter-status li.crpay-active').data('value');
            transactions_filters[2] = _(main).find('.crpay-filter-cryptocurrency li.crpay-active').data('value');
            transactions_filters[3] = datepicker ? datepicker.getDates('yyyy-mm-dd') : false;
            transactions_filters[4] = _(main).find('.crpay-filter-checkout li.crpay-active').data('value');
            this.print(() => {
                if (icon) {
                    loading(icon, false);
                }
                loading(transactions_table, false);
            });
        },

        download: function (onSuccess) {
            ajax('download-transactions', { search: transactions_filters[0], status: transactions_filters[1], cryptocurrency: transactions_filters[2], date_range: transactions_filters[3], checkout_id: transactions_filters[4] }, (response) => {
                onSuccess(response);
            });
        }
    }

    /*
    * ----------------------------------------------------------
    * CRPAYCheckout
    * ----------------------------------------------------------
    */

    var CRPAYCheckout = {

        row: function (checkout) {
            return `<tr data-checkout-id="${checkout.id}"><td><div class="crpay-title"><span>${checkout.id}</span><span>${checkout.title}</span></div></td><td><div class="crpay-text">${checkout.currency ? checkout.currency : CRPAY_CURRENCY} ${checkout.price}</div></td></tr>`;
        },

        embed: function (id = false) {
            let index = WP ? 3 : 2;
            for (var i = 0; i < index; i++) {
                let elements = active_area.find('#crpay-checkout-' + (i == 0 ? 'payment-link' : (i == 1 ? 'embed-code' : 'shortcode'))).find('div, i');
                if (id) {
                    let content = i == 0 ? `${CRPAY_CLOUD.custom_domain ? CRPAY_CLOUD.custom_domain : CRPAY_URL}${CRPAY_CLOUD.cloud ? '/checkout/' : (CRPAY_ADMIN_SETTINGS.url_rewrite_checkout ? CRPAY_ADMIN_SETTINGS.url_rewrite_checkout : '/vendor/cryptd/pay.php?checkout_id=')}${id}${CRPAY_CLOUD.custom_domain ? '' : cloudURL()}` : (i == 1 ? `<div data-cryptopayment="${id}"></div>${CRPAY_CLOUD.cloud ? ' <script id="cryptopayment" src="' + CRPAY_URL + 'js/client.js?cloud=' + CRPAY_CLOUD.cloud + '"></script>' : ''}` : `[cryptopayment id="${id}"]`);
                    content = content.replace('//checkout', '/checkout');
                    _(elements.e[0]).html(i == 0 ? `<a href="${content}" target="_blank">${content}</a>` : content.replace(/</g, '&lt;'));
                    _(elements.e[1]).data('text', window.btoa(content));
                } else {
                    _(elements.e[0]).html('');
                    _(elements.e[1]).data('text', '');
                }
            }
        },

        get: function (id, remove = false) {
            for (var i = 0; i < CRPAY_CHECKOUTS.length; i++) {
                if (id == CRPAY_CHECKOUTS[i].id) {
                    if (remove) {
                        CRPAY_CHECKOUTS.splice(i, 1);
                        return true;
                    }
                    return CRPAY_CHECKOUTS[i];
                }
            }
            return false;
        }
    }

    /*
    * ----------------------------------------------------------
    * CRPAYAdmin
    * ----------------------------------------------------------
    */

    var CRPAYAdmin = {
        active_element: false,

        card: function (message, type = false) {
            var card = main.find('.crpay-info-card');
            card.removeClass('crpay-info-card-error crpay-info-card-warning crpay-info-card-info');
            if (!type) {
                clearTimeout(timeout);
            } else if (type == 'error') {
                card.addClass('crpay-info-card-error');
            } else {
                card.addClass('crpay-info-card-info');
            }
            card.html(crpay_(message));
            timeout = setTimeout(() => { card.html('') }, 5000);
        },

        error: function (message, loading_area = false) {
            window.scrollTo({ top: 0, behavior: 'smooth' });
            main.find('.crpay-info').html(message);
            if (loading_area) {
                loading(loading_area, false);
            }
        },

        confirm: function (message, onSuccess, loading_element, ignore = false) {
            if (ignore) {
                return onSuccess();
            }
            CRYPTOPayment.lightbox(crpay_('Are you sure?'), '<div class="crpay-text">' + crpay_(message) + '</div>', [['crpay-confirm-btn', 'Confirm'], ['crpay-cancel-btn', 'Cancel']], 'confirm');
            confirm_on_success = [onSuccess, loading_element];
        },

        balance: function (area) {
            if (!loading(area)) {
                let table = main.find('#crpay-table-balances tbody');
                if (!table.e.length) return;
                if (!table.html()) area.addClass('crpay-loading-first');
                ajax('get-balances', {}, (response) => {
                    let code = '';
                    let balances = response.balances;
                    main.find('#crpay-balance-total').html(`${CRPAY_CURRENCY} ${response.total}`);
                    for (var key in balances) {
                        let img = key in response.token_images ? response.token_images[key] : `${CRPAY_URL}media/icon-${key}.svg`;
                        code += `<tr data-cryptocurrency="${key}"><td><div class="crpay-flex"><img src="${img}" /> ${balances[key].name}${CRYPTOPayment.network(key, true, true)}</div></td><td><div class="crpay-balance crpay-title">${balances[key].amount} ${CRYPTOPayment.baseCode(key).toUpperCase()}</div><div class="crpay-text">${CRPAY_CURRENCY} ${balances[key].fiat}</div></td></tr>`;
                    }
                    code ? table.html(code) : table.parent().html('<p class="crpay-text">Add your addresses from the Settings area.</p>');
                    area.removeClass('crpay-loading-first');
                    loading(area, false);
                });
            }
        },

        explorer: function (cryptocurrency, value, type = 'address') {
            let explorer = '';
            let replace = type == 'address' ? 'address' : (['ltc', 'usdt_tron'].includes(cryptocurrency) ? 'transaction' : 'tx');
            if (value.indexOf('cus_') === 0) {
                cryptocurrency = 'stripe';
                replace = value;
            }
            switch (cryptocurrency) {
                case 'btc':
                    explorer = CRPAY_ADMIN_SETTINGS.testnet_btc ? 'https://mempool.space/testnet/{R}/{V}' : 'https://www.blockchain.com/btc/{R}/{V}';
                    break;
                case 'eth':
                    explorer = 'https://www.blockchain.com/eth/{R}/{V}';
                    break;
                case 'doge':
                    explorer = 'https://dogechain.info/{R}/{V}';
                    break;
                case 'link':
                case 'bat':
                case 'shib':
                case 'usdc':
                case 'usdt':
                    explorer = 'https://etherscan.io/{R}/{V}' + (type == 'address' ? '' : '#tokentxns');
                    break;
                case 'usdt_tron':
                    explorer = 'https://tronscan.org/#/{R}/{V}';
                    break;
                    break;
                case 'algo':
                    explorer = 'https://algoexplorer.io/{R}/{V}';
                    break;
                case 'stripe':
                    explorer = 'https://dashboard.stripe.com/' + (CRPAY_ADMIN_SETTINGS.stripe_test_mode ? 'test/' : '') + 'customers/{R}'
                    break;
                case 'verifone':
                    explorer = 'https://secure.2checkout.com/cpanel/order_info.php?refno={V}';
                    break;
                case 'usdt_bsc':
                case 'busd':
                case 'bnb':
                    explorer = 'https://bscscan.com/{R}/{V}';
                    break;
                case 'ltc':
                    explorer = 'https://blockchair.com/litecoin/{R}/{V}'
                    break;
                case 'bch':
                    explorer = 'https://www.blockchain.com/bch/{R}/{V}'
                    break;
                case 'xrp':
                    replace = type == 'address' ? 'accounts' : 'transactions';
                    explorer = 'https://livenet.xrpl.org/{R}/{V}'
                    break;
                case 'sol':
                    replace = type == 'address' ? 'account' : 'tx';
                    explorer = 'https://solscan.io/{R}/{V}';
                    break;
                default:
                    let network = CRYPTOPayment.network(cryptocurrency, 'code');
                    if (network == 'eth') {
                        explorer = 'https://etherscan.io/{R}/{V}' + (type == 'address' ? '' : '#tokentxns');
                    }
                    if (network == 'bsc') {
                        explorer = 'https://bscscan.com/{R}/{V}';
                    }
            }
            return explorer.replace('{R}', replace).replace('{V}', value);
        },

        datepicker: function (input) {
            if (datepicker) {
                datepicker.destroy();
            }
            datepicker = new DateRangePicker(_(input).parent().e[0], date_picker_settings);
            datepicker.datepickers[0].show();
        },

        repeater: {
            add: function (button, print = true, index = false) {
                let parent = _(button).parent();
                let items = parent.find('[data-type], hr');
                let code = '<div class="crpay-repater-line"><hr /><i class="crpay-icon-close"></i></div>';
                index = index ? index : parseInt(_(button).data('index'));
                for (var i = 0; i < items.e.length; i++) {
                    if (items.e[i].nodeName == 'HR') {
                        break;
                    } else {
                        let id = _(items.e[i]).attr('id');
                        code += items.e[i].outerHTML.replace(id, id + '-' + index);
                    }
                }
                if (print) {
                    code += button.outerHTML.replace('"' + index + '"', '"' + (index + 1) + '"');
                } else {
                    return code;
                }
                button.remove();
                parent.append(code);
            },

            open: function (settings, area) {
                area.find('.crpay-btn-repater').e.forEach(e => {
                    let index = 2;
                    let code = '';
                    let id = _(e.parentElement).find('[id]').attr('id');
                    while (id + '-' + index in settings) {
                        code += this.add(e, false, index);
                        index++;
                    }
                    if (code) {
                        code += e.outerHTML.replace('"2"', '"' + index + '"');
                        _(e).parent().append(code);
                        e.remove();
                    }
                });
            },

            delete: function (i) {
                let is_first = _(i).hasClass('crpay-first-repeater-item');
                let parent = _(i).parent();
                let items = Array.from(parent.parent().e[0].childNodes);
                let index = items.indexOf(parent.e[0]) + 1;
                let button = parent.parent().find('.crpay-btn-repater');
                let button_index = parseInt(button.data('index')) - 1;
                let is_reset = false;
                for (var i = index; i < items.length; i++) {
                    let item = _(items[i]);
                    if (is_reset || item.hasClass('crpay-repater-line') || item.hasClass('crpay-btn-repater')) {
                        is_reset = true;
                        let item_id = item.attr('id');
                        let item_index = parseInt(item_id && item_id.includes('-') ? item_id.substr(item_id.lastIndexOf('-') + 1) : false);
                        if (item_index) {
                            item.attr('id', item_id.replace('-' + item_index, '-' + (item_index - 1)));
                        }
                    } else if (is_first) {
                        inputValue(inputGet(item), '');
                    } else {
                        item.remove();
                    }
                }
                button.data('index', button_index < 2 ? 2 : button_index);
                if (!is_first) {
                    parent.remove();
                }
            }
        }
    }

    window.CRPAYTransactions = CRPAYTransactions;
    window.CRPAYCheckout = CRPAYCheckout;
    window.CRPAYAdmin = CRPAYAdmin;

    /*
    * ----------------------------------------------------------
    * Functions
    * ----------------------------------------------------------
    */

    function loading(element, action = -1) {
        return CRYPTOPayment.loading(element, action);
    }

    function ajax(function_name, data = {}, onSuccess = false) {
        return CRYPTOPayment.ajax(function_name, data, onSuccess);
    }

    function activate(element, activate = true) {
        return CRYPTOPayment.activate(element, activate);
    }

    function card(message, type = false) {
        CRPAYAdmin.card(message, type);
    }

    function crpay_(text) {
        return CRPAY_TRANSLATIONS && text in CRPAY_TRANSLATIONS ? CRPAY_TRANSLATIONS[text] : text;
    }

    function showError(message, loading_area = false) {
        CRPAYAdmin.error(message, loading_area);
    }

    function scrollBottom() {
        window.scrollTo(0, document.body.scrollHeight - 800);
    }

    function inputValue(input, value = -1) {
        if (!input || !_(input).e.length) {
            return '';
        }
        input = _(input).e[0];
        if (value === -1) {
            return _(input).is('checkbox') ? input.checked : input.value.trim();
        }
        if (_(input).is('checkbox')) {
            input.checked = value == 0 ? false : value;
        } else {
            input.value = value;
        }
        if (_(input).is('textarea')) {
            resizeOnInput(input);
        }
    }

    function inputGet(parent) {
        return _(parent).find('input, select, textarea');
    }

    function openURL() {
        let url = window.location.href;
        if (url.includes('#')) {
            let anchor = url.substr(url.indexOf('#'));
            if (anchor.includes('?')) anchor = anchor.substr(0, anchor.indexOf('?'));
            if (anchor.length > 1) {
                let item = main.find('.crpay-nav ' + anchor);
                if (item.e.length) {
                    nav(item);
                    return true;
                }
            }
        }
        return false;
    }

    function nav(nav_item) {
        if (!_(nav_item).e.length) return;
        let items = main.find('main > div');
        let index = nav_item.index();
        active_area = _(items.e[index]);
        active_area_id = nav_item.attr('id');
        main.removeClass('crpay-area-transactions crpay-area-checkouts crpay-area-balances crpay-area-settings').addClass('crpay-area-' + active_area_id);
        activate(items, false);
        activate(nav_item.siblings(), false);
        activate(nav_item);
        activate(active_area);
        window.scrollTo(0, 0);
        if (!window.location.href.includes(active_area_id)) {
            window.history.pushState('', '', '#' + active_area_id);
        }
        switch (active_area_id) {
            case 'transactions':
                if (!loading(active_area)) {
                    if (datepicker) {
                        datepicker.destroy();
                        datepicker = false;
                    }
                    loading(active_area);
                    pagination = 0;
                    transactions_table.find('tbody').html('');
                    CRPAYTransactions.print(() => {
                        loading(items.e[index], false);
                        setTimeout(() => { window.scrollTo(0, 0) }, 100); 
                    });
                }
                break;
            case 'checkouts':
                if (loading(active_area, 'check')) {
                    ajax('get-checkouts', {}, (response) => {
                        let code = '';
                        CRPAY_CHECKOUTS = response;
                        for (var i = 0; i < response.length; i++) {
                            code += CRPAYCheckout.row(response[i]);
                        }
                        print(main.find('#crpay-table-checkouts tbody'), code);
                        loading(items.e[index], false);
                    });
                }
                break;
            case 'balances':
                CRPAYAdmin.balance(active_area);
                break;
            case 'settings':
                if (loading(active_area, 'check')) {
                    ajax('get-settings', {}, (response) => {
                        if (response) {
                            if (false) {
                                let name = 'custom-token-code';
                                let code_2 = '';
                                let i = 0;
                                while (response[name + (i ? '-' + (i + 1) : '')]) {
                                    code_2 += `<option value="${response[name + (i ? '-' + (i + 1) : '')]}">${response['custom-token-name' + (i ? '-' + (i + 1) : '')]}</option>`;
                                    i++;
                                }
                                if (code_2) {
                                    active_area.find('#exchange-default-get select').append(code_2);
                                }
                            }
                            CRPAYAdmin.repeater.open(response, active_area);
                            for (var key in response) {
                                let item = main.find('#' + key);
                                if (item) {
                                    inputValue(inputGet(item), response[key]);
                                }
                            }
                            active_area.find('#btc-wallet-key input, #eth-wallet-key input, #ln-macaroon input').e.forEach(e => {
                                if (e.value) {
                                    e.value = '********';
                                }
                            });
                            CRYPTOPayment.event('SettingsLoaded', response);
                        }
                        loading(items.e[index], false);
                    });
                }
                break;
            case 'analytics':
                if (datepicker) {
                    datepicker.destroy();
                    datepicker = false;
                }
                CRPAYAdminShop.analytics(datepicker);
                break;
        }
    }

    function slugToString(string) {
        string = string.replace(/_/g, ' ').replace(/-/g, ' ');
        return string.charAt(0).toUpperCase() + string.slice(1);
    }

    function stringToSlug(string) {
        string = string.trim();
        string = string.toLowerCase();
        var from = "åàáãäâèéëêìíïîòóöôùúüûñç·/_,:;";
        var to = "aaaaaaeeeeiiiioooouuuunc------";
        for (var i = 0, l = from.length; i < l; i++) {
            string = string.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
        }
        return string.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-').replace(/^-+/, '').replace(/-+$/, '').replace(/ /g, '');
    }

    function print(area, code, append = false) {
        if (!code) return area.html() ? false : area.html(`<p class="crpay-not-found">${crpay_('There\'s nothing here yet.')}</p>`);
        area.find('.crpay-not-found').remove();
        area.html((append ? area.html() : '') + code);
    }

    function resizeOnInputListener() {
        resizeOnInput(this);
    }

    function resizeOnInput(e) {
        e.style.height = 'auto';
        e.style.height = (e.scrollHeight) + 'px';
    }

    function cloudURL() {
        return CRPAY_CLOUD && CRPAY_CLOUD.cloud && !CRPAY_CLOUD.custom_domain ? '&cloud=' + CRPAY_CLOUD.cloud : '';
    }

    document.addEventListener('DOMContentLoaded', () => {

        /*
        * ----------------------------------------------------------
        * Init
        * ----------------------------------------------------------
        */

        body = _(document.body);
        main = body.find('.crpay-main');
        if (!main.e.length) {
            return;
        }
        areas = main.find('main > div');
        active_area = areas.e[0];
        transactions_table = main.find('#crpay-table-transactions');
        upload_input = body.find('#crpay-upload-form input');
        WP = typeof CRPAY_WP != 'undefined';
        if (CRYPTOPayment.cookie('CRPAY_LOGIN') && !main.hasClass('crpay-installation')) {
            active_area_id = 'transactions';
            if (!openURL()) {
                CRPAYTransactions.print(() => { loading(active_area, false) });
            }
            if (localStorage.getItem('crpay-cron') != today.getDate()) {
                ajax('cron', { domain: CRPAY_URL });
                localStorage.setItem('crpay-cron', today.getDate());
            }
            let textareas = document.getElementsByTagName('textarea');
            for (let i = 0; i < textareas.length; i++) {
                textareas[i].setAttribute('style', 'height:' + (textareas[i].scrollHeight) + 'px;overflow-y:hidden;');
                textareas[i].addEventListener('input', resizeOnInputListener, false);
            }
            CRPAYAdmin.balance(main.find('main > [data-area="balance"]'));
        }

        /*
        * ----------------------------------------------------------
        * Transactions
        * ----------------------------------------------------------
        */

        transactions_table.on('click', 'tr', function (e) {
            if (['A', 'I', 'LI'].includes(e.target.nodeName)) return;
            let hash = _(this).data('hash');
            if (hash) {
                let cryptocurrency = _(this).data('cryptocurrency');
                let url = CRPAYAdmin.explorer(cryptocurrency, hash, 'tx');
                if (url) {
                    window.open(url);
                }
            }
        });

        main.on('click', '#crpay-filters', function () {
            main.find('.crpay-nav-filters').toggleClass('crpay-active');
        });

        main.on('click', '.crpay-filter-date', function () {
            if (!datepicker_loaded) {
                _.load(CRPAY_URL + 'vendor/datepicker/datepicker.min.css', false);
                _.load(CRPAY_URL + 'vendor/datepicker/datepicker.min.js', true, () => {
                    if (CRPAY_LANG) {
                        date_picker_settings.language = CRPAY_LANG;
                        _.load(`${CRPAY_URL}vendor/datepicker/locales/${CRPAY_LANG}.js`, true, () => {
                            datepicker_loaded = true;
                            CRPAYAdmin.datepicker(this);
                        });
                    } else {
                        datepicker_loaded = true;
                        CRPAYAdmin.datepicker(this);
                    }
                });
            } else if (!datepicker) {
                CRPAYAdmin.datepicker(this);
            }
        });

        main.on('click', '.crpay-filter-status li, .crpay-filter-cryptocurrency li, .crpay-filter-checkout li, .datepicker-cell, .datepicker .clear-btn', function () {
            setTimeout(() => {
                if (active_area_id == 'transactions') {
                    CRPAYTransactions.query();
                } else {
                    CRPAYAdminShop.analytics(datepicker);
                }
            }, 100);
        });

        main.on('click', '#crpay-download-transitions', function () {
            if (loading(this)) return;
            CRPAYTransactions.download((response) => {
                window.open(response);
                loading(this, false);
            });
        });

        transactions_table.on('click', '.crpay-transaction-menu-btn:not(.crpay-loading)', function () {
            let active = _(this).hasClass('crpay-active');
            let row = _(this.closest('tr'));
            let code = `<ul class="crpay-transaction-menu crpay-ul"><li data-value="details">${crpay_('Details')}</li>`;
            let status = row.data('status');
            let cryptocurrency_code = row.data('cryptocurrency');
            activate(transactions_table.find('.crpay-transaction-menu-btn'), false);
            transactions_table.find('.crpay-transaction-menu').remove();
            if (!active) {
                activate(this);
                if (status == 'P' && row.data('amount')) {
                    code += `<li data-value="payment-link">${crpay_('Payment link')}</li>`;
                } else if (CRPAY_ADMIN_SETTINGS.invoice) {
                    code += `<li data-value="invoice">${crpay_('Invoice')}</li>`;
                }
                if (['C', 'X'].includes(status) && ((CRPAY_REFUNDS.includes('coinbase') && ['btc', 'eth', 'xrp', 'usdt', 'usdc', 'busd', 'bnb', 'link', 'doge', 'shib', 'ltc', 'algo', 'bat', 'bch', 'sol'].includes(cryptocurrency_code)) || (CRPAY_REFUNDS.includes('btc') && cryptocurrency_code == 'btc') || (CRPAY_REFUNDS.includes('eth') && ['eth', 'usdt', 'usdc', 'link', 'shib', 'bat'].includes(cryptocurrency_code)))) {
                    code += `<li data-value="refund">${crpay_('Issue a refund')}</li>`;
                }
                for (var key in transaction_statuses) {
                    if (key != status) {
                        code += `<li data-value="${key}">${crpay_('Mark as ' + transaction_statuses[key].toLowerCase())}</li>`;
                    }
                }
                _(this).parent().append(code + '</ul>');
            }
        });

        transactions_table.on('click', '.crpay-transaction-menu li', function () {
            let row = _(this.closest('tr'));
            let menu = row.find('.crpay-transaction-menu-btn');
            if (loading(menu)) return;
            let value = _(this).data('value');
            let id = row.data('id');
            if (['C', 'P', 'R'].includes(value)) {
                let message;
                if (value == 'C') {
                    switch (row.data('type')) {
                        case '2':
                        case '1':
                            message = 'The transaction will be processed, and any user purchase associated with it will be processed and finalized.';
                            break;
                        case '3':
                            message = 'The exchange will be processed and the amount to be exchanged will be automatically sent to the user.';
                            break;
                    }
                }
                CRPAYAdmin.confirm(message, () => {
                    ajax('update-transaction', { transaction_id: id, values: { status: value } }, (response) => {
                        if (response === true) {
                            row.data('status', value).find('.crpay-td-status span').attr('class', 'crpay-status-' + value).html(crpay_(transaction_statuses[value]));
                        } else {
                            card(response, 'error');
                        }
                        loading(menu, false);
                    });
                }, menu, !message);
            }
            if (value == 'invoice') {
                ajax('encryption', { string: id }, (response) => {
                    window.open((CRPAY_CLOUD.custom_domain ? CRPAY_CLOUD.custom_domain : CRPAY_URL) + (CRPAY_ADMIN_SETTINGS.url_rewrite_invoice ? CRPAY_ADMIN_SETTINGS.url_rewrite_invoice : '/vendor/cryptd/pay.php?invoice=') + response + cloudURL());
                    loading(menu, false);
                });
            }
            if (value == 'payment-link') {
                ajax(value, { transaction_id: id }, (response) => {
                    window.open(response + cloudURL());
                    loading(menu, false);
                });
            }
            if (value == 'details') {
                let code = '<div class="crpay-text-list crpay-transaction-details-list">';
                let details = [['ID', 'id'], ['Checkout', 'data-title'], ['Hash', 'data-hash'], ['Time', 'time'], ['Notes', 'data-notes'], ['Status', 'status'], ['From', 'from'], ['To', 'to'], ['Amount', 'amount'], ['VAT', 'data-vat'], ['License key', 'data-license-key']];
                for (var i = 0; i < details.length; i++) {
                    let slug = details[i][1];
                    let value = slug.includes('data') ? row.attr(slug) : row.find('.crpay-td-' + slug).html();
                    if (value) {
                        value = value.trim();
                        if (slug == 'data-hash') {
                            value = `<a href="${CRPAYAdmin.explorer(row.data('cryptocurrency'), value, 'tx')}" target="_blank">${value}</a>`;
                        } else if (slug == 'data-title') {
                            value = value + ' - ID ' + row.data('checkout-id');
                        } else if (slug == 'data-notes') {
                            value = value.replaceAll('\\r\\n', '\n').replaceAll('\\r', '\n').replaceAll('\\n', '<br>').replaceAll('\n', '<br>');
                        } else if (slug == 'data-license-key') {
                            let status = row.attr('data-license-key-status').trim();
                            if (status != 1) {
                                value = '<div class="crpay-label-code">' + value + '</div><div class="crpay-label">' + crpay_('Disabled') + '</div>';
                            }
                            value += `<div id="crpay-btn-license-key-status" class="crpay-btn-text crpay-underline" data-status="${status}" data-id="${row.find('.crpay-td-id').html()}">${crpay_(status == 1 ? 'Disable' : 'Enable')}</div>`;
                        }
                        code += `<div data-name="${slug}"><div>${crpay_(details[i][0])}</div><div>${value}</div></div>`;
                    }
                }
                if (CRPAY_ADMIN_SETTINGS.apps.includes('shop')) {
                    if (row.data('discount-code')) {
                        code += `<div><div>${crpay_('Discount code')}</div><div>${row.data('discount-code')}</div></div>`;
                    }
                    if (row.data('customer-id')) {
                        code += `<div data-name="customer"><div>${crpay_('Customer details')}</div><div class="crpay-loading"></div></div>`;
                        ajax('get-customer', { customer_id: row.data('customer-id') }, (response) => {
                            let name = response.first_name + (response.last_name ? ' ' + response.last_name : '');
                            body.find('#crpay-lightbox [data-name="customer"] .crpay-loading').html((name ? name + ' - ' : '') + 'ID ' + response.id + (response.email ? '<br>' + response.email : '') + (response.phone ? '<br>' + response.phone : '') + (response.country ? '<br>' + response.country + ' ' + response.country_code : '')).removeClass('crpay-loading');
                        });
                    }
                }
                CRYPTOPayment.lightbox('Transaction details', code);
                loading(menu, false);
            }
            if (value == 'refund') {
                card(crpay_('Sending refund in 5 seconds.') + ' <span id="cancel-refund">' + crpay_('Cancel') + '</span>', 'info');
                refund_timeout = setTimeout(() => {
                    ajax(value, { transaction_id: id }, (response) => {
                        if (response.status === true) {
                            row.data('status', value).find('.crpay-td-status span').attr('class', 'crpay-status-R').html(crpay_(transaction_statuses['R']));
                            card(response.message);
                            let link = main.find('.crpay-info-card a');
                            link.attr('href', CRPAYAdmin.explorer(row.data('cryptocurrency'), link.data('hash'), 'tx'));
                        } else {
                            card(response.message, 'error');
                        }
                        loading(menu, false);
                    });
                }, 5000);
            }
            row.find('.crpay-ul').remove();
            activate(menu, false);
        });

        main.on('click', '#cancel-refund', function () {
            clearTimeout(refund_timeout);
            loading(transactions_table.find('.crpay-loading'), false);
        });

        main.on('click', '#crpay-request-payment', function () {
            let code = '';
            let fields = [['price', 'Price', 'number'], ['currency', 'Currency code', 'text'], ['pay', 'Payment Code', 'text'], ['redirect', 'Redirect URL', 'url'], ['note', 'Notes', 'text']];
            for (var i = 0; i < fields.length; i++) {
                code += `<div class="crpay-input"><span>${crpay_(fields[i][1])}</span><input data-url-attribute="${crpay_(fields[i][0])}" type="${crpay_(fields[i][2])}"></div>`;
            }
            CRYPTOPayment.lightbox('Create a payment request', code + `<div id="crpay-create-payment-link" class="crpay-btn">${crpay_('Create payment link')}</div>`);
        });

        body.on('click', '#crpay-create-payment-link', function () {
            let inputs = _(this).parent().find('input').e;
            let url = '';
            for (var i = 0; i < inputs.length; i++) {
                let slug = _(inputs[i]).data('url-attribute');
                let value = _(inputs[i]).val();
                if (value) {
                    if (slug == 'redirect' || slug == 'note') {
                        value = encodeURIComponent(value);
                    }
                    url += '&' + slug + '=' + value;
                }
            }
            if (url) {
                url = `${CRPAY_CLOUD.custom_domain ? CRPAY_CLOUD.custom_domain : CRPAY_URL}${CRPAY_ADMIN_SETTINGS.url_rewrite_checkout ? CRPAY_ADMIN_SETTINGS.url_rewrite_checkout : '/vendor/cryptd/pay.php?checkout_id='}custom-${Math.floor(Date.now() / 1000)}${cloudURL()}${url && CRPAY_ADMIN_SETTINGS.url_rewrite_checkout ? '?' + url.substring(1) : url}`;
                _(this).parent().find('#crpay-payment-request-url-box').remove();
                _(this).insert(`<div id="crpay-payment-request-url-box" class="crpay-input"><a href="${url}" target="_blank">${url.replace(/&/g, '&amp')}</a><i class="crpay-icon-copy crpay-clipboard" data-text="${window.btoa(url)}"></i></div>`);
            }
        });

        body.on('click', '#crpay-btn-license-key-status', function () {
            let status = _(this).data('status') == 1 ? 2 : 1;
            let id = _(this).data('id');
            ajax('update-license-key-status', { transaction_id: id, status: status }, () => {
                _(this).html(crpay_(status == 1 ? 'Disable' : 'Enable')).data('status', status);
                transactions_table.find(`[data-id="${id}"]`).data('data-license-key-status', status);
                _(this).parent().find('.crpay-label').remove();
                if (status != 1) {
                    _(this).insert('<div class="crpay-label">' + crpay_('Disabled') + '</div>');
                }
            });
        });

        /*
        * ----------------------------------------------------------
        * Checkouts
        * ----------------------------------------------------------
        */

        main.on('click', '#crpay-create-checkout, #crpay-table-checkouts td', function () {
            let checkout = false;
            main.addClass('crpay-area-create-checkout');
            if (_(this).is('td')) {
                let id = _(this).parent().data('checkout-id');
                checkout = CRPAYCheckout.get(id);
                active_area.data('checkout-id', id);
                for (var key in checkout) {
                    let input = inputGet(active_area.find(`#crpay-checkout-${key}`));
                    let value = checkout[key];
                    inputValue(input, value);
                }
                CRPAYCheckout.embed(checkout.slug ? checkout.slug : id);
            } else {
                inputGet(active_area).e.forEach(e => {
                    inputValue(e, '');
                });
                active_area.find('#crpay-checkout-type select').val('I');
                active_area.data('checkout-id', '');
                CRPAYCheckout.embed();
            }
            if (CRPAY_ADMIN_SETTINGS.apps.includes('shop')) {
                CRPAYAdminShop.checkoutOpen(checkout, active_area);
            }
        });

        main.on('click', '#crpay-checkouts-list', function () {
            main.removeClass('crpay-area-create-checkout');
            active_area.data('checkout-id', '');
        });

        main.on('click', '#crpay-save-checkout', function () {
            if (loading(this)) return;
            let error = false;
            let checkout = {};
            let inputs = active_area.find('.crpay-input');
            let checkout_id = active_area.data('checkout-id');
            main.find('.crpay-info').html('');
            inputs.removeClass('crpay-error');

            // Get values
            inputs.e.forEach(e => {
                let id = _(e).attr('id');
                let input = _(e).find('input, select');
                let value = inputValue(input);
                if (!value && input.e.length && input.e[0].hasAttribute('required')) {
                    error = true;
                    _(e).addClass('crpay-error');
                }
                checkout[id.replace('crpay-checkout-', '')] = value;
            });
            if (error) {
                showError('Fields in red are required.', this);
                return;
            }
            if (checkout_id) {
                checkout.id = checkout_id;
            }
            if (CRPAY_ADMIN_SETTINGS.apps.includes('shop')) {
                checkout = CRPAYAdminShop.checkoutSave(checkout, active_area);
            }

            // Slug
            if (CRPAY_ADMIN_SETTINGS.url_rewrite_checkout && !checkout.slug && checkout.title) {
                let slug = stringToSlug(checkout.title);
                checkout.slug = slug;
            }
            for (var i = 0; i < CRPAY_CHECKOUTS.length; i++) {
                if (CRPAY_CHECKOUTS[i].slug == checkout.slug && (!checkout.id || checkout.id != CRPAY_CHECKOUTS[i].id)) {
                    checkout.slug = '';
                }
            }
            active_area.find('#crpay-checkout-slug input').val(checkout.slug);

            // Save changes
            ajax('save-checkout', { checkout: JSON.stringify(checkout) }, (response) => {
                loading(this, false);
                if (Number.isInteger(response)) {
                    checkout.id = response;
                    active_area.data('checkout-id', response);
                    active_area.find('#crpay-table-checkouts tbody').append(CRPAYCheckout.row(checkout));
                    CRPAYCheckout.embed(checkout.slug ? checkout.slug : response);
                    CRPAY_CHECKOUTS.push(checkout);
                    card('Checkout saved successfully');
                } else if (response === true) {
                    CRPAYCheckout.get(checkout_id, true);
                    CRPAY_CHECKOUTS.push(checkout);
                    active_area.find(`tr[data-checkout-id="${checkout_id}"]`).replace(CRPAYCheckout.row(checkout));
                    CRPAYCheckout.embed(checkout.slug ? checkout.slug : response);
                    card('Checkout saved successfully');
                } else {
                    showError(response, this.closest('form'));
                }
            });
        });

        main.on('click', '#crpay-delete-checkout', function () {
            if (loading(this)) return;
            let id = active_area.data('checkout-id');
            ajax('delete-checkout', { checkout_id: id }, () => {
                loading(this, false);
                active_area.data('checkout-id', '');
                active_area.find(`tr[data-checkout-id="${id}"]`).remove();
                active_area.find('#crpay-checkouts-list').e[0].click();
                card('Checkout deleted', 'error');
            });
        });

        main.on('click', '#checkout-downloads .crpay-repater-line i', function () {
            let file_name = _(this).parent().next().find('input').val();
            if (file_name) {
                ajax('delete-file', { file_name: file_name, folder: 'checkout/' });
            }
        });

        main.on('click', '#checkout-downloads input', function () {
            if (_(this).val()) {
                window.open(CRPAY_CLOUD.aws ? CRPAY_CLOUD.aws + _(this).val() : CRPAY_URL + 'uploads' + (CRPAY_CLOUD ? CRPAY_CLOUD.path_part : '') + '/checkout/' + _(this).val());
            }
        });

        /*
        * ----------------------------------------------------------
        * Settings
        * ----------------------------------------------------------
        */

        main.on('click', '#crpay-save-settings', function () {
            if (loading(this)) return;
            let settings = {};
            main.find('[data-area="settings"]').find('.crpay-input[id]:not([data-type="multi-input"]),[data-type="multi-input"] [id]').e.forEach(e => {
                settings[_(e).attr('id')] = inputValue(inputGet(e));
            });
            ajax('save-settings', { settings: JSON.stringify(settings) }, (response) => {
                card(response === true ? 'Settings saved' : response, response === true ? false : 'error');
                loading(this, false);
            });
        });

        main.on('click', '#update-btn a', function (e) {
            if (loading(this)) return;
            ajax('update', { domain: CRPAY_URL }, (response) => {
                loading(this, false);
                let errors = false;
                let latest_all = true;
                if (typeof response === 'string') {
                    card(slugToString(response), 'error');
                } else {
                    for (var key in response) {
                        if (response[key] !== true && response[key] !== '' && response[key] !== 'latest-version-installed') {
                            errors = true;
                        } else if (response[key] === true) {
                            latest_all = false;
                        }
                    }
                    if (latest_all) {
                        card('You have the latest version!');
                    } else if (!errors) {
                        card('Update completed. Reload in progress...');
                        setTimeout(() => { location.reload(); }, 500);
                    } else {
                        card(slugToString(JSON.stringify(response)), 'error');
                    }
                }
            });
            e.preventDefault();
            return false;
        });

        main.on('click', '#email-test-btn a', function (e) {
            if (loading(this)) return;
            ajax('email-test', {}, (response) => {
                card(response === true ? 'Email successfully sent.' : response);
                loading(this, false);
            });
            e.preventDefault();
            return false;
        });

        main.on('click', '.crpay-btn-repater', function () {
            CRPAYAdmin.repeater.add(this);
        });

        main.on('click', '.crpay-repater-line i', function () {
            CRPAYAdmin.repeater.delete(this);
        });

        main.on('click', '#two-fa-pairing a', function (e) {
            ajax('2fa', {}, (response) => {
                window.open(response);
            });
            e.preventDefault();
            return false;
        });

        main.on('click', '#two-fa-validation a', function (e) {
            let input = main.find('#two-fa-code input');
            let code = input.val().trim();
            if (code) {
                ajax('2fa', { code: code }, (response) => {
                    card(response === true ? '2FA activated!' : 'Invalid code.', response === true ? 'info' : 'error');
                    input.val('');
                });
            }
            e.preventDefault();
            return false;
        });

        /*
        * ----------------------------------------------------------
        * Responsive
        * ----------------------------------------------------------
        */

        if (responsive) {
            main.on('click', '.crpay-icon-menu', function () {
                let area = _(this).parent();
                activate(area, !area.hasClass('crpay-active'));
            });
        }

        /*
        * ----------------------------------------------------------
        * Miscellaneous
        * ----------------------------------------------------------
        */

        main.on('click', '#crpay-submit-installation', function () {
            if (loading(this)) return;
            let error = false;
            let installation_data = {};
            let url = window.location.href.replace('/admin', '').replace('.php', '').replace(/#$|\/$/, '');
            let inputs = main.find('.crpay-input');
            inputs.removeClass('crpay-error');
            main.find('.crpay-info').html('');
            inputs.e.forEach(e => {
                let id = _(e).attr('id');
                let input = _(e).find('input');
                let value = input.val().trim();
                if (!value && input.e[0].hasAttribute('required')) {
                    error = true;
                    _(e).addClass('crpay-error');
                }
                installation_data[id] = value;
            });
            if (error) {
                error = 'Username, password and the database details are required.';
                let password = installation_data.password;
                if (password) {
                    if (password.length < 8) {
                        error = 'Minimum password length is 8 characters.';
                    } else if (password != installation_data['password-check']) {
                        error = 'The passwords do not match.';
                    }
                }
                showError(error, this);
                return;
            }
            if (url.includes('?')) url = url.substr(0, url.indexOf('?'));
            installation_data['url'] = url + '/';
            ajax('installation', { installation_data: JSON.stringify(installation_data) }, (response) => {
                if (response === true) {
                    location.reload();
                } else {
                    showError(response, this);
                }
            });
        });

        main.on('click', '.crpay-nav > div', function () {
            nav(_(this));
        });

        main.on('click', '#crpay-submit-login', function () {
            if (loading(this)) return;
            ajax('login', { username: main.find('#username input').val().trim(), password: main.find('#password input').val().trim(), code: main.find('#two-fa input').val().trim() }, (response) => {
                let error = false;
                if (response == 'ip-ban') {
                    error = 'Too many login attempts. Please retry again in a few hours.';
                }
                if (response == '2fa') {
                    error = 'Invalid verification code.';
                }
                if (!response) {
                    error = 'Invalid username or password.';
                }
                if (error) {
                    main.find('.crpay-info').html(crpay_(error));
                    return loading(this, false);
                }
                CRYPTOPayment.cookie('CRPAY_LOGIN', response, 365, 'set');
                location.reload();
            });
        });

        main.on('click', '#crpay-logout', function () {
            CRYPTOPayment.cookie('CRPAY_LOGIN', false, false, 'delete');
            CRYPTOPayment.cookie('CRPAY_CLOUD', false, false, 'delete');
            location.reload();
        });

        main.on('click', '#crpay-card', function () {
            _(this).html('');
        });

        main.on('input', '.crpay-search-input', function () {
            CRYPTOPayment.search(this, (search, icon) => {
                if (active_area_id == 'transactions') {
                    CRPAYTransactions.query(icon, search);
                }
            });
        });

        main.on('click', '#crpay-table-balances tr', function () {
            let cryptocurrency = _(this).data('cryptocurrency');
            let url = CRPAYAdmin.explorer(cryptocurrency, CRPAY_ADDRESS[cryptocurrency]);
            if (url) {
                window.open(url);
            }
        });

        window.onscroll = function () {
            if (active_area && (document.documentElement || document.body.parentNode || document.body).scrollTop + window.innerHeight == _.documentHeight() && pagination_count) {
                if (active_area_id == 'transactions') {
                    if (!_(active_area).find('> .crpay-loading-global').e.length) {
                        CRPAYTransactions.print(() => {
                            scrollBottom();
                            _(active_area).find('> .crpay-loading-global').remove();
                        });
                        _(active_area).append('<div class="crpay-loading-global crpay-loading"></div>');
                    }
                }
            }
        };

        window.onpopstate = function () {
            openURL();
        }

        document.addEventListener('click', function (e) {
            if (CRPAYAdmin.active_element && !CRPAYAdmin.active_element.contains(e.target)) {
                activate(CRPAYAdmin.active_element, false);
                CRPAYAdmin.active_element = false;
            }
        });

        main.on('click', '[data-type="upload-file"] > div', function () {
            if (loading(this, 'check')) {
                return;
            }
            upload_input.val('')
            upload_input.e[0].click();
            upload_target = this;
        });

        if (upload_input && upload_input.e.length) {
            upload_input.e[0].addEventListener('change', function (data) {
                let files = this.files;
                loading(upload_target);
                for (var i = 0; i < files.length; i++) {
                    let file = files[i];
                    let size_mb = file.size / (1024 ** 2);
                    let max_size = CRPAY_ADMIN_SETTINGS.max_file_size;
                    let form = new FormData();
                    let target = _(upload_target).parent().data('upload-target');
                    if (size_mb > max_size) {
                        return showError(crpay_('Maximum upload size is {R}MB. File size: {R2}MB.').replace('{R}', max_size).replace('{R2}', size_mb.toFixed(2)), this);
                    }
                    form.append('file', file);
                    fetch(CRPAY_URL + '/vendor/cryptd/upload.php?target=' + target, {
                        method: 'POST',
                        body: form
                    }).then((response) => response.json()).then(function (response) {
                        loading(upload_target, false);
                        if (response[0]) {
                            let parent = _(upload_target).parent();
                            let input = parent.find('input');
                            if (input.val()) {
                                ajax('delete-file', { file_name: input.val(), folder: target == 'checkout-file' ? 'checkout/' : '' });
                            }
                            input.val(response[2]);
                            if (parent.hasClass('crpay-upload-image')) {
                                parent.find('.crpay-btn-icon').e[0].style.backgroundImage = 'url("' + response[1] + '")';
                            }
                        } else {
                            showError(response[1]);
                        }
                        CRYPTOPayment.event('CRPAYUpload', response);
                    });
                }
                this.value = '';
            });
        }

        main.on('click', '.crpay-upload-image > i', function () {
            let prev = _(this).prev();
            prev.attr('style', '');
            if (prev.prev().val()) {
                ajax('delete-file', { file_name: prev.prev().val() });
            }
            prev.prev().val('');
        });

        body.on('click', '#crpay-confirm-btn, #crpay-cancel-btn', function () {
            if (_(this).attr('id') == 'crpay-cancel-btn') {
                if (confirm_on_success[1]) {
                    loading(confirm_on_success[1], false);
                };
            } else {
                confirm_on_success[0]();
            }
            CRYPTOPayment.lightboxClose();
        });
    });
}());