Failed Conditions
Push — develop ( 240468...f473d3 )
by Remco
06:11
created

Gateway   F

Complexity

Total Complexity 87

Size/Duplication

Total Lines 1033
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 27
Bugs 1 Features 0
Metric Value
eloc 322
dl 0
loc 1033
ccs 0
cts 420
cp 0
rs 2
c 27
b 1
f 0
wmc 87

39 Methods

Rating   Name   Duplication   Size   Complexity  
B process_resume_subscription() 0 56 8
A record_suspend_subscription() 0 1 1
A process_create_subscription() 0 1 1
A record_refund() 0 1 1
A process_update_subscription() 0 1 1
A record_trial_payment() 0 1 1
A process_trial_payment() 0 1 1
A record_update_subscription() 0 1 1
A process_suspend_subscription() 0 38 5
A record_create_subscription() 0 1 1
A process_payment() 0 1 1
A record_subscription_payment() 0 36 3
A force_ssl() 0 2 1
A process_refund() 0 1 1
A get_config_id() 0 15 3
B process_cancel_subscription() 0 55 6
A display_update_account_form() 0 29 2
A record_resume_subscription() 0 1 1
A process_payment_form() 0 12 2
A enqueue_user_account_scripts() 0 1 1
A spc_payment_fields() 0 20 3
B display_payment_form() 0 69 5
B record_payment() 0 53 7
A is_test_mode() 0 2 1
A record_payment_failure() 0 24 4
A validate_options_form() 0 2 1
A set_defaults() 0 30 2
A process_update_account_form() 0 1 1
A get_icon() 0 2 1
A validate_payment_form() 0 2 1
A record_cancel_subscription() 0 1 1
A process_signup_form() 0 1 1
A load() 0 4 1
A validate_update_account_form() 0 2 1
A enqueue_payment_form_scripts() 0 1 1
B payment_redirect() 0 52 6
A __construct() 0 39 3
A display_options_form() 0 34 2
A display_payment_page() 0 17 3

How to fix   Complexity   

Complex Class

Complex classes like Gateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Gateway, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 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.3.1
36
 * @since   1.0.0
37
 */
