function get_download_for_variation_tx_result($cart_item_name, $ret_product, $script_location, $random_key, $payment_data = '')
{
    $download_link = get_download_for_variation($cart_item_name, $ret_product, $script_location, $random_key, $payment_data);
    $download_link = nl2br($download_link);
    $download_link = wp_eStore_replace_url_in_string_with_link($download_link);
    return $download_link;
}
function wp_eStore_send_wp_mail($to, $subject, $message, $headers = '', $attachments = '')
{
    $wp_eStore_config = WP_eStore_Config::getInstance();
    $content_type = "text/plain";
    $eStore_email_content_type = $wp_eStore_config->getValue('eStore_email_content_type');
    switch ($eStore_email_content_type) {
        case "html":
            $content_type = 'text/html';
            break;
        case "text":
            $content_type = 'text/plain';
            break;
        default:
            $content_type = 'text/plain';
            break;
    }
    //add_filter('wp_mail_from', 'eStore_get_from_address');
    //add_filter('wp_mail_from_name', 'eStore_get_from_name');
    //Massage the email body according to content type
    if ($content_type == "text/html") {
        add_filter('wp_mail_content_type', create_function('', 'return "' . $content_type . '"; '));
        $message = nl2br($message);
        $message = wp_eStore_replace_url_in_string_with_link($message);
    }
    if (empty($attachments)) {
        wp_mail($to, $subject, $message, $headers);
    } else {
        wp_mail($to, $subject, $message, $headers, $attachments);
    }
    //remove_filter( 'wp_mail_from', 'eStore_get_from_address');
    //remove_filter( 'wp_mail_from_name', 'eStore_get_from_name');
    //remove_filter( 'wp_mail_content_type', 'eStore_get_email_content_type');
}
function eStore_manual_gateway_form_processing_code()
{
    if (isset($_POST['submit_shipping'])) {
        if (eStore_get_total_cart_item_qty() < 1) {
            //Cart does not have any item
            echo '<div class="eStore_error_message">Error! Your shopping cart is empty. Please add items to your cart before checking out.</div>';
            return;
        }
        $input_verified = false;
        global $wpdb;
        $wp_eStore_config = WP_eStore_Config::getInstance();
        $err_msg = eStore_check_address_details();
        if (!empty($err_msg)) {
            $msg = '<div id="error">';
            $msg .= ESTORE_REQUIRED_FIELDS_MISSING;
            $msg .= $err_msg;
            $msg .= '</div>';
            echo $msg;
        } else {
            //Fire the begin processing hook
            $clientip = $_SERVER['REMOTE_ADDR'];
            $clientemail = $_POST['email'];
            do_action('eStore_begin_manual_co_processing', $clientemail, $clientip);
            $last_records_id = $wp_eStore_config->getValue('eStore_custom_receipt_counter');
            //get_option('eStore_custom_receipt_counter');
            if (empty($last_records_id)) {
                $last_records_id = 0;
            }
            $receipt_counter = $last_records_id + 1;
            $wp_eStore_config->setValue('eStore_custom_receipt_counter', $receipt_counter);
            $wp_eStore_config->saveConfig();
            $address = $_POST['address'] . ", " . $_POST['city'] . ", " . $_POST['state'] . " " . $_POST['postcode'] . " " . $_POST['country'];
            $payment_data = extract_manaul_co_general_payment_data($_POST['firstname'], $_POST['lastname'], $_POST['email'], $address, $_POST['phone']);
            $cart_items = extract_manual_item_data();
            $cust_direction = get_option('eStore_manual_co_cust_direction');
            $curr_symbol = get_option('cart_currency_symbol');
            if (!empty($cust_direction)) {
                $cust_direction_mod = eStore_apply_post_payment_dynamic_tags($cust_direction, $payment_data, $cart_items);
                $body .= "\n-------------------------------\n";
                $body .= $cust_direction_mod;
                $body .= "\n-------------------------------\n";
            }
            $count = 1;
            $constructed_download_link .= "<br />";
            $product_key_data = "";
            $show_tax_inc_price = $wp_eStore_config->getValue('eStore_show_tax_inclusive_price');
            foreach ($_SESSION['eStore_cart'] as $item) {
                $products_table_name = $wpdb->prefix . "wp_eStore_tbl";
                $key = $item['item_number'];
                $retrieved_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$key}'", OBJECT);
                $rounded_price = round($item['price'], 2);
                $body .= "\n" . WP_ESTORE_DETAILS_OF_ORDERED_PRODUCT . ": " . $count;
                $body .= "\n-------------------------";
                $body .= "\n" . ESTORE_PRODUCT_ID . ": " . $item['item_number'];
                $body .= "\n" . ESTORE_PRODUCT_NAME . ": " . $item['name'];
                if ($show_tax_inc_price == '1') {
                    $rounded_price = eStore_get_tax_include_price_by_prod_id($item['item_number'], $rounded_price);
                }
                $formatted_price = print_digi_cart_payment_currency($rounded_price, $curr_symbol);
                $body .= "\n" . ESTORE_PRICE . ": " . $formatted_price;
                $body .= "\n" . ESTORE_QUANTITY . ": " . $item['quantity'] . "\n";
                if (get_option('eStore_manual_co_give_download_links') != '') {
                    $download_link = generate_download_link_for_product($item['item_number'], $item['name'], $payment_data);
                    $constructed_download_link .= $download_link . "<br />";
                    $body .= $download_link . "\n";
                    $product_key = eStore_post_sale_retrieve_serial_key_and_update($retrieved_product, $item['name'], $item['quantity']);
                    $product_key_data .= $product_key;
                    $body .= $product_key;
                    $product_specific_instructions = eStore_get_product_specific_instructions($retrieved_product);
                    $product_specific_instructions = eStore_apply_post_payment_dynamic_tags($product_specific_instructions, $payment_data, $cart_items);
                    $body .= $product_specific_instructions;
                }
                $count++;
                //Check and signup WishList or WP eMember user if needed
                //if(get_option('eStore_manual_co_auto_update_db')=='1')
                if ($wp_eStore_config->getValue('eStore_manual_co_auto_create_membership') == '1') {
                    $member_ref = $retrieved_product->ref_text;
                    eStore_payment_debug('Checking if membership inegration is being used. Reference Text Value: ' . $member_ref, true);
                    if (!empty($member_ref)) {
                        if (get_option('eStore_enable_wishlist_int')) {
                            eStore_payment_debug('WishList integration is being used... doing member account creation/upgrade task... see the "subscription_handle_debug.log" file for details', true);
                            wl_handle_subsc_signup($payment_data, $member_ref, $payment_data['txn_id']);
                        } else {
                            if (function_exists('wp_eMember_install')) {
                                $eMember_id = $payment_data['eMember_userid'];
                                eStore_payment_debug('eMember integration is being used... doing member account creation/upgrade task... see the "subscription_handle_debug.log" file for details', true);
                                eMember_handle_subsc_signup($payment_data, $member_ref, $payment_data['txn_id'], $eMember_id);
                            }
                        }
                    }
                }
                //=== End of membership handling code ===
            }
            $body .= "\n-------------------------------\n";
            if ($show_tax_inc_price != '1') {
                $body .= ESTORE_SUB_TOTAL . ": " . print_digi_cart_payment_currency($_SESSION['eStore_cart_sub_total'], $curr_symbol);
            }
            if (!empty($_SESSION['eStore_cart_postage_cost'])) {
                $body .= "\n" . ESTORE_SHIPPING . ": " . print_digi_cart_payment_currency($_SESSION['eStore_cart_postage_cost'], $curr_symbol);
            }
            if (!empty($_SESSION['eStore_cart_total_tax'])) {
                $body .= "\n" . WP_ESTORE_TAX . ": " . print_digi_cart_payment_currency($_SESSION['eStore_cart_total_tax'], $curr_symbol);
            }
            $total = $_SESSION['eStore_cart_sub_total'] + $_SESSION['eStore_cart_postage_cost'] + $_SESSION['eStore_cart_total_tax'];
            $body .= "\n" . ESTORE_TOTAL . ": " . print_digi_cart_payment_currency($total, $curr_symbol);
            $conversion_rate = get_option('eStore_secondary_currency_conversion_rate');
            if (!empty($conversion_rate)) {
                $secondary_curr_symbol = get_option('eStore_secondary_currency_symbol');
                $body .= "\n" . ESTORE_TOTAL . ' (' . get_option('eStore_secondary_currency_code') . '): ' . print_digi_cart_payment_currency($total * $conversion_rate, $secondary_curr_symbol);
            }
            if (isset($_SESSION['eStore_store_pickup_checked']) && $_SESSION['eStore_store_pickup_checked'] == '1') {
                $body .= "\nStore Pickup: Yes";
            }
            $total_items = $count - 1;
            $body .= "\n" . WP_ESTORE_TOTAL_ITEMS_ORDERED . ": " . $total_items;
            $body .= "\n" . ESTORE_TRANSACTION_ID . ": " . $payment_data['txn_id'];
            $body .= "\n\n" . WP_ESTORE_CUSTOMER_DETAILS;
            $body .= "\n-------------------------";
            $body .= "\n" . WP_ESTORE_NAME . ": " . $_POST['firstname'] . " " . $_POST['lastname'];
            $body .= "\n" . ESTORE_EMAIL . ": " . $_POST['email'];
            $body .= "\n" . ESTORE_PHONE . ": " . $_POST['phone'];
            $body .= "\n" . ESTORE_ADDRESS . ": " . $_POST['address'];
            $body .= "\n" . ESTORE_CITY . ": " . $_POST['city'];
            $body .= "\n" . ESTORE_STATE . ": " . $_POST['state'];
            $body .= "\n" . ESTORE_POSTCODE . ": " . $_POST['postcode'];
            $body .= "\n" . ESTORE_COUNTRY . ": " . $_POST['country'];
            $body .= "\n" . WP_ESTORE_ADDITIONAL_COMMENT . ": " . $_POST['additional_comment'];
            $notify_email = get_option('eStore_manual_notify_email');
            $buyer_email = $_POST['email'];
            if (empty($notify_email)) {
                $notify_email = get_bloginfo('admin_email');
            }
            // Get referrer
            if (!empty($_SESSION['ap_id'])) {
                $referrer = $_SESSION['ap_id'];
            } else {
                if (isset($_COOKIE['ap_id'])) {
                    $referrer = $_COOKIE['ap_id'];
                }
            }
            //Call the filter for email notification body
            eStore_payment_debug('Applying filter - eStore_notification_email_body_filter', true);
            $body = apply_filters('eStore_notification_email_body_filter', $body, $payment_data, $cart_items);
            $seller_email_body = $body . "\n\n" . WP_ESTORE_REFERRER . ": " . $referrer;
            $from_email_address = get_option('eStore_download_email_address');
            $headers = 'From: ' . $from_email_address . "\r\n";
            // Notify Seller
            $n_subject = $wp_eStore_config->getValue('seller_email_subject_manual_co');
            if (empty($n_subject)) {
                $n_subject = get_option('eStore_seller_email_subj');
            }
            wp_mail($notify_email, $n_subject, $seller_email_body, $headers);
            // Notify Buyer
            $buyer_email_subj = $wp_eStore_config->getValue('buyer_email_subject_manual_co');
            if (empty($buyer_email_subj)) {
                $buyer_email_subj = get_option('eStore_buyer_email_subj');
            }
            wp_mail($buyer_email, $buyer_email_subj, $body, $headers);
            if (!empty($product_key_data)) {
                //Lets add any serial key info to the data
                $payment_data['product_key_data'] = $product_key_data;
            }
            //Fire the manual checkout hook
            do_action('eStore_manual_checkout_form_data', $payment_data, $cart_items);
            //Add to the customer database if the option is enabled
            if (get_option('eStore_manual_co_auto_update_db') == '1') {
                record_sales_data($payment_data, $cart_items);
            }
            //Perform autoresponder signup
            if (get_option('eStore_manual_co_do_autoresponder_signup') == '1') {
                eStore_item_specific_autoresponder_signup($cart_items, $_POST['firstname'], $_POST['lastname'], $_POST['email']);
                eStore_global_autoresponder_signup($_POST['firstname'], $_POST['lastname'], $_POST['email']);
            }
            //Award Affiliate Commission
            eStore_award_commission_manual_co($payment_data, $cart_items);
            // Revenue sharing
            eStore_award_author_commission_manual_co($payment_data, $cart_items);
            //Create affiliate account if needed
            eStore_handle_auto_affiliate_account_creation($payment_data);
            //Post IPN data to external site if needed
            eStore_POST_IPN_data_to_url($payment_data, '', $cart_items);
            //Save transaction result for thank you page display
            if (get_option('eStore_manual_co_give_download_links') != '') {
                $constructed_download_link = wp_eStore_replace_url_in_string_with_link($constructed_download_link);
                eStore_save_trans_result_for_thank_you_page_display($payment_data, $constructed_download_link, $cart_items);
            }
            $return_url = get_option('eStore_manual_return_url');
            if (empty($return_url)) {
                $return_url = get_bloginfo('wpurl');
            }
            //Google analytics tracking
            if (get_option('eStore_enable_analytics_tracking') && get_option('eStore_manual_co_give_download_links') != '') {
                eStore_track_ga_ecommerce($payment_data, $cart_items);
                $return_url = eStore_append_http_get_data_to_url($return_url, "eStore_manual_co_track_ga", "1");
            }
            //Reset cart and redirect to Thank you page
            reset_eStore_cart();
            eStore_redirect_to_url($return_url);
        }
    }
}
function process_payment_data(&$payment_data, $cart_items)
{
    global $wpdb, $wp_eStore_config;
    $wp_eStore_config = WP_eStore_Config::getInstance();
    $products_table_name = WP_ESTORE_PRODUCTS_TABLE_NAME;
    $script_location = get_option('eStore_download_script');
    $random_key = get_option('eStore_random_code');
    $payment_currency = get_option('cart_payment_currency');
    $customvariables = get_custom_var($payment_data['custom']);
    $product_specific_instructions = "";
    $currency_symbol = get_option('cart_currency_symbol');
    //Fire the begin processing hook
    do_action('eStore_begin_payment_processing', $payment_data['payer_email'], $customvariables['ip']);
    $product_id_array = array();
    $product_name_array = array();
    $product_price_array = array();
    $product_qty_array = array();
    $download_link_array = array();
    $download_link_for_digital_item = array();
    $counter = 0;
    $product_key_data = "";
    foreach ($cart_items as $current_cart_item) {
        $cart_item_data_num = $current_cart_item['item_number'];
        $cart_item_data_name = $current_cart_item['item_name'];
        $cart_item_data_quantity = $current_cart_item['quantity'];
        $cart_item_data_total = $current_cart_item['mc_gross'];
        $cart_item_data_currency = $current_cart_item['mc_currency'];
        eStore_payment_debug('Item Number: ' . $cart_item_data_num, true);
        eStore_payment_debug('Item Name: ' . $cart_item_data_name, true);
        eStore_payment_debug('Item Quantity: ' . $cart_item_data_quantity, true);
        eStore_payment_debug('Item Total: ' . $cart_item_data_total, true);
        eStore_payment_debug('Item Currency: ' . $cart_item_data_currency, true);
        if ($cart_item_data_num != "SHIPPING") {
            // Compare the values with the values stored in the database
            $key = $cart_item_data_num;
            $retrieved_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$key}'", OBJECT);
            if (!$retrieved_product) {
                eStore_payment_debug('No Item found for the Item ID: ' . $cart_item_data_num, false);
                return false;
            }
            $coupon_code = $customvariables['coupon'];
            if (!empty($coupon_code)) {
                eStore_payment_debug('Coupon Code Used : ' . $coupon_code, true);
                $coupon_table_name = $wpdb->prefix . "wp_eStore_coupon_tbl";
                $ret_coupon = $wpdb->get_row("SELECT * FROM {$coupon_table_name} WHERE coupon_code = '{$coupon_code}'", OBJECT);
                if ($ret_coupon) {
                    $discount_amount = $ret_coupon->discount_value;
                    $discount_type = $ret_coupon->discount_type;
                    if ($discount_type == 0) {
                        //apply % discount
                        $discount = $retrieved_product->price * $discount_amount / 100;
                        $true_product_price = $retrieved_product->price - $discount;
                        eStore_payment_debug('Product Price after applying % discount: ' . $true_product_price, true);
                    } else {
                        // apply value discount
                        $true_product_price = $retrieved_product->price - $discount_amount;
                        eStore_payment_debug('Product Price after applying fixed amount discount: ' . $true_product_price, true);
                    }
                } else {
                    eStore_payment_debug('Could not find the coupon in the database: ' . $coupon_code, false);
                }
            } else {
                if (is_numeric($retrieved_product->a3)) {
                    $true_product_price = 0;
                    //subscription product
                } else {
                    if (is_numeric($retrieved_product->price)) {
                        $true_product_price = $retrieved_product->price * $cart_item_data_quantity;
                    } else {
                        $true_product_price = 0;
                        //most likely a subscription
                    }
                }
            }
            $true_product_price = round($true_product_price, 2);
            if ($cart_item_data_total < $true_product_price) {
                eStore_payment_debug('Wrong Product Price Detected. Actual Product Price : ' . $true_product_price, false);
                eStore_payment_debug('Paid Product Price : ' . $cart_item_data_total, false);
                return false;
            }
            if ($payment_currency != $cart_item_data_currency) {
                eStore_payment_debug('Invalid Product Currency. Expected currency: ' . $payment_currency . ', Received Currency: ' . $cart_item_data_currency, false);
                return false;
            }
            //*** Handle Membership Payment ***
            eStore_payment_debug('Checking if membership inegration is being used.', true);
            $member_ref = $retrieved_product->ref_text;
            if (!empty($member_ref)) {
                if (get_option('eStore_enable_wishlist_int')) {
                    eStore_payment_debug('WishList integration is being used... creating member account... see the "subscription_handle_debug.log" file for details', true);
                    wl_handle_subsc_signup($payment_data, $member_ref, $payment_data['txn_id']);
                } else {
                    if (function_exists('wp_eMember_install')) {
                        $eMember_id = $customvariables['eMember_id'];
                        eStore_payment_debug('eMember integration is being used... creating member account... see the "subscription_handle_debug.log" file for details', true);
                        eMember_handle_subsc_signup($payment_data, $member_ref, $payment_data['txn_id'], $eMember_id);
                    }
                }
            }
            //== End of Membership payment handling ==
            $item_name = $cart_item_data_name;
            //$retrieved_product->name;
            $download_link = generate_download_link($retrieved_product, $item_name, $payment_data);
            eStore_payment_debug('Download Link: [hidden]', true);
            //$download_link
            $product_specific_instructions .= eStore_get_product_specific_instructions($retrieved_product);
            if ($retrieved_product->create_license == 1) {
                $license_key = eStore_generate_license_key($payment_data);
                $product_license_data .= "\n" . $cart_item_data_name . " License Key: " . $license_key;
                eStore_payment_debug('License Key: [hidden]', true);
                //$license_key
            }
            //Issue serial key if this feature is being used
            $product_key_data .= eStore_post_sale_retrieve_serial_key_and_update($retrieved_product, $cart_item_data_name, $cart_item_data_quantity);
            array_push($product_name_array, $cart_item_data_name);
            array_push($product_id_array, $cart_item_data_num);
            if (empty($cart_item_data_total)) {
                $cart_item_data_total = $retrieved_product->price;
            }
            array_push($product_price_array, $cart_item_data_total);
            array_push($product_qty_array, $cart_item_data_quantity);
            array_push($download_link_array, $download_link);
            if (eStore_check_if_string_contains_url($download_link)) {
                array_push($download_link_for_digital_item, $download_link);
            }
        }
        $counter++;
    }
    if (!empty($product_key_data)) {
        $payment_data['product_key_data'] = $product_key_data;
    }
    // How long the download link remain valid (hours)
    $download_url_life = get_option('eStore_download_url_life');
    // Emails
    $notify_email = get_option('eStore_notify_email_address');
    // Email which will recive notification of sale (sellers email)
    $download_email = get_option('eStore_download_email_address');
    // Email from which the mail wil be sent
    $email_subject = get_option('eStore_buyer_email_subj');
    $email_body = get_option('eStore_buyer_email_body');
    $notify_subject = get_option('eStore_seller_email_subj');
    $notify_body = get_option('eStore_seller_email_body');
    // Send the product
    for ($i = 0; $i < sizeof($product_name_array); $i++) {
        $constructed_products_name .= $product_name_array[$i];
        $constructed_products_name .= ", ";
        $constructed_products_price .= $product_price_array[$i];
        $constructed_products_price .= ", ";
        $constructed_products_id .= $product_id_array[$i];
        $constructed_products_id .= ", ";
        $constructed_products_details .= "\n" . $product_name_array[$i] . " x " . $product_qty_array[$i] . " - " . $currency_symbol . $product_price_array[$i] . " (" . $payment_currency . ")";
        $tax_inc_price = eStore_get_tax_include_price_by_prod_id($product_id_array[$i], $product_price_array[$i]);
        $constructed_products_details_tax_inc .= "\n" . $product_name_array[$i] . " x " . $product_qty_array[$i] . " - " . $currency_symbol . $tax_inc_price . " (" . $payment_currency . ")";
        //Download links list for all items in the cart
        $constructed_download_link .= "\n";
        if (is_array($download_link_array[$i])) {
            $package_downloads = $download_link_array[$i];
            for ($j = 0; $j < sizeof($package_downloads); $j++) {
                $constructed_download_link .= $package_downloads[$j];
                $constructed_download_link .= "\n";
            }
        } else {
            $constructed_download_link .= $download_link_array[$i];
        }
        //Download links for only digital items in the cart
        $constructed_download_link_for_digital_item .= "\n";
        if (is_array($download_link_for_digital_item[$i])) {
            $package_downloads2 = $download_link_for_digital_item[$i];
            for ($j = 0; $j < sizeof($package_downloads2); $j++) {
                $constructed_download_link_for_digital_item .= $package_downloads2[$j];
                $constructed_download_link_for_digital_item .= "\n";
            }
        } else {
            $constructed_download_link_for_digital_item .= $download_link_for_digital_item[$i];
        }
    }
    //Counter for incremental receipt number
    $last_records_id = $wp_eStore_config->getValue('eStore_custom_receipt_counter');
    //get_option('eStore_custom_receipt_counter');
    if (empty($last_records_id)) {
        $last_records_id = 0;
    }
    $receipt_counter = $last_records_id + 1;
    eStore_payment_debug('Incremental counter value: ' . $receipt_counter, true);
    $wp_eStore_config->setValue('eStore_custom_receipt_counter', $receipt_counter);
    $wp_eStore_config->saveConfig();
    $purchase_date = date("Y-m-d");
    //$total_purchase_amt = $payment_data['mc_gross'];
    $total_minus_total_tax = number_format($payment_data['mc_gross'] - $payment_data['mc_tax'], 2);
    $txn_id = $payment_data['txn_id'];
    $buyer_shipping_info = $payment_data['address'];
    $buyer_phone = $payment_data['phone'];
    $shipping_option = $customvariables['ship_option'];
    if (empty($shipping_option)) {
        $shipping_option = "Default";
    }
    $product_specific_instructions = eStore_apply_post_payment_dynamic_tags($product_specific_instructions, $payment_data, $cart_items);
    //$tags = array("{first_name}","{last_name}","{payer_email}","{product_name}","{product_link}","{product_price}","{product_id}","{download_life}","{product_specific_instructions}","{product_details}","{shipping_info}","{license_data}","{purchase_date}","{purchase_amt}","{transaction_id}","{shipping_option_selected}","{product_link_digital_items_only}","{total_tax}","{total_shipping}","{total_minus_total_tax}","{customer_phone}","{counter}","{coupon_code}","{serial_key}");
    //$vals = array($payment_data['first_name'],$payment_data['last_name'],$payment_data['payer_email'],$constructed_products_name,$constructed_download_link,$constructed_products_price,$constructed_products_id,$download_url_life,$product_specific_instructions,$constructed_products_details,$buyer_shipping_info,$product_license_data,$purchase_date,$payment_data['mc_gross'],$txn_id,$shipping_option,$constructed_download_link_for_digital_item,$payment_data['mc_tax'],$payment_data['mc_shipping'],$total_minus_total_tax,$buyer_phone,$receipt_counter,$coupon_code,$product_key_data);
    $additional_data = array();
    $additional_data['constructed_products_name'] = $constructed_products_name;
    $additional_data['constructed_products_price'] = $constructed_products_price;
    $additional_data['constructed_products_id'] = $constructed_products_id;
    $additional_data['constructed_products_details'] = $constructed_products_details;
    $additional_data['constructed_products_details_tax_inc'] = $constructed_products_details_tax_inc;
    $additional_data['product_specific_instructions'] = $product_specific_instructions;
    $additional_data['constructed_download_link'] = $constructed_download_link;
    $additional_data['constructed_download_link_for_digital_item'] = $constructed_download_link_for_digital_item;
    $additional_data['product_license_data'] = $product_license_data;
    //this is the license mgr key (not the normal serial key code)
    $subject = eStore_apply_post_payment_dynamic_tags($email_subject, $payment_data, $cart_items, $additional_data);
    //str_replace($tags,$vals,$email_subject);
    $body = eStore_apply_post_payment_dynamic_tags($email_body, $payment_data, $cart_items, $additional_data);
    //str_replace($tags,$vals,$email_body);
    $headers = 'From: ' . $download_email . "\r\n";
    $attachment = '';
    //Call the filter for email notification body
    eStore_payment_debug('Applying filter - eStore_notification_email_body_filter', true);
    $body = apply_filters('eStore_notification_email_body_filter', $body, $payment_data, $cart_items);
    eStore_payment_debug('Sending product email to : ' . $payment_data["payer_email"], true);
    if (get_option('eStore_use_wp_mail')) {
        wp_eStore_send_wp_mail($payment_data['payer_email'], $subject, $body, $headers);
        //wp_mail($payment_data['payer_email'], $subject, $body, $headers);
        eStore_payment_debug('Product Email successfully sent to ' . $payment_data['payer_email'] . '.', true);
    } else {
        if (@eStore_send_mail($payment_data['payer_email'], $body, $subject, $download_email, $attachment)) {
            eStore_payment_debug('Product Email successfully sent (using PHP mail) to ' . $payment_data['payer_email'] . '.', true);
        } else {
            eStore_payment_debug('Error sending product Email (using PHP mail) to ' . $payment_data['payer_email'] . '.', false);
        }
    }
    // Notify seller
    foreach ($payment_data as $key => $value) {
        $post_string .= "{$key}={$value}, ";
    }
    $n_subject = eStore_apply_post_payment_dynamic_tags($notify_subject, $payment_data, $cart_items, $additional_data);
    //str_replace($tags,$vals,$notify_subject);
    $n_body = eStore_apply_post_payment_dynamic_tags($notify_body, $payment_data, $cart_items, $additional_data);
    //str_replace($tags,$vals,$notify_body);
    if ($wp_eStore_config->getValue('eStore_add_payment_parameters_to_admin_email') == '1') {
        $n_body .= "\n\n------- User Email ----------\n" . $body . "\n\n------- Paypal Parameters (Only admin will receive this) -----\n" . $post_string;
    }
    $n_body = stripslashes($n_body);
    $notify_emails_array = explode(",", $notify_email);
    foreach ($notify_emails_array as $notify_email_address) {
        if (!empty($notify_email_address)) {
            $recipient_email_address = trim($notify_email_address);
            if (get_option('eStore_use_wp_mail')) {
                wp_eStore_send_wp_mail($recipient_email_address, $n_subject, $n_body, $headers);
                //wp_mail($recipient_email_address, $n_subject, $n_body, $headers);
                eStore_payment_debug('Notify Email successfully sent to ' . $recipient_email_address . '.', true);
            } else {
                if (@eStore_send_mail($recipient_email_address, $n_body, $n_subject, $download_email)) {
                    eStore_payment_debug('Notify Email successfully sent (using PHP mail) to ' . $recipient_email_address . '.', true);
                } else {
                    eStore_payment_debug('Error sending notify Email (using PHP mail) to ' . $recipient_email_address . '.', false);
                }
            }
        }
    }
    //Record details for the Thank You page display
    eStore_payment_debug('Creating transaction result display value', true);
    //Save transaction result for thank you page display
    $constructed_download_link = nl2br($constructed_download_link);
    $constructed_download_link = wp_eStore_replace_url_in_string_with_link($constructed_download_link);
    eStore_save_trans_result_for_thank_you_page_display($payment_data, $constructed_download_link, $cart_items);
    global $wp_eStore_transaction_result_display_content;
    $wp_eStore_transaction_result_display_content = $_SESSION['eStore_tx_result'];
    eStore_payment_debug('Transaction result display value set', true);
    return true;
}
function eStore_display_members_purchase_history($user_id, $show_download = false)
{
    global $wpdb;
    $customer_table_name = WP_ESTORE_CUSTOMER_TABLE_NAME;
    $products_table_name = WP_ESTORE_PRODUCTS_TABLE_NAME;
    $ret_customer_db = $wpdb->get_results("SELECT * FROM {$customer_table_name} WHERE member_username = '******'", OBJECT);
    if (!$show_download) {
        $output .= '
		<table class="widefat">
		<thead><tr>
		<th scope="col">' . ESTORE_PRODUCT_NAME . '</th>	
	    <th scope="col">' . ESTORE_DATE . '</th>
	    <th scope="col">' . ESTORE_PRICE_PAID . '</th>
		</tr></thead>
		<tbody>';
        if ($ret_customer_db) {
            foreach ($ret_customer_db as $ret_customer_db) {
                $ret_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$ret_customer_db->purchased_product_id}'", OBJECT);
                $product_details = $ret_customer_db->product_name;
                if (!empty($ret_product->product_url)) {
                    $product_details = '<a href="' . $ret_product->product_url . '" target="_blank">' . $ret_customer_db->product_name . '</a>';
                }
                $output .= '<tr>';
                $output .= '<td align="left">' . $product_details . '</td>';
                $output .= '<td align="center">' . $ret_customer_db->date . '</td>';
                $output .= '<td align="center">' . $ret_customer_db->sale_amount . '</td>';
                $output .= '</tr>';
            }
        } else {
            $output .= '<tr><td colspan="4">' . ESTORE_NO_PURCHASE_FOUND . '</td></tr>';
        }
        $output .= '</tbody></table>';
    } else {
        if (isset($_POST['eStore_purchase_history_generate_download_now_button'])) {
            //Show the generated download links
            $id = strip_tags($_POST['download_now_product_id']);
            $retrieved_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$id}'", OBJECT);
            //$payment_data = free_download_pseudo_payment_data($cust_name, $cust_email);
            $item_name = strip_tags($_POST['download_now_item_name']);
            $download_link = generate_download_link($retrieved_product, $item_name);
            $download_link = nl2br($download_link);
            $download_link = wp_eStore_replace_url_in_string_with_link($download_link);
            $output .= '<p style="border:1px solid #ccc; padding:10px;">' . $download_link . '</p>';
        }
        $output .= '
		<table class="widefat">
		<thead><tr>
		<th scope="col">' . ESTORE_PRODUCT_NAME . '</th>	
	    <th scope="col">' . ESTORE_DATE . '</th>
	    <th scope="col">' . ESTORE_PRICE_PAID . '</th>
	    <th scope="col">' . ESTORE_GENERATE_DOWNLOAD . '</th>
		</tr></thead>
		<tbody>';
        if ($ret_customer_db) {
            foreach ($ret_customer_db as $ret_customer_db) {
                $ret_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$ret_customer_db->purchased_product_id}'", OBJECT);
                if ($ret_product) {
                    $product_details = $ret_customer_db->product_name;
                    if (!empty($ret_product->product_url)) {
                        $product_details = '<a href="' . $ret_product->product_url . '" target="_blank">' . $ret_customer_db->product_name . '</a>';
                    }
                    $output .= '<tr>';
                    $output .= '<td align="left">' . $product_details . '</td>';
                    $output .= '<td align="center">' . $ret_customer_db->date . '</td>';
                    $output .= '<td align="center">' . $ret_customer_db->sale_amount . '</td>';
                    $output .= '<td align="center">';
                    $output .= '<form method="post" action="" style="display:inline">';
                    $output .= '<input type="hidden" name="eStore_purchase_history_generate_download_now_button" value="1" />';
                    $output .= '<input type="hidden" name="download_now_product_id" value="' . $ret_product->id . '" />';
                    $output .= '<input type="hidden" name="download_now_item_name" value="' . $ret_customer_db->product_name . '" />';
                    $output .= '<input type="submit" name="submit" class="generate_download_now_button" value="' . ESTORE_GENERATE_DOWNLOAD . '" />';
                    $output .= '</form>';
                    $output .= '</td>';
                    $output .= '</tr>';
                } else {
                    $output .= "<br />Error - Could not find the requested product ID in the products database!";
                }
            }
        } else {
            $output .= '<tr><td colspan="4">' . ESTORE_NO_PURCHASE_FOUND . '</td></tr>';
        }
        $output .= '</tbody></table>';
    }
    return $output;
}