Completed
Branch FET-10486-add-timestamp-checki... (611b15)
by
unknown
105:07 queued 90:18
created

EE_Payment::status_obj()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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