38
class Gateway extends MeprBaseRealGateway {
39
	/**
40
	 * Payment method.
41
	 *
42
	 * @var string|null
43
	 */
44
	protected $payment_method;
45
46
	/**
47
	 * Class alias.
48
	 *
49
	 * @var string
50
	 */
51
	protected $class_alias;
52
53
	/**
54
	 * Key.
55
	 * 
56
	 * The key property is not defined in the MemberPress library,
57
	 * but it is a MemberPress property.
58
	 * 
59
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php
60
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L12
61
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/helpers/MeprOptionsHelper.php#L192
62
	 * @var string
63
	 */
64
	public $key;
65
66
	/**
67
	 * MemberPress transaction.
68
	 *
69
	 * @var MeprTransaction
70
	 */
71
	public $mp_txn;
72
73
	/**
74
	 * Pronamic payment.
75
	 *
76
	 * @var Payment
77
	 */
78
	public $pronamic_payment;
79
80
	/**
81
	 * Constructs and initialize gateway.
82
	 * 
83
	 * @param string      $class_alias    Class alias.
84
	 * @param string|null $payment_method Payment method.
85
	 */
86
	public function __construct( $class_alias = 'MeprPronamicGateway', $payment_method = null ) {
87
		$this->class_alias = $class_alias;
88
89
		$this->payment_method = $payment_method;
90
91
		// Set the name of this gateway.
92
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L12-13.
93
		$this->name = __( 'Pronamic', 'pronamic_ideal' );
94
95
		if ( ! empty( $this->payment_method ) ) {
96
			$this->name = sprintf(
97
				/* translators: %s: payment method name */
98
				__( 'Pronamic - %s', 'pronamic_ideal' ),
99
				PaymentMethods::get_name( $this->payment_method )
100
			);
101
		}
102
103
		// Set the default settings.
104
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73.
105
		$this->set_defaults();
106
107
		// Set the capabilities of this gateway.
108
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L36-37.
109
		$this->capabilities = array();
110
111
		// Setup the notification actions for this gateway.
112
		$this->notifiers = array();
113
114
		// Support single-page checkout.
115
		$this->has_spc_form = true;
116
117
		// Key.
118
		$key = 'pronamic_pay';
119
120
		if ( null !== $this->payment_method ) {
121
			$key = sprintf( 'pronamic_pay_%s', $this->payment_method );
122
		}
123
124
		$this->key = $key;
125
	}
126
127
	/**
128
	 * Load the specified settings.
129
	 *
130
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L73-L74
131
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprGatewayFactory.php#L18
132
	 * @param mixed $settings MemberPress gateway settings array.
133
	 * @return void
134
	 */
135
	public function load( $settings ) {
136
		$this->settings = (object) $settings;
137
138
		$this->set_defaults();
139
	}
140
141
	/**
142
	 * Get icon function (this is not a MemberPress function).
143
	 *
144
	 * @since 1.0.2
145
	 * @return string|null
146
	 */
147
	protected function get_icon() {
148
		return PaymentMethods::get_icon_url( $this->payment_method );
149
	}
150
151
	/**
152
	 * Set the default settings.
153
	 *
154
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L76-L77
155
	 * @return void
156
	 */
157
	protected function set_defaults() {
158
		if ( ! isset( $this->settings ) ) {
159
			$this->settings = array();
160
		}
161
162
		$this->settings = (object) array_merge(
163
			array(
164
				'gateway'   => $this->class_alias,
165
				'id'        => $this->generate_id(),
166
				'label'     => '',
167
				'use_label' => true,
168
				'icon'      => $this->get_icon(),
169
				'use_icon'  => true,
170
				'desc'      => '',
171
				'use_desc'  => true,
172
				'config_id' => '',
173
				'email'     => '',
174
				'sandbox'   => false,
175
				'debug'     => false,
176
			),
177
			(array) $this->settings
178
		);
179
180
		$this->id        = $this->settings->id;
181
		$this->label     = $this->settings->label;
182
		$this->use_label = $this->settings->use_label;
183
		$this->icon      = $this->settings->icon;
184
		$this->use_icon  = $this->settings->use_icon;
185
		$this->desc      = $this->settings->desc;
186
		$this->use_desc  = $this->settings->use_desc;
187
	}
188
189
	/**
190
	 * Process payment.
191
	 *
192
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L149-L152
193
	 * @param MeprTransaction $txn MemberPress transaction object.
194
	 * @return void
195
	 */
196
	public function process_payment( $txn ) {
197
198
	}
199
200
	/**
201
	 * Record subscription payment.
202
	 *
203
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L170-L175
204
	 * @return void
205
	 */
206
	public function record_subscription_payment() {
207
		$transaction = $this->mp_txn;
208
209
		$transaction->status     = MeprTransaction::$complete_str;
210
		$transaction->expires_at = MeprUtils::ts_to_mysql_date( $this->pronamic_payment->get_end_date()->getTimestamp(), 'Y-m-d 23:59:59' );
0 ignored issues
show
Bug Best Practice introduced by
The property expires_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
211
		$transaction->store();
212
213
		$subscription = $transaction->subscription();
214
215
		if ( $subscription ) {
216
			$should_activate = ! \in_array(
217
				$subscription->status,
218
				array(
219
					MeprSubscription::$active_str,
220
					MeprSubscription::$cancelled_str,
221
				),
222
				true
223
			);
224
225
			if ( $should_activate ) {
226
				$subscription->status = MeprSubscription::$active_str;
227
				$subscription->store();
228
			}
229
230
			$subscription->expire_confirmation_txn();
231
232
			$subscription->limit_payment_cycles();
233
		}
234
235
		/**
236
		 * Send transaction receipt notices.
237
		 * 
238
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
239
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L249
240
		 */
241
		MeprUtils::send_transaction_receipt_notices( $transaction );
242
	}
243
244
	/**
245
	 * Record payment failure.
246
	 *
247
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L177-L178
248
	 * @return void
249
	 */
250
	public function record_payment_failure() {
251
		$transaction = $this->mp_txn;
252
253
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
254
		$transaction->status = MeprTransaction::$failed_str;
255
		$transaction->store();
256
257
		// Expire associated subscription transactions for non-recurring payments.
258
		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...
259
			$subscription = $transaction->subscription();
260
261
			if ( $subscription ) {
262
				$subscription->expire_txns();
263
				$subscription->store();
264
			}
265
		}
266
267
		/**
268
		 * Send failed transaction notices.
269
		 * 
270
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
271
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
272
		 */
273
		MeprUtils::send_failed_txn_notices( $transaction );
274
	}
