/**
 * get post purchase buttons
 * final step for post purchase
 *
 * @param void
 * @return $html
 */
function mgm_get_post_purchase_buttons()
{
    // get current user data - issue #1421
    $user = wp_get_current_user();
    // pack
    $pack = NULL;
    // addon options
    if ($addon_option_ids = mgm_post_var('addon_options')) {
        $addon_options = mgm_get_addon_options_only($addon_option_ids);
        // mgm_pr($addon_options);
    }
    // post purchase
    if (isset($_POST['post_id'])) {
        //issue #1250
        if (isset($_POST['mgm_postpurchase_field']['coupon']) && !empty($_POST['mgm_postpurchase_field']['coupon'])) {
            //issue #1250 - Coupon validation
            if (!empty($_POST['form_action'])) {
                // check if its a valid coupon
                if (!($coupon = mgm_get_coupon_data($_POST['mgm_postpurchase_field']['coupon']))) {
                    //redirect back to the form
                    $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_postpurchase_field']['coupon']);
                    $redirect = add_query_arg($q_arg, $_POST['form_action']);
                    mgm_redirect($redirect);
                    exit;
                }
            }
        }
        // post id
        $post_id = $_POST['post_id'];
        // gete mgm data
        $post_obj = mgm_get_post($post_id);
        $cost = mgm_convert_to_currency($post_obj->purchase_cost);
        $product = $post_obj->product;
        $allowed_modules = $post_obj->allowed_modules;
        // post data
        $post = get_post($post_id);
        $title = $post->post_title;
        // item name -issue #1380
        $item_name = apply_filters('mgm_post_purchase_itemname', sprintf(__('Purchase Post - %s', 'mgm'), $title));
        // set pack
        $pack = array('duration' => 1, 'item_name' => $item_name, 'buypost' => 1, 'cost' => $cost, 'title' => $title, 'product' => $product, 'post_id' => $post_id, 'allowed_modules' => $allowed_modules);
    } else {
        if (isset($_POST['postpack_id'])) {
            // post pack purchase
            //issue #1250
            if (isset($_POST['mgm_postpurchase_field']['coupon']) && !empty($_POST['mgm_postpurchase_field']['coupon'])) {
                //issue #1250 - Coupon validation
                if (!empty($_POST['form_action'])) {
                    // check if its a valid coupon
                    if (!($coupon = mgm_get_coupon_data($_POST['mgm_postpurchase_field']['coupon']))) {
                        //redirect back to the form
                        $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_postpurchase_field']['coupon']);
                        $redirect = add_query_arg($q_arg, $_POST['form_action']);
                        mgm_redirect($redirect);
                        exit;
                    }
                }
            }
            // post pack purchase
            $postpack_id = $_POST['postpack_id'];
            // pcak id
            $postpack_post_id = $_POST['postpack_post_id'];
            // post id where pack is listed, redirect here
            // get pack
            $postpack = mgm_get_postpack($postpack_id);
            $cost = mgm_convert_to_currency($postpack->cost);
            $product = json_decode($postpack->product, true);
            $modules = json_decode($postpack->modules, true);
            //mgm_pr($postpack);
            // item name -issue #1380
            $item_name = apply_filters('mgm_postpack_purchase_itemname', sprintf(__('Purchase Post Pack - %s', 'mgm'), $postpack->name));
            // post id
            $post_id = mgm_get_postpack_posts_csv($postpack_id);
            // set pack
            $pack = array('duration' => 1, 'item_name' => $item_name, 'buypost' => 1, 'cost' => $cost, 'title' => $postpack->name, 'product' => $product, 'post_id' => $post_id, 'postpack_id' => $postpack_id, 'postpack_post_id' => $postpack_post_id, 'allowed_modules' => $modules);
        }
    }
    // check
    if (!$pack) {
        return __('Error in Payment! No data available ');
        exit;
    }
    // guest token	-issue #1421
    if (isset($_POST['guest_purchase']) && $_POST['guest_purchase'] == TRUE && $user->ID <= 0) {
        $pack['guest_token'] = sanitize_title_for_query(mgm_create_token());
    }
    // addon options
    if (isset($addon_options) && !empty($addon_options)) {
        $pack['addon_options'] = $addon_options;
    }
    // get coupon
    $post_purchase_coupon = mgm_save_partial_fields(array('on_postpurchase' => true), 'mgm_postpurchase_field', $pack['cost'], false, 'postpurchase');
    // alter
    mgm_get_post_purchase_coupon_pack($post_purchase_coupon, $pack);
    // Eg: $_POST['mgm_payment_gateways'] = mgm_paypal
    $cf_payment_gateways = isset($_POST['mgm_payment_gateways']) && !empty($_POST['mgm_payment_gateways']) ? $_POST['mgm_payment_gateways'] : null;
    // bypass step2 if payment gateway is submitted: issue #: 469
    if (!is_null($cf_payment_gateways)) {
        // get pack
        // mgm_get_upgrade_coupon_pack($member, $selected_pack);
        // cost
        if ((double) $pack['cost'] > 0) {
            //get an object of the payment gateway:
            $mod_obj = mgm_get_module($cf_payment_gateways, 'payment');
            // tran options
            $tran_options = array('user_id' => $user->ID);
            // is register & purchase
            if (isset($_POST['post_id'])) {
                $tran_options['post_id'] = (int) $_POST['post_id'];
            }
            // postpack id
            if (isset($_POST['postpack_id'])) {
                $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
            }
            // is register & purchase postpack
            if (isset($_POST['postpack_post_id']) && isset($_POST['postpack_id'])) {
                $tran_options['postpack_post_id'] = (int) $_POST['postpack_post_id'];
                $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
            }
            // create transaction
            $tran_id = mgm_add_transaction($pack, $tran_options);
            // bypass directly to process return if manual payment:
            if ($cf_payment_gateways == 'mgm_manualpay') {
                // set
                $_POST['custom'] = $tran_id;
                // direct call to module return function:
                $mod_obj->process_return();
                // exit
                exit;
            }
            // encode id:
            $tran_id = mgm_encode_id($tran_id);
            $redirect = $mod_obj->_get_endpoint('html_redirect', true);
            $redirect = add_query_arg(array('tran_id' => $tran_id), $redirect);
            // redirect
            mgm_redirect($redirect);
            // this goes to subscribe, mgm_functions.php/mgm_get_subscription_buttons
            // exit
            exit;
        }
    }
    // get payment modules
    $a_payment_modules = mgm_get_class('system')->get_active_modules('payment');
    // init
    $payment_modules = array();
    // when active
    if ($a_payment_modules) {
        // loop
        foreach ($a_payment_modules as $payment_module) {
            // not trial
            if (in_array($payment_module, array('mgm_free', 'mgm_trial'))) {
                continue;
            }
            // store
            $payment_modules[] = $payment_module;
        }
    }
    // init
    $button = '';
    // transaction
    $tran_id = NULL;
    $button_printed = 0;
    // loop modules
    foreach ($payment_modules as $module) {
        // object
        $mod_obj = mgm_get_module($module, 'payment');
        // check buypost support
        if (in_array('buypost', $mod_obj->supported_buttons)) {
            // create transaction
            if (!$tran_id) {
                $tran_id = mgm_add_transaction($pack);
            }
            // button code
            if (isset($pack['allowed_modules'])) {
                // Issue #1562: If no payment module is selected, display all supported modules
                if (!empty($pack['allowed_modules']) && FALSE === in_array($module, $pack['allowed_modules'])) {
                    continue;
                }
            }
            $button_code = $mod_obj->get_button_buypost(array('pack' => $pack, 'tran_id' => $tran_id), true);
            $button_printed++;
            // get button
            $button .= "<div class='mgm_custom_filed_table'>" . $button_code . "</div>";
        }
    }
    // none active
    if ($button_printed == 0) {
        $button .= sprintf('<p class="mgm-no-module"> %s </p>', __('No Payment module active for this Content Purchase.', 'mgm'));
    }
    // if Cost is zero, then process using free module.: issue#: 883
    if ($tran_id && $pack['cost'] == 0 && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->is_enabled()) {
        // module
        $module = 'mgm_free';
        // payments url
        $payments_url = mgm_get_custom_url('transactions');
        // query_args
        $query_args = array('method' => 'payment_return', 'module' => $module, 'custom' => $tran_id);
        // redirector
        if (isset($_REQUEST['redirector'])) {
            // set
            $query_args['redirector'] = $_REQUEST['redirector'];
        }
        // redirect to module to mark the payment as complete
        $redirect = add_query_arg($query_args, $payments_url);
        // redirect
        mgm_redirect($redirect);
    }
    // html
    $return = '<div class="post_purchase_select_gateway">' . __('Please Select a Payment Gateway.', 'mgm') . '</div>' . $button;
    // return
    return $return;
}
/**
 * filters post purchase modules on the fly 
 * hooks to mgm_payment_gateways_as_custom_field
 *
 * @param array $modules
 * @return array $modules
 * @since 1.8.36
 */
