/** * 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); } }
/** * Complete a purchase * * 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) { if ($old_status == 'publish' || $old_status == 'complete') { return; } // Make sure that payments are only completed once // Make sure the payment completion is only processed when new status is complete if ($new_status != 'publish' && $new_status != 'complete') { return; } $payment_meta = give_get_payment_meta($payment_id); $creation_date = get_post_field('post_date', $payment_id, 'raw'); $completed_date = give_get_payment_completed_date($payment_id); $user_info = give_get_payment_meta_user_info($payment_id); $donor_id = give_get_payment_customer_id($payment_id); $amount = give_get_payment_amount($payment_id); do_action('give_pre_complete_purchase', $payment_id); $price_id = isset($_POST['give-price-id']) ? (int) $_POST['give-price-id'] : false; // Ensure these actions only run once, ever if (empty($completed_date)) { if (!give_is_test_mode() || apply_filters('give_log_test_payment_stats', false)) { give_record_sale_in_log($payment_meta['form_id'], $payment_id, $price_id, $creation_date); give_increase_purchase_count($payment_meta['form_id']); give_increase_earnings($payment_meta['form_id'], $amount); } do_action('give_complete_form_donation', $payment_meta['form_id'], $payment_id, $payment_meta); } // 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 Give()->customers->increment_stats($donor_id, $amount); give_increase_total_earnings($amount); // Ensure this action only runs once ever if (empty($completed_date)) { // Save the completed date give_update_payment_meta($payment_id, '_give_completed_date', current_time('mysql')); do_action('give_complete_purchase', $payment_id); } }
/** * * 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; }
/** * Save * * Once items have been set, an update is needed to save them to the database. * * @access public * * @return bool True of the save occurred, false if it failed or wasn't needed */ public function save() { $saved = false; //Must have an ID if (empty($this->ID)) { $payment_id = $this->insert_payment(); if (false === $payment_id) { $saved = false; } else { $this->ID = $payment_id; } } //Set ID if not matching if ($this->ID !== $this->_ID) { $this->ID = $this->_ID; } // If we have something pending, let's save it if (!empty($this->pending)) { $total_increase = 0; $total_decrease = 0; foreach ($this->pending as $key => $value) { switch ($key) { case 'donations': // Update totals for pending donations foreach ($this->pending[$key] as $item) { $quantity = isset($item['quantity']) ? $item['quantity'] : 1; $price_id = isset($item['price_id']) ? $item['price_id'] : 0; switch ($item['action']) { case 'add': $price = $item['price']; if ('publish' === $this->status || 'complete' === $this->status) { // Add sales logs $log_date = date_i18n('Y-m-d G:i:s', current_time('timestamp')); $y = 0; while ($y < $quantity) { give_record_sale_in_log($item['id'], $this->ID, $price_id, $log_date); $y++; } $form = new Give_Donate_Form($item['id']); $form->increase_sales($quantity); $form->increase_earnings($price); $total_increase += $price; } break; case 'remove': $log_args = array('post_type' => 'give_log', 'post_parent' => $item['id'], 'numberposts' => $quantity, 'meta_query' => array(array('key' => '_give_log_payment_id', 'value' => $this->ID, 'compare' => '='), array('key' => '_give_log_price_id', 'value' => $price_id, 'compare' => '='))); $found_logs = get_posts($log_args); foreach ($found_logs as $log) { wp_delete_post($log->ID, true); } if ('publish' === $this->status || 'complete' === $this->status) { $form = new Give_Donate_Form($item['id']); $form->decrease_sales($quantity); $form->decrease_earnings($item['amount']); $total_decrease += $item['amount']; } break; } } break; case 'fees': if ('publish' !== $this->status && 'complete' !== $this->status) { break; } if (empty($this->pending[$key])) { break; } foreach ($this->pending[$key] as $fee) { switch ($fee['action']) { case 'add': $total_increase += $fee['amount']; break; case 'remove': $total_decrease += $fee['amount']; break; } } break; case 'status': $this->update_status($this->status); break; case 'gateway': $this->update_meta('_give_payment_gateway', $this->gateway); break; case 'mode': $this->update_meta('_give_payment_mode', $this->mode); break; case 'transaction_id': $this->update_meta('_give_payment_transaction_id', $this->transaction_id); break; case 'ip': $this->update_meta('_give_payment_user_ip', $this->ip); break; case 'customer_id': $this->update_meta('_give_payment_customer_id', $this->customer_id); break; case 'user_id': $this->update_meta('_give_payment_user_id', $this->user_id); break; case 'form_title': $this->update_meta('_give_payment_form_title', $this->form_title); break; case 'form_id': $this->update_meta('_give_payment_form_id', $this->form_id); break; case 'price_id': $this->update_meta('_give_payment_price_id', $this->price_id); break; case 'first_name': $this->user_info['first_name'] = $this->first_name; break; case 'last_name': $this->user_info['last_name'] = $this->last_name; break; case 'address': $this->user_info['address'] = $this->address; break; case 'email': $this->update_meta('_give_payment_user_email', $this->email); break; case 'key': $this->update_meta('_give_payment_purchase_key', $this->key); break; case 'number': $this->update_meta('_give_payment_number', $this->number); break; case 'date': $args = array('ID' => $this->ID, 'post_date' => $this->date, 'edit_date' => true); wp_update_post($args); break; case 'completed_date': $this->update_meta('_give_completed_date', $this->completed_date); break; case 'parent_payment': $args = array('ID' => $this->ID, 'post_parent' => $this->parent_payment); wp_update_post($args); break; default: do_action('give_payment_save', $this, $key); break; } } if ('pending' !== $this->status) { $customer = new Give_Customer($this->customer_id); $total_change = $total_increase - $total_decrease; if ($total_change < 0) { $total_change = -$total_change; // Decrease the customer's purchase stats $customer->decrease_value($total_change); give_decrease_total_earnings($total_change); } else { if ($total_change > 0) { // Increase the customer's purchase stats $customer->increase_value($total_change); give_increase_total_earnings($total_change); } } } $this->update_meta('_give_payment_total', $this->total); $new_meta = array('form_title' => $this->form_title, 'form_id' => $this->form_id, 'price_id' => $this->price_id, 'fees' => $this->fees, 'currency' => $this->currency, 'user_info' => $this->user_info); $meta = $this->get_meta(); $merged_meta = array_merge($meta, $new_meta); // Only save the payment meta if it's changed if (md5(serialize($meta)) !== md5(serialize($merged_meta))) { $updated = $this->update_meta('_give_payment_meta', $merged_meta); if (false !== $updated) { $saved = true; } } $this->pending = array(); $saved = true; } if (true === $saved) { $this->setup_payment($this->ID); } return $saved; }
/** * * 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; }