// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
/**
 * View Order Details Page
 *
 * @since 1.6
 * @return void
*/
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    wp_die(__('Payment ID not supplied. Please try again', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'));
}
// Setup the variables
$payment_id = absint($_GET['id']);
$payment = new EDD_Payment($payment_id);
// Sanity check... fail if purchase ID is invalid
$payment_exists = $payment->ID;
if (empty($payment_exists)) {
    wp_die(__('The specified ID does not belong to a payment. Please try again', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'));
}
$number = $payment->number;
$payment_meta = $payment->get_meta();
$transaction_id = esc_attr($payment->transaction_id);
$cart_items = $payment->cart_details;
$user_id = $payment->user_id;
$payment_date = strtotime($payment->date);
$unlimited = $payment->has_unlimited_downloads;
$user_info = edd_get_payment_meta_user_info($payment_id);
$address = $payment->address;
$gateway = $payment->gateway;
Пример #2
0
/**
 * Process the payment details edit
 *
 * @access      private
 * @since       1.9
 * @return      void
*/
function edd_update_payment_details($data)
{
    if (!current_user_can('edit_shop_payments', $data['edd_payment_id'])) {
        wp_die(__('You do not have permission to edit this payment record', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403));
    }
    check_admin_referer('edd_update_payment_details_nonce');
    // Retrieve the payment ID
    $payment_id = absint($data['edd_payment_id']);
    $payment = new EDD_Payment($payment_id);
    // Retrieve existing payment meta
    $meta = $payment->get_meta();
    $user_info = $payment->user_info;
    $status = $data['edd-payment-status'];
    $unlimited = isset($data['edd-unlimited-downloads']) ? '1' : '';
    $date = sanitize_text_field($data['edd-payment-date']);
    $hour = sanitize_text_field($data['edd-payment-time-hour']);
    // Restrict to our high and low
    if ($hour > 23) {
        $hour = 23;
    } elseif ($hour < 0) {
        $hour = 00;
    }
    $minute = sanitize_text_field($data['edd-payment-time-min']);
    // Restrict to our high and low
    if ($minute > 59) {
        $minute = 59;
    } elseif ($minute < 0) {
        $minute = 00;
    }
    $address = array_map('trim', $data['edd-payment-address'][0]);
    $curr_total = edd_sanitize_amount($payment->total);
    $new_total = edd_sanitize_amount($_POST['edd-payment-total']);
    $tax = isset($_POST['edd-payment-tax']) ? edd_sanitize_amount($_POST['edd-payment-tax']) : 0;
    $date = date('Y-m-d', strtotime($date)) . ' ' . $hour . ':' . $minute . ':00';
    $curr_customer_id = sanitize_text_field($data['edd-current-customer']);
    $new_customer_id = sanitize_text_field($data['customer-id']);
    // Setup purchased Downloads and price options
    $updated_downloads = isset($_POST['edd-payment-details-downloads']) ? $_POST['edd-payment-details-downloads'] : false;
    if ($updated_downloads && !empty($_POST['edd-payment-downloads-changed'])) {
        foreach ($updated_downloads as $download) {
            // If this item doesn't have a log yet, add one for each quantity count
            $has_log = absint($download['has_log']);
            $has_log = empty($has_log) ? false : true;
            if ($has_log) {
                continue;
            }
            if (empty($download['item_price'])) {
                $download['item_price'] = 0.0;
            }
            $item_price = $download['item_price'];
            $download_id = absint($download['id']);
            $quantity = absint($download['quantity']) > 0 ? absint($download['quantity']) : 1;
            $price_id = false;
            if (edd_has_variable_prices($download_id) && isset($download['price_id'])) {
                $price_id = absint($download['price_id']);
            }
            // Set some defaults
            $args = array('quantity' => $quantity, 'item_price' => $item_price, 'price_id' => $price_id);
            $payment->add_download($download_id, $args);
        }
        $deleted_downloads = json_decode(stripcslashes($data['edd-payment-removed']), true);
        foreach ($deleted_downloads as $deleted_download) {
            $deleted_download = $deleted_download[0];
            if (empty($deleted_download['id'])) {
                continue;
            }
            $price_id = empty($deleted_download['price_id']) ? 0 : (int) $deleted_download['price_id'];
            $args = array('quantity' => (int) $deleted_download['quantity'], 'price_id' => (int) $price_id, 'item_price' => (double) $deleted_download['amount']);
            $payment->remove_download($deleted_download['id'], $args);
            do_action('edd_remove_download_from_payment', $payment_id, $deleted_download['id']);
        }
    }
    do_action('edd_update_edited_purchase', $payment_id);
    $payment->date = $date;
    $updated = $payment->save();
    if (0 === $updated) {
        wp_die(__('Error Updating Payment', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 400));
    }
    $customer_changed = false;
    if (isset($data['edd-new-customer']) && $data['edd-new-customer'] == '1') {
        $email = isset($data['edd-new-customer-email']) ? sanitize_text_field($data['edd-new-customer-email']) : '';
        $names = isset($data['edd-new-customer-name']) ? sanitize_text_field($data['edd-new-customer-name']) : '';
        if (empty($email) || empty($names)) {
            wp_die(__('New Customers require a name and email address', 'easy-digital-downloads'));
        }
        $customer = new EDD_Customer($email);
        if (empty($customer->id)) {
            $customer_data = array('name' => $names, 'email' => $email);
            $user_id = email_exists($email);
            if (false !== $user_id) {
                $customer_data['user_id'] = $user_id;
            }
            if (!$customer->create($customer_data)) {
                // Failed to crete the new customer, assume the previous customer
                $customer_changed = false;
                $customer = new EDD_Customer($curr_customer_id);
                edd_set_error('edd-payment-new-customer-fail', __('Error creating new customer', 'easy-digital-downloads'));
            }
        }
        $new_customer_id = $customer->id;
        $previous_customer = new EDD_Customer($curr_customer_id);
        $customer_changed = true;
    } elseif ($curr_customer_id !== $new_customer_id) {
        $customer = new EDD_Customer($new_customer_id);
        $email = $customer->email;
        $names = $customer->name;
        $previous_customer = new EDD_Customer($curr_customer_id);
        $customer_changed = true;
    } else {
        $customer = new EDD_Customer($curr_customer_id);
        $email = $customer->email;
        $names = $customer->name;
    }
    // Setup first and last name from input values
    $names = explode(' ', $names);
    $first_name = !empty($names[0]) ? $names[0] : '';
    $last_name = '';
    if (!empty($names[1])) {
        unset($names[0]);
        $last_name = implode(' ', $names);
    }
    if ($customer_changed) {
        // Remove the stats and payment from the previous customer and attach it to the new customer
        $previous_customer->remove_payment($payment_id, false);
        $customer->attach_payment($payment_id, false);
        // If purchase was completed and not ever refunded, adjust stats of customers
        if ('revoked' == $status || 'publish' == $status) {
            $previous_customer->decrease_purchase_count();
            $previous_customer->decrease_value($new_total);
            $customer->increase_purchase_count();
            $customer->increase_value($new_total);
        }
        $payment->customer_id = $customer->id;
    }
    // Set new meta values
    $payment->user_id = $customer->user_id;
    $payment->email = $customer->email;
    $payment->first_name = $first_name;
    $payment->last_name = $last_name;
    $payment->address = $address;
    $payment->total = $new_total;
    $payment->tax = $tax;
    $payment->has_unlimited_downloads = $unlimited;
    // Check for payment notes
    if (!empty($data['edd-payment-note'])) {
        $note = wp_kses($data['edd-payment-note'], array());
        edd_insert_payment_note($payment->ID, $note);
    }
    // Set new status
    $payment->status = $status;
    // Adjust total store earnings if the payment total has been changed
    if ($new_total !== $curr_total && ('publish' == $status || 'revoked' == $status)) {
        if ($new_total > $curr_total) {
            // Increase if our new total is higher
            $difference = $new_total - $curr_total;
            edd_increase_total_earnings($difference);
        } elseif ($curr_total > $new_total) {
            // Decrease if our new total is lower
            $difference = $curr_total - $new_total;
            edd_decrease_total_earnings($difference);
        }
    }
    $payment->save();
    do_action('edd_updated_edited_purchase', $payment_id);
    wp_safe_redirect(admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details&edd-message=payment-updated&id=' . $payment_id));
    exit;
}
 /**
  * Retrieves Recent Sales
  *
  * @access public
  * @since  1.5
  * @return array
  */
 public function get_recent_sales()
 {
     global $wp_query;
     $sales = array();
     if (!user_can($this->user_id, 'view_shop_reports') && !$this->override) {
         return $sales;
     }
     if (isset($wp_query->query_vars['id'])) {
         $query = array();
         $query[] = new EDD_Payment($wp_query->query_vars['id']);
     } elseif (isset($wp_query->query_vars['purchasekey'])) {
         $query = array();
         $query[] = edd_get_payment_by('key', $wp_query->query_vars['purchasekey']);
     } elseif (isset($wp_query->query_vars['email'])) {
         $query = edd_get_payments(array('fields' => 'ids', 'meta_key' => '_edd_payment_user_email', 'meta_value' => $wp_query->query_vars['email'], 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish'));
     } else {
         $query = edd_get_payments(array('fields' => 'ids', 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish'));
     }
     if ($query) {
         $i = 0;
         foreach ($query as $payment) {
             if (is_numeric($payment)) {
                 $payment = new EDD_Payment($payment);
             }
             $payment_meta = $payment->get_meta();
             $user_info = $payment->user_info;
             $sales['sales'][$i]['ID'] = $payment->number;
             $sales['sales'][$i]['transaction_id'] = $payment->transaction_id;
             $sales['sales'][$i]['key'] = $payment->key;
             $sales['sales'][$i]['discount'] = !empty($payment->discounts) ? explode(',', $payment->discounts) : array();
             $sales['sales'][$i]['subtotal'] = $payment->subtotal;
             $sales['sales'][$i]['tax'] = $payment->tax;
             $sales['sales'][$i]['fees'] = $payment->fees;
             $sales['sales'][$i]['total'] = $payment->total;
             $sales['sales'][$i]['gateway'] = $payment->gateway;
             $sales['sales'][$i]['email'] = $payment->email;
             $sales['sales'][$i]['date'] = $payment->date;
             $sales['sales'][$i]['products'] = array();
             $c = 0;
             foreach ($payment->cart_details as $key => $item) {
                 $item_id = isset($item['id']) ? $item['id'] : $item;
                 $price = isset($item['price']) ? $item['price'] : false;
                 $price_id = isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null;
                 $quantity = isset($item['quantity']) && $item['quantity'] > 0 ? $item['quantity'] : 1;
                 if (!$price) {
                     // This function is only used on payments with near 1.0 cart data structure
                     $price = edd_get_download_final_price($item_id, $user_info, null);
                 }
                 $price_name = '';
                 if (isset($item['item_number']) && isset($item['item_number']['options'])) {
                     $price_options = $item['item_number']['options'];
                     if (isset($price_options['price_id'])) {
                         $price_name = edd_get_price_option_name($item_id, $price_options['price_id'], $payment->ID);
                     }
                 }
                 $sales['sales'][$i]['products'][$c]['id'] = $item_id;
                 $sales['sales'][$i]['products'][$c]['quantity'] = $quantity;
                 $sales['sales'][$i]['products'][$c]['name'] = get_the_title($item_id);
                 $sales['sales'][$i]['products'][$c]['price'] = $price;
                 $sales['sales'][$i]['products'][$c]['price_name'] = $price_name;
                 $c++;
             }
             $i++;
         }
     }
     return $sales;
 }
Пример #4
0
/**
 * Updates all old payments, prior to 1.2, with new
 * meta for the total purchase amount
 *
 * This is so that payments can be queried by their totals
 *
 * @since 1.2
 * @param array $data Arguments passed
 * @return void
*/
function edd_update_old_payments_with_totals($data)
{
    if (!wp_verify_nonce($data['_wpnonce'], 'edd_upgrade_payments_nonce')) {
        return;
    }
    if (get_option('edd_payment_totals_upgraded')) {
        return;
    }
    $payments = edd_get_payments(array('offset' => 0, 'number' => -1, 'mode' => 'all'));
    if ($payments) {
        foreach ($payments as $payment) {
            $payment = new EDD_Payment($payment->ID);
            $meta = $payment->get_meta();
            $payment->total = $meta['amount'];
            $payment->save();
        }
    }
    add_option('edd_payment_totals_upgraded', 1);
}
/**
 * Email template tag: file_urls
 * A plain-text list of download URLs for each download purchased
 *
 * @param int $payment_id
 *
 * @return string $file_urls
 */
function edd_email_tag_file_urls($payment_id)
{
    $payment = new EDD_Payment($payment_id);
    $payment_data = $payment->get_meta();
    $file_urls = '';
    $cart_items = $payment->cart_details;
    $email = $payment->email;
    foreach ($cart_items as $item) {
        $price_id = edd_get_cart_item_price_id($item);
        $files = edd_get_download_files($item['id'], $price_id);
        if ($files) {
            foreach ($files as $filekey => $file) {
                $file_url = edd_get_download_file_url($payment_data['key'], $email, $filekey, $item['id'], $price_id);
                $file_urls .= esc_html($file_url) . '<br/>';
            }
        } elseif (edd_is_bundled_product($item['id'])) {
            $bundled_products = apply_filters('edd_email_tag_bundled_products', edd_get_bundled_products($item['id']), $item, $payment_id, 'file_urls');
            foreach ($bundled_products as $bundle_item) {
                $files = edd_get_download_files($bundle_item);
                foreach ($files as $filekey => $file) {
                    $file_url = edd_get_download_file_url($payment_data['key'], $email, $filekey, $bundle_item, $price_id);
                    $file_urls .= esc_html($file_url) . '<br/>';
                }
            }
        }
    }
    return $file_urls;
}
Пример #6
0
/**
 * Retrieves arbitrary fees for the payment
 *
 * @since 1.5
 * @param int $payment_id Payment ID
 * @param string $type Fee type
 * @return mixed array if payment fees found, false otherwise
 */
function edd_get_payment_fees($payment_id = 0, $type = 'all')
{
    $payment = new EDD_Payment($payment_id);
    return $payment->get_fees($type);
}
/**
 * Possibly refunds a payment made with PayPal Standard or PayPal Express.
 *
 * @access public
 * @since  2.6.0
 *
 * @param int $payment_id The current payment ID.
 * @return void
 */
function edd_maybe_refund_paypal_purchase(EDD_Payment $payment)
{
    if (!current_user_can('edit_shop_payments', $payment->ID)) {
        return;
    }
    if (empty($_POST['edd-paypal-refund'])) {
        return;
    }
    $processed = $payment->get_meta('_edd_paypal_refunded', true);
    // If the status is not set to "refunded", return early.
    if ('publish' !== $payment->old_status && 'revoked' !== $payment->old_status) {
        return;
    }
    // If not PayPal/PayPal Express, return early.
    if ('paypal' !== $payment->gateway) {
        return;
    }
    // If the payment has already been refunded in the past, return early.
    if ($processed) {
        return;
    }
    // Process the refund in PayPal.
    edd_refund_paypal_purchase($payment);
}
 public static function create_payment($data)
 {
     if (wp_verify_nonce($data['edd_create_payment_nonce'], 'edd_create_payment_nonce')) {
         global $edd_options;
         $data['downloads'] = array_values($data['downloads']);
         if ($data['downloads'][0]['id'] == 0) {
             wp_die(sprintf(__('Please select at least one %s to add to the payment.', 'edd-manual-purchases'), edd_get_label_singular()));
         }
         $by_user_id = false;
         if (!empty($data['email'])) {
             $user = strip_tags(trim($data['email']));
             $by_user_id = false;
         } elseif (empty($data['email']) && !empty($data['customer'])) {
             $user = strip_tags(trim($data['customer']));
         } else {
             $user = null;
         }
         if (null == $user) {
             wp_die(__('Please select a customer or create a new one.', 'edd-manual-purchases'));
         }
         $payment = new EDD_Payment();
         $customer = new EDD_Customer($user, $by_user_id);
         $user_id = $by_user_id == true ? $user : 0;
         $email = $by_user_id == false ? $user : '';
         $first = isset($data['first']) ? sanitize_text_field($data['first']) : '';
         $last = isset($data['last']) ? sanitize_text_field($data['last']) : '';
         if (!$customer->id > 0) {
             $user = $by_user_id == false ? get_user_by('email', $user) : get_user_by('id', $user);
             if ($user) {
                 $user_id = $user->ID;
                 $email = $user->user_email;
             }
             $customer->create(array('email' => $email, 'name' => $first . ' ' . $last, 'user_id' => $user_id));
         } else {
             $email = $customer->email;
         }
         $total = 0.0;
         $payment->customer_id = $customer->id;
         $payment->user_id = $user_id;
         $payment->first_name = $first;
         $payment->last_name = $last;
         $payment->email = $email;
         // Make sure the user info data is set
         $payment->user_info = array('first_name' => $first, 'last_name' => $last, 'id' => $user_id, 'email' => $email);
         $cart_details = array();
         $total = 0;
         foreach ($data['downloads'] as $key => $download) {
             // calculate total purchase cost
             if (isset($download['price_id']) && empty($download['amount'])) {
                 $prices = get_post_meta($download['id'], 'edd_variable_prices', true);
                 $price_key = $download['options']['price_id'];
                 $item_price = $prices[$download['price_id']]['amount'];
             } elseif (empty($download['amount'])) {
                 $item_price = edd_get_download_price($download['id']);
             }
             $item_tax = $args = array('quantity' => !empty($download['quantity']) ? absint($download['quantity']) : 1, 'price_id' => isset($download['price_id']) ? $download['price_id'] : null, 'item_price' => !empty($download['amount']) ? edd_sanitize_amount($download['amount']) : $item_price);
             $args['tax'] = !empty($download['tax']) ? edd_sanitize_amount($download['tax'] * $args['quantity']) : 0;
             $payment->add_download($download['id'], $args);
             $total += $args['item_price'] * $args['quantity'];
         }
         if (!empty($data['amount'])) {
             $total = edd_sanitize_amount(strip_tags(trim($data['amount'])));
             $payment->total = $total;
         }
         // if we are using Wallet, ensure the customer can afford this purchase
         if (!empty($data['wallet']) && class_exists('EDD_Wallet') && $user_id > 0) {
             $wallet_value = edd_wallet()->wallet->balance($user_id);
             if ($wallet_value < $total) {
                 wp_die(__('The customer does not have sufficient funds in their wallet to pay for this purchase.', 'edd-manual-purchases'));
             }
         }
         $date = !empty($data['date']) ? date('Y-m-d H:i:s', strtotime(strip_tags(trim($data['date'])))) : false;
         if (!$date) {
             $date = date('Y-m-d H:i:s', current_time('timestamp'));
         }
         if (strtotime($date, time()) > time()) {
             $date = date('Y-m-d H:i:s', current_time('timestamp'));
         }
         $payment->date = $date;
         $payment->status = 'pending';
         $payment->currency = edd_get_currency();
         $payment->gateway = sanitize_text_field($_POST['gateway']);
         $payment->mode = edd_is_test_mode() ? 'test' : 'live';
         if (!empty($_POST['transaction_id'])) {
             $payment->transaction_id = sanitize_text_field($_POST['transaction_id']);
         }
         $payment->save();
         if (!isset($data['receipt'])) {
             remove_action('edd_complete_purchase', 'edd_trigger_purchase_receipt', 999);
         }
         if (isset($_POST['status']) && 'pending' !== $_POST['status']) {
             $payment->status = $_POST['status'];
             $payment->save();
         }
         if (!empty($data['wallet']) && class_exists('EDD_Wallet') && $user_id > 0) {
             // Update the user wallet
             edd_wallet()->wallet->withdraw($user_id, $total, 'withdrawal', $payment->ID);
         }
         if (!empty($data['shipped'])) {
             update_post_meta($payment->ID, '_edd_payment_shipping_status', '2');
         }
         wp_redirect(admin_url('edit.php?post_type=download&page=edd-payment-history&edd-message=payment_created'));
         exit;
     }
 }
 /**
  * Set up and store a payment record from a CSV row
  *
  * @since 2.6
  * @return void
  */
 public function create_payment($row = array())
 {
     $payment = new EDD_Payment();
     $payment->status = 'pending';
     if (!empty($this->field_mapping['number']) && !empty($row[$this->field_mapping['number']])) {
         $payment->number = sanitize_text_field($row[$this->field_mapping['number']]);
     }
     if (!empty($this->field_mapping['mode']) && !empty($row[$this->field_mapping['mode']])) {
         $mode = strtolower(sanitize_text_field($row[$this->field_mapping['mode']]));
         $mode = 'test' != $mode && 'live' != $mode ? false : $mode;
         if (!$mode) {
             $mode = edd_is_test_mode() ? 'test' : 'live';
         }
         $payment->mode = $mode;
     }
     if (!empty($this->field_mapping['date']) && !empty($row[$this->field_mapping['date']])) {
         $date = sanitize_text_field($row[$this->field_mapping['date']]);
         if (!strtotime($date)) {
             $date = date('Y-m-d H:i:s', current_time('timestamp'));
         } else {
             $date = date('Y-m-d H:i:s', strtotime($date));
         }
         $payment->date = $date;
     }
     $payment->customer_id = $this->set_customer($row);
     if (!empty($this->field_mapping['email']) && !empty($row[$this->field_mapping['email']])) {
         $payment->email = sanitize_text_field($row[$this->field_mapping['email']]);
     }
     if (!empty($this->field_mapping['first_name']) && !empty($row[$this->field_mapping['first_name']])) {
         $payment->first_name = sanitize_text_field($row[$this->field_mapping['first_name']]);
     }
     if (!empty($this->field_mapping['last_name']) && !empty($row[$this->field_mapping['last_name']])) {
         $payment->last_name = sanitize_text_field($row[$this->field_mapping['last_name']]);
     }
     if (!empty($this->field_mapping['user_id']) && !empty($row[$this->field_mapping['user_id']])) {
         $user_id = sanitize_text_field($row[$this->field_mapping['user_id']]);
         if (is_numeric($user_id)) {
             $user_id = absint($row[$this->field_mapping['user_id']]);
             $user = get_userdata($user_id);
         } elseif (is_email($user_id)) {
             $user = get_user_by('email', $user_id);
         } else {
             $user = get_user_by('user_login', $user_id);
         }
         if ($user) {
             $payment->user_id = $user->ID;
         }
     }
     if (!empty($this->field_mapping['discounts']) && !empty($row[$this->field_mapping['discounts']])) {
         $payment->discounts = sanitize_text_field($row[$this->field_mapping['discounts']]);
     }
     if (!empty($this->field_mapping['transaction_id']) && !empty($row[$this->field_mapping['transaction_id']])) {
         $payment->transaction_id = sanitize_text_field($row[$this->field_mapping['transaction_id']]);
     }
     if (!empty($this->field_mapping['ip']) && !empty($row[$this->field_mapping['ip']])) {
         $payment->ip = sanitize_text_field($row[$this->field_mapping['ip']]);
     }
     if (!empty($this->field_mapping['gateway']) && !empty($row[$this->field_mapping['gateway']])) {
         $gateways = edd_get_payment_gateways();
         $gateway = strtolower(sanitize_text_field($row[$this->field_mapping['gateway']]));
         if (!array_key_exists($gateway, $gateways)) {
             foreach ($gateways as $key => $enabled_gateway) {
                 if ($enabled_gateway['checkout_label'] == $gateway) {
                     $gateway = $key;
                     break;
                 }
             }
         }
         $payment->gateway = $gateway;
     }
     if (!empty($this->field_mapping['currency']) && !empty($row[$this->field_mapping['currency']])) {
         $payment->currency = strtoupper(sanitize_text_field($row[$this->field_mapping['currency']]));
     }
     if (!empty($this->field_mapping['key']) && !empty($row[$this->field_mapping['key']])) {
         $payment->key = sanitize_text_field($row[$this->field_mapping['key']]);
     }
     if (!empty($this->field_mapping['parent_payment_id']) && !empty($row[$this->field_mapping['parent_payment_id']])) {
         $payment->parent_payment_id = absint($row[$this->field_mapping['parent_payment_id']]);
     }
     if (!empty($this->field_mapping['downloads']) && !empty($row[$this->field_mapping['downloads']])) {
         if (__('Products (Raw)', 'easy-digital-downloads') == $this->field_mapping['downloads']) {
             // This is an EDD export so we can extract prices
             $downloads = $this->get_downloads_from_edd($row[$this->field_mapping['downloads']]);
         } else {
             $downloads = $this->str_to_array($row[$this->field_mapping['downloads']]);
         }
         if (is_array($downloads)) {
             $download_count = count($downloads);
             foreach ($downloads as $download) {
                 if (is_array($download)) {
                     $download_name = $download['download'];
                     $price = $download['price'];
                     $tax = $download['tax'];
                 } else {
                     $download_name = $download;
                 }
                 $download_id = $this->maybe_create_download($download_name);
                 if (!$download_id) {
                     continue;
                 }
                 $item_price = !isset($price) ? edd_get_download_price($download_id) : $price;
                 $item_tax = !isset($tax) ? $download_count > 1 ? 0.0 : $payment->tax : $tax;
                 $payment->add_download($download_id, array('item_price' => $item_price, 'tax' => $item_tax));
             }
         }
     }
     if (!empty($this->field_mapping['total']) && !empty($row[$this->field_mapping['total']])) {
         $payment->total = edd_sanitize_amount($row[$this->field_mapping['total']]);
     }
     if (!empty($this->field_mapping['tax']) && !empty($row[$this->field_mapping['tax']])) {
         $payment->tax = edd_sanitize_amount($row[$this->field_mapping['tax']]);
     }
     if (!empty($this->field_mapping['subtotal']) && !empty($row[$this->field_mapping['subtotal']])) {
         $payment->subtotal = edd_sanitize_amount($row[$this->field_mapping['subtotal']]);
     } else {
         $payment->subtotal = $payment->total - $payment->tax;
     }
     $address = array('line1' => '', 'line2' => '', 'city' => '', 'state' => '', 'zip' => '', 'country' => '');
     foreach ($address as $key => $address_field) {
         if (!empty($this->field_mapping[$key]) && !empty($row[$this->field_mapping[$key]])) {
             $address[$key] = sanitize_text_field($row[$this->field_mapping[$key]]);
         }
     }
     $payment->address = $address;
     $payment->save();
     // The status has to be set after payment is created to ensure status update properly
     if (!empty($this->field_mapping['status']) && !empty($row[$this->field_mapping['status']])) {
         $payment->status = strtolower(sanitize_text_field($row[$this->field_mapping['status']]));
     } else {
         $payment->status = 'complete';
     }
     // Save a second time to update stats
     $payment->save();
 }
 /**
  * Get the Export Data
  *
  * @access public
  * @since 2.4
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @return array $data The data for the CSV file
  */
 public function get_data()
 {
     global $wpdb;
     $data = array();
     $args = array('number' => 30, 'page' => $this->step, 'status' => $this->status, 'order' => 'ASC', 'orderby' => 'date');
     if (!empty($this->start) || !empty($this->end)) {
         $args['date_query'] = array(array('after' => date('Y-n-d 00:00:00', strtotime($this->start)), 'before' => date('Y-n-d 23:59:59', strtotime($this->end)), 'inclusive' => true));
     }
     //echo json_encode($args ); exit;
     $payments = edd_get_payments($args);
     if ($payments) {
         foreach ($payments as $payment) {
             $payment = new EDD_Payment($payment->ID);
             $payment_meta = $payment->payment_meta;
             $user_info = $payment->user_info;
             $downloads = $payment->cart_details;
             $total = $payment->total;
             $user_id = isset($user_info['id']) && $user_info['id'] != -1 ? $user_info['id'] : $user_info['email'];
             $products = '';
             $products_raw = '';
             $skus = '';
             if ($downloads) {
                 foreach ($downloads as $key => $download) {
                     // Download ID
                     $id = isset($payment_meta['cart_details']) ? $download['id'] : $download;
                     $qty = isset($download['quantity']) ? $download['quantity'] : 1;
                     if (isset($download['price'])) {
                         $price = $download['price'];
                     } else {
                         // If the download has variable prices, override the default price
                         $price_override = isset($payment_meta['cart_details']) ? $download['price'] : null;
                         $price = edd_get_download_final_price($id, $user_info, $price_override);
                     }
                     $download_tax = isset($download['tax']) ? $download['tax'] : 0;
                     /* Set up verbose product column */
                     $products .= html_entity_decode(get_the_title($id));
                     if ($qty > 1) {
                         $products .= html_entity_decode(' (' . $qty . ')');
                     }
                     $products .= ' - ';
                     if (edd_use_skus()) {
                         $sku = edd_get_download_sku($id);
                         if (!empty($sku)) {
                             $skus .= $sku;
                         }
                     }
                     if (isset($downloads[$key]['item_number']) && isset($downloads[$key]['item_number']['options'])) {
                         $price_options = $downloads[$key]['item_number']['options'];
                         if (isset($price_options['price_id']) && !is_null($price_options['price_id'])) {
                             $products .= html_entity_decode(edd_get_price_option_name($id, $price_options['price_id'], $payment->ID)) . ' - ';
                         }
                     }
                     $products .= html_entity_decode(edd_currency_filter(edd_format_amount($price)));
                     if ($key != count($downloads) - 1) {
                         $products .= ' / ';
                         if (edd_use_skus()) {
                             $skus .= ' / ';
                         }
                     }
                     /* Set up raw products column - Nothing but product names */
                     $products_raw .= html_entity_decode(get_the_title($id)) . '|' . $price . '{' . $download_tax . '}';
                     if ($key != count($downloads) - 1) {
                         $products_raw .= ' / ';
                     }
                 }
             }
             if (is_numeric($user_id)) {
                 $user = get_userdata($user_id);
             } else {
                 $user = false;
             }
             $data[] = array('id' => $payment->ID, 'seq_id' => $payment->number, 'email' => $payment_meta['email'], 'customer_id' => $payment->customer_id, 'first' => $user_info['first_name'], 'last' => $user_info['last_name'], 'address1' => isset($user_info['address']['line1']) ? $user_info['address']['line1'] : '', 'address2' => isset($user_info['address']['line2']) ? $user_info['address']['line2'] : '', 'city' => isset($user_info['address']['city']) ? $user_info['address']['city'] : '', 'state' => isset($user_info['address']['state']) ? $user_info['address']['state'] : '', 'country' => isset($user_info['address']['country']) ? $user_info['address']['country'] : '', 'zip' => isset($user_info['address']['zip']) ? $user_info['address']['zip'] : '', 'products' => $products, 'products_raw' => $products_raw, 'skus' => $skus, 'amount' => html_entity_decode(edd_format_amount($total)), 'tax' => html_entity_decode(edd_format_amount(edd_get_payment_tax($payment->ID, $payment_meta))), 'discount' => isset($user_info['discount']) && $user_info['discount'] != 'none' ? $user_info['discount'] : __('none', 'easy-digital-downloads'), 'gateway' => edd_get_gateway_admin_label(get_post_meta($payment->ID, '_edd_payment_gateway', true)), 'trans_id' => $payment->transaction_id, 'key' => $payment_meta['key'], 'date' => $payment->date, 'user' => $user ? $user->display_name : __('guest', 'easy-digital-downloads'), 'currency' => $payment->currency, 'ip' => $payment->ip, 'mode' => $payment->get_meta('_edd_payment_mode', true), 'status' => 'publish' === $payment->status ? 'complete' : $payment->status);
         }
         $data = apply_filters('edd_export_get_data', $data);
         $data = apply_filters('edd_export_get_data_' . $this->export_type, $data);
         return $data;
     }
     return false;
 }