/**
 * Reduces earnings and sales stats when a purchase is refunded
 *
 * @since 1.8.2
 * @param $data Arguments passed
 * @return void
 */
function edd_undo_purchase_on_refund($payment_id, $new_status, $old_status)
{
    if ('publish' != $old_status && 'revoked' != $old_status) {
        return;
    }
    if ('refunded' != $new_status) {
        return;
    }
    $downloads = edd_get_payment_meta_cart_details($payment_id);
    if ($downloads) {
        foreach ($downloads as $download) {
            edd_undo_purchase($download['id'], $payment_id);
        }
    }
    // Decrease store earnings
    $amount = edd_get_payment_amount($payment_id);
    edd_decrease_total_earnings($amount);
    // Decrement the stats for the customer
    $customer_id = edd_get_payment_customer_id($payment_id);
    if ($customer_id) {
        $customer = new EDD_Customer($customer_id);
        $customer->decrease_value($amount);
        $customer->decrease_purchase_count();
    }
    // Clear the This Month earnings (this_monththis_month is NOT a typo)
    delete_transient(md5('edd_earnings_this_monththis_month'));
}
/**
 * Reduces earnings and sales stats when a purchase is refunded
 *
 * @since 1.8.2
 * @param $data Arguments passed
 * @return void
 */
function edd_undo_purchase_on_refund($payment_id, $new_status, $old_status)
{
    global $edd_logs;
    if ('publish' != $old_status && 'revoked' != $old_status) {
        return;
    }
    if ('refunded' != $new_status) {
        return;
    }
    $payment = new EDD_Payment($payment_id);
    $downloads = $payment->cart_details;
    if ($downloads) {
        foreach ($downloads as $download) {
            edd_undo_purchase($download['id'], $payment->ID);
        }
    }
    // Decrease store earnings
    edd_decrease_total_earnings($payment->total);
    // Decrement the stats for the customer
    if (!empty($payment->customer_id)) {
        $customer = new EDD_Customer($payment->customer_id);
        $customer->decrease_value($payment->total);
        $customer->decrease_purchase_count();
    }
    // Remove related sale log entries
    $edd_logs->delete_logs(null, 'sale', array(array('key' => '_edd_log_payment_id', 'value' => $payment->ID)));
    // Clear the This Month earnings (this_monththis_month is NOT a typo)
    delete_transient(md5('edd_earnings_this_monththis_month'));
}
/**
 * Deletes a Purchase
 *
 * @since 1.0
 * @global $edd_logs
 *
 * @uses EDD_Logging::delete_logs()
 *
 * @param int $payment_id Payment ID (default: 0)
 * @param bool $update_customer If we should update the customer stats (default:true)
 * @param bool $delete_download_logs If we should remove all file download logs associated with the payment (default:false)
 *
 * @return void
 */
