Failed Conditions
Push — develop ( 2dbcf1...bdd916 )
by Remco
06:44
created

src/Gateways/Gateway.php (12 issues)

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|null
70
	 */
71
	private $memberpress_transaction;
72
73
	/**
74
	 * Pronamic payment.
75
	 *
76
	 * @var Payment|null
77
	 */
78
	private $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
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L520-L585
194
	 * @param MeprTransaction $txn MemberPress transaction object.
195
	 * @return void
196
	 */
197
	public function process_payment( $txn ) {
198
199
	}
200
201
	/**
202
	 * Set record data.
203
	 * 
204
	 * @param Payment         $pronamic_payment        Pronamic payment.
205
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction.
206
	 */
207
	public function set_record_data( $pronamic_payment, $memberpress_transaction ) {
208
		$this->pronamic_payment        = $pronamic_payment;
209
		$this->memberpress_transaction = $memberpress_transaction;
210
	}
211
212
	/**
213
	 * Get payment method.
214
	 * 
215
	 * @var string|null
216
	 */
217
	public function get_payment_method() {
218
		return $this->payment_method;
219
	}
220
221
	/**
222
	 * Record subscription payment.
223
	 *
224
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L170-L175
225
	 * @return void
226
	 */
227
	public function record_subscription_payment() {
228
		if ( nulll === $this->pronamic_payment ) {
0 ignored issues
show
The constant Pronamic\WordPress\Pay\E...berPress\Gateways\nulll was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
229
			return;
230
		}
231
232
		if ( nulll === $this->memberpress_transaction ) {
233
			return;
234
		}
235
236
		$transaction = $this->memberpress_transaction;
237
238
		$transaction->status     = MeprTransaction::$complete_str;
239
		$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
The method get_end_date() does not exist on null. ( Ignorable by Annotation )

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

239
		$transaction->expires_at = MeprUtils::ts_to_mysql_date( $this->pronamic_payment->/** @scrutinizer ignore-call */ get_end_date()->getTimestamp(), 'Y-m-d 23:59:59' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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...
240
		$transaction->store();
0 ignored issues
show
The method store() does not exist on null. ( Ignorable by Annotation )

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

240
		$transaction->/** @scrutinizer ignore-call */ 
241
                store();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
242
		$subscription = $transaction->subscription();
243
244
		if ( $subscription ) {
245
			$should_activate = ! \in_array(
246
				$subscription->status,
247
				array(
248
					MeprSubscription::$active_str,
249
					MeprSubscription::$cancelled_str,
250
				),
251
				true
252
			);
253
254
			if ( $should_activate ) {
255
				$subscription->status = MeprSubscription::$active_str;
256
				$subscription->store();
257
			}
258
259
			$subscription->expire_confirmation_txn();
260
261
			$subscription->limit_payment_cycles();
262
		}
263
264
		/**
265
		 * Send transaction receipt notices.
266
		 * 
267
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
268
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L249
269
		 */
270
		MeprUtils::send_transaction_receipt_notices( $transaction );
271
	}
272
273
	/**
274
	 * Record payment failure.
275
	 *
276
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L177-L178
277
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L833-L910
278
	 * @return void
279
	 */
280
	public function record_payment_failure() {
281
		if ( nulll === $this->pronamic_payment ) {
0 ignored issues
show
The constant Pronamic\WordPress\Pay\E...berPress\Gateways\nulll was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
282
			return;
283
		}
284
285
		if ( nulll === $this->memberpress_transaction ) {
286
			return;
287
		}
288
289
		$transaction = $this->memberpress_transaction;
290
291
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
292
		$transaction->status = MeprTransaction::$failed_str;
293
		$transaction->store();
294
295
		// Expire associated subscription transactions for non-recurring payments.
296
		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...
297
			$subscription = $transaction->subscription();
298
299
			if ( $subscription ) {
300
				$subscription->expire_txns();
301
				$subscription->store();
302
			}
303
		}
304
305
		/**
306
		 * Send failed transaction notices.
307
		 * 
308
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
309
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
310
		 */
311
		MeprUtils::send_failed_txn_notices( $transaction );
312
	}
313
314
	/**
315
	 * Record payment.
316
	 *
317
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L154-L159
318
	 * @return void
319
	 */
