Failed Conditions
Push — develop ( dcdad5...3d8e5d )
by Remco
06:27
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
	 * @return 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
		$payment = Plugin::start_payment( $payment );
565
566
		$gateway->redirect( $payment );
567
	}
568
569
	/**
570
	 * Display payment page.
571
	 *
572
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L249-L253
573
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1766-L1768
574
	 * @param MeprTransaction $txn MemberPress transaction object.
575
	 * @return void
576
	 * @throws \Exception Throws exception on gateway payment start error.
577
	 */
578
	public function display_payment_page( $txn ) {
579
		// Gateway.
580
		$config_id = $this->get_config_id();
581
582
		$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...
583
584
		if ( null === $gateway ) {
585
			return;
586
		}
587
588
		// Redirect payment on empty input HTML.
589
		$gateway->set_payment_method( $this->payment_method );
590
591
		$html = $gateway->get_input_html();
592
593
		if ( empty( $html ) ) {
594
			$this->payment_redirect( $txn );
595
		}
596
	}
597
598
	/**
599
	 * Process payment form.
600
	 *
601
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
602
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
603
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
604
	 *
605
	 * @param MeprTransaction $txn MemberPress transaction object.
606
	 *
607
	 * @return void
608
	 * @throws \Exception Throws exception on gateway payment start error.
609
	 */
610
	public function process_payment_form( $txn ) {
611
		// Gateway.
612
		$config_id = $this->get_config_id();
613
614
		$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...
615
616
		if ( null === $gateway ) {
617
			return;
618
		}
619
620
		// Redirect.
621
		$this->payment_redirect( $txn );
622
	}
623
624
	/**
625
	 * Enqueue payment form scripts.
626
	 *
627
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L255-L258
628
	 * @return void
629
	 */
630
	public function enqueue_payment_form_scripts() {
631
632
	}
633
634
	/**
635
	 * Display payment form.
636
	 *
637
	 * This spits out html for the payment form on the registration / payment
638
	 * page for the user to fill out for payment.
639
	 *
640
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L571
641
	 * @param float    $amount     Transaction amount to create a payment form for.
642
	 * @param MeprUser $user       MemberPress user object.
643
	 * @param int      $product_id Product ID.
644
	 * @param int      $txn_id     Transaction ID.
645
	 * @return void
646
	 */
647
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
648
		// Gateway.
649
		$config_id = $this->get_config_id();
650
651
		$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...
652
653
		if ( null === $gateway ) {
654
655
			$admin_message = null;
656
657
			if ( \current_user_can( 'manage_options' ) ) {
658
				$admin_message = __( 'For admins only: check payment method settings in MemberPress.', 'pronamic_ideal' );
659
			}
660
661
			printf(
662
				'<div class="mp_wrapper mp_payment_form_wrapper"><ul><li>%s</li>%s</ul></div>',
663
				\esc_html( Plugin::get_default_error_message() ),
664
				null === $admin_message ? '' : sprintf( '<li><em>%s</em></li>', \esc_html( $admin_message ) )
665
			);
666
667
			return;
668
		}
669
670
		// Invoice.
671
		$product = new MeprProduct( $product_id );
672
673
		$coupon = false;
674
675
		$txn = new MeprTransaction( $txn_id );
676
677
		// Artificially set the price of the $prd in case a coupon was used.
678
		if ( $product->price !== $amount ) {
679
			$coupon         = true;
680
			$product->price = $amount;
681
		}
682
683
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
684
685
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
686
		echo $invoice;
687
688
		?>
689
		<div class="mp_wrapper mp_payment_form_wrapper">
690
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
691
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
692
				<input type="hidden" name="mepr_transaction_id" value="<?php echo \esc_attr( (string) $txn_id ); ?>"/>
693
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
694
695
				<div class="mepr_spacer">&nbsp;</div>
696
697
				<?php
698
699
				$gateway->set_payment_method( $this->payment_method );
700
701
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
702
				echo $gateway->get_input_html();
703
704
				?>
705
706
				<div class="mepr_spacer">&nbsp;</div>
707
708
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
709
				<img src="<?php echo \esc_url( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
710
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
711
712
				<noscript>
713
					<p class="mepr_nojs">
714
						<?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' ); ?>
715
					</p>
716
				</noscript>
717
			</form>
718
		</div>
719
		<?php
720
	}
721
722
	/**
723
	 * Single-page checkout payment fields.
724
	 *
725
	 * @return string
726
	 */
727
	public function spc_payment_fields() {
728
		// Gateway.
729
		$config_id = $this->get_config_id();
730
731
		$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...
732
733
		if ( null === $gateway ) {
734
			return '';
735
		}
736
737
		// Input HTML.
738
		$gateway->set_payment_method( $this->payment_method );
739
740
		$html = $gateway->get_input_html();
741
742
		if ( empty( $html ) ) {
743
			return '';
744
		}
745
746
		return $html;
747
	}
