<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action('admin_post_nopriv_elementsell_payment', 'elementsell_handle_payment');
add_action('admin_post_elementsell_payment', 'elementsell_handle_payment');

function elementsell_handle_payment() {

    if (
        ! isset($_POST['_wpnonce']) ||
        ! wp_verify_nonce($_POST['_wpnonce'], 'platebni_formular_nonce')
    ) {
        wp_die('Neplatný bezpečnostní token.');
    }

    if ( elementsell_is_billing_blocked() ) {
        wp_die( 'Platební formulář je dočasně nedostupný. Kontaktujte provozovatele webu.' );
    }

    $settings = elementsell_validate_payment_submission();

    $payment_method = isset($_POST['payment_method'])
        ? sanitize_text_field($_POST['payment_method'])
        : '';

    if ( $payment_method === 'fio' ) {
        elementsell_handle_fio_payment( $settings );
        exit;
    }

    elementsell_handle_thepay_payment( $settings );
    exit;
}