Give_Customer::remove_payment PHP Method

remove_payment() public method

Remove a payment from this customer, then triggers reducing stats.
Since: 1.0
public remove_payment ( integer $payment_id, boolean $update_stats = true ) : boolean
$payment_id integer The Payment ID to remove.
$update_stats boolean For backwards compatibility, if we should increase the stats or not.
return boolean If the removal was successful.
    public function remove_payment($payment_id = 0, $update_stats = true)
    {
        if (empty($payment_id)) {
            return false;
        }
        $payment = new Give_Payment($payment_id);
        if ('publish' !== $payment->status && 'revoked' !== $payment->status) {
            $update_stats = false;
        }
        $new_payment_ids = '';
        if (!empty($this->payment_ids)) {
            $payment_ids = array_map('absint', explode(',', $this->payment_ids));
            $pos = array_search($payment_id, $payment_ids);
            if (false === $pos) {
                return false;
            }
            unset($payment_ids[$pos]);
            $payment_ids = array_filter($payment_ids);
            $new_payment_ids = implode(',', array_unique(array_values($payment_ids)));
        }
        do_action('give_customer_pre_remove_payment', $payment_id, $this->id);
        $payment_removed = $this->update(array('payment_ids' => $new_payment_ids));
        if ($payment_removed) {
            $this->payment_ids = $new_payment_ids;
            if ($update_stats) {
                // We removed this payment successfully, decrement the stats
                $payment_amount = give_get_payment_amount($payment_id);
                if (!empty($payment_amount)) {
                    $this->decrease_value($payment_amount);
                }
                $this->decrease_purchase_count();
            }
        }
        do_action('give_customer_post_remove_payment', $payment_removed, $payment_id, $this->id);
        return $payment_removed;
    }

Usage Example

 /**
  * Removes a payment ID from a customer
  *
  * @access  public
  * @since   1.0
  */
 public function remove_payment($customer_id = 0, $payment_id = 0)
 {
     $customer = new Give_Customer($customer_id);
     if (!$customer) {
         return false;
     }
     // Remove the payment, but don't decrease stats, as this function previously did not
     return $customer->remove_payment($payment_id, false);
 }
All Usage Examples Of Give_Customer::remove_payment