Test Setup Failed
Push — develop ( 5e850b...eb5e5d )
by Reüel
08:21
created

Gateway::spc_payment_fields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 20
ccs 0
cts 5
cp 0
crap 12
rs 9.9666
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.2.3
36
 * @since   1.0.0
37
 */
38
class Gateway extends MeprBaseRealGateway {
39
	/**
40
	 * Payment method.
41
	 *
42
	 * @var string
43
	 */
44
	protected $payment_method;
45
46
	/**
47
	 * MemberPress transaction.
48
	 *
49
	 * @var MeprTransaction
50
	 */
51
	public $mp_txn;
52
53
	/**
54
	 * Pronamic payment.
55
	 *
56
	 * @var Payment
57
	 */
58
	public $pronamic_payment;
59
60
	/**
61
	 * Constructs and initialize iDEAL gateway.
62
	 */
63
	public function __construct() {
64
		// Set the name of this gateway.
65
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L12-13.
66
		$this->name = __( 'Pronamic', 'pronamic_ideal' );
67
68
		if ( ! empty( $this->payment_method ) ) {
69
			$this->name = sprintf(
70
				/* translators: %s: payment method name */
71
				__( 'Pronamic - %s', 'pronamic_ideal' ),
72
				PaymentMethods::get_name( $this->payment_method )
73
			);
74
		}
75
76
		// Set the default settings.
77
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73.
78
		$this->set_defaults();
79
80
		// Set the capabilities of this gateway.
81
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L36-37.
82
		$this->capabilities = array();
83
84
		// Setup the notification actions for this gateway.
85
		$this->notifiers = array();
86
87
		// Support single-page checkout.
88
		$this->has_spc_form = true;
0 ignored issues
show
Bug Best Practice introduced by
The property has_spc_form does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
	}
90
91
	/**
92
	 * Load the specified settings.
93
	 *
94
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L69-70
95
	 *
96
	 * @param array $settings MemberPress gateway settings array.
97
	 */
98
	public function load( $settings ) {
99
		$this->settings = (object) $settings;
100
101
		$this->set_defaults();
102
	}
103
104
	/**
105
	 * Custom helper function to send transaction notices.
106
	 *
107
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprUtils.php#L1333-L1351
108
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
109
	 *
110
	 * @param MeprTransaction $transaction MemberPress transaction object.
111
	 * @param string          $method      PHP function name to call.
112
	 *
113
	 * @return mixed
114
	 */
115
	public function send_transaction_notices( $transaction, $method ) {
116
		$class = 'MeprUtils';
117
118
		if ( ! Core_Util::class_method_exists( $class, $method ) ) {
119
			$class = $this;
120
		}
121
122
		// `send_product_welcome_notices` is called from `send_signup_notices` in newer versions.
123
		if ( 'send_product_welcome_notices' === $method ) {
124
			if ( 'MeprUtils' === $class ) {
125
				return null;
126
			}
127
128
			if ( ! \method_exists( $class, 'send_product_welcome_notices' ) ) {
129
				return null;
130
			}
131
		}
132
133
		return call_user_func( array( $class, $method ), $transaction );
134
	}
135
136
	/**
137
	 * Get icon function (this is not a MemberPress function).
138
	 *
139
	 * @since 1.0.2
140
	 * @return string
141
	 */
142
	protected function get_icon() {
143
		return '';
144
	}
145
146
	/**
147
	 * Get class alias name.
148
	 *
149
	 * @return string
150
	 */
151
	public function get_alias() {
152
		return 'MeprPronamicGateway';
153
	}
154
155
	/**
156
	 * Set the default settings.
157
	 *
158
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73
159
	 */
160
	protected function set_defaults() {
161
		if ( ! isset( $this->settings ) ) {
162
			$this->settings = array();
163
		}
164
165
		$this->settings = (object) array_merge(
166
			array(
167
				'gateway'   => $this->get_alias(),
168
				'id'        => $this->generate_id(),
169
				'label'     => '',
170
				'use_label' => true,
171
				'icon'      => $this->get_icon(),
172
				'use_icon'  => true,
173
				'desc'      => '',
174
				'use_desc'  => true,
175
				'config_id' => '',
176
				'email'     => '',
177
				'sandbox'   => false,
178
				'debug'     => false,
179
			),
180
			(array) $this->settings
181
		);
182
183
		$this->id        = $this->settings->id;
184
		$this->label     = $this->settings->label;
185
		$this->use_label = $this->settings->use_label;
186
		$this->icon      = $this->settings->icon;
187
		$this->use_icon  = $this->settings->use_icon;
188
		$this->desc      = $this->settings->desc;
189
		$this->use_desc  = $this->settings->use_desc;
190
	}
191
192
	/**
193
	 * Process payment.
194
	 *
195
	 * @param MeprTransaction $txn MemberPress transaction object.
196
	 *
197
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L119-122
198
	 */
199
	public function process_payment( $txn ) {
200
201
	}
202
203
	/**
204
	 * Record subscription payment.
205
	 *
206
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L140-145
207
	 */
208
	public function record_subscription_payment() {
209
		$transaction = $this->mp_txn;
210
211
		$transaction->status     = MeprTransaction::$complete_str;
212
		$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...
213
		$transaction->store();
214
215
		$subscription = $transaction->subscription();
216
217
		if ( $subscription ) {
218
			$should_activate = ! \in_array(
219
				$subscription->status,
220
				array(
221
					MeprSubscription::$active_str,
222
					MeprSubscription::$cancelled_str,
223
				),
224
				true
225
			);
226
227
			if ( $should_activate ) {
228
				$subscription->status = MeprSubscription::$active_str;
229
				$subscription->store();
230
			}
231
232
			$subscription->expire_confirmation_txn();
233
234
			$subscription->limit_payment_cycles();
235
		}
236
237
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
238
239
		return $transaction;
240
	}
241
242
	/**
243
	 * Record payment failure.
244
	 *
245
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L147-148
246
	 */
247
	public function record_payment_failure() {
248
		$transaction = $this->mp_txn;
249
250
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
251
		$transaction->status = MeprTransaction::$failed_str;
252
		$transaction->store();
253
254
		// Expire associated subscription transactions for non-recurring payments.
255
		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...
256
			$subscription = $transaction->subscription();
257
258
			if ( $subscription ) {
259
				$subscription->expire_txns();
260
				$subscription->store();
261
			}
262
		}
263
264
		$this->send_transaction_notices( $transaction, 'send_failed_txn_notices' );
265
266
		return $transaction;
267
	}