function edd_delete_purchase($payment_id = 0, $update_customer = true, $delete_download_logs = false)
{
    global $edd_logs;
    $post = get_post($payment_id);
    if (!$post) {
        return;
    }
    $downloads = edd_get_payment_meta_downloads($payment_id);
    if (is_array($downloads)) {
        // Update sale counts and earnings for all purchased products
        foreach ($downloads as $download) {
            edd_undo_purchase($download['id'], $payment_id);
        }
    }
    $amount = edd_get_payment_amount($payment_id);
    $status = $post->post_status;
    $customer_id = edd_get_payment_customer_id($payment_id);
    $customer = new EDD_Customer($customer_id);
    if ($status == 'revoked' || $status == 'publish') {
        // Only decrease earnings if they haven't already been decreased (or were never increased for this payment)
        edd_decrease_total_earnings($amount);
        // Clear the This Month earnings (this_monththis_month is NOT a typo)
        delete_transient(md5('edd_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('edd_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
    $edd_logs->delete_logs(null, 'sale', array(array('key' => '_edd_log_payment_id', 'value' => $payment_id)));
    if ($delete_download_logs) {
        $edd_logs->delete_logs(null, 'file_download', array(array('key' => '_edd_log_payment_id', 'value' => $payment_id)));
    }
    do_action('edd_payment_deleted', $payment_id);
}
 /**
  * Used during the process of moving to refunded or pending, to decrement stats
  *
  * @since  2.5.10
  * @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)
 {
     edd_undo_purchase(false, $this->ID);
     // Decrease store earnings
     if (true === $alter_store_earnings) {
         edd_decrease_total_earnings($this->total);
     }
     // Decrement the stats for the customer
     if (!empty($this->customer_id)) {
         $customer = new EDD_Customer($this->customer_id);
         if (true === $alter_customer_value) {
             $customer->decrease_value($this->total);
         }
         if (true === $alter_customer_purchase_count) {
             $customer->decrease_purchase_count();
         }
     }
 }
/**
 * Delete Purchase
 *
 * @access      private
 * @since       1.0 
 * @return      void
*/
function edd_delete_purchase($data)
{
    if (wp_verify_nonce($data['_wpnonce'], 'edd_payment_nonce')) {
        $payment_id = $data['purchase_id'];
        $payment_data = edd_get_payment_meta($payment_id);
        $downloads = maybe_unserialize($payment_data['downloads']);
        // update sale counts and earnings for all purchased products
        foreach ($downloads as $download) {
            edd_undo_purchase($download['id'], $payment_id);
        }
        wp_delete_post($payment_id, true);
        wp_redirect(admin_url('/edit.php?post_type=download&page=edd-payment-history&edd-message=payment_deleted'));
        exit;
    }
}
 /**
  * When a payment is set to a status of 'refunded' process the necessary actions to reduce stats
  *
  * @since  2.5.7
  * @access private
  * @return void
  */
 private function process_refund()
 {
     global $edd_logs;
     $process_refund = true;
     // If the payment was not in publish or revoked status, don't decrement stats as they were never incremented
     if ('publish' != $this->old_status && 'revoked' != $this->old_status || 'refunded' != $this->status) {
         $process_refund = false;
     }
     // Allow extensions to filter for their own payment types, Example: Recurring Payments
     $process_refund = apply_filters('edd_should_process_refund', $process_refund, $this);
     if (false === $process_refund) {
         return;
     }
     do_action('edd_pre_refund_payment', $this);
     edd_undo_purchase(false, $this->ID);
     // Decrease store earnings
     $maybe_decrease_store_earnings = apply_filters('edd_decrease_store_earnings_on_refund', true, $this);
     if (true === $maybe_decrease_store_earnings) {
         edd_decrease_total_earnings($this->total);
     }
     // Decrement the stats for the customer
     if (!empty($this->customer_id)) {
         $customer = new EDD_Customer($this->customer_id);
         $maybe_decrease_value = apply_filters('edd_decrease_customer_value_on_refund', true, $this);
         if (true === $maybe_decrease_value) {
             $customer->decrease_value($this->total);
         }
         $maybe_decrease_purchase_count = apply_filters('edd_decrease_customer_purchase_count_on_refund', true, $this);
         if (true === $maybe_decrease_purchase_count) {
             $customer->decrease_purchase_count();
         }
     }
     // Remove related sale log entries
     $edd_logs->delete_logs(null, 'sale', array(array('key' => '_edd_log_payment_id', 'value' => $this->ID)));
     // Clear the This Month earnings (this_monththis_month is NOT a typo)
     delete_transient(md5('edd_earnings_this_monththis_month'));
     do_action('edd_post_refund_payment', $this);
 }
/**
 * Delete Purchase
 *
 * @param int $payment_id
 * @access      private
 * @since       1.0
 * @return      void
 */
function edd_delete_purchase($payment_id = 0)
{
    global $edd_logs;
    $downloads = edd_get_payment_meta_downloads($payment_id);
    if (is_array($downloads)) {
        // update sale counts and earnings for all purchased products
        foreach ($downloads as $download) {
            edd_undo_purchase($download['id'], $payment_id);
        }
    }
    do_action('edd_payment_delete', $payment_id);
    // remove the payment
    wp_delete_post($payment_id, true);
    // remove related sale log entries
    $edd_logs->delete_logs(null, 'sale', array(array('key' => '_edd_log_payment_id', 'value' => $payment_id)));
    do_action('edd_payment_deleted', $payment_id);
}
Beispiel #8
0
/**
 * Process PayPal IPN
 *
 * @access      private
 * @since       1.0 
 * @return      void
*/
function edd_process_paypal_ipn()
{
    global $edd_options;
    // instantiate the IpnListener class
    if (!class_exists('IpnListener')) {
        include_once EDD_PLUGIN_DIR . 'includes/gateways/libraries/paypal/ipnlistener.php';
    }
    $listener = new IpnListener();
    if (edd_is_test_mode()) {
        $listener->use_sandbox = true;
    }
    if (isset($edd_options['ssl'])) {
        $listener->use_ssl = false;
    }
    // to post using the fsockopen() function rather than cURL, use:
    if (isset($edd_options['paypal_disable_curl'])) {
        $listener->use_curl = false;
    }
    try {
        $listener->requirePostMethod();
        $verified = $listener->processIpn();
    } catch (Exception $e) {
        wp_mail(get_bloginfo('admin_email'), 'IPN Error', $e->getMessage());
        exit(0);
    }
    if ($verified) {
        $payment_id = $_POST['custom'];
        $purchase_key = $_POST['item_number'];
        $paypal_amount = $_POST['mc_gross'];
        $payment_status = $_POST['payment_status'];
        $currency_code = strtolower($_POST['mc_currency']);
        // retrieve the meta info for this payment
        $payment_meta = get_post_meta($payment_id, '_edd_payment_meta', true);
        $payment_amount = edd_format_amount($payment_meta['amount']);
        if ($currency_code != strtolower($edd_options['currency'])) {
            return;
            // the currency code is invalid
        }
        if ($paypal_amount != $payment_amount) {
            return;
            // the prices don't match
        }
        if ($purchase_key != $payment_meta['key']) {
            return;
            // purchase keys don't match
        }
        if (isset($_POST['txn_type']) && $_POST['txn_type'] == 'web_accept') {
            $status = strtolower($payment_status);
            if ($status == 'completed' || edd_is_test_mode()) {
                // set the payment to complete. This also sends the emails
                edd_update_payment_status($payment_id, 'publish');
            } else {
                if ($status == 'refunded') {
                    // this refund process doesn't work yet
                    $payment_data = get_post_meta($payment_id, '_edd_payment_meta', true);
                    $downloads = maybe_unserialize($payment_data['downloads']);
                    if (is_array($downloads)) {
                        foreach ($downloads as $download) {
                            edd_undo_purchase($download['id'], $payment_id);
                        }
                    }
                    wp_update_post(array('ID' => $payment_id, 'post_status' => 'refunded'));
                }
            }
        }
    } else {
        wp_mail(get_bloginfo('admin_email'), __('Invalid IPN', 'edd'), $listener->getTextReport());
    }
}