Пример #1
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;
}
 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;
     }
 }
Пример #3
0
/**
 * Insert Payment
 *
 * @since 1.0
 * @param array $payment_data
 * @return int|bool Payment ID if payment is inserted, false otherwise
 */
function edd_insert_payment($payment_data = array())
{
    if (empty($payment_data)) {
        return false;
    }
    $payment = new EDD_Payment();
    if (is_array($payment_data['cart_details']) && !empty($payment_data['cart_details'])) {
        foreach ($payment_data['cart_details'] as $item) {
            $args = array('quantity' => $item['quantity'], 'price_id' => isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null, 'tax' => $item['tax'], 'item_price' => isset($item['item_price']) ? $item['item_price'] : $item['price'], 'fees' => isset($item['fees']) ? $item['fees'] : array(), 'discount' => isset($item['discount']) ? $item['discount'] : 0);
            $options = isset($item['item_number']['options']) ? $item['item_number']['options'] : array();
            $payment->add_download($item['id'], $args, $options);
        }
    }
    $payment->increase_tax(edd_get_cart_fee_tax());
    $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : '';
    $gateway = empty($gateway) && isset($_POST['edd-gateway']) ? $_POST['edd-gateway'] : $gateway;
    $payment->status = !empty($payment_data['status']) ? $payment_data['status'] : 'pending';
    $payment->currency = !empty($payment_data['currency']) ? $payment_data['currency'] : edd_get_currency();
    $payment->user_info = $payment_data['user_info'];
    $payment->gateway = $gateway;
    $payment->user_id = $payment_data['user_info']['id'];
    $payment->email = $payment_data['user_email'];
    $payment->first_name = $payment_data['user_info']['first_name'];
    $payment->last_name = $payment_data['user_info']['last_name'];
    $payment->email = $payment_data['user_info']['email'];
    $payment->ip = edd_get_ip();
    $payment->key = $payment_data['purchase_key'];
    $payment->mode = edd_is_test_mode() ? 'test' : 'live';
    $payment->parent_payment = !empty($payment_data['parent']) ? absint($payment_data['parent']) : '';
    $payment->discounts = !empty($payment_data['user_info']['discount']) ? $payment_data['user_info']['discount'] : array();
    if (isset($payment_data['post_date'])) {
        $payment->date = $payment_data['post_date'];
    }
    if (edd_get_option('enable_sequential')) {
        $number = edd_get_next_payment_number();
        $payment->number = edd_format_payment_number($number);
        update_option('edd_last_payment_number', $number);
    }
    // Clear the user's purchased cache
    delete_transient('edd_user_' . $payment_data['user_info']['id'] . '_purchases');
    $payment->save();
    do_action('edd_insert_payment', $payment->ID, $payment_data);
    if (!empty($payment->ID)) {
        return $payment->ID;
    }
    // Return false if no payment was inserted
    return false;
}
 /**
  * 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();
 }