320
	public function record_payment() {
321
		if ( nulll === $this->pronamic_payment ) {
0 ignored issues
show
The constant Pronamic\WordPress\Pay\E...berPress\Gateways\nulll was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
322
			return;
323
		}
324
325
		if ( nulll === $this->memberpress_transaction ) {
326
			return;
327
		}
328
329
		$transaction = $this->memberpress_transaction;
330
331
		$transaction->status = MeprTransaction::$complete_str;
332
333
		// This will only work before maybe_cancel_old_sub is run.
334
		$upgrade   = $transaction->is_upgrade();
335
		$downgrade = $transaction->is_downgrade();
336
337
		$event_transaction = $transaction->maybe_cancel_old_sub();
338
339
		$subscription = $transaction->subscription();
340
341
		if ( $subscription ) {
342
			$event_subscription = $subscription->maybe_cancel_old_sub();
343
344
			$subscription->status     = MeprSubscription::$active_str;
345
			$subscription->created_at = $transaction->created_at;
346
			$subscription->store();
347
348
			if ( false === $event_transaction && false !== $event_subscription ) {
349
				$event_transaction = $event_subscription;
350
			}
351
		}
352
353
		$transaction->store();
354
355
		// Send upgrade/downgrade notices.
356
		$product = $transaction->product();
357
358
		if ( 'lifetime' === $product->period_type ) {
359
			if ( $upgrade ) {
360
				$this->upgraded_sub( $transaction, $event_transaction );
361
			} elseif ( $downgrade ) {
362
				$this->downgraded_sub( $transaction, $event_transaction );
363
			} else {
364
				$this->new_sub( $transaction );
365
			}
366
		}
367
368
		/**
369
		 * Send signup notices.
370
		 * 
371
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
372
		 */
373
		MeprUtils::send_signup_notices( $transaction );
374
375
		/**
376
		 * Send transaction receipt notices.
377
		 * 
378
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
379
		 */
380
		MeprUtils::send_transaction_receipt_notices( $transaction );
381
	}
382
383
	/**
384
	 * Process refund.
385
	 *
386
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L161-L163
387
	 * @param MeprTransaction $txn MemberPress transaction object.
388
	 * @return void
389
	 */
390
	public function process_refund( MeprTransaction $txn ) {
391
392
	}
393
394
	/**
395
	 * Record refund.
396
	 *
397
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L165-L168
398
	 * @return void
399
	 */
400
	public function record_refund() {
401
402
	}
403
404
	/**
405
	 * Process trial payment.
406
	 *
407
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L180-L187
408
	 * @param MeprTransaction $transaction MemberPress transaction object.
409
	 * @return void
410
	 */
411
	public function process_trial_payment( $transaction ) {
412
413
	}
414
415
	/**
416
	 * Record trial payment.
417
	 *
418
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L189-L191
419
	 * @param MeprTransaction $transaction MemberPress transaction object.
420
	 * @return void
421
	 */
422
	public function record_trial_payment( $transaction ) {
423
424
	}
425
426
	/**
427
	 * Process create subscription.
428
	 *
429
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L193-L197
430
	 * @param MeprTransaction $txn MemberPress transaction object.
431
	 * @return void
432
	 */
433
	public function process_create_subscription( $txn ) {
434
435
	}
436
437
	/**
438
	 * Record create subscription.
439
	 *
440
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L199-L204
441
	 * @return void
442
	 */
443
	public function record_create_subscription() {
444
445
	}
446
447
	/**
448
	 * Process update subscription.
449
	 *
450
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L206
451
	 * @param int $sub_id Subscription ID.
452
	 * @return void
453
	 */
454
	public function process_update_subscription( $sub_id ) {
455
456
	}
457
458
	/**
459
	 * Record update subscription.
460
	 *
461
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L208-L212
462
	 * @return void
463
	 */
464
	public function record_update_subscription() {
465
466
	}
467
468
	/**
469
	 * Process suspend subscription.
470
	 *
471
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L214-L216
472
	 * @param int $sub_id Subscription id.
473
	 * @return void
474
	 */
