Completed
Branch FET/11129/facilitate-stripe-an... (fc6ced)
by
unknown
90:22 queued 79:00
created

EE_Payment::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 5
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\domain\values\currency\UsesMoneyInterface;
4
use EventEspresso\core\exceptions\InvalidDataTypeException;
5
use EventEspresso\core\exceptions\InvalidEntityException;
6
use EventEspresso\core\exceptions\InvalidIdentifierException;
7
use EventEspresso\core\exceptions\InvalidInterfaceException;
8
use EventEspresso\core\services\currency\MoneyFactory;
9
use EventEspresso\core\services\loaders\LoaderFactory;
10
11
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
12
	exit( 'No direct script access allowed' );
13
}
14
15
/**
16
 * Payment class
17
 *
18
 * @package        Event Espresso
19
 * @subpackage  includes/classes/EE_Payment.class.php
20
 * @author      Brent Christensen
21
 */
22
class EE_Payment extends EE_Base_Class implements EEI_Payment, UsesMoneyInterface {
23
24
    /**
25
     * @param array        $props_n_values      incoming values
26
     * @param string       $timezone            incoming timezone (if not set the timezone set for the website will be
27
     *                                          used.)
28
     * @param array        $date_formats        incoming date_formats in an array where the first value is the
29
     *                                          date_format and the second value is the time format
30
     * @param MoneyFactory $money_factory
31
     * @return EE_Payment
32
     * @throws InvalidArgumentException
33
     * @throws InvalidInterfaceException
34
     * @throws InvalidDataTypeException
35
     * @throws EE_Error
36
     */
37 View Code Duplication
    public static function new_instance(
38
        $props_n_values = array(),
39
        $timezone = null,
40
        $date_formats = array(),
41
        MoneyFactory $money_factory = null
42
    ) {
43
        $has_object = parent::_check_for_object(
44
            $props_n_values,
45
            __CLASS__,
46
            $timezone,
47
            $date_formats
48
        );
49
        return $has_object
50
            ? $has_object
51
            : new self(
52
                $props_n_values,
53
                false,
54
                $timezone,
55
                $date_formats,
56
                $money_factory
57
            );
58
    }
59
60
61
    /**
62
     * @param array        $props_n_values      incoming values from the database
63
     * @param string       $timezone            incoming timezone as set by the model.  If not set the timezone for
64
     *                                          the website will be used.
65
     * @param MoneyFactory $money_factory
66
     * @return EE_Payment
67
     * @throws InvalidArgumentException
68
     * @throws InvalidInterfaceException
69
     * @throws InvalidDataTypeException
70
     * @throws EE_Error
71
     */
72
    public static function new_instance_from_db(
73
        $props_n_values = array(),
74
        $timezone = null,
75
        MoneyFactory $money_factory = null
76
    ) {
77
        return new self(
78
            $props_n_values,
79
            true,
80
            $timezone,
81
            array(),
82
            $money_factory
83
        );
84
    }
85
86
87
    /**
88
     * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children
89
     * play nice
90
     *
91
     * @param array        $fieldValues  where each key is a field (ie, array key in the 2nd layer of the model's
92
     *                                   _fields array, (eg, EVT_ID, TXN_amount, QST_name, etc) and values are their
93
     *                                   values
94
     * @param boolean      $bydb         a flag for setting if the class is instantiated by the corresponding db model
95
     *                                   or not.
96
     * @param string       $timezone     indicate what timezone you want any datetime fields to be in when
97
     *                                   instantiating
98
     *                                   a EE_Base_Class object.
99
     * @param array        $date_formats An array of date formats to set on construct where first value is the
100
     *                                   date_format and second value is the time format.
101
     * @param MoneyFactory $money_factory
102
     * @throws InvalidArgumentException
103
     * @throws InvalidInterfaceException
104
     * @throws InvalidDataTypeException
105
     * @throws EE_Error
106
     */
107 View Code Duplication
    protected function __construct(
108
        array $fieldValues = array(),
109
        $bydb = false,
110
        $timezone = '',
111
        array $date_formats = array(),
112
        MoneyFactory $money_factory = null
113
    ) {
114
        if (! $money_factory instanceof MoneyFactory) {
115
            $money_factory = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\currency\MoneyFactory');
116
        }
117
        $this->money_factory = $money_factory;
118
        parent::__construct($fieldValues, $bydb, $timezone, $date_formats);
119
    }
120
121
122
    /**
123
	 * Set Transaction ID
124
	 *
125
	 * @access public
126
	 * @param int $TXN_ID
127
	 * @throws EE_Error
128
	 */
129
	public function set_transaction_id( $TXN_ID = 0 ) {
130
		$this->set( 'TXN_ID', $TXN_ID );
131
	}
132
133
134
135
	/**
136
	 * Gets the transaction related to this payment
137
	 *
138
	 * @return EE_Transaction
139
	 * @throws EE_Error
140
	 */
141
	public function transaction() {
142
		return $this->get_first_related( 'Transaction' );
143
	}
144
145
146
147
	/**
148
	 * Set Status
149
	 *
150
	 * @access public
151
	 * @param string $STS_ID
152
	 * @throws EE_Error
153
	 */
154
	public function set_status( $STS_ID = '' ) {
155
		$this->set( 'STS_ID', $STS_ID );
156
	}
157
158
159
160
	/**
161
	 * Set Payment Timestamp
162
	 *
163
	 * @access public
164
	 * @param int $timestamp
165
	 * @throws EE_Error
166
	 */
167
	public function set_timestamp( $timestamp = 0 ) {
168
		$this->set( 'PAY_timestamp', $timestamp );
169
	}
170
171
172
173
	/**
174
	 * Set Payment Method
175
	 *
176
	 * @access public
177
	 * @param string $PAY_source
178
	 * @throws EE_Error
179
	 */
180
	public function set_source( $PAY_source = '' ) {
181
		$this->set( 'PAY_source', $PAY_source );
182
	}
183
184
185
186
	/**
187
	 * Set Payment Amount
188
	 *
189
	 * @access public
190
	 * @param float $amount
191
	 * @throws EE_Error
192
	 */
193
	public function set_amount( $amount = 0.00 ) {
194
		$this->set( 'PAY_amount', (float)$amount );
195
	}
196
197
198
199
	/**
200
	 * Set Payment Gateway Response
201
	 *
202
	 * @access public
203
	 * @param string $gateway_response
204
	 * @throws EE_Error
205
	 */
206
	public function set_gateway_response( $gateway_response = '' ) {
207
		$this->set( 'PAY_gateway_response', $gateway_response );
208
	}
209
210
211
212
	/**
213
	 * Returns the name of the payment method used on this payment (previously known merely as 'gateway')
214
	 * but since 4.6.0, payment methods are models and the payment keeps a foreign key to the payment method
215
	 * used on it
216
	 *
217
	 * @deprecated
218
	 * @return string
219
	 * @throws EE_Error
220
	 */
221
	public function gateway() {
222
		EE_Error::doing_it_wrong(
223
			'EE_Payment::gateway',
224
			__(
225
				'The method EE_Payment::gateway() has been deprecated. Consider instead using EE_Payment::payment_method()->name()',
226
				'event_espresso'
227
			),
228
			'4.6.0'
229
		);
230
		return $this->payment_method() ? $this->payment_method()->name() : __( 'Unknown', 'event_espresso' );
231
	}
232
233
234
235
	/**
236
	 * Set Gateway Transaction ID
237
	 *
238
	 * @access public
239
	 * @param string $txn_id_chq_nmbr
240
	 * @throws EE_Error
241
	 */
242
	public function set_txn_id_chq_nmbr( $txn_id_chq_nmbr = '' ) {
243
		$this->set( 'PAY_txn_id_chq_nmbr', $txn_id_chq_nmbr );
244
	}
245
246
247
248
	/**
249
	 * Set Purchase Order Number
250
	 *
251
	 * @access public
252
	 * @param string $po_number
253
	 * @throws EE_Error
254
	 */
255
	public function set_po_number( $po_number = '' ) {
256
		$this->set( 'PAY_po_number', $po_number );
257
	}
258
259
260
261
	/**
262
	 * Set Extra Accounting Field
263
	 *
264
	 * @access public
265
	 * @param string $extra_accntng
266
	 * @throws EE_Error
267
	 */
268
	public function set_extra_accntng( $extra_accntng = '' ) {
269
		$this->set( 'PAY_extra_accntng', $extra_accntng );
270
	}
271
272
273
274
	/**
275
	 * Set Payment made via admin flag
276
	 *
277
	 * @access public
278
	 * @param bool $via_admin
279
	 * @throws EE_Error
280
	 */
281
	public function set_payment_made_via_admin( $via_admin = false ) {
282
		if ( $via_admin ) {
283
			$this->set( 'PAY_source', EEM_Payment_Method::scope_admin );
284
		} else {
285
			$this->set( 'PAY_source', EEM_Payment_Method::scope_cart );
286
		}
287
	}
288
289
290
291
	/**
292
	 * Set Payment Details
293
	 *
294
	 * @access public
295
	 * @param string|array $details
296
	 * @throws EE_Error
297
	 */
298
	public function set_details( $details = '' ) {
299 View Code Duplication
		if ( is_array( $details ) ) {
300
			array_walk_recursive( $details, array( $this, '_strip_all_tags_within_array' ) );
301
		} else {
302
			$details = wp_strip_all_tags( $details );
303
		}
304
		$this->set( 'PAY_details', $details );
305
	}
306
307
308
309
	/**
310
	 * Sets redirect_url
311
	 *
312
	 * @param string $redirect_url
313
	 * @throws EE_Error
314
	 */
315
	public function set_redirect_url( $redirect_url ) {
316
		$this->set( 'PAY_redirect_url', $redirect_url );
317
	}
318
319
320
321
	/**
322
	 * Sets redirect_args
323
	 *
324
	 * @param array $redirect_args
325
	 * @throws EE_Error
326
	 */
327
	public function set_redirect_args( $redirect_args ) {
328
		$this->set( 'PAY_redirect_args', $redirect_args );
329
	}
330
331
332
333
	/**
334
	 * get Payment Transaction ID
335
	 *
336
	 * @access public
337
	 * @throws EE_Error
338
	 */
339
	public function TXN_ID() {
340
		return $this->get( 'TXN_ID' );
341
	}
342
343
344
345
	/**
346
	 * get Payment Status
347
	 *
348
	 * @access public
349
	 * @throws EE_Error
350
	 */
351
	public function status() {
352
		return $this->get( 'STS_ID' );
353
	}
354
355
356
357
	/**
358
	 * get Payment Status
359
	 *
360
	 * @access public
361
	 * @throws EE_Error
362
	 */
363
	public function STS_ID() {
364
		return $this->get( 'STS_ID' );
365
	}
366
367
368
369
	/**
370
	 * get Payment Timestamp
371
	 *
372
	 * @access public
373
	 * @param string $dt_frmt
374
	 * @param string $tm_frmt
375
	 * @return string
376
	 * @throws EE_Error
377
	 */
378
	public function timestamp( $dt_frmt = '', $tm_frmt = '' ) {
379
		return $this->get_i18n_datetime( 'PAY_timestamp', trim( $dt_frmt . ' ' . $tm_frmt) );
380
	}
381
382
383
384
	/**
385
	 * get Payment Source
386
	 *
387
	 * @access public
388
	 * @throws EE_Error
389
	 */
390
	public function source() {
391
		return $this->get( 'PAY_source' );
392
	}
393
394
395
396
	/**
397
	 * get Payment Amount
398
	 *
399
	 * @access public
400
	 * @return float
401
	 * @throws EE_Error
402
	 */
403
	public function amount() {
404
		return (float)$this->get( 'PAY_amount' );
405
	}
406
407
408
409
	/**
410
	 * @return mixed
411
	 * @throws EE_Error
412
	 */
413
	public function amount_no_code() {
414
		return $this->get_pretty( 'PAY_amount', 'no_currency_code' );
415
	}
416
417
418
419
	/**
420
	 * get Payment Gateway Response
421
	 *
422
	 * @access public
423
	 * @throws EE_Error
424
	 */
425
	public function gateway_response() {
426
		return $this->get( 'PAY_gateway_response' );
427
	}
428
429
430
431
	/**
432
	 * get Payment Gateway Transaction ID
433
	 *
434
	 * @access public
435
	 * @throws EE_Error
436
	 */
437
	public function txn_id_chq_nmbr() {
438
		return $this->get( 'PAY_txn_id_chq_nmbr' );
439
	}
440
441
442
443
	/**
444
	 * get Purchase Order Number
445
	 *
446
	 * @access public
447
	 * @throws EE_Error
448
	 */
449
	public function po_number() {
450
		return $this->get( 'PAY_po_number' );
451
	}
452
453
454
455
	/**
456
	 * get Extra Accounting Field
457
	 *
458
	 * @access public
459
	 * @throws EE_Error
460
	 */
461
	public function extra_accntng() {
462
		return $this->get( 'PAY_extra_accntng' );
463
	}
464
465
466
467
	/**
468
	 * get Payment made via admin source
469
	 *
470
	 * @access public
471
	 * @throws EE_Error
472
	 */
473
	public function payment_made_via_admin() {
474
		return ( $this->get( 'PAY_source' ) === EEM_Payment_Method::scope_admin );
475
	}
476
477
478
479
	/**
480
	 * get Payment Details
481
	 *
482
	 * @access public
483
	 * @throws EE_Error
484
	 */
485
	public function details() {
486
		return $this->get( 'PAY_details' );
487
	}
488
489
490
491
	/**
492
	 * Gets redirect_url
493
	 *
494
	 * @return string
495
	 * @throws EE_Error
496
	 */
497
	public function redirect_url() {
498
		return $this->get( 'PAY_redirect_url' );
499
	}
500
501
502
503
	/**
504
	 * Gets redirect_args
505
	 *
506
	 * @return array
507
	 * @throws EE_Error
508
	 */
509
	public function redirect_args() {
510
		return $this->get( 'PAY_redirect_args' );
511
	}
512
513
514
515
	/**
516
	 * echoes $this->pretty_status()
517
	 *
518
	 * @param bool $show_icons
519
	 * @return void
520
	 * @throws EE_Error
521
	 */
522
	public function e_pretty_status( $show_icons = false ) {
523
		echo $this->pretty_status( $show_icons );
524
	}
525
526
527
    /**
528
     * returns a pretty version of the status, good for displaying to users
529
     *
530
     * @param bool $show_icons
531
     * @return string
532
     * @throws InvalidArgumentException
533
     * @throws InvalidInterfaceException
534
     * @throws InvalidDataTypeException
535
     * @throws EE_Error
536
     */
537
	public function pretty_status( $show_icons = false ) {
538
		$status = EEM_Status::instance()->localized_status(
539
			array( $this->STS_ID() => __( 'unknown', 'event_espresso' ) ),
540
			false,
541
			'sentence'
542
		);
543
		$icon = '';
544
		switch ( $this->STS_ID() ) {
545
			case EEM_Payment::status_id_approved:
546
				$icon = $show_icons
547
					? '<span class="dashicons dashicons-yes ee-icon-size-24 green-text"></span>'
548
					: '';
549
				break;
550
			case EEM_Payment::status_id_pending:
551
				$icon = $show_icons
552
					? '<span class="dashicons dashicons-clock ee-icon-size-16 orange-text"></span>'
553
					: '';
554
				break;
555
			case EEM_Payment::status_id_cancelled:
556
				$icon = $show_icons
557
					? '<span class="dashicons dashicons-no ee-icon-size-16 lt-grey-text"></span>'
558
					: '';
559
				break;
560
			case EEM_Payment::status_id_declined:
561
				$icon = $show_icons
562
					? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>'
563
					: '';
564
				break;
565
		}
566
		return $icon . $status[ $this->STS_ID() ];
567
	}
568
569
570
571
	/**
572
	 * For determining the status of the payment
573
	 *
574
	 * @return boolean whether the payment is approved or not
575
	 * @throws EE_Error
576
	 */
577
	public function is_approved() {
578
		return $this->status_is( EEM_Payment::status_id_approved );
579
	}
580
581
582
583
	/**
584
	 * Generally determines if the status of this payment equals
585
	 * the $STS_ID string
586
	 *
587
	 * @param string $STS_ID an ID from the esp_status table/
588
	 *                       one of the status_id_* on the EEM_Payment model
589
	 * @return boolean whether the status of this payment equals the status id
590
	 * @throws EE_Error
591
	 */
592
	protected function status_is( $STS_ID ) {
593
		return $STS_ID === $this->STS_ID() ? true : false;
594
	}
595
596
597
598
	/**
599
	 * For determining the status of the payment
600
	 *
601
	 * @return boolean whether the payment is pending or not
602
	 * @throws EE_Error
603
	 */
604
	public function is_pending() {
605
		return $this->status_is( EEM_Payment::status_id_pending );
606
	}
607
608
609
610
	/**
611
	 * For determining the status of the payment
612
	 *
613
	 * @return boolean
614
	 * @throws EE_Error
615
	 */
616
	public function is_cancelled() {
617
		return $this->status_is( EEM_Payment::status_id_cancelled );
618
	}
619
620
621
622
	/**
623
	 * For determining the status of the payment
624
	 *
625
	 * @return boolean
626
	 * @throws EE_Error
627
	 */
628
	public function is_declined() {
629
		return $this->status_is( EEM_Payment::status_id_declined );
630
	}
631
632
633
634
	/**
635
	 * For determining the status of the payment
636
	 *
637
	 * @return boolean
638
	 * @throws EE_Error
639
	 */
640
	public function is_failed() {
641
		return $this->status_is( EEM_Payment::status_id_failed );
642
	}
643
644
645
646
	/**
647
	 * For determining if the payment is actually a refund ( ie: has a negative value )
648
	 *
649
	 * @return boolean
650
	 * @throws EE_Error
651
	 */
652
	public function is_a_refund() {
653
		return $this->amount() < 0 ? true : false;
654
	}
655
656
657
658
	/**
659
	 * Get the status object of this object
660
	 *
661
	 * @return EE_Status
662
	 * @throws EE_Error
663
	 */
664
	public function status_obj() {
665
		return $this->get_first_related( 'Status' );
666
	}
667
668
669
670
	/**
671
	 * Gets all the extra meta info on this payment
672
	 *
673
	 * @param array $query_params like EEM_Base::get_all
674
	 * @return EE_Extra_Meta
675
	 * @throws EE_Error
676
	 */
677
	public function extra_meta( $query_params = array() ) {
678
		return $this->get_many_related( 'Extra_Meta', $query_params );
679
	}
680
681
682
683
	/**
684
	 * Gets the last-used payment method on this transaction
685
	 * (we COULD just use the last-made payment, but some payment methods, namely
686
	 * offline ones, dont' create payments)
687
	 *
688
	 * @return EE_Payment_Method
689
	 * @throws EE_Error
690
	 */
691
	public function payment_method() {
692
		return $this->get_first_related( 'Payment_Method' );
693
	}
694
695
696
697
	/**
698
	 * Gets the HTML for redirecting the user to an offsite gateway
699
	 * You can pass it special content to put inside the form, or use
700
	 * the default inner content (or possibly generate this all yourself using
701
	 * redirect_url() and redirect_args() or redirect_args_as_inputs()).
702
	 * Creates a POST request by default, but if no redirect args are specified, creates a GET request instead
703
	 * (and any querystring variables in the redirect_url are converted into html inputs
704
	 * so browsers submit them properly)
705
	 *
706
	 * @param string $inside_form_html
707
	 * @return string html
708
	 * @throws EE_Error
709
	 */
710
	public function redirect_form( $inside_form_html = null ) {
711
		$redirect_url = $this->redirect_url();
712
		if ( ! empty( $redirect_url ) ) {
713
			// what ? no inner form content?
714
			if ( $inside_form_html === null ) {
715
				$inside_form_html = EEH_HTML::p(
716
					sprintf(
717
						__(
718
							'If you are not automatically redirected to the payment website within 10 seconds... %1$s %2$s Click Here %3$s',
719
							'event_espresso'
720
						),
721
						EEH_HTML::br( 2 ),
722
						'<input type="submit" value="',
723
						'">'
724
					),
725
					'',
726
					'',
727
					'text-align:center;'
728
				);
729
			}
730
			$method = apply_filters(
731
				'FHEE__EE_Payment__redirect_form__method',
732
				$this->redirect_args() ? 'POST' : 'GET',
733
				$this
734
			);
735
			//if it's a GET request, we need to remove all the GET params in the querystring
736
			//and put them into the form instead
737
			if ( $method === 'GET' ) {
738
				$querystring = parse_url( $redirect_url, PHP_URL_QUERY );
739
				$get_params = null;
740
				parse_str( $querystring, $get_params );
741
				$inside_form_html .= $this->_args_as_inputs( $get_params );
742
				$redirect_url = str_replace( '?' . $querystring, '', $redirect_url );
743
			}
744
			$form = EEH_HTML::nl( 1 )
745
			        . '<form method="'
746
			        . $method
747
			        . '" name="gateway_form" action="'
748
			        . $redirect_url
749
			        . '">';
750
			$form .= EEH_HTML::nl( 1 ) . $this->redirect_args_as_inputs();
751
			$form .= $inside_form_html;
752
			$form .= EEH_HTML::nl( -1 ) . '</form>' . EEH_HTML::nl( -1 );
753
			return $form;
754
		} else {
755
			return null;
756
		}
757
	}
758
759
760
761
	/**
762
	 * Changes all the name-value pairs of the redirect args into html inputs
763
	 * and returns the html as a string
764
	 *
765
	 * @return string
766
	 * @throws EE_Error
767
	 */
768
	public function redirect_args_as_inputs() {
769
		return $this->_args_as_inputs( $this->redirect_args() );
770
	}
771
772
773
774
	/**
775
	 * Converts a 1d array of key-value pairs into html hidden inputs
776
	 * and returns the string of html
777
	 *
778
	 * @param array $args key-value pairs
779
	 * @return string
780
	 */
781
	protected function _args_as_inputs( $args ) {
782
		$html = '';
783
		if ( $args !== null && is_array( $args ) ) {
784
			foreach ( $args as $name => $value ) {
785
				$html .= EEH_HTML::nl( 0 )
786
				         . '<input type="hidden" name="'
787
				         . $name
788
				         . '" value="'
789
				         . esc_attr( $value )
790
				         . '"/>';
791
			}
792
		}
793
		return $html;
794
	}
795
796
797
798
	/**
799
	 * Returns the currency of the payment.
800
	 * (At the time of writing, this will always be the currency in the configuration;
801
	 * however in the future it is anticipated that this will be stored on the payment
802
	 * object itself)
803
	 *
804
	 * @return string for the currency code
805
	 */
806
	public function currency_code() {
807
		return EE_Config::instance()->currency->code;
808
	}
809
810
811
812
	/**
813
	 * apply wp_strip_all_tags to all elements within an array
814
	 *
815
	 * @access private
816
	 * @param mixed $item
817
	 */
818
	private function _strip_all_tags_within_array( &$item ) {
819
		if ( is_object( $item ) ) {
820
			$item = (array)$item;
821
		}
822 View Code Duplication
		if ( is_array( $item ) ) {
823
			array_walk_recursive( $item, array( $this, '_strip_all_tags_within_array' ) );
824
		} else {
825
			$item = wp_strip_all_tags( $item );
826
		}
827
	}
828
829
830
831
	/**
832
	 * Returns TRUE is this payment was set to approved during this request (or
833
	 * is approved and was created during this request). False otherwise.
834
	 *
835
	 * @return boolean
836
	 * @throws EE_Error
837
	 */
838
	public function just_approved() {
839
		$original_status = EEH_Array::is_set(
840
			$this->_props_n_values_provided_in_constructor,
841
			'STS_ID',
842
			$this->get_model()->field_settings_for( 'STS_ID' )->get_default_value()
843
		);
844
		$current_status = $this->status();
845
		if (
846
			$original_status !== EEM_Payment::status_id_approved
847
			&& $current_status === EEM_Payment::status_id_approved
848
		) {
849
			return true;
850
		} else {
851
			return false;
852
		}
853
	}
854
855
856
857
	/**
858
	 * Overrides parents' get_pretty() function just for legacy reasons
859
	 * (to allow ticket https://events.codebasehq.com/projects/event-espresso/tickets/7420)
860
	 *
861
	 * @param string $field_name
862
	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
863
	 *                                (in cases where the same property may be used for different outputs
864
	 *                                - i.e. datetime, money etc.)
865
	 * @return mixed
866
	 * @throws EE_Error
867
	 */
868
	public function get_pretty( $field_name, $extra_cache_ref = null ) {
869
		if ( $field_name === 'PAY_gateway' ) {
870
			return $this->payment_method() ? $this->payment_method()->name() : __( 'Unknown', 'event_espresso' );
871
		}
872
		return $this->_get_cached_property( $field_name, true, $extra_cache_ref );
873
	}
874
875
876
877
	/**
878
	 * Gets details regarding which registrations this payment was applied to
879
	 *
880
	 * @param array $query_params like EEM_Base::get_all
881
	 * @return EE_Registration_Payment[]
882
	 * @throws EE_Error
883
	 */
884
	public function registration_payments( $query_params = array() ) {
885
		return $this->get_many_related( 'Registration_Payment', $query_params );
886
	}
887
888
889
    /**
890
     * Gets the first event for this payment (it's possible that it could be for multiple)
891
     *
892
     * @return EE_Event|null
893
     * @throws EE_Error
894
     */
895
    public function get_first_event()
896
    {
897
        $transaction = $this->transaction();
898
        if ($transaction instanceof EE_Transaction) {
899
            $primary_registrant = $transaction->primary_registration();
900
            if ($primary_registrant instanceof EE_Registration) {
901
                return $primary_registrant->event_obj();
902
            }
903
        }
904
        return null;
905
    }
906
907
908
    /**
909
     * Gets the name of the first event for which is being paid
910
     *
911
     * @return string
912
     * @throws EE_Error
913
     */
914
    public function get_first_event_name()
915
    {
916
        $event = $this->get_first_event();
917
        return $event instanceof EE_Event ? $event->name() : __('Event', 'event_espresso');
918
    }
919
920
921
    /**
922
     * Returns the payment's transaction's primary registration
923
     * @return EE_Registration|null
924
     * @throws EE_Error
925
     */
926
    public function get_primary_registration()
927
    {
928
        if ($this->transaction() instanceof EE_Transaction) {
929
            return $this->transaction()->primary_registration();
930
        }
931
        return null;
932
    }
933
934
935
    /**
936
     * Gets the payment's transaction's primary registration's attendee, or null
937
     * @return EE_Attendee|null
938
     * @throws EE_Error
939
     */
940
    public function get_primary_attendee()
941
    {
942
        $primary_reg = $this->get_primary_registration();
943
        if( $primary_reg instanceof EE_Registration) {
944
            return $primary_reg->attendee();
945
        }
946
        return null;
947
    }
948
949
950
    /**
951
     * Returns the payment's amount in subunits (if the currency has subunits; otherwise this will actually be
952
     * in the currency's main units)
953
     *
954
     * @return int
955
     * @throws EE_Error
956
     * @throws InvalidEntityException
957
     * @throws DomainException
958
     */
959
    public function amountInSubunits()
960
    {
961
        return $this->moneyInSubunits('PAY_amount');
962
    }
963
964
965
    /**
966
     * Sets the payment's amount based on the incoming monetary subunits (eg pennies). If the currency has no subunits,
967
     * the amount is actually assumed to be in the currency's main units
968
     *
969
     * @param int $amount_in_subunits
970
     * @return void
971
     * @throws EE_Error
972
     * @throws InvalidArgumentException
973
     * @throws InvalidInterfaceException
974
     * @throws InvalidIdentifierException
975
     * @throws InvalidDataTypeException
976
     * @throws DomainException
977
     */
978
    public function setAmountInSubunits($amount_in_subunits)
979
    {
980
        $this->setMoneySubunits('PAY_amount', $amount_in_subunits);
981
    }
982
}
983
/* End of file EE_Payment.class.php */
984
/* Location: /includes/classes/EE_Payment.class.php */
985