| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Payment Actions |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Admin/Payments |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
use Give\Donations\Models\Donation; |
| 14 |
use Give\Donations\ValueObjects\DonationMetaKeys; |
| 15 |
|
| 16 |
if (!defined('ABSPATH')) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* |
| 22 |
* Process the payment details edit |
| 23 |
* |
| 24 |
* @since 2.27.0 Change to save comment to donations meta table |
| 25 |
* @since 1.0 |
| 26 |
* |
| 27 |
* @access private |
| 28 |
* |
| 29 |
* @param array $data Donation data. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
function give_update_payment_details( $data ) { |
| 34 |
|
| 35 |
if ( ! current_user_can( 'edit_give_payments', $data['give_payment_id'] ) ) { |
| 36 |
wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 37 |
} |
| 38 |
|
| 39 |
check_admin_referer( 'give_update_payment_details_nonce' ); |
| 40 |
|
| 41 |
// Retrieve the payment ID. |
| 42 |
$payment_id = absint( $data['give_payment_id'] ); |
| 43 |
|
| 44 |
/* @var Give_Payment $payment */ |
| 45 |
$payment = new Give_Payment( $payment_id ); |
| 46 |
|
| 47 |
$status = $data['give-payment-status']; |
| 48 |
$hour = sanitize_text_field( $data['give-payment-time-hour'] ); |
| 49 |
|
| 50 |
// Restrict to our high and low. |
| 51 |
if ( $hour > 23 ) { |
| 52 |
$hour = 23; |
| 53 |
} elseif ( $hour < 0 ) { |
| 54 |
$hour = 00; |
| 55 |
} |
| 56 |
|
| 57 |
$minute = sanitize_text_field( $data['give-payment-time-min'] ); |
| 58 |
|
| 59 |
// Restrict to our high and low. |
| 60 |
if ( $minute > 59 ) { |
| 61 |
$minute = 59; |
| 62 |
} elseif ( $minute < 0 ) { |
| 63 |
$minute = 00; |
| 64 |
} |
| 65 |
|
| 66 |
$address = give_clean( $data['give-payment-address'][0] ); |
| 67 |
|
| 68 |
$curr_total = $payment->total; |
| 69 |
$new_total = give_maybe_sanitize_amount( ( ! empty( $data['give-payment-total'] ) ? $data['give-payment-total'] : 0 ) ); |
| 70 |
$date = date( 'Y-m-d', strtotime( give_clean( $data['give-payment-date'] ) ) ) . ' ' . $hour . ':' . $minute . ':00'; |
| 71 |
|
| 72 |
$curr_donor_id = sanitize_text_field( $data['give-current-donor'] ); |
| 73 |
$new_donor_id = sanitize_text_field( $data['donor-id'] ); |
| 74 |
|
| 75 |
/** |
| 76 |
* Fires before updating edited donation. |
| 77 |
* |
| 78 |
* @since 1.0 |
| 79 |
* @since 1.8.9 Changes hook name give_update_edited_purchase -> give_update_edited_donation |
| 80 |
* |
| 81 |
* @param int $payment_id The ID of the payment. |
| 82 |
*/ |
| 83 |
do_action( 'give_update_edited_donation', $payment_id ); |
| 84 |
|
| 85 |
$payment->date = $date; |
| 86 |
$payment->anonymous = isset( $data['give_anonymous_donation'] ) ? absint( $data['give_anonymous_donation'] ) : 0; |
| 87 |
|
| 88 |
$updated = $payment->save(); |
| 89 |
|
| 90 |
if ( 0 === $updated ) { |
| 91 |
wp_die( __( 'Error Updating Donation.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) ); |
| 92 |
} |
| 93 |
|
| 94 |
$donor_changed = false; |
| 95 |
|
| 96 |
if ( isset( $data['give-new-donor'] ) && $data['give-new-donor'] == '1' ) { |
| 97 |
|
| 98 |
$email = ! empty( $data['give-new-donor-email'] ) ? sanitize_text_field( $data['give-new-donor-email'] ) : ''; |
| 99 |
$first_name = ! empty( $data['give-new-donor-first-name'] ) ? sanitize_text_field( $data['give-new-donor-first-name'] ) : ''; |
| 100 |
$last_name = ! empty( $data['give-new-donor-last-name'] ) ? sanitize_text_field( $data['give-new-donor-last-name'] ) : ''; |
| 101 |
$names = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) ); |
| 102 |
|
| 103 |
if ( empty( $email ) || empty( $first_name ) ) { |
| 104 |
wp_die( __( 'New Donor requires first name and email address.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) ); |
| 105 |
} |
| 106 |
|
| 107 |
$donor = new Give_Donor( $email ); |
| 108 |
if ( empty( $donor->id ) ) { |
| 109 |
$donor_data = array( |
| 110 |
'name' => $names, |
| 111 |
'email' => $email, |
| 112 |
); |
| 113 |
$user_id = email_exists( $email ); |
| 114 |
if ( false !== $user_id ) { |
| 115 |
$donor_data['user_id'] = $user_id; |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! $donor->create( $donor_data ) ) { |
| 119 |
// Failed to create the new donor, assume the previous donor. |
| 120 |
$donor_changed = false; |
| 121 |
$donor = new Give_Donor( $curr_donor_id ); |
| 122 |
give_set_error( 'give-payment-new-donor-fail', __( 'Error creating new donor.', 'give' ) ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// Create and Update Donor First Name and Last Name in Meta Fields. |
| 127 |
$donor->update_meta( '_give_donor_first_name', $first_name ); |
| 128 |
$donor->update_meta( '_give_donor_last_name', $last_name ); |
| 129 |
|
| 130 |
$new_donor_id = $donor->id; |
| 131 |
|
| 132 |
$previous_donor = new Give_Donor( $curr_donor_id ); |
| 133 |
|
| 134 |
$donor_changed = true; |
| 135 |
|
| 136 |
} elseif ( $curr_donor_id !== $new_donor_id ) { |
| 137 |
|
| 138 |
$donor = new Give_Donor( $new_donor_id ); |
| 139 |
$email = $donor->email; |
| 140 |
$names = $donor->name; |
| 141 |
|
| 142 |
$previous_donor = new Give_Donor( $curr_donor_id ); |
| 143 |
|
| 144 |
$donor_changed = true; |
| 145 |
|
| 146 |
} else { |
| 147 |
$donor = new Give_Donor( $curr_donor_id ); |
| 148 |
$email = $donor->email; |
| 149 |
$names = $donor->name; |
| 150 |
} |
| 151 |
|
| 152 |
if ( $donor_changed ) { |
| 153 |
|
| 154 |
// Setup first and last name from input values. |
| 155 |
$first_name = $donor->get_first_name(); |
| 156 |
$last_name = $donor->get_last_name(); |
| 157 |
|
| 158 |
$payment->first_name = $first_name; |
| 159 |
$payment->last_name = $last_name; |
| 160 |
|
| 161 |
// Remove the stats and payment from the previous donor and attach it to the new donor. |
| 162 |
$previous_donor->remove_payment( $payment_id, false ); |
| 163 |
$donor->attach_payment( $payment_id, false ); |
| 164 |
|
| 165 |
if ( 'publish' == $status ) { |
| 166 |
|
| 167 |
// Reduce previous user donation count and amount. |
| 168 |
$previous_donor->decrease_donation_count(); |
| 169 |
$previous_donor->decrease_value( $curr_total ); |
| 170 |
|
| 171 |
// If donation was completed adjust stats of new donors. |
| 172 |
$donor->increase_purchase_count(); |
| 173 |
$donor->increase_value( $new_total ); |
| 174 |
} |
| 175 |
|
| 176 |
$payment->customer_id = $donor->id; |
| 177 |
} else { |
| 178 |
|
| 179 |
if ( 'publish' === $status ) { |
| 180 |
// Update user donation stat. |
| 181 |
$donor->update_donation_value( $curr_total, $new_total ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// Set new meta values. |
| 186 |
$payment->user_id = $donor->user_id; |
| 187 |
$payment->email = $donor->email; |
| 188 |
$payment->address = $address; |
| 189 |
$payment->total = $new_total; |
| 190 |
|
| 191 |
// Check for payment notes. |
| 192 |
if ( ! empty( $data['give-payment-note'] ) ) { |
| 193 |
|
| 194 |
$note = wp_kses( $data['give-payment-note'], array() ); |
| 195 |
give_insert_payment_note( $payment_id, $note ); |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
// Set new status. |
| 200 |
$payment->status = $status; |
| 201 |
|
| 202 |
// Adjust total store earnings if the payment total has been changed. |
| 203 |
if ( $new_total !== $curr_total && 'publish' == $status ) { |
| 204 |
|
| 205 |
if ( $new_total > $curr_total ) { |
| 206 |
// Increase if our new total is higher. |
| 207 |
$difference = $new_total - $curr_total; |
| 208 |
give_increase_total_earnings( $difference ); |
| 209 |
|
| 210 |
// Increase form earnings. |
| 211 |
give_increase_earnings( $payment->form_id, $difference, $payment->ID ); |
| 212 |
} elseif ( $curr_total > $new_total ) { |
| 213 |
// Decrease if our new total is lower. |
| 214 |
$difference = $curr_total - $new_total; |
| 215 |
give_decrease_total_earnings( $difference ); |
| 216 |
|
| 217 |
// Decrease form earnings. |
| 218 |
give_decrease_form_earnings( $payment->form_id, $difference, $payment->ID ); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
$payment->save(); |
| 223 |
|
| 224 |
// Get new give form ID. |
| 225 |
$new_form_id = absint( $data['give-payment-form-select'] ); |
| 226 |
$current_form_id = absint( $payment->get_meta( '_give_payment_form_id' ) ); |
| 227 |
|
| 228 |
// We are adding payment transfer code in last to remove any conflict with above functionality. |
| 229 |
// For example: above code will automatically handle form stat (increase/decrease) when payment status changes. |
| 230 |
// Check if user want to transfer current payment to new give form id. |
| 231 |
if ( $new_form_id && $new_form_id != $current_form_id ) { |
| 232 |
|
| 233 |
// Get new give form title. |
| 234 |
$new_form_title = get_the_title( $new_form_id ); |
| 235 |
|
| 236 |
// Update payment give form meta data. |
| 237 |
$payment->update_meta( '_give_payment_form_id', $new_form_id ); |
| 238 |
$payment->update_meta( '_give_payment_form_title', $new_form_title ); |
| 239 |
|
| 240 |
// Update price id payment metadata. |
| 241 |
if ( ! give_has_variable_prices( $new_form_id ) ) { |
| 242 |
$payment->update_meta( '_give_payment_price_id', '' ); |
| 243 |
} |
| 244 |
|
| 245 |
// If donation was completed, adjust stats of forms. |
| 246 |
if ( 'publish' == $status ) { |
| 247 |
|
| 248 |
// Decrease sale of old give form. For other payment status. |
| 249 |
$current_form = new Give_Donate_Form( $current_form_id ); |
| 250 |
$current_form->decrease_sales(); |
| 251 |
$current_form->decrease_earnings( $curr_total, $payment->ID ); |
| 252 |
|
| 253 |
// Increase sale of new give form. |
| 254 |
$new_form = new Give_Donate_Form( $new_form_id ); |
| 255 |
$new_form->increase_sales(); |
| 256 |
$new_form->increase_earnings( $new_total, $payment->ID ); |
| 257 |
} |
| 258 |
|
| 259 |
// Re setup payment to update new meta value in object. |
| 260 |
$payment->update_payment_setup( $payment->ID ); |
| 261 |
|
| 262 |
// Update form id in payment logs. |
| 263 |
Give()->async_process->data( |
| 264 |
array( |
| 265 |
'data' => array( $new_form_id, $payment_id ), |
| 266 |
'hook' => 'give_update_log_form_id', |
| 267 |
) |
| 268 |
)->dispatch(); |
| 269 |
} |
| 270 |
|
| 271 |
// Update price id if current form is variable form. |
| 272 |
/* @var Give_Donate_Form $form */ |
| 273 |
$form = new Give_Donate_Form( $payment->form_id ); |
| 274 |
|
| 275 |
if ( isset( $data['give-variable-price'] ) && $form->has_variable_prices() ) { |
| 276 |
|
| 277 |
// Get payment meta data. |
| 278 |
$payment_meta = $payment->get_meta(); |
| 279 |
|
| 280 |
$price_info = array(); |
| 281 |
$price_id = ''; |
| 282 |
|
| 283 |
// Get price info |
| 284 |
if ( 0 <= $data['give-variable-price'] ) { |
| 285 |
foreach ( $form->prices as $variable_price ) { |
| 286 |
if ( $new_total === give_maybe_sanitize_amount( $variable_price['_give_amount'] ) ) { |
| 287 |
$price_info = $variable_price; |
| 288 |
break; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
// Set price id. |
| 294 |
if ( ! empty( $price_info ) ) { |
| 295 |
$price_id = $data['give-variable-price']; |
| 296 |
|
| 297 |
if ( $data['give-variable-price'] !== $price_info['_give_id']['level_id'] ) { |
| 298 |
// Set price id to amount match. |
| 299 |
$price_id = $price_info['_give_id']['level_id']; |
| 300 |
} |
| 301 |
} elseif ( $form->is_custom_price_mode() ) { |
| 302 |
$price_id = 'custom'; |
| 303 |
} |
| 304 |
|
| 305 |
// Update payment meta data. |
| 306 |
$payment_meta['price_id'] = $price_id; |
| 307 |
|
| 308 |
// Update payment give form meta data. |
| 309 |
$payment->update_meta( '_give_payment_price_id', $price_id ); |
| 310 |
$payment->update_meta( '_give_payment_meta', $payment_meta ); |
| 311 |
|
| 312 |
// Re setup payment to update new meta value in object. |
| 313 |
$payment->update_payment_setup( $payment->ID ); |
| 314 |
} |
| 315 |
|
| 316 |
// Update payment campaign. |
| 317 |
$donation = Donation::find($payment->ID); |
| 318 |
|
| 319 |
if ($donation) { |
| 320 |
$new_campaign_id = absint($data['give-payment-campaign-select']); |
| 321 |
$current_campaign_id = absint($donation->campaignId); |
| 322 |
|
| 323 |
if ($new_campaign_id && $new_campaign_id !== $current_campaign_id) { |
| 324 |
$donation->campaignId = $new_campaign_id; |
| 325 |
$donation->save(); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
$comment_id = isset( $data['give_comment_id'] ) ? absint( $data['give_comment_id'] ) : 0; |
| 330 |
$has_anonymous_setting_field = give_is_anonymous_donation_field_enabled( $payment->form_id ); |
| 331 |
|
| 332 |
if ( $has_anonymous_setting_field ) { |
| 333 |
give_update_meta( $payment->ID, '_give_anonymous_donation', $payment->anonymous ); |
| 334 |
} |
| 335 |
|
| 336 |
// Update comment. |
| 337 |
if ( give_is_donor_comment_field_enabled( $payment->form_id ) ) { |
| 338 |
// We are access comment directly from $_POST because comment formatting remove because of give_clean in give_post_actions. |
| 339 |
$data['give_comment'] = trim($_POST['give_comment']); |
| 340 |
$payment->update_meta(DonationMetaKeys::COMMENT, sanitize_textarea_field($data['give_comment'])); |
| 341 |
} |
| 342 |
|
| 343 |
// Check if payment status is not completed then update the goal progress for donation form. |
| 344 |
if ( 'publish' !== $status ) { |
| 345 |
give_update_goal_progress( $form->ID ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Fires after updating edited donation. |
| 350 |
* |
| 351 |
* @since 1.0 |
| 352 |
* @since 1.8.9 Changes hook name give_updated_edited_purchase -> give_updated_edited_donation |
| 353 |
* |
| 354 |
* @param int $payment_id The ID of the payment. |
| 355 |
*/ |
| 356 |
do_action( 'give_updated_edited_donation', $payment_id ); |
| 357 |
|
| 358 |
wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=payment-updated&id=' . $payment_id ) ); |
| 359 |
exit; |
| 360 |
} |
| 361 |
|
| 362 |
add_action( 'give_update_payment_details', 'give_update_payment_details' ); |
| 363 |
|
| 364 |
/** |
| 365 |
* Trigger a Donation Deletion. |
| 366 |
* |
| 367 |
* @since 1.0 |
| 368 |
* |
| 369 |
* @param array $data Arguments passed. |
| 370 |
* |
| 371 |
* @return void |
| 372 |
*/ |
| 373 |
function give_trigger_donation_delete( $data ) { |
| 374 |
if ( wp_verify_nonce( $data['_wpnonce'], 'give_donation_nonce' ) ) { |
| 375 |
|
| 376 |
$payment_id = absint( $data['purchase_id'] ); |
| 377 |
|
| 378 |
if ( ! current_user_can( 'delete_give_payments', $payment_id ) ) { |
| 379 |
wp_die( __( 'You do not have permission to delete payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 380 |
} |
| 381 |
|
| 382 |
give_delete_donation( $payment_id ); |
| 383 |
wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&give-messages[]=donation-deleted' ) ); |
| 384 |
give_die(); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
add_action( 'give_delete_payment', 'give_trigger_donation_delete' ); |
| 389 |
|
| 390 |
/** |
| 391 |
* AJAX Store Donation Note |
| 392 |
* |
| 393 |
* @since 2.25.3 Add nonce check. |
| 394 |
*/ |
| 395 |
function give_ajax_store_payment_note() { |
| 396 |
check_ajax_referer('give_insert_payment_note'); |
| 397 |
|
| 398 |
$payment_id = absint($_POST['payment_id']); |
| 399 |
$note = wp_kses($_POST['note'], []); |
| 400 |
$note_type = give_clean($_POST['type']); |
| 401 |
|
| 402 |
if ( ! current_user_can('edit_give_payments', $payment_id)) { |
| 403 |
wp_die(__('You do not have permission to edit payments.', 'give'), __('Error', 'give'), ['response' => 403]); |
| 404 |
} |
| 405 |
|
| 406 |
if ( empty( $payment_id ) || empty( $note ) ) { |
| 407 |
die( '-1' ); |
| 408 |
} |
| 409 |
|
| 410 |
if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) { |
| 411 |
// Backward compatibility. |
| 412 |
$note_id = give_insert_payment_note( $payment_id, $note ); |
| 413 |
} else { |
| 414 |
$note_id = Give()->comment->db->add( |
| 415 |
array( |
| 416 |
'comment_parent' => $payment_id, |
| 417 |
'user_id' => get_current_user_id(), |
| 418 |
'comment_content' => $note, |
| 419 |
'comment_type' => 'donation', |
| 420 |
) |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
if ( $note_id && $note_type ) { |
| 425 |
|
| 426 |
if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) { |
| 427 |
add_comment_meta( $note_id, 'note_type', $note_type, true ); |
| 428 |
} else { |
| 429 |
Give()->comment->db_meta->update_meta( $note_id, 'note_type', $note_type ); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Fire the action |
| 434 |
* |
| 435 |
* @since 2.3.0 |
| 436 |
*/ |
| 437 |
do_action( 'give_donor-note_email_notification', $note_id, $payment_id ); |
| 438 |
} |
| 439 |
|
| 440 |
die( give_get_payment_note_html( $note_id ) ); |
| 441 |
} |
| 442 |
|
| 443 |
add_action( 'wp_ajax_give_insert_payment_note', 'give_ajax_store_payment_note' ); |
| 444 |
|
| 445 |
/** |
| 446 |
* Triggers a donation note deletion without ajax |
| 447 |
* |
| 448 |
* @since 1.0 |
| 449 |
* |
| 450 |
* @param array $data Arguments passed |
| 451 |
* |
| 452 |
* @return void |
| 453 |
*/ |
| 454 |
function give_trigger_payment_note_deletion( $data ) { |
| 455 |
|
| 456 |
if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_delete_payment_note_' . $data['note_id'] ) ) { |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
if ( ! current_user_can( 'edit_give_payments', $data['payment_id'] ) ) { |
| 461 |
wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 462 |
} |
| 463 |
|
| 464 |
$edit_order_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=donation-note-deleted&id=' . absint( $data['payment_id'] ) ); |
| 465 |
|
| 466 |
give_delete_payment_note( $data['note_id'], $data['payment_id'] ); |
| 467 |
|
| 468 |
wp_redirect( $edit_order_url ); |
| 469 |
} |
| 470 |
|
| 471 |
add_action( 'give_delete_payment_note', 'give_trigger_payment_note_deletion' ); |
| 472 |
|
| 473 |
/** |
| 474 |
* Delete a payment note deletion with ajax |
| 475 |
* |
| 476 |
* @since 2.25.3 Add nonce check. |
| 477 |
* @since 1.0 |
| 478 |
* |
| 479 |
* @return void |
| 480 |
*/ |
| 481 |
function give_ajax_delete_payment_note() { |
| 482 |
check_ajax_referer('give_delete_payment_note'); |
| 483 |
|
| 484 |
if ( ! current_user_can( 'edit_give_payments', $_POST['payment_id'] ) ) { |
| 485 |
wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 486 |
} |
| 487 |
|
| 488 |
if ( give_delete_payment_note( $_POST['note_id'], $_POST['payment_id'] ) ) { |
| 489 |
die( '1' ); |
| 490 |
} else { |
| 491 |
die( '-1' ); |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' ); |
| 497 |
|