public function setUp() { parent::setUp(); give_set_error('invalid_email', 'Please enter a valid email address.'); give_set_error('invalid_user', 'The user information is invalid.'); give_set_error('username_incorrect', 'The username you entered does not exist.'); give_set_error('password_incorrect', 'The password you entered is incorrect.'); }
/** * Sets an error on checkout if no gateways are enabled * * @since 1.0 * @return void */ function give_no_gateway_error() { $gateways = give_get_enabled_payment_gateways(); if (empty($gateways)) { give_set_error('no_gateways', __('You must enable a payment gateway to use Give', 'give')); } else { give_unset_error('no_gateways'); } }
/** * Validate ReCAPTCHA * * @param $valid_data * @param $data */ function give_validate_recaptcha($valid_data, $data) { $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'; $recaptcha_secret_key = 'MY SITE KEY HERE'; // <----- UPDATE WITH YOUR SITE KEY $recaptcha_response = wp_remote_post($recaptcha_url . "?secret=" . $recaptcha_secret_key . "&response=" . $data['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']); $recaptcha_data = wp_remote_retrieve_body($recaptcha_response); if (isset($recaptcha_data->success) && $recaptcha_data->success == true) { //User must have validated the reCAPTCHA to proceed with donation if (!isset($data['g-recaptcha-response']) || empty($data['g-recaptcha-response'])) { give_set_error('g-recaptcha-response', __('Please verify that you are not a robot.', 'give')); } } }
/** * Validate Custom Field * * @description check for errors without custom fields * * @param $valid_data * @param $data */ function give_donations_validate_custom_fields($valid_data, $data) { // Only validate the form with the IDs "754" and "586"; // Remove "If" statement to display on all forms // For a single form, use this instead: // if ( $form_id == 754) { $forms = array(754, 586); if (in_array($data['give-form-id'], $forms)) { return; } //Check for message data if (empty($data['give_message'])) { give_set_error('give_message', __('Please tell us what you would like inscribed on the plaque.', 'give')); } }
/** * Validation donation amount. Note: Give handles validation minimum amount out-of-the-box. * * Check that a donation is above or below a maximum amount. * * @param $valid_data * @param $data */ function give_donations_validate_donation_amount($valid_data, $data) { // Only validate the form with the IDs "754" and "586"; // Remove "If" statement to validation for all forms // For a single form, use this instead: // $forms = array( 1425 ); // if ( ! in_array( $data['give-form-id'], $forms ) ) { // return; // } $sanitized_amount = (int) give_sanitize_amount($data['give-amount']); $max_amount = 1000; //Check for message data if ($sanitized_amount >= $max_amount) { give_set_error('give_message', sprintf(__('Sorry, we can\'t accept donations more than %s.', 'give'), give_currency_filter(give_format_amount($max_amount)))); } }
/** * Process Profile Updater Form. * * Processes the profile updater form by updating the necessary fields. * * @since 1.0 * * @param array $data Data sent from the profile editor. * * @return bool */ function give_process_profile_editor_updates($data) { // Profile field change request if (empty($_POST['give_profile_editor_submit']) && !is_user_logged_in()) { return false; } // Nonce security if (!wp_verify_nonce($data['give_profile_editor_nonce'], 'give-profile-editor-nonce')) { return false; } $user_id = get_current_user_id(); $old_user_data = get_userdata($user_id); $display_name = isset($data['give_display_name']) ? sanitize_text_field($data['give_display_name']) : $old_user_data->display_name; $first_name = isset($data['give_first_name']) ? sanitize_text_field($data['give_first_name']) : $old_user_data->first_name; $last_name = isset($data['give_last_name']) ? sanitize_text_field($data['give_last_name']) : $old_user_data->last_name; $email = isset($data['give_email']) ? sanitize_email($data['give_email']) : $old_user_data->user_email; $line1 = isset($data['give_address_line1']) ? sanitize_text_field($data['give_address_line1']) : ''; $line2 = isset($data['give_address_line2']) ? sanitize_text_field($data['give_address_line2']) : ''; $city = isset($data['give_address_city']) ? sanitize_text_field($data['give_address_city']) : ''; $state = isset($data['give_address_state']) ? sanitize_text_field($data['give_address_state']) : ''; $zip = isset($data['give_address_zip']) ? sanitize_text_field($data['give_address_zip']) : ''; $country = isset($data['give_address_country']) ? sanitize_text_field($data['give_address_country']) : ''; $userdata = array('ID' => $user_id, 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => $display_name, 'user_email' => $email); $address = array('line1' => $line1, 'line2' => $line2, 'city' => $city, 'state' => $state, 'zip' => $zip, 'country' => $country); /** * Fires before updating user profile. * * @since 1.0 * * @param int $user_id The ID of the user. * @param array $userdata User info, including ID, first name, last name, display name and email. */ do_action('give_pre_update_user_profile', $user_id, $userdata); // New password if (!empty($data['give_new_user_pass1'])) { if ($data['give_new_user_pass1'] !== $data['give_new_user_pass2']) { give_set_error('password_mismatch', esc_html__('The passwords you entered do not match. Please try again.', 'give')); } else { $userdata['user_pass'] = $data['give_new_user_pass1']; } } if (empty($email)) { // Make sure email should not be empty. give_set_error('email_empty', esc_html__('The email you entered is empty.', 'give')); } else { if (!is_email($email)) { // Make sure email should be valid. give_set_error('email_not_valid', esc_html__('The email you entered is not valid. Please use another', 'give')); } else { if ($email != $old_user_data->user_email) { // Make sure the new email doesn't belong to another user if (email_exists($email)) { give_set_error('email_exists', esc_html__('The email you entered belongs to another user. Please use another.', 'give')); } } } } // Check for errors $errors = give_get_errors(); if ($errors) { // Send back to the profile editor if there are errors wp_redirect($data['give_redirect']); give_die(); } // Update the user $meta = update_user_meta($user_id, '_give_user_address', $address); $updated = wp_update_user($userdata); if ($updated) { /** * Fires after updating user profile. * * @since 1.0 * * @param int $user_id The ID of the user. * @param array $userdata User info, including ID, first name, last name, display name and email. */ do_action('give_user_profile_updated', $user_id, $userdata); wp_redirect(add_query_arg('updated', 'true', $data['give_redirect'])); give_die(); } return false; }
give_set_error('give_recaptcha_connection_issue', apply_filters('give_recaptcha_connection_issue_message', __('Unable to connect to reCAPTCHA server', 'give'))); } } else { give_set_error('give_recaptcha_failed', apply_filters('give_recaptcha_failed_message', __('Sorry, it looks like the reCAPTCHA test has failed', 'give'))); } } //If no errors or only expired token key error - then send email if (!give_get_errors()) { $customer = Give()->customers->get_customer_by('email', $email); if (isset($customer->id)) { if (Give()->email_access->can_send_email($customer->id)) { Give()->email_access->send_email($customer->id, $email); $show_form = false; } } else { give_set_error('give_no_donor_email_exists', apply_filters('give_no_donor_email_exists_message', __('Sorry, it looks like that donor email address does not exist', 'give'))); } } } //Print any messages & errors give_print_errors(0); //Show the email login form? if ($show_form) { ?> <div class="give-form"> <?php if (!give_get_errors()) { give_output_error(__('Please enter the email address you used for your donation. A verification email containing an access link will be sent to you.', 'give'), true); }
/** * Validates the credit card info * * @access private * @since 1.0 * @return array */ function give_purchase_form_validate_cc() { $card_data = give_get_purchase_cc_info(); // Validate the card zip if (!empty($card_data['card_zip'])) { if (!give_purchase_form_validate_cc_zip($card_data['card_zip'], $card_data['card_country'])) { give_set_error('invalid_cc_zip', esc_html__('The zip / postal code you entered for your billing address is invalid.', 'give')); } } //Ensure no spaces if (!empty($card_data['card_number'])) { $card_data['card_number'] = str_replace('+', '', $card_data['card_number']); //no "+" signs $card_data['card_number'] = str_replace(' ', '', $card_data['card_number']); // No spaces } // This should validate card numbers at some point too return $card_data; }
/** * Check the purchase to ensure a banned email is not allowed through * * @since 1.0 * @return void */ function give_check_purchase_email($valid_data, $posted) { $is_banned = false; $banned = give_get_banned_emails(); if (empty($banned)) { return; } if (is_user_logged_in()) { // The user is logged in, check that their account email is not banned $user_data = get_userdata(get_current_user_id()); if (give_is_email_banned($user_data->user_email)) { $is_banned = true; } if (give_is_email_banned($posted['give_email'])) { $is_banned = true; } } elseif (isset($posted['give-purchase-var']) && $posted['give-purchase-var'] == 'needs-to-login') { // The user is logging in, check that their email is not banned $user_data = get_user_by('login', $posted['give_user_login']); if ($user_data && give_is_email_banned($user_data->user_email)) { $is_banned = true; } } else { // Guest purchase, check that the email is not banned if (give_is_email_banned($posted['give_email'])) { $is_banned = true; } } if ($is_banned) { // Set an error and give the donor a general error (don't alert them that they were banned) give_set_error('email_banned', __('An internal error has occurred, please try again or contact support.', 'give')); } }
/** * Is this a valid token? * * @since 1.0 * @access public * * @param $token The token. * * @return bool */ public function is_valid_token($token) { global $wpdb; // Make sure token isn't expired $expires = date('Y-m-d H:i:s', time() - $this->token_expiration); $email = $wpdb->get_var($wpdb->prepare("SELECT email FROM {$wpdb->prefix}give_customers WHERE token = %s AND verify_throttle >= %s LIMIT 1", $token, $expires)); if (!empty($email)) { $this->token_email = $email; $this->token = $token; return true; } //Set error only if email access form isn't being submitted if (!isset($_POST['give_email']) && !isset($_POST['_wpnonce'])) { give_set_error('give_email_token_expired', apply_filters('give_email_token_expired_message', 'Sorry, your access token has expired. Please request a new one below:', 'give')); } return false; }
/** * Process Register Form * * @since 2.0 * * @param array $data Data sent from the register form * * @return void */ function give_process_register_form($data) { if (is_user_logged_in()) { return; } if (empty($_POST['give_register_submit'])) { return; } do_action('give_pre_process_register_form'); if (empty($data['give_user_login'])) { give_set_error('empty_username', __('Invalid username', 'give')); } if (username_exists($data['give_user_login'])) { give_set_error('username_unavailable', __('Username already taken', 'give')); } if (!validate_username($data['give_user_login'])) { give_set_error('username_invalid', __('Invalid username', 'give')); } if (email_exists($data['give_user_email'])) { give_set_error('email_unavailable', __('Email address already taken', 'give')); } if (empty($data['give_user_email']) || !is_email($data['give_user_email'])) { give_set_error('email_invalid', __('Invalid email', 'give')); } if (!empty($data['give_payment_email']) && $data['give_payment_email'] != $data['give_user_email'] && !is_email($data['give_payment_email'])) { give_set_error('payment_email_invalid', __('Invalid payment email', 'give')); } if (empty($_POST['give_user_pass'])) { give_set_error('empty_password', __('Please enter a password', 'give')); } if (!empty($_POST['give_user_pass']) && empty($_POST['give_user_pass2']) || $_POST['give_user_pass'] !== $_POST['give_user_pass2']) { give_set_error('password_mismatch', __('Passwords do not match', 'give')); } do_action('give_process_register_form'); // Check for errors and redirect if none present $errors = give_get_errors(); if (empty($errors)) { $redirect = apply_filters('give_register_redirect', $data['give_redirect']); give_register_and_login_new_user(array('user_login' => $data['give_user_login'], 'user_pass' => $data['give_user_pass'], 'user_email' => $data['give_user_email'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role'))); wp_redirect($redirect); give_die(); } }
/** * Force a different minimum donation amount and show an error if not met */ function check_donation_amount($valid_data, $posted) { if ($posted['give-amount'] < 5) { give_set_error('donation_amount', 'The donation amount must be $5 or more. Please go back and select a specified amount or enter a larger donation amount.', 'give'); } }
/** * * 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; }
/** * Authorize.net Payments * * @param $purchase_data */ public function give_process_authorize_net_payment($purchase_data) { if (!isset($_POST['card_number']) || $_POST['card_number'] == '') { give_set_error('empty_card', __('You must enter a card number', 'give')); } if (!isset($_POST['card_name']) || $_POST['card_name'] == '') { give_set_error('empty_card_name', __('You must enter the name on your card', 'give')); } if (!isset($_POST['card_exp_month']) || $_POST['card_exp_month'] == '') { give_set_error('empty_month', __('You must enter an expiration month', 'give')); } if (!isset($_POST['card_exp_year']) || $_POST['card_exp_year'] == '') { give_set_error('empty_year', __('You must enter an expiration year', 'give')); } if (!isset($_POST['card_cvc']) || $_POST['card_cvc'] == '' || strlen($_POST['card_cvc']) < 3) { give_set_error('empty_cvc', __('You must enter a valid CVC', 'give')); } $errors = give_get_errors(); //No errors: Continue with payment processing if (!$errors) { //Include Authorize SDK require_once GIVE_AUTHORIZE_PLUGIN_DIR . '/includes/anet_php_sdk/AuthorizeNet.php'; if (!give_is_test_mode()) { //LIVE: $authorize_api_login = give_get_option('give_api_login'); $authorize_trans_key = give_get_option('give_transaction_key'); } else { //SANDBOX $authorize_api_login = give_get_option('give_authorize_sandbox_api_login'); $authorize_trans_key = give_get_option('give_authorize_sandbox_transaction_key'); } //Check for credentials entered if (empty($authorize_api_login) || empty($authorize_trans_key)) { give_set_error('error_id_here', __('Error: Missing API Login or Transaction key. Please enter them in the plugin settings.', 'give-authorize')); return; } //Proceed with Authorize AIM $transaction = new AuthorizeNetAIM($authorize_api_login, $authorize_trans_key); $transaction->VERIFY_PEER = false; //Sandbox or not? if (give_is_test_mode()) { $transaction->setSandbox(true); } else { $transaction->setSandbox(false); } $card_info = $purchase_data['card_info']; $card_names = explode(' ', $card_info['card_name']); $first_name = isset($card_names[0]) ? $card_names[0] : $purchase_data['user_info']['first_name']; if (!empty($card_names[1])) { unset($card_names[0]); $last_name = implode(' ', $card_names); } else { $last_name = $purchase_data['user_info']['last_name']; } $transaction->amount = $purchase_data['price']; $transaction->card_num = strip_tags(trim($card_info['card_number'])); $transaction->card_code = strip_tags(trim($card_info['card_cvc'])); $transaction->exp_date = strip_tags(trim($card_info['card_exp_month'])) . '/' . strip_tags(trim($card_info['card_exp_year'])); $transaction->description = give_get_purchase_summary($purchase_data); $transaction->first_name = $first_name; $transaction->last_name = $last_name; $transaction->address = $card_info['card_address'] . ' ' . $card_info['card_address_2']; $transaction->city = $card_info['card_city']; $transaction->country = $card_info['card_country']; $transaction->state = $card_info['card_state']; $transaction->zip = $card_info['card_zip']; $transaction->customer_ip = give_get_ip(); $transaction->email = $purchase_data['user_email']; $transaction->invoice_num = $purchase_data['purchase_key']; try { $response = $transaction->authorizeAndCapture(); if ($response->approved) { $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => intval($purchase_data['post_data']['give-form-id']), 'price_id' => isset($purchase_data['post_data']['give-price-id']) ? intval($purchase_data['post_data']['give-price-id']) : '', 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => 'authorizenet'); $payment = give_insert_payment($payment_data); if ($payment) { give_update_payment_status($payment, 'publish'); give_send_to_success_page(); } else { give_set_error('authorize_error', __('Error: your payment could not be recorded. Please try again', 'give')); give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']); } } else { if (isset($response->response_reason_text)) { $error = $response->response_reason_text; } elseif (isset($response->error_message)) { $error = $response->error_message; } else { $error = ''; } if (strpos(strtolower($error), 'the credit card number is invalid') !== false) { give_set_error('invalid_card', __('Your card number is invalid', 'give')); } elseif (strpos(strtolower($error), 'this transaction has been declined') !== false) { give_set_error('invalid_card', __('Your card has been declined', 'give')); } elseif (isset($response->response_reason_text)) { give_set_error('api_error', $response->response_reason_text); } elseif (isset($response->error_message)) { give_set_error('api_error', $response->error_message); } else { give_set_error('api_error', sprintf(__('An error occurred. Error data: %s', 'give'), print_r($response, true))); } give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']); } } catch (AuthorizeNetException $e) { give_set_error('request_error', $e->getMessage()); give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']); } } else { give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']); } }
/** * Disconnect a user ID from a donor * * @since 1.0 * * @param array $args Array of arguements * * @return bool If the disconnect was sucessful */ function give_disconnect_customer_user_id($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 edit this donor.', 'give')); } if (empty($args)) { return; } $customer_id = (int) $args['customer_id']; $nonce = $args['_wpnonce']; if (!wp_verify_nonce($nonce, 'edit-customer')) { wp_die(__('Cheatin\' eh?!', 'give')); } $customer = new Give_Customer($customer_id); if (empty($customer->id)) { return false; } do_action('give_pre_customer_disconnect_user_id', $customer_id, $customer->user_id); $customer_args = array('user_id' => 0); if ($customer->update($customer_args)) { global $wpdb; if (!empty($customer->payment_ids)) { $wpdb->query("UPDATE {$wpdb->postmeta} SET meta_value = 0 WHERE meta_key = '_give_payment_user_id' AND post_id IN ( {$customer->payment_ids} )"); } $output['success'] = true; } else { $output['success'] = false; give_set_error('give-disconnect-user-fail', __('Failed to disconnect user from donor', 'give')); } do_action('give_post_customer_disconnect_user_id', $customer_id); if (defined('DOING_AJAX') && DOING_AJAX) { header('Content-Type: application/json'); echo json_encode($output); wp_die(); } return $output; }
/** * Renders the customer view wrapper * * @since 1.0 * * @param string $view The View being requested * @param array $callbacks The Registered views and their callback functions * * @return void */ function give_render_customer_view($view, $callbacks) { $render = true; $customer_view_role = apply_filters('give_view_customers_role', 'view_give_reports'); if (!current_user_can($customer_view_role)) { give_set_error('give-no-access', __('You are not permitted to view this data.', 'give')); $render = false; } if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { give_set_error('give-invalid_customer', __('Invalid Donor ID Provided.', 'give')); $render = false; } $customer_id = (int) $_GET['id']; $customer = new Give_Customer($customer_id); if (empty($customer->id)) { give_set_error('give-invalid_customer', __('Invalid Donor ID Provided.', 'give')); $render = false; } $customer_tabs = give_customer_tabs(); ?> <div class='wrap'> <?php if (give_get_errors()) { ?> <div class="error settings-error"> <?php give_print_errors(0); ?> </div> <?php } ?> <?php if ($customer && $render) { ?> <div id="customer-tab-wrapper"> <ul id="customer-tab-wrapper-list" class="nav-tab-wrapper"> <?php foreach ($customer_tabs as $key => $tab) { ?> <?php $active = $key === $view ? true : false; ?> <?php $class = $active ? 'active' : 'inactive'; ?> <li class="<?php echo sanitize_html_class($class); ?> "> <?php if (!$active) { ?> <a title="<?php echo esc_attr($tab['title']); ?> " aria-label="<?php echo esc_attr($tab['title']); ?> " href="<?php echo esc_url(admin_url('edit.php?post_type=give_forms&page=give-donors&view=' . $key . '&id=' . $customer->id)); ?> "> <?php } ?> <span class="dashicons <?php echo sanitize_html_class($tab['dashicon']); ?> "></span> <?php echo esc_attr($tab['title']); ?> <?php if (!$active) { ?> </a> <?php } ?> </li> <?php } ?> </ul> </div> <div id="give-customer-card-wrapper"> <?php $callbacks[$view]($customer); ?> </div> <?php } ?> </div> <?php }
/** * * 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; }