Failed Conditions
Push — develop ( 29fa7c...45a5b9 )
by Reüel
08:46 queued 11s
created

src/Gateways/Gateway.php (7 issues)

1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress\Gateways;
12
13
use MeprBaseRealGateway;
14
use MeprEmailFactory;
15
use MeprOptions;
16
use MeprProduct;
17
use MeprSubscription;
18
use MeprTransaction;
19
use MeprTransactionsHelper;
20
use MeprUser;
21
use MeprUtils;
22
use MeprView;
23
use Pronamic\WordPress\Pay\Core\PaymentMethods;
24
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
25
use Pronamic\WordPress\Pay\Payments\Payment;
26
use Pronamic\WordPress\Pay\Plugin;
27
use Pronamic\WordPress\Pay\Extensions\MemberPress\Pronamic;
28
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus;
29
use ReflectionClass;
30
31
/**
32
 * WordPress pay MemberPress gateway
33
 *
34
 * @author  Remco Tolsma
35
 * @version 2.0.12
36
 * @since   1.0.0
37
 */
38
class Gateway extends MeprBaseRealGateway {
39
	/**
40
	 * Payment method.
41
	 *
42
	 * @var string
43
	 */
44
	protected $payment_method;
45
46
	/**
47
	 * MemberPress transaction.
48
	 *
49
	 * @var MeprTransaction
50
	 */
51
	public $mp_txn;
52
53
	/**
54
	 * Pronamic payment.
55
	 *
56
	 * @var Payment
57
	 */
58
	public $pronamic_payment;
59
60
	/**
61
	 * Constructs and initialize iDEAL gateway.
62
	 */
63
	public function __construct() {
64
		// Set the name of this gateway.
65
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L12-13.
66
		$this->name = __( 'Pronamic', 'pronamic_ideal' );
67
68
		if ( ! empty( $this->payment_method ) ) {
69
			$this->name = sprintf(
70
				/* translators: %s: payment method name */
71
				__( 'Pronamic - %s', 'pronamic_ideal' ),
72
				PaymentMethods::get_name( $this->payment_method )
73
			);
74
		}
75
76
		// Set the default settings.
77
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73.
78
		$this->set_defaults();
79
80
		// Set the capabilities of this gateway.
81
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L36-37.
82
		$this->capabilities = array();
83
84
		// Setup the notification actions for this gateway.
85
		$this->notifiers = array();
86
	}
87
88
	/**
89
	 * Load the specified settings.
90
	 *
91
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L69-70
92
	 *
93
	 * @param array $settings MemberPress gateway settings array.
94
	 */
95
	public function load( $settings ) {
96
		$this->settings = (object) $settings;
97
98
		$this->set_defaults();
99
	}
100
101
	/**
102
	 * Custom helper function to send transaction notices.
103
	 *
104
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprUtils.php#L1333-L1351
105
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
106
	 *
107
	 * @param MeprTransaction $transaction MemberPress transaction object.
108
	 * @param string          $method      PHP function name to call.
109
	 *
110
	 * @return mixed
111
	 */
112
	public function send_transaction_notices( $transaction, $method ) {
113
		$class = 'MeprUtils';
114
115
		if ( ! Core_Util::class_method_exists( $class, $method ) ) {
116
			$class = $this;
117
		}
118
119
		if ( 'MeprUtils' === $class && 'send_product_welcome_notices' === $method ) {
120
			// `send_product_welcome_notices` is called from `send_signup_notices` in newer versions.
121
			return;
122
		}
123
124
		return call_user_func( array( $class, $method ), $transaction );
125
	}
126
127
	/**
128
	 * Get icon function (this is not a MemberPress function).
129
	 *
130
	 * @since 1.0.2
131
	 * @return string
132
	 */
133
	protected function get_icon() {
134
		return '';
135
	}
136
137
	/**
138
	 * Get class alias name.
139
	 *
140
	 * @return string
141
	 */
142
	public function get_alias() {
143
		return 'MeprPronamicGateway';
144
	}
145
146
	/**
147
	 * Set the default settings.
148
	 *
149
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73
150
	 */
151
	protected function set_defaults() {
152
		if ( ! isset( $this->settings ) ) {
153
			$this->settings = array();
154
		}
155
156
		$this->settings = (object) array_merge(
157
			array(
158
				'gateway'   => $this->get_alias(),
159
				'id'        => $this->generate_id(),
160
				'label'     => '',
161
				'use_label' => true,
162
				'icon'      => $this->get_icon(),
163
				'use_icon'  => true,
164
				'desc'      => '',
165
				'use_desc'  => true,
166
				'config_id' => '',
167
				'email'     => '',
168
				'sandbox'   => false,
169
				'debug'     => false,
170
			),
171
			(array) $this->settings
172
		);
173
174
		$this->id        = $this->settings->id;
175
		$this->label     = $this->settings->label;
176
		$this->use_label = $this->settings->use_label;
177
		$this->icon      = $this->settings->icon;
178
		$this->use_icon  = $this->settings->use_icon;
179
		$this->desc      = $this->settings->desc;
180
		$this->use_desc  = $this->settings->use_desc;
181
	}
182
183
	/**
184
	 * Process payment.
185
	 *
186
	 * @param MeprTransaction $txn MemberPress transaction object.
187
	 *
188
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L119-122
189
	 */
190
	public function process_payment( $txn ) {
191
192
	}
193
194
	/**
195
	 * Record subscription payment.
196
	 *
197
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L140-145
198
	 */
199
	public function record_subscription_payment() {
200
		$transaction = $this->mp_txn;
201
202
		$transaction->status = MeprTransaction::$complete_str;
203
		$transaction->store();
204
205
		$subscription = $transaction->subscription();
206
207
		if ( $subscription ) {
208
			if ( MeprSubscription::$active_str !== $subscription->status ) {
209
				$subscription->status = MeprSubscription::$active_str;
210
				$subscription->store();
211
			}
212
213
			$subscription->expire_confirmation_txn();
214
215
			$subscription->limit_payment_cycles();
216
		}
217
218
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
219
220
		return $transaction;
221
	}
222
223
	/**
224
	 * Record payment failure.
225
	 *
226
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L147-148
227
	 */
228
	public function record_payment_failure() {
229
		$transaction = $this->mp_txn;
230
231
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
232
		$transaction->status = MeprTransaction::$failed_str;
233
		$transaction->store();
234
235
		// Expire associated subscription transactions for non-recurring payments.
236
		if ( ! ( isset( $this->pronamic_payment ) && $this->pronamic_payment->get_recurring() ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pronamic_payment->get_recurring() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
237
			$subscription = $transaction->subscription();
238
239
			if ( $subscription ) {
240
				$subscription->expire_txns();
241
				$subscription->store();
242
			}
243
		}
244
245
		$this->send_transaction_notices( $transaction, 'send_failed_txn_notices' );
246
247
		return $transaction;
248
	}
249
250
	/**
251
	 * Record payment.
252
	 *
253
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L124-129
254
	 */
255
	public function record_payment() {
256
		$transaction = $this->mp_txn;
257
258
		$transaction->status = MeprTransaction::$complete_str;
259
260
		// This will only work before maybe_cancel_old_sub is run.
261
		$upgrade   = $transaction->is_upgrade();
262
		$downgrade = $transaction->is_downgrade();
263
264
		$event_transaction = $transaction->maybe_cancel_old_sub();
0 ignored issues
show
Are you sure the assignment to $event_transaction is correct as $transaction->maybe_cancel_old_sub() targeting MeprTransaction::maybe_cancel_old_sub() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
265
266
		$subscription = $transaction->subscription();
267
268
		if ( $subscription ) {
269
			$event_subscription = $subscription->maybe_cancel_old_sub();
0 ignored issues
show
Are you sure the assignment to $event_subscription is correct as $subscription->maybe_cancel_old_sub() targeting MeprSubscription::maybe_cancel_old_sub() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
270
271
			$subscription->status     = MeprSubscription::$active_str;
272
			$subscription->created_at = $transaction->created_at;
273
			$subscription->store();
274
275
			if ( false === $event_transaction && false !== $event_subscription ) {
276
				$event_transaction = $event_subscription;
277
			}
278
		}
279
280
		$transaction->store();
281
282
		/*
283
		 * For some reasons the `send_product_welcome_notices` function accepts 1 or 3 arguments. We are not sure
284
		 * if this is a difference in the 'Business' and 'Developer' edition or between version `1.2.4` and `1.2.7`.
285
		 *
286
		 * @link https://github.com/wp-premium/memberpress-developer/blob/1.2.4/app/lib/MeprBaseGateway.php#L596-L612
287
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/lib/MeprBaseGateway.php#L609-L619
288
		 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L51
289
		 */
290
		$reflection = new ReflectionClass( 'MeprBaseRealGateway' );
291
292
		if ( $reflection->hasMethod( 'send_product_welcome_notices' ) && 3 === $reflection->getMethod( 'send_product_welcome_notices' )->getNumberOfParameters() ) {
293
			$uemail = MeprEmailFactory::fetch(
294
				'MeprUserProductWelcomeEmail',
295
				'MeprBaseProductEmail',
296
				array(
297
					array(
298
						'product_id' => $transaction->product_id,
299
					),
300
				)
301
			);
302
303
			/**
304
			 * The `send_product_welcome_notices` method is only available in earlier version of MemberPress.
305
			 *
306
			 * @scrutinizer ignore-call
307
			 */
308
			$this->send_product_welcome_notices(
309
				$uemail,
310
				MeprTransactionsHelper::get_email_params( $transaction ),
311
				$transaction->user()
312
			);
313
		} else {
314
			$this->send_transaction_notices( $transaction, 'send_product_welcome_notices' );
315
		}
316
317
		// Send upgrade/downgrade notices.
318
		$product = $transaction->product();
319
320
		if ( 'lifetime' === $product->period_type ) {
321
			if ( $upgrade ) {
322
				$this->upgraded_sub( $transaction, $event_transaction );
323
				$this->send_transaction_notices( $transaction, 'send_upgraded_txn_notices' );
324
			} elseif ( $downgrade ) {
325
				$this->downgraded_sub( $transaction, $event_transaction );
326
				$this->send_transaction_notices( $transaction, 'send_downgraded_txn_notices' );
327
			} else {
328
				$this->new_sub( $transaction );
329
			}
330
		}
331
332
		$this->send_transaction_notices( $transaction, 'send_signup_notices' );
333
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
334
335
		return $transaction;
336
	}
337
338
	/**
339
	 * Process refund.
340
	 *
341
	 * @param MeprTransaction $txn MemberPress transaction object.
342
	 *
343
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L131-133
344
	 */
345
	public function process_refund( MeprTransaction $txn ) {
346
347
	}
348
349
	/**
350
	 * Record refund.
351
	 *
352
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L135-138
353
	 */
354
	public function record_refund() {
355
356
	}
357
358
	/**
359
	 * Process trial payment.
360
	 *
361
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L150-157
362
	 *
363
	 * @param MeprTransaction $transaction MemberPress transaction object.
364
	 */
365
	public function process_trial_payment( $transaction ) {
366
367
	}
368
369
	/**
370
	 * Reord trial payment.
371
	 *
372
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L159-161
373
	 *
374
	 * @param MeprTransaction $transaction MemberPress transaction object.
375
	 */
376
	public function record_trial_payment( $transaction ) {
377
378
	}
379
380
	/**
381
	 * Process create subscription.
382
	 *
383
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L163-167
384
	 *
385
	 * @param MeprTransaction $txn MemberPress transaction object.
386
	 */
387
	public function process_create_subscription( $txn ) {
388
389
	}
390
391
	/**
392
	 * Record create subscription.
393
	 *
394
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L169-174
395
	 */
396
	public function record_create_subscription() {
397
398
	}
399
400
	/**
401
	 * Process update subscription.
402
	 *
403
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L176
404
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L194
405
	 *
406
	 * @param int $sub_id Subscription ID.
407
	 */
408
	public function process_update_subscription( $sub_id ) {
409
410
	}
411
412
	/**
413
	 * Record update subscription.
414
	 *
415
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L178-182
416
	 */
417
	public function record_update_subscription() {
418
419
	}
420
421
	/**
422
	 * Process suspend subscription.
423
	 *
424
	 * @param int $sub_id Subscription id.
425
	 *
426
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L184-186
427
	 */
428
	public function process_suspend_subscription( $sub_id ) {
429
		if ( ! MeprSubscription::exists( $sub_id ) ) {
430
			return;
431
		}
432
433
		$sub = new MeprSubscription( $sub_id );
434
435
		if ( MeprSubscription::$suspended_str === $sub->status ) {
436
			// Subscription is already suspended.
437
			return;
438
		}
439
440
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
441
442
		if ( ! $subscription ) {
443
			return;
444
		}
445
446
		$sub->status = MeprSubscription::$suspended_str;
447
448
		$sub->store();
449
450
		// Send suspended subscription notices.
451
		MeprUtils::send_suspended_sub_notices( $sub );
452
453
		$note = sprintf(
454
			/* translators: %s: MemberPress */
455
			__( '%s subscription on hold.', 'pronamic_ideal' ),
456
			__( 'MemberPress', 'pronamic_ideal' )
457
		);
458
459
		$subscription->add_note( $note );
460
461
		// The status of canceled or completed subscriptions will not be changed automatically.
462
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
463
			$subscription->set_status( SubscriptionStatus::ON_HOLD );
464
465
			$subscription->save();
466
		}
467
	}
468
469
	/**
470
	 * Record suspend subscription.
471
	 *
472
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L188-191
473
	 */
474
	public function record_suspend_subscription() {
475
476
	}
477
478
	/**
479
	 * Process resume subscription.
480
	 *
481
	 * @param int $sub_id Subscription id.
482
	 *
483
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L193-195
484
	 */
485
	public function process_resume_subscription( $sub_id ) {
486
		if ( ! MeprSubscription::exists( $sub_id ) ) {
487
			return;
488
		}
489
490
		$sub = new MeprSubscription( $sub_id );
491
492
		if ( MeprSubscription::$active_str === $sub->status ) {
493
			// Subscription is already active.
494
			return;
495
		}
496
497
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
498
499
		if ( ! $subscription ) {
500
			return;
501
		}
502
503
		$sub->status = MeprSubscription::$active_str;
504
505
		$sub->store();
506
507
		// Check if prior txn is expired yet or not, if so create a temporary txn so the user can access the content immediately.
508
		$prior_txn = $sub->latest_txn();
509
510
		if ( false === $prior_txn || ! ( $prior_txn instanceof MeprTransaction ) || strtotime( $prior_txn->expires_at ) < time() ) {
511
			$txn                  = new MeprTransaction();
512
			$txn->subscription_id = $sub->id;
513
			$txn->trans_num       = $sub->subscr_id . '-' . uniqid();
514
			$txn->status          = MeprTransaction::$confirmed_str;
515
			$txn->txn_type        = MeprTransaction::$subscription_confirmation_str;
516
			$txn->response        = (string) $sub;
517
			$txn->expires_at      = MeprUtils::ts_to_mysql_date( time() + MeprUtils::days( 1 ), 'Y-m-d 23:59:59' );
518
519
			$txn->set_subtotal( 0.00 ); // Just a confirmation txn.
520
521
			$txn->store();
522
		}
523
524
		// Send resumed subscription notices.
525
		MeprUtils::send_resumed_sub_notices( $sub );
526
527
		// Add note.
528
		$note = sprintf(
529
			/* translators: %s: MemberPress */
530
			__( '%s subscription reactivated.', 'pronamic_ideal' ),
531
			__( 'MemberPress', 'pronamic_ideal' )
532
		);
533
534
		$subscription->add_note( $note );
535
536
		// The status of canceled or completed subscriptions will not be changed automatically.
537
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
538
			$subscription->set_status( SubscriptionStatus::ACTIVE );
539
540
			$subscription->save();
541
		}
542
	}
