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