Failed Conditions
Push — master ( 85457b...1516a3 )
by Reüel
15:24 queued 05:52
created

src/Gateways/Gateway.php (8 issues)

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

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

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

}

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

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

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

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

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

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

}

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

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

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

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

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

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

}

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

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

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

Loading history...
650
651
		if ( ! $gateway ) {
652
			return;
653
		}
654
655
		// Create Pronamic payment.
656
		$payment = Pronamic::get_payment( $txn );
657
658
		$payment->config_id = $this->settings->config_id;
659
		$payment->method    = $this->payment_method;
660
661
		$error = null;
662
663
		try {
664
			$payment = Plugin::start_payment( $payment );
665
		} catch ( \Exception $e ) {
666
			$error = $e;
667
		}
668
669
		/*
670
		 * Update transaction subtotal.
671
		 *
672
		 * Notes:
673
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
674
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
675
		 */
676
		$subscription = $txn->subscription();
677
678
		if ( $subscription && $subscription->in_trial() ) {
679
			$txn->set_subtotal( $subscription->trial_amount );
680
			$txn->store();
681
		}
682
683
		if ( $error instanceof \Exception ) {
684
			// Rethrow error, catched by MemberPress.
685
			throw $error;
686
		}
687
688
		// Redirect.
689
		$gateway->redirect( $payment );
690
	}
691
692
	/**
693
	 * Display payment page.
694
	 *
695
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
696
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L290
697
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L775-L850
698
	 *
699
	 * @param MeprTransaction $txn MemberPress transaction object.
700
	 *
701
	 * @throws \Exception Throws exception on gateway payment start error.
702
	 */
703
	public function display_payment_page( $txn ) {
704
		// Gateway.
705
		$config_id = $this->settings->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 ( $gateway && '' === $gateway->get_input_html() ) {
710
			$this->payment_redirect( $txn );
711
		}
712
	}
713
714
	/**
715
	 * Process payment form.
716
	 *
717
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
718
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
719
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
720
	 *
721
	 * @param MeprTransaction $txn MemberPress transaction object.
722
	 *
723
	 * @return bool
724
	 * @throws \Exception Throws exception on gateway payment start error.
725
	 */
726
	public function process_payment_form( $txn ) {
727
		if ( ! filter_has_var( INPUT_POST, 'pronamic_pay_memberpress_pay' ) ) {
728
			return false;
729
		}
730
731
		// Gateway.
732
		$config_id = $this->settings->config_id;
733
734
		$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...
735
736
		if ( $gateway ) {
737
			$this->payment_redirect( $txn );
738
		}
739
	}
740
741
	/**
742
	 * Enqueue payment form scripts.
743
	 *
744
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
745
	 */
746
	public function enqueue_payment_form_scripts() {
747
748
	}
749
750
	/**
751
	 * Display payment form.
752
	 *
753
	 * This spits out html for the payment form on the registration / payment
754
	 * page for the user to fill out for payment.
755
	 *
756
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L230-233
757
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L248-L251
758
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L318
759
	 *
760
	 * @param float    $amount     Transaction amount to create a payment form for.
761
	 * @param MeprUser $user       MemberPress user object.
762
	 * @param int      $product_id Product ID.
763
	 * @param int      $txn_id     Transaction ID.
764
	 */
765
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
766
		$product = new MeprProduct( $product_id );
767
768
		$coupon = false;
769
770
		$txn = new MeprTransaction( $txn_id );
771
772
		// Artifically set the price of the $prd in case a coupon was used.
773
		if ( $product->price !== $amount ) {
774
			$coupon         = true;
775
			$product->price = $amount;
776
		}
777
778
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
779
780
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
781
		echo $invoice;
782
783
		?>
784
		<div class="mp_wrapper mp_payment_form_wrapper">
785
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
786
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
787
				<input type="hidden" name="mepr_transaction_id" value="<?php echo esc_attr( $txn_id ); ?>"/>