268
269
	/**
270
	 * Record payment.
271
	 *
272
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L124-129
273
	 */
274
	public function record_payment() {
275
		$transaction = $this->mp_txn;
276
277
		$transaction->status = MeprTransaction::$complete_str;
278
279
		// This will only work before maybe_cancel_old_sub is run.
280
		$upgrade   = $transaction->is_upgrade();
281
		$downgrade = $transaction->is_downgrade();
282
283
		$event_transaction = $transaction->maybe_cancel_old_sub();
0 ignored issues
show
Bug introduced by
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...
284
285
		$subscription = $transaction->subscription();
286
287
		if ( $subscription ) {
288
			$event_subscription = $subscription->maybe_cancel_old_sub();
0 ignored issues
show
Bug introduced by
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...
289
290
			$subscription->status     = MeprSubscription::$active_str;
291
			$subscription->created_at = $transaction->created_at;
292
			$subscription->store();
293
294
			if ( false === $event_transaction && false !== $event_subscription ) {
0 ignored issues
show
introduced by
The condition false !== $event_subscription is always true.
Loading history...
introduced by
The condition false === $event_transaction is always false.
Loading history...
295
				$event_transaction = $event_subscription;
296
			}
297
		}
298
299
		$transaction->store();
300
301
		/*
302
		 * For some reasons the `send_product_welcome_notices` function accepts 1 or 3 arguments. We are not sure
303
		 * if this is a difference in the 'Business' and 'Developer' edition or between version `1.2.4` and `1.2.7`.
304
		 *
305
		 * @link https://github.com/wp-premium/memberpress-developer/blob/1.2.4/app/lib/MeprBaseGateway.php#L596-L612
306
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/lib/MeprBaseGateway.php#L609-L619
307
		 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L51
308
		 */
309
		$reflection = new ReflectionClass( 'MeprBaseRealGateway' );
310
311
		if ( $reflection->hasMethod( 'send_product_welcome_notices' ) && 3 === $reflection->getMethod( 'send_product_welcome_notices' )->getNumberOfParameters() ) {
312
			$uemail = MeprEmailFactory::fetch(
313
				'MeprUserProductWelcomeEmail',
314
				'MeprBaseProductEmail',
315
				array(
316
					array(
317
						'product_id' => $transaction->product_id,
318
					),
319
				)
320
			);
321
322
			/**
323
			 * The `send_product_welcome_notices` method is only available in earlier version of MemberPress.
324
			 *
325
			 * @scrutinizer ignore-call
326
			 */
327
			$this->send_product_welcome_notices(
328
				$uemail,
329
				MeprTransactionsHelper::get_email_params( $transaction ),
330
				$transaction->user()
331
			);
332
		} else {
333
			$this->send_transaction_notices( $transaction, 'send_product_welcome_notices' );
334
		}
335
336
		// Send upgrade/downgrade notices.
337
		$product = $transaction->product();
338
339
		if ( 'lifetime' === $product->period_type ) {
340
			if ( $upgrade ) {
341
				$this->upgraded_sub( $transaction, $event_transaction );
0 ignored issues
show
Unused Code introduced by
The call to MeprBaseGateway::upgraded_sub() has too many arguments starting with $event_transaction. ( Ignorable by Annotation )

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

341
				$this->/** @scrutinizer ignore-call */ 
342
           upgraded_sub( $transaction, $event_transaction );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
342
				$this->send_transaction_notices( $transaction, 'send_upgraded_txn_notices' );
343
			} elseif ( $downgrade ) {
344
				$this->downgraded_sub( $transaction, $event_transaction );
0 ignored issues
show
Unused Code introduced by
The call to MeprBaseGateway::downgraded_sub() has too many arguments starting with $event_transaction. ( Ignorable by Annotation )

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

344
				$this->/** @scrutinizer ignore-call */ 
345
           downgraded_sub( $transaction, $event_transaction );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
345
				$this->send_transaction_notices( $transaction, 'send_downgraded_txn_notices' );
346
			} else {
347
				$this->new_sub( $transaction );
348
			}
349
		}
350
351
		$this->send_transaction_notices( $transaction, 'send_signup_notices' );
352
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
353
354
		return $transaction;
355
	}
