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