PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.1.7
GiveWP – Donation Plugin and Fundraising Platform v2.1.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 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
2058 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 case 'total':
958 $this->update_meta( '_give_payment_total', give_sanitize_amount_for_db( $this->total ) );
959 break;
960
961 default:
962 /**
963 * Fires while saving payment.
964 *
965 * @since 1.7
966 *
967 * @param Give_Payment $this Payment object.
968 */
969 do_action( 'give_payment_save', $this, $key );
970 break;
971 }// End switch().
972 }// End foreach().
973
974 if ( 'pending' !== $this->status ) {
975
976 $donor = new Give_Donor( $this->customer_id );
977
978 $total_change = $total_increase - $total_decrease;
979 if ( $total_change < 0 ) {
980
981 $total_change = - ( $total_change );
982
983 // Decrease the donor's donation stats.
984 $donor->decrease_value( $total_change );
985 give_decrease_total_earnings( $total_change );
986
987 $donor->decrease_donation_count();
988
989 } elseif ( $total_change > 0 ) {
990
991 // Increase the donor's donation stats.
992 $donor->increase_value( $total_change );
993 give_increase_total_earnings( $total_change );
994
995 $donor->increase_purchase_count();
996
997 }
998
999 // Verify and update form meta based on the form status.
1000 give_set_form_closed_status( $this->form_id );
1001 }
1002
1003 $this->pending = array();
1004 $saved = true;
1005 }// End if().
1006
1007 if ( true === $saved ) {
1008 $this->setup_payment( $this->ID );
1009 }
1010
1011 return $saved;
1012 }
1013
1014 /**
1015 * Add a donation to a given payment
1016 *
1017 * @since 1.5
1018 * @access public
1019 *
1020 * @param int $form_id The donation form to add
1021 * @param array $args Other arguments to pass to the function
1022 * @param array $options List of donation options
1023 *
1024 * @return bool True when successful, false otherwise
1025 */
1026 public function add_donation( $form_id = 0, $args = array(), $options = array() ) {
1027
1028 $donation = new Give_Donate_Form( $form_id );
1029
1030 // Bail if this post isn't a give donation form.
1031 if ( ! $donation || $donation->post_type !== 'give_forms' ) {
1032 return false;
1033 }
1034
1035 // Set some defaults.
1036 $defaults = array(
1037 'price' => false,
1038 'price_id' => false,
1039 );
1040
1041 $args = wp_parse_args( apply_filters( 'give_payment_add_donation_args', $args, $donation->ID ), $defaults );
1042
1043 // Allow overriding the price.
1044 if ( false !== $args['price'] ) {
1045 $donation_amount = $args['price'];
1046 } else {
1047
1048 // Deal with variable pricing.
1049 if ( give_has_variable_prices( $donation->ID ) ) {
1050 $prices = give_get_meta( $form_id, '_give_donation_levels', true );
1051 $donation_amount = '';
1052 // Loop through prices.
1053 foreach ( $prices as $price ) {
1054 // Find a match between price_id and level_id.
1055 // First verify array keys exists THEN make the match.
1056 if ( ( isset( $args['price_id'] ) && isset( $price['_give_id']['level_id'] ) )
1057 && $args['price_id'] == $price['_give_id']['level_id']
1058 ) {
1059 $donation_amount = $price['_give_amount'];
1060 }
1061 }
1062 // Fallback to the lowest price point.
1063 if ( $donation_amount == '' ) {
1064 $donation_amount = give_get_lowest_price_option( $donation->ID );
1065 $args['price_id'] = give_get_lowest_price_id( $donation->ID );
1066 }
1067 } else {
1068 // Simple form price.
1069 $donation_amount = give_get_form_price( $donation->ID );
1070 }
1071 }
1072
1073 // Sanitizing the price here so we don't have a dozen calls later.
1074 $donation_amount = give_maybe_sanitize_amount( $donation_amount );
1075 $total = round( $donation_amount, give_get_price_decimals( $this->ID ) );
1076
1077 // Add Options.
1078 $default_options = array();
1079 if ( false !== $args['price_id'] ) {
1080 $default_options['price_id'] = (int) $args['price_id'];
1081 }
1082 $options = wp_parse_args( $options, $default_options );
1083
1084 // Do not allow totals to go negative.
1085 if ( $total < 0 ) {
1086 $total = 0;
1087 }
1088
1089 $donation = array(
1090 'name' => $donation->post_title,
1091 'id' => $donation->ID,
1092 'price' => round( $total, give_get_price_decimals( $this->ID ) ),
1093 'subtotal' => round( $total, give_get_price_decimals( $this->ID ) ),
1094 'price_id' => $args['price_id'],
1095 'action' => 'add',
1096 'options' => $options,
1097 );
1098
1099 $this->pending['donations'][] = $donation;
1100
1101 $this->increase_subtotal( $total );
1102
1103 return true;
1104
1105 }
1106
1107 /**
1108 * Remove a donation from the payment
1109 *
1110 * @since 1.5
1111 * @access public
1112 *
1113 * @param int $form_id The form ID to remove
1114 * @param array $args Arguments to pass to identify (quantity, amount, price_id)
1115 *
1116 * @return bool If the item was removed or not
1117 */
1118 public function remove_donation( $form_id, $args = array() ) {
1119
1120 // Set some defaults.
1121 $defaults = array(
1122 'quantity' => 1,
1123 'price' => false,
1124 'price_id' => false,
1125 );
1126 $args = wp_parse_args( $args, $defaults );
1127
1128 $form = new Give_Donate_Form( $form_id );
1129
1130 // Bail if this post isn't a valid give donation form.
1131 if ( ! $form || $form->post_type !== 'give_forms' ) {
1132 return false;
1133 }
1134
1135 $pending_args = $args;
1136 $pending_args['id'] = $form_id;
1137 $pending_args['amount'] = $this->total;
1138 $pending_args['price_id'] = false !== $args['price_id'] ? (int) $args['price_id'] : false;
1139 $pending_args['quantity'] = $args['quantity'];
1140 $pending_args['action'] = 'remove';
1141
1142 $this->pending['donations'][] = $pending_args;
1143
1144 $this->decrease_subtotal( $this->total );
1145
1146 return true;
1147 }
1148
1149
1150 /**
1151 * Add a note to a payment
1152 *
1153 * @since 1.5
1154 * @access public
1155 *
1156 * @param string|bool $note The note to add
1157 *
1158 * @return bool If the note was specified or not
1159 */
1160 public function add_note( $note = false ) {
1161 // Bail if no note specified.
1162 if ( ! $note ) {
1163 return false;
1164 }
1165
1166 give_insert_payment_note( $this->ID, $note );
1167 }
1168
1169 /**
1170 * Increase the payment's subtotal
1171 *
1172 * @since 1.5
1173 * @access private
1174 *
1175 * @param float $amount The amount to increase the payment subtotal by.
1176 *
1177 * @return void
1178 */
1179 private function increase_subtotal( $amount = 0.00 ) {
1180 $amount = (float) $amount;
1181 $this->subtotal += $amount;
1182
1183 $this->recalculate_total();
1184 }
1185
1186 /**
1187 * Decrease the payment's subtotal.
1188 *
1189 * @since 1.5
1190 * @access private
1191 *
1192 * @param float $amount The amount to decrease the payment subtotal by.
1193 *
1194 * @return void
1195 */
1196 private function decrease_subtotal( $amount = 0.00 ) {
1197 $amount = (float) $amount;
1198 $this->subtotal -= $amount;
1199
1200 if ( $this->subtotal < 0 ) {
1201 $this->subtotal = 0;
1202 }
1203
1204 $this->recalculate_total();
1205 }
1206
1207 /**
1208 * Set or update the total for a payment.
1209 *
1210 * @since 1.5
1211 * @since 2.1.4 reset total in pending property
1212 * @access private
1213 *
1214 * @return void
1215 */
1216 private function recalculate_total() {
1217 $this->pending['total'] = $this->total = $this->subtotal;
1218 }
1219
1220 /**
1221 * Set the payment status and run any status specific changes necessary.
1222 *
1223 * @since 1.5
1224 * @access public
1225 *
1226 * @param string|bool $status The status to set the payment to.
1227 *
1228 * @return bool $updated Returns if the status was successfully updated.
1229 */
1230 public function update_status( $status = false ) {
1231
1232 // standardize the 'complete(d)' status.
1233 if ( $status == 'completed' || $status == 'complete' ) {
1234 $status = 'publish';
1235 }
1236
1237 $old_status = ! empty( $this->old_status ) ? $this->old_status : false;
1238
1239 if ( $old_status === $status ) {
1240 return false; // Don't permit status changes that aren't changes.
1241 }
1242
1243 $do_change = apply_filters( 'give_should_update_payment_status', true, $this->ID, $status, $old_status );
1244
1245 $updated = false;
1246
1247 if ( $do_change ) {
1248
1249 /**
1250 * Fires before changing payment status.
1251 *
1252 * @since 1.5
1253 *
1254 * @param int $payment_id Payments ID.
1255 * @param string $status The new status.
1256 * @param string $old_status The old status.
1257 */
1258 do_action( 'give_before_payment_status_change', $this->ID, $status, $old_status );
1259
1260 $update_fields = array(
1261 'ID' => $this->ID,
1262 'post_status' => $status,
1263 'edit_date' => current_time( 'mysql' ),
1264 );
1265
1266 $updated = wp_update_post( apply_filters( 'give_update_payment_status_fields', $update_fields ) );
1267
1268 $all_payment_statuses = give_get_payment_statuses();
1269 $this->status_nicename = array_key_exists( $status, $all_payment_statuses ) ? $all_payment_statuses[ $status ] : ucfirst( $status );
1270
1271 // Process any specific status functions.
1272 $this->process_status( $status );
1273
1274 /**
1275 * Fires after changing payment status.
1276 *
1277 * @since 1.5
1278 *
1279 * @param int $payment_id Payment ID.
1280 * @param string $status The new status.
1281 * @param string $old_status The old status.
1282 */
1283 do_action( 'give_update_payment_status', $this->ID, $status, $old_status );
1284
1285 }// End if().
1286
1287 return $updated;
1288
1289 }
1290
1291 /**
1292 * Change the status of the payment to refunded, and run the necessary changes
1293 *
1294 * @since 1.5
1295 * @access public
1296 *
1297 * @return void
1298 */
1299 public function refund() {
1300 $this->old_status = $this->status;
1301 $this->status = 'refunded';
1302 $this->pending['status'] = $this->status;
1303
1304 $this->save();
1305 }
1306
1307 /**
1308 * Get a post meta item for the payment
1309 *
1310 * @since 1.5
1311 * @access public
1312 *
1313 * @param string $meta_key The Meta Key
1314 * @param boolean $single Return single item or array
1315 *
1316 * @return mixed The value from the post meta
1317 */
1318 public function get_meta( $meta_key = '_give_payment_meta', $single = true ) {
1319 if( ! has_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta' ) && ! doing_filter( 'get_post_metadata' ) ) {
1320 add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 );
1321 }
1322
1323 $meta = give_get_meta( $this->ID, $meta_key, $single );
1324
1325 /**
1326 * Filter the specific meta key value.
1327 *
1328 * @since 1.5
1329 */
1330 $meta = apply_filters( "give_get_payment_meta_{$meta_key}", $meta, $this->ID );
1331
1332 // Security check.
1333 if ( is_serialized( $meta ) ) {
1334 preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $meta, $matches );
1335 if ( ! empty( $matches ) ) {
1336 $meta = array();
1337 }
1338 }
1339
1340 /**
1341 * Filter the all meta keys.
1342 *
1343 * @since 1.5
1344 */
1345 return apply_filters( 'give_get_payment_meta', $meta, $this->ID, $meta_key );
1346 }
1347
1348 /**
1349 * Update the post meta
1350 *
1351 * @since 1.5
1352 * @access public
1353 *
1354 * @param string $meta_key The meta key to update
1355 * @param string $meta_value The meta value
1356 * @param string $prev_value Previous meta value
1357 *
1358 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure
1359 */
1360 public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) {
1361 if ( empty( $meta_key ) ) {
1362 return false;
1363 }
1364
1365
1366 /**
1367 * Filter the single meta key while updating
1368 *
1369 * @since 1.5
1370 */
1371 $meta_value = apply_filters( "give_update_payment_meta_{$meta_key}", $meta_value, $this->ID );
1372
1373 return give_update_meta( $this->ID, $meta_key, $meta_value, $prev_value );
1374 }
1375
1376 /**
1377 * Process Donation Status.
1378 *
1379 * @param string $status Donation Status.
1380 *
1381 * @since 2.0.2
1382 * @access private
1383 *
1384 * @return void
1385 */
1386 private function process_status( $status ) {
1387 $process = true;
1388
1389 // Bailout, if changed from completed to preapproval/processing.
1390 // Bailout, if current status = previous status or status is publish.
1391 if (
1392 'preapproval' === $status ||
1393 'processing' === $status ||
1394 'publish' !== $this->old_status ||
1395 $status !== $this->status
1396 ) {
1397 $process = false;
1398 }
1399
1400 // Allow extensions to filter for their own payment types, Example: Recurring Payments.
1401 $process = apply_filters( "give_should_process_{$status}", $process, $this );
1402
1403 if ( false === $process ) {
1404 return;
1405 }
1406
1407 /**
1408 * Fires before processing donation status.
1409 *
1410 * @param Give_Payment $this Payment object.
1411 *
1412 * @since 1.5
1413 */
1414 do_action( "give_pre_{$status}_payment", $this );
1415
1416 $decrease_earnings = apply_filters( "give_decrease_earnings_on_{$status}", true, $this );
1417 $decrease_donor_value = apply_filters( "give_decrease_donor_value_on_{$status}", true, $this );
1418 $decrease_donation_count = apply_filters( "give_decrease_donors_donation_count_on_{$status}", true, $this );
1419
1420 $this->maybe_alter_stats( $decrease_earnings, $decrease_donor_value, $decrease_donation_count );
1421 $this->delete_sales_logs();
1422
1423 // @todo: Refresh only range related stat cache
1424 give_delete_donation_stats();
1425
1426 /**
1427 * Fires after processing donation status.
1428 *
1429 * @param Give_Payment $this Payment object.
1430 *
1431 * @since 1.5
1432 */
1433 do_action( "give_post_{$status}_payment", $this );
1434 }
1435
1436 /**
1437 * Used during the process of moving to refunded or pending, to decrement stats
1438 *
1439 * @since 1.5
1440 * @access private
1441 *
1442 * @param bool $alter_store_earnings If the method should alter the store earnings
1443 * @param bool $alter_customer_value If the method should reduce the donor value
1444 * @param bool $alter_customer_purchase_count If the method should reduce the donor's purchase count
1445 *
1446 * @return void
1447 */
1448 private function maybe_alter_stats( $alter_store_earnings, $alter_customer_value, $alter_customer_purchase_count ) {
1449
1450 give_undo_donation( $this->ID );
1451
1452 // Decrease store earnings.
1453 if ( true === $alter_store_earnings ) {
1454 give_decrease_total_earnings( $this->total );
1455 }
1456
1457 // Decrement the stats for the donor.
1458 if ( ! empty( $this->customer_id ) ) {
1459
1460 $donor = new Give_Donor( $this->customer_id );
1461
1462 if ( true === $alter_customer_value ) {
1463 $donor->decrease_value( $this->total );
1464 }
1465
1466 if ( true === $alter_customer_purchase_count ) {
1467 $donor->decrease_donation_count();
1468 }
1469 }
1470
1471 }
1472
1473 /**
1474 * Delete sales logs for this donation
1475 *
1476 * @since 1.5
1477 * @access private
1478 *
1479 * @return void
1480 */
1481 private function delete_sales_logs() {
1482 // Remove related sale log entries.
1483 Give()->logs->delete_logs( $this->ID );
1484 }
1485
1486 /**
1487 * Setup functions only, these are not to be used by developers.
1488 * These functions exist only to allow the setup routine to be backwards compatible with our old
1489 * helper functions.
1490 *
1491 * These will run whenever setup_payment is called, which should only be called once.
1492 * To update an attribute, update it directly instead of re-running the setup routine
1493 */
1494
1495 /**
1496 * Setup the payment completed date
1497 *
1498 * @since 1.5
1499 * @access private
1500 *
1501 * @return string The date the payment was completed
1502 */
1503 private function setup_completed_date() {
1504 $payment = get_post( $this->ID );
1505
1506 if ( 'pending' == $payment->post_status || 'preapproved' == $payment->post_status ) {
1507 return false; // This payment was never completed.
1508 }
1509
1510 $date = ( $date = $this->get_meta( '_give_completed_date', true ) ) ? $date : $payment->modified_date;
1511
1512 return $date;
1513 }
1514
1515 /**
1516 * Setup the payment mode
1517 *
1518 * @since 1.5
1519 * @access private
1520 *
1521 * @return string The payment mode
1522 */
1523 private function setup_mode() {
1524 return $this->get_meta( '_give_payment_mode' );
1525 }
1526
1527 /**
1528 * Setup the payment import data
1529 *
1530 * @since 1.8.13
1531 * @access private
1532 *
1533 * @return bool The payment import
1534 */
1535 private function setup_import() {
1536 return (bool) $this->get_meta( '_give_payment_import' );
1537 }
1538
1539 /**
1540 * Setup the payment total
1541 *
1542 * @since 1.5
1543 * @access private
1544 *
1545 * @return float The payment total
1546 */
1547 private function setup_total() {
1548 $amount = $this->get_meta( '_give_payment_total', true );
1549
1550 return round( floatval( $amount ), give_get_price_decimals( $this->ID ) );
1551 }
1552
1553 /**
1554 * Setup the payment subtotal
1555 *
1556 * @since 1.5
1557 * @access private
1558 *
1559 * @return float The subtotal of the payment
1560 */
1561 private function setup_subtotal() {
1562 $subtotal = $this->total;
1563
1564 return $subtotal;
1565 }
1566
1567 /**
1568 * Setup the currency code
1569 *
1570 * @since 1.5
1571 * @since 2.0 Set currency from _give_payment_currency meta key
1572 * @access private
1573 *
1574 * @return string The currency for the payment
1575 */
1576 private function setup_currency() {
1577 $currency = $this->get_meta( '_give_payment_currency', true );
1578 $currency = ! empty( $currency ) ?
1579 $currency :
1580 /**
1581 * Filter the default donation currency
1582 *
1583 * @since 1.5
1584 */
1585 apply_filters(
1586 'give_payment_currency_default',
1587 give_get_currency( $this->form_id, $this ),
1588 $this
1589 );
1590
1591 return $currency;
1592 }
1593
1594 /**
1595 * Setup the gateway used for the payment
1596 *
1597 * @since 1.5
1598 * @access private
1599 *
1600 * @return string The gateway
1601 */
1602 private function setup_gateway() {
1603 $gateway = $this->get_meta( '_give_payment_gateway', true );
1604
1605 return $gateway;
1606 }
1607
1608 /**
1609 * Setup the donation ID
1610 *
1611 * @since 1.5
1612 * @access private
1613 *
1614 * @return string The donation ID
1615 */
1616 private function setup_transaction_id() {
1617 $transaction_id = $this->get_meta( '_give_payment_transaction_id', true );
1618
1619 if ( empty( $transaction_id ) ) {
1620 $gateway = $this->gateway;
1621 $transaction_id = apply_filters( "give_get_payment_transaction_id-{$gateway}", $this->ID );
1622 }
1623
1624 return $transaction_id;
1625 }
1626
1627 /**
1628 * Setup the IP Address for the payment
1629 *
1630 * @since 1.5
1631 * @since 2.0 Set ip address from _give_payment_donor_ip meta key
1632 * @access private
1633 *
1634 * @return string The IP address for the payment
1635 */
1636 private function setup_ip() {
1637 $ip = $this->get_meta( '_give_payment_donor_ip', true );
1638
1639 return $ip;
1640 }
1641
1642 /**
1643 * Setup the donor ID.
1644 *
1645 * @since 1.5
1646 * @since 2.0 Set id from _give_payment_donor_id meta key
1647 * @access private
1648 *
1649 * @return int The Donor ID.
1650 */
1651 private function setup_donor_id() {
1652 $donor_id = $this->get_meta( '_give_payment_donor_id', true );
1653
1654 return $donor_id;
1655 }
1656
1657 /**
1658 * Setup the User ID associated with the donation
1659 *
1660 * @since 1.5
1661 * @since 2.0 Get user id connect to donor from donor table instead of payment meta.
1662 *
1663 * @access private
1664 *
1665 * @return int The User ID
1666 */
1667 private function setup_user_id() {
1668
1669 $donor = Give()->customers->get_customer_by( 'id', $this->customer_id );
1670 $user_id = $donor ? absint( $donor->user_id ) : 0;
1671
1672
1673 return $user_id;
1674 }
1675
1676 /**
1677 * Setup the email address for the donation.
1678 *
1679 * @since 1.5
1680 * @since 2.0 Set email from _give_payment_donor_email meta key
1681 *
1682 * @access private
1683 *
1684 * @return string The email address for the payment.
1685 */
1686 private function setup_email() {
1687 $email = $this->get_meta( '_give_payment_donor_email', true );
1688
1689 if ( empty( $email ) && $this->customer_id ) {
1690 $email = Give()->donors->get_column( 'email', $this->customer_id );
1691 }
1692
1693 return $email;
1694 }
1695
1696 /**
1697 * Setup the user info.
1698 *
1699 * @since 1.5
1700 * @access private
1701 *
1702 * @return array The user info associated with the payment.
1703 */
1704 private function setup_user_info() {
1705 $defaults = array(
1706 'first_name' => $this->first_name,
1707 'last_name' => $this->last_name,
1708 );
1709
1710 $user_info = isset( $this->payment_meta['user_info'] ) ? $this->payment_meta['user_info'] : array();
1711
1712 if ( is_serialized( $user_info ) ) {
1713 preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $user_info, $matches );
1714 if ( ! empty( $matches ) ) {
1715 $user_info = array();
1716 }
1717 }
1718
1719 $user_info = wp_parse_args( $user_info, $defaults );
1720
1721 if ( empty( $user_info ) ) {
1722 // Get the donor, but only if it's been created.
1723 $donor = new Give_Donor( $this->customer_id );
1724
1725 if ( $donor->id > 0 ) {
1726 $user_info = array(
1727 'first_name' => $donor->get_first_name(),
1728 'last_name' => $donor->get_last_name(),
1729 'email' => $donor->email,
1730 'discount' => 'none',
1731 );
1732 }
1733 } else {
1734 // Get the donor, but only if it's been created.
1735 $donor = new Give_Donor( $this->customer_id );
1736
1737 if ( $donor->id > 0 ) {
1738 foreach ( $user_info as $key => $value ) {
1739 if ( ! empty( $value ) ) {
1740 continue;
1741 }
1742
1743 switch ( $key ) {
1744 case 'first_name':
1745 $user_info[ $key ] = $donor->get_first_name();
1746 break;
1747
1748 case 'last_name':
1749 $user_info[ $key ] = $donor->get_last_name();
1750 break;
1751
1752 case 'email':
1753 $user_info[ $key ] = $donor->email;
1754 break;
1755 }
1756 }
1757 }
1758 }// End if().
1759
1760 return $user_info;
1761
1762 }
1763
1764 /**
1765 * Setup the Address for the payment.
1766 *
1767 * @since 1.5
1768 * @access private
1769 *
1770 * @return array The Address information for the payment.
1771 */
1772 private function setup_address() {
1773 $address['line1'] = give_get_meta( $this->ID, '_give_donor_billing_address1', true, '' );
1774 $address['line2'] = give_get_meta( $this->ID, '_give_donor_billing_address2', true, '' );
1775 $address['city'] = give_get_meta( $this->ID, '_give_donor_billing_city', true, '' );
1776 $address['state'] = give_get_meta( $this->ID, '_give_donor_billing_state', true, '' );
1777 $address['zip'] = give_get_meta( $this->ID, '_give_donor_billing_zip', true, '' );
1778 $address['country'] = give_get_meta( $this->ID, '_give_donor_billing_country', true, '' );
1779
1780 return $address;
1781 }
1782
1783 /**
1784 * Setup the form title.
1785 *
1786 * @since 1.5
1787 * @access private
1788 *
1789 * @return string The Form Title.
1790 */
1791 private function setup_form_title() {
1792
1793 $form_id = $this->get_meta( '_give_payment_form_title', true );
1794
1795 return $form_id;
1796 }
1797
1798 /**
1799 * Setup the form ID.
1800 *
1801 * @since 1.5
1802 * @access private
1803 *
1804 * @return int The Form ID
1805 */
1806 private function setup_form_id() {
1807
1808 $form_id = $this->get_meta( '_give_payment_form_id', true );
1809
1810 return $form_id;
1811 }
1812
1813 /**
1814 * Setup the price ID.
1815 *
1816 * @since 1.5
1817 * @access private
1818 *
1819 * @return int The Form Price ID.
1820 */
1821 private function setup_price_id() {
1822 $price_id = $this->get_meta( '_give_payment_price_id', true );
1823
1824 return $price_id;
1825 }
1826
1827 /**
1828 * Setup the payment key.
1829 *
1830 * @since 1.5
1831 * @access private
1832 *
1833 * @return string The Payment Key.
1834 */
1835 private function setup_payment_key() {
1836 $key = $this->get_meta( '_give_payment_purchase_key', true );
1837
1838 return $key;
1839 }
1840
1841 /**
1842 * Setup the payment number.
1843 *
1844 * @since 1.5
1845 * @access private
1846 *
1847 * @return int|string Integer by default, or string if sequential order numbers is enabled.
1848 */
1849 private function setup_payment_number() {
1850 return $this->get_serial_code();
1851 }
1852
1853 /**
1854 * Converts this object into an array for special cases.
1855 *
1856 * @access public
1857 *
1858 * @return array The payment object as an array.
1859 */
1860 public function array_convert() {
1861 return get_object_vars( $this );
1862 }
1863
1864
1865 /**
1866 * Flag to check if donation is completed or not.
1867 *
1868 * @since 1.8
1869 * @access public
1870 *
1871 * @return bool
1872 */
1873 public function is_completed() {
1874 return ( 'publish' === $this->status && $this->completed_date );
1875 }
1876
1877 /**
1878 * Retrieve payment completion date.
1879 *
1880 * @since 1.5
1881 * @access private
1882 *
1883 * @return string Date payment was completed.
1884 */
1885 private function get_completed_date() {
1886 return apply_filters( 'give_payment_completed_date', $this->completed_date, $this->ID, $this );
1887 }
1888
1889 /**
1890 * Retrieve payment subtotal.
1891 *
1892 * @since 1.5
1893 * @access private
1894 *
1895 * @return float Payment subtotal.
1896 */
1897 private function get_subtotal() {
1898 return apply_filters( 'give_get_payment_subtotal', $this->subtotal, $this->ID, $this );
1899 }
1900
1901 /**
1902 * Retrieve payment currency.
1903 *
1904 * @since 1.5
1905 * @access private
1906 *
1907 * @return string Payment currency code.
1908 */
1909 private function get_currency() {
1910 return apply_filters( 'give_payment_currency_code', $this->currency, $this->ID, $this );
1911 }
1912
1913 /**
1914 * Retrieve payment gateway.
1915 *
1916 * @since 1.5
1917 * @access private
1918 *
1919 * @return string Gateway used.
1920 */
1921 private function get_gateway() {
1922 return apply_filters( 'give_payment_gateway', $this->gateway, $this->ID, $this );
1923 }
1924
1925 /**
1926 * Retrieve donation ID.
1927 *
1928 * @since 1.5
1929 * @access private
1930 *
1931 * @return string Donation ID from merchant processor.
1932 */
1933 private function get_transaction_id() {
1934 return apply_filters( 'give_get_payment_transaction_id', $this->transaction_id, $this->ID, $this );
1935 }
1936
1937 /**
1938 * Retrieve payment IP
1939 *
1940 * @since 1.5
1941 * @access private
1942 *
1943 * @return string Payment IP address
1944 */
1945 private function get_ip() {
1946 return apply_filters( 'give_payment_user_ip', $this->ip, $this->ID, $this );
1947 }
1948
1949 /**
1950 * Retrieve payment donor ID.
1951 *
1952 * @since 1.5
1953 * @access private
1954 *
1955 * @return int Payment donor ID.
1956 */
1957 private function get_donor_id() {
1958 return apply_filters( 'give_payment_customer_id', $this->customer_id, $this->ID, $this );
1959 }
1960
1961 /**
1962 * Retrieve payment user ID.
1963 *
1964 * @since 1.5
1965 * @access private
1966 *
1967 * @return int Payment user ID.
1968 */
1969 private function get_user_id() {
1970 return apply_filters( 'give_payment_user_id', $this->user_id, $this->ID, $this );
1971 }
1972
1973 /**
1974 * Retrieve payment email.
1975 *
1976 * @since 1.5
1977 * @access private
1978 *
1979 * @return string Payment donor email.
1980 */
1981 private function get_email() {
1982 return apply_filters( 'give_payment_user_email', $this->email, $this->ID, $this );
1983 }
1984
1985 /**
1986 * Retrieve payment user info.
1987 *
1988 * @since 1.5
1989 * @access private
1990 *
1991 * @return array Payment user info.
1992 */
1993 private function get_user_info() {
1994 return apply_filters( 'give_payment_meta_user_info', $this->user_info, $this->ID, $this );
1995 }
1996
1997 /**
1998 * Retrieve payment billing address.
1999 *
2000 * @since 1.5
2001 * @access private
2002 *
2003 * @return array Payment billing address.
2004 */
2005 private function get_address() {
2006 return apply_filters( 'give_payment_address', $this->address, $this->ID, $this );
2007 }
2008
2009 /**
2010 * Retrieve payment key.
2011 *
2012 * @since 1.5
2013 * @access private
2014 *
2015 * @return string Payment key.
2016 */
2017 private function get_key() {
2018 return apply_filters( 'give_payment_key', $this->key, $this->ID, $this );
2019 }
2020
2021 /**
2022 * Retrieve payment form id
2023 *
2024 * @since 1.5
2025 * @access private
2026 *
2027 * @return string Payment form id
2028 */
2029 private function get_form_id() {
2030 return apply_filters( 'give_payment_form_id', $this->form_id, $this->ID, $this );
2031 }
2032
2033 /**
2034 * Retrieve payment number
2035 *
2036 * @since 1.5
2037 * @access private
2038 *
2039 * @return int|string Payment number
2040 */
2041 private function get_number() {
2042 return apply_filters( 'give_payment_number', $this->number, $this->ID, $this );
2043 }
2044
2045 /**
2046 * Get serial code
2047 *
2048 * @since 2.1
2049 *
2050 * @param array $args
2051 *
2052 * @return string
2053 */
2054 public function get_serial_code( $args = array() ) {
2055 return Give()->seq_donation_number->get_serial_code( $this, $args );
2056 }
2057 }
2058