* @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; $currency_code = $payment->currency; $customer = new EDD_Customer($payment->customer_id); ?> <div class="wrap edd-wrap"> <h2><?php printf(__('Payment %s', 'easy-digital-downloads'), $number); ?>
/** * 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; }
/** * 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; }
/** * 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); }
/** * Get Payment Meta for a specific Payment * * @since 1.2 * @param int $payment_id Payment ID * @param string $meta_key The meta key to pull * @param bool $single Pull single meta entry or as an object * @return mixed $meta Payment Meta */ function edd_get_payment_meta($payment_id = 0, $meta_key = '_edd_payment_meta', $single = true) { $payment = new EDD_Payment($payment_id); return $payment->get_meta($meta_key, $single); }
/** * 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); }
/** * 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; }