function eStore_check_licese_key_flag_and_generate_key($retrieved_product, $payment_data)
{
    if ($retrieved_product->create_license == 1) {
        $license_key = eStore_generate_license_key($payment_data);
        $product_license_data .= "\n" . $retrieved_product->name . " License Key: " . $license_key;
    }
    return $product_license_data;
}
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;
}