Failed Conditions
Push — develop ( 1d8dee...aae3b6 )
by Reüel
14:33
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 MeprDb;
15
use MeprEmailFactory;
16
use MeprOptions;
17
use MeprProduct;
18
use MeprSubscription;
19
use MeprTransaction;
20
use MeprTransactionsHelper;
21
use MeprUser;
22
use MeprUtils;
23
use MeprView;
24
use Pronamic\WordPress\Pay\Core\PaymentMethods;
25
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
26
use Pronamic\WordPress\Pay\Payments\Payment;
27
use Pronamic\WordPress\Pay\Plugin;
28
use Pronamic\WordPress\Pay\Extensions\MemberPress\Pronamic;
29
use Pronamic\WordPress\Pay\Extensions\MemberPress\MemberPress;
30
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus;
31
use ReflectionClass;
32
33
/**
34
 * WordPress pay MemberPress gateway
35
 *
36
 * @author  Remco Tolsma
37
 * @version 2.0.4
38
 * @since   1.0.0
39
 */
40
class Gateway extends MeprBaseRealGateway {
41
	/**
42
	 * Payment method.
43
	 *
44
	 * @var string
45
	 */
46
	protected $payment_method;
47
48
	/**
49
	 * MemberPress transaction.
50
	 *
51
	 * @var MeprTransaction
52
	 */
53
	public $mp_txn;
54
55
	/**
56
	 * Pronamic payment.
57
	 *
58
	 * @var Payment
59
	 */
60
	public $pronamic_payment;
61
62
	/**
63
	 * Constructs and initialize iDEAL gateway.
64
	 */
65
	public function __construct() {
66
		// Set the name of this gateway.
67
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L12-13.
68
		$this->name = __( 'Pronamic', 'pronamic_ideal' );
69
70
		if ( ! empty( $this->payment_method ) ) {
71
			$this->name = sprintf(
72
				/* translators: %s: payment method name */
73
				__( 'Pronamic - %s', 'pronamic_ideal' ),
74
				PaymentMethods::get_name( $this->payment_method )
75
			);
76
		}
77
78
		// Set the default settings.
79
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73.
80
		$this->set_defaults();
81
82
		// Set the capabilities of this gateway.
83
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L36-37.
84
		$this->capabilities = array();
85
86
		// Setup the notification actions for this gateway.
87
		$this->notifiers = array();
88
	}
89
90
	/**
91
	 * Load the specified settings.
92
	 *
93
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L69-70
94
	 *
95
	 * @param array $settings MemberPress gateway settings array.
96
	 */
97
	public function load( $settings ) {
98
		$this->settings = (object) $settings;
99
100
		$this->set_defaults();
101
	}
102
103
	/**
104
	 * Custom helper function to send transaction notices.
105
	 *
106
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprUtils.php#L1333-L1351
107
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
108
	 *
109
	 * @param MeprTransaction $transaction MemberPress transaction object.
110
	 * @param string          $method      PHP function name to call.
111
	 *
112
	 * @return mixed
113
	 */
114
	public function send_transaction_notices( $transaction, $method ) {
115
		$class = 'MeprUtils';
116
117
		if ( ! Core_Util::class_method_exists( $class, $method ) ) {
118
			$class = $this;
119
		}
120
121
		if ( 'MeprUtils' === $class && 'send_product_welcome_notices' === $method ) {
122
			// `send_product_welcome_notices` is called from `send_signup_notices` in newer versions.
123
			return;
124
		}
125
126
		return call_user_func( array( $class, $method ), $transaction );
127
	}
128
129
	/**
130
	 * Get icon function (this is not a MemberPress function).
131
	 *
132
	 * @since 1.0.2
133
	 * @return string
134
	 */
135
	protected function get_icon() {
136
		return '';
137
	}
138
139
	/**
140
	 * Get class alias name.
141
	 *
142
	 * @return string
143
	 */
144
	public function get_alias() {
145
		return 'MeprPronamicGateway';
146
	}
147
148
	/**
149
	 * Set the default settings.
150
	 *
151
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73
152
	 */
153
	protected function set_defaults() {
154
		if ( ! isset( $this->settings ) ) {
155
			$this->settings = array();
156
		}
157
158
		$this->settings = (object) array_merge(
159
			array(
160
				'gateway'   => $this->get_alias(),
161
				'id'        => $this->generate_id(),
162
				'label'     => '',
163
				'use_label' => true,
164
				'icon'      => $this->get_icon(),
165
				'use_icon'  => true,
166
				'desc'      => '',
167
				'use_desc'  => true,
168
				'config_id' => '',
169
				'email'     => '',
170
				'sandbox'   => false,
171
				'debug'     => false,
172
			),
173
			(array) $this->settings
174
		);
175
176
		$this->id        = $this->settings->id;
177
		$this->label     = $this->settings->label;
178
		$this->use_label = $this->settings->use_label;
179
		$this->icon      = $this->settings->icon;
180
		$this->use_icon  = $this->settings->use_icon;
181
		$this->desc      = $this->settings->desc;
182
		$this->use_desc  = $this->settings->use_desc;
183
	}
184
185
	/**
186
	 * Process payment.
187
	 *
188
	 * @param MeprTransaction $txn MemberPress transaction object.
189
	 *
190
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L119-122
191
	 */
192
	public function process_payment( $txn ) {
193
194
	}
195
196
	/**
197
	 * Record subscription payment.
198
	 *
199
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L140-145
200
	 */
201
	public function record_subscription_payment() {
202
		$transaction = $this->mp_txn;
203
204
		$transaction->status = MeprTransaction::$complete_str;
205
		$transaction->store();
206
207
		$subscription = $transaction->subscription();
208
209
		if ( $subscription ) {
210
			if ( MeprSubscription::$active_str !== $subscription->status ) {
211
				$subscription->status = MeprSubscription::$active_str;
212
				$subscription->store();
213
			}
214
215
			$subscription->expire_confirmation_txn();
216
217
			$subscription->limit_payment_cycles();
218
		}
219
220
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
221
222
		return $transaction;
223
	}
224
225
	/**
226
	 * Record payment failure.
227
	 *
228
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L147-148
229
	 */
230
	public function record_payment_failure() {
231
		$transaction = $this->mp_txn;
232
233
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
234
		$transaction->status = MeprTransaction::$failed_str;
235
		$transaction->store();
236
237
		// Expire associated subscription transactions for non-recurring payments.
238
		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...
239
			$subscription = $transaction->subscription();
240
241
			if ( $subscription ) {
242
				$subscription->expire_txns();
243
				$subscription->store();
244
			}
245
		}
246
247
		$this->send_transaction_notices( $transaction, 'send_failed_txn_notices' );
248
249
		return $transaction;
250
	}