788
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
789
790
				<div class="mepr_spacer">&nbsp;</div>
791
792
				<?php
793
794
				// Gateway.
795
				$config_id = $this->settings->config_id;
796
797
				$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...
798
799
				if ( $gateway ) {
800
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
801
					echo $gateway->get_input_html();
802
				}
803
804
				?>
805
806
				<div class="mepr_spacer">&nbsp;</div>
807
808
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
809
				<img src="<?php echo esc_attr( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
810
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
811
812
				<noscript>
813
					<p class="mepr_nojs">
814
						<?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' ); ?>
815
					</p>
816
				</noscript>
817
			</form>
818
		</div>
819
		<?php
820
	}
821
822
	/**
823
	 * Validate payment form.
824
	 *
825
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L235-236
826
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L330
827
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L253-L254
828
	 *
829
	 * @param array $errors Array with errors.
830
	 * @return array
831
	 */
832
	public function validate_payment_form( $errors ) {
833
		return $errors;
834
	}
835
836
	/**
837
	 * Display options form.
838
	 *
839
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L291-292
840
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/gateways/MeprAuthorizeGateway.php#L1027-1037
841
	 */
842
	public function display_options_form() {
843
		$mepr_options = MeprOptions::fetch();
844
845
		?>
846
		<table>
847
			<tr>
848
				<?php
849
850
				$name = sprintf(
851
					'%s[%s][%s]',
852
					$mepr_options->integrations_str,
853
					$this->id,
854
					'config_id'
855
				);
856
857
				?>
858
				<td>
859
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
860
				</td>
861
				<td>
862
					<select name="<?php echo esc_attr( $name ); ?>">
863
						<?php
864
865
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
866
							printf(
867
								'<option value="%s" %s>%s</option>',
868
								esc_attr( $value ),
869
								selected( $value, $this->settings->config_id, false ),
870
								esc_html( $label )
871
							);
872
						}
873
874
						?>
875
					</select>
876
				</td>
877
			</tr>
878
		</table>
879
		<?php
880
	}
881
882
	/**
883
	 * Validate options form.
884
	 *
885
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L294-295
886
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L909-L924
887
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L416-L423
888
	 *
889
	 * @param array $errors Array with errors.
890
	 * @return array
891
	 */
892
	public function validate_options_form( $errors ) {
893
		return $errors;
894
	}
895
896
	/**
897
	 * Enqueue user account scripts.
898
	 *
899
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L297-302
900
	 */
901
	public function enqueue_user_account_scripts() {
902
903
	}
904
905
	/**
906
	 * Display update account form.
907
	 *
908
	 * @param int    $sub_id  Subscription ID.
909
	 * @param array  $errors  Array with errors.
910
	 * @param string $message Update message.
911
	 *
912
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L365-366
913
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseStaticGateway.php#L160-L161
914
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1108-L1168
915
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprAccountCtrl.php#L388
916
	 */
917
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
918
919
	}
920
921
	/**
922
	 * Validate update account form.
923
	 *
924
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L368-369
925
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1170-L1173
926
	 *
927
	 * @param  array $errors Array with errors.
928
	 * @return array
929
	 */
930
	public function validate_update_account_form( $errors = array() ) {
931
		return $errors;
932
	}
933
934
	/**
935
	 * Process update account form.
936
	 *
937
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L371-372
938
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1175-L1181
939
	 *
940
	 * @param int $sub_id Subscription ID.
941
	 */
942
	public function process_update_account_form( $sub_id ) {
943
944
	}
945
946
	/**
947
	 * Is test mode.
948
	 *
949
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
950
	 *
951
	 * @return boolean
952
	 */
953
	public function is_test_mode() {
954
		return false;
955
	}
956
957
	/**
958
	 * Force SSL.
959
	 *
960
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
961
	 *
962
	 * @return boolean
963
	 */
964
	public function force_ssl() {
965
		return false;
966
	}
967
}
968