/** * Delete a customer * * @since 1.0 * * @param array $args The $_POST array being passeed * * @return int Wether it was a successful deletion */ function give_customer_delete($args) { $customer_edit_role = apply_filters('give_edit_customers_role', 'edit_give_payments'); if (!is_admin() || !current_user_can($customer_edit_role)) { wp_die(__('You do not have permission to delete this donor.', 'give')); } if (empty($args)) { return; } $customer_id = (int) $args['customer_id']; $confirm = !empty($args['give-customer-delete-confirm']) ? true : false; $remove_data = !empty($args['give-customer-delete-records']) ? true : false; $nonce = $args['_wpnonce']; if (!wp_verify_nonce($nonce, 'delete-customer')) { wp_die(__('Cheatin\' eh?!', 'give')); } if (!$confirm) { give_set_error('customer-delete-no-confirm', __('Please confirm you want to delete this donor', 'give')); } if (give_get_errors()) { wp_redirect(admin_url('edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $customer_id)); exit; } $customer = new Give_Customer($customer_id); do_action('give_pre_delete_customer', $customer_id, $confirm, $remove_data); $success = false; if ($customer->id > 0) { $payments_array = explode(',', $customer->payment_ids); $success = Give()->customers->delete($customer->id); if ($success) { if ($remove_data) { // Remove all payments, logs, etc foreach ($payments_array as $payment_id) { give_delete_purchase($payment_id, false, true); } } else { // Just set the payments to customer_id of 0 foreach ($payments_array as $payment_id) { give_update_payment_meta($payment_id, '_give_payment_customer_id', 0); } } $redirect = admin_url('edit.php?post_type=give_forms&page=give-donors&give-message=customer-deleted'); } else { give_set_error('give-donor-delete-failed', __('Error deleting donor', 'give')); $redirect = admin_url('edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $customer_id); } } else { give_set_error('give-customer-delete-invalid-id', __('Invalid Donor ID', 'give')); $redirect = admin_url('edit.php?post_type=give_forms&page=give-donors'); } wp_redirect($redirect); exit; }
/** * Looks up purchases by email that match the registering user * * This is for users that purchased as a guest and then came * back and created an account. * * @access public * @since 1.0 * * @param $user_id INT - the new user's ID * * @return void */ function give_add_past_purchases_to_new_user($user_id) { $email = get_the_author_meta('user_email', $user_id); $payments = give_get_payments(array('s' => $email)); if ($payments) { foreach ($payments as $payment) { if (intval(give_get_payment_user_id($payment->ID)) > 0) { continue; } // This payment already associated with an account $meta = give_get_payment_meta($payment->ID); $meta['user_info'] = maybe_unserialize($meta['user_info']); $meta['user_info']['id'] = $user_id; $meta['user_info'] = $meta['user_info']; // Store the updated user ID in the payment meta give_update_payment_meta($payment->ID, '_give_payment_meta', $meta); give_update_payment_meta($payment->ID, '_give_payment_user_id', $user_id); } } }
/** * Sets a Transaction ID in post meta for the given Payment ID. * * @since 1.0 * * @param int $payment_id Payment ID * @param string $transaction_id The transaction ID from the gateway * * @return bool|mixed */ function give_set_payment_transaction_id($payment_id = 0, $transaction_id = '') { if (empty($payment_id) || empty($transaction_id)) { return false; } $transaction_id = apply_filters('give_set_payment_transaction_id', $transaction_id, $payment_id); return give_update_payment_meta($payment_id, '_give_payment_transaction_id', $transaction_id); }
/** * Updates all old payments, prior to 1.2, with new * meta for the total purchase amount * * This is so that payments can be queried 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) { $meta = give_get_payment_meta($payment->ID); give_update_payment_meta($payment->ID, '_give_payment_total', $meta['amount']); } } add_option('give_payment_totals_upgraded', 1); }
/** * Process web accept (one time) payment IPNs * * @since 1.0 * * @param array $data IPN Data * * @return void */ function give_process_paypal_web_accept_and_cart($data, $payment_id) { if ($data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded') { return; } if (empty($payment_id)) { return; } // Collect payment details $purchase_key = isset($data['invoice']) ? $data['invoice'] : $data['item_number']; $paypal_amount = $data['mc_gross']; $payment_status = strtolower($data['payment_status']); $currency_code = strtolower($data['mc_currency']); $business_email = isset($data['business']) && is_email($data['business']) ? trim($data['business']) : trim($data['receiver_email']); $payment_meta = give_get_payment_meta($payment_id); if (give_get_payment_gateway($payment_id) != 'paypal') { return; // this isn't a PayPal standard IPN } // Verify payment recipient if (strcasecmp($business_email, trim(give_get_option('paypal_email'))) != 0) { give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid business email in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id); give_update_payment_status($payment_id, 'failed'); give_insert_payment_note($payment_id, __('Payment failed due to invalid PayPal business email.', 'give')); return; } // Verify payment currency if ($currency_code != strtolower($payment_meta['currency'])) { give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid currency in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id); give_update_payment_status($payment_id, 'failed'); give_insert_payment_note($payment_id, __('Payment failed due to invalid currency in PayPal IPN.', 'give')); return; } if (!give_get_payment_user_email($payment_id)) { // No email associated with purchase, so store from PayPal give_update_payment_meta($payment_id, '_give_payment_user_email', $data['payer_email']); // Setup and store the donors's details $address = array(); $address['line1'] = !empty($data['address_street']) ? sanitize_text_field($data['address_street']) : false; $address['city'] = !empty($data['address_city']) ? sanitize_text_field($data['address_city']) : false; $address['state'] = !empty($data['address_state']) ? sanitize_text_field($data['address_state']) : false; $address['country'] = !empty($data['address_country_code']) ? sanitize_text_field($data['address_country_code']) : false; $address['zip'] = !empty($data['address_zip']) ? sanitize_text_field($data['address_zip']) : false; $user_info = array('id' => '-1', 'email' => sanitize_text_field($data['payer_email']), 'first_name' => sanitize_text_field($data['first_name']), 'last_name' => sanitize_text_field($data['last_name']), 'discount' => '', 'address' => $address); $payment_meta['user_info'] = $user_info; give_update_payment_meta($payment_id, '_give_payment_meta', $payment_meta); } if ($payment_status == 'refunded' || $payment_status == 'reversed') { // Process a refund give_process_paypal_refund($data, $payment_id); } else { if (get_post_status($payment_id) == 'publish') { return; // Only complete payments once } // Retrieve the total purchase amount (before PayPal) $payment_amount = give_get_payment_amount($payment_id); if (number_format((double) $paypal_amount, 2) < number_format((double) $payment_amount, 2)) { // The prices don't match give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid payment amount in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id); give_update_payment_status($payment_id, 'failed'); give_insert_payment_note($payment_id, __('Payment failed due to invalid amount in PayPal IPN.', 'give')); return; } if ($purchase_key != give_get_payment_key($payment_id)) { // Purchase keys don't match give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid purchase key in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id); give_update_payment_status($payment_id, 'failed'); give_insert_payment_note($payment_id, __('Payment failed due to invalid purchase key in PayPal IPN.', 'give')); return; } if ($payment_status == 'completed' || give_is_test_mode()) { give_insert_payment_note($payment_id, sprintf(__('PayPal Transaction ID: %s', 'give'), $data['txn_id'])); give_set_payment_transaction_id($payment_id, $data['txn_id']); give_update_payment_status($payment_id, 'publish'); } else { if ('pending' == $payment_status && isset($data['pending_reason'])) { // Look for possible pending reasons, such as an echeck $note = ''; switch (strtolower($data['pending_reason'])) { case 'echeck': $note = __('Payment made via eCheck and will clear automatically in 5-8 days', 'give'); break; case 'address': $note = __('Payment requires a confirmed donor address and must be accepted manually through PayPal', 'give'); break; case 'intl': $note = __('Payment must be accepted manually through PayPal due to international account regulations', 'give'); break; case 'multi-currency': $note = __('Payment received in non-shop currency and must be accepted manually through PayPal', 'give'); break; case 'paymentreview': case 'regulatory_review': $note = __('Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'give'); break; case 'unilateral': $note = __('Payment was sent to non-confirmed or non-registered email address.', 'give'); break; case 'upgrade': $note = __('PayPal account must be upgraded before this payment can be accepted', 'give'); break; case 'verify': $note = __('PayPal account is not verified. Verify account in order to accept this payment', 'give'); break; case 'other': $note = __('Payment is pending for unknown reasons. Contact PayPal support for assistance', 'give'); break; } if (!empty($note)) { give_insert_payment_note($payment_id, $note); } } } } }
/** * Updates the email address of a customer record when the email on a user is updated * * @since 1.4.3 * @access public * * @param int $user_id User ID. * @param WP_User $old_user_data User data. * * @return bool */ public function update_customer_email_on_user_update($user_id = 0, $old_user_data) { $customer = new Give_Customer($user_id, true); if (!$customer) { return false; } $user = get_userdata($user_id); if (!empty($user) && $user->user_email !== $customer->email) { if (!$this->get_customer_by('email', $user->user_email)) { $success = $this->update($customer->id, array('email' => $user->user_email)); if ($success) { // Update some payment meta if we need to $payments_array = explode(',', $customer->payment_ids); if (!empty($payments_array)) { foreach ($payments_array as $payment_id) { give_update_payment_meta($payment_id, 'email', $user->user_email); } } do_action('give_update_customer_email_on_user_update', $user, $customer); } } } }
/** * * 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; }