251
252
	/**
253
	 * Record payment.
254
	 *
255
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L124-129
256
	 */
257
	public function record_payment() {
258
		$transaction = $this->mp_txn;
259
260
		$transaction->status = MeprTransaction::$complete_str;
261
262
		// This will only work before maybe_cancel_old_sub is run.
263
		$upgrade   = $transaction->is_upgrade();
264
		$downgrade = $transaction->is_downgrade();
265
266
		$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...
267
268
		$subscription = $transaction->subscription();
269
270
		if ( $subscription ) {
271
			$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...
272
273
			$subscription->status     = MeprSubscription::$active_str;
274
			$subscription->created_at = $transaction->created_at;
275
			$subscription->store();
276
277
			if ( false === $event_transaction && false !== $event_subscription ) {
278
				$event_transaction = $event_subscription;
279
			}
280
		}
281
282
		$transaction->store();
283
284
		/*
285
		 * For some reasons the `send_product_welcome_notices` function accepts 1 or 3 arguments. We are not sure
286
		 * if this is a difference in the 'Business' and 'Developer' edition or between version `1.2.4` and `1.2.7`.
287
		 *
288
		 * @link https://github.com/wp-premium/memberpress-developer/blob/1.2.4/app/lib/MeprBaseGateway.php#L596-L612
289
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/lib/MeprBaseGateway.php#L609-L619
290
		 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L51
291
		 */
292
		$reflection = new ReflectionClass( 'MeprBaseRealGateway' );
293
294
		if ( $reflection->hasMethod( 'send_product_welcome_notices' ) && 3 === $reflection->getMethod( 'send_product_welcome_notices' )->getNumberOfParameters() ) {
295
			$uemail = MeprEmailFactory::fetch(
296
				'MeprUserProductWelcomeEmail',
297
				'MeprBaseProductEmail',
298
				array(
299
					array(
300
						'product_id' => $transaction->product_id,
301
					),
302
				)
303
			);
304
305
			/**
306
			 * The `send_product_welcome_notices` method is only available in earlier version of MemberPress.
307
			 *
308
			 * @scrutinizer ignore-call
309
			 */
310
			$this->send_product_welcome_notices(
311
				$uemail,
312
				MeprTransactionsHelper::get_email_params( $transaction ),
313
				$transaction->user()
314
			);
315
		} else {
316
			$this->send_transaction_notices( $transaction, 'send_product_welcome_notices' );
317
		}
318
319
		// Send upgrade/downgrade notices.
320
		$product = $transaction->product();
321
322
		if ( 'lifetime' === $product->period_type ) {
323
			if ( $upgrade ) {
324
				$this->upgraded_sub( $transaction, $event_transaction );
325
				$this->send_transaction_notices( $transaction, 'send_upgraded_txn_notices' );
326
			} elseif ( $downgrade ) {
327
				$this->downgraded_sub( $transaction, $event_transaction );
328
				$this->send_transaction_notices( $transaction, 'send_downgraded_txn_notices' );
329
			} else {
330
				$this->new_sub( $transaction );
331
			}
332
		}
333
334
		$this->send_transaction_notices( $transaction, 'send_signup_notices' );
335
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
336
337
		return $transaction;
338
	}
