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