475
	public function process_suspend_subscription( $sub_id ) {
476
		if ( ! MeprSubscription::exists( $sub_id ) ) {
477
			return;
478
		}
479
480
		$sub = new MeprSubscription( $sub_id );
481
482
		if ( MeprSubscription::$suspended_str === $sub->status ) {
483
			// Subscription is already suspended.
484
			return;
485
		}
486
487
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
488
489
		if ( ! $subscription ) {
490
			return;
491
		}
492
493
		$sub->status = MeprSubscription::$suspended_str;
494
495
		$sub->store();
496
497
		// Send suspended subscription notices.
498
		MeprUtils::send_suspended_sub_notices( $sub );
499
500
		$note = sprintf(
501
			/* translators: %s: extension name */
502
			__( '%s subscription on hold.', 'pronamic_ideal' ),
503
			__( 'MemberPress', 'pronamic_ideal' )
504
		);
505
506
		$subscription->add_note( $note );
507
508
		// The status of canceled or completed subscriptions will not be changed automatically.
509
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
510
			$subscription->set_status( SubscriptionStatus::ON_HOLD );
511
512
			$subscription->save();
513
		}
514
	}
515
516
	/**
517
	 * Record suspend subscription.
518
	 *
519
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L218-L221
520
	 * @return void
521
	 */
522
	public function record_suspend_subscription() {
523
524
	}
525
526
	/**
527
	 * Process resume subscription.
528
	 *
529
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L223-L225
530
	 * @param int $sub_id Subscription id.
531
	 * @return void
532
	 */
533
	public function process_resume_subscription( $sub_id ) {
534
		if ( ! MeprSubscription::exists( $sub_id ) ) {
535
			return;
536
		}
537
538
		$sub = new MeprSubscription( $sub_id );
539
540
		if ( MeprSubscription::$active_str === $sub->status ) {
541
			// Subscription is already active.
542
			return;
543
		}
544
545
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
546
547
		if ( ! $subscription ) {
548
			return;
549
		}
550
551
		$sub->status = MeprSubscription::$active_str;
552
553
		$sub->store();
554
555
		// Check if prior txn is expired yet or not, if so create a temporary txn so the user can access the content immediately.
556
		$prior_txn = $sub->latest_txn();
557
558
		if ( false === $prior_txn || ! ( $prior_txn instanceof MeprTransaction ) || strtotime( $prior_txn->expires_at ) < time() ) {
559
			$txn                  = new MeprTransaction();
560
			$txn->subscription_id = $sub->id;
561
			$txn->trans_num       = $sub->subscr_id . '-' . uniqid();
562
			$txn->status          = MeprTransaction::$confirmed_str;
563
			$txn->txn_type        = MeprTransaction::$subscription_confirmation_str;
564
			$txn->response        = (string) $sub;
565
			$txn->expires_at      = MeprUtils::ts_to_mysql_date( time() + MeprUtils::days( 1 ), 'Y-m-d 23:59:59' );
566
567
			$txn->set_subtotal( 0.00 ); // Just a confirmation txn.
568
569
			$txn->store();
570
		}
571
572
		// Send resumed subscription notices.
573
		MeprUtils::send_resumed_sub_notices( $sub );
574
575
		// Add note.
576
		$note = sprintf(
577
			/* translators: %s: extension name */
578
			__( '%s subscription reactivated.', 'pronamic_ideal' ),
579
			__( 'MemberPress', 'pronamic_ideal' )
580
		);
581
582
		$subscription->add_note( $note );
583
584
		// The status of canceled or completed subscriptions will not be changed automatically.
585
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
586
			$subscription->set_status( SubscriptionStatus::ACTIVE );
587
588
			$subscription->save();
589
		}
590
	}
591
592
	/**
593
	 * Record resume subscription.
594
	 *
595
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L227-L230
596
	 * @return void
597
	 */
598
	public function record_resume_subscription() {
599
600
	}
601
602
	/**
603
	 * Process cancel subscription.
604
	 *
605
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L232-L236
606
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1687-L1715
607
	 * @param int $sub_id Subscription id.
608
	 * @return void
609
	 */
610
	public function process_cancel_subscription( $sub_id ) {
611
		if ( ! MeprSubscription::exists( $sub_id ) ) {
612
			return;
613
		}
614
615
		$sub = new MeprSubscription( $sub_id );
616
617
		if ( MeprSubscription::$cancelled_str === $sub->status ) {
618
			// Subscription is already cancelled.
619
			return;
620
		}
621
622
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
623
624
		if ( ! $subscription ) {
625
			return;
626
		}
627
628
		// Add note.
629
		$note = sprintf(
630
			/* translators: %s: extension name */
631
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
632
			__( 'MemberPress', 'pronamic_ideal' )
633
		);
634
635
		$subscription->add_note( $note );
636
637
		// The status of canceled or completed subscriptions will not be changed automatically.
638
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
639
			$subscription->set_status( SubscriptionStatus::CANCELLED );
640
641
			$subscription->next_payment_date          = null;
642
			$subscription->next_payment_delivery_date = null;
643
644
			// Delete next payment post meta.
645
			$subscription->set_meta( 'next_payment', null );
646
			$subscription->set_meta( 'next_payment_delivery_date', null );
647
648
			$subscription->save();
649
		}
