Failed Conditions
Push — develop ( 859ffd...1f8c75 )
by Remco
06:53
created

src/Gateways/Gateway.php (9 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
	 * MemberPress transaction.
68
	 *
69
	 * @var MeprTransaction|null
70
	 */
71
	public $mp_txn;
72
73
	/**
74
	 * Pronamic payment.
75
	 *
76
	 * @var Payment|null
77
	 */
78
	public $pronamic_payment;
79
80
	/**
81
	 * Constructs and initialize gateway.
82
	 * 
83
	 * @param string      $class_alias    Class alias.
84
	 * @param string|null $payment_method Payment method.
85
	 */
86
	public function __construct( $class_alias = 'MeprPronamicGateway', $payment_method = null ) {
87
		$this->class_alias = $class_alias;
88
89
		$this->payment_method = $payment_method;
90
91
		// Set the name of this gateway.
92
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L12-13.
93
		$this->name = __( 'Pronamic', 'pronamic_ideal' );
94
95
		if ( ! empty( $this->payment_method ) ) {
96
			$this->name = sprintf(
97
				/* translators: %s: payment method name */
98
				__( 'Pronamic - %s', 'pronamic_ideal' ),
99
				PaymentMethods::get_name( $this->payment_method )
100
			);
101
		}
102
103
		// Set the default settings.
104
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73.
105
		$this->set_defaults();
106
107
		// Set the capabilities of this gateway.
108
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L36-37.
109
		$this->capabilities = array();
110
111
		// Setup the notification actions for this gateway.
112
		$this->notifiers = array();
113
114
		// Support single-page checkout.
115
		$this->has_spc_form = true;
116
117
		// Key.
118
		$key = 'pronamic_pay';
119
120
		if ( null !== $this->payment_method ) {
121
			$key = sprintf( 'pronamic_pay_%s', $this->payment_method );
122
		}
123
124
		$this->key = $key;
125
	}
126
127
	/**
128
	 * Load the specified settings.
129
	 *
130
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L73-L74
131
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprGatewayFactory.php#L18
132
	 * @param mixed $settings MemberPress gateway settings array.
133
	 * @return void
134
	 */
135
	public function load( $settings ) {
136
		$this->settings = (object) $settings;
137
138
		$this->set_defaults();
139
	}
140
141
	/**
142
	 * Get icon function (this is not a MemberPress function).
143
	 *
144
	 * @since 1.0.2
145
	 * @return string|null
146
	 */
147
	private function get_icon() {
148
		return PaymentMethods::get_icon_url( $this->payment_method );
149
	}
150
151
	/**
152
	 * Set the default settings.
153
	 *
154
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L76-L77
155
	 * @return void
156
	 */
157
	protected function set_defaults() {
158
		if ( ! isset( $this->settings ) ) {
159
			$this->settings = array();
160
		}
161
162
		$this->settings = (object) array_merge(
163
			array(
164
				'gateway'   => $this->class_alias,
165
				'id'        => $this->generate_id(),
166
				'label'     => '',
167
				'use_label' => true,
168
				'icon'      => $this->get_icon(),
169
				'use_icon'  => true,
170
				'desc'      => '',
171
				'use_desc'  => true,
172
				'config_id' => '',
173
				'email'     => '',
174
				'sandbox'   => false,
175
				'debug'     => false,
176
			),
177
			(array) $this->settings
178
		);
179
180
		$this->id        = $this->settings->id;
181
		$this->label     = $this->settings->label;
182
		$this->use_label = $this->settings->use_label;
183
		$this->icon      = $this->settings->icon;
184
		$this->use_icon  = $this->settings->use_icon;
185
		$this->desc      = $this->settings->desc;
186
		$this->use_desc  = $this->settings->use_desc;
187
	}
188
189
	/**
190
	 * Process payment.
191
	 *
192
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L149-L152
193
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L520-L585
194
	 * @param MeprTransaction $txn MemberPress transaction object.
195
	 * @return void
196
	 */
197
	public function process_payment( $txn ) {
198
199
	}
200
201
	/**
202
	 * Record subscription payment.
203
	 *
204
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L170-L175
205
	 * @return void
206
	 */
207
	public function record_subscription_payment() {
208
		$transaction = $this->mp_txn;
209
210
		$transaction->status     = MeprTransaction::$complete_str;
211
		$transaction->expires_at = MeprUtils::ts_to_mysql_date( $this->pronamic_payment->get_end_date()->getTimestamp(), '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...
The method get_end_date() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
		$transaction->expires_at = MeprUtils::ts_to_mysql_date( $this->pronamic_payment->/** @scrutinizer ignore-call */ get_end_date()->getTimestamp(), 'Y-m-d 23:59:59' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
		$transaction->store();
0 ignored issues
show
The method store() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
		$transaction->/** @scrutinizer ignore-call */ 
213
                store();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
213
214
		$subscription = $transaction->subscription();
215
216
		if ( $subscription ) {
217
			$should_activate = ! \in_array(
218
				$subscription->status,
219
				array(
220
					MeprSubscription::$active_str,
221
					MeprSubscription::$cancelled_str,
222
				),
223
				true
224
			);
225
226
			if ( $should_activate ) {
227
				$subscription->status = MeprSubscription::$active_str;
228
				$subscription->store();
229
			}
230
231
			$subscription->expire_confirmation_txn();
232
233
			$subscription->limit_payment_cycles();
234
		}
235
236
		/**
237
		 * Send transaction receipt notices.
238
		 * 
239
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
240
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L249
241
		 */
242
		MeprUtils::send_transaction_receipt_notices( $transaction );
243
	}
244
245
	/**
246
	 * Record payment failure.
247
	 *
248
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L177-L178
249
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L833-L910
250
	 * @return void
251
	 */
252
	public function record_payment_failure() {
253
		$transaction = $this->mp_txn;
254
255
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
256
		$transaction->status = MeprTransaction::$failed_str;
257
		$transaction->store();
258
259
		// Expire associated subscription transactions for non-recurring payments.
260
		if ( ! ( isset( $this->pronamic_payment ) && $this->pronamic_payment->get_recurring() ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pronamic_payment->get_recurring() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

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