543
544
	/**
545
	 * Record resume subscription.
546
	 *
547
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L197-201
548
	 */
549
	public function record_resume_subscription() {
550
551
	}
552
553
	/**
554
	 * Process cancel subscription.
555
	 *
556
	 * @param int $sub_id Subscription id.
557
	 *
558
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L202-206
559
	 */
560
	public function process_cancel_subscription( $sub_id ) {
561
		if ( ! MeprSubscription::exists( $sub_id ) ) {
562
			return;
563
		}
564
565
		$sub = new MeprSubscription( $sub_id );
566
567
		if ( MeprSubscription::$cancelled_str === $sub->status ) {
568
			// Subscription is already cancelled.
569
			return;
570
		}
571
572
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
573
574
		if ( ! $subscription ) {
575
			return;
576
		}
577
578
		$sub->status = MeprSubscription::$cancelled_str;
579
580
		$sub->store();
581
582
		// Expire the grace period (confirmation) if no completed payments have come through.
583
		if ( (int) $sub->txn_count <= 0 ) {
584
			$sub->expire_txns();
585
		}
586
587
		$sub->limit_reached_actions();
588
589
		// Send cancelled subscription notices.
590
		MeprUtils::send_cancelled_sub_notices( $sub );
591
592
		// Add note.
593
		$note = sprintf(
594
			/* translators: %s: MemberPress */
595
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
596
			__( 'MemberPress', 'pronamic_ideal' )
597
		);
598
599
		$subscription->add_note( $note );
600
601
		// The status of canceled or completed subscriptions will not be changed automatically.
602
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
603
			$subscription->set_status( SubscriptionStatus::CANCELLED );
604
605
			$subscription->save();
606
		}
