Example #1
0
 public function add_pending_referral($order_id = 0, $current_status, $previous_status, $order)
 {
     if ($this->was_referred()) {
         if ($this->is_affiliate_email(wpsc_get_buyers_email($order_id))) {
             return;
             // Customers cannot refer themselves
         }
         $description = '';
         $items = $order->get_cart_contents();
         foreach ($items as $key => $item) {
             $description .= $item->name;
             if ($key + 1 < count($items)) {
                 $description .= ', ';
             }
         }
         $amount = $order->get('totalprice');
         if (affiliate_wp()->settings->get('exclude_tax')) {
             $amount -= $order->get('wpec_taxes_total');
         }
         if (affiliate_wp()->settings->get('exclude_shipping')) {
             $amount -= $order->get('total_shipping');
         }
         $referral_total = $this->calculate_referral_amount($amount, $order_id);
         $this->insert_pending_referral($referral_total, $order_id, $description);
     }
 }
Example #2
0
/**
 * Send sales log tracking email via AJAX
 *
 * @since 3.8.9
 * @access private
 *
 * @uses $wpdb              WordPress database object for queries
 * @uses get_option()       Gets option from DB given key
 * @uses add_filter()       Calls 'wp_mail_from' which can replace the from email address
 * @uses add_filter()       Calls 'wp_mail_from_name' allows replacement of the from name on WordPress emails
 * @uses wp_mail()          All the emailses in WordPress are sent through this function
 * @uses WP_Error           WordPress Error class
 *
 * @return array|WP_Error   $return     Response args if successful, WP_Error if otherwise
 */
function _wpsc_ajax_purchase_log_send_tracking_email()
{
    global $wpdb;
    $id = absint($_POST['log_id']);
    $sql = $wpdb->prepare("SELECT `track_id` FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE `id`=%d LIMIT 1", $id);
    $trackingid = $wpdb->get_var($sql);
    $message = get_option('wpsc_trackingid_message');
    $message = str_replace('%trackid%', $trackingid, $message);
    $message = str_replace('%shop_name%', get_option('blogname'), $message);
    $email = wpsc_get_buyers_email($id);
    $subject = get_option('wpsc_trackingid_subject');
    $subject = str_replace('%shop_name%', get_option('blogname'), $subject);
    add_filter('wp_mail_from', 'wpsc_replace_reply_address', 0);
    add_filter('wp_mail_from_name', 'wpsc_replace_reply_name', 0);
    $result = wp_mail($email, $subject, $message);
    if (!$result) {
        return new WP_Error('wpsc_cannot_send_tracking_email', __("Couldn't send tracking email. Please try again.", 'wpsc'));
    }
    $return = array('id' => $id, 'tracking_id' => $trackingid, 'subject' => $subject, 'message' => $message, 'email' => $email);
    return $return;
}
 public function get_address()
 {
     return apply_filters('wpsc_purchase_log_customer_notification_address', wpsc_get_buyers_email($this->purchase_log->get('id')), $this);
 }
/**
 * transaction_results function main function for creating the purchase reports, transaction results page, and email receipts
 * @access public
 *
 * @since 3.7
 * @param $sessionid (string) unique session id
 * @param echo_to_screen (boolean) whether to output the results or return them (potentially redundant)
 * @param $transaction_id (int) the transaction id
 */
