This class handles customers.
Since: 1.0
Ejemplo n.º 1
0
/**
 * Complete a purchase aka donation
 *
 * Performs all necessary actions to complete a purchase.
 * Triggered by the give_update_payment_status() function.
 *
 * @since  1.0
 *
 * @param  int    $payment_id The ID number of the payment.
 * @param  string $new_status The status of the payment, probably "publish".
 * @param  string $old_status The status of the payment prior to being marked as "complete", probably "pending".
 *
 * @return void
 */
function give_complete_purchase($payment_id, $new_status, $old_status)
{
    // Make sure that payments are only completed once
    if ($old_status == 'publish' || $old_status == 'complete') {
        return;
    }
    // Make sure the payment completion is only processed when new status is complete
    if ($new_status != 'publish' && $new_status != 'complete') {
        return;
    }
    $payment = new Give_Payment($payment_id);
    $creation_date = get_post_field('post_date', $payment_id, 'raw');
    $payment_meta = $payment->payment_meta;
    $completed_date = $payment->completed_date;
    $user_info = $payment->user_info;
    $customer_id = $payment->customer_id;
    $amount = $payment->total;
    $price_id = $payment->price_id;
    $form_id = $payment->form_id;
    do_action('give_pre_complete_purchase', $payment_id);
    // Ensure these actions only run once, ever
    if (empty($completed_date)) {
        give_record_sale_in_log($form_id, $payment_id, $price_id, $creation_date);
        do_action('give_complete_form_donation', $form_id, $payment_id, $payment_meta);
    }
    // Increase the earnings for this form ID
    give_increase_earnings($form_id, $amount);
    give_increase_purchase_count($form_id);
    // Clear the total earnings cache
    delete_transient('give_earnings_total');
    // Clear the This Month earnings (this_monththis_month is NOT a typo)
    delete_transient(md5('give_earnings_this_monththis_month'));
    delete_transient(md5('give_earnings_todaytoday'));
    // Increase the donor's purchase stats
    $customer = new Give_Customer($customer_id);
    $customer->increase_purchase_count();
    $customer->increase_value($amount);
    give_increase_total_earnings($amount);
    // Ensure this action only runs once ever
    if (empty($completed_date)) {
        // Save the completed date
        $payment->completed_date = current_time('mysql');
        $payment->save();
        /**
         * Fires after a donation successfully complete.
         *
         * @since 1.6
         *
         * @param int $payment_id The ID of the payment.
         */
        do_action('give_complete_purchase', $payment_id);
    }
}
 /**
  * Get the Export Data
  *
  * @access public
  * @since 1.5
  * @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()
 {
     $args = array('number' => $this->per_step, 'offset' => $this->per_step * ($this->step - 1), 'orderby' => 'id', 'order' => 'DESC');
     $customers = Give()->customers->get_customers($args);
     if ($customers) {
         $allowed_payment_status = apply_filters('give_recount_customer_payment_statuses', give_get_payment_status_keys());
         foreach ($customers as $customer) {
             $attached_payment_ids = explode(',', $customer->payment_ids);
             $attached_args = array('post__in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status);
             $attached_payments = (array) give_get_payments($attached_args);
             $unattached_args = array('post__not_in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status, 'meta_query' => array(array('key' => '_give_payment_user_email', 'value' => $customer->email, 'compare' => '=')));
             $unattached_payments = give_get_payments($unattached_args);
             $payments = array_merge($attached_payments, $unattached_payments);
             $purchase_value = 0.0;
             $purchase_count = 0;
             $payment_ids = array();
             if ($payments) {
                 foreach ($payments as $payment) {
                     $should_process_payment = 'publish' == $payment->post_status ? true : false;
                     $should_process_payment = apply_filters('give_customer_recount_should_process_payment', $should_process_payment, $payment);
                     if (true === $should_process_payment) {
                         if (apply_filters('give_customer_recount_should_increase_value', true, $payment)) {
                             $purchase_value += give_get_payment_amount($payment->ID);
                         }
                         if (apply_filters('give_customer_recount_should_increase_count', true, $payment)) {
                             $purchase_count++;
                         }
                     }
                     $payment_ids[] = $payment->ID;
                 }
             }
             $payment_ids = implode(',', $payment_ids);
             $customer_update_data = array('purchase_count' => $purchase_count, 'purchase_value' => $purchase_value, 'payment_ids' => $payment_ids);
             $customer_instance = new Give_Customer($customer->id);
             $customer_instance->update($customer_update_data);
         }
         return true;
     }
     return false;
 }
 /**
  * Zero out the data on step one
  *
  * @access public
  * @since 1.5
  * @return void
  */
 public function pre_fetch()
 {
     if ($this->step === 1) {
         $allowed_payment_status = apply_filters('give_recount_customer_payment_statuses', give_get_payment_status_keys());
         // Before we start, let's zero out the customer's data
         $customer = new Give_Customer($this->customer_id);
         $customer->update(array('purchase_value' => give_format_amount(0), 'purchase_count' => 0));
         $attached_payment_ids = explode(',', $customer->payment_ids);
         $attached_args = array('post__in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status);
         $attached_payments = give_get_payments($attached_args);
         $unattached_args = array('post__not_in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status, 'meta_query' => array(array('key' => '_give_payment_user_email', 'value' => $customer->email)));
         $unattached_payments = give_get_payments($unattached_args);
         $payments = array_merge($attached_payments, $unattached_payments);
         $this->store_data('give_recount_customer_payments_' . $customer->id, $payments);
     }
 }
 /**
  * Decrements customer purchase stats
  *
  * @access  public
  * @since   1.0
  */
 public function decrement_stats($customer_id = 0, $amount = 0.0)
 {
     $customer = new Give_Customer($customer_id);
     if (!$customer) {
         return false;
     }
     $decreased_count = $customer->decrease_purchase_count();
     $decreased_value = $customer->decrease_value($amount);
     return $decreased_count && $decreased_value ? true : false;
 }
Ejemplo n.º 5
0
/**
 * Disconnect a user ID from a donor
 *
 * @since  1.0
 *
 * @param  array $args Array of arguements
 *
 * @return bool        If the disconnect was sucessful
 */
function give_disconnect_customer_user_id($args)
{
    $customer_edit_role = apply_filters('give_edit_customers_role', 'edit_give_payments');
    if (!is_admin() || !current_user_can($customer_edit_role)) {
        wp_die(__('You do not have permission to edit this donor.', 'give'));
    }
    if (empty($args)) {
        return;
    }
    $customer_id = (int) $args['customer_id'];
    $nonce = $args['_wpnonce'];
    if (!wp_verify_nonce($nonce, 'edit-customer')) {
        wp_die(__('Cheatin\' eh?!', 'give'));
    }
    $customer = new Give_Customer($customer_id);
    if (empty($customer->id)) {
        return false;
    }
    do_action('give_pre_customer_disconnect_user_id', $customer_id, $customer->user_id);
    $customer_args = array('user_id' => 0);
    if ($customer->update($customer_args)) {
        global $wpdb;
        if (!empty($customer->payment_ids)) {
            $wpdb->query("UPDATE {$wpdb->postmeta} SET meta_value = 0 WHERE meta_key = '_give_payment_user_id' AND post_id IN ( {$customer->payment_ids} )");
        }
        $output['success'] = true;
    } else {
        $output['success'] = false;
        give_set_error('give-disconnect-user-fail', __('Failed to disconnect user from donor', 'give'));
    }
    do_action('give_post_customer_disconnect_user_id', $customer_id);
    if (defined('DOING_AJAX') && DOING_AJAX) {
        header('Content-Type: application/json');
        echo json_encode($output);
        wp_die();
    }
    return $output;
}
Ejemplo n.º 6
0
/**
 * Deletes a Donation
 *
 * @since  1.0
 * @global      $give_logs
 *
 * @param  int  $payment_id      Payment ID (default: 0)
 * @param  bool $update_customer If we should update the customer stats (default:true)
 *
 * @return void
 */
function give_delete_purchase($payment_id = 0, $update_customer = true)
{
    global $give_logs;
    $payment = new Give_Payment($payment_id);
    $amount = give_get_payment_amount($payment_id);
    $status = $payment->post_status;
    $customer_id = give_get_payment_customer_id($payment_id);
    $customer = new Give_Customer($customer_id);
    //Only undo purchases that aren't these statuses
    $dont_undo_statuses = apply_filters('give_undo_purchase_statuses', array('pending', 'cancelled'));
    if (!in_array($status, $dont_undo_statuses)) {
        give_undo_purchase(false, $payment_id);
    }
    if ($status == 'publish') {
        // Only decrease earnings if they haven't already been decreased (or were never increased for this payment)
        give_decrease_total_earnings($amount);
        // Clear the This Month earnings (this_monththis_month is NOT a typo)
        delete_transient(md5('give_earnings_this_monththis_month'));
        if ($customer->id && $update_customer) {
            // Decrement the stats for the customer
            $customer->decrease_purchase_count();
            $customer->decrease_value($amount);
        }
    }
    do_action('give_payment_delete', $payment_id);
    if ($customer->id && $update_customer) {
        // Remove the payment ID from the customer
        $customer->remove_payment($payment_id);
    }
    // Remove the payment
    wp_delete_post($payment_id, true);
    // Remove related sale log entries
    $give_logs->delete_logs(null, 'sale', array(array('key' => '_give_log_payment_id', 'value' => $payment_id)));
    do_action('give_payment_deleted', $payment_id);
}
Ejemplo n.º 7
0
 public function test_customer_notes()
 {
     $customer = new Give_Customer('*****@*****.**');
     $this->assertInternalType('array', $customer->notes);
     $this->assertEquals(0, $customer->get_notes_count());
     $note_1 = $customer->add_note('Testing');
     $this->assertEquals(0, array_search($note_1, $customer->notes));
     $this->assertEquals(1, $customer->get_notes_count());
     $note_2 = $customer->add_note('Test 2nd Note');
     $this->assertEquals(1, array_search($note_1, $customer->notes));
     $this->assertEquals(0, array_search($note_2, $customer->notes));
     $this->assertEquals(2, $customer->get_notes_count());
     // Verify we took out all empty rows
     $this->assertEquals(count($customer->notes), count(array_values($customer->notes)));
     // Test 1 note per page, page 1
     $newest_note = $customer->get_notes(1);
     $this->assertEquals(1, count($newest_note));
     $this->assertEquals($newest_note[0], $note_2);
     // Test 1 note per page, page 2
     $second_note = $customer->get_notes(1, 2);
     $this->assertEquals(1, count($second_note));
     $this->assertEquals($second_note[0], $note_1);
 }
Ejemplo n.º 8
0
 /**
  * Used during the process of moving to refunded or pending, to decrement stats
  *
  * @since  1.5
  * @access private
  *
  * @param  bool $alter_store_earnings If the method should alter the store earnings
  * @param  bool $alter_customer_value If the method should reduce the customer value
  * @param  bool $alter_customer_purchase_count If the method should reduce the customer's purchase count
  *
  * @return void
  */
 private function maybe_alter_stats($alter_store_earnings, $alter_customer_value, $alter_customer_purchase_count)
 {
     give_undo_purchase(false, $this->ID);
     // Decrease store earnings
     if (true === $alter_store_earnings) {
         give_decrease_total_earnings($this->total);
     }
     // Decrement the stats for the customer
     if (!empty($this->customer_id)) {
         $customer = new Give_Customer($this->customer_id);
         if (true === $alter_customer_value) {
             $customer->decrease_value($this->total);
         }
         if (true === $alter_customer_purchase_count) {
             $customer->decrease_purchase_count();
         }
     }
 }
Ejemplo n.º 9
0
/**
 * Insert Payment
 *
 * @since 1.0
 *
 * @param array $payment_data
 *
 * @return int|bool Payment ID if payment is inserted, false otherwise
 */
function give_insert_payment($payment_data = array())
{
    if (empty($payment_data)) {
        return false;
    }
    // Make sure the payment is inserted with the correct timezone
    date_default_timezone_set(give_get_timezone_id());
    // Construct the payment title
    if (isset($payment_data['user_info']['first_name']) || isset($payment_data['user_info']['last_name'])) {
        $payment_title = $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'];
    } else {
        $payment_title = $payment_data['user_email'];
    }
    // Find the next payment number, if enabled
    if (give_get_option('enable_sequential')) {
        $number = give_get_next_payment_number();
    }
    $args = apply_filters('give_insert_payment_args', array('post_title' => $payment_title, 'post_status' => isset($payment_data['status']) ? $payment_data['status'] : 'pending', 'post_type' => 'give_payment', 'post_parent' => isset($payment_data['parent']) ? $payment_data['parent'] : null, 'post_date' => isset($payment_data['post_date']) ? $payment_data['post_date'] : null, 'post_date_gmt' => isset($payment_data['post_date']) ? get_gmt_from_date($payment_data['post_date']) : null), $payment_data);
    // Create a blank payment
    $payment = wp_insert_post($args);
    if ($payment) {
        $payment_meta = array('currency' => $payment_data['currency'], 'form_title' => $payment_data['give_form_title'], 'form_id' => $payment_data['give_form_id'], 'price_id' => give_get_price_id($payment_data['give_form_id'], $payment_data['price']), 'user_info' => $payment_data['user_info']);
        $mode = give_is_test_mode() ? 'test' : 'live';
        $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : '';
        $gateway = empty($gateway) && isset($_POST['give-gateway']) ? $_POST['give-gateway'] : $gateway;
        if (!$payment_data['price']) {
            // Ensures the _give_payment_total meta key is created for donations with an amount of 0
            $payment_data['price'] = '0.00';
        }
        // Create or update a customer
        $customer = new Give_Customer($payment_data['user_email']);
        $customer_data = array('name' => $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'], 'email' => $payment_data['user_email'], 'user_id' => $payment_data['user_info']['id']);
        if (empty($customer->id)) {
            $customer->create($customer_data);
        } else {
            // Only update the customer if their name or email has changed
            if ($customer_data['email'] !== $customer->email || $customer_data['name'] !== $customer->name) {
                // We shouldn't be updating the User ID here, that is an admin task
                unset($customer_data['user_id']);
                $customer->update($customer_data);
            }
        }
        $customer->attach_payment($payment, false);
        // Record the payment details
        give_update_payment_meta($payment, '_give_payment_meta', apply_filters('give_payment_meta', $payment_meta, $payment_data));
        give_update_payment_meta($payment, '_give_payment_user_id', $payment_data['user_info']['id']);
        give_update_payment_meta($payment, '_give_payment_donor_id', $customer->id);
        give_update_payment_meta($payment, '_give_payment_user_email', $payment_data['user_email']);
        give_update_payment_meta($payment, '_give_payment_user_ip', give_get_ip());
        give_update_payment_meta($payment, '_give_payment_purchase_key', $payment_data['purchase_key']);
        give_update_payment_meta($payment, '_give_payment_total', $payment_data['price']);
        give_update_payment_meta($payment, '_give_payment_mode', $mode);
        give_update_payment_meta($payment, '_give_payment_gateway', $gateway);
        if (give_get_option('enable_sequential')) {
            give_update_payment_meta($payment, '_give_payment_number', give_format_payment_number($number));
            update_option('give_last_payment_number', $number);
        }
        // Clear the user's purchased cache
        delete_transient('give_user_' . $payment_data['user_info']['id'] . '_purchases');
        do_action('give_insert_payment', $payment, $payment_data);
        return $payment;
        // Return the ID
    }
    // Return false if no payment was inserted
    return false;
}
Ejemplo n.º 10
0
/**
 *
 * Process the payment details edit
 *
 * @access      private
 *
 * @param $data
 *
 * @since       1.0
 * @return      void
 *
 */
function give_update_payment_details($data)
{
    if (!current_user_can('edit_give_payments', $data['give_payment_id'])) {
        wp_die(__('You do not have permission to edit this payment record', 'give'), __('Error', 'give'), array('response' => 403));
    }
    check_admin_referer('give_update_payment_details_nonce');
    // Retrieve the payment ID
    $payment_id = absint($data['give_payment_id']);
    // Retrieve existing payment meta
    $meta = give_get_payment_meta($payment_id);
    $user_info = give_get_payment_meta_user_info($payment_id);
    $status = $data['give-payment-status'];
    $user_id = isset($data['give-payment-user-id']) ? intval($data['give-payment-user-id']) : '';
    $date = sanitize_text_field($data['give-payment-date']);
    $hour = sanitize_text_field($data['give-payment-time-hour']);
    $form_id = give_get_payment_form_id($payment_id);
    // Restrict to our high and low
    if ($hour > 23) {
        $hour = 23;
    } elseif ($hour < 0) {
        $hour = 00;
    }
    $minute = sanitize_text_field($data['give-payment-time-min']);
    // Restrict to our high and low
    if ($minute > 59) {
        $minute = 59;
    } elseif ($minute < 0) {
        $minute = 00;
    }
    $address = array_map('trim', $data['give-payment-address'][0]);
    $date = date('Y-m-d', strtotime($date)) . ' ' . $hour . ':' . $minute . ':00';
    $curr_total = give_sanitize_amount(give_get_payment_amount($payment_id));
    $new_total = give_sanitize_amount($_POST['give-payment-total']);
    $curr_customer_id = sanitize_text_field($data['give-current-customer']);
    $new_customer_id = sanitize_text_field($data['customer-id']);
    do_action('give_update_edited_purchase', $payment_id);
    // Update main payment record
    $updated = wp_update_post(array('ID' => $payment_id, 'edit_date' => true, 'post_date' => $date));
    if (0 === $updated) {
        wp_die(esc_attr__('Error Updating Payment', 'give'), esc_attr__('Error', 'give'), array('response' => 400));
    }
    $customer_changed = false;
    if (isset($data['give-new-customer']) && $data['give-new-customer'] == '1') {
        $email = isset($data['give-new-customer-email']) ? sanitize_text_field($data['give-new-customer-email']) : '';
        $names = isset($data['give-new-customer-name']) ? sanitize_text_field($data['give-new-customer-name']) : '';
        if (empty($email) || empty($names)) {
            wp_die(esc_attr__('New Customers require a name and email address', 'give'));
        }
        $customer = new Give_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 Give_Customer($curr_customer_id);
                give_set_error('give-payment-new-customer-fail', __('Error creating new customer', 'give'));
            }
        }
        $new_customer_id = $customer->id;
        $previous_customer = new Give_Customer($curr_customer_id);
        $customer_changed = true;
    } elseif ($curr_customer_id !== $new_customer_id) {
        $customer = new Give_Customer($new_customer_id);
        $email = $customer->email;
        $names = $customer->name;
        $previous_customer = new Give_Customer($curr_customer_id);
        $customer_changed = true;
    } else {
        $customer = new Give_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);
        }
        update_post_meta($payment_id, '_give_payment_customer_id', $customer->id);
    }
    // Set new meta values
    $user_info['id'] = $customer->user_id;
    $user_info['email'] = $customer->email;
    $user_info['first_name'] = $first_name;
    $user_info['last_name'] = $last_name;
    $user_info['address'] = $address;
    $meta['user_info'] = $user_info;
    // Check for payment notes
    if (!empty($data['give-payment-note'])) {
        $note = wp_kses($data['give-payment-note'], array());
        give_insert_payment_note($payment_id, $note);
    }
    // Set new status
    give_update_payment_status($payment_id, $status);
    give_update_payment_meta($payment_id, '_give_payment_user_id', $customer->user_id);
    give_update_payment_meta($payment_id, '_give_payment_user_email', $customer->email);
    give_update_payment_meta($payment_id, '_give_payment_meta', $meta);
    give_update_payment_meta($payment_id, '_give_payment_total', $new_total);
    // 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;
            give_increase_total_earnings($difference);
            $form = new Give_Donate_Form($form_id);
            $form->increase_earnings($difference);
        } elseif ($curr_total > $new_total) {
            // Decrease if our new total is lower
            $difference = $curr_total - $new_total;
            give_decrease_total_earnings($difference);
            $form = new Give_Donate_Form($form_id);
            $form->decrease_earnings($difference);
        }
    }
    do_action('give_updated_edited_purchase', $payment_id);
    wp_safe_redirect(admin_url('edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-updated&id=' . $payment_id));
    exit;
}
Ejemplo n.º 11
0
/**
 *
 * Process the payment details edit
 *
 * @access      private
 *
 * @param array $data
 *
 * @since       1.0
 * @return      void
 *
 */