607
	}
608
609
	/**
610
	 * Record cancel subscription.
611
	 *
612
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L208-212
613
	 */
614
	public function record_cancel_subscription() {
615
616
	}
617
618
	/**
619
	 * Process signup form.
620
	 *
621
	 * Gets called when the signup form is posted used for running any payment
622
	 * method specific actions when processing the customer signup form.
623
	 *
624
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L214-217
625
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L262
626
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L232-L235
627
	 *
628
	 * @param MeprTransaction $txn MemberPress transaction object.
629
	 */
630
	public function process_signup_form( $txn ) {
631
632
	}
633
634
	/**
635
	 * Payment redirect.
636
	 *
637
	 * @param MeprTransaction $txn MemberPress transaction object.
638
	 *
639
	 * @throws \Exception Throws exception on gateway payment start error.
640
	 * @since 1.0.2
641
	 */
642
	public function payment_redirect( $txn ) {
643
		$txn = new MeprTransaction( $txn->id );
644
645
		// Gateway.
646
		$config_id = $this->settings->config_id;
647
648
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
649
650
		if ( ! $gateway ) {
651
			return;
652
		}
653
654
		// Create Pronamic payment.
655
		$payment = Pronamic::get_payment( $txn );
656
657
		$payment->config_id = $this->settings->config_id;
658
		$payment->method    = $this->payment_method;
659
660
		$error = null;
661
662
		try {
663
			$payment = Plugin::start_payment( $payment );
664
		} catch ( \Exception $e ) {
665
			$error = $e;
666
		}
667
668
		/*
669
		 * Update transaction subtotal.
670
		 *
671
		 * Notes:
672
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
673
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
674
		 */
675
		$subscription = $txn->subscription();
676
677
		if ( $subscription && $subscription->in_trial() ) {
678
			$txn->set_subtotal( $subscription->trial_amount );
679
			$txn->store();
680
		}
681
682
		if ( $error instanceof \Exception ) {
683
			// Rethrow error, catched by MemberPress.
684
			throw $error;
685
		}
686
687
		// Redirect.
688
		$gateway->redirect( $payment );
689
	}
