function generate_download_link($retrieved_product, $item_name, $payment_data = '')
{
    $time = time();
    global $wpdb;
    global $products_table_name;
    if (isset($retrieved_product)) {
        $product_id = $retrieved_product->id;
    } else {
        $product_id = "";
    }
    $script_location = get_option('eStore_download_script');
    $random_key = get_option('eStore_random_code');
    $eStore_auto_shorten_url = WP_ESTORE_AUTO_SHORTEN_DOWNLOAD_LINKS;
    //Check if nextgen gallery integration is being used
    $pid_check_value = eStore_is_ngg_pid_present($item_name);
    if ($pid_check_value != -1) {
        $pictureID = $pid_check_value;
        $download_link = eStore_get_ngg_image_url($pictureID, $item_name);
        //Generate link from Nextgen gallery
        return $download_link;
    }
    //check if it is a digital variation
    $is_digital_variation = false;
    if (!empty($retrieved_product->variation3) && eStore_check_if_string_contains_url($retrieved_product->variation3)) {
        $is_digital_variation = true;
    }
    if (empty($retrieved_product->product_download_url) && !$is_digital_variation) {
        $download_link = "\n" . $item_name . WP_ESTORE_THIS_ITEM_DOES_NOT_HAVE_DOWNLOAD;
    } else {
        if (empty($payment_data['txn_id'])) {
            $payment_data['txn_id'] = "";
        }
        if (!empty($retrieved_product->variation3)) {
            $download_link = get_download_for_variation($item_name, $retrieved_product, $script_location, $random_key, $payment_data);
        } else {
            $download_url_field = $retrieved_product->product_download_url;
            $product_ids = explode(',', $download_url_field);
            $package_product = eStore_is_package_product($retrieved_product);
            $multi_parts = false;
            if (sizeof($product_ids) > 1 && !$package_product) {
                $multi_parts = true;
            }
            if ($package_product) {
                eStore_payment_debug('Generating download link for package product.', true);
                //$count = 0;
                foreach ($product_ids as $id) {
                    $id = trim($id);
                    $retrieved_product_for_specific_id = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$id}'", OBJECT);
                    //recursively generate the links
                    $download_link .= generate_download_link($retrieved_product_for_specific_id, $retrieved_product_for_specific_id->name, $payment_data);
                }
            } else {
                if ($multi_parts) {
                    eStore_payment_debug('Generating download link for multi part file.', true);
                    $count = 1;
                    $download_link .= "\n" . stripslashes($item_name) . " - ";
                    foreach ($product_ids as $id) {
                        $id = trim($id);
                        if (!empty($id)) {
                            //$download = $product_id.'|'.$time.'|'.$id;
                            //$download_key = eStore_generate_download_key($product_id,$id);
                            $download_key = eStore_check_stamping_flag_and_generate_download_key($retrieved_product, $product_id, $id, $payment_data);
                            $encrypted_download_url = eStore_construct_raw_encrypted_dl_url($download_key);
                            $download_item = "\n" . ESTORE_PART . " " . $count . " : " . $encrypted_download_url;
                            $download_link .= $download_item;
                            eStore_register_link_in_db('', $download_key, $download_item, '', '', '', 0, $payment_data['txn_id']);
                            $count++;
                        }
                    }
                } else {
                    eStore_payment_debug('Generating download link for single file.', true);
                    //$download = $product_id.'|'.$time;
                    //$download_key = eStore_generate_download_key($product_id);
                    $download_key = eStore_check_stamping_flag_and_generate_download_key($retrieved_product, $product_id, '', $payment_data);
                    $encrypted_download_url = eStore_construct_raw_encrypted_dl_url($download_key);
                    $download_link = "\n" . stripslashes($item_name) . " - " . $encrypted_download_url;
                    eStore_register_link_in_db('', $download_key, $download_link, '', '', '', 0, $payment_data['txn_id']);
                }
            }
        }
    }
    return $download_link;
}
Ejemplo n.º 2
0
function eStore_process_PDT_payment_data($keyarray)
{
    setcookie("cart_in_use", "true", time() + 21600, "/");
    //TODO - refactor using eStore_do_thank_you_page_display_tasks()
    //TODO - do a multi-submission check
    eStore_payment_debug("Processing PayPal PDT...", true);
    global $tx_result_msg, $tx_result_error_msg;
    if (get_option('eStore_strict_email_check') != '') {
        $seller_paypal_email = get_option('cart_paypal_email');
        if ($seller_paypal_email != $keyarray['receiver_email']) {
            $tx_result_error_msg .= 'Invalid Seller Paypal Email Address Detected: ' . $keyarray['receiver_email'];
            eStore_payment_debug('Invalid Seller Paypal Email Address Detected: ' . $keyarray['receiver_email'], fales);
            return false;
        }
    }
    $payment_status = $keyarray['payment_status'];
    if ($payment_status != "Completed" && $payment_status != "Processed") {
        $tx_result_error_msg .= ESTORE_PENDING_PAYMENT_EMAIL_BODY;
        //'The Fund have not been cleared yet. Product will be delivered when the fund clears!';
        eStore_payment_debug("The fund did not clear. Product will be delivered via email when the fund clears! Payment Status: " . $payment_status, false);
        return false;
    }
    $custom = $keyarray['custom'];
    $delimiter = "&";
    $customvariables = array();
    $namevaluecombos = explode($delimiter, $custom);
    foreach ($namevaluecombos as $keyval_unparsed) {
        $equalsignposition = strpos($keyval_unparsed, '=');
        if ($equalsignposition === false) {
            $customvariables[$keyval_unparsed] = '';
            continue;
        }
        $key = substr($keyval_unparsed, 0, $equalsignposition);
        $value = substr($keyval_unparsed, $equalsignposition + 1);
        $customvariables[$key] = $value;
    }
    isset($customvariables['ngg_pid']) ? $pictureID = $customvariables['ngg_pid'] : ($pictureID = '');
    //$pictureID = $customvariables['ngg_pid'];
    //Fire the begin processing hook
    do_action('eStore_begin_paypal_pdt_processing', $keyarray['payer_email'], $customvariables['ip']);
    $transaction_type = $keyarray['txn_type'];
    $transaction_id = $keyarray['txn_id'];
    $transaction_subject = $keyarray['transaction_subject'];
    $gross_total = $keyarray['mc_gross'];
    if ($transaction_type == "cart") {
        // Cart Items
        $num_cart_items = $keyarray['num_cart_items'];
        $tx_result_msg .= 'Number of Cart Items: ' . $num_cart_items;
        $i = 1;
        $cart_items = array();
        while ($i < $num_cart_items + 1) {
            $item_number = $keyarray['item_number' . $i];
            $item_name = $keyarray['item_name' . $i];
            $quantity = $keyarray['quantity' . $i];
            $mc_gross = $keyarray['mc_gross_' . $i];
            $mc_currency = $keyarray['mc_currency'];
            $current_item = array('item_number' => $item_number, 'item_name' => $item_name, 'quantity' => $quantity, 'mc_gross' => $mc_gross, 'mc_currency' => $mc_currency);
            array_push($cart_items, $current_item);
            $i++;
        }
    } else {
        $cart_items = array();
        $tx_result_msg .= 'Transaction Type: Buy Now/Subscribe';
        $item_number = $keyarray['item_number'];
        $item_name = $keyarray['item_name'];
        $quantity = $keyarray['quantity'];
        $mc_gross = $keyarray['mc_gross'];
        $mc_currency = $keyarray['mc_currency'];
        $current_item = array('item_number' => $item_number, 'item_name' => $item_name, 'quantity' => $quantity, 'mc_gross' => $mc_gross, 'mc_currency' => $mc_currency);
        array_push($cart_items, $current_item);
    }
    $script_location = get_option('eStore_download_script');
    $random_key = get_option('eStore_random_code');
    global $wpdb;
    $products_table_name = $wpdb->prefix . "wp_eStore_tbl";
    $customer_table_name = $wpdb->prefix . "wp_eStore_customer_tbl";
    $sales_table_name = $wpdb->prefix . "wp_eStore_sales_tbl";
    $payment_currency = get_option('cart_payment_currency');
    $product_id_array = array();
    $product_name_array = array();
    $product_price_array = array();
    $product_qty_array = array();
    $download_link_array = array();
    $counter = 0;
    foreach ($cart_items as $current_cart_item) {
        $cart_item_data_num = $current_cart_item['item_number'];
        $key = $cart_item_data_num;
        $retrieved_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$key}'", OBJECT);
        $cart_item_data_name = trim($current_cart_item['item_name']);
        //$retrieved_product->name;
        //$cart_item_data_name = mb_convert_encoding($cart_item_data_name, "UTF-8");
        $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'];
        $tx_result_msg .= '<br />Item Number: ' . $cart_item_data_num;
        $tx_result_msg .= '<br />Item Name: ' . $cart_item_data_name;
        $tx_result_msg .= '<br />Item Quantity: ' . $cart_item_data_quantity;
        $tx_result_msg .= '<br />Item Total: ' . $cart_item_data_total;
        $tx_result_msg .= '<br />Item Currency: ' . $cart_item_data_currency;
        // Compare the values with the values stored in the database
        isset($customvariables['coupon']) ? $coupon_code = $customvariables['coupon'] : ($coupon_code = '');
        //$coupon_code = $customvariables['coupon'];
        if (!empty($coupon_code)) {
            $tx_result_msg .= 'Coupon Code Used : ' . $coupon_code;
            $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;
                } else {
                    // apply value discount
                    $true_product_price = $retrieved_product->price - $discount_amount;
                }
            }
        } else {
            $true_product_price = $retrieved_product->price * $cart_item_data_quantity;
        }
        $check_price = true;
        $msg = "";
        $msg = apply_filters('eStore_before_checking_price_filter', $msg, $current_cart_item);
        if (!empty($msg) && $msg == "currency-check-override") {
            $check_price = false;
            $tx_result_msg .= '<br />PDT check - Price and currency check override enabled by filter eStore_before_checking_price_filter';
        }
        if ($check_price) {
            $true_product_price = round($true_product_price, 2);
            if ($cart_item_data_total < $true_product_price) {
                $tx_result_error_msg .= 'Wrong Product Price Detected. Actual Product Price : ' . $true_product_price;
                $tx_result_error_msg .= 'Paid Product Price : ' . $cart_item_data_total;
                return false;
            }
            if (!empty($retrieved_product->currency_code)) {
                $payment_currency = $retrieved_product->currency_code;
            }
            if ($payment_currency != $cart_item_data_currency) {
                $tx_result_error_msg .= 'Invalid Product Currency : ' . $cart_item_data_currency;
                return false;
            }
        }
        //Check if nextgen gallery integration is being used
        $pid_check_value = eStore_is_ngg_pid_present($cart_item_data_name);
        if ($pid_check_value != -1) {
            $pictureID = $pid_check_value;
        }
        if (!empty($pictureID)) {
            $download_link = eStore_get_ngg_image_url_html($pictureID, $cart_item_data_name);
            $pictureID = "";
        } else {
            $eStore_auto_shorten_url = WP_ESTORE_AUTO_SHORTEN_DOWNLOAD_LINKS;
            $dl_link_target = 'target="_self"';
            if (WP_ESTORE_OPEN_IN_NEW_WINDOW_THANKU_DL_LINKS == '1') {
                $dl_link_target = 'target="_blank"';
            }
            $product_id = $retrieved_product->id;
            //check if it is a digital variation
            $is_digital_variation = false;
            if (!empty($retrieved_product->variation3) && eStore_check_if_string_contains_url($retrieved_product->variation3)) {
                $is_digital_variation = true;
            }
            if (empty($retrieved_product->product_download_url) && !$is_digital_variation) {
                $download_link = "<br /><strong>" . $cart_item_data_name . "</strong>" . WP_ESTORE_THIS_ITEM_DOES_NOT_HAVE_DOWNLOAD;
            } else {
                $payment_data = array();
                $payment_data['customer_name'] = $keyarray['first_name'] . " " . $keyarray['last_name'];
                $payment_data['payer_email'] = $keyarray['payer_email'];
                isset($keyarray['contact_phone']) ? $payment_data['contact_phone'] = $keyarray['contact_phone'] : ($payment_data['contact_phone'] = '');
                $payment_data['address'] = $keyarray['address_street'] . ", " . $keyarray['address_city'] . ", " . $keyarray['address_state'] . " " . $keyarray['address_zip'] . ", " . $keyarray['address_country'];
                $payment_data['txn_id'] = $keyarray['txn_id'];
                if (!empty($retrieved_product->variation3)) {
                    $download_link = get_download_for_variation_tx_result($cart_item_data_name, $retrieved_product, $script_location, $random_key, $payment_data);
                } else {
                    $download_url_field = $retrieved_product->product_download_url;
                    $product_ids = explode(',', $download_url_field);
                    $package_product = true;
                    $multi_parts = false;
                    foreach ($product_ids as $id) {
                        if (!is_numeric($id)) {
                            $package_product = false;
                        }
                    }
                    if (sizeof($product_ids) > 1 && !$package_product) {
                        $multi_parts = true;
                    }
                    if ($package_product) {
                        $tx_result_msg .= 'The product is a package product.';
                        foreach ($product_ids as $id) {
                            $id = trim($id);
                            $retrieved_product_for_id = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$id}'", OBJECT);
                            $download_key = eStore_check_stamping_flag_and_generate_download_key($retrieved_product_for_id, $retrieved_product_for_id->id, '', $payment_data);
                            $download_url = eStore_construct_raw_encrypted_dl_url($download_key);
                            if (WP_ESTORE_USE_ANCHOR_FOR_THANKU_DL_LINKS === '1') {
                                $raw_download = '<a href="' . $download_url . '" ' . $dl_link_target . '>' . WP_ESTORE_CLICK_HERE_TO_DOWNLOAD . '</a>';
                            } else {
                                $raw_download = '<a href="' . $download_url . '" ' . $dl_link_target . '>' . $download_url . '</a>';
                            }
                            $download_link .= "<br /><strong>" . $retrieved_product_for_id->name . "</strong> - " . $raw_download . '<br />';
                            eStore_register_link_in_db('', $download_key, $download_url, '', '', '', 0, $payment_data['txn_id']);
                        }
                    } else {
                        if ($multi_parts) {
                            $tx_result_msg .= 'The product has multiple downloadable files.';
                            $count = 1;
                            $download_link .= "<br /><strong>" . $cart_item_data_name . "</strong> - ";
                            foreach ($product_ids as $id) {
                                $id = trim($id);
                                if (!empty($id)) {
                                    $download_key = eStore_check_stamping_flag_and_generate_download_key($retrieved_product, $product_id, $id, $payment_data);
                                    $download_url = eStore_construct_raw_encrypted_dl_url($download_key);
                                    if (WP_ESTORE_USE_ANCHOR_FOR_THANKU_DL_LINKS === '1') {
                                        $raw_download = '<a href="' . $download_url . '" ' . $dl_link_target . '>' . WP_ESTORE_CLICK_HERE_TO_DOWNLOAD . '</a>';
                                    } else {
                                        $raw_download = '<a href="' . $download_url . '" ' . $dl_link_target . '>' . $download_url . '</a>';
                                    }
                                    $download_link .= "<br />" . ESTORE_PART . " " . $count . " : " . $raw_download;
                                    eStore_register_link_in_db('', $download_key, $download_url, '', '', '', 0, $payment_data['txn_id']);
                                    $count++;
                                }
                            }
                        } else {
                            $download_key = eStore_check_stamping_flag_and_generate_download_key($retrieved_product, $product_id, '', $payment_data);
                            $download_url = eStore_construct_raw_encrypted_dl_url($download_key);
                            if (WP_ESTORE_USE_ANCHOR_FOR_THANKU_DL_LINKS === '1') {
                                $raw_download = '<a href="' . $download_url . '" ' . $dl_link_target . '>' . WP_ESTORE_CLICK_HERE_TO_DOWNLOAD . '</a>';
                            } else {
                                $raw_download = '<a href="' . $download_url . '" ' . $dl_link_target . '>' . $download_url . '</a>';
                            }
                            $download_link = "<br /><strong>" . stripslashes($cart_item_data_name) . "</strong> - " . $raw_download;
                            eStore_register_link_in_db('', $download_key, $download_url, '', '', '', 0, $payment_data['txn_id']);
                        }
                    }
                }
            }
        }
        $tx_result_msg .= 'Download Link : ' . $download_link;
        array_push($product_name_array, $cart_item_data_name);
        array_push($product_id_array, $product_id);
        array_push($product_price_array, $cart_item_data_total);
        array_push($product_qty_array, $cart_item_data_quantity);
        //array_push($attachments_array, $retrieved_product->product_download_url);
        array_push($download_link_array, $download_link);
        $counter++;
        $download_link = '';
    }
    // How long the download link remain valid (hours)
    $download_url_life = get_option('eStore_download_url_life');
    $email_body = get_option('eStore_buyer_email_body');
    // Send the product
    $constructed_products_name = "";
    $constructed_products_price = "";
    $constructed_products_id = "";
    $constructed_download_link = "";
    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_download_link .= "<br />";
        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 .= "<br />";
            }
        } else {
            $constructed_download_link .= $download_link_array[$i];
        }
    }
    //Save transaction result for thank you page display
    eStore_payment_debug("Saving transaction data for thank you page display.", true);
    eStore_save_trans_result_for_thank_you_page_display($keyarray, $constructed_download_link, $cart_items);
    //Google Analytics e-commerce tracking (only do it if set in settings menu)
    if (get_option('eStore_enable_analytics_tracking')) {
        $mc_shipping = $keyarray['mc_shipping'];
        $mc_tax = $keyarray['tax'];
        $city = $keyarray['address_city'];
        $state = $keyarray['address_state'];
        $country = $keyarray['address_country'];
        $eStore_analytics_code = array();
        $eStore_analytics_code[] = "'_addTrans'," . "'" . $transaction_id . "','" . get_bloginfo('name') . "','" . $gross_total . "','" . $mc_tax . "','" . $mc_shipping . "','" . $city . "','" . $state . "','" . $country . "'";
        for ($j = 0; $j < sizeof($product_name_array); $j++) {
            $eStore_analytics_code[] = "'_addItem'," . "'" . $transaction_id . "','" . $product_id_array[$j] . "','" . $product_name_array[$j] . "','','" . $product_price_array[$j] . "','" . $product_qty_array[$j] . "'";
        }
        $eStore_analytics_code[] = "'_trackTrans'";
        $_SESSION['eStore_ga_code'] = $eStore_analytics_code;
        add_filter('yoast-ga-push-after-pageview', 'eStore_add_trans_to_ga_tracking');
    }
}