356
357
	/**
358
	 * Process refund.
359
	 *
360
	 * @param MeprTransaction $txn MemberPress transaction object.
361
	 *
362
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L131-133
363
	 */
364
	public function process_refund( MeprTransaction $txn ) {
365
366
	}
367
368
	/**
369
	 * Record refund.
370
	 *
371
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L135-138
372
	 */
373
	public function record_refund() {
374
375
	}
376
377
	/**
378
	 * Process trial payment.
379
	 *
380
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L150-157
381
	 *
382
	 * @param MeprTransaction $transaction MemberPress transaction object.
383
	 */
384
	public function process_trial_payment( $transaction ) {
385
386
	}
387
388
	/**
389
	 * Record trial payment.
390
	 *
391
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L159-161
392
	 *
393
	 * @param MeprTransaction $transaction MemberPress transaction object.
394
	 */
395
	public function record_trial_payment( $transaction ) {
396
397
	}
398
399
	/**
400
	 * Process create subscription.
401
	 *
402
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L163-167
403
	 *
404
	 * @param MeprTransaction $txn MemberPress transaction object.
405
	 */
406
	public function process_create_subscription( $txn ) {
407
408
	}
409
410
	/**
411
	 * Record create subscription.
412
	 *
413
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L169-174
414
	 */