690
691
	/**
692
	 * Display payment page.
693
	 *
694
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
695
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L290
696
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L775-L850
697
	 *
698
	 * @param MeprTransaction $txn MemberPress transaction object.
699
	 *
700
	 * @throws \Exception Throws exception on gateway payment start error.
701
	 */
702
	public function display_payment_page( $txn ) {
703
		// Gateway.
704
		$config_id = $this->settings->config_id;
705
706
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
707
708
		if ( $gateway && '' === $gateway->get_input_html() ) {
709
			$this->payment_redirect( $txn );
710
		}
711
	}
712
713
	/**
714
	 * Process payment form.
715
	 *
716
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
717
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
718
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
719
	 *
720
	 * @param MeprTransaction $txn MemberPress transaction object.
721
	 *
722
	 * @return bool
723
	 * @throws \Exception Throws exception on gateway payment start error.
724
	 */
725
	public function process_payment_form( $txn ) {
726
		if ( ! filter_has_var( INPUT_POST, 'pronamic_pay_memberpress_pay' ) ) {
727
			return false;
728
		}
729
730
		// Gateway.
731
		$config_id = $this->settings->config_id;
732
733
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
734
735
		if ( $gateway ) {
736
			$this->payment_redirect( $txn );
737
		}
738
	}
739
740
	/**
741
	 * Enqueue payment form scripts.
742
	 *
743
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
744
	 */
