Failed Conditions
Push — develop ( e051ae...fb8dd5 )
by Remco
07:13 queued 18s
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\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 2.3.1
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
		// Check if prior txn is expired yet or not, if so create a temporary txn so the user can access the content immediately.
454
		$prior_txn = $memberpress_subscription->latest_txn();
455
456
		if ( false === $prior_txn || ! ( $prior_txn instanceof MeprTransaction ) || strtotime( $prior_txn->expires_at ) < time() ) {
457
			$txn                  = new MeprTransaction();
458
			$txn->subscription_id = $memberpress_subscription->id;
0 ignored issues
show
Bug Best Practice introduced by
The property subscription_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
459
			$txn->trans_num       = $memberpress_subscription->subscr_id . '-' . uniqid();
0 ignored issues
show
Bug Best Practice introduced by
The property trans_num does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
460
			$txn->status          = MeprTransaction::$confirmed_str;
461
			$txn->txn_type        = MeprTransaction::$subscription_confirmation_str;
462
			$txn->response        = (string) $memberpress_subscription;
463
			$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...
464
465
			$txn->set_subtotal( 0.00 ); // Just a confirmation txn.
466
467
			$txn->store();
468
		}
469
470
		// Send resumed subscription notices.
471
		MeprUtils::send_resumed_sub_notices( $memberpress_subscription );
472
473
		// Add note.
474
		$note = sprintf(
475
			/* translators: %s: extension name */
476
			__( '%s subscription reactivated.', 'pronamic_ideal' ),
477
			__( 'MemberPress', 'pronamic_ideal' )
478
		);
479
480
		$pronamic_subscription->add_note( $note );
481
482
		// The status of canceled or completed subscriptions will not be changed automatically.
483
		if ( ! in_array( $pronamic_subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
484
			$pronamic_subscription->set_status( SubscriptionStatus::ACTIVE );
485
486
			$pronamic_subscription->save();
487
		}
488
	}
489
490
	/**
491
	 * Record resume subscription.
492
	 *
493
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L227-L230
494
	 * @return void
495
	 */
496
	public function record_resume_subscription() {
497
498
	}
499
500
	/**
501
	 * Process cancel subscription.
502
	 *
503
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L232-L236
504
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1687-L1715
505
	 * @param int $subscription_id Subscription id.
506
	 * @return void
507
	 */
508
	public function process_cancel_subscription( $subscription_id ) {
509
		$memberpress_subscription = MemberPress::get_subscription_by_id( $subscription_id );
510
511
		if ( null === $memberpress_subscription ) {
512
			return;
513
		}
514
515
		/**
516
		 * If the MemberPress subscription is already cancelled we bail out.
517
		 */
518
		if ( MeprSubscription::$cancelled_str === $memberpress_subscription->status ) {
519
			return;
520
		}
521
522
		$pronamic_subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id );
523
524
		if ( ! $pronamic_subscription ) {
525
			return;
526
		}
527
528
		// Add note.
529
		$note = sprintf(
530
			/* translators: %s: extension name */
531
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
532
			__( 'MemberPress', 'pronamic_ideal' )
533
		);
534
535
		$pronamic_subscription->add_note( $note );
536
537
		// The status of canceled or completed subscriptions will not be changed automatically.
538
		if ( ! in_array( $pronamic_subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
539
			$pronamic_subscription->set_status( SubscriptionStatus::CANCELLED );
540
541
			$pronamic_subscription->next_payment_date          = null;
542
			$pronamic_subscription->next_payment_delivery_date = null;
543
544
			// Delete next payment post meta.
545
			$pronamic_subscription->set_meta( 'next_payment', null );
546
			$pronamic_subscription->set_meta( 'next_payment_delivery_date', null );
547
548
			$pronamic_subscription->save();
549
		}
550
551
		// Cancel MemberPress subscription.
552
		$memberpress_subscription->status = MeprSubscription::$cancelled_str;
553
554
		$memberpress_subscription->store();
555
556
		// Expire the grace period (confirmation) if no completed payments have come through.
557
		if ( (int) $memberpress_subscription->txn_count <= 0 ) {
558
			$memberpress_subscription->expire_txns();
559
		}
560
561
		$memberpress_subscription->limit_reached_actions();
562
563
		// Send cancelled subscription notices.
564
		MeprUtils::send_cancelled_sub_notices( $memberpress_subscription );
565
	}
566
567
	/**
568
	 * Record cancel subscription.
569
	 *
570
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L238-L242
571
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1717-L1753
572
	 * @return void
573
	 */
574
	public function record_cancel_subscription() {
575
576
	}
577
578
	/**
579
	 * Process signup form.
580
	 *
581
	 * Gets called when the signup form is posted used for running any payment
582
	 * method specific actions when processing the customer signup form.
583
	 *
584
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L244-L247
585
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1755-L1764
586
	 * @param MeprTransaction $txn MemberPress transaction object.
587
	 * @return void
588
	 */
589
	public function process_signup_form( $txn ) {
590
591
	}
592
593
	/**
594
	 * Display payment page.
595
	 *
596
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L249-L253
597
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1766-L1768
598
	 * @param MeprTransaction $txn MemberPress transaction object.
599
	 * @return void
600
	 * @throws \Exception Throws exception on gateway payment start error.
601
	 */
602
	public function display_payment_page( $txn ) {
603
604
	}
605
606
	/**
607
	 * Enqueue payment form scripts.
608
	 *
609
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L255-L258
610
	 * @return void
611
	 */
612
	public function enqueue_payment_form_scripts() {
613
614
	}