275
276
	/**
277
	 * Record payment.
278
	 *
279
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L154-L159
280
	 * @return void
281
	 */
282
	public function record_payment() {
283
		$transaction = $this->mp_txn;
284
285
		$transaction->status = MeprTransaction::$complete_str;
286
287
		// This will only work before maybe_cancel_old_sub is run.
288
		$upgrade   = $transaction->is_upgrade();
289
		$downgrade = $transaction->is_downgrade();
290
291
		$event_transaction = $transaction->maybe_cancel_old_sub();
292
293
		$subscription = $transaction->subscription();
294
295
		if ( $subscription ) {
296
			$event_subscription = $subscription->maybe_cancel_old_sub();
297
298
			$subscription->status     = MeprSubscription::$active_str;
299
			$subscription->created_at = $transaction->created_at;
300
			$subscription->store();
301
302
			if ( false === $event_transaction && false !== $event_subscription ) {
303
				$event_transaction = $event_subscription;
304
			}
305
		}
306
307
		$transaction->store();
308
309
		// Send upgrade/downgrade notices.
310
		$product = $transaction->product();
311
312
		if ( 'lifetime' === $product->period_type ) {
313
			if ( $upgrade ) {
314
				$this->upgraded_sub( $transaction, $event_transaction );
315
			} elseif ( $downgrade ) {
316
				$this->downgraded_sub( $transaction, $event_transaction );
317
			} else {
318
				$this->new_sub( $transaction );
319
			}
320
		}
321
322
		/**
323
		 * Send signup notices.
324
		 * 
325
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
326
		 */
327
		MeprUtils::send_signup_notices( $transaction );
328
329
		/**
330
		 * Send transaction receipt notices.
331
		 * 
332
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
333
		 */
334
		MeprUtils::send_transaction_receipt_notices( $transaction );
335
	}
336
337
	/**
338
	 * Process refund.
339
	 *
340
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L161-L163
341
	 * @param MeprTransaction $txn MemberPress transaction object.
342
	 * @return void
343
	 */
344
	public function process_refund( MeprTransaction $txn ) {
345
346
	}
347
348
	/**
349
	 * Record refund.
350
	 *
351
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L165-L168
352
	 * @return void
353
	 */
354
	public function record_refund() {
355
356
	}
357
358
	/**
359
	 * Process trial payment.
360
	 *
361
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L180-L187
362
	 * @param MeprTransaction $transaction MemberPress transaction object.
363
	 * @return void
364
	 */
365
	public function process_trial_payment( $transaction ) {
366
367
	}
368
369
	/**
370
	 * Record trial payment.
371
	 *
372
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L189-L191
373
	 * @param MeprTransaction $transaction MemberPress transaction object.
374
	 * @return void
375
	 */
376
	public function record_trial_payment( $transaction ) {
377
378
	}
379
380
	/**
381
	 * Process create subscription.
382
	 *
383
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L193-L197
384
	 * @param MeprTransaction $txn MemberPress transaction object.
385
	 * @return void
386
	 */
387
	public function process_create_subscription( $txn ) {
388
389
	}
390
391
	/**
392
	 * Record create subscription.
393
	 *
394
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L199-L204
395
	 * @return void
396
	 */
397
	public function record_create_subscription() {
398
399
	}