339
340
	/**
341
	 * Process refund.
342
	 *
343
	 * @param MeprTransaction $txn MemberPress transaction object.
344
	 *
345
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L131-133
346
	 */
347
	public function process_refund( MeprTransaction $txn ) {
348
349
	}
350
351
	/**
352
	 * Record refund.
353
	 *
354
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L135-138
355
	 */
356
	public function record_refund() {
357
358
	}
359
360
	/**
361
	 * Process trial payment.
362
	 *
363
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L150-157
364
	 *
365
	 * @param MeprTransaction $transaction MemberPress transaction object.
366
	 */
367
	public function process_trial_payment( $transaction ) {
368
369
	}
370
371
	/**
372
	 * Reord trial payment.
373
	 *
374
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L159-161
375
	 *
376
	 * @param MeprTransaction $transaction MemberPress transaction object.
377
	 */
378
	public function record_trial_payment( $transaction ) {
379
380
	}
381
382
	/**
383
	 * Process create subscription.
384
	 *
385
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L163-167
386
	 *
387
	 * @param MeprTransaction $txn MemberPress transaction object.
388
	 */
389
	public function process_create_subscription( $txn ) {
390
391
	}
392
393
	/**
394
	 * Record create subscription.
395
	 *
396
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L169-174
397
	 */
398
	public function record_create_subscription() {
399
400
	}
401
402
	/**
403
	 * Process update subscription.
404
	 *
405
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L176
406
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L194
407
	 *
408
	 * @param int $sub_id Subscription ID.
409
	 */
410
	public function process_update_subscription( $sub_id ) {
411
412
	}
413
414
	/**
415
	 * Record update subscription.
416
	 *
417
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L178-182
418
	 */
419
	public function record_update_subscription() {
420
421
	}
422
423
	/**
424
	 * Process suspend subscription.
425
	 *
426
	 * @param int $sub_id Subscription id.
427
	 *
428
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L184-186
429
	 */
430
	public function process_suspend_subscription( $sub_id ) {
431
		if ( ! MeprSubscription::exists( $sub_id ) ) {
432
			return;
433
		}
434
435
		$sub = new MeprSubscription( $sub_id );
436
437
		if ( MeprSubscription::$suspended_str === $sub->status ) {
438
			// Subscription is already suspended.
439
			return;
440
		}
441
442
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
443
444
		if ( ! $subscription ) {
445
			return;
446
		}
447
448
		$sub->status = MeprSubscription::$suspended_str;
449
450
		$sub->store();
451
452
		// Send suspended subscription notices.
453
		MeprUtils::send_suspended_sub_notices( $sub );
454
455
		$note = sprintf(
456
			/* translators: %s: MemberPress */
457
			__( '%s subscription on hold.', 'pronamic_ideal' ),
458
			__( 'MemberPress', 'pronamic_ideal' )
459
		);
460
461
		$subscription->add_note( $note );
462
463
		// The status of canceled or completed subscriptions will not be changed automatically.
464
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
465
			$subscription->set_status( SubscriptionStatus::ON_HOLD );
466
467
			$subscription->save();
468
		}
