This class is for working with payments in Give.
Since: 1.5
Beispiel #1
0
/**
 * Check for Variation Prices HTML  (Multi-level donation forms)
 *
 * @since  1.6
 *
 * @return void
 */
function give_check_for_form_price_variations_html()
{
    if (!current_user_can('edit_give_payments', get_current_user_id())) {
        wp_die();
    }
    $form_id = intval($_POST['form_id']);
    $payment_id = intval($_POST['payment_id']);
    $form = get_post($form_id);
    if ('give_forms' != $form->post_type) {
        wp_die();
    }
    if (!give_has_variable_prices($form_id)) {
        esc_html_e('n/a', 'give');
    } else {
        // Payment object.
        $payment = new Give_Payment($payment_id);
        // Payment meta.
        $payment_meta = $payment->get_meta();
        // Variable price dropdown options.
        $variable_price_dropdown_option = array('id' => $form_id, 'name' => 'give-variable-price', 'chosen' => true, 'show_option_all' => '', 'selected' => $payment_meta['price_id']);
        // Render variable prices select tag html.
        give_get_form_variable_price_dropdown($variable_price_dropdown_option, true);
    }
    give_die();
}
 /**
  * Test Pending without Affecting Stats
  */
 public function test_pending_without_affecting_stats()
 {
     add_filter('give_decrease_earnings_on_undo', '__return_false');
     add_filter('give_decrease_sales_on_undo', '__return_false');
     add_filter('give_decrease_customer_value_on_pending', '__return_false');
     add_filter('give_decrease_customer_purchase_count_on_pending', '__return_false');
     add_filter('give_decrease_store_earnings_on_pending', '__return_false');
     $payment = new Give_Payment($this->_payment_id);
     $payment->status = 'complete';
     $payment->save();
     $customer = new Give_Customer($payment->customer_id);
     $form = new Give_Donate_Form($payment->form_id);
     $customer_sales = $customer->purchase_count;
     $customer_earnings = $customer->purchase_value;
     $form_sales = $form->sales;
     $form_earnings = $form->earnings;
     $site_earnings = give_get_total_earnings();
     $site_sales = give_get_total_sales();
     $payment->status = 'pending';
     $payment->save();
     wp_cache_flush();
     $payment = new Give_Payment($this->_payment_id);
     $this->assertEmpty($payment->completed_date);
     $customer = new Give_Customer($payment->customer_id);
     $form = new Give_Donate_Form($payment->form_id);
     $this->assertEquals($customer_earnings, $customer->purchase_value);
     $this->assertEquals($customer_sales, $customer->purchase_count);
     $this->assertEquals($form_earnings, $form->earnings);
     $this->assertEquals($form_sales, $form->sales);
     $this->assertEquals($site_earnings, give_get_total_earnings());
     // Store sales are based off 'publish' & 'revoked' status. So it reduces this count
     $this->assertEquals($site_sales - 1, give_get_total_sales());
     remove_filter('give_decrease_earnings_on_undo', '__return_false');
     remove_filter('give_decrease_sales_on_undo', '__return_false');
     remove_filter('give_decrease_customer_value_on_pending', '__return_false');
     remove_filter('give_decrease_customer_purchase_count_on_pending', '__return_false');
     remove_filter('give_decrease_store_earnings_on_pending', '__return_false ');
 }
Beispiel #3
0
/**
 * Retrieves arbitrary fees for the payment
 *
 * @since 1.5
 *
 * @param int    $payment_id Payment ID
 * @param string $type       Fee type
 *
 * @return mixed array if payment fees found, false otherwise
 */
function give_get_payment_fees($payment_id = 0, $type = 'all')
{
    $payment = new Give_Payment($payment_id);
    return $payment->get_fees($type);
}
Beispiel #4
0
/**
 * Update Old Payments Totals
 *
 * Updates all old payments, prior to 1.2, with new meta for the total purchase amount.
 *
 * It's done to query payments by their totals.
 *
 * @since  1.0
 *
 * @param  array $data Arguments passed.
 *
 * @return void
 */
