Passed
Push — develop ( 4bdfa9...a056ec )
by Reüel
04:32
created

Payment::get_entrance_code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Payments;
12
13
use Pronamic\WordPress\Pay\Currency;
14
use Pronamic\WordPress\Pay\DateTime;
15
use Pronamic\WordPress\Pay\Core\Statuses;
16
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
17
use WP_Post;
18
19
/**
20
 * Payment
21
 *
22
 * @author Remco Tolsma
23
 * @version 4.4.3
24
 * @since 1.0.0
25
 */
26
class Payment {
27
	/**
28
	 * The payment post object.
29
	 *
30
	 * @var WP_Post|array
31
	 */
32
	public $post;
33
34
	/**
35
	 * The date of this payment.
36
	 *
37
	 * @var DateTime
38
	 */
39
	public $date;
40
41
	/**
42
	 * The subscription.
43
	 *
44
	 * @var Subscription
45
	 */
46
	public $subscription;
47
48
	/**
49
	 * The unique ID of this payment.
50
	 *
51
	 * @var string
52
	 */
53
	protected $id;
54
55
	/**
56
	 * The title of this payment.
57
	 *
58
	 * @var string
59
	 */
60
	public $title;
61
62
	/**
63
	 * The configuration ID.
64
	 *
65
	 * @var integer
66
	 */
67
	public $config_id;
68
69
	/**
70
	 * The key of this payment, used in URL's for security.
71
	 *
72
	 * @var string
73
	 */
74
	public $key;
75
76
	/**
77
	 * Identifier for the source which started this payment.
78
	 * For example: 'woocommerce', 'gravityforms', 'easydigitaldownloads', etc.
79
	 *
80
	 * @var string
81
	 */
82
	public $source;
83
84
	/**
85
	 * Unique ID at the source which started this payment, for example:
86
	 * - WooCommerce order ID.
87
	 * - Easy Digital Downloads payment ID.
88
	 * - Gravity Forms entry ID.
89
	 *
90
	 * @var string
91
	 */
92
	public $source_id;
93
94
	/**
95
	 * The purchase ID.
96
	 *
97
	 * @todo Is this required/used?
98
	 * @var string
99
	 */
100
	public $purchase_id;
101
102
	/**
103
	 * The transaction ID of this payment.
104
	 *
105
	 * @var string
106
	 */
107
	public $transaction_id;
108
109
	/**
110
	 * The order ID of this payment.
111
	 *
112
	 * @todo Is this required/used?
113
	 * @var string
114
	 */
115
	public $order_id;
116
117
	/**
118
	 * The amount of this payment, for example 18.95.
119
	 *
120
	 * @var float
121
	 */
122
	public $amount;
123
124
	/**
125
	 * The currency of this subscription, for example 'EUR' or 'USD'.
126
	 *
127
	 * @var string
128
	 */
129
	public $currency;
130
131
	/**
132
	 * The expiration period of this payment.
133
	 *
134
	 * @todo Is this required/used?
135
	 * @var string
136
	 */
137
	public $expiration_period;
138
139
	/**
140
	 * The language of the user who started this payment.
141
	 *
142
	 * @var string
143
	 */
144
	public $language;
145
146
	/**
147
	 * The locale of the user who started this payment.
148
	 *
149
	 * @var string
150
	 */
151
	public $locale;
152
153
	/**
154
	 * The entrance code of this payment.
155
	 *
156
	 * @todo Is this required/used?
157
	 * @var string
158
	 */
159
	public $entrance_code;
160
161
	/**
162
	 * The description of this payment.
163
	 *
164
	 * @var string
165
	 */
166
	public $description;
167
168
	/**
169
	 * The name of the consumer of this payment.
170
	 *
171
	 * @todo Is this required and should we add the 'consumer' part?
172
	 * @var  string
173
	 */
174
	public $consumer_name;
175
176
	/**
177
	 * The account number of the consumer of this payment.
178
	 *
179
	 * @todo Is this required and should we add the 'consumer' part?
180
	 * @var  string
181
	 */
182
	public $consumer_account_number;
183
184
	/**
185
	 * The IBAN of the consumer of this payment.
186
	 *
187
	 * @todo Is this required and should we add the 'consumer' part?
188
	 * @var  string
189
	 */
190
	public $consumer_iban;
191
192
	/**
193
	 * The BIC of the consumer of this payment.
194
	 *
195
	 * @todo Is this required and should we add the 'consumer' part?
196
	 * @var  string
197
	 */
198
	public $consumer_bic;
199
200
	/**
201
	 * The city of the consumer of this payment.
202
	 *
203
	 * @todo Is this required and should we add the 'consumer' part?
204
	 * @var  string
205
	 */
206
	public $consumer_city;
207
208
	/**
209
	 * The customer name of the consumer of this payment.
210
	 *
211
	 * @todo Is this required?
212
	 * @var  string
213
	 */
214
	public $customer_name;
215
216
	/**
217
	 * The address of the consumer of this payment.
218
	 *
219
	 * @todo Is this required?
220
	 * @var  string
221
	 */
222
	public $address;
223
224
	/**
225
	 * The city of the consumer of this payment.
226
	 *
227
	 * @todo Is this required?
228
	 * @var  string
229
	 */
230
	public $city;
231
232
	/**
233
	 * The ZIP of the consumer of this payment.
234
	 *
235
	 * @todo Is this required?
236
	 * @var  string
237
	 */
238
	public $zip;
239
240
	/**
241
	 * The country of the consumer of this payment.
242
	 *
243
	 * @todo Is this required?
244
	 * @var  string
245
	 */
246
	public $country;
247
248
	/**
249
	 * The telephone number of the consumer of this payment.
250
	 *
251
	 * @todo Is this required?
252
	 * @var  string
253
	 */
254
	public $telephone_number;
255
256
	/**
257
	 * The Google Analytics client ID of the user who started this payment.
258
	 *
259
	 * @var string
260
	 */
261
	public $analytics_client_id;
262
263
	/**
264
	 * The status of this payment.
265
	 *
266
	 * @todo   Check constant?
267
	 * @var string
268
	 */
269
	public $status;
270
271
	/**
272
	 * Status requests?
273
	 *
274
	 * @todo What is this?
275
	 * @var ?
276
	 */
277
	public $status_requests;
278
279
	/**
280
	 * The email of the user who started this payment.
281
	 *
282
	 * @var string
283
	 */
284
	public $email;
285
286
	/**
287
	 * The action URL for this payment.
288
	 *
289
	 * @var string
290
	 */
291
	public $action_url;
292
293
	/**
294
	 * The payment method chosen by to user who started this payment.
295
	 *
296
	 * @var string
297
	 */
298
	public $method;
299
300
	/**
301
	 * The issuer chosen by to user who started this payment.
302
	 *
303
	 * @var string
304
	 */
305
	public $issuer;
306
307
	/**
308
	 * Subscription ID.
309
	 *
310
	 * @todo Is this required?
311
	 * @var string
312
	 */
313
	public $subscription_id;
314
315
	/**
316
	 * Flag to indicate a recurring payment
317
	 *
318
	 * @todo Is this required?
319
	 * @var boolean
320
	 */
321
	public $recurring;
322
323
	/**
324
	 * The first name of the user who started this payment.
325
	 *
326
	 * @var string
327
	 */
328
	public $first_name;
329
330
	/**
331
	 * The last name of the user who started this payment.
332
	 *
333
	 * @var string
334
	 */
335
	public $last_name;
336
337
	/**
338
	 * The recurring type.
339
	 *
340
	 * @todo Improve documentation, is this used?
341
	 * @var string
342
	 */
343
	public $recurring_type;
344
345
	/**
346
	 * Meta.
347
	 *
348
	 * @var array
349
	 */
350
	public $meta;
351
352
	/**
353
	 * Start date if the payment is related to a specific period.
354
	 *
355
	 * @var DateTime
356
	 */
357
	public $start_date;
358
359
	/**
360
	 * End date if the payment is related to a specific period.
361
	 *
362
	 * @var DateTime
363
	 */
364
	public $end_date;
365
366
	/**
367
	 * Construct and initialize payment object.
368
	 *
369
	 * @param integer $post_id A payment post ID or null.
370
	 */
371
	public function __construct( $post_id = null ) {
372
		$this->id   = $post_id;
373
		$this->date = new DateTime();
374
		$this->meta = array();
375
376
		if ( null !== $post_id ) {
377
			pronamic_pay_plugin()->payments_data_store->read( $this );
378
		}
379
	}
380
381
	/**
382
	 * Get the ID of this payment.
383
	 *
384
	 * @return string
385
	 */
386
	public function get_id() {
387
		return $this->id;
388
	}
389
390
	/**
391
	 * Set the ID of this payment.
392
	 *
393
	 * @param string $id Unique ID.
394
	 */
395
	public function set_id( $id ) {
396
		$this->id = $id;
397
	}
398
399
	/**
400
	 * Save payment.
401
	 *
402
	 * @return void
403
	 */
404
	public function save() {
405
		pronamic_pay_plugin()->payments_data_store->update( $this );
406
	}
407
408
	/**
409
	 * Add a note to this payment.
410
	 *
411
	 * @param string $note The note to add.
412
	 */
413
	public function add_note( $note ) {
414
		$commentdata = array(
415
			'comment_post_ID'  => $this->id,
416
			'comment_content'  => $note,
417
			'comment_type'     => 'payment_note',
418
			'user_id'          => get_current_user_id(),
419
			'comment_approved' => true,
420
		);
421
422
		$comment_id = wp_insert_comment( $commentdata );
423
424
		return $comment_id;
425
	}
426
427
	/**
428
	 * Get the source identifier of this payment.
429
	 *
430
	 * @return string
431
	 */
432
	public function get_source() {
433
		return $this->source;
434
	}
435
436
	/**
437
	 * Get the source ID of this payment.
438
	 *
439
	 * @return string
440
	 */
441
	public function get_source_id() {
442
		return $this->source_id;
443
	}
444
445
	/**
446
	 * Get the source text of this payment.
447
	 *
448
	 * @return string
449
	 */
450
	public function get_source_text() {
451
		$text = $this->get_source() . '<br />' . $this->get_source_id();
452
453
		$text = apply_filters( 'pronamic_payment_source_text_' . $this->get_source(), $text, $this );
454
		$text = apply_filters( 'pronamic_payment_source_text', $text, $this );
455
456
		return $text;
457
	}
458
459
	/**
460
	 * Get the order ID of this payment.
461
	 *
462
	 * @return string
463
	 */
464
	public function get_order_id() {
465
		return $this->order_id;
466
	}
467
468
	/**
469
	 * Get the payment amount.
470
	 *
471
	 * @return float
472
	 */
473
	public function get_amount() {
474
		return $this->amount;
475
	}
476
477
	/**
478
	 * Get the payment currency.
479
	 *
480
	 * @return string
481
	 */
482
	public function get_currency() {
483
		return $this->currency;
484
	}
485
486
	/**
487
	 * Get currency numeric code
488
	 *
489
	 * @return string|null
490
	 */
491
	public function get_currency_numeric_code() {
492
		return Currency::transform_code_to_number( $this->get_currency() );
493
	}
494
495
	/**
496
	 * Get the payment method.
497
	 *
498
	 * @todo Constant?
499
	 * @return string
500
	 */
501
	public function get_method() {
502
		return $this->method;
503
	}
504
505
	/**
506
	 * Get the payment issuer.
507
	 *
508
	 * @return string
509
	 */
510
	public function get_issuer() {
511
		return $this->issuer;
512
	}
513
514
	/**
515
	 * Get the payment language.
516
	 *
517
	 * @return string
518
	 */
519
	public function get_language() {
520
		return $this->language;
521
	}
522
523
	/**
524
	 * Get the payment locale.
525
	 *
526
	 * @return string
527
	 */
528
	public function get_locale() {
529
		return $this->locale;
530
	}
531
532
	/**
533
	 * Get the payment description.
534
	 *
535
	 * @return string
536
	 */
537
	public function get_description() {
538
		return $this->description;
539
	}
540
541
	/**
542
	 * Set the transaction ID.
543
	 *
544
	 * @param string $transaction_id Transaction ID.
545
	 */
546
	public function set_transaction_id( $transaction_id ) {
547
		$this->transaction_id = $transaction_id;
548
	}
549
550
	/**
551
	 * Get the payment transaction ID.
552
	 *
553
	 * @return string
554
	 */
555
	public function get_transaction_id() {
556
		return $this->transaction_id;
557
	}
558
559
	/**
560
	 * Get the payment status.
561
	 *
562
	 * @todo Constant?
563
	 * @return string
564
	 */
565
	public function get_status() {
566
		return $this->status;
567
	}
568
569
	/**
570
	 * Set the payment status.
571
	 *
572
	 * @param string $status Status.
573
	 */
574
	public function set_status( $status ) {
575
		$this->status = $status;
576
	}
577
578
	/**
579
	 * Get the meta value of this specified meta key.
580
	 *
581
	 * @param string $key Meta key.
582
	 * @return mixed
583
	 */
584
	public function get_meta( $key ) {
585
		$key = '_pronamic_payment_' . $key;
586
587
		return get_post_meta( $this->id, $key, true );
588
	}
589
590
	/**
591
	 * Set meta value at the specified key.
592
	 *
593
	 * @param string $key   Meta key.
594
	 * @param string $value Meta value.
595
	 */
596
	public function set_meta( $key, $value ) {
597
		$this->meta[ $key ] = $value;
598
	}
599
600
	/**
601
	 * Get the pay redirect URL.
602
	 *
603
	 * @return string
604
	 */
605
	public function get_pay_redirect_url() {
606
		return add_query_arg( 'payment_redirect', $this->id, home_url( '/' ) );
607
	}
608
609
	/**
610
	 * Get the return URL for this payment. This URL is passed to the payment providers / gateways
611
	 * so they know where they should return users to.
612
	 *
613
	 * @return string
614
	 */
615
	public function get_return_url() {
616
		$url = add_query_arg(
617
			array(
618
				'payment' => $this->id,
619
				'key'     => $this->key,
620
			),
621
			home_url( '/' )
622
		);
623
624
		return $url;
625
	}
626
627
	/**
628
	 * Get action URL.
629
	 *
630
	 * @return string
631
	 */
632
	public function get_action_url() {
633
		if ( '' === $this->get_amount() || 0.0 === $this->get_amount() ) {
634
			$this->set_status( Statuses::SUCCESS );
635
636
			return $this->get_return_redirect_url();
637
		}
638
639
		return $this->action_url;
640
	}
641
642
	/**
643
	 * Set the action URL.
644
	 *
645
	 * @param string $action_url Action URL.
646
	 */
647
	public function set_action_url( $action_url ) {
648
		$this->action_url = $action_url;
649
	}
650
651
	/**
652
	 * Get the return redirect URL for this payment. This URL is used after a user is returned
653
	 * from a payment provider / gateway to WordPress. It allows WordPress payment extensions
654
	 * to redirect users to the correct URL.
655
	 *
656
	 * @return string
657
	 */
658
	public function get_return_redirect_url() {
659
		$url = home_url( '/' );
660
661
		$url = apply_filters( 'pronamic_payment_redirect_url', $url, $this );
662
		$url = apply_filters( 'pronamic_payment_redirect_url_' . $this->source, $url, $this );
663
664
		return $url;
665
	}
666
667
	/**
668
	 * Get the redirect URL for this payment.
669
	 *
670
	 * @deprecated 4.1.2 Use get_return_redirect_url()
671
	 * @return string
672
	 */
673
	public function get_redirect_url() {
674
		_deprecated_function( __FUNCTION__, '4.1.2', 'get_return_redirect_url()' );
675
676
		return $this->get_return_redirect_url();
677
	}
678
679
	/**
680
	 * Get source description.
681
	 *
682
	 * @return string
683
	 */
684
	public function get_source_description() {
685
		$description = $this->source;
686
687
		$description = apply_filters( 'pronamic_payment_source_description', $description, $this );
688
		$description = apply_filters( 'pronamic_payment_source_description_' . $this->source, $description, $this );
689
690
		return $description;
691
	}
692
693
	/**
694
	 * Get the source link for this payment.
695
	 *
696
	 * @return string|null
697
	 */
698
	public function get_source_link() {
699
		$url = null;
700
701
		$url = apply_filters( 'pronamic_payment_source_url', $url, $this );
702
		$url = apply_filters( 'pronamic_payment_source_url_' . $this->source, $url, $this );
703
704
		return $url;
705
	}
706
707
	/**
708
	 * Get provider link for this payment.
709
	 *
710
	 * @return string
711
	 */
712
	public function get_provider_link() {
713
		$url = null;
714
715
		$config_id  = get_post_meta( $this->id, '_pronamic_payment_config_id', true );
716
		$gateway_id = get_post_meta( $config_id, '_pronamic_gateway_id', true );
0 ignored issues
show
Bug introduced by
It seems like $config_id can also be of type false and string; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

716
		$gateway_id = get_post_meta( /** @scrutinizer ignore-type */ $config_id, '_pronamic_gateway_id', true );
Loading history...
717
718
		$url = apply_filters( 'pronamic_payment_provider_url', $url, $this );
719
		$url = apply_filters( 'pronamic_payment_provider_url_' . $gateway_id, $url, $this );
0 ignored issues
show
Bug introduced by
Are you sure $gateway_id of type mixed|false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

719
		$url = apply_filters( 'pronamic_payment_provider_url_' . /** @scrutinizer ignore-type */ $gateway_id, $url, $this );
Loading history...
720
721
		return $url;
722
	}
723
724
	/**
725
	 * Get subscription.
726
	 *
727
	 * @return Subscription
728
	 */
729
	public function get_subscription() {
730
		if ( is_object( $this->subscription ) ) {
731
			return $this->subscription;
732
		}
733
734
		if ( empty( $this->subscription_id ) ) {
735
			return false;
736
		}
737
738
		$this->subscription = new Subscription( $this->subscription_id );
739
740
		return $this->subscription;
741
	}
742
743
	/**
744
	 * Format string
745
	 *
746
	 * @see https://github.com/woocommerce/woocommerce/blob/v2.2.3/includes/abstracts/abstract-wc-email.php#L187-L195
747
	 *
748
	 * @param string $string The string to format.
749
	 * @return string
750
	 */
751
	public function format_string( $string ) {
752
		// Replacements definition.
753
		$replacements = array(
754
			'{order_id}'   => $this->get_order_id(),
755
			'{payment_id}' => $this->get_id(),
756
		);
757
758
		// Find and replace.
759
		$string = str_replace(
760
			array_keys( $replacements ),
761
			array_values( $replacements ),
762
			$string,
763
			$count
764
		);
765
766
		// Make sure there is an dynamic part in the order ID.
767
		// @see https://secure.ogone.com/ncol/param_cookbook.asp.
768
		if ( 0 === $count ) {
769
			$string .= $this->get_id();
770
		}
771
772
		return $string;
773
	}
774
775
	/**
776
	 * Set consumer name.
777
	 *
778
	 * @param string $name Name.
779
	 */
780
	public function set_consumer_name( $name ) {
781
		$this->consumer_name = $name;
782
	}
783
784
	/**
785
	 * Set consumer account number.
786
	 *
787
	 * @param string $account_number Account number.
788
	 */
789
	public function set_consumer_account_number( $account_number ) {
790
		$this->consumer_account_number = $account_number;
791
	}
792
793
	/**
794
	 * Set consumer IBAN.
795
	 *
796
	 * @param string $iban IBAN.
797
	 */
798
	public function set_consumer_iban( $iban ) {
799
		$this->consumer_iban = $iban;
800
	}
801
802
	/**
803
	 * Set consumer BIC.
804
	 *
805
	 * @param string $bic BIC.
806
	 */
807
	public function set_consumer_bic( $bic ) {
808
		$this->consumer_bic = $bic;
809
	}
810
811
	/**
812
	 * Set consumer city.
813
	 *
814
	 * @param string $city City.
815
	 */
816
	public function set_consumer_city( $city ) {
817
		$this->consumer_city = $city;
818
	}
819
820
	/**
821
	 * Get payment email.
822
	 *
823
	 * @return string
824
	 */
825
	public function get_email() {
826
		return $this->email;
827
	}
828
829
	/**
830
	 * Get first name.
831
	 *
832
	 * @return string
833
	 */
834
	public function get_first_name() {
835
		return $this->first_name;
836
	}
837
838
	/**
839
	 * Get last name.
840
	 *
841
	 * @return string
842
	 */
843
	public function get_last_name() {
844
		return $this->last_name;
845
	}
846
847
	/**
848
	 * Get customer name.
849
	 *
850
	 * @return string
851
	 */
852
	public function get_customer_name() {
853
		return $this->customer_name;
854
	}
855
856
	/**
857
	 * Get address.
858
	 *
859
	 * @return string
860
	 */
861
	public function get_address() {
862
		return $this->address;
863
	}
864
865
	/**
866
	 * Get city.
867
	 *
868
	 * @return string
869
	 */
870
	public function get_city() {
871
		return $this->city;
872
	}
873
874
	/**
875
	 * Get ZIP.
876
	 *
877
	 * @return string
878
	 */
879
	public function get_zip() {
880
		return $this->zip;
881
	}
882
883
	/**
884
	 * Get country.
885
	 *
886
	 * @return string
887
	 */
888
	public function get_country() {
889
		return $this->country;
890
	}
891
892
	/**
893
	 * Get telephone number.
894
	 *
895
	 * @return string
896
	 */
897
	public function get_telephone_number() {
898
		return $this->telephone_number;
899
	}
900
901
	/**
902
	 * Get Google Analytics client ID.
903
	 *
904
	 * @return string
905
	 */
906
	public function get_analytics_client_id() {
907
		return $this->analytics_client_id;
908
	}
909
910
	/**
911
	 * Get entrance code.
912
	 *
913
	 * @return string
914
	 */
915
	public function get_entrance_code() {
916
		return $this->entrance_code;
917
	}
918
919
	/**
920
	 * Set the credit card to use for this payment.
921
	 *
922
	 * @param CreditCard $credit_card Credit Card.
923
	 */
924
	public function set_credit_card( $credit_card ) {
925
		$this->credit_card = $credit_card;
926
	}
927
928
	/**
929
	 * Get the credit card to use for this payment.
930
	 *
931
	 * @return CreditCard|null
932
	 */
933
	public function get_credit_card() {
934
		return $this->credit_card;
935
	}
936
937
	/**
938
	 * Get payment subscription ID.
939
	 *
940
	 * @return string
941
	 */
942
	public function get_subscription_id() {
943
		return $this->subscription_id;
944
	}
945
946
	/**
947
	 * Get reucrring.
948
	 *
949
	 * @return TODO
950
	 */
951
	public function get_recurring() {
952
		return $this->recurring;
953
	}
954
}
955