469
	}
470
471
	/**
472
	 * Record suspend subscription.
473
	 *
474
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L188-191
475
	 */
476
	public function record_suspend_subscription() {
477
478
	}
479
480
	/**
481
	 * Process resume subscription.
482
	 *
483
	 * @param int $sub_id Subscription id.
484
	 *
485
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L193-195
486
	 */
487
	public function process_resume_subscription( $sub_id ) {
488
		if ( ! MeprSubscription::exists( $sub_id ) ) {
489
			return;
490
		}
491
492
		$sub = new MeprSubscription( $sub_id );
493
494
		if ( MeprSubscription::$active_str === $sub->status ) {
495
			// Subscription is already active.
496
			return;
497
		}
498
499
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
500
501
		if ( ! $subscription ) {
502
			return;
503
		}
504
505
		$sub->status = MeprSubscription::$active_str;
506
507
		$sub->store();
508
509
		// Check if prior txn is expired yet or not, if so create a temporary txn so the user can access the content immediately.
510
		$prior_txn = $sub->latest_txn();
511
512
		if ( false === $prior_txn || ! ( $prior_txn instanceof MeprTransaction ) || strtotime( $prior_txn->expires_at ) < time() ) {
513
			$txn                  = new MeprTransaction();
514
			$txn->subscription_id = $sub->id;
515
			$txn->trans_num       = $sub->subscr_id . '-' . uniqid();
516
			$txn->status          = MeprTransaction::$confirmed_str;
517
			$txn->txn_type        = MeprTransaction::$subscription_confirmation_str;
518
			$txn->response        = (string) $sub;
519
			$txn->expires_at      = MeprUtils::ts_to_mysql_date( time() + MeprUtils::days( 1 ), 'Y-m-d 23:59:59' );
520
521
			$txn->set_subtotal( 0.00 ); // Just a confirmation txn.
522
523
			$txn->store();
524
		}
525
526
		// Send resumed subscription notices.
527
		MeprUtils::send_resumed_sub_notices( $sub );
528
529
		// Add note.
530
		$note = sprintf(
531
			/* translators: %s: MemberPress */
532
			__( '%s subscription reactivated.', 'pronamic_ideal' ),
533
			__( 'MemberPress', 'pronamic_ideal' )
534
		);
535
536
		$subscription->add_note( $note );
537
538
		// The status of canceled or completed subscriptions will not be changed automatically.
539
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
540
			$subscription->set_status( SubscriptionStatus::ACTIVE );
541
542
			$subscription->save();
543
		}
544
	}
545
546
	/**
547
	 * Record resume subscription.
548
	 *
549
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L197-201
550
	 */
551
	public function record_resume_subscription() {
552
553
	}
554
555
	/**
556
	 * Process cancel subscription.
557
	 *
558
	 * @param int $sub_id Subscription id.
559
	 *
560
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L202-206
561
	 */
562
	public function process_cancel_subscription( $sub_id ) {
563
		if ( ! MeprSubscription::exists( $sub_id ) ) {
564
			return;
565
		}
566
567
		$sub = new MeprSubscription( $sub_id );
568
569
		if ( MeprSubscription::$cancelled_str === $sub->status ) {
570
			// Subscription is already cancelled.
571
			return;
572
		}
573
574
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
575
576
		if ( ! $subscription ) {
577
			return;
578
		}
579
580
		$sub->status = MeprSubscription::$cancelled_str;
581
582
		$sub->store();
583
584
		// Expire the grace period (confirmation) if no completed payments have come through.
585
		if ( (int) $sub->txn_count <= 0 ) {
586
			$sub->expire_txns();
587
		}
588
589
		$sub->limit_reached_actions();
590
591
		// Send cancelled subscription notices.
592
		MeprUtils::send_cancelled_sub_notices( $sub );
593
594
		// Add note.
595
		$note = sprintf(
596
			/* translators: %s: MemberPress */
597
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
598
			__( 'MemberPress', 'pronamic_ideal' )
599
		);
600
601
		$subscription->add_note( $note );
602
603
		// The status of canceled or completed subscriptions will not be changed automatically.
604
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
605
			$subscription->set_status( SubscriptionStatus::CANCELLED );
606
607
			$subscription->save();
608
		}
