/**
 * register post, validate required custom fields
 *
 * @param string $sanitized_user_login 
 * @param string $user_email
 * @param object $errors
 * @param boolean $show_fields
 * @return object $errors
 */
function mgm_register_post($sanitized_user_login = '', $user_email = '', $errors = null, $show_fields = null)
{
    // get mgm_system
    $system_obj = mgm_get_class('system');
    // hide
    $hide_custom_fields = $system_obj->get_setting('hide_custom_fields');
    // error
    if (is_null($errors)) {
        $errors = new WP_Error();
    }
    // unset old errors
    unset($errors->errors);
    // errors
    $error_codes = $errors->get_error_codes();
    // user_login
    if (array_key_exists('user_login', $_POST)) {
        $sanitized_user_login = sanitize_user($_POST['user_login']);
        if ($sanitized_user_login == '') {
            if (!in_array('empty_username', $error_codes)) {
                $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.', 'mgm'));
            }
        } elseif (!validate_username($sanitized_user_login)) {
            if (!in_array('invalid_username', $error_codes)) {
                $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'mgm'));
            }
            $sanitized_user_login = '';
        } elseif (!mgm_validate_username($sanitized_user_login)) {
            if (!in_array('invalid_username', $error_codes)) {
                $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid because it uses illegal characters, spaces are not allowed. Please enter a valid username.', 'mgm'));
            }
            $sanitized_user_login = '';
        } elseif (username_exists($sanitized_user_login)) {
            if (!in_array('username_exists', $error_codes)) {
                $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.', 'mgm'));
            }
        }
    }
    // user_email
    if (array_key_exists('user_email', $_POST)) {
        $user_email = apply_filters('user_registration_email', $_POST['user_email']);
        // Check the e-mail address
        if ($user_email == '') {
            if (!in_array('empty_email', $error_codes)) {
                $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.', 'mgm'));
            }
        } elseif (!is_email($user_email)) {
            if (!in_array('invalid_email', $error_codes)) {
                $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.', 'mgm'));
            }
            $user_email = '';
        } elseif (email_exists($user_email)) {
            if (!in_array('email_exists', $error_codes)) {
                $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.', 'mgm'));
            }
        }
    }
    // check email only #1106
    if (in_array('email_exists', $errors->get_error_codes())) {
        $label = 'email';
        $url = mgm_get_complete_registration_url('email', $user_email);
        // check
        if ($url !== FALSE) {
            // unset old errors
            unset($errors->errors);
            // set
            $errors->add('unfinished_registration', sprintf(__('<strong>ERROR</strong>: This %s has an unfinished registration. Click here to <a href="%s">complete</a>.', 'mgm'), $label, $url));
            // return form here
            return $errors;
        }
    }
    // get custom fields
    $cf_register_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true)));
    //#739 modified starts
    if (empty($show_fields)) {
        $cf_alias_fields = mgm_get_class('member_custom_fields')->get_fields_where(array('attributes' => array('capture_only' => true)));
        if (!empty($cf_alias_fields)) {
            foreach ($cf_alias_fields as $key => $array) {
                unset($cf_register_page[$key]);
            }
        }
    }
    // #739 modified ends
    // #740 starts
    $args_fields = "";
    // Show fields in short code to filter the registration form #Issue 740
    if (isset($show_fields)) {
        $package = isset($args['package']) ? $args['package'] : NULL;
        $args_fields = $show_fields;
        if (!empty($args_fields)) {
            $cf_register_page = mgm_show_fields_result($args_fields, $cf_register_page, $package);
        }
    }
    // #740 ends
    $check = 0;
    if (isset($_POST['mgm_widget_active'])) {
        if (isset($_POST['mgm_custom_fields']) && $_POST['mgm_custom_fields'] == 1) {
            $check = $_POST['mgm_custom_fields'];
        }
    } else {
        $check = 1;
    }
    // wordpress register
    $wordpres_form = mgm_check_wordpress_login();
    // check
    if ($check) {
        // loop
        foreach ($cf_register_page as $field) {
            // skip custom fields by settings call
            if ($hide_custom_fields == 'Y' || $hide_custom_fields == 'W' && $wordpres_form || $hide_custom_fields == 'C' && !$wordpres_form) {
                // some are required
                if (!in_array($field['name'], array('subscription_options', 'payment_gateways'))) {
                    continue;
                }
            }
            // skip default fields, validated already
            if (in_array($field['name'], array('username', 'email'))) {
                continue;
            }
            // by name
            switch ($field['name']) {
                case 'terms_conditions':
                    // terms & conditions
                    if (!isset($_POST['mgm_tos']) || empty($_POST['mgm_tos'])) {
                        $errors->add('mgm_tos', __('<strong>ERROR</strong>: You must accept the Terms and Conditions.', 'mgm'));
                    }
                    break;
                case 'subscription_options':
                    // subscription options
                    if (!isset($_POST['mgm_subscription']) || empty($_POST['mgm_subscription'])) {
                        $errors->add('mgm_subscription', __('<strong>ERROR</strong>: You must select a Subscription Type.', 'mgm'));
                    }
                    break;
                case 'payment_gateways':
                    // payment gateways
                    if (isset($_POST['mgm_subscription'])) {
                        // pack
                        $sub_pack = mgm_decode_package($_POST['mgm_subscription']);
                        // check
                        if (isset($sub_pack['pack_id'])) {
                            $pack = mgm_get_class('subscription_packs')->get_pack($sub_pack['pack_id']);
                            $pack_modules = array_diff($pack['modules'], array('mgm_free', 'mgm_trial'));
                            // take paid module
                            // validate
                            if (!empty($pack_modules) && (!isset($_POST['mgm_payment_gateways']) || isset($_POST['mgm_payment_gateways']) && empty($_POST['mgm_payment_gateways']))) {
                                $errors->add('mgm_subscription', __('<strong>ERROR</strong>: You must select a Payment Gateway.', 'mgm'));
                            }
                        }
                    }
                    break;
                case 'coupon':
                    if (isset($_POST['mgm_register_field']['coupon']) && !empty($_POST['mgm_register_field']['coupon'])) {
                        // coupon
                        if ($coupon_code = trim($_POST['mgm_register_field']['coupon'])) {
                            // check if its a valid coupon
                            if (!($coupon = mgm_get_coupon_data($coupon_code))) {
                                $errors->add('mgm_coupon', sprintf(__('<strong>ERROR</strong>: Coupon Code "%s" is not valid, use a valid coupon only.', 'mgm'), $coupon_code));
                            } else {
                                // get subs
                                if ($subs_pack = mgm_decode_package(mgm_post_var('mgm_subscription'))) {
                                    // values
                                    $coupon_values = mgm_get_coupon_values(NULL, $coupon['value'], true);
                                    // check
                                    if (isset($coupon_values['new_membership_type']) && $coupon_values['new_membership_type'] != $subs_pack['membership_type']) {
                                        $new_membership_type = mgm_get_membership_type_name($coupon_values['new_membership_type']);
                                        $errors->add('mgm_coupon', sprintf(__('<strong>ERROR</strong>: Coupon Code "%s" is only available with Membership Type "%s".', 'mgm'), $coupon_code, $new_membership_type));
                                    }
                                }
                            }
                        }
                    } elseif ((bool) $field['attributes']['required'] === true) {
                        $errors->add('mgm_coupon', sprintf(__('<strong>ERROR</strong>: Please enter a valid coupon code.', 'mgm')));
                    }
                    break;
                case 'birthdate':
                    // validate age
                    if (isset($_POST['mgm_register_field'][$field['name']]) && !empty($_POST['mgm_register_field'][$field['name']])) {
                        // format
                        $short_format = mgm_get_date_format('date_format_short');
                        // date
                        $birthdate = mgm_format_inputdate_to_mysql($_POST['mgm_register_field'][$field['name']], $short_format);
                        // current date
                        $current_date = mgm_get_current_datetime('Y-m-d H:i:s');
                        // add
                        if (strtotime($birthdate) > $current_date['timestamp']) {
                            $errors->add($field['name'], __('<strong>ERROR</strong>: Birthdate should not be in future.', 'mgm'));
                        } else {
                            if (isset($field['attributes']['verify_age'])) {
                                // age
                                $unit = (int) $field['attributes']['verify_age_unit'];
                                $period = $field['attributes']['verify_age_period'];
                                // check
                                if ($field['attributes']['verify_age'] == 1 && (int) $field['attributes']['verify_age_unit'] > 0) {
                                    // verify_age_period
                                    $birthdate_should = strtotime(sprintf('-%d %s', $unit, $period), $current_date['timestamp']);
                                    if (strtotime($birthdate) > $birthdate_should) {
                                        $errors->add($field['name'], sprintf(__('<strong>ERROR</strong>: Birthdate should be on or before %s.', 'mgm'), date($short_format, $birthdate_should)));
                                    }
                                }
                            }
                        }
                    }
                    // left other process run
                // left other process run
                default:
                    // on type
                    switch ($field['type']) {
                        case 'captcha':
                            // captcha
                            if (!isset($_POST['recaptcha_response_field']) || empty($_POST['recaptcha_response_field'])) {
                                $errors->add('mgm_captcha', __('<strong>ERROR</strong>: You must enter the Captcha String.', 'mgm'));
                            } else {
                                $captcha = mgm_get_class('recaptcha')->recaptcha_check_answer($_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
                                if (!isset($captcha->is_valid) || !$captcha->is_valid) {
                                    $errors->add('mgm_captcha', __('<strong>ERROR</strong>: ' . (!empty($captcha->error) ? $captcha->error : 'The Captcha String isn\'t correct.'), 'mgm'));
                                }
                            }
                            break;
                        default:
                            // check register and required
                            if ((bool) $field['attributes']['required'] === true) {
                                // error
                                $error_codes = $errors->get_error_codes();
                                // validate other
                                // confirm password
                                if ($field['name'] == 'password' || $field['name'] == 'password_conf') {
                                    if ($field['name'] == 'password' && (!isset($_POST['user_password']) || empty($_POST['user_password'])) || $field['name'] == 'password_conf' && (!isset($_POST['user_password_conf']) || empty($_POST['user_password_conf']))) {
                                        // issue #703
                                        $errors->add($field['name'], __('<strong>ERROR</strong>: You must provide a ', 'mgm') . mgm_stripslashes_deep($field['label']) . '.');
                                    } elseif ($field['name'] == 'password' && !empty($_POST['user_password']) && !empty($_POST['user_password_conf']) && (isset($field['attributes']['password_min_length']) && $field['attributes']['password_min_length'] == true || isset($field['attributes']['password_min_length']) && $field['attributes']['password_max_length'] == true)) {
                                        if (strlen($_POST['user_password']) < $field['attributes']['password_min_length_field_alias'] || strlen($_POST['user_password_conf']) < $field['attributes']['password_min_length_field_alias']) {
                                            $errors->add($field['name'], sprintf(__('<strong>ERROR</strong>:%s is too short, minimum %d characters.', 'mgm'), mgm_stripslashes_deep($field['label']), $field['attributes']['password_min_length_field_alias']));
                                        } elseif (strlen($_POST['user_password']) > $field['attributes']['password_max_length_field_alias'] || strlen($_POST['user_password_conf']) > $field['attributes']['password_max_length_field_alias']) {
                                            $errors->add($field['name'], sprintf(__('<strong>ERROR</strong>:%s is too long, maximum %d characters.', 'mgm'), mgm_stripslashes_deep($field['label']), $field['attributes']['password_max_length_field_alias']));
                                        } elseif ($field['name'] == 'password' && !empty($_POST['user_password']) && !empty($_POST['user_password_conf']) && $_POST['user_password'] != $_POST['user_password_conf']) {
                                            $errors->add($field['name'], __('<strong>ERROR</strong>: Password does not match. Please re-type.', 'mgm'));
                                        }
                                    } elseif ($field['name'] == 'password' && !empty($_POST['user_password']) && !empty($_POST['user_password_conf']) && $_POST['user_password'] != $_POST['user_password_conf']) {
                                        $errors->add($field['name'], __('<strong>ERROR</strong>: Password does not match. Please re-type.', 'mgm'));
                                    }
                                } else {
                                    //issue #1315
                                    if ($field['name'] == 'user_email' || $field['name'] == 'email_conf') {
                                        if ($field['name'] == 'email_conf' && (!isset($_POST['user_email_conf']) || empty($_POST['user_email_conf']))) {
                                            $errors->add($field['name'], __('<strong>ERROR</strong>: Please type your confirm e-mail address.', 'mgm'));
                                        } elseif (!is_email($_POST['user_email_conf'])) {
                                            $errors->add('invalid_email_conf', __('<strong>ERROR</strong>: The confirm email address isn&#8217;t correct.', 'mgm'));
                                        } elseif (email_exists($_POST['user_email_conf'])) {
                                            $errors->add('email_conf_exists', __('<strong>ERROR</strong>: This confirm email is already registered, please choose another one.', 'mgm'));
                                        } elseif (is_email($_POST['user_email']) && $_POST['user_email_conf'] != $_POST['user_email']) {
                                            $errors->add($field['name'], __('<strong>ERROR</strong>: E-mail does not match. Please re-type.', 'mgm'));
                                        }
                                    } elseif (!isset($_POST['mgm_register_field'][$field['name']]) || empty($_POST['mgm_register_field'][$field['name']])) {
                                        //issue #703
                                        $errors->add($field['name'], __('<strong>ERROR</strong>: You must provide a ', 'mgm') . mgm_stripslashes_deep($field['label']) . '.');
                                    }
                                }
                            }
                            break;
                    }
                    break;
            }
        }
    }
    // return
    return $errors;
}
/**
 * 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;
}
/**
 * validate coupon
 *
 * @param string $code
 * @param decimal $cost
 * @return array
 */