function transaction_results($sessionid, $display_to_screen = true, $transaction_id = null)
{
    // Do we seriously need this many globals?
    global $wpdb, $wpsc_cart, $echo_to_screen, $purchase_log, $order_url;
    global $message_html, $cart, $errorcode, $wpsc_purchlog_statuses, $wpsc_gateways;
    $wpec_taxes_controller = new wpec_taxes_controller();
    $is_transaction = false;
    $errorcode = 0;
    $purchase_log = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid`= %s LIMIT 1", $sessionid), ARRAY_A);
    $order_status = $purchase_log['processed'];
    $curgateway = $purchase_log['gateway'];
    if (!is_bool($display_to_screen)) {
        $display_to_screen = true;
    }
    $echo_to_screen = $display_to_screen;
    //new variable to check whether function is being called from wpsc_purchlog_resend_email()
    $resend_email = isset($_REQUEST['email_buyer_id']) ? true : false;
    if (is_numeric($sessionid)) {
        if ($echo_to_screen) {
            echo apply_filters('wpsc_pre_transaction_results', '');
        }
        // New code to check whether transaction is processed, true if accepted false if pending or incomplete
        $is_transaction = wpsc_check_purchase_processed($purchase_log['processed']);
        $message_html = $message = stripslashes(get_option('wpsc_email_receipt'));
        if ($is_transaction) {
            $message = __('The Transaction was successful', 'wpsc') . "\r\n" . $message;
            $message_html = __('The Transaction was successful', 'wpsc') . "<br />" . $message_html;
        }
        $country = get_option('country_form_field');
        $billing_country = '';
        $shipping_country = '';
        if (!empty($purchase_log['shipping_country'])) {
            $billing_country = $purchase_log['billing_country'];
            $shipping_country = $purchase_log['shipping_country'];
        } elseif (!empty($country)) {
            $country = $wpdb->get_var($wpdb->prepare("SELECT `value` FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` WHERE `log_id` = %d AND `form_id` = %d LIMIT 1", $purchase_log['id'], get_option('country_form_field')));
            $billing_country = $country;
            $shipping_country = $country;
        }
        $email = wpsc_get_buyers_email($purchase_log['id']);
        $previous_download_ids = array();
        $product_list = $product_list_html = $report_product_list = '';
        $cart = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid` = %d", $purchase_log['id']), ARRAY_A);
        if ($cart != null && $errorcode == 0) {
            $total_shipping = '';
            foreach ($cart as $row) {
                $link = array();
                $wpdb->update(WPSC_TABLE_DOWNLOAD_STATUS, array('active' => '1'), array('cartid' => $row['id'], 'purchid' => $purchase_log['id']));
                do_action('wpsc_transaction_result_cart_item', array("purchase_id" => $purchase_log['id'], "cart_item" => $row, "purchase_log" => $purchase_log));
                if ($is_transaction) {
                    $download_data = $wpdb->get_results($wpdb->prepare("SELECT *\n\t\t\t\t\tFROM `" . WPSC_TABLE_DOWNLOAD_STATUS . "`\n\t\t\t\t\tWHERE `active`='1'\n\t\t\t\t\tAND `purchid` = %d\n\t\t\t\t\tAND `cartid` = %d", $purchase_log['id'], $row['id']), ARRAY_A);
                    if (count($download_data) > 0) {
                        foreach ($download_data as $single_download) {
                            $file_data = get_post($single_download['product_id']);
                            // if the uniqueid is not equal to null, its "valid", regardless of what it is
                            $argsdl = array('post_type' => 'wpsc-product-file', 'post_parent' => $single_download['product_id'], 'numberposts' => -1, 'post_status' => 'all');
                            $download_file_posts = (array) get_posts($argsdl);
                            foreach ((array) $download_file_posts as $single_file_post) {
                                if ($single_file_post->ID == $single_download['fileid']) {
                                    $current_Dl_product_file_post = $single_file_post;
                                    break;
                                }
                            }
                            $file_name = $current_Dl_product_file_post->post_title;
                            if ($single_download['uniqueid'] == null) {
                                $link[] = array("url" => site_url("?downloadid=" . $single_download['id']), "name" => $file_name);
                            } else {
                                $link[] = array("url" => site_url("?downloadid=" . $single_download['uniqueid']), "name" => $file_name);
                            }
                        }
                    } else {
                        $order_status = $purchase_log['processed'];
                    }
                    if (isset($download_data['id'])) {
                        $previous_download_ids[] = $download_data['id'];
                    }
                }
                do_action('wpsc_confirm_checkout', $purchase_log['id']);
                $total = 0;
                $shipping = $row['pnp'];
                $total_shipping += $shipping;
                $total += $row['price'] * $row['quantity'];
                $message_price = wpsc_currency_display($total, array('display_as_html' => false));
                $message_price_html = wpsc_currency_display($total);
                $shipping_price = wpsc_currency_display($shipping, array('display_as_html' => false));
                if (isset($purchase['gateway']) && 'wpsc_merchant_testmode' != $purchase['gateway']) {
                    if ($gateway['internalname'] == $purch_data[0]['gateway']) {
                        $gateway_name = $gateway['name'];
                    }
                } else {
                    $gateway_name = "Manual Payment";
                }
                $variation_list = '';
                if (!empty($link)) {
                    $additional_content = apply_filters('wpsc_transaction_result_content', array("purchase_id" => $purchase_log['id'], "cart_item" => $row, "purchase_log" => $purchase_log));
                    if (!is_string($additional_content)) {
                        $additional_content = '';
                    }
                    $product_list .= " - " . $row['name'] . "  " . $message_price . " " . __('Click to download', 'wpsc') . ":";
                    $product_list_html .= " - " . $row['name'] . "  " . $message_price_html . "&nbsp;&nbsp;" . __('Click to download', 'wpsc') . ":\n\r";
                    foreach ($link as $single_link) {
                        $product_list .= "\n\r " . $single_link["name"] . ": " . $single_link["url"] . "\n\r";
                        $product_list_html .= "<a href='" . $single_link["url"] . "'>" . $single_link["name"] . "</a>\n";
                    }
                    $product_list .= $additional_content;
                    $product_list_html .= $additional_content;
                } else {
                    $product_list .= " - " . $row['quantity'] . " " . $row['name'] . "  " . $message_price . "\n\r";
                    if ($shipping > 0) {
                        $product_list .= sprintf(__(' - Shipping: %s
', 'wpsc'), $shipping_price);
                    }
                    $product_list_html .= "\n\r - " . $row['quantity'] . " " . $row['name'] . "  " . $message_price_html . "\n\r";
                    if ($shipping > 0) {
                        $product_list_html .= sprintf(__(' &nbsp; Shipping: %s
', 'wpsc'), $shipping_price);
                    }
                }
                //add tax if included
                if ($wpec_taxes_controller->wpec_taxes_isenabled() && $wpec_taxes_controller->wpec_taxes_isincluded()) {
                    $taxes_text = ' - - ' . __('Tax Included', 'wpsc') . ': ' . wpsc_currency_display($row['tax_charged'], array('display_as_html' => false)) . "\n\r";
                    $taxes_text_html = ' - - ' . __('Tax Included', 'wpsc') . ': ' . wpsc_currency_display($row['tax_charged']);
                    $product_list .= $taxes_text;
                    $product_list_html .= $taxes_text_html;
                }
                // if
                $report = get_option('wpsc_email_admin');
                $report_product_list .= " - " . $row['quantity'] . " " . $row['name'] . "  " . $message_price . "\n\r";
            }
            // closes foreach cart as row
            // Decrement the stock here
            if ($is_transaction) {
                wpsc_decrement_claimed_stock($purchase_log['id']);
            }
            if (!empty($purchase_log['discount_data'])) {
                $coupon_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_COUPON_CODES . "` WHERE coupon_code = %s LIMIT 1", $purchase_log['discount_data']), ARRAY_A);
                if ($coupon_data['use-once'] == 1) {
                    $wpdb->update(WPSC_TABLE_COUPON_CODES, array('active' => '0', 'is-used' => '1'), array('id' => $coupon_data['id']));
                }
            }
            $total_shipping = wpsc_get_total_shipping($purchase_log['id']);
            $total = $purchase_log['totalprice'];
            $total_price_email = '';
            $total_price_html = '';
            $total_tax_html = '';
            $total_tax = '';
            $total_shipping_html = '';
            $total_shipping_email = '';
            if (wpsc_uses_shipping() || !empty($purchase_log['base_shipping'])) {
                $total_shipping_email .= sprintf(__('Total Shipping: %s
	', 'wpsc'), wpsc_currency_display($total_shipping, array('display_as_html' => false)));
            }
            $total_price_email .= sprintf(__('Total: %s
', 'wpsc'), wpsc_currency_display($total, array('display_as_html' => false)));
            if ($purchase_log['discount_value'] > 0) {
                $discount_email = __('Discount', 'wpsc') . "\n\r: ";
                $discount_email .= $purchase_log['discount_data'] . ' : ' . wpsc_currency_display($purchase_log['discount_value'], array('display_as_html' => false)) . "\n\r";
                $report .= $discount_email . "\n\r";
                $total_shipping_email .= $discount_email;
                $total_shipping_html .= __('Discount', 'wpsc') . ": " . wpsc_currency_display($purchase_log['discount_value']) . "\n\r";
            }
            //only show total tax if tax is not included
            if ($wpec_taxes_controller->wpec_taxes_isenabled() && !$wpec_taxes_controller->wpec_taxes_isincluded()) {
                $total_tax_html .= __('Total Tax', 'wpsc') . ': ' . wpsc_currency_display($purchase_log['wpec_taxes_total']) . "\n\r";
                $total_tax .= __('Total Tax', 'wpsc') . ': ' . wpsc_currency_display($purchase_log['wpec_taxes_total'], array('display_as_html' => false)) . "\n\r";
            }
            if (wpsc_uses_shipping() || !empty($purchase_log['base_shipping'])) {
                $total_shipping_html .= '<hr>' . sprintf(__('Total Shipping: %s
	', 'wpsc'), wpsc_currency_display($total_shipping));
            }
            $total_price_html .= sprintf(__('Total: %s
', 'wpsc'), wpsc_currency_display($total));
            $report_id = sprintf(__("Purchase # %s\n", 'wpsc'), $purchase_log['id']);
            if (isset($_GET['ti'])) {
                $message .= "\n\r" . __('Your Transaction ID', 'wpsc') . ": " . $_GET['ti'];
                $message_html .= "\n\r" . __('Your Transaction ID', 'wpsc') . ": " . $_GET['ti'];
                $report .= "\n\r" . __('Transaction ID', 'wpsc') . ": " . $_GET['ti'];
            }
            $message = apply_filters('wpsc_transaction_result_message', $message);
            $message = str_replace('%purchase_id%', $report_id, $message);
            $message = str_replace('%product_list%', $product_list, $message);
            $message = str_replace('%total_tax%', $total_tax, $message);
            $message = str_replace('%total_shipping%', $total_shipping_email, $message);
            $message = str_replace('%total_price%', $total_price_email, $message);
            $message = str_replace('%shop_name%', get_option('blogname'), $message);
            $message = str_replace('%find_us%', $purchase_log['find_us'], $message);
            $report = apply_filters('wpsc_transaction_result_report', $report);
            $report = str_replace('%purchase_id%', $report_id, $report);
            $report = str_replace('%product_list%', $report_product_list, $report);
            $report = str_replace('%total_tax%', $total_tax, $report);
            $report = str_replace('%total_shipping%', $total_shipping_email, $report);
            $report = str_replace('%total_price%', $total_price_email, $report);
            $report = str_replace('%shop_name%', get_option('blogname'), $report);
            $report = str_replace('%find_us%', $purchase_log['find_us'], $report);
            $message_html = apply_filters('wpsc_transaction_result_message_html', $message_html);
            $message_html = str_replace('%purchase_id%', $report_id, $message_html);
            $message_html = str_replace('%product_list%', $product_list_html, $message_html);
            $message_html = str_replace('%total_tax%', $total_tax_html, $message_html);
            $message_html = str_replace('%total_shipping%', $total_shipping_html, $message_html);
            $message_html = str_replace('%total_price%', $total_price_html, $message_html);
            $message_html = str_replace('%shop_name%', get_option('blogname'), $message_html);
            $message_html = str_replace('%find_us%', $purchase_log['find_us'], $message_html);
            if (!empty($email)) {
                add_filter('wp_mail_from', 'wpsc_replace_reply_address', 0);
                add_filter('wp_mail_from_name', 'wpsc_replace_reply_name', 0);
                $message = apply_filters('wpsc_email_message', $message, $report_id, $product_list, $total_tax, $total_shipping_email, $total_price_email);
                if (!$is_transaction) {
                    $payment_instructions = strip_tags(stripslashes(get_option('payment_instructions')));
                    if (!empty($payment_instructions)) {
                        $payment_instructions .= "\n\r";
                    }
                    $message = __('Thank you, your purchase is pending, you will be sent an email once the order clears.', 'wpsc') . "\n\r" . $payment_instructions . $message;
                    $message_html = __('Thank you, your purchase is pending, you will be sent an email once the order clears.', 'wpsc') . "\n\r" . $payment_instructions . $message_html;
                    // prevent email duplicates
                    if (!get_transient("{$sessionid}_pending_email_sent") || $resend_email) {
                        wp_mail($email, __('Order Pending: Payment Required', 'wpsc'), $message);
                        set_transient("{$sessionid}_pending_email_sent", true, 60 * 60 * 12);
                    }
                } elseif (!get_transient("{$sessionid}_receipt_email_sent") || $resend_email) {
                    wp_mail($email, __('Purchase Receipt', 'wpsc'), $message);
                    set_transient("{$sessionid}_receipt_email_sent", true, 60 * 60 * 12);
                }
            }
            remove_filter('wp_mail_from_name', 'wpsc_replace_reply_name');
            remove_filter('wp_mail_from', 'wpsc_replace_reply_address');
            $report_user = __('Customer Details', 'wpsc') . "\n\r";
            $form_sql = $wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` WHERE `log_id` = %d", $purchase_log['id']);
            $form_data = $wpdb->get_results($form_sql, ARRAY_A);
            if ($form_data != null) {
                foreach ($form_data as $form_field) {
                    $form_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `id` = %d LIMIT 1", $form_field['form_id']), ARRAY_A);
                    switch ($form_data['type']) {
                        case "country":
                            $country_code = $form_field['value'];
                            $report_user .= $form_data['name'] . ": " . wpsc_get_country($country_code) . "\n";
                            //check if country has a state then display if it does.
                            $country_data = wpsc_country_has_state($country_code);
                            if ($country_data['has_regions'] == 1) {
                                $report_user .= __('Billing State', 'wpsc') . ": " . wpsc_get_region($purchase_log['billing_region']) . "\n";
                            }
                            break;
                        case "delivery_country":
                            $report_user .= $form_data['name'] . ": " . wpsc_get_country($form_field['value']) . "\n";
                            break;
                        default:
                            if ($form_data['name'] == 'State' && is_numeric($form_field['value'])) {
                                $report_user .= __('Delivery State', 'wpsc') . ": " . wpsc_get_state_by_id($form_field['value'], 'name') . "\n";
                            } else {
                                $report_user .= wp_kses($form_data['name'], array()) . ": " . $form_field['value'] . "\n";
                            }
                            break;
                    }
                }
            }
            $report_user .= "\n\r";
            $report = $report_id . $report_user . $report;
            //echo '======REPORT======<br />'.$report.'<br />';
            //echo '======EMAIL======<br />'.$message.'<br />';
            if (get_option('purch_log_email') != null && $purchase_log['email_sent'] != 1) {
                wp_mail(get_option('purch_log_email'), __('Purchase Report', 'wpsc'), $report);
                $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('email_sent' => '1'), array('sessionid' => $sessionid));
            }
            /// Adjust stock and empty the cart
            $wpsc_cart->submit_stock_claims($purchase_log['id']);
            $wpsc_cart->empty_cart();
        }
    }
}