615
616
	/**
617
	 * Display payment form.
618
	 *
619
	 * This spits out html for the payment form on the registration / payment
620
	 * page for the user to fill out for payment.
621
	 *
622
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L571
623
	 * @param float    $amount     Transaction amount to create a payment form for.
624
	 * @param MeprUser $user       MemberPress user object.
625
	 * @param int      $product_id Product ID.
626
	 * @param int      $txn_id     Transaction ID.
627
	 * @return void
628
	 */
629
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
630
		// Gateway.
631
		$config_id = $this->get_config_id();
632
633
		$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...
634
635
		if ( null === $gateway ) {
636
637
			$admin_message = null;
638
639
			if ( \current_user_can( 'manage_options' ) ) {
640
				$admin_message = __( 'For admins only: check payment method settings in MemberPress.', 'pronamic_ideal' );
641
			}
642
643
			printf(
644
				'<div class="mp_wrapper mp_payment_form_wrapper"><ul><li>%s</li>%s</ul></div>',
645
				\esc_html( Plugin::get_default_error_message() ),
646
				null === $admin_message ? '' : sprintf( '<li><em>%s</em></li>', \esc_html( $admin_message ) )
647
			);
648
649
			return;
650
		}
651
652
		// Invoice.
653
		$product = new MeprProduct( $product_id );
654
655
		$coupon = false;
656
657
		$txn = new MeprTransaction( $txn_id );
658
659
		// Artificially set the price of the $prd in case a coupon was used.
660
		if ( $product->price !== $amount ) {
661
			$coupon         = true;
662
			$product->price = $amount;
663
		}
664
665
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
666
667
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
668
		echo $invoice;
669
670
		?>
671
		<div class="mp_wrapper mp_payment_form_wrapper">
672
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
673
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
674
				<input type="hidden" name="mepr_transaction_id" value="<?php echo \esc_attr( (string) $txn_id ); ?>"/>
675
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
676
677
				<div class="mepr_spacer">&nbsp;</div>
678
679
				<?php
680
681
				$gateway->set_payment_method( $this->payment_method );
682
683
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
684
				echo $gateway->get_input_html();
685
686
				?>
687
688
				<div class="mepr_spacer">&nbsp;</div>
689
690
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
691
				<img src="<?php echo \esc_url( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
692
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
693
694
				<noscript>
695
					<p class="mepr_nojs">
696
						<?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' ); ?>
697
					</p>
698
				</noscript>
699
			</form>
700
		</div>
701
		<?php
702
	}
703
704
	/**
705
	 * Single-page checkout payment fields.
706
	 *
707
	 * @return string
708
	 */
709
	public function spc_payment_fields() {
710
		// Gateway.
711
		$config_id = $this->get_config_id();
712
713
		$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...
714
715
		if ( null === $gateway ) {
716
			return '';
717
		}
718
719
		// Input HTML.
720
		$gateway->set_payment_method( $this->payment_method );
721
722
		$html = $gateway->get_input_html();
723
724
		if ( empty( $html ) ) {
725
			return '';
726
		}
727
728
		return $html;
729
	}
730
731
	/**
732
	 * Validate payment form.
733
	 *
734
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprCheckoutCtrl.php#L648
735
	 * @param string[] $errors Array with errors.
736
	 * @return string[]
737
	 */
738
	public function validate_payment_form( $errors ) {
739
		return $errors;
740
	}
741
742
	/**
743
	 * Display options form.
744
	 *
745
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/views/admin/options/gateway.php#L41
746
	 * @return void
747
	 */
748
	public function display_options_form() {
749
		$mepr_options = MeprOptions::fetch();
750
751
		?>
752
		<table>
753
			<tr>
754
				<?php
755
756
				$name = sprintf(
757
					'%s[%s][%s]',
758
					$mepr_options->integrations_str,
759
					$this->id,
760
					'config_id'
761
				);
762
763
				?>
764
				<td>
765
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
766
				</td>
767
				<td>
768
					<select name="<?php echo esc_attr( $name ); ?>">
769
						<?php
770
771
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
772
							printf(
773
								'<option value="%s" %s>%s</option>',
774
								esc_attr( $value ),
775
								selected( $value, $this->settings->config_id, false ),
776
								esc_html( $label )
777
							);
778
						}
779
780
						?>
781
					</select>
782
				</td>
783
			</tr>
784
		</table>
785
		<?php
786
	}
787
788
	/**
789
	 * Validate options form.
790
	 *
791
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L468
792
	 * @ilnk https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2006-L2026
793
	 * @param string[] $errors Array with errors.
794
	 * @return string[]
795
	 */
796
	public function validate_options_form( $errors ) {
797
		return $errors;
798
	}
799
800
	/**
801
	 * Enqueue user account scripts.
802
	 *
803
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L126
804
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L2028-L2044
805
	 * @return void
806
	 */
807
	public function enqueue_user_account_scripts() {
808
809
	}
810
811
	/**
812
	 * Display update account form.
813
	 *
814
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/controllers/MeprAccountCtrl.php#L438
815
	 * @param string   $sub_id  Subscription ID.
816
	 * @param string[] $errors  Array with errors.
817
	 * @param string   $message Update message.
818
	 * @return void
819
	 */
820
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
821
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $sub_id );
822
823
		$subscriptions = ( null === $subscriptions ) ? array() : $subscriptions;
824
825
		$subscription = \reset( $subscriptions );
826
827
		$message = \__( 'The payment method for this subscription can not be updated manually.', 'pronamic_ideal' );
828
829
		if ( false !== $subscription ) {
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( $subscription->get_mandate_selection_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