745
	public function enqueue_payment_form_scripts() {
746
747
	}
748
749
	/**
750
	 * Display payment form.
751
	 *
752
	 * This spits out html for the payment form on the registration / payment
753
	 * page for the user to fill out for payment.
754
	 *
755
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L230-233
756
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L248-L251
757
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L318
758
	 *
759
	 * @param float    $amount     Transaction amount to create a payment form for.
760
	 * @param MeprUser $user       MemberPress user object.
761
	 * @param int      $product_id Product ID.
762
	 * @param int      $txn_id     Transaction ID.
763
	 */
764
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
765
		$product = new MeprProduct( $product_id );
766
767
		$coupon = false;
768
769
		$txn = new MeprTransaction( $txn_id );
770
771
		// Artifically set the price of the $prd in case a coupon was used.
772
		if ( $product->price !== $amount ) {
773
			$coupon         = true;
774
			$product->price = $amount;
775
		}
776
777
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
778
779
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
780
		echo $invoice;
781
782
		?>
783
		<div class="mp_wrapper mp_payment_form_wrapper">
784
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
785
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
786
				<input type="hidden" name="mepr_transaction_id" value="<?php echo esc_attr( $txn_id ); ?>"/>
787
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
788
789
				<div class="mepr_spacer">&nbsp;</div>