415
	public function record_create_subscription() {
416
417
	}
418
419
	/**
420
	 * Process update subscription.
421
	 *
422
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L176
423
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L194
424
	 *
425
	 * @param int $sub_id Subscription ID.
426
	 */
427
	public function process_update_subscription( $sub_id ) {
428
429
	}
430
431
	/**
432
	 * Record update subscription.
433
	 *
434
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L178-182
435
	 */
436
	public function record_update_subscription() {
437
438
	}
439
440
	/**
441
	 * Process suspend subscription.
442
	 *
443
	 * @param int $sub_id Subscription id.
444
	 *
445
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L184-186
446
	 */
447
	public function process_suspend_subscription( $sub_id ) {
448
		if ( ! MeprSubscription::exists( $sub_id ) ) {
449
			return;
450
		}
451
452
		$sub = new MeprSubscription( $sub_id );
453
454
		if ( MeprSubscription::$suspended_str === $sub->status ) {
455
			// Subscription is already suspended.
456
			return;
457
		}
458
459
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
460
461
		if ( ! $subscription ) {
462
			return;
463
		}
464
465
		$sub->status = MeprSubscription::$suspended_str;
466
467
		$sub->store();
468
469
		// Send suspended subscription notices.
470
		MeprUtils::send_suspended_sub_notices( $sub );
471
472
		$note = sprintf(
473
			/* translators: %s: extension name */
474
			__( '%s subscription on hold.', 'pronamic_ideal' ),
475
			__( 'MemberPress', 'pronamic_ideal' )
476
		);
477
478
		$subscription->add_note( $note );
479
480
		// The status of canceled or completed subscriptions will not be changed automatically.
481
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
482
			$subscription->set_status( SubscriptionStatus::ON_HOLD );
483
484
			$subscription->save();
485
		}
486
	}
487
488
	/**
489
	 * Record suspend subscription.
490
	 *
491
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L188-191
492
	 */
493
	public function record_suspend_subscription() {
494
495
	}
496
497
	/**
498
	 * Process resume subscription.
499
	 *
500
	 * @param int $sub_id Subscription id.
501
	 *
502
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L193-195
503
	 */
504
	public function process_resume_subscription( $sub_id ) {
505
		if ( ! MeprSubscription::exists( $sub_id ) ) {
506
			return;
507
		}
508
509
		$sub = new MeprSubscription( $sub_id );
510
511
		if ( MeprSubscription::$active_str === $sub->status ) {
512
			// Subscription is already active.
513
			return;
514
		}
515
516
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
517
518
		if ( ! $subscription ) {
519
			return;
520
		}
521
522
		$sub->status = MeprSubscription::$active_str;
523
524
		$sub->store();
525
526
		// Check if prior txn is expired yet or not, if so create a temporary txn so the user can access the content immediately.
527
		$prior_txn = $sub->latest_txn();
528
529
		if ( false === $prior_txn || ! ( $prior_txn instanceof MeprTransaction ) || strtotime( $prior_txn->expires_at ) < time() ) {
530
			$txn                  = new MeprTransaction();
531
			$txn->subscription_id = $sub->id;
532
			$txn->trans_num       = $sub->subscr_id . '-' . uniqid();
533
			$txn->status          = MeprTransaction::$confirmed_str;
534
			$txn->txn_type        = MeprTransaction::$subscription_confirmation_str;
535
			$txn->response        = (string) $sub;
536
			$txn->expires_at      = MeprUtils::ts_to_mysql_date( time() + MeprUtils::days( 1 ), 'Y-m-d 23:59:59' );
537
538
			$txn->set_subtotal( 0.00 ); // Just a confirmation txn.
539
540
			$txn->store();
541
		}
542
543
		// Send resumed subscription notices.
544
		MeprUtils::send_resumed_sub_notices( $sub );
545
546
		// Add note.
547
		$note = sprintf(
548
			/* translators: %s: extension name */
549
			__( '%s subscription reactivated.', 'pronamic_ideal' ),
550
			__( 'MemberPress', 'pronamic_ideal' )
551
		);
552
553
		$subscription->add_note( $note );
554
555
		// The status of canceled or completed subscriptions will not be changed automatically.
556
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
557
			$subscription->set_status( SubscriptionStatus::ACTIVE );
558
559
			$subscription->save();
560
		}