function give_update_payment_details($data)
{
    if (!current_user_can('edit_give_payments', $data['give_payment_id'])) {
        wp_die(esc_html__('You do not have permission to edit payment records.', 'give'), esc_html__('Error', 'give'), array('response' => 403));
    }
    check_admin_referer('give_update_payment_details_nonce');
    // Retrieve the payment ID
    $payment_id = absint($data['give_payment_id']);
    /* @var Give_Payment $payment */
    $payment = new Give_Payment($payment_id);
    // Retrieve existing payment meta
    $meta = $payment->get_meta();
    $user_info = $payment->user_info;
    $status = $data['give-payment-status'];
    $date = sanitize_text_field($data['give-payment-date']);
    $hour = sanitize_text_field($data['give-payment-time-hour']);
    // Restrict to our high and low
    if ($hour > 23) {
        $hour = 23;
    } elseif ($hour < 0) {
        $hour = 00;
    }
    $minute = sanitize_text_field($data['give-payment-time-min']);
    // Restrict to our high and low
    if ($minute > 59) {
        $minute = 59;
    } elseif ($minute < 0) {
        $minute = 00;
    }
    $address = array_map('trim', $data['give-payment-address'][0]);
    $curr_total = give_sanitize_amount($payment->total);
    $new_total = give_sanitize_amount($data['give-payment-total']);
    $date = date('Y-m-d', strtotime($date)) . ' ' . $hour . ':' . $minute . ':00';
    $curr_customer_id = sanitize_text_field($data['give-current-customer']);
    $new_customer_id = sanitize_text_field($data['customer-id']);
    /**
     * Fires before updating edited purchase.
     *
     * @since 1.0
     *
     * @param int $payment_id The ID of the payment.
     */
    do_action('give_update_edited_purchase', $payment_id);
    $payment->date = $date;
    $updated = $payment->save();
    if (0 === $updated) {
        wp_die(esc_html__('Error Updating Payment.', 'give'), esc_html__('Error', 'give'), array('response' => 400));
    }
    $customer_changed = false;
    if (isset($data['give-new-customer']) && $data['give-new-customer'] == '1') {
        $email = isset($data['give-new-customer-email']) ? sanitize_text_field($data['give-new-customer-email']) : '';
        $names = isset($data['give-new-customer-name']) ? sanitize_text_field($data['give-new-customer-name']) : '';
        if (empty($email) || empty($names)) {
            wp_die(esc_html__('New Customers require a name and email address.', 'give'), esc_html__('Error', 'give'), array('response' => 400));
        }
        $customer = new Give_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 donor, assume the previous donor
                $customer_changed = false;
                $customer = new Give_Customer($curr_customer_id);
                give_set_error('give-payment-new-customer-fail', esc_html__('Error creating new donor.', 'give'));
            }
        }
        $new_customer_id = $customer->id;
        $previous_customer = new Give_Customer($curr_customer_id);
        $customer_changed = true;
    } elseif ($curr_customer_id !== $new_customer_id) {
        $customer = new Give_Customer($new_customer_id);
        $email = $customer->email;
        $names = $customer->name;
        $previous_customer = new Give_Customer($curr_customer_id);
        $customer_changed = true;
    } else {
        $customer = new Give_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 ('publish' == $status) {
            // Reduce previous user donation count and amount.
            $previous_customer->decrease_purchase_count();
            $previous_customer->decrease_value($curr_total);
            // If purchase was completed adjust stats of new customers.
            $customer->increase_purchase_count();
            $customer->increase_value($new_total);
        }
        $payment->customer_id = $customer->id;
    } else {
        if ('publish' === $status) {
            // Update user donation stat.
            $customer->update_donation_value($curr_total, $new_total);
        }
    }
    // 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;
    // Check for payment notes
    if (!empty($data['give-payment-note'])) {
        $note = wp_kses($data['give-payment-note'], array());
        give_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) {
        if ($new_total > $curr_total) {
            // Increase if our new total is higher
            $difference = $new_total - $curr_total;
            give_increase_total_earnings($difference);
        } elseif ($curr_total > $new_total) {
            // Decrease if our new total is lower
            $difference = $curr_total - $new_total;
            give_decrease_total_earnings($difference);
        }
    }
    $payment->save();
    // Get new give form ID.
    $new_form_id = absint($data['forms']);
    $current_form_id = absint($payment->get_meta('_give_payment_form_id'));
    // We are adding payment transfer code in last to remove any conflict with above functionality.
    // For example: above code will automatically handle form stat (increase/decrease) when payment status changes.
    /* Check if user want to transfer current payment to new give form id. */
    if ($new_form_id != $current_form_id) {
        // Get new give form title.
        $new_form_title = get_the_title($new_form_id);
        // Update new give form data in payment data.
        $payment_meta = $payment->get_meta();
        $payment_meta['form_title'] = $new_form_title;
        $payment_meta['form_id'] = $new_form_id;
        // Update price id post meta data for set donation form.
        if (!give_has_variable_prices($new_form_id)) {
            $payment_meta['price_id'] = '';
        }
        // Update payment give form meta data.
        $payment->update_meta('_give_payment_form_id', $new_form_id);
        $payment->update_meta('_give_payment_form_title', $new_form_title);
        $payment->update_meta('_give_payment_meta', $payment_meta);
        // Update price id payment metadata.
        if (!give_has_variable_prices($new_form_id)) {
            $payment->update_meta('_give_payment_price_id', '');
        }
        // If purchase was completed, adjust stats of forms
        if ('publish' == $status) {
            // Decrease sale of old give form. For other payment status
            $current_form = new Give_Donate_Form($current_form_id);
            $current_form->decrease_sales();
            $current_form->decrease_earnings($curr_total);
            // Increase sale of new give form.
            $new_form = new Give_Donate_Form($new_form_id);
            $new_form->increase_sales();
            $new_form->increase_earnings($new_total);
        }
        // Re setup payment to update new meta value in object.
        $payment->update_payment_setup($payment->ID);
    }
    // Update price id if current form is variable form.
    if (!empty($data['give-variable-price']) && give_has_variable_prices($payment->form_id)) {
        // Get payment meta data.
        $payment_meta = $payment->get_meta();
        // Set payment id to empty string if variable price id is negative ( i.e. custom amount feature enabled ).
        $data['give-variable-price'] = 'custom' === $data['give-variable-price'] ? 'custom' : 0 < $data['give-variable-price'] ? $data['give-variable-price'] : '';
        // Update payment meta data.
        $payment_meta['price_id'] = $data['give-variable-price'];
        // Update payment give form meta data.
        $payment->update_meta('_give_payment_price_id', $data['give-variable-price']);
        $payment->update_meta('_give_payment_meta', $payment_meta);
        // Re setup payment to update new meta value in object.
        $payment->update_payment_setup($payment->ID);
    }
    /**
     * Fires after updating edited purchase.
     *
     * @since 1.0
     *
     * @param int $payment_id The ID of the payment.
     */
    do_action('give_updated_edited_purchase', $payment_id);
    wp_safe_redirect(admin_url('edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-updated&id=' . $payment_id));
    exit;
}