PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.5.7
GiveWP – Donation Plugin and Fundraising Platform v2.5.7
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / payments / class-give-payment.php
give / includes / payments Last commit date
actions.php 7 years ago backward-compatibility.php 7 years ago class-give-payment.php 7 years ago class-give-sequential-donation-number.php 6 years ago class-payment-stats.php 7 years ago class-payments-query.php 6 years ago functions.php 7 years ago
class-give-payment.php
2094 lines
1 <?php
2 /**
3 * Payments
4 *
5 * @package Give
6 * @subpackage Classes/Give_Payment
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.5
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Give_Payment Class
19 *
20 * This class is for working with payments in Give.
21 *
22 * @property int $ID
23 * @property bool $new
24 * @property string $number
25 * @property string $mode
26 * @property string $import
27 * @property string $key
28 * @property string $form_title
29 * @property string|int $form_id
30 * @property string|int $price_id
31 * @property string|int $total
32 * @property string|int $subtotal
33 * @property string|int $fees
34 * @property string|int $fees_total
35 * @property string $post_status
36 * @property string $date
37 * @property string $post_date
38 * @property string $status
39 * @property string $email
40 * @property array $payment_meta
41 * @property string $customer_id
42 * @property string $donor_id
43 * @property string $completed_date
44 * @property string $currency
45 * @property string $ip
46 * @property array $user_info
47 * @property string $gateway
48 * @property string $user_id
49 * @property string $title_prefix
50 * @property string $first_name
51 * @property string $last_name
52 * @property string $parent_payment
53 * @property string $transaction_id
54 * @property string $old_status
55 *
56 * @since 1.5
57 */
58 final class Give_Payment {
59
60 /**
61 * The Payment ID.
62 *
63 * @since 1.5
64 *
65 * @var int
66 */
67 public $ID = 0;
68
69 /**
70 * Protected non-read $_ID.
71 *
72 * @var int
73 */
74 protected $_ID = 0;
75
76 /**
77 * Identify if the payment is a new one or existing.
78 *
79 * @since 1.5
80 * @access protected
81 *
82 * @var boolean
83 */
84 protected $new = false;
85
86 /**
87 * The Payment number (for use with sequential payments).
88 *
89 * @since 1.5
90 * @access protected
91 *
92 * @var string
93 */
94 protected $number = '';
95
96 /**
97 * The Gateway mode the payment was made in.
98 *
99 * @since 1.5
100 * @access protected
101 *
102 * @var string
103 */
104 protected $mode = 'live';
105
106 /**
107 * Is donations is Import or not.
108 *
109 * @since 1.8.13
110 * @access protected
111 *
112 * @var bool
113 */
114 protected $import = false;
115
116 /**
117 * The unique donation payment key.
118 *
119 * @since 1.5
120 * @access protected
121 *
122 * @var string
123 */
124 protected $key = '';
125
126 /**
127 * The Donation Form Title
128 *
129 * @since 1.5
130 * @access protected
131 *
132 * @var string
133 */
134 protected $form_title = 0;
135
136 /**
137 * The Donation Form ID
138 *
139 * @since 1.5
140 * @access protected
141 *
142 * @var string
143 */
144 protected $form_id = 0;
145
146 /**
147 * The Donation Form Price ID
148 *
149 * @since 1.5
150 * @access protected
151 *
152 * @var string|int
153 */
154 protected $price_id = 0;
155
156 /**
157 * The total amount of the donation payment.
158 *
159 * @since 1.5
160 * @access protected
161 *
162 * @var float
163 */
164 protected $total = 0.00;
165
166 /**
167 * The Subtotal fo the payment.
168 *
169 * @since 1.5
170 * @access protected
171 *
172 * @var float
173 */
174 protected $subtotal = 0;
175
176 /**
177 * The date the payment was created
178 *
179 * @since 1.5
180 * @access protected
181 *
182 * @var string
183 */
184 protected $date = '';
185
186 /**
187 * The date the payment post was created.
188 *
189 * @var string
190 */
191 protected $post_date = '';
192
193 /**
194 * The date the payment was marked as 'complete'.
195 *
196 * @since 1.5
197 * @access protected
198 *
199 * @var string
200 */
201 protected $completed_date = '';
202
203 /**
204 * The status of the donation payment.
205 *
206 * @since 1.5
207 * @access protected
208 *
209 * @var string
210 */
211 protected $status = 'pending';
212
213 /**
214 * Donation Status.
215 *
216 * @var string
217 */
218 protected $post_status = 'pending'; // Same as $status but here for backwards compat.
219
220 /**
221 * When updating, the old status prior to the change
222 *
223 * @since 1.5
224 * @access protected
225 *
226 * @var string
227 */
228 protected $old_status = '';
229
230 /**
231 * The display name of the current payment status.
232 *
233 * @since 1.5
234 * @access protected
235 *
236 * @var string
237 */
238 protected $status_nicename = '';
239
240 /**
241 * The donor ID that made the payment.
242 *
243 * @since 1.5
244 * @access protected
245 *
246 * @var integer
247 */
248 protected $customer_id = null;
249
250 /**
251 * The Donor ID (if logged in) that made the payment
252 *
253 * @since 1.8.13
254 * @access protected
255 *
256 * @var integer
257 */
258 protected $donor_id = 0;
259
260 /**
261 * The User ID (if logged in) that made the payment
262 *
263 * @since 1.5
264 * @access protected
265 *
266 * @var integer
267 */
268 protected $user_id = 0;
269
270 /**
271 * The Title Prefix/Salutation of the Donor.
272 *
273 * @since 2.2
274 *
275 * @var string
276 */
277 protected $title_prefix = '';
278
279 /**
280 * The first name of the payee
281 *
282 * @since 1.5
283 * @access protected
284 *
285 * @var string
286 */
287 protected $first_name = '';
288
289 /**
290 * The last name of the payee
291 *
292 * @since 1.5
293 * @access protected
294 *
295 * @var string
296 */
297 protected $last_name = '';
298
299 /**
300 * The email used for the payment
301 *
302 * @since 1.5
303 * @access protected
304 *
305 * @var string
306 */
307 protected $email = '';
308
309 /**
310 * Legacy (not to be accessed) array of user information
311 *
312 * @since 1.5
313 * @access private
314 *
315 * @var array
316 */
317 private $user_info = array();
318
319 /**
320 * Legacy (not to be accessed) payment meta array
321 *
322 * @since 1.5
323 * @access private
324 *
325 * @var array
326 */
327 private $payment_meta = array();
328
329 /**
330 * The physical address used for the payment if provided
331 *
332 * @since 1.5
333 * @access protected
334 *
335 * @var array
336 */
337 protected $address = array();
338
339 /**
340 * The transaction ID returned by the gateway
341 *
342 * @since 1.5
343 * @access protected
344 *
345 * @var string
346 */
347 protected $transaction_id = '';
348
349 /**
350 * IP Address payment was made from
351 *
352 * @since 1.5
353 * @access protected
354 *
355 * @var string
356 */
357 protected $ip = '';
358
359 /**
360 * The gateway used to process the payment
361 *
362 * @since 1.5
363 * @access protected
364 *
365 * @var string
366 */
367 protected $gateway = '';
368
369 /**
370 * The the payment was made with
371 *
372 * @since 1.5
373 * @access protected
374 *
375 * @var string
376 */
377 protected $currency = '';
378
379 /**
380 * Array of items that have changed since the last save() was run.
381 * This is for internal use, to allow fewer update_payment_meta calls to be run.
382 *
383 * @since 1.5
384 * @access private
385 *
386 * @var array
387 */
388 private $pending;
389
390 /**
391 * The parent payment (if applicable)
392 *
393 * @since 1.5
394 * @access protected
395 *
396 * @var integer
397 */
398 protected $parent_payment = 0;
399
400 /**
401 * Setup the Give Payments class
402 *
403 * @since 1.5
404 * @access public
405 *
406 * @param int|bool $payment_id A given payment.
407 *
408 * @return mixed void|false
409 */
410 public function __construct( $payment_id = false ) {
411
412 if ( empty( $payment_id ) ) {
413 return false;
414 }
415
416 $this->setup_payment( $payment_id );
417 }
418
419 /**
420 * Magic GET function.
421 *
422 * @since 1.5
423 * @access public
424 *
425 * @param string $key The property.
426 *
427 * @return mixed The value.
428 */
429 public function __get( $key ) {
430
431 if ( method_exists( $this, 'get_' . $key ) ) {
432
433 $value = call_user_func( array( $this, 'get_' . $key ) );
434
435 } else {
436
437 $value = $this->$key;
438
439 }
440
441 return $value;
442 }
443
444 /**
445 * Magic SET function
446 *
447 * Sets up the pending array for the save method
448 *
449 * @since 1.5
450 * @access public
451 *
452 * @param string $key The property name.
453 * @param mixed $value The value of the property.
454 */
455 public function __set( $key, $value ) {
456 $ignore = array( '_ID' );
457
458 if ( 'status' === $key ) {
459 $this->old_status = $this->status;
460 }
461
462 if ( ! in_array( $key, $ignore ) ) {
463 $this->pending[ $key ] = $value;
464 }
465
466 if ( '_ID' !== $key ) {
467 $this->$key = $value;
468 }
469 }
470
471 /**
472 * Magic ISSET function, which allows empty checks on protected elements
473 *
474 * @since 1.5
475 * @access public
476 *
477 * @param string $name The attribute to get.
478 *
479 * @return boolean|null If the item is set or not
480 */
481 public function __isset( $name ) {
482 if ( property_exists( $this, $name ) ) {
483 return false === empty( $this->$name );
484 } else {
485 return null;
486 }
487 }
488
489 /**
490 * Setup payment properties
491 *
492 * @since 1.5
493 * @access private
494 *
495 * @param int $payment_id The payment ID.
496 *
497 * @return bool If the setup was successful or not
498 */
499 private function setup_payment( $payment_id ) {
500 $this->pending = array();
501
502 if ( empty( $payment_id ) ) {
503 return false;
504 }
505
506 $payment = get_post( absint( $payment_id ) );
507
508 if ( ! $payment || is_wp_error( $payment ) ) {
509 return false;
510 }
511
512 if ( 'give_payment' !== $payment->post_type ) {
513 return false;
514 }
515
516 Give_Payments_Query::update_meta_cache( array( $payment_id ) );
517
518 /**
519 * Fires before payment setup.
520 *
521 * Allow extensions to perform actions before the payment is loaded.
522 *
523 * @since 1.5
524 *
525 * @param Give_Payment $this Payment object.
526 * @param int $payment_id The ID of the payment.
527 */
528 do_action( 'give_pre_setup_payment', $this, $payment_id );
529
530 // Get payment from cache.
531 $donation_vars = Give_Cache::get_group( $payment_id, 'give-donations' );
532
533 if ( is_null( $donation_vars ) ) {
534 // Primary Identifier.
535 $this->ID = absint( $payment_id );
536
537 // Protected ID that can never be changed.
538 $this->_ID = absint( $payment_id );
539
540 // We have a payment, get the generic payment_meta item to reduce calls to it.
541 $this->payment_meta = $this->get_meta();
542
543 // Status and Dates.
544 $this->date = $payment->post_date;
545 $this->post_date = $payment->post_date;
546 $this->completed_date = $this->setup_completed_date();
547 $this->status = $payment->post_status;
548 $this->post_status = $this->status;
549 $this->mode = $this->setup_mode();
550 $this->import = $this->setup_import();
551 $this->parent_payment = $payment->post_parent;
552
553 $all_payment_statuses = give_get_payment_statuses();
554 $this->status_nicename = array_key_exists( $this->status, $all_payment_statuses ) ? $all_payment_statuses[ $this->status ] : ucfirst( $this->status );
555
556 // Currency Based.
557 $this->total = $this->setup_total();
558 $this->subtotal = $this->setup_subtotal();
559 $this->currency = $this->setup_currency();
560
561 // Gateway based.
562 $this->gateway = $this->setup_gateway();
563 $this->transaction_id = $this->setup_transaction_id();
564
565 // User based.
566 $this->ip = $this->setup_ip();
567 $this->customer_id = $this->setup_donor_id(); // Backward compatibility.
568 $this->donor_id = $this->setup_donor_id();
569 $this->user_id = $this->setup_user_id();
570 $this->email = $this->setup_email();
571 $this->user_info = $this->setup_user_info();
572 $this->address = $this->setup_address();
573 $this->first_name = $this->user_info['first_name'];
574 $this->last_name = $this->user_info['last_name'];
575 $this->title_prefix = isset( $this->user_info['title'] ) ? $this->user_info['title'] : '';
576
577 // Other Identifiers.
578 $this->form_title = $this->setup_form_title();
579 $this->form_id = $this->setup_form_id();
580 $this->price_id = $this->setup_price_id();
581 $this->key = $this->setup_payment_key();
582 $this->number = $this->setup_payment_number();
583
584 Give_Cache::set_group( $this->ID, get_object_vars( $this ), 'give-donations' );
585 } else {
586
587 foreach ( $donation_vars as $donation_var => $value ) {
588 $this->$donation_var = $value;
589 }
590 } // End if().
591
592 /**
593 * Fires after payment setup.
594 *
595 * Allow extensions to add items to this object via hook.
596 *
597 * @since 1.5
598 *
599 * @param Give_Payment $this Payment object.
600 * @param int $payment_id The ID of the payment.
601 */
602 do_action( 'give_setup_payment', $this, $payment_id );
603
604 return true;
605 }
606
607
608 /**
609 * Payment class object is storing various meta value in object parameter.
610 * So if user is updating payment meta but not updating payment object, then payment meta values will not reflect/changes on payment meta automatically
611 * and you can still access payment meta old value in any old payment object ( previously created ) which can cause to show or save wrong payment data.
612 * To prevent that user can use this function after updating any payment meta value ( in bulk or single update ).
613 *
614 * @since 1.6
615 * @access public
616 *
617 * @param int $payment_id Payment ID.
618 *
619 * @return void
620 */
621 public function update_payment_setup( $payment_id ) {
622 // Delete cache.
623 Give_Cache::delete_group( $this->ID,'give-donations' );
624
625 $this->setup_payment( $payment_id );
626 }
627
628 /**
629 * Create the base of a payment.
630 *
631 * @since 1.5
632 * @access private
633 *
634 * @return int|bool False on failure, the payment ID on success.
635 */
636 private function insert_payment() {
637
638 // Construct the payment title.
639 $payment_title = '';
640 if ( ! empty( $this->first_name ) && ! empty( $this->last_name ) ) {
641 $payment_title = $this->first_name . ' ' . $this->last_name;
642 } elseif ( ! empty( $this->first_name ) && empty( $this->last_name ) ) {
643 $payment_title = $this->first_name;
644 } elseif ( ! empty( $this->email ) && is_email( $this->email ) ) {
645 $payment_title = $this->email;
646 }
647
648 // Set Key.
649 if ( empty( $this->key ) ) {
650
651 $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
652 $this->key = strtolower( md5( $this->email . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'give', true ) ) ); // Unique key.
653 $this->pending['key'] = $this->key;
654 }
655
656 // Set IP.
657 if ( empty( $this->ip ) ) {
658
659 $this->ip = give_get_ip();
660 $this->pending['ip'] = $this->ip;
661
662 }
663
664 // @todo: payment data exist here only for backward compatibility
665 // issue: https://github.com/impress-org/give/issues/1132
666 $payment_data = array(
667 'price' => $this->total,
668 'date' => $this->date,
669 'user_email' => $this->email,
670 'purchase_key' => $this->key,
671 'form_title' => $this->form_title,
672 'form_id' => $this->form_id,
673 'donor_id' => $this->donor_id,
674 'price_id' => $this->price_id,
675 'currency' => $this->currency,
676 'user_info' => array(
677 'id' => $this->user_id,
678 'title' => $this->title_prefix,
679 'email' => $this->email,
680 'first_name' => $this->first_name,
681 'last_name' => $this->last_name,
682 'address' => $this->address,
683 ),
684 'status' => $this->status,
685 );
686
687 $args = apply_filters( 'give_insert_payment_args', array(
688 'post_title' => $payment_title,
689 'post_status' => $this->status,
690 'post_type' => 'give_payment',
691 'post_date' => ! empty( $this->date ) ? $this->date : null,
692 'post_date_gmt' => ! empty( $this->date ) ? get_gmt_from_date( $this->date ) : null,
693 'post_parent' => $this->parent_payment,
694 ), $payment_data );
695
696 // Create a blank payment.
697 $payment_id = wp_insert_post( $args );
698
699 if ( ! empty( $payment_id ) ) {
700
701 $this->ID = $payment_id;
702 $this->_ID = $payment_id;
703
704 $donor = new stdClass;
705
706 if ( did_action( 'give_pre_process_donation' ) && is_user_logged_in() ) {
707 $donor = new Give_Donor( get_current_user_id(), true );
708
709 // Donor is logged in but used a different email to purchase with so assign to their donor record.
710 if ( ! empty( $donor->id ) && $this->email !== $donor->email ) {
711 $donor->add_email( $this->email );
712 }
713 }
714
715 if ( empty( $donor->id ) ) {
716 $donor = new Give_Donor( $this->email );
717 }
718
719 if ( empty( $donor->id ) ) {
720
721 $donor_data = array(
722 'name' => ! is_email( $payment_title ) ? $this->first_name . ' ' . $this->last_name : '',
723 'email' => $this->email,
724 'user_id' => $this->user_id,
725 );
726
727 $donor->create( $donor_data );
728
729 }
730
731 /**
732 * Filters the donor object after donation is completed but before donor table is updated.
733 *
734 * @since 1.8.13
735 * @since 2.4.2 Moved location of filter to occur after donor is hydrated.
736 *
737 * @param Give_Donor $donor Donor object.
738 * @param int $payment_id Payment ID.
739 * @param array $payment_data Payment data array.
740 * @param array $args Payment args.
741 */
742 $donor = apply_filters( 'give_update_donor_information', $donor, $payment_id, $payment_data, $args );
743
744 // Update Donor Meta once donor is created.
745 $donor->update_meta( '_give_donor_first_name', $this->first_name );
746 $donor->update_meta( '_give_donor_last_name', $this->last_name );
747 $donor->update_meta( '_give_donor_title_prefix', $this->title_prefix );
748
749 $this->customer_id = $donor->id;
750 $this->pending['customer_id'] = $this->customer_id;
751 $donor->attach_payment( $this->ID, false );
752
753 $this->payment_meta = apply_filters( 'give_payment_meta', $this->payment_meta, $payment_data );
754
755 /**
756 * _give_payment_meta backward compatibility.
757 *
758 * @since 2.0.1
759 */
760 $custom_payment_meta = array_diff(
761 array_map( 'maybe_serialize', $this->payment_meta ),
762 array_map( 'maybe_serialize', $payment_data )
763 );
764
765 if ( ! empty( $custom_payment_meta ) ) {
766 give_doing_it_wrong( '_give_payment_meta', __( 'This custom meta key has been deprecated for performance reasons. Your custom meta data will still be stored but we recommend updating your code to store meta keys individually.', 'give' ), '2.0.0' );
767
768 $this->update_meta( '_give_payment_meta', array_map( 'maybe_unserialize', $custom_payment_meta ) );
769 }
770
771 $give_company = ( ! empty( $_REQUEST['give_company_name'] ) ? give_clean( $_REQUEST['give_company_name'] ) : '' );
772
773 // Check $page_url is not empty.
774 if ( $give_company ) {
775 give_update_meta( $payment_id, '_give_donation_company', $give_company );
776
777 $donor_id = absint( $donor->id );
778 if ( ! empty( $donor_id ) ) {
779 Give()->donor_meta->update_meta( $donor_id, '_give_donor_company', $give_company );
780 }
781 }
782
783 $this->new = true;
784 } // End if().
785
786 return $this->ID;
787
788 }
789
790 /**
791 * Save
792 *
793 * Once items have been set, an update is needed to save them to the database.
794 *
795 * @access public
796 *
797 * @return bool True of the save occurred, false if it failed or wasn't needed
798 */
799 public function save() {
800 $saved = false;
801
802 // Must have an ID.
803 if ( empty( $this->ID ) ) {
804
805 $payment_id = $this->insert_payment();
806
807 if ( false === $payment_id ) {
808 $saved = false;
809 } else {
810 $this->ID = $payment_id;
811 }
812 }
813
814 // Set ID if not matching.
815 if ( $this->ID !== $this->_ID ) {
816 $this->ID = $this->_ID;
817 }
818
819 // If we have something pending, let's save it.
820 if ( ! empty( $this->pending ) ) {
821
822 $total_increase = 0;
823 $total_decrease = 0;
824
825 foreach ( $this->pending as $key => $value ) {
826
827 switch ( $key ) {
828
829 case 'donations':
830 // Update totals for pending donations.
831 foreach ( $this->pending[ $key ] as $item ) {
832
833 $quantity = isset( $item['quantity'] ) ? $item['quantity'] : 1;
834 $price_id = isset( $item['price_id'] ) ? $item['price_id'] : 0;
835
836 switch ( $item['action'] ) {
837
838 case 'add':
839
840 $price = $item['price'];
841
842 if ( 'publish' === $this->status || 'complete' === $this->status ) {
843
844 // Add donation to logs.
845 $log_date = date_i18n( 'Y-m-d G:i:s', current_time( 'timestamp' ) );
846 give_record_donation_in_log( $item['id'], $this->ID, $price_id, $log_date );
847
848 $form = new Give_Donate_Form( $item['id'] );
849 $form->increase_sales( $quantity );
850 $form->increase_earnings( $price, $this->ID );
851
852 $total_increase += $price;
853 }
854 break;
855
856 case 'remove':
857 $this->delete_sales_logs();
858 if ( 'publish' === $this->status || 'complete' === $this->status ) {
859 $form = new Give_Donate_Form( $item['id'] );
860 $form->decrease_sales( $quantity );
861 $form->decrease_earnings( $item['amount'], $this->ID );
862
863 $total_decrease += $item['amount'];
864 }
865 break;
866
867 }// End switch().
868 }// End foreach().
869 break;
870
871 case 'status':
872 $this->update_status( $this->status );
873 break;
874
875 case 'gateway':
876 $this->update_meta( '_give_payment_gateway', $this->gateway );
877 break;
878
879 case 'mode':
880 $this->update_meta( '_give_payment_mode', $this->mode );
881 break;
882
883 case 'transaction_id':
884 $this->update_meta( '_give_payment_transaction_id', $this->transaction_id );
885 break;
886
887 case 'ip':
888 $this->update_meta( '_give_payment_donor_ip', $this->ip );
889 break;
890
891 case 'customer_id':
892 $this->update_meta( '_give_payment_donor_id', $this->customer_id );
893 break;
894
895 case 'form_title':
896 $this->update_meta( '_give_payment_form_title', $this->form_title );
897 break;
898
899 case 'form_id':
900 $this->update_meta( '_give_payment_form_id', $this->form_id );
901 break;
902
903 case 'price_id':
904 $this->update_meta( '_give_payment_price_id', $this->price_id );
905 break;
906
907 case 'first_name':
908 $this->update_meta( '_give_donor_billing_first_name', $this->first_name );
909 break;
910
911 case 'last_name':
912 $this->update_meta( '_give_donor_billing_last_name', $this->last_name );
913 break;
914
915 case 'currency':
916 $this->update_meta( '_give_payment_currency', $this->currency );
917 break;
918
919 case 'address':
920 if ( ! empty( $this->address ) ) {
921 foreach ( $this->address as $address_name => $address ) {
922 switch ( $address_name ) {
923 case 'line1':
924 $this->update_meta( '_give_donor_billing_address1', $address );
925 break;
926
927 case 'line2':
928 $this->update_meta( '_give_donor_billing_address2', $address );
929 break;
930
931 default:
932 $this->update_meta( "_give_donor_billing_{$address_name}", $address );
933 }
934 }
935 }
936 break;
937
938 case 'email':
939 $this->update_meta( '_give_payment_donor_email', $this->email );
940 break;
941
942 case 'title_prefix':
943 $this->update_meta( '_give_payment_donor_title_prefix', $this->title_prefix );
944 break;
945
946 case 'key':
947 $this->update_meta( '_give_payment_purchase_key', $this->key );
948 break;
949
950 case 'number':
951 // @todo: remove unused meta data.
952 // Core is using post_title to store donation serial code ( fi enabled ) instead this meta key.
953 // Do not use this meta key in your logic, can be remove in future
954 $this->update_meta( '_give_payment_number', $this->number );
955 break;
956
957 case 'date':
958 $args = array(
959 'ID' => $this->ID,
960 'post_date' => date( 'Y-m-d H:i:s', strtotime( $this->date ) ),
961 'post_date_gmt' => get_gmt_from_date( $this->date ),
962 'edit_date' => true,
963 );
964
965 wp_update_post( $args );
966 break;
967
968 case 'completed_date':
969 $this->update_meta( '_give_completed_date', $this->completed_date );
970 break;
971
972 case 'parent_payment':
973 $args = array(
974 'ID' => $this->ID,
975 'post_parent' => $this->parent_payment,
976 );
977
978 wp_update_post( $args );
979 break;
980
981 case 'total':
982 $this->update_meta( '_give_payment_total', give_sanitize_amount_for_db( $this->total ) );
983 break;
984
985 default:
986 /**
987 * Fires while saving payment.
988 *
989 * @since 1.7
990 *
991 * @param Give_Payment $this Payment object.
992 */
993 do_action( 'give_payment_save', $this, $key );
994 break;
995 } // End switch().
996 } // End foreach().
997
998 if ( 'pending' !== $this->status ) {
999
1000 $donor = new Give_Donor( $this->customer_id );
1001
1002 $total_change = $total_increase - $total_decrease;
1003 if ( $total_change < 0 ) {
1004
1005 $total_change = - ( $total_change );
1006
1007 // Decrease the donor's donation stats.
1008 $donor->decrease_value( $total_change );
1009 give_decrease_total_earnings( $total_change );
1010
1011 $donor->decrease_donation_count();
1012
1013 } elseif ( $total_change > 0 ) {
1014
1015 // Increase the donor's donation stats.
1016 $donor->increase_value( $total_change );
1017 give_increase_total_earnings( $total_change );
1018
1019 $donor->increase_purchase_count();
1020
1021 }
1022
1023 // Verify and update form meta based on the form status.
1024 give_set_form_closed_status( $this->form_id );
1025 }
1026
1027 $this->pending = array();
1028 $saved = true;
1029 } // End if().
1030
1031 if ( true === $saved ) {
1032 $this->setup_payment( $this->ID );
1033 }
1034
1035 return $saved;
1036 }
1037
1038 /**
1039 * Add a donation to a given payment
1040 *
1041 * @since 1.5
1042 * @access public
1043 *
1044 * @param int $form_id The donation form to add.
1045 * @param array $args Other arguments to pass to the function.
1046 * @param array $options List of donation options.
1047 *
1048 * @return bool True when successful, false otherwise
1049 */
1050 public function add_donation( $form_id = 0, $args = array(), $options = array() ) {
1051
1052 $donation = new Give_Donate_Form( $form_id );
1053
1054 // Bail if this post isn't a give donation form.
1055 if ( ! $donation || 'give_forms' !== $donation->post_type ) {
1056 return false;
1057 }
1058
1059 // Set some defaults.
1060 $defaults = array(
1061 'price' => false,
1062 'price_id' => false,
1063 );
1064
1065 $args = wp_parse_args( apply_filters( 'give_payment_add_donation_args', $args, $donation->ID ), $defaults );
1066
1067 // Allow overriding the price.
1068 if ( false !== $args['price'] ) {
1069 $donation_amount = $args['price'];
1070 } else {
1071
1072 // Deal with variable pricing.
1073 if ( give_has_variable_prices( $donation->ID ) ) {
1074 $prices = give_get_meta( $form_id, '_give_donation_levels', true );
1075 $donation_amount = '';
1076
1077 // Loop through prices.
1078 foreach ( $prices as $price ) {
1079 // Find a match between price_id and level_id.
1080 // First verify array keys exists THEN make the match.
1081 if (
1082 isset( $args['price_id'] ) &&
1083 isset( $price['_give_id']['level_id'] ) &&
1084 $args['price_id'] === (int) $price['_give_id']['level_id']
1085 ) {
1086 $donation_amount = $price['_give_amount'];
1087 }
1088 }
1089
1090 // Fallback to the lowest price point.
1091 if ( '' === $donation_amount ) {
1092 $donation_amount = give_get_lowest_price_option( $donation->ID );
1093 $args['price_id'] = give_get_lowest_price_id( $donation->ID );
1094 }
1095 } else {
1096 // Simple form price.
1097 $donation_amount = give_get_form_price( $donation->ID );
1098 }
1099 }
1100
1101 // Sanitizing the price here so we don't have a dozen calls later.
1102 $donation_amount = give_maybe_sanitize_amount( $donation_amount );
1103 $total = round( $donation_amount, give_get_price_decimals( $this->ID ) );
1104
1105 // Add Options.
1106 $default_options = array();
1107 if ( false !== $args['price_id'] ) {
1108 $default_options['price_id'] = (int) $args['price_id'];
1109 }
1110 $options = wp_parse_args( $options, $default_options );
1111
1112 // Do not allow totals to go negative.
1113 if ( $total < 0 ) {
1114 $total = 0;
1115 }
1116
1117 $donation = array(
1118 'name' => $donation->post_title,
1119 'id' => $donation->ID,
1120 'price' => round( $total, give_get_price_decimals( $this->ID ) ),
1121 'subtotal' => round( $total, give_get_price_decimals( $this->ID ) ),
1122 'price_id' => $args['price_id'],
1123 'action' => 'add',
1124 'options' => $options,
1125 );
1126
1127 $this->pending['donations'][] = $donation;
1128
1129 $this->increase_subtotal( $total );
1130
1131 return true;
1132
1133 }
1134
1135 /**
1136 * Remove a donation from the payment
1137 *
1138 * @since 1.5
1139 * @access public
1140 *
1141 * @param int $form_id The form ID to remove.
1142 * @param array $args Arguments to pass to identify (quantity, amount, price_id).
1143 *
1144 * @return bool If the item was removed or not
1145 */
1146 public function remove_donation( $form_id, $args = array() ) {
1147
1148 // Set some defaults.
1149 $defaults = array(
1150 'quantity' => 1,
1151 'price' => false,
1152 'price_id' => false,
1153 );
1154 $args = wp_parse_args( $args, $defaults );
1155
1156 $form = new Give_Donate_Form( $form_id );
1157
1158 // Bail if this post isn't a valid give donation form.
1159 if ( ! $form || 'give_forms' !== $form->post_type ) {
1160 return false;
1161 }
1162
1163 $pending_args = $args;
1164 $pending_args['id'] = $form_id;
1165 $pending_args['amount'] = $this->total;
1166 $pending_args['price_id'] = false !== $args['price_id'] ? (int) $args['price_id'] : false;
1167 $pending_args['quantity'] = $args['quantity'];
1168 $pending_args['action'] = 'remove';
1169
1170 $this->pending['donations'][] = $pending_args;
1171
1172 $this->decrease_subtotal( $this->total );
1173
1174 return true;
1175 }
1176
1177
1178 /**
1179 * Add a note to a payment
1180 *
1181 * @since 1.5
1182 * @access public
1183 *
1184 * @param string|bool $note The note to add.
1185 *
1186 * @return bool If the note was specified or not
1187 */
1188 public function add_note( $note = false ) {
1189 // Bail if no note specified.
1190 if ( ! $note ) {
1191 return false;
1192 }
1193
1194 give_insert_payment_note( $this->ID, $note );
1195 }
1196
1197 /**
1198 * Increase the payment's subtotal
1199 *
1200 * @since 1.5
1201 * @access private
1202 *
1203 * @param float $amount The amount to increase the payment subtotal by.
1204 *
1205 * @return void
1206 */
1207 private function increase_subtotal( $amount = 0.00 ) {
1208 $amount = (float) $amount;
1209 $this->subtotal += $amount;
1210
1211 $this->recalculate_total();
1212 }
1213
1214 /**
1215 * Decrease the payment's subtotal.
1216 *
1217 * @since 1.5
1218 * @access private
1219 *
1220 * @param float $amount The amount to decrease the payment subtotal by.
1221 *
1222 * @return void
1223 */
1224 private function decrease_subtotal( $amount = 0.00 ) {
1225 $amount = (float) $amount;
1226 $this->subtotal -= $amount;
1227
1228 if ( $this->subtotal < 0 ) {
1229 $this->subtotal = 0;
1230 }
1231
1232 $this->recalculate_total();
1233 }
1234
1235 /**
1236 * Set or update the total for a payment.
1237 *
1238 * @since 1.5
1239 * @since 2.1.4 reset total in pending property
1240 * @access private
1241 *
1242 * @return void
1243 */
1244 private function recalculate_total() {
1245 $this->pending['total'] = $this->total = $this->subtotal;
1246 }
1247
1248 /**
1249 * Set the payment status and run any status specific changes necessary.
1250 *
1251 * @since 1.5
1252 * @access public
1253 *
1254 * @param string|bool $status The status to set the payment to.
1255 *
1256 * @return bool $updated Returns if the status was successfully updated.
1257 */
1258 public function update_status( $status = false ) {
1259
1260 // standardize the 'complete(d)' status.
1261 if ( 'completed' === $status || 'complete' === $status ) {
1262 $status = 'publish';
1263 }
1264
1265 $old_status = ! empty( $this->old_status ) ? $this->old_status : false;
1266
1267 if ( $old_status === $status ) {
1268 return false; // Don't permit status changes that aren't changes.
1269 }
1270
1271 $do_change = apply_filters( 'give_should_update_payment_status', true, $this->ID, $status, $old_status );
1272
1273 $updated = false;
1274
1275 if ( $do_change ) {
1276
1277 /**
1278 * Fires before changing payment status.
1279 *
1280 * @since 1.5
1281 *
1282 * @param int $payment_id Payments ID.
1283 * @param string $status The new status.
1284 * @param string $old_status The old status.
1285 */
1286 do_action( 'give_before_payment_status_change', $this->ID, $status, $old_status );
1287
1288 $update_fields = array(
1289 'ID' => $this->ID,
1290 'post_status' => $status,
1291 'edit_date' => current_time( 'mysql' ),
1292 );
1293
1294 $updated = wp_update_post( apply_filters( 'give_update_payment_status_fields', $update_fields ) );
1295
1296 $all_payment_statuses = give_get_payment_statuses();
1297 $this->status_nicename = array_key_exists( $status, $all_payment_statuses ) ? $all_payment_statuses[ $status ] : ucfirst( $status );
1298
1299 // Process any specific status functions.
1300 $this->process_status( $status );
1301
1302 /**
1303 * Fires after changing payment status.
1304 *
1305 * @since 1.5
1306 *
1307 * @param int $payment_id Payment ID.
1308 * @param string $status The new status.
1309 * @param string $old_status The old status.
1310 */
1311 do_action( 'give_update_payment_status', $this->ID, $status, $old_status );
1312
1313 } // End if().
1314
1315 return $updated;
1316
1317 }
1318
1319 /**
1320 * Change the status of the payment to refunded, and run the necessary changes
1321 *
1322 * @since 1.5
1323 * @access public
1324 *
1325 * @return void
1326 */
1327 public function refund() {
1328 $this->old_status = $this->status;
1329 $this->status = 'refunded';
1330 $this->pending['status'] = $this->status;
1331
1332 $this->save();
1333 }
1334
1335 /**
1336 * Get a post meta item for the payment
1337 *
1338 * @since 1.5
1339 * @access public
1340 *
1341 * @param string $meta_key The Meta Key.
1342 * @param boolean $single Return single item or array.
1343 *
1344 * @return mixed The value from the post meta
1345 */
1346 public function get_meta( $meta_key = '_give_payment_meta', $single = true ) {
1347 if (
1348 ! has_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta' ) &&
1349 ! doing_filter( 'get_post_metadata' )
1350 ) {
1351 add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 );
1352 }
1353
1354 $meta = give_get_meta( $this->ID, $meta_key, $single );
1355
1356 /**
1357 * Filter the specific meta key value.
1358 *
1359 * @since 1.5
1360 */
1361 $meta = apply_filters( "give_get_payment_meta_{$meta_key}", $meta, $this->ID );
1362
1363 // Security check.
1364 if ( is_serialized( $meta ) ) {
1365 preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $meta, $matches );
1366 if ( ! empty( $matches ) ) {
1367 $meta = array();
1368 }
1369 }
1370
1371 /**
1372 * Filter the all meta keys.
1373 *
1374 * @since 1.5
1375 */
1376 return apply_filters( 'give_get_payment_meta', $meta, $this->ID, $meta_key );
1377 }
1378
1379 /**
1380 * Update the post meta
1381 *
1382 * @since 1.5
1383 * @access public
1384 *
1385 * @param string $meta_key The meta key to update.
1386 * @param string $meta_value The meta value.
1387 * @param string $prev_value Previous meta value.
1388 *
1389 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure
1390 */
1391 public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) {
1392 if ( empty( $meta_key ) ) {
1393 return false;
1394 }
1395
1396
1397 /**
1398 * Filter the single meta key while updating
1399 *
1400 * @since 1.5
1401 */
1402 $meta_value = apply_filters( "give_update_payment_meta_{$meta_key}", $meta_value, $this->ID );
1403
1404 return give_update_meta( $this->ID, $meta_key, $meta_value, $prev_value );
1405 }
1406
1407 /**
1408 * Process Donation Status.
1409 *
1410 * @param string $status Donation Status.
1411 *
1412 * @since 2.0.2
1413 * @access private
1414 *
1415 * @return void
1416 */
1417 private function process_status( $status ) {
1418 $process = true;
1419
1420 // Bailout, if changed from completed to preapproval/processing.
1421 // Bailout, if current status = previous status or status is publish.
1422 if (
1423 'preapproval' === $status ||
1424 'processing' === $status ||
1425 'publish' !== $this->old_status ||
1426 $status !== $this->status
1427 ) {
1428 $process = false;
1429 }
1430
1431 // Allow extensions to filter for their own payment types, Example: Recurring Payments.
1432 $process = apply_filters( "give_should_process_{$status}", $process, $this );
1433
1434 if ( false === $process ) {
1435 return;
1436 }
1437
1438 /**
1439 * Fires before processing donation status.
1440 *
1441 * @param Give_Payment $this Payment object.
1442 *
1443 * @since 1.5
1444 */
1445 do_action( "give_pre_{$status}_payment", $this );
1446
1447 $decrease_earnings = apply_filters( "give_decrease_earnings_on_{$status}", true, $this );
1448 $decrease_donor_value = apply_filters( "give_decrease_donor_value_on_{$status}", true, $this );
1449 $decrease_donation_count = apply_filters( "give_decrease_donors_donation_count_on_{$status}", true, $this );
1450
1451 $this->maybe_alter_stats( $decrease_earnings, $decrease_donor_value, $decrease_donation_count );
1452 $this->delete_sales_logs();
1453
1454 // @todo: Refresh only range related stat cache
1455 give_delete_donation_stats();
1456
1457 /**
1458 * Fires after processing donation status.
1459 *
1460 * @param Give_Payment $this Payment object.
1461 *
1462 * @since 1.5
1463 */
1464 do_action( "give_post_{$status}_payment", $this );
1465 }
1466
1467 /**
1468 * Used during the process of moving to refunded or pending, to decrement stats
1469 *
1470 * @since 1.5
1471 * @access private
1472 *
1473 * @param bool $alter_store_earnings If the method should alter the store earnings.
1474 * @param bool $alter_customer_value If the method should reduce the donor value.
1475 * @param bool $alter_customer_purchase_count If the method should reduce the donor's purchase count.
1476 *
1477 * @return void
1478 */
1479 private function maybe_alter_stats( $alter_store_earnings, $alter_customer_value, $alter_customer_purchase_count ) {
1480
1481 give_undo_donation( $this->ID );
1482
1483 // Decrease store earnings.
1484 if ( true === $alter_store_earnings ) {
1485 give_decrease_total_earnings( $this->total );
1486 }
1487
1488 // Decrement the stats for the donor.
1489 if ( ! empty( $this->customer_id ) ) {
1490
1491 $donor = new Give_Donor( $this->customer_id );
1492
1493 if ( true === $alter_customer_value ) {
1494 $donor->decrease_value( $this->total );
1495 }
1496
1497 if ( true === $alter_customer_purchase_count ) {
1498 $donor->decrease_donation_count();
1499 }
1500 }
1501
1502 }
1503
1504 /**
1505 * Delete sales logs for this donation
1506 *
1507 * @since 1.5
1508 * @access private
1509 *
1510 * @return void
1511 */
1512 private function delete_sales_logs() {
1513 // Remove related sale log entries.
1514 Give()->logs->delete_logs( $this->ID );
1515 }
1516
1517 /**
1518 * Setup functions only, these are not to be used by developers.
1519 * These functions exist only to allow the setup routine to be backwards compatible with our old
1520 * helper functions.
1521 *
1522 * These will run whenever setup_payment is called, which should only be called once.
1523 * To update an attribute, update it directly instead of re-running the setup routine
1524 */
1525
1526 /**
1527 * Setup the payment completed date
1528 *
1529 * @since 1.5
1530 * @access private
1531 *
1532 * @return string The date the payment was completed
1533 */
1534 private function setup_completed_date() {
1535 $payment = get_post( $this->ID );
1536
1537 if ( 'pending' === $payment->post_status || 'preapproved' === $payment->post_status ) {
1538 return false; // This payment was never completed.
1539 }
1540
1541 $date = ( $date = $this->get_meta( '_give_completed_date', true ) ) ? $date : $payment->modified_date;
1542
1543 return $date;
1544 }
1545
1546 /**
1547 * Setup the payment mode
1548 *
1549 * @since 1.5
1550 * @access private
1551 *
1552 * @return string The payment mode
1553 */
1554 private function setup_mode() {
1555 return $this->get_meta( '_give_payment_mode' );
1556 }
1557
1558 /**
1559 * Setup the payment import data
1560 *
1561 * @since 1.8.13
1562 * @access private
1563 *
1564 * @return bool The payment import
1565 */
1566 private function setup_import() {
1567 return (bool) $this->get_meta( '_give_payment_import' );
1568 }
1569
1570 /**
1571 * Setup the payment total
1572 *
1573 * @since 1.5
1574 * @access private
1575 *
1576 * @return float The payment total
1577 */
1578 private function setup_total() {
1579 $amount = $this->get_meta( '_give_payment_total', true );
1580
1581 return round( floatval( $amount ), give_get_price_decimals( $this->ID ) );
1582 }
1583
1584 /**
1585 * Setup the payment subtotal
1586 *
1587 * @since 1.5
1588 * @access private
1589 *
1590 * @return float The subtotal of the payment
1591 */
1592 private function setup_subtotal() {
1593 $subtotal = $this->total;
1594
1595 return $subtotal;
1596 }
1597
1598 /**
1599 * Setup the currency code
1600 *
1601 * @since 1.5
1602 * @since 2.0 Set currency from _give_payment_currency meta key
1603 * @access private
1604 *
1605 * @return string The currency for the payment
1606 */
1607 private function setup_currency() {
1608 $currency = $this->get_meta( '_give_payment_currency', true );
1609 $currency = ! empty( $currency ) ?
1610 $currency :
1611 /**
1612 * Filter the default donation currency
1613 *
1614 * @since 1.5
1615 */
1616 apply_filters(
1617 'give_payment_currency_default',
1618 give_get_currency( $this->form_id, $this ),
1619 $this
1620 );
1621
1622 return $currency;
1623 }
1624
1625 /**
1626 * Setup the gateway used for the payment
1627 *
1628 * @since 1.5
1629 * @access private
1630 *
1631 * @return string The gateway
1632 */
1633 private function setup_gateway() {
1634 $gateway = $this->get_meta( '_give_payment_gateway', true );
1635
1636 return $gateway;
1637 }
1638
1639 /**
1640 * Setup the donation ID
1641 *
1642 * @since 1.5
1643 * @access private
1644 *
1645 * @return string The donation ID
1646 */
1647 private function setup_transaction_id() {
1648 $transaction_id = $this->get_meta( '_give_payment_transaction_id', true );
1649
1650 if ( empty( $transaction_id ) ) {
1651 $gateway = $this->gateway;
1652 $transaction_id = apply_filters( "give_get_payment_transaction_id-{$gateway}", $this->ID );
1653 }
1654
1655 return $transaction_id;
1656 }
1657
1658 /**
1659 * Setup the IP Address for the payment
1660 *
1661 * @since 1.5
1662 * @since 2.0 Set ip address from _give_payment_donor_ip meta key
1663 * @access private
1664 *
1665 * @return string The IP address for the payment
1666 */
1667 private function setup_ip() {
1668 $ip = $this->get_meta( '_give_payment_donor_ip', true );
1669
1670 return $ip;
1671 }
1672
1673 /**
1674 * Setup the donor ID.
1675 *
1676 * @since 1.5
1677 * @since 2.0 Set id from _give_payment_donor_id meta key
1678 * @access private
1679 *
1680 * @return int The Donor ID.
1681 */
1682 private function setup_donor_id() {
1683 $donor_id = $this->get_meta( '_give_payment_donor_id', true );
1684
1685 return $donor_id;
1686 }
1687
1688 /**
1689 * Setup the User ID associated with the donation
1690 *
1691 * @since 1.5
1692 * @since 2.0 Get user id connect to donor from donor table instead of payment meta.
1693 *
1694 * @access private
1695 *
1696 * @return int The User ID
1697 */
1698 private function setup_user_id() {
1699
1700 $donor = Give()->donors->get_donor_by( 'id', $this->donor_id );
1701 $user_id = $donor ? absint( $donor->user_id ) : 0;
1702
1703
1704 return $user_id;
1705 }
1706
1707 /**
1708 * Setup the email address for the donation.
1709 *
1710 * @since 1.5
1711 * @since 2.0 Set email from _give_payment_donor_email meta key
1712 *
1713 * @access private
1714 *
1715 * @return string The email address for the payment.
1716 */
1717 private function setup_email() {
1718 $email = $this->get_meta( '_give_payment_donor_email', true );
1719
1720 if ( empty( $email ) && $this->customer_id ) {
1721 $email = Give()->donors->get_column( 'email', $this->customer_id );
1722 }
1723
1724 return $email;
1725 }
1726
1727 /**
1728 * Setup the user info.
1729 *
1730 * @since 1.5
1731 * @access private
1732 *
1733 * @return array The user info associated with the payment.
1734 */
1735 private function setup_user_info() {
1736 $defaults = array(
1737 'title' => $this->title_prefix,
1738 'first_name' => $this->first_name,
1739 'last_name' => $this->last_name,
1740 );
1741
1742 $user_info = isset( $this->payment_meta['user_info'] ) ? $this->payment_meta['user_info'] : array();
1743
1744 if ( is_serialized( $user_info ) ) {
1745 preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $user_info, $matches );
1746 if ( ! empty( $matches ) ) {
1747 $user_info = array();
1748 }
1749 }
1750
1751 $user_info = wp_parse_args( $user_info, $defaults );
1752
1753 if ( empty( $user_info ) ) {
1754 // Get the donor, but only if it's been created.
1755 $donor = new Give_Donor( $this->customer_id );
1756
1757 if ( $donor->id > 0 ) {
1758 $user_info = array(
1759 'first_name' => $donor->get_first_name(),
1760 'last_name' => $donor->get_last_name(),
1761 'email' => $donor->email,
1762 'discount' => 'none',
1763 );
1764 }
1765 } else {
1766 // Get the donor, but only if it's been created.
1767 $donor = new Give_Donor( $this->customer_id );
1768
1769 if ( $donor->id > 0 ) {
1770 foreach ( $user_info as $key => $value ) {
1771 if ( ! empty( $value ) ) {
1772 continue;
1773 }
1774
1775 switch ( $key ) {
1776 case 'title':
1777 $user_info[ $key ] = Give()->donor_meta->get_meta( $donor->id, '_give_donor_title_prefix', true );
1778 break;
1779
1780 case 'first_name':
1781 $user_info[ $key ] = $donor->get_first_name();
1782 break;
1783
1784 case 'last_name':
1785 $user_info[ $key ] = $donor->get_last_name();
1786 break;
1787
1788 case 'email':
1789 $user_info[ $key ] = $donor->email;
1790 break;
1791 }
1792 }
1793 }
1794 }// End if().
1795
1796 return $user_info;
1797
1798 }
1799
1800 /**
1801 * Setup the Address for the payment.
1802 *
1803 * @since 1.5
1804 * @access private
1805 *
1806 * @return array The Address information for the payment.
1807 */
1808 private function setup_address() {
1809 $address['line1'] = give_get_meta( $this->ID, '_give_donor_billing_address1', true, '' );
1810 $address['line2'] = give_get_meta( $this->ID, '_give_donor_billing_address2', true, '' );
1811 $address['city'] = give_get_meta( $this->ID, '_give_donor_billing_city', true, '' );
1812 $address['state'] = give_get_meta( $this->ID, '_give_donor_billing_state', true, '' );
1813 $address['zip'] = give_get_meta( $this->ID, '_give_donor_billing_zip', true, '' );
1814 $address['country'] = give_get_meta( $this->ID, '_give_donor_billing_country', true, '' );
1815
1816 return $address;
1817 }
1818
1819 /**
1820 * Setup the form title.
1821 *
1822 * @since 1.5
1823 * @access private
1824 *
1825 * @return string The Form Title.
1826 */
1827 private function setup_form_title() {
1828
1829 $form_id = $this->get_meta( '_give_payment_form_title', true );
1830
1831 return $form_id;
1832 }
1833
1834 /**
1835 * Setup the form ID.
1836 *
1837 * @since 1.5
1838 * @access private
1839 *
1840 * @return int The Form ID
1841 */
1842 private function setup_form_id() {
1843
1844 $form_id = $this->get_meta( '_give_payment_form_id', true );
1845
1846 return $form_id;
1847 }
1848
1849 /**
1850 * Setup the price ID.
1851 *
1852 * @since 1.5
1853 * @access private
1854 *
1855 * @return int The Form Price ID.
1856 */
1857 private function setup_price_id() {
1858 $price_id = $this->get_meta( '_give_payment_price_id', true );
1859
1860 return $price_id;
1861 }
1862
1863 /**
1864 * Setup the payment key.
1865 *
1866 * @since 1.5
1867 * @access private
1868 *
1869 * @return string The Payment Key.
1870 */
1871 private function setup_payment_key() {
1872 $key = $this->get_meta( '_give_payment_purchase_key', true );
1873
1874 return $key;
1875 }
1876
1877 /**
1878 * Setup the payment number.
1879 *
1880 * @since 1.5
1881 * @access private
1882 *
1883 * @return int|string Integer by default, or string if sequential order numbers is enabled.
1884 */
1885 private function setup_payment_number() {
1886 return $this->get_serial_code();
1887 }
1888
1889 /**
1890 * Converts this object into an array for special cases.
1891 *
1892 * @access public
1893 *
1894 * @return array The payment object as an array.
1895 */
1896 public function array_convert() {
1897 return get_object_vars( $this );
1898 }
1899
1900
1901 /**
1902 * Flag to check if donation is completed or not.
1903 *
1904 * @since 1.8
1905 * @access public
1906 *
1907 * @return bool
1908 */
1909 public function is_completed() {
1910 return ( 'publish' === $this->status && $this->completed_date );
1911 }
1912
1913 /**
1914 * Retrieve payment completion date.
1915 *
1916 * @since 1.5
1917 * @access private
1918 *
1919 * @return string Date payment was completed.
1920 */
1921 private function get_completed_date() {
1922 return apply_filters( 'give_payment_completed_date', $this->completed_date, $this->ID, $this );
1923 }
1924
1925 /**
1926 * Retrieve payment subtotal.
1927 *
1928 * @since 1.5
1929 * @access private
1930 *
1931 * @return float Payment subtotal.
1932 */
1933 private function get_subtotal() {
1934 return apply_filters( 'give_get_payment_subtotal', $this->subtotal, $this->ID, $this );
1935 }
1936
1937 /**
1938 * Retrieve payment currency.
1939 *
1940 * @since 1.5
1941 * @access private
1942 *
1943 * @return string Payment currency code.
1944 */
1945 private function get_currency() {
1946 return apply_filters( 'give_payment_currency_code', $this->currency, $this->ID, $this );
1947 }
1948
1949 /**
1950 * Retrieve payment gateway.
1951 *
1952 * @since 1.5
1953 * @access private
1954 *
1955 * @return string Gateway used.
1956 */
1957 private function get_gateway() {
1958 return apply_filters( 'give_payment_gateway', $this->gateway, $this->ID, $this );
1959 }
1960
1961 /**
1962 * Retrieve donation ID.
1963 *
1964 * @since 1.5
1965 * @access private
1966 *
1967 * @return string Donation ID from merchant processor.
1968 */
1969 private function get_transaction_id() {
1970 return apply_filters( 'give_get_payment_transaction_id', $this->transaction_id, $this->ID, $this );
1971 }
1972
1973 /**
1974 * Retrieve payment IP
1975 *
1976 * @since 1.5
1977 * @access private
1978 *
1979 * @return string Payment IP address
1980 */
1981 private function get_ip() {
1982 return apply_filters( 'give_payment_user_ip', $this->ip, $this->ID, $this );
1983 }
1984
1985 /**
1986 * Retrieve payment donor ID.
1987 *
1988 * @since 1.5
1989 * @access private
1990 *
1991 * @return int Payment donor ID.
1992 */
1993 private function get_donor_id() {
1994 return apply_filters( 'give_payment_customer_id', $this->customer_id, $this->ID, $this );
1995 }
1996
1997 /**
1998 * Retrieve payment user ID.
1999 *
2000 * @since 1.5
2001 * @access private
2002 *
2003 * @return int Payment user ID.
2004 */
2005 private function get_user_id() {
2006 return apply_filters( 'give_payment_user_id', $this->user_id, $this->ID, $this );
2007 }
2008
2009 /**
2010 * Retrieve payment email.
2011 *
2012 * @since 1.5
2013 * @access private
2014 *
2015 * @return string Payment donor email.
2016 */
2017 private function get_email() {
2018 return apply_filters( 'give_payment_user_email', $this->email, $this->ID, $this );
2019 }
2020
2021 /**
2022 * Retrieve payment user info.
2023 *
2024 * @since 1.5
2025 * @access private
2026 *
2027 * @return array Payment user info.
2028 */
2029 private function get_user_info() {
2030 return apply_filters( 'give_payment_meta_user_info', $this->user_info, $this->ID, $this );
2031 }
2032
2033 /**
2034 * Retrieve payment billing address.
2035 *
2036 * @since 1.5
2037 * @access private
2038 *
2039 * @return array Payment billing address.
2040 */
2041 private function get_address() {
2042 return apply_filters( 'give_payment_address', $this->address, $this->ID, $this );
2043 }
2044
2045 /**
2046 * Retrieve payment key.
2047 *
2048 * @since 1.5
2049 * @access private
2050 *
2051 * @return string Payment key.
2052 */
2053 private function get_key() {
2054 return apply_filters( 'give_payment_key', $this->key, $this->ID, $this );
2055 }
2056
2057 /**
2058 * Retrieve payment form id
2059 *
2060 * @since 1.5
2061 * @access private
2062 *
2063 * @return string Payment form id
2064 */
2065 private function get_form_id() {
2066 return apply_filters( 'give_payment_form_id', $this->form_id, $this->ID, $this );
2067 }
2068
2069 /**
2070 * Retrieve payment number
2071 *
2072 * @since 1.5
2073 * @access private
2074 *
2075 * @return int|string Payment number
2076 */
2077 private function get_number() {
2078 return apply_filters( 'give_payment_number', $this->number, $this->ID, $this );
2079 }
2080
2081 /**
2082 * Get serial code
2083 *
2084 * @since 2.1
2085 *
2086 * @param array $args List of arguments.
2087 *
2088 * @return string
2089 */
2090 public function get_serial_code( $args = array() ) {
2091 return Give()->seq_donation_number->get_serial_code( $this, $args );
2092 }
2093 }
2094