| 1 |
<?php |
| 2 |
/** |
| 3 |
* Payment Actions |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage 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 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Complete a donation |
| 19 |
* |
| 20 |
* Performs all necessary actions to complete a donation. |
| 21 |
* Triggered by the give_update_payment_status() function. |
| 22 |
* |
| 23 |
* @since 1.0 |
| 24 |
* |
| 25 |
* @param int $payment_id The ID number of the payment. |
| 26 |
* @param string $new_status The status of the payment, probably "publish". |
| 27 |
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
function give_complete_purchase( $payment_id, $new_status, $old_status ) { |
| 32 |
|
| 33 |
// Make sure that payments are only completed once. |
| 34 |
if ( $old_status == 'publish' || $old_status == 'complete' ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// Make sure the payment completion is only processed when new status is complete. |
| 39 |
if ( $new_status != 'publish' && $new_status != 'complete' ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$payment = new Give_Payment( $payment_id ); |
| 44 |
|
| 45 |
$creation_date = get_post_field( 'post_date', $payment_id, 'raw' ); |
| 46 |
$payment_meta = $payment->payment_meta; |
| 47 |
$completed_date = $payment->completed_date; |
| 48 |
$user_info = $payment->user_info; |
| 49 |
$donor_id = $payment->customer_id; |
| 50 |
$amount = $payment->total; |
| 51 |
$price_id = $payment->price_id; |
| 52 |
$form_id = $payment->form_id; |
| 53 |
|
| 54 |
/** |
| 55 |
* Fires before completing donation. |
| 56 |
* |
| 57 |
* @since 1.0 |
| 58 |
* |
| 59 |
* @param int $payment_id The ID of the payment. |
| 60 |
*/ |
| 61 |
do_action( 'give_pre_complete_donation', $payment_id ); |
| 62 |
|
| 63 |
// Ensure these actions only run once, ever. |
| 64 |
if ( empty( $completed_date ) ) { |
| 65 |
|
| 66 |
give_record_donation_in_log( $form_id, $payment_id, $price_id, $creation_date ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Fires after logging donation record. |
| 70 |
* |
| 71 |
* @since 1.0 |
| 72 |
* |
| 73 |
* @param int $form_id The ID number of the form. |
| 74 |
* @param int $payment_id The ID number of the payment. |
| 75 |
* @param array $payment_meta The payment meta. |
| 76 |
*/ |
| 77 |
do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta ); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
// Increase the earnings for this form ID. |
| 82 |
give_increase_earnings( $form_id, $amount, $payment_id ); |
| 83 |
give_increase_donation_count( $form_id ); |
| 84 |
|
| 85 |
// Update the goal progress for this form ID. |
| 86 |
give_update_goal_progress( $form_id ); |
| 87 |
|
| 88 |
// @todo: Refresh only range related stat cache |
| 89 |
give_delete_donation_stats(); |
| 90 |
|
| 91 |
// Increase the donor's donation stats. |
| 92 |
$donor = new Give_Donor( $donor_id ); |
| 93 |
$donor->increase_purchase_count(); |
| 94 |
$donor->increase_value( $amount ); |
| 95 |
|
| 96 |
give_increase_total_earnings( $amount ); |
| 97 |
|
| 98 |
// Ensure this action only runs once ever. |
| 99 |
if ( empty( $completed_date ) ) { |
| 100 |
|
| 101 |
// Save the completed date. |
| 102 |
$payment->completed_date = current_time( 'mysql' ); |
| 103 |
$payment->save(); |
| 104 |
|
| 105 |
/** |
| 106 |
* Fires after a donation successfully complete. |
| 107 |
* |
| 108 |
* @since 1.0 |
| 109 |
* |
| 110 |
* @param int $payment_id The ID of the payment. |
| 111 |
*/ |
| 112 |
do_action( 'give_complete_donation', $payment_id ); |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 ); |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Record payment status change |
| 122 |
* |
| 123 |
* @since 1.0 |
| 124 |
* |
| 125 |
* @param int $payment_id The ID number of the payment. |
| 126 |
* @param string $new_status The status of the payment, probably "publish". |
| 127 |
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
function give_record_status_change( $payment_id, $new_status, $old_status ) { |
| 132 |
|
| 133 |
// Get the list of statuses so that status in the payment note can be translated. |
| 134 |
$stati = give_get_payment_statuses(); |
| 135 |
$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status; |
| 136 |
$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status; |
| 137 |
|
| 138 |
// Skip recording payment status change if there is no update |
| 139 |
if ( $old_status === $new_status ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// translators: 1: old status 2: new status. |
| 144 |
$status_change = sprintf( esc_html__( 'Status changed from %1$s to %2$s.', 'give' ), $old_status, $new_status ); |
| 145 |
|
| 146 |
give_insert_payment_note( $payment_id, $status_change ); |
| 147 |
} |
| 148 |
|
| 149 |
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 ); |
| 150 |
|
| 151 |
|
| 152 |
/** |
| 153 |
* Update Old Payments Totals |
| 154 |
* |
| 155 |
* Updates all old payments, prior to 1.2, with new meta for the total donation amount. |
| 156 |
* |
| 157 |
* It's done to query payments by their totals. |
| 158 |
* |
| 159 |
* @since 1.0 |
| 160 |
* |
| 161 |
* @param array $data Arguments passed. |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
function give_update_old_payments_with_totals( $data ) { |
| 166 |
if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
if ( get_option( 'give_payment_totals_upgraded' ) ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
$payments = give_get_payments( |
| 175 |
array( |
| 176 |
'offset' => 0, |
| 177 |
'number' => - 1, |
| 178 |
'mode' => 'all', |
| 179 |
) |
| 180 |
); |
| 181 |
|
| 182 |
if ( $payments ) { |
| 183 |
foreach ( $payments as $payment ) { |
| 184 |
|
| 185 |
$payment = new Give_Payment( $payment->ID ); |
| 186 |
$meta = $payment->get_meta(); |
| 187 |
|
| 188 |
$payment->total = $meta['amount']; |
| 189 |
$payment->save(); |
| 190 |
|
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
add_option( 'give_payment_totals_upgraded', 1 ); |
| 195 |
} |
| 196 |
|
| 197 |
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' ); |
| 198 |
|
| 199 |
/** |
| 200 |
* Mark Abandoned Donations |
| 201 |
* |
| 202 |
* Updates over a week-old 'pending' donations to 'abandoned' status. |
| 203 |
* |
| 204 |
* @since 1.0 |
| 205 |
* |
| 206 |
* @return void |
| 207 |
*/ |
| 208 |
function give_mark_abandoned_donations() { |
| 209 |
$args = array( |
| 210 |
'status' => 'pending', |
| 211 |
'number' => - 1, |
| 212 |
'output' => 'give_payments', |
| 213 |
); |
| 214 |
|
| 215 |
add_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
| 216 |
|
| 217 |
$payments = give_get_payments( $args ); |
| 218 |
|
| 219 |
remove_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
| 220 |
|
| 221 |
if ( $payments ) { |
| 222 |
/** |
| 223 |
* Filter payment gateways: Used to set payment gateways that can be skipped while updating the donation status from pending to abandoned. |
| 224 |
* |
| 225 |
* @since 1.6 |
| 226 |
* |
| 227 |
* @param array $skip_payment_gateways Array of payment gateways |
| 228 |
*/ |
| 229 |
$skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) ); |
| 230 |
|
| 231 |
/* @var Give_Payment $payment */ |
| 232 |
foreach ( $payments as $payment ) { |
| 233 |
$gateway = give_get_payment_gateway( $payment->ID ); |
| 234 |
|
| 235 |
// Skip payment gateways. |
| 236 |
if ( in_array( $gateway, $skip_payment_gateways ) ) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
$payment->status = 'abandoned'; |
| 241 |
$payment->save(); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
Give_Cron::add_weekly_event( 'give_mark_abandoned_donations' ); |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* Trigger the refresh of this month reports transients |
| 251 |
* |
| 252 |
* @since 1.7 |
| 253 |
* |
| 254 |
* @param int $payment_ID Payment ID. |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
function give_refresh_thismonth_stat_transients( $payment_ID ) { |
| 259 |
// Monthly stats. |
| 260 |
Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 261 |
|
| 262 |
// @todo: Refresh only range related stat cache |
| 263 |
give_delete_donation_stats(); |
| 264 |
} |
| 265 |
|
| 266 |
add_action( 'save_post_give_payment', 'give_refresh_thismonth_stat_transients' ); |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Add support to get all payment meta. |
| 271 |
* Note: only use for internal purpose |
| 272 |
* |
| 273 |
* @since 3.19.0 change $donor_data['address'] to array instead of false |
| 274 |
* @since 2.0 |
| 275 |
* |
| 276 |
* @param $check |
| 277 |
* @param $object_id |
| 278 |
* @param $meta_key |
| 279 |
* @param $single |
| 280 |
* |
| 281 |
* @return array |
| 282 |
*/ |
| 283 |
function give_bc_v20_get_payment_meta( $check, $object_id, $meta_key, $single ) { |
| 284 |
// Bailout. |
| 285 |
if ( |
| 286 |
'give_payment' !== get_post_type( $object_id ) |
| 287 |
|| '_give_payment_meta' !== $meta_key |
| 288 |
) { |
| 289 |
return $check; |
| 290 |
} |
| 291 |
|
| 292 |
$cache_key = "_give_payment_meta_{$object_id}"; |
| 293 |
|
| 294 |
// Get already calculate payment meta from cache. |
| 295 |
$payment_meta = Give_Cache::get_db_query( $cache_key ); |
| 296 |
|
| 297 |
if ( is_null( $payment_meta ) ) { |
| 298 |
// Remove filter. |
| 299 |
remove_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999 ); |
| 300 |
|
| 301 |
$donation = new Give_Payment( $object_id ); |
| 302 |
|
| 303 |
// Get all payment meta. |
| 304 |
$payment_meta = give_get_meta( $object_id ); |
| 305 |
|
| 306 |
// Set default value to array. |
| 307 |
if ( empty( $payment_meta ) ) { |
| 308 |
return $check; |
| 309 |
} |
| 310 |
|
| 311 |
// Convert all meta key value to string instead of array |
| 312 |
array_walk( |
| 313 |
$payment_meta, |
| 314 |
function ( &$meta, $key ) { |
| 315 |
$meta = current( $meta ); |
| 316 |
} |
| 317 |
); |
| 318 |
|
| 319 |
/** |
| 320 |
* Add backward compatibility to old meta keys. |
| 321 |
*/ |
| 322 |
// Donation key. |
| 323 |
$payment_meta['key'] = ! empty( $payment_meta['_give_payment_purchase_key'] ) ? $payment_meta['_give_payment_purchase_key'] : ''; |
| 324 |
|
| 325 |
// Donation form. |
| 326 |
$payment_meta['form_title'] = ! empty( $payment_meta['_give_payment_form_title'] ) ? $payment_meta['_give_payment_form_title'] : ''; |
| 327 |
|
| 328 |
// Donor email. |
| 329 |
$payment_meta['email'] = ! empty( $payment_meta['_give_payment_donor_email'] ) ? $payment_meta['_give_payment_donor_email'] : ''; |
| 330 |
$payment_meta['email'] = ! empty( $payment_meta['email'] ) ? |
| 331 |
$payment_meta['email'] : |
| 332 |
Give()->donors->get_column( 'email', $donation->donor_id ); |
| 333 |
|
| 334 |
// Form id. |
| 335 |
$payment_meta['form_id'] = ! empty( $payment_meta['_give_payment_form_id'] ) ? $payment_meta['_give_payment_form_id'] : ''; |
| 336 |
|
| 337 |
// Price id. |
| 338 |
$payment_meta['price_id'] = isset( $payment_meta['_give_payment_price_id'] ) ? $payment_meta['_give_payment_price_id'] : ''; |
| 339 |
|
| 340 |
// Date. |
| 341 |
$payment_meta['date'] = ! empty( $payment_meta['_give_payment_date'] ) ? $payment_meta['_give_payment_date'] : ''; |
| 342 |
$payment_meta['date'] = ! empty( $payment_meta['date'] ) ? |
| 343 |
$payment_meta['date'] : |
| 344 |
get_post_field( 'post_date', $object_id ); |
| 345 |
|
| 346 |
// Currency. |
| 347 |
$payment_meta['currency'] = ! empty( $payment_meta['_give_payment_currency'] ) ? $payment_meta['_give_payment_currency'] : ''; |
| 348 |
|
| 349 |
// Decode donor data. |
| 350 |
$donor_id = ! empty( $payment_meta['_give_payment_donor_id'] ) ? $payment_meta['_give_payment_donor_id'] : 0; |
| 351 |
$donor = new Give_Donor( $donor_id ); |
| 352 |
|
| 353 |
// Donor first name. |
| 354 |
$donor_data['first_name'] = ! empty( $payment_meta['_give_donor_billing_first_name'] ) ? $payment_meta['_give_donor_billing_first_name'] : ''; |
| 355 |
$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ? |
| 356 |
$donor_data['first_name'] : |
| 357 |
$donor->get_first_name(); |
| 358 |
|
| 359 |
// Donor last name. |
| 360 |
$donor_data['last_name'] = ! empty( $payment_meta['_give_donor_billing_last_name'] ) ? $payment_meta['_give_donor_billing_last_name'] : ''; |
| 361 |
$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ? |
| 362 |
$donor_data['last_name'] : |
| 363 |
$donor->get_last_name(); |
| 364 |
|
| 365 |
// Donor email. |
| 366 |
$donor_data['email'] = $payment_meta['email']; |
| 367 |
|
| 368 |
// User ID. |
| 369 |
$donor_data['id'] = $donation->user_id; |
| 370 |
|
| 371 |
$donor_data['address'] = []; |
| 372 |
|
| 373 |
// Address1. |
| 374 |
$address1 = ! empty( $payment_meta['_give_donor_billing_address1'] ) ? $payment_meta['_give_donor_billing_address1'] : ''; |
| 375 |
if ( $address1 ) { |
| 376 |
$donor_data['address']['line1'] = $address1; |
| 377 |
} |
| 378 |
|
| 379 |
// Address2. |
| 380 |
$address2 = ! empty( $payment_meta['_give_donor_billing_address2'] ) ? $payment_meta['_give_donor_billing_address2'] : ''; |
| 381 |
if ( $address2 ) { |
| 382 |
$donor_data['address']['line2'] = $address2; |
| 383 |
} |
| 384 |
|
| 385 |
// City. |
| 386 |
$city = ! empty( $payment_meta['_give_donor_billing_city'] ) ? $payment_meta['_give_donor_billing_city'] : ''; |
| 387 |
if ( $city ) { |
| 388 |
$donor_data['address']['city'] = $city; |
| 389 |
} |
| 390 |
|
| 391 |
// Zip. |
| 392 |
$zip = ! empty( $payment_meta['_give_donor_billing_zip'] ) ? $payment_meta['_give_donor_billing_zip'] : ''; |
| 393 |
if ( $zip ) { |
| 394 |
$donor_data['address']['zip'] = $zip; |
| 395 |
} |
| 396 |
|
| 397 |
// State. |
| 398 |
$state = ! empty( $payment_meta['_give_donor_billing_state'] ) ? $payment_meta['_give_donor_billing_state'] : ''; |
| 399 |
if ( $state ) { |
| 400 |
$donor_data['address']['state'] = $state; |
| 401 |
} |
| 402 |
|
| 403 |
// Country. |
| 404 |
$country = ! empty( $payment_meta['_give_donor_billing_country'] ) ? $payment_meta['_give_donor_billing_country'] : ''; |
| 405 |
if ( $country ) { |
| 406 |
$donor_data['address']['country'] = $country; |
| 407 |
} |
| 408 |
|
| 409 |
$payment_meta['user_info'] = $donor_data; |
| 410 |
|
| 411 |
// Add filter |
| 412 |
add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 ); |
| 413 |
|
| 414 |
// Set custom meta key into payment meta. |
| 415 |
if ( ! empty( $payment_meta['_give_payment_meta'] ) ) { |
| 416 |
$payment_meta['_give_payment_meta'] = is_array( $payment_meta['_give_payment_meta'] ) ? $payment_meta['_give_payment_meta'] : array(); |
| 417 |
|
| 418 |
$payment_meta = array_merge( maybe_unserialize( $payment_meta['_give_payment_meta'] ), $payment_meta ); |
| 419 |
} |
| 420 |
|
| 421 |
// Set cache. |
| 422 |
Give_Cache::set_db_query( $cache_key, $payment_meta ); |
| 423 |
} |
| 424 |
|
| 425 |
if ( $single ) { |
| 426 |
/** |
| 427 |
* Filter the payment meta |
| 428 |
* Add custom meta key to payment meta |
| 429 |
* |
| 430 |
* @since 2.0 |
| 431 |
*/ |
| 432 |
$new_payment_meta[0] = apply_filters( 'give_get_payment_meta', $payment_meta, $object_id, $meta_key ); |
| 433 |
|
| 434 |
$payment_meta = $new_payment_meta; |
| 435 |
} |
| 436 |
|
| 437 |
return $payment_meta; |
| 438 |
} |
| 439 |
|
| 440 |
if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) { |
| 441 |
add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 ); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Add meta in payment that store page id and page url. |
| 446 |
* |
| 447 |
* Will add/update when user add click on the checkout page. |
| 448 |
* The status of the donation doest not matter as it get change when user had made the payment successfully. |
| 449 |
* |
| 450 |
* @since 1.8.13 |
| 451 |
* |
| 452 |
* @param int $payment_id Payment id for which the meta value should be updated. |
| 453 |
*/ |
| 454 |
function give_payment_save_page_data( $payment_id ) { |
| 455 |
$page_url = ( ! empty( $_REQUEST['give-current-url'] ) ? esc_url( $_REQUEST['give-current-url'] ) : false ); |
| 456 |
|
| 457 |
// Check $page_url is not empty. |
| 458 |
if ( $page_url ) { |
| 459 |
update_post_meta( $payment_id, '_give_current_url', $page_url ); |
| 460 |
$page_id = url_to_postid( $page_url ); |
| 461 |
// Check $page_id is not empty. |
| 462 |
if ( $page_id ) { |
| 463 |
update_post_meta( $payment_id, '_give_current_page_id', $page_id ); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
// Fire when payment is save. |
| 469 |
add_action( 'give_insert_payment', 'give_payment_save_page_data' ); |
| 470 |
|