609
	}
610
611
	/**
612
	 * Record cancel subscription.
613
	 *
614
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L208-212
615
	 */
616
	public function record_cancel_subscription() {
617
618
	}
619
620
	/**
621
	 * Process signup form.
622
	 *
623
	 * Gets called when the signup form is posted used for running any payment
624
	 * method specific actions when processing the customer signup form.
625
	 *
626
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L214-217
627
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L262
628
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L232-L235
629
	 *
630
	 * @param MeprTransaction $txn MemberPress transaction object.
631
	 */
632
	public function process_signup_form( $txn ) {
633
634
	}
635
636
	/**
637
	 * Payment redirect.
638
	 *
639
	 * @since 1.0.2
640
	 *
641
	 * @param MeprTransaction $txn MemberPress transaction object.
642
	 */
643
	public function payment_redirect( $txn ) {
644
		$txn = new MeprTransaction( $txn->id );
645
646
		// Gateway.
647
		$config_id = $this->settings->config_id;
648
649
		$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...
650
651
		if ( ! $gateway ) {
652
			return;
653
		}
654
655
		// Create Pronamic payment.
656
		$payment = Pronamic::get_payment( $txn );
657
658
		$payment->config_id = $this->settings->config_id;
659
		$payment->method    = $this->payment_method;
660
661
		$error = null;
662
663
		try {
664
			$payment = Plugin::start_payment( $payment );
665
		} catch ( \Exception $e ) {
666
			$error = $e;
667
		}
668
669
		/*
670
		 * Update transaction subtotal.
671
		 *
672
		 * Notes:
673
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
674
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
675
		 */
676
		$subscription = $txn->subscription();
677
678
		if ( $subscription && $subscription->in_trial() ) {
679
			$txn->set_subtotal( $subscription->trial_amount );
680
			$txn->store();
681
		}
682
683
		if ( $error instanceof \Exception ) {
684
			// Rethrow error, catched by MemberPress.
685
			throw $error;
686
		}
687
688
		// Redirect.
689
		$gateway->redirect( $payment );
690
	}
691
692
	/**
693
	 * Display payment page.
694
	 *
695
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
696
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L290
697
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L775-L850
698
	 *
699
	 * @param MeprTransaction $txn MemberPress transaction object.
700
	 */
701
	public function display_payment_page( $txn ) {
702
		// Gateway.
703
		$config_id = $this->settings->config_id;
704
705
		$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...
706
707
		if ( $gateway && '' === $gateway->get_input_html() ) {
708
			$this->payment_redirect( $txn );
709
		}
710
	}
711
712
	/**
713
	 * Process payment form.
714
	 *
715
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
716
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
717
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
718
	 *
719
	 * @param  MeprTransaction $txn MemberPress transaction object.
720
	 * @return bool
721
	 */
722
	public function process_payment_form( $txn ) {
723
		if ( ! filter_has_var( INPUT_POST, 'pronamic_pay_memberpress_pay' ) ) {
724
			return false;
725
		}
726
727
		// Gateway.
728
		$config_id = $this->settings->config_id;
729
730
		$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...
731
732
		if ( $gateway ) {
733
			$this->payment_redirect( $txn );
734
		}
735
	}
736
737
	/**
738
	 * Enqueue payment form scripts.
739
	 *
740
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
741
	 */
742
	public function enqueue_payment_form_scripts() {
743
744
	}
745
746
	/**
747
	 * Display payment form.
748
	 *
749
	 * This spits out html for the payment form on the registration / payment
750
	 * page for the user to fill out for payment.
751
	 *
752
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L230-233
753
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L248-L251
754
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L318
755
	 *
756
	 * @param float    $amount     Transaction amount to create a payment form for.
757
	 * @param MeprUser $user       MemberPress user object.
758
	 * @param int      $product_id Product ID.
759
	 * @param int      $txn_id     Transaction ID.
760
	 */
761
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
762
		$product = new MeprProduct( $product_id );
763
764
		$coupon = false;
765
766
		$txn = new MeprTransaction( $txn_id );
767
768
		// Artifically set the price of the $prd in case a coupon was used.