function mgm_validate_coupon($code, $cost = NULL)
{
    // get coupon
    $code = trim($code);
    // check
    if (!empty($code)) {
        // if found
        if ($coupon = mgm_get_coupon_data($code)) {
            // init
            $new_coupon = $coupon;
            // what type of coupon is it %, scalar, sub_id
            $type = mgm_get_coupon_type($coupon['value']);
            // double check we still have content
            if ($type) {
                // check on type
                switch ($type) {
                    case 'percent':
                        // string % with number, issue #135, accept period for fraction value
                        $values = mgm_get_coupon_values('percent', $coupon['value']);
                        // percent
                        $percent = $values['value'] / 100;
                        // new cost
                        if ($cost) {
                            // calc
                            $cost = $cost * (1 - $percent);
                            // zero cost
                            if ($cost < 0) {
                                $cost = 0;
                            }
                            // set
                            $new_coupon['cost'] = $cost;
                        }
                        break;
                    case 'sub_pack':
                        // sub_pack#Price_Duration-Unit_Duration-Type_Membership-Type_Billing-Cycle
                        $values = mgm_get_coupon_values('sub_pack', $coupon['value']);
                        // set
                        $new_coupon['cost'] = $values['new_cost'];
                        $new_coupon['duration'] = $values['new_duration'];
                        $new_coupon['duration_type'] = strtolower($values['new_duration_type']);
                        $new_coupon['membership_type'] = strtolower(str_replace('-', '_', $values['new_membership_type']));
                        // billing cycle:
                        if (isset($values['new_num_cycles']) && is_numeric($values['new_num_cycles'])) {
                            $new_coupon['num_cycles'] = $values['new_num_cycles'];
                        }
                        break;
                    case 'sub_pack_trial':
                        // subs_pack_trial#Trial-Duration-Unit_Trial-Duration-Type_Trial-Price_Trial-Occurrences
                        // subs_pack_trial#Trial-Price_Trial-Duration-Unit_Trial-Duration-Type_Trial-Occurrences
                        $values = mgm_get_coupon_values('sub_pack_trial', $coupon['value']);
                        // set
                        $new_coupon['trial_on'] = 1;
                        $new_coupon['trial_cost'] = $values['new_cost'];
                        $new_coupon['trial_duration'] = $values['new_duration'] * $values['new_num_cycles'];
                        $new_coupon['trial_duration_type'] = strtolower($values['new_duration_type']);
                        $new_coupon['trial_num_cycles'] = $values['new_num_cycles'];
                        break;
                    case 'scalar':
                    case 'flat':
                    default:
                        // issue #135, accept period for fraction value
                        $values = mgm_get_coupon_values('flat', $coupon['value']);
                        // cost
                        if ($cost) {
                            // calc
                            $cost = $cost - $values['value'];
                            // zero cost
                            if ($cost < 0) {
                                $cost = 0;
                            }
                            // set
                            $new_coupon['cost'] = $cost;
                        }
                        break;
                }
            }
            // format cost
            if (isset($new_coupon['cost']) && is_numeric($new_coupon['cost'])) {
                // has ',' separator
                if (strpos($new_coupon['cost'], ',') === false) {
                    $new_coupon['cost'] = number_format($new_coupon['cost'], 2, '.', '');
                } else {
                    $new_coupon['cost'] = number_format($new_coupon['cost'], 2, ',', '');
                }
            }
            // return array
            return $new_coupon;
        }
    }
    // error
    return false;
}