400
401
	/**
402
	 * Process update subscription.
403
	 *
404
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L206
405
	 * @param int $sub_id Subscription ID.
406
	 * @return void
407
	 */
408
	public function process_update_subscription( $sub_id ) {
409
410
	}
411
412
	/**
413
	 * Record update subscription.
414
	 *
415
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L208-L212
416
	 * @return void
417
	 */
418
	public function record_update_subscription() {
419
420
	}
421
422
	/**
423
	 * Process suspend subscription.
424
	 *
425
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L214-L216
426
	 * @param int $sub_id Subscription id.
427
	 * @return void
428
	 */
429
	public function process_suspend_subscription( $sub_id ) {
430
		if ( ! MeprSubscription::exists( $sub_id ) ) {
431
			return;
432
		}
433
434
		$sub = new MeprSubscription( $sub_id );
435
436
		if ( MeprSubscription::$suspended_str === $sub->status ) {
437
			// Subscription is already suspended.
438
			return;
439
		}
440
441
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
442
443
		if ( ! $subscription ) {
444
			return;
445
		}
446
447
		$sub->status = MeprSubscription::$suspended_str;
448
449
		$sub->store();
450
451
		// Send suspended subscription notices.
452
		MeprUtils::send_suspended_sub_notices( $sub );
453
454
		$note = sprintf(
455
			/* translators: %s: extension name */
456
			__( '%s subscription on hold.', 'pronamic_ideal' ),
457
			__( 'MemberPress', 'pronamic_ideal' )
458
		);
459
460
		$subscription->add_note( $note );
461
462
		// The status of canceled or completed subscriptions will not be changed automatically.
463
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
464
			$subscription->set_status( SubscriptionStatus::ON_HOLD );
465
466
			$subscription->save();
467
		}
468
	}
469
470
	/**
471
	 * Record suspend subscription.
472
	 *
473
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L218-L221
474
	 * @return void
475
	 */
476
	public function record_suspend_subscription() {
477
478
	}
479
480
	/**
481
	 * Process resume subscription.
482
	 *
483
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L223-L225
484
	 * @param int $sub_id Subscription id.
485
	 * @return void
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: extension name */
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://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L227-L230
550
	 * @return void
551
	 */
552
	public function record_resume_subscription() {
553
554
	}
555
556
	/**
557
	 * Process cancel subscription.
558
	 *
559
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L232-L236
560
	 * @param int $sub_id Subscription id.
561
	 * @return void
562
	 */
563
	public function process_cancel_subscription( $sub_id ) {
564
		if ( ! MeprSubscription::exists( $sub_id ) ) {
565
			return;
566
		}
567
568
		$sub = new MeprSubscription( $sub_id );
569
570
		if ( MeprSubscription::$cancelled_str === $sub->status ) {
571
			// Subscription is already cancelled.
572
			return;
573
		}
574
575
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
576
577
		if ( ! $subscription ) {
578
			return;
579
		}
580
581
		// Add note.
582
		$note = sprintf(
583
			/* translators: %s: extension name */
584
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
585
			__( 'MemberPress', 'pronamic_ideal' )
586
		);
587
588
		$subscription->add_note( $note );
589
590
		// The status of canceled or completed subscriptions will not be changed automatically.
591
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
592
			$subscription->set_status( SubscriptionStatus::CANCELLED );
593
594
			$subscription->next_payment_date          = null;
595
			$subscription->next_payment_delivery_date = null;
596
597
			// Delete next payment post meta.
598
			$subscription->set_meta( 'next_payment', null );
599
			$subscription->set_meta( 'next_payment_delivery_date', null );
600
601
			$subscription->save();
602
		}
603
604
		// Cancel MemberPress subscription.
605
		$sub->status = MeprSubscription::$cancelled_str;
606
607
		$sub->store();
608
609
		// Expire the grace period (confirmation) if no completed payments have come through.
610
		if ( (int) $sub->txn_count <= 0 ) {
611
			$sub->expire_txns();
612
		}
613
614
		$sub->limit_reached_actions();
615
616
		// Send cancelled subscription notices.