function mgm_payment_gateways_as_custom_field_on_postpurchase($modules)
{
    global $post;
    if (isset($GLOBALS['buttons_postpack_id'])) {
        $post_pack = mgm_get_postpack($GLOBALS['buttons_postpack_id']);
        unset($GLOBALS['buttons_postpack_id']);
        if (isset($post_pack->modules)) {
            return (array) json_decode($post_pack->modules, true);
        }
    } else {
        if (isset($post->ID) && isset($_REQUEST['method']) != 'guest_purchase') {
            $mgm_post = mgm_get_post($post->ID);
            if (isset($mgm_post->allowed_modules)) {
                return (array) $mgm_post->allowed_modules;
            }
        } else {
            if (isset($_REQUEST['method']) == 'guest_purchase' && isset($_REQUEST['post_id']) && !empty($_REQUEST['post_id'])) {
                $mgm_post = mgm_get_post($_REQUEST['post_id']);
                if (isset($mgm_post->allowed_modules)) {
                    return (array) $mgm_post->allowed_modules;
                }
            }
        }
    }
    return $modules;
}
/**
 * generate guest purchase post form - issue #1396
 *
 * @param int post id
 * @return string html
 */
function mgm_guest_purchase_postpack_form($postpack_id, $postpack_post_id)
{
    global $wpdb;
    // get postpack
    $postpack = mgm_get_postpack($postpack_id);
    // validate
    if (!$postpack->id) {
        return __('Bad data', 'mgm');
    }
    // return - issue #1396
    return mgm_get_postpack_template($postpack_id, true, $postpack_post_id);
}