561
	}
562
563
	/**
564
	 * Record resume subscription.
565
	 *
566
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L197-201
567
	 */
568
	public function record_resume_subscription() {
569
570
	}
571
572
	/**
573
	 * Process cancel subscription.
574
	 *
575
	 * @param int $sub_id Subscription id.
576
	 *
577
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L202-206
578
	 */
579
	public function process_cancel_subscription( $sub_id ) {
580
		if ( ! MeprSubscription::exists( $sub_id ) ) {
581
			return;
582
		}
583
584
		$sub = new MeprSubscription( $sub_id );
585
586
		if ( MeprSubscription::$cancelled_str === $sub->status ) {
587
			// Subscription is already cancelled.
588
			return;
589
		}
590
591
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
592
593
		if ( ! $subscription ) {
594
			return;
595
		}
596
597
		// Add note.
598
		$note = sprintf(
599
			/* translators: %s: extension name */
600
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
601
			__( 'MemberPress', 'pronamic_ideal' )
602
		);
603
604
		$subscription->add_note( $note );
605
606
		// The status of canceled or completed subscriptions will not be changed automatically.
607
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
608
			$subscription->set_status( SubscriptionStatus::CANCELLED );
609
610
			$subscription->next_payment_date          = null;
611
			$subscription->next_payment_delivery_date = null;
612
613
			// Delete next payment post meta.
614
			$subscription->set_meta( 'next_payment', null );
615
			$subscription->set_meta( 'next_payment_delivery_date', null );
616
617
			$subscription->save();
618
		}
619
620
		// Cancel MemberPress subscription.
621
		$sub->status = MeprSubscription::$cancelled_str;
622
623
		$sub->store();
624
625
		// Expire the grace period (confirmation) if no completed payments have come through.
626
		if ( (int) $sub->txn_count <= 0 ) {
627
			$sub->expire_txns();
628
		}
629
630
		$sub->limit_reached_actions();
631
632
		// Send cancelled subscription notices.
633
		MeprUtils::send_cancelled_sub_notices( $sub );
634
	}
635
636
	/**
637
	 * Record cancel subscription.
638
	 *
639
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L208-212
640
	 */
641
	public function record_cancel_subscription() {
642
643
	}
644
645
	/**
646
	 * Process signup form.
647
	 *
648
	 * Gets called when the signup form is posted used for running any payment
649
	 * method specific actions when processing the customer signup form.
650
	 *
651
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L214-217
652
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L262
653
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L232-L235
654
	 *
655
	 * @param MeprTransaction $txn MemberPress transaction object.
656
	 */
657
	public function process_signup_form( $txn ) {
658
659
	}
660
661
	/**
662
	 * Payment redirect.
663
	 *
664
	 * @param MeprTransaction $txn MemberPress transaction object.
665
	 *
666
	 * @throws \Exception Throws exception on gateway payment start error.
667
	 * @since 1.0.2
668
	 */
