Example #1
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);
}
Example #2
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();
         }
     }
 }
Example #3
0
/**
 * Reduces earnings and donation stats when a donation is refunded
 *
 * @since 1.0
 *
 * @param $payment_id
 * @param $new_status
 * @param $old_status
 *
 * @return void
 */
function give_undo_donation_on_refund($payment_id, $new_status, $old_status)
{
    if ('publish' != $old_status && 'revoked' != $old_status) {
        return;
    }
    if ('refunded' != $new_status) {
        return;
    }
    // Set necessary vars
    $payment_meta = give_get_payment_meta($payment_id);
    $amount = give_get_payment_amount($payment_id);
    // Undo this purchase
    give_undo_purchase($payment_meta['form_id'], $payment_id);
    // Decrease total earnings
    give_decrease_total_earnings($amount);
    // Decrement the stats for the donor
    $donor_id = give_get_payment_customer_id($payment_id);
    if ($donor_id) {
        Give()->customers->decrement_stats($donor_id, $amount);
    }
    // Clear the This Month earnings (this_monththis_month is NOT a typo)
    delete_transient(md5('give_earnings_this_monththis_month'));
}
Example #4
0
/**
 * Deletes a Donation
 *
 * @since 1.0
 * @global    $give_logs
 * @uses  Give_Logging::delete_logs()
 *
 * @param int $payment_id Payment ID (default: 0)
 *
 * @return void
 */
function give_delete_purchase($payment_id = 0)
{
    global $give_logs;
    $post = get_post($payment_id);
    if (!$post) {
        return;
    }
    $form_id = give_get_payment_form_id($payment_id);
    give_undo_purchase($form_id, $payment_id);
    $amount = give_get_payment_amount($payment_id);
    $status = $post->post_status;
    $donor_id = give_get_payment_customer_id($payment_id);
    if ($status == 'revoked' || $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 ($donor_id) {
            // Decrement the stats for the donor
            Give()->customers->decrement_stats($donor_id, $amount);
        }
    }
    do_action('give_payment_delete', $payment_id);
    if ($donor_id) {
        // Remove the payment ID from the donor
        Give()->customers->remove_payment($donor_id, $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);
}