617
		MeprUtils::send_cancelled_sub_notices( $sub );
618
	}
619
620
	/**
621
	 * Record cancel subscription.
622
	 *
623
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L238-L242
624
	 * @return void
625
	 */
626
	public function record_cancel_subscription() {
627
628
	}
629
630
	/**
631
	 * Process signup form.
632
	 *
633
	 * Gets called when the signup form is posted used for running any payment
634
	 * method specific actions when processing the customer signup form.
635
	 *
636
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L244-L247
637
	 * @param MeprTransaction $txn MemberPress transaction object.
638
	 * @return void
639
	 */
640
	public function process_signup_form( $txn ) {
641
642
	}
643
644
	/**
645
	 * Payment redirect.
646
	 * 
647
	 * Note: this is not a MemberPress method.
648
	 *
649
	 * @since 1.0.2
650
	 * @param MeprTransaction $txn MemberPress transaction object.
651
	 * @return void
652
	 * @throws \Exception Throws exception on gateway payment start error.
653
	 */
654
	private function payment_redirect( $txn ) {
655
		// Gateway.
656
		$config_id = $this->get_config_id();
657
658
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
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...
659
660
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
661
			return;
662
		}
663
664
		// Create Pronamic payment.
665
		$txn = new MeprTransaction( $txn->id );
666
667
		$payment = Pronamic::get_payment( $txn );
668
669
		$payment->config_id = $config_id;
670
		$payment->method    = $this->payment_method;
671
672
		$error = null;
673
674
		try {
675
			$payment = Plugin::start_payment( $payment );
676
		} catch ( \Exception $e ) {
677
			$error = $e;
678
		}
679
680
		/*
681
		 * Update trial transaction.
682
		 *
683
		 * Notes:
684
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
685
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
686
		 * - Reload transaction to make sure actual status is being used (i.e. on free downgrade).
687
		 */
688
		$txn = new MeprTransaction( $txn->id );
689
690
		$subscription = $txn->subscription();
691
692
		if ( $subscription && $subscription->in_trial() ) {
693
			$txn->expires_at = MeprUtils::ts_to_mysql_date( $payment->get_end_date()->getTimestamp(), 'Y-m-d 23:59:59' );
694
695
			$txn->set_subtotal( $subscription->trial_amount );
696
			$txn->store();
697
		}
698
699
		if ( $error instanceof \Exception ) {
700
			// Rethrow error, caught by MemberPress.
701
			throw $error;
702
		}
703
704
		// Redirect.
705
		$gateway->redirect( $payment );
706
	}
707
708
	/**
709
	 * Display payment page.
710
	 *
711
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L249-L253
712
	 * @param MeprTransaction $txn MemberPress transaction object.
713
	 * @return void
714
	 * @throws \Exception Throws exception on gateway payment start error.
715
	 */
716
	public function display_payment_page( $txn ) {
717
		// Gateway.
718
		$config_id = $this->get_config_id();
719
720
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
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...
721
722
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
723
			return;
724
		}
725
726
		// Redirect payment on empty input HTML.
727
		$gateway->set_payment_method( $this->payment_method );
728
729
		$html = $gateway->get_input_html();
730
731
		if ( empty( $html ) ) {
732
			$this->payment_redirect( $txn );
733
		}
734
	}
735
736
	/**
737
	 * Process payment form.
738
	 *
739
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
740
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
741
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
742
	 *
743
	 * @param MeprTransaction $txn MemberPress transaction object.
744
	 *
745
	 * @return void
746
	 * @throws \Exception Throws exception on gateway payment start error.
747
	 */
748
	public function process_payment_form( $txn ) {
749
		// Gateway.
750
		$config_id = $this->get_config_id();
751
752
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
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...
753
754
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
755
			return;
756
		}
757
758
		// Redirect.
759
		$this->payment_redirect( $txn );
760
	}
761
762
	/**
763
	 * Enqueue payment form scripts.
764
	 *
765
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L255-L258
766
	 * @return void
767
	 */
