Failed Conditions
Push — develop ( da59bc...2d4e05 )
by Reüel
06:13
created

src/Gateways/Gateway.php (3 issues)

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