function give_update_old_payments_with_totals($data)
{
    if (!wp_verify_nonce($data['_wpnonce'], 'give_upgrade_payments_nonce')) {
        return;
    }
    if (get_option('give_payment_totals_upgraded')) {
        return;
    }
    $payments = give_get_payments(array('offset' => 0, 'number' => -1, 'mode' => 'all'));
    if ($payments) {
        foreach ($payments as $payment) {
            $payment = new Give_Payment($payment->ID);
            $meta = $payment->get_meta();
            $payment->total = $meta['amount'];
            $payment->save();
        }
    }
    add_option('give_payment_totals_upgraded', 1);
}
 /**
  * Create Simple Donation w/ Fee
  *
  * @return bool|int
  */
 public static function create_simple_payment_with_fee()
 {
     global $give_options;
     // Enable a few options
     $give_options['sequential_prefix'] = 'GIVE-';
     $simple_form = Give_Helper_Form::create_simple_form();
     // Generate some sales
     $user = get_userdata(1);
     $user_info = array('id' => $user->ID, 'email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     $donation_details = array(array('id' => $simple_form->ID, 'options' => array('price_id' => 0), 'quantity' => 2));
     $total = 0;
     $simple_price = get_post_meta($simple_form->ID, 'give_price', true);
     $total += $simple_price;
     $payment_details = array(array('name' => 'Test Donation', 'id' => $simple_form->ID, 'options' => array('price_id' => 1), 'price' => $simple_price * 2, 'item_price' => $simple_price, 'quantity' => 2));
     $purchase_data = array('price' => number_format((double) $total, 2), 'date' => date('Y-m-d H:i:s', strtotime('-1 day')), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user_info['email'], 'user_info' => $user_info, 'currency' => 'USD', 'donations' => $donation_details, 'payment_details' => $payment_details, 'status' => 'pending');
     $fee_args = array('label' => 'Test Fee', 'type' => 'test', 'amount' => 5);
     //@TODO: Incorporate Fees
     //Give()->fees->add_fee( $fee_args );
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     $_SERVER['SERVER_NAME'] = 'give_virtual';
     $payment_id = give_insert_payment($purchase_data);
     $transaction_id = 'FIR3SID3';
     $payment = new Give_Payment($payment_id);
     $payment->transaction_id = $transaction_id;
     $payment->save();
     give_insert_payment_note($payment_id, sprintf(esc_html__('PayPal Transaction ID: %s', 'give'), $transaction_id));
     return $payment_id;
 }
 /**
  * Pre Fetch Data
  *
  * @access public
  * @since 1.5
  */
 public function pre_fetch()
 {
     global $give_logs, $wpdb;
     if ($this->step == 1) {
         $this->delete_data('give_temp_recount_all_total');
         $this->delete_data('give_temp_recount_all_stats');
         $this->delete_data('give_temp_payment_items');
         $this->delete_data('give_temp_processed_payments');
     }
     $accepted_statuses = apply_filters('give_recount_accepted_statuses', array('publish'));
     $total = $this->get_stored_data('give_temp_recount_all_total');
     if (false === $total) {
         $total = 0;
         $payment_items = $this->get_stored_data('give_temp_payment_items');
         if (false === $payment_items) {
             $payment_items = array();
             $this->store_data('give_temp_payment_items', $payment_items);
         }
         $all_forms = $this->get_stored_data('give_temp_form_ids');
         if (false === $all_forms) {
             $args = array('post_status' => 'any', 'post_type' => 'give_forms', 'posts_per_page' => -1, 'fields' => 'ids');
             $all_forms = get_posts($args);
             $this->store_data('give_temp_form_ids', $all_forms);
         }
         $args = apply_filters('give_recount_form_stats_total_args', array('post_parent__in' => $all_forms, 'post_type' => 'give_log', 'post_status' => 'publish', 'log_type' => 'sale', 'fields' => 'ids', 'nopaging' => true));
         $all_logs = $give_logs->get_connected_logs($args, 'sale');
         if ($all_logs) {
             $log_ids = implode(',', $all_logs);
             $payment_ids = $wpdb->get_col("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key='_give_log_payment_id' AND post_id IN ({$log_ids})");
             unset($log_ids);
             $payment_ids = implode(',', $payment_ids);
             $payments = $wpdb->get_results("SELECT ID, post_status FROM {$wpdb->posts} WHERE ID IN (" . $payment_ids . ")");
             unset($payment_ids);
             foreach ($payments as $payment) {
                 $payment = new Give_Payment($payment->ID);
                 $form_id = $payment->form_id;
                 //If for some reason somehow the form_ID isn't set check payment meta
                 if (empty($payment->form_id)) {
                     $payment_meta = $payment->get_meta();
                     $form_id = isset($payment_meta['form_id']) ? $payment_meta['form_id'] : 0;
                 }
                 if (!in_array($payment->post_status, $accepted_statuses)) {
                     continue;
                 }
                 if (!array_key_exists($payment->ID, $payment_items)) {
                     $payment_items[$payment->ID] = array('id' => $form_id, 'payment_id' => $payment->ID, 'price' => $payment->total);
                 }
             }
             $total = count($all_logs);
         }
         $this->store_data('give_temp_payment_items', $payment_items);
         $this->store_data('give_recount_all_total', $total);
     }
 }
Beispiel #7
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;
}
Beispiel #8
0
 /**
  * Retrieves Recent Donations
  *
  * @access public
  * @since  1.1
  * @return array
  */
 public function get_recent_donations()
 {
     global $wp_query;
     $sales = array();
     if (!user_can($this->user_id, 'view_give_reports') && !$this->override) {
         return $sales;
     }
     if (isset($wp_query->query_vars['id'])) {
         $query = array();
         $query[] = new Give_Payment($wp_query->query_vars['id']);
     } elseif (isset($wp_query->query_vars['purchasekey'])) {
         $query = array();
         $query[] = give_get_payment_by('key', $wp_query->query_vars['purchasekey']);
     } elseif (isset($wp_query->query_vars['email'])) {
         $args = array('fields' => 'ids', 'meta_key' => '_give_payment_user_email', 'meta_value' => $wp_query->query_vars['email'], 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish');
         $query = give_get_payments($args);
     } else {
         $args = array('fields' => 'ids', 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish');
         $query = give_get_payments($args);
     }
     if ($query) {
         $i = 0;
         foreach ($query as $payment) {
             if (is_numeric($payment)) {
                 $payment = new Give_Payment($payment);
                 $payment_meta = $payment->get_meta();
                 $user_info = $payment->user_info;
             } else {
                 continue;
             }
             $payment_meta = $payment->get_meta();
             $user_info = $payment->user_info;
             $first_name = isset($user_info['first_name']) ? $user_info['first_name'] : '';
             $last_name = isset($user_info['last_name']) ? $user_info['last_name'] : '';
             $sales['donations'][$i]['ID'] = $payment->number;
             $sales['donations'][$i]['transaction_id'] = $payment->transaction_id;
             $sales['donations'][$i]['key'] = $payment->key;
             $sales['donations'][$i]['total'] = $payment->total;
             $sales['donations'][$i]['gateway'] = $payment->gateway;
             $sales['donations'][$i]['name'] = $first_name . ' ' . $last_name;
             $sales['donations'][$i]['fname'] = $first_name;
             $sales['donations'][$i]['lname'] = $last_name;
             $sales['donations'][$i]['email'] = $payment->email;
             $sales['donations'][$i]['date'] = $payment->date;
             $form_id = isset($payment_meta['form_id']) ? $payment_meta['form_id'] : $payment_meta;
             $price = isset($payment_meta['form_id']) ? give_get_form_price($payment_meta['form_id']) : false;
             $price_id = isset($payment_meta['price_id']) ? $payment_meta['price_id'] : null;
             $sales['donations'][$i]['form']['id'] = $form_id;
             $sales['donations'][$i]['form']['name'] = get_the_title($payment_meta['form_id']);
             $sales['donations'][$i]['form']['price'] = $price;
             if (give_has_variable_prices($form_id)) {
                 if (isset($payment_meta['price_id'])) {
                     $price_name = give_get_price_option_name($form_id, $payment_meta['price_id'], $payment->ID);
                     $sales['donations'][$i]['form']['price_name'] = $price_name;
                     $sales['donations'][$i]['form']['price_id'] = $price_id;
                     $sales['donations'][$i]['form']['price'] = give_get_price_option_amount($form_id, $price_id);
                 }
             }
             //Add custom meta to API
             foreach ($payment_meta as $meta_key => $meta_value) {
                 $exceptions = array('form_title', 'form_id', 'price_id', 'user_info', 'key', 'email', 'date');
                 //Don't clutter up results with dupes
                 if (in_array($meta_key, $exceptions)) {
                     continue;
                 }
                 $sales['donations'][$i]['payment_meta'][$meta_key] = $meta_value;
             }
             $i++;
         }
     }
     return apply_filters('give_api_donations_endpoint', $sales);
 }
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
/**
 * View Order Details Page
 *
 * @since 1.0
 * @return void
 */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    wp_die(esc_html__('Donation ID not supplied. Please try again.', 'give'), esc_html__('Error', 'give'), array('response' => 400));
}
// Setup the variables
$payment_id = absint($_GET['id']);
$payment = new Give_Payment($payment_id);
// Sanity check... fail if purchase ID is invalid
$payment_exists = $payment->ID;
if (empty($payment_exists)) {
    wp_die(esc_html__('The specified ID does not belong to a payment. Please try again.', 'give'), esc_html__('Error', 'give'), array('response' => 400));
}
$number = $payment->number;
$payment_meta = $payment->get_meta();
$transaction_id = esc_attr($payment->transaction_id);
$user_id = $payment->user_id;
$customer_id = $payment->customer_id;
$payment_date = strtotime($payment->date);
$user_info = give_get_payment_meta_user_info($payment_id);
$address = $payment->address;
$gateway = $payment->gateway;
$currency_code = $payment->currency;