768
	public function enqueue_payment_form_scripts() {
769
770
	}
771
772
	/**
773
	 * Display payment form.
774
	 *
775
	 * This spits out html for the payment form on the registration / payment
776
	 * page for the user to fill out for payment.
777
	 *
778
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L571
779
	 * @param float    $amount     Transaction amount to create a payment form for.
780
	 * @param MeprUser $user       MemberPress user object.
781
	 * @param int      $product_id Product ID.
782
	 * @param int      $txn_id     Transaction ID.
783
	 * @return void
784
	 */
785
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
786
		// Gateway.
787
		$config_id = $this->get_config_id();
788
789
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
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...
790
791
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
792
793
			$admin_message = null;
794
795
			if ( \current_user_can( 'manage_options' ) ) {
796
				$admin_message = __( 'For admins only: check payment method settings in MemberPress.', 'pronamic_ideal' );
797
			}
798
799
			printf(
800
				'<div class="mp_wrapper mp_payment_form_wrapper"><ul><li>%s</li>%s</ul></div>',
801
				\esc_html( Plugin::get_default_error_message() ),
802
				null === $admin_message ? '' : sprintf( '<li><em>%s</em></li>', \esc_html( $admin_message ) )
803
			);
804
805
			return;
806
		}
807
808
		// Invoice.
809
		$product = new MeprProduct( $product_id );
810
811
		$coupon = false;
812
813
		$txn = new MeprTransaction( $txn_id );
814
815
		// Artificially set the price of the $prd in case a coupon was used.
816
		if ( $product->price !== $amount ) {
817
			$coupon         = true;
818
			$product->price = $amount;
819
		}
820
821
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
822
823
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
824
		echo $invoice;
825
826
		?>
827
		<div class="mp_wrapper mp_payment_form_wrapper">
828
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
829
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
830
				<input type="hidden" name="mepr_transaction_id" value="<?php echo \esc_attr( (string) $txn_id ); ?>"/>
831
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
832
833
				<div class="mepr_spacer">&nbsp;</div>
834
835
				<?php
836
837
				$gateway->set_payment_method( $this->payment_method );
838
839
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
840
				echo $gateway->get_input_html();
841
842
				?>
843
844
				<div class="mepr_spacer">&nbsp;</div>
845
846
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
847
				<img src="<?php echo \esc_url( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
848
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
849
850
				<noscript>
851
					<p class="mepr_nojs">
852
						<?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' ); ?>
853
					</p>
854
				</noscript>
855
			</form>
856
		</div>
857
		<?php
858
	}
859
860
	/**
861
	 * Single-page checkout payment fields.
862
	 *
863
	 * @return string
864
	 */
865
	public function spc_payment_fields() {
866
		// Gateway.
867
		$config_id = $this->get_config_id();
868
869
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
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...
870
871
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
872
			return '';
873
		}
874
875
		// Input HTML.
876
		$gateway->set_payment_method( $this->payment_method );
877
878
		$html = $gateway->get_input_html();
879
880
		if ( empty( $html ) ) {
881
			return '';
882
		}
883
884
		return $html;
885
	}
886
887
	/**
888
	 * Validate payment form.
889
	 *
890
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L648
891
	 * @param string[] $errors Array with errors.
892
	 * @return string[]
893
	 */
894
	public function validate_payment_form( $errors ) {
895
		return $errors;
896
	}
897
898
	/**
899
	 * Display options form.
900
	 *
901
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/views/admin/options/gateway.php#L41
902
	 * @return void
903
	 */
904
	public function display_options_form() {
905
		$mepr_options = MeprOptions::fetch();
906
907
		?>
908
		<table>
909
			<tr>
910
				<?php
911
912
				$name = sprintf(
913
					'%s[%s][%s]',
914
					$mepr_options->integrations_str,
915
					$this->id,
916
					'config_id'
917
				);
918
919
				?>
920
				<td>
921
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
922
				</td>
923
				<td>
924
					<select name="<?php echo esc_attr( $name ); ?>">
925
						<?php
926
927
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
928
							printf(
929
								'<option value="%s" %s>%s</option>',
930
								esc_attr( $value ),
931
								selected( $value, $this->settings->config_id, false ),
932
								esc_html( $label )
933
							);
934
						}
935
936
						?>
937
					</select>
938
				</td>
939
			</tr>
940
		</table>
941
		<?php
942
	}
943
944
	/**
945
	 * Validate options form.
946
	 *
947
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L468
948
	 * @param string[] $errors Array with errors.
949
	 * @return string[]
950
	 */
951
	public function validate_options_form( $errors ) {
952
		return $errors;
953
	}
954
955
	/**
956
	 * Enqueue user account scripts.
957
	 *
958
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L126
959
	 * @return void
960
	 */
961
	public function enqueue_user_account_scripts() {
962
963
	}
964
965
	/**
966
	 * Display update account form.
967
	 *
968
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L423
969
	 * @param int      $sub_id  Subscription ID.
970
	 * @param string[] $errors  Array with errors.
971
	 * @param string   $message Update message.
972
	 * @return void
973
	 */
974
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
975
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress', (int) $sub_id );
976
977
		$message = \__( 'The payment method for this subscription can not be updated manually.', 'pronamic_ideal' );
978
979
		if ( \is_array( $subscriptions ) ) {
0 ignored issues
show
introduced by
The condition is_array($subscriptions) is always true.
Loading history...
980
			$subscription = \array_shift( $subscriptions );
981
982
			$message = \sprintf(
983
				/* translators: %s: mandate selection URL anchor */
984
				\__( 'To update the payment method for this subscription, please visit the %s page.', 'pronamic_ideal' ),
985
				\sprintf(
986
					'<a href="%1$s" title="%2$s">%3$s</a>',
987
					\esc_url( $subscription->get_mandate_selection_url() ),
988
					\esc_attr( \__( 'payment method update', 'pronamic_ideal' ) ),
989
					\esc_html( \__( 'payment method update', 'pronamic_ideal' ) )
990
				)
991
			);
992
		}
993
994
		?>
995
996
		<h3>
997
			<?php echo \esc_html( __( 'Update payment method', 'pronamic_ideal' ) ); ?>
998
		</h3>
999
1000
		<div>
1001
			<?php echo \wp_kses_post( $message ); ?>
1002
		</div>
1003
1004
		<?php
1005
	}
1006
1007
	/**
1008
	 * Validate update account form.
1009
	 *
1010
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L1182-L1197
1011
	 * @param string[] $errors Array with errors.
1012
	 * @return string[]
1013
	 */
1014
	public function validate_update_account_form( $errors = array() ) {
1015
		return $errors;
1016
	}
1017
1018
	/**
1019
	 * Process update account form.
1020
	 *
1021
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L430
1022
	 * @param int $sub_id Subscription ID.
1023
	 * @return void
1024
	 */
1025
	public function process_update_account_form( $sub_id ) {
1026
1027
	}
1028
1029
	/**
1030
	 * Is test mode.
1031
	 *
1032
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
1033
	 *
1034
	 * @return boolean
1035
	 */
1036
	public function is_test_mode() {
1037
		return false;
1038
	}
1039
1040
	/**
1041
	 * Force SSL.
1042
	 *
1043
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
1044
	 *
1045
	 * @return boolean
1046
	 */
1047
	public function force_ssl() {
1048
		return false;
1049
	}
1050
1051
	/**
1052
	 * Get config ID.
1053
	 *
1054
	 * @return string|int|null
1055
	 */
1056
	protected function get_config_id() {
1057
		// Get config ID setting.
1058
		$config_id = $this->settings->config_id;
1059
1060
		// Check empty config ID.
1061
		if ( empty( $config_id ) ) {
1062
			$config_id = \get_option( 'pronamic_pay_config_id' );
1063
		}
1064
1065
		// Check empty config ID.
1066
		if ( empty( $config_id ) ) {
1067
			$config_id = null;
1068
		}
1069
1070
		return $config_id;
1071
	}
1072
}
1073