669
	public function payment_redirect( $txn ) {
670
		$txn = new MeprTransaction( $txn->id );
671
672
		// Gateway.
673
		$config_id = $this->settings->config_id;
674
675
		$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...
676
677
		if ( ! $gateway ) {
0 ignored issues
show
introduced by
$gateway is of type null, thus it always evaluated to false.
Loading history...
678
			return;
679
		}
680
681
		// Create Pronamic payment.
682
		$payment = Pronamic::get_payment( $txn );
683
684
		$payment->config_id = $this->settings->config_id;
685
		$payment->method    = $this->payment_method;
686
687
		$error = null;
688
689
		try {
690
			$payment = Plugin::start_payment( $payment );
691
		} catch ( \Exception $e ) {
692
			$error = $e;
693
		}
694
695
		/*
696
		 * Update trial transaction.
697
		 *
698
		 * Notes:
699
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
700
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
701
		 * - Reload transaction to make sure actual status is being used (i.e. on free downgrade).
702
		 */
703
		$txn = new MeprTransaction( $txn->id );
704
705
		$subscription = $txn->subscription();
706
707
		if ( $subscription && $subscription->in_trial() ) {
708
			$txn->expires_at = MeprUtils::ts_to_mysql_date( $payment->get_end_date()->getTimestamp(), 'Y-m-d 23:59:59' );
709
710
			$txn->set_subtotal( $subscription->trial_amount );
711
			$txn->store();
712
		}
713
714
		if ( $error instanceof \Exception ) {
715
			// Rethrow error, caught by MemberPress.
716
			throw $error;
717
		}
718
719
		// Redirect.
720
		$gateway->redirect( $payment );
721
	}
722
723
	/**
724
	 * Display payment page.
725
	 *
726
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
727
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L290
728
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L775-L850
729
	 *
730
	 * @param MeprTransaction $txn MemberPress transaction object.
731
	 *
732
	 * @throws \Exception Throws exception on gateway payment start error.
733
	 */
734
	public function display_payment_page( $txn ) {
735
		// Gateway.
736
		$config_id = $this->settings->config_id;
737
738
		$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...
739
740
		// Check gateway.
741
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
742
			return;
743
		}
744
745
		$gateway->set_payment_method( $this->payment_method );
746
747
		$html = $gateway->get_input_html();
748
749
		if ( empty( $html ) ) {
750
			$this->payment_redirect( $txn );
751
		}
752
	}
753
754
	/**
755
	 * Process payment form.
756
	 *
757
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
758
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
759
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
760
	 *
761
	 * @param MeprTransaction $txn MemberPress transaction object.
762
	 *
763
	 * @return void
764
	 * @throws \Exception Throws exception on gateway payment start error.
765
	 */
766
	public function process_payment_form( $txn ) {
767
		// Gateway.
768
		$config_id = $this->settings->config_id;
769
770
		$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...
771
772
		if ( $gateway ) {
0 ignored issues
show
introduced by
$gateway is of type null, thus it always evaluated to false.
Loading history...
773
			$this->payment_redirect( $txn );
774
		}
775
	}
776
777
	/**
778
	 * Enqueue payment form scripts.
779
	 *
780
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
781
	 */
782
	public function enqueue_payment_form_scripts() {
783
784
	}
785
786
	/**
787
	 * Display payment form.
788
	 *
789
	 * This spits out html for the payment form on the registration / payment
790
	 * page for the user to fill out for payment.
791
	 *
792
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L230-233
793
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L248-L251
794
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L318
795
	 *
796
	 * @param float    $amount     Transaction amount to create a payment form for.
797
	 * @param MeprUser $user       MemberPress user object.
798
	 * @param int      $product_id Product ID.
799
	 * @param int      $txn_id     Transaction ID.
800
	 */
801
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
802
		$product = new MeprProduct( $product_id );
803
804
		$coupon = false;
805
806
		$txn = new MeprTransaction( $txn_id );
807
808
		// Artificially set the price of the $prd in case a coupon was used.
809
		if ( $product->price !== $amount ) {
810
			$coupon         = true;
811
			$product->price = $amount;
812
		}
813
814
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
815
816
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
817
		echo $invoice;
818
819
		?>
820
		<div class="mp_wrapper mp_payment_form_wrapper">
821
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
822
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
823
				<input type="hidden" name="mepr_transaction_id" value="<?php echo esc_attr( $txn_id ); ?>"/>