790
791
				<?php
792
793
				// Gateway.
794
				$config_id = $this->settings->config_id;
795
796
				$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
797
798
				if ( $gateway ) {
799
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
800
					echo $gateway->get_input_html();
801
				}
802
803
				?>
804
805
				<div class="mepr_spacer">&nbsp;</div>
806
807
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
808
				<img src="<?php echo esc_attr( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
809
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
810
811
				<noscript>
812
					<p class="mepr_nojs">
813
						<?php esc_html_e( 'JavaScript is disabled in your browser. You will not be able to complete your purchase until you either enable JavaScript in your browser, or switch to a browser that supports it.', 'pronamic_ideal' ); ?>
814
					</p>
815
				</noscript>
816
			</form>
817
		</div>
818
		<?php
819
	}
820
821
	/**
822
	 * Validate payment form.
823
	 *
824
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L235-236
825
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L330
826
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L253-L254
827
	 *
828
	 * @param array $errors Array with errors.
829
	 * @return array
830
	 */
831
	public function validate_payment_form( $errors ) {
832
		return $errors;
833
	}
834
835
	/**
836
	 * Display options form.
837
	 *
838
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L291-292
839
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/gateways/MeprAuthorizeGateway.php#L1027-1037
840
	 */
841
	public function display_options_form() {
842
		$mepr_options = MeprOptions::fetch();
843
844
		?>
845
		<table>
846
			<tr>
847
				<?php
848
849
				$name = sprintf(
850
					'%s[%s][%s]',
851
					$mepr_options->integrations_str,
852
					$this->id,
853
					'config_id'
854
				);
855
856
				?>
857
				<td>
858
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
859
				</td>
860
				<td>
861
					<select name="<?php echo esc_attr( $name ); ?>">
862
						<?php
863
864
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
865
							printf(
866
								'<option value="%s" %s>%s</option>',
867
								esc_attr( $value ),
868
								selected( $value, $this->settings->config_id, false ),
869
								esc_html( $label )
870
							);
871
						}
872
873
						?>
874
					</select>
875
				</td>
876
			</tr>
877
		</table>
878
		<?php
879
	}
880
881
	/**
882
	 * Validate options form.
883
	 *
884
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L294-295
885
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L909-L924
886
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L416-L423
887
	 *
888
	 * @param array $errors Array with errors.
889
	 * @return array
890
	 */
891
	public function validate_options_form( $errors ) {
892
		return $errors;
893
	}
894
895
	/**
896
	 * Enqueue user account scripts.
897
	 *
898
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L297-302
899
	 */
900
	public function enqueue_user_account_scripts() {
901
902
	}
903
904
	/**
905
	 * Display update account form.
906
	 *
907
	 * @param int    $sub_id  Subscription ID.
908
	 * @param array  $errors  Array with errors.
909
	 * @param string $message Update message.
910
	 *
911
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L365-366
912
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseStaticGateway.php#L160-L161
913
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1108-L1168
914
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprAccountCtrl.php#L388
915
	 */
916
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
917
918
	}
919
920
	/**
921
	 * Validate update account form.
922
	 *
923
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L368-369
924
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1170-L1173
925
	 *
926
	 * @param  array $errors Array with errors.
927
	 * @return array
928
	 */
929
	public function validate_update_account_form( $errors = array() ) {
930
		return $errors;
931
	}
932
933
	/**
934
	 * Process update account form.
935
	 *
936
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L371-372
937
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1175-L1181
938
	 *
939
	 * @param int $sub_id Subscription ID.
940
	 */
941
	public function process_update_account_form( $sub_id ) {
942
943
	}
944
945
	/**
946
	 * Is test mode.
947
	 *
948
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
949
	 *
950
	 * @return boolean
951
	 */
952
	public function is_test_mode() {
953
		return false;
954
	}
955
956
	/**
957
	 * Force SSL.
958
	 *
959
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
960
	 *
961
	 * @return boolean
962
	 */
963
	public function force_ssl() {
964
		return false;
965
	}
966
}
967