769
		if ( $product->price !== $amount ) {
770
			$coupon         = true;
771
			$product->price = $amount;
772
		}
773
774
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
775
776
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
777
		echo $invoice;
778
779
		?>
780
		<div class="mp_wrapper mp_payment_form_wrapper">
781
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
782
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
783
				<input type="hidden" name="mepr_transaction_id" value="<?php echo esc_attr( $txn_id ); ?>"/>
784
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
785
786
				<div class="mepr_spacer">&nbsp;</div>
787
788
				<?php
789
790
				// Gateway.
791
				$config_id = $this->settings->config_id;
792
793
				$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...
794
795
				if ( $gateway ) {
796
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
797
					echo $gateway->get_input_html();
798
				}
799
800
				?>
801
802
				<div class="mepr_spacer">&nbsp;</div>
803
804
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
805
				<img src="<?php echo esc_attr( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
806
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
807
808
				<noscript>
809
					<p class="mepr_nojs">
810
						<?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' ); ?>
811
					</p>
812
				</noscript>
813
			</form>
814
		</div>
815
		<?php
816
	}
817
818
	/**
819
	 * Validate payment form.
820
	 *
821
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L235-236
822
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L330
823
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L253-L254
824
	 *
825
	 * @param array $errors Array with errors.
826
	 * @return array
827
	 */
828
	public function validate_payment_form( $errors ) {
829
		return $errors;
830
	}
831
832
	/**
833
	 * Display options form.
834
	 *
835
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L291-292
836
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/gateways/MeprAuthorizeGateway.php#L1027-1037
837
	 */
838
	public function display_options_form() {
839
		$mepr_options = MeprOptions::fetch();
840
841
		?>
842
		<table>
843
			<tr>
844
				<?php
845
846
				$name = sprintf(
847
					'%s[%s][%s]',
848
					$mepr_options->integrations_str,
849
					$this->id,
850
					'config_id'
851
				);
852
853
				?>
854
				<td>
855
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
856
				</td>
857
				<td>
858
					<select name="<?php echo esc_attr( $name ); ?>">
859
						<?php
860
861
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
862
							printf(
863
								'<option value="%s" %s>%s</option>',
864
								esc_attr( $value ),
865
								selected( $value, $this->settings->config_id, false ),
866
								esc_html( $label )
867
							);
868
						}
869
870
						?>
871
					</select>
872
				</td>
873
			</tr>
874
		</table>
875
		<?php
876
	}
877
878
	/**
879
	 * Validate options form.
880
	 *
881
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L294-295
882
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L909-L924
883
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L416-L423
884
	 *
885
	 * @param array $errors Array with errors.
886
	 * @return array
887
	 */
888
	public function validate_options_form( $errors ) {
889
		return $errors;
890
	}
891
892
	/**
893
	 * Enqueue user account scripts.
894
	 *
895
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L297-302
896
	 */
897
	public function enqueue_user_account_scripts() {
898
899
	}
900
901
	/**
902
	 * Display update account form.
903
	 *
904
	 * @param int    $sub_id  Subscription ID.
905
	 * @param array  $errors  Array with errors.
906
	 * @param string $message Update message.
907
	 *
908
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L365-366
909
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseStaticGateway.php#L160-L161
910
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1108-L1168
911
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprAccountCtrl.php#L388
912
	 */
913
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
914
915
	}
916
917
	/**
918
	 * Validate update account form.
919
	 *
920
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L368-369
921
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1170-L1173
922
	 *
923
	 * @param  array $errors Array with errors.
924
	 * @return array
925
	 */
926
	public function validate_update_account_form( $errors = array() ) {
927
		return $errors;
928
	}
929
930
	/**
931
	 * Process update account form.
932
	 *
933
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L371-372
934
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1175-L1181
935
	 *
936
	 * @param int $sub_id Subscription ID.
937
	 */
938
	public function process_update_account_form( $sub_id ) {
939
940
	}
941
942
	/**
943
	 * Is test mode.
944
	 *
945
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
946
	 *
947
	 * @return boolean
948
	 */
949
	public function is_test_mode() {
950
		return false;
951
	}
952
953
	/**
954
	 * Force SSL.
955
	 *
956
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
957
	 *
958
	 * @return boolean
959
	 */
960
	public function force_ssl() {
961
		return false;
962
	}
963
}
964