824
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
825
826
				<div class="mepr_spacer">&nbsp;</div>
827
828
				<?php
829
830
				// Gateway.
831
				$config_id = $this->settings->config_id;
832
833
				$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...
834
835
				if ( null !== $gateway ) {
0 ignored issues
show
introduced by
The condition null !== $gateway is always false.
Loading history...
836
					$gateway->set_payment_method( $this->payment_method );
837
838
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
839
					echo $gateway->get_input_html();
840
				}
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_attr( 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->settings->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
		// Check gateway.
872
		if ( null === $gateway ) {
0 ignored issues
show
introduced by
The condition null === $gateway is always true.
Loading history...
873
			return '';
874
		}
875
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://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L235-236
891
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L330
892
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L253-L254
893
	 *
894
	 * @param array $errors Array with errors.
895
	 * @return array
896
	 */
897
	public function validate_payment_form( $errors ) {
898
		return $errors;
899
	}
900
901
	/**
902
	 * Display options form.
903
	 *
904
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L291-292
905
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/gateways/MeprAuthorizeGateway.php#L1027-1037
906
	 */
907
	public function display_options_form() {
908
		$mepr_options = MeprOptions::fetch();
909
910
		?>
911
		<table>
912
			<tr>
913
				<?php
914
915
				$name = sprintf(
916
					'%s[%s][%s]',
917
					$mepr_options->integrations_str,
918
					$this->id,
919
					'config_id'
920
				);
921
922
				?>
923
				<td>
924
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
925
				</td>
926
				<td>
927
					<select name="<?php echo esc_attr( $name ); ?>">
928
						<?php
929
930
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
931
							printf(
932
								'<option value="%s" %s>%s</option>',
933
								esc_attr( $value ),
934
								selected( $value, $this->settings->config_id, false ),
935
								esc_html( $label )
936
							);
937
						}
938
939
						?>
940
					</select>
941
				</td>
942
			</tr>
943
		</table>
944
		<?php
945
	}
946
947
	/**
948
	 * Validate options form.
949
	 *
950
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L294-295
951
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L909-L924
952
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L416-L423
953
	 *
954
	 * @param array $errors Array with errors.
955
	 * @return array
956
	 */
957
	public function validate_options_form( $errors ) {
958
		return $errors;
959
	}
960
961
	/**
962
	 * Enqueue user account scripts.
963
	 *
964
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L297-302
965
	 */
966
	public function enqueue_user_account_scripts() {
967
968
	}
969
970
	/**
971
	 * Display update account form.
972
	 *
973
	 * @param int    $sub_id  Subscription ID.
974
	 * @param array  $errors  Array with errors.
975
	 * @param string $message Update message.
976
	 *
977
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L365-366
978
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseStaticGateway.php#L160-L161
979
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1108-L1168
980
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprAccountCtrl.php#L388
981
	 */
982
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
983
984
	}
985
986
	/**
987
	 * Validate update account form.
988
	 *
989
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L368-369
990
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1170-L1173
991
	 *
992
	 * @param  array $errors Array with errors.
993
	 * @return array
994
	 */
995
	public function validate_update_account_form( $errors = array() ) {
996
		return $errors;
997
	}
998
999
	/**
1000
	 * Process update account form.
1001
	 *
1002
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L371-372
1003
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1175-L1181
1004
	 *
1005
	 * @param int $sub_id Subscription ID.
1006
	 */
1007
	public function process_update_account_form( $sub_id ) {
1008
1009
	}
1010
1011
	/**
1012
	 * Is test mode.
1013
	 *
1014
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
1015
	 *
1016
	 * @return boolean
1017
	 */
1018
	public function is_test_mode() {
1019
		return false;
1020
	}
1021
1022
	/**
1023
	 * Force SSL.
1024
	 *
1025
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
1026
	 *
1027
	 * @return boolean
1028
	 */
1029
	public function force_ssl() {
1030
		return false;
1031
	}
1032
}
1033