748
749
	/**
750
	 * Validate payment form.
751
	 *
752
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L648
753
	 * @param string[] $errors Array with errors.
754
	 * @return string[]
755
	 */
756
	public function validate_payment_form( $errors ) {
757
		return $errors;
758
	}
759
760
	/**
761
	 * Display options form.
762
	 *
763
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/views/admin/options/gateway.php#L41
764
	 * @return void
765
	 */
766
	public function display_options_form() {
767
		$mepr_options = MeprOptions::fetch();
768
769
		?>
770
		<table>
771
			<tr>
772
				<?php
773
774
				$name = sprintf(
775
					'%s[%s][%s]',
776
					$mepr_options->integrations_str,
777
					$this->id,
778
					'config_id'
779
				);
780
781
				?>
782
				<td>
783
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
784
				</td>
785
				<td>
786
					<select name="<?php echo esc_attr( $name ); ?>">
787
						<?php
788
789
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
790
							printf(
791
								'<option value="%s" %s>%s</option>',
792
								esc_attr( $value ),
793
								selected( $value, $this->settings->config_id, false ),
794
								esc_html( $label )
795
							);
796
						}
797
798
						?>
799
					</select>
800
				</td>
801
			</tr>
802
		</table>
803
		<?php
804
	}
805
806
	/**
807
	 * Validate options form.
808
	 *
809
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L468
810
	 * @ilnk https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2006-L2026
811
	 * @param string[] $errors Array with errors.
812
	 * @return string[]
813
	 */
814
	public function validate_options_form( $errors ) {
815
		return $errors;
816
	}
817
818
	/**
819
	 * Enqueue user account scripts.
820
	 *
821
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L126
822
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2028-L2044
823
	 * @return void
824
	 */
825
	public function enqueue_user_account_scripts() {
826
827
	}
828
829
	/**
830
	 * Display update account form.
831
	 *
832
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L423
833
	 * @param int      $sub_id  Subscription ID.
834
	 * @param string[] $errors  Array with errors.
835
	 * @param string   $message Update message.
836
	 * @return void
837
	 */
838
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
839
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', (int) $sub_id );
840
841
		$message = \__( 'The payment method for this subscription can not be updated manually.', 'pronamic_ideal' );
842
843
		if ( \is_array( $subscriptions ) ) {
844
			$subscription = \array_shift( $subscriptions );
845
846
			$message = \sprintf(
847
				/* translators: %s: mandate selection URL anchor */
848
				\__( 'To update the payment method for this subscription, please visit the %s page.', 'pronamic_ideal' ),
849
				\sprintf(
850
					'<a href="%1$s" title="%2$s">%3$s</a>',
851
					\esc_url( $subscription->get_mandate_selection_url() ),
852
					\esc_attr( \__( 'payment method update', 'pronamic_ideal' ) ),
853
					\esc_html( \__( 'payment method update', 'pronamic_ideal' ) )
854
				)
855
			);
856
		}
857
858
		?>
859
860
		<h3>
861
			<?php echo \esc_html( __( 'Update payment method', 'pronamic_ideal' ) ); ?>
862
		</h3>
863
864
		<div>
865
			<?php echo \wp_kses_post( $message ); ?>
866
		</div>
867
868
		<?php
869
	}
870
871
	/**
872
	 * Validate update account form.
873
	 *
874
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L1182-L1197
875
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2100-L2103
876
	 * @param string[] $errors Array with errors.
877
	 * @return string[]
878
	 */
879
	public function validate_update_account_form( $errors = array() ) {
880
		return $errors;
881
	}
882
883
	/**
884
	 * Process update account form.
885
	 *
886
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L430
887
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2105-L2111
888
	 * @param int $sub_id Subscription ID.
889
	 * @return void
890
	 */
891
	public function process_update_account_form( $sub_id ) {
892
893
	}
894
895
	/**
896
	 * Is test mode.
897
	 *
898
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
899
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2113-L2121
900
	 * @return boolean
901
	 */
902
	public function is_test_mode() {
903
		return false;
904
	}
905
906
	/**
907
	 * Force SSL.
908
	 *
909
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
910
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2123-L2125
911
	 * @return boolean
912
	 */
913
	public function force_ssl() {
914
		return false;
915
	}
916
917
	/**
918
	 * Get config ID.
919
	 *
920
	 * @return string|int|null
921
	 */
922
	protected function get_config_id() {
923
		// Get config ID setting.
924
		$config_id = $this->settings->config_id;
925
926
		// Check empty config ID.
927
		if ( empty( $config_id ) ) {
928
			$config_id = \get_option( 'pronamic_pay_config_id' );
929
		}
930
931
		// Check empty config ID.
932
		if ( empty( $config_id ) ) {
933
			$config_id = null;
934
		}
935
936
		return $config_id;
937
	}
938
}
939