Failed Conditions
Push — develop ( 55b8d9...dcdad5 )
by Remco
06:21
created

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