650
651
		// Cancel MemberPress subscription.
652
		$sub->status = MeprSubscription::$cancelled_str;
653
654
		$sub->store();
655
656
		// Expire the grace period (confirmation) if no completed payments have come through.
657
		if ( (int) $sub->txn_count <= 0 ) {
658
			$sub->expire_txns();
659
		}
660
661
		$sub->limit_reached_actions();
662
663
		// Send cancelled subscription notices.
664
		MeprUtils::send_cancelled_sub_notices( $sub );
665
	}
666
667
	/**
668
	 * Record cancel subscription.
669
	 *
670
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L238-L242
671
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1717-L1753
672
	 * @return void
673
	 */
674
	public function record_cancel_subscription() {
675
676
	}
677
678
	/**
679
	 * Process signup form.
680
	 *
681
	 * Gets called when the signup form is posted used for running any payment
682
	 * method specific actions when processing the customer signup form.
683
	 *
684
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L244-L247
685
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1755-L1764
686
	 * @param MeprTransaction $txn MemberPress transaction object.
687
	 * @return void
688
	 */
689
	public function process_signup_form( $txn ) {
690
691
	}
692
693
	/**
694
	 * Payment redirect.
695
	 * 
696
	 * Note: this is not a MemberPress method.
697
	 *
698
	 * @since 1.0.2
699
	 * @param MeprTransaction $txn MemberPress transaction object.
700
	 * @return void
701
	 * @throws \Exception Throws exception on gateway payment start error.
702
	 */
703
	private function payment_redirect( $txn ) {
704
		// Gateway.
705
		$config_id = $this->get_config_id();
706
707
		$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...
708
709
		if ( null === $gateway ) {
710
			return;
711
		}
712
713
		// Create Pronamic payment.
714
		$txn = new MeprTransaction( $txn->id );
715
716
		$payment = Pronamic::get_payment( $txn );
717
718
		$payment->config_id = $config_id;
719
		$payment->method    = $this->payment_method;
720
721
		$error = null;
722
723
		try {
724
			$payment = Plugin::start_payment( $payment );
725
		} catch ( \Exception $e ) {
726
			$error = $e;
727
		}
728
729
		/*
730
		 * Update trial transaction.
731
		 *
732
		 * Notes:
733
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
734
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
735
		 * - Reload transaction to make sure actual status is being used (i.e. on free downgrade).
736
		 */
737
		$txn = new MeprTransaction( $txn->id );
738
739
		$subscription = $txn->subscription();
740
741
		if ( $subscription && $subscription->in_trial() ) {
742
			$txn->expires_at = MeprUtils::ts_to_mysql_date( $payment->get_end_date()->getTimestamp(), 'Y-m-d 23:59:59' );
743
744
			$txn->set_subtotal( $subscription->trial_amount );
745
			$txn->store();
746
		}
747
748
		if ( $error instanceof \Exception ) {
749
			// Rethrow error, caught by MemberPress.
750
			throw $error;
751
		}
752
753
		// Redirect.
754
		$gateway->redirect( $payment );
755
	}
756
757
	/**
758
	 * Display payment page.
759
	 *
760
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L249-L253
761
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1766-L1768
762
	 * @param MeprTransaction $txn MemberPress transaction object.
763
	 * @return void
764
	 * @throws \Exception Throws exception on gateway payment start error.
765
	 */
766
	public function display_payment_page( $txn ) {
767
		// Gateway.
768
		$config_id = $this->get_config_id();
769
770
		$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...
771
772
		if ( null === $gateway ) {
773
			return;
774
		}
775
776
		// Redirect payment on empty input HTML.
777
		$gateway->set_payment_method( $this->payment_method );
778
779
		$html = $gateway->get_input_html();
780
781
		if ( empty( $html ) ) {
782
			$this->payment_redirect( $txn );
783
		}
784
	}
785
786
	/**
787
	 * Process payment form.
788
	 *
789
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
790
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
791
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
792
	 *
793
	 * @param MeprTransaction $txn MemberPress transaction object.
794
	 *
795
	 * @return void
796
	 * @throws \Exception Throws exception on gateway payment start error.
797
	 */
798
	public function process_payment_form( $txn ) {
799
		// Gateway.
800
		$config_id = $this->get_config_id();
801
802
		$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...
803
804
		if ( null === $gateway ) {
805
			return;
806
		}
807
808
		// Redirect.
809
		$this->payment_redirect( $txn );
810
	}
811
812
	/**
813
	 * Enqueue payment form scripts.
814
	 *
815
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L255-L258
816
	 * @return void
817
	 */
818
	public function enqueue_payment_form_scripts() {
819
820
	}
821
822
	/**
823
	 * Display payment form.
824
	 *
825
	 * This spits out html for the payment form on the registration / payment
826
	 * page for the user to fill out for payment.
827
	 *
828
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L571
829
	 * @param float    $amount     Transaction amount to create a payment form for.
830
	 * @param MeprUser $user       MemberPress user object.
831
	 * @param int      $product_id Product ID.
832
	 * @param int      $txn_id     Transaction ID.
833
	 * @return void
834
	 */
835
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
836
		// Gateway.
837
		$config_id = $this->get_config_id();
838
839
		$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...
840
841
		if ( null === $gateway ) {
842
843
			$admin_message = null;
844
845
			if ( \current_user_can( 'manage_options' ) ) {
846
				$admin_message = __( 'For admins only: check payment method settings in MemberPress.', 'pronamic_ideal' );
847
			}
848
849
			printf(
850
				'<div class="mp_wrapper mp_payment_form_wrapper"><ul><li>%s</li>%s</ul></div>',
851
				\esc_html( Plugin::get_default_error_message() ),
852
				null === $admin_message ? '' : sprintf( '<li><em>%s</em></li>', \esc_html( $admin_message ) )
853
			);
854
855
			return;
856
		}
857
858
		// Invoice.
859
		$product = new MeprProduct( $product_id );
860
861
		$coupon = false;
862
863
		$txn = new MeprTransaction( $txn_id );
864
865
		// Artificially set the price of the $prd in case a coupon was used.
866
		if ( $product->price !== $amount ) {
867
			$coupon         = true;
868
			$product->price = $amount;
869
		}
870
871
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
872
873
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
874
		echo $invoice;
875
876
		?>
877
		<div class="mp_wrapper mp_payment_form_wrapper">
878
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
879
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
880
				<input type="hidden" name="mepr_transaction_id" value="<?php echo \esc_attr( (string) $txn_id ); ?>"/>
881
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
882
883
				<div class="mepr_spacer">&nbsp;</div>
884
885
				<?php
886
887
				$gateway->set_payment_method( $this->payment_method );
888
889
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
890
				echo $gateway->get_input_html();
891
892
				?>
893
894
				<div class="mepr_spacer">&nbsp;</div>
895
896
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
897
				<img src="<?php echo \esc_url( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
898
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
899
900
				<noscript>
901
					<p class="mepr_nojs">
902
						<?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' ); ?>
903
					</p>
904
				</noscript>
905
			</form>
906
		</div>
907
		<?php
908
	}
909
910
	/**
911
	 * Single-page checkout payment fields.
912
	 *
913
	 * @return string
914
	 */
915
	public function spc_payment_fields() {
916
		// Gateway.
917
		$config_id = $this->get_config_id();
918
919
		$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...
920
921
		if ( null === $gateway ) {
922
			return '';
923
		}
924
925
		// Input HTML.
926
		$gateway->set_payment_method( $this->payment_method );
927
928
		$html = $gateway->get_input_html();
929
930
		if ( empty( $html ) ) {
931
			return '';
932
		}
933
934
		return $html;
935
	}
936
937
	/**
938
	 * Validate payment form.
939
	 *
940
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L648
941
	 * @param string[] $errors Array with errors.
942
	 * @return string[]
943
	 */
944
	public function validate_payment_form( $errors ) {
945
		return $errors;
946
	}
947
948
	/**
949
	 * Display options form.
950
	 *
951
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/views/admin/options/gateway.php#L41
952
	 * @return void
953
	 */
954
	public function display_options_form() {
955
		$mepr_options = MeprOptions::fetch();
956
957
		?>
958
		<table>
959
			<tr>
960
				<?php
961
962
				$name = sprintf(
963
					'%s[%s][%s]',
964
					$mepr_options->integrations_str,
965
					$this->id,
966
					'config_id'
967
				);
968
969
				?>
970
				<td>
971
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
972
				</td>
973
				<td>
974
					<select name="<?php echo esc_attr( $name ); ?>">
975
						<?php
976
977
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
978
							printf(
979
								'<option value="%s" %s>%s</option>',
980
								esc_attr( $value ),
981
								selected( $value, $this->settings->config_id, false ),
982
								esc_html( $label )
983
							);
984
						}
985
986
						?>
987
					</select>
988
				</td>
989
			</tr>
990
		</table>
991
		<?php
992
	}
993
994
	/**
995
	 * Validate options form.
996
	 *
997
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L468
998
	 * @ilnk https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2006-L2026
999
	 * @param string[] $errors Array with errors.
1000
	 * @return string[]
1001
	 */
1002
	public function validate_options_form( $errors ) {
1003
		return $errors;
1004
	}
1005
1006
	/**
1007
	 * Enqueue user account scripts.
1008
	 *
1009
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L126
1010
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2028-L2044
1011
	 * @return void
1012
	 */
1013
	public function enqueue_user_account_scripts() {
1014
1015
	}
1016
1017
	/**
1018
	 * Display update account form.
1019
	 *
1020
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L423
1021
	 * @param int      $sub_id  Subscription ID.
1022
	 * @param string[] $errors  Array with errors.
1023
	 * @param string   $message Update message.
1024
	 * @return void
1025
	 */
1026
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
1027
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress', (int) $sub_id );
1028
1029
		$message = \__( 'The payment method for this subscription can not be updated manually.', 'pronamic_ideal' );
1030
1031
		if ( \is_array( $subscriptions ) ) {
1032
			$subscription = \array_shift( $subscriptions );
1033
1034
			$message = \sprintf(
1035
				/* translators: %s: mandate selection URL anchor */
1036
				\__( 'To update the payment method for this subscription, please visit the %s page.', 'pronamic_ideal' ),
1037
				\sprintf(
1038
					'<a href="%1$s" title="%2$s">%3$s</a>',
1039
					\esc_url( $subscription->get_mandate_selection_url() ),
1040
					\esc_attr( \__( 'payment method update', 'pronamic_ideal' ) ),
1041
					\esc_html( \__( 'payment method update', 'pronamic_ideal' ) )
1042
				)
1043
			);
1044
		}
1045
1046
		?>
1047
1048
		<h3>
1049
			<?php echo \esc_html( __( 'Update payment method', 'pronamic_ideal' ) ); ?>
1050
		</h3>
1051
1052
		<div>
1053
			<?php echo \wp_kses_post( $message ); ?>
1054
		</div>
1055
1056
		<?php
1057
	}
1058
1059
	/**
1060
	 * Validate update account form.
1061
	 *
1062
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L1182-L1197
1063
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2100-L2103
1064
	 * @param string[] $errors Array with errors.
1065
	 * @return string[]
1066
	 */
1067
	public function validate_update_account_form( $errors = array() ) {
1068
		return $errors;
1069
	}
1070
1071
	/**
1072
	 * Process update account form.
1073
	 *
1074
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L430
1075
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2105-L2111
1076
	 * @param int $sub_id Subscription ID.
1077
	 * @return void
1078
	 */
1079
	public function process_update_account_form( $sub_id ) {
1080
1081
	}
1082
1083
	/**
1084
	 * Is test mode.
1085
	 *
1086
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
1087
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2113-L2121
1088
	 * @return boolean
1089
	 */
1090
	public function is_test_mode() {
1091
		return false;
1092
	}
1093
1094
	/**
1095
	 * Force SSL.
1096
	 *
1097
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
1098
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2123-L2125
1099
	 * @return boolean
1100
	 */
1101
	public function force_ssl() {
1102
		return false;
1103
	}
1104
1105
	/**
1106
	 * Get config ID.
1107
	 *
1108
	 * @return string|int|null
1109
	 */
1110
	protected function get_config_id() {
1111
		// Get config ID setting.
1112
		$config_id = $this->settings->config_id;
1113
1114
		// Check empty config ID.
1115
		if ( empty( $config_id ) ) {
1116
			$config_id = \get_option( 'pronamic_pay_config_id' );
1117
		}
1118
1119
		// Check empty config ID.
1120
		if ( empty( $config_id ) ) {
1121
			$config_id = null;
1122
		}
1123
1124
		return $config_id;
1125
	}
1126
}
1127