Failed Conditions
Push — master ( bf2baf...c757d7 )
by Reüel
09:53 queued 11s
created

src/Gateways/Gateway.php (8 issues)

1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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.1.2
36
 * @since   1.0.0
37
 */
38
class Gateway extends MeprBaseRealGateway {
39
	/**
40
	 * Payment method.
41
	 *
42
	 * @var string
43
	 */
44
	protected $payment_method;
45
46
	/**
47
	 * MemberPress transaction.
48
	 *
49
	 * @var MeprTransaction
50
	 */
51
	public $mp_txn;
52
53
	/**
54
	 * Pronamic payment.
55
	 *
56
	 * @var Payment
57
	 */
58
	public $pronamic_payment;
59
60
	/**
61
	 * Constructs and initialize iDEAL gateway.
62
	 */
63
	public function __construct() {
64
		// Set the name of this gateway.
65
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L12-13.
66
		$this->name = __( 'Pronamic', 'pronamic_ideal' );
67
68
		if ( ! empty( $this->payment_method ) ) {
69
			$this->name = sprintf(
70
				/* translators: %s: payment method name */
71
				__( 'Pronamic - %s', 'pronamic_ideal' ),
72
				PaymentMethods::get_name( $this->payment_method )
73
			);
74
		}
75
76
		// Set the default settings.
77
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73.
78
		$this->set_defaults();
79
80
		// Set the capabilities of this gateway.
81
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L36-37.
82
		$this->capabilities = array();
83
84
		// Setup the notification actions for this gateway.
85
		$this->notifiers = array();
86
	}
87
88
	/**
89
	 * Load the specified settings.
90
	 *
91
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L69-70
92
	 *
93
	 * @param array $settings MemberPress gateway settings array.
94
	 */
95
	public function load( $settings ) {
96
		$this->settings = (object) $settings;
97
98
		$this->set_defaults();
99
	}
100
101
	/**
102
	 * Custom helper function to send transaction notices.
103
	 *
104
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprUtils.php#L1333-L1351
105
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
106
	 *
107
	 * @param MeprTransaction $transaction MemberPress transaction object.
108
	 * @param string          $method      PHP function name to call.
109
	 *
110
	 * @return mixed
111
	 */
112
	public function send_transaction_notices( $transaction, $method ) {
113
		$class = 'MeprUtils';
114
115
		if ( ! Core_Util::class_method_exists( $class, $method ) ) {
116
			$class = $this;
117
		}
118
119
		// `send_product_welcome_notices` is called from `send_signup_notices` in newer versions.
120
		if ( 'send_product_welcome_notices' === $method ) {
121
			if ( 'MeprUtils' === $class ) {
122
				return null;
123
			}
124
125
			if ( ! \method_exists( $class, 'send_product_welcome_notices' ) ) {
126
				return null;
127
			}
128
		}
129
130
		return call_user_func( array( $class, $method ), $transaction );
131
	}
132
133
	/**
134
	 * Get icon function (this is not a MemberPress function).
135
	 *
136
	 * @since 1.0.2
137
	 * @return string
138
	 */
139
	protected function get_icon() {
140
		return '';
141
	}
142
143
	/**
144
	 * Get class alias name.
145
	 *
146
	 * @return string
147
	 */
148
	public function get_alias() {
149
		return 'MeprPronamicGateway';
150
	}
151
152
	/**
153
	 * Set the default settings.
154
	 *
155
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L72-73
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->get_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
	 * @param MeprTransaction $txn MemberPress transaction object.
193
	 *
194
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L119-122
195
	 */
196
	public function process_payment( $txn ) {
197
198
	}
199
200
	/**
201
	 * Record subscription payment.
202
	 *
203
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L140-145
204
	 */
205
	public function record_subscription_payment() {
206
		$transaction = $this->mp_txn;
207
208
		$transaction->status     = MeprTransaction::$complete_str;
209
		$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...
210
		$transaction->store();
211
212
		$subscription = $transaction->subscription();
213
214
		if ( $subscription ) {
215
			if ( MeprSubscription::$active_str !== $subscription->status ) {
216
				$subscription->status = MeprSubscription::$active_str;
217
				$subscription->store();
218
			}
219
220
			$subscription->expire_confirmation_txn();
221
222
			$subscription->limit_payment_cycles();
223
		}
224
225
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
226
227
		return $transaction;
228
	}
229
230
	/**
231
	 * Record payment failure.
232
	 *
233
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L147-148
234
	 */
235
	public function record_payment_failure() {
236
		$transaction = $this->mp_txn;
237
238
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L50.
239
		$transaction->status = MeprTransaction::$failed_str;
240
		$transaction->store();
241
242
		// Expire associated subscription transactions for non-recurring payments.
243
		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...
244
			$subscription = $transaction->subscription();
245
246
			if ( $subscription ) {
247
				$subscription->expire_txns();
248
				$subscription->store();
249
			}
250
		}
251
252
		$this->send_transaction_notices( $transaction, 'send_failed_txn_notices' );
253
254
		return $transaction;
255
	}
256
257
	/**
258
	 * Record payment.
259
	 *
260
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L124-129
261
	 */
262
	public function record_payment() {
263
		$transaction = $this->mp_txn;
264
265
		$transaction->status = MeprTransaction::$complete_str;
266
267
		// This will only work before maybe_cancel_old_sub is run.
268
		$upgrade   = $transaction->is_upgrade();
269
		$downgrade = $transaction->is_downgrade();
270
271
		$event_transaction = $transaction->maybe_cancel_old_sub();
0 ignored issues
show
Are you sure the assignment to $event_transaction is correct as $transaction->maybe_cancel_old_sub() targeting MeprTransaction::maybe_cancel_old_sub() 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...
272
273
		$subscription = $transaction->subscription();
274
275
		if ( $subscription ) {
276
			$event_subscription = $subscription->maybe_cancel_old_sub();
0 ignored issues
show
Are you sure the assignment to $event_subscription is correct as $subscription->maybe_cancel_old_sub() targeting MeprSubscription::maybe_cancel_old_sub() 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...
277
278
			$subscription->status     = MeprSubscription::$active_str;
279
			$subscription->created_at = $transaction->created_at;
280
			$subscription->store();
281
282
			if ( false === $event_transaction && false !== $event_subscription ) {
283
				$event_transaction = $event_subscription;
284
			}
285
		}
286
287
		$transaction->store();
288
289
		/*
290
		 * For some reasons the `send_product_welcome_notices` function accepts 1 or 3 arguments. We are not sure
291
		 * if this is a difference in the 'Business' and 'Developer' edition or between version `1.2.4` and `1.2.7`.
292
		 *
293
		 * @link https://github.com/wp-premium/memberpress-developer/blob/1.2.4/app/lib/MeprBaseGateway.php#L596-L612
294
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/lib/MeprBaseGateway.php#L609-L619
295
		 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprTransaction.php#L51
296
		 */
297
		$reflection = new ReflectionClass( 'MeprBaseRealGateway' );
298
299
		if ( $reflection->hasMethod( 'send_product_welcome_notices' ) && 3 === $reflection->getMethod( 'send_product_welcome_notices' )->getNumberOfParameters() ) {
300
			$uemail = MeprEmailFactory::fetch(
301
				'MeprUserProductWelcomeEmail',
302
				'MeprBaseProductEmail',
303
				array(
304
					array(
305
						'product_id' => $transaction->product_id,
306
					),
307
				)
308
			);
309
310
			/**
311
			 * The `send_product_welcome_notices` method is only available in earlier version of MemberPress.
312
			 *
313
			 * @scrutinizer ignore-call
314
			 */
315
			$this->send_product_welcome_notices(
316
				$uemail,
317
				MeprTransactionsHelper::get_email_params( $transaction ),
318
				$transaction->user()
319
			);
320
		} else {
321
			$this->send_transaction_notices( $transaction, 'send_product_welcome_notices' );
322
		}
323
324
		// Send upgrade/downgrade notices.
325
		$product = $transaction->product();
326
327
		if ( 'lifetime' === $product->period_type ) {
328
			if ( $upgrade ) {
329
				$this->upgraded_sub( $transaction, $event_transaction );
330
				$this->send_transaction_notices( $transaction, 'send_upgraded_txn_notices' );
331
			} elseif ( $downgrade ) {
332
				$this->downgraded_sub( $transaction, $event_transaction );
333
				$this->send_transaction_notices( $transaction, 'send_downgraded_txn_notices' );
334
			} else {
335
				$this->new_sub( $transaction );
336
			}
337
		}
338
339
		$this->send_transaction_notices( $transaction, 'send_signup_notices' );
340
		$this->send_transaction_notices( $transaction, 'send_transaction_receipt_notices' );
341
342
		return $transaction;
343
	}
344
345
	/**
346
	 * Process refund.
347
	 *
348
	 * @param MeprTransaction $txn MemberPress transaction object.
349
	 *
350
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L131-133
351
	 */
352
	public function process_refund( MeprTransaction $txn ) {
353
354
	}
355
356
	/**
357
	 * Record refund.
358
	 *
359
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L135-138
360
	 */
361
	public function record_refund() {
362
363
	}
364
365
	/**
366
	 * Process trial payment.
367
	 *
368
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L150-157
369
	 *
370
	 * @param MeprTransaction $transaction MemberPress transaction object.
371
	 */
372
	public function process_trial_payment( $transaction ) {
373
374
	}
375
376
	/**
377
	 * Reord trial payment.
378
	 *
379
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L159-161
380
	 *
381
	 * @param MeprTransaction $transaction MemberPress transaction object.
382
	 */
383
	public function record_trial_payment( $transaction ) {
384
385
	}
386
387
	/**
388
	 * Process create subscription.
389
	 *
390
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L163-167
391
	 *
392
	 * @param MeprTransaction $txn MemberPress transaction object.
393
	 */
394
	public function process_create_subscription( $txn ) {
395
396
	}
397
398
	/**
399
	 * Record create subscription.
400
	 *
401
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L169-174
402
	 */
403
	public function record_create_subscription() {
404
405
	}
406
407
	/**
408
	 * Process update subscription.
409
	 *
410
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L176
411
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L194
412
	 *
413
	 * @param int $sub_id Subscription ID.
414
	 */
415
	public function process_update_subscription( $sub_id ) {
416
417
	}
418
419
	/**
420
	 * Record update subscription.
421
	 *
422
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L178-182
423
	 */
424
	public function record_update_subscription() {
425
426
	}
427
428
	/**
429
	 * Process suspend subscription.
430
	 *
431
	 * @param int $sub_id Subscription id.
432
	 *
433
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L184-186
434
	 */
435
	public function process_suspend_subscription( $sub_id ) {
436
		if ( ! MeprSubscription::exists( $sub_id ) ) {
437
			return;
438
		}
439
440
		$sub = new MeprSubscription( $sub_id );
441
442
		if ( MeprSubscription::$suspended_str === $sub->status ) {
443
			// Subscription is already suspended.
444
			return;
445
		}
446
447
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
448
449
		if ( ! $subscription ) {
450
			return;
451
		}
452
453
		$sub->status = MeprSubscription::$suspended_str;
454
455
		$sub->store();
456
457
		// Send suspended subscription notices.
458
		MeprUtils::send_suspended_sub_notices( $sub );
459
460
		$note = sprintf(
461
			/* translators: %s: MemberPress */
462
			__( '%s subscription on hold.', 'pronamic_ideal' ),
463
			__( 'MemberPress', 'pronamic_ideal' )
464
		);
465
466
		$subscription->add_note( $note );
467
468
		// The status of canceled or completed subscriptions will not be changed automatically.
469
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
470
			$subscription->set_status( SubscriptionStatus::ON_HOLD );
471
472
			$subscription->save();
473
		}
474
	}
475
476
	/**
477
	 * Record suspend subscription.
478
	 *
479
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L188-191
480
	 */
481
	public function record_suspend_subscription() {
482
483
	}
484
485
	/**
486
	 * Process resume subscription.
487
	 *
488
	 * @param int $sub_id Subscription id.
489
	 *
490
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L193-195
491
	 */
492
	public function process_resume_subscription( $sub_id ) {
493
		if ( ! MeprSubscription::exists( $sub_id ) ) {
494
			return;
495
		}
496
497
		$sub = new MeprSubscription( $sub_id );
498
499
		if ( MeprSubscription::$active_str === $sub->status ) {
500
			// Subscription is already active.
501
			return;
502
		}
503
504
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
505
506
		if ( ! $subscription ) {
507
			return;
508
		}
509
510
		$sub->status = MeprSubscription::$active_str;
511
512
		$sub->store();
513
514
		// Check if prior txn is expired yet or not, if so create a temporary txn so the user can access the content immediately.
515
		$prior_txn = $sub->latest_txn();
516
517
		if ( false === $prior_txn || ! ( $prior_txn instanceof MeprTransaction ) || strtotime( $prior_txn->expires_at ) < time() ) {
518
			$txn                  = new MeprTransaction();
519
			$txn->subscription_id = $sub->id;
520
			$txn->trans_num       = $sub->subscr_id . '-' . uniqid();
521
			$txn->status          = MeprTransaction::$confirmed_str;
522
			$txn->txn_type        = MeprTransaction::$subscription_confirmation_str;
523
			$txn->response        = (string) $sub;
524
			$txn->expires_at      = MeprUtils::ts_to_mysql_date( time() + MeprUtils::days( 1 ), 'Y-m-d 23:59:59' );
525
526
			$txn->set_subtotal( 0.00 ); // Just a confirmation txn.
527
528
			$txn->store();
529
		}
530
531
		// Send resumed subscription notices.
532
		MeprUtils::send_resumed_sub_notices( $sub );
533
534
		// Add note.
535
		$note = sprintf(
536
			/* translators: %s: MemberPress */
537
			__( '%s subscription reactivated.', 'pronamic_ideal' ),
538
			__( 'MemberPress', 'pronamic_ideal' )
539
		);
540
541
		$subscription->add_note( $note );
542
543
		// The status of canceled or completed subscriptions will not be changed automatically.
544
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
545
			$subscription->set_status( SubscriptionStatus::ACTIVE );
546
547
			$subscription->save();
548
		}
549
	}
550
551
	/**
552
	 * Record resume subscription.
553
	 *
554
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L197-201
555
	 */
556
	public function record_resume_subscription() {
557
558
	}
559
560
	/**
561
	 * Process cancel subscription.
562
	 *
563
	 * @param int $sub_id Subscription id.
564
	 *
565
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L202-206
566
	 */
567
	public function process_cancel_subscription( $sub_id ) {
568
		if ( ! MeprSubscription::exists( $sub_id ) ) {
569
			return;
570
		}
571
572
		$sub = new MeprSubscription( $sub_id );
573
574
		if ( MeprSubscription::$cancelled_str === $sub->status ) {
575
			// Subscription is already cancelled.
576
			return;
577
		}
578
579
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $sub->id );
580
581
		if ( ! $subscription ) {
582
			return;
583
		}
584
585
		$sub->status = MeprSubscription::$cancelled_str;
586
587
		$sub->store();
588
589
		// Expire the grace period (confirmation) if no completed payments have come through.
590
		if ( (int) $sub->txn_count <= 0 ) {
591
			$sub->expire_txns();
592
		}
593
594
		$sub->limit_reached_actions();
595
596
		// Send cancelled subscription notices.
597
		MeprUtils::send_cancelled_sub_notices( $sub );
598
599
		// Add note.
600
		$note = sprintf(
601
			/* translators: %s: MemberPress */
602
			__( '%s subscription cancelled.', 'pronamic_ideal' ),
603
			__( 'MemberPress', 'pronamic_ideal' )
604
		);
605
606
		$subscription->add_note( $note );
607
608
		// The status of canceled or completed subscriptions will not be changed automatically.
609
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
610
			$subscription->set_status( SubscriptionStatus::CANCELLED );
611
612
			$subscription->save();
613
		}
614
	}
615
616
	/**
617
	 * Record cancel subscription.
618
	 *
619
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L208-212
620
	 */
621
	public function record_cancel_subscription() {
622
623
	}
624
625
	/**
626
	 * Process signup form.
627
	 *
628
	 * Gets called when the signup form is posted used for running any payment
629
	 * method specific actions when processing the customer signup form.
630
	 *
631
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L214-217
632
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L262
633
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L232-L235
634
	 *
635
	 * @param MeprTransaction $txn MemberPress transaction object.
636
	 */
637
	public function process_signup_form( $txn ) {
638
639
	}
640
641
	/**
642
	 * Payment redirect.
643
	 *
644
	 * @param MeprTransaction $txn MemberPress transaction object.
645
	 *
646
	 * @throws \Exception Throws exception on gateway payment start error.
647
	 * @since 1.0.2
648
	 */
649
	public function payment_redirect( $txn ) {
650
		$txn = new MeprTransaction( $txn->id );
651
652
		// Gateway.
653
		$config_id = $this->settings->config_id;
654
655
		$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...
656
657
		if ( ! $gateway ) {
658
			return;
659
		}
660
661
		// Create Pronamic payment.
662
		$payment = Pronamic::get_payment( $txn );
663
664
		$payment->config_id = $this->settings->config_id;
665
		$payment->method    = $this->payment_method;
666
667
		$error = null;
668
669
		try {
670
			$payment = Plugin::start_payment( $payment );
671
		} catch ( \Exception $e ) {
672
			$error = $e;
673
		}
674
675
		/*
676
		 * Update transaction subtotal.
677
		 *
678
		 * Notes:
679
		 * - MemberPress also uses trial amount for prorated upgrade/downgrade
680
		 * - Not updated BEFORE payment start, as transaction total amount is used for subscription amount.
681
		 * - Reload transaction to make sure actual status is being used (i.e. on free downgrade).
682
		 */
683
		$txn = new MeprTransaction( $txn->id );
684
685
		$subscription = $txn->subscription();
686
687
		if ( $subscription && $subscription->in_trial() ) {
688
			$txn->set_subtotal( $subscription->trial_amount );
689
			$txn->store();
690
		}
691
692
		if ( $error instanceof \Exception ) {
693
			// Rethrow error, catched by MemberPress.
694
			throw $error;
695
		}
696
697
		// Redirect.
698
		$gateway->redirect( $payment );
699
	}
700
701
	/**
702
	 * Display payment page.
703
	 *
704
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
705
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L290
706
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L775-L850
707
	 *
708
	 * @param MeprTransaction $txn MemberPress transaction object.
709
	 *
710
	 * @throws \Exception Throws exception on gateway payment start error.
711
	 */
712
	public function display_payment_page( $txn ) {
713
		// Gateway.
714
		$config_id = $this->settings->config_id;
715
716
		$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...
717
718
		if ( $gateway && '' === $gateway->get_input_html() ) {
719
			$this->payment_redirect( $txn );
720
		}
721
	}
722
723
	/**
724
	 * Process payment form.
725
	 *
726
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L239-289
727
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L336
728
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L1011
729
	 *
730
	 * @param MeprTransaction $txn MemberPress transaction object.
731
	 *
732
	 * @return bool
733
	 * @throws \Exception Throws exception on gateway payment start error.
734
	 */
735
	public function process_payment_form( $txn ) {
736
		if ( ! filter_has_var( INPUT_POST, 'pronamic_pay_memberpress_pay' ) ) {
737
			return false;
738
		}
739
740
		// Gateway.
741
		$config_id = $this->settings->config_id;
742
743
		$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...
744
745
		if ( $gateway ) {
746
			$this->payment_redirect( $txn );
747
		}
748
	}
749
750
	/**
751
	 * Enqueue payment form scripts.
752
	 *
753
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L219-223
754
	 */
755
	public function enqueue_payment_form_scripts() {
756
757
	}
758
759
	/**
760
	 * Display payment form.
761
	 *
762
	 * This spits out html for the payment form on the registration / payment
763
	 * page for the user to fill out for payment.
764
	 *
765
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L230-233
766
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L248-L251
767
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L318
768
	 *
769
	 * @param float    $amount     Transaction amount to create a payment form for.
770
	 * @param MeprUser $user       MemberPress user object.
771
	 * @param int      $product_id Product ID.
772
	 * @param int      $txn_id     Transaction ID.
773
	 */
774
	public function display_payment_form( $amount, $user, $product_id, $txn_id ) {
775
		$product = new MeprProduct( $product_id );
776
777
		$coupon = false;
778
779
		$txn = new MeprTransaction( $txn_id );
780
781
		// Artifically set the price of the $prd in case a coupon was used.
782
		if ( $product->price !== $amount ) {
783
			$coupon         = true;
784
			$product->price = $amount;
785
		}
786
787
		$invoice = MeprTransactionsHelper::get_invoice( $txn );
788
789
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
790
		echo $invoice;
791
792
		?>
793
		<div class="mp_wrapper mp_payment_form_wrapper">
794
			<form action="" method="post" id="payment-form" class="mepr-form" novalidate>
795
				<input type="hidden" name="mepr_process_payment_form" value="Y"/>
796
				<input type="hidden" name="mepr_transaction_id" value="<?php echo esc_attr( $txn_id ); ?>"/>
797
				<input type="hidden" name="pronamic_pay_memberpress_pay" value="1"/>
798
799
				<div class="mepr_spacer">&nbsp;</div>
800
801
				<?php
802
803
				// Gateway.
804
				$config_id = $this->settings->config_id;
805
806
				$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...
807
808
				if ( $gateway ) {
809
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
810
					echo $gateway->get_input_html();
811
				}
812
813
				?>
814
815
				<div class="mepr_spacer">&nbsp;</div>
816
817
				<input type="submit" class="mepr-submit" value="<?php esc_attr_e( 'Pay', 'pronamic_ideal' ); ?>"/>
818
				<img src="<?php echo esc_attr( admin_url( 'images/loading.gif' ) ); ?>" style="display: none;" class="mepr-loading-gif"/>
819
				<?php MeprView::render( '/shared/has_errors', get_defined_vars() ); ?>
820
821
				<noscript>
822
					<p class="mepr_nojs">
823
						<?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' ); ?>
824
					</p>
825
				</noscript>
826
			</form>
827
		</div>
828
		<?php
829
	}
830
831
	/**
832
	 * Validate payment form.
833
	 *
834
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L235-236
835
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprCheckoutCtrl.php#L330
836
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseGateway.php#L253-L254
837
	 *
838
	 * @param array $errors Array with errors.
839
	 * @return array
840
	 */
841
	public function validate_payment_form( $errors ) {
842
		return $errors;
843
	}
844
845
	/**
846
	 * Display options form.
847
	 *
848
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L291-292
849
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/gateways/MeprAuthorizeGateway.php#L1027-1037
850
	 */
851
	public function display_options_form() {
852
		$mepr_options = MeprOptions::fetch();
853
854
		?>
855
		<table>
856
			<tr>
857
				<?php
858
859
				$name = sprintf(
860
					'%s[%s][%s]',
861
					$mepr_options->integrations_str,
862
					$this->id,
863
					'config_id'
864
				);
865
866
				?>
867
				<td>
868
					<?php esc_html_e( 'Configuration', 'pronamic_ideal' ); ?>
869
				</td>
870
				<td>
871
					<select name="<?php echo esc_attr( $name ); ?>">
872
						<?php
873
874
						foreach ( Plugin::get_config_select_options( $this->payment_method ) as $value => $label ) {
875
							printf(
876
								'<option value="%s" %s>%s</option>',
877
								esc_attr( $value ),
878
								selected( $value, $this->settings->config_id, false ),
879
								esc_html( $label )
880
							);
881
						}
882
883
						?>
884
					</select>
885
				</td>
886
			</tr>
887
		</table>
888
		<?php
889
	}
890
891
	/**
892
	 * Validate options form.
893
	 *
894
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L294-295
895
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalGateway.php#L909-L924
896
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L416-L423
897
	 *
898
	 * @param array $errors Array with errors.
899
	 * @return array
900
	 */
901
	public function validate_options_form( $errors ) {
902
		return $errors;
903
	}
904
905
	/**
906
	 * Enqueue user account scripts.
907
	 *
908
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L297-302
909
	 */
910
	public function enqueue_user_account_scripts() {
911
912
	}
913
914
	/**
915
	 * Display update account form.
916
	 *
917
	 * @param int    $sub_id  Subscription ID.
918
	 * @param array  $errors  Array with errors.
919
	 * @param string $message Update message.
920
	 *
921
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L365-366
922
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/lib/MeprBaseStaticGateway.php#L160-L161
923
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1108-L1168
924
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprAccountCtrl.php#L388
925
	 */
926
	public function display_update_account_form( $sub_id, $errors = array(), $message = '' ) {
927
928
	}
929
930
	/**
931
	 * Validate update account form.
932
	 *
933
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L368-369
934
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1170-L1173
935
	 *
936
	 * @param  array $errors Array with errors.
937
	 * @return array
938
	 */
939
	public function validate_update_account_form( $errors = array() ) {
940
		return $errors;
941
	}
942
943
	/**
944
	 * Process update account form.
945
	 *
946
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L371-372
947
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprStripeGateway.php#L1175-L1181
948
	 *
949
	 * @param int $sub_id Subscription ID.
950
	 */
951
	public function process_update_account_form( $sub_id ) {
952
953
	}
954
955
	/**
956
	 * Is test mode.
957
	 *
958
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L374-375
959
	 *
960
	 * @return boolean
961
	 */
962
	public function is_test_mode() {
963
		return false;
964
	}
965
966
	/**
967
	 * Force SSL.
968
	 *
969
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprBaseGateway.php#L377-378
970
	 *
971
	 * @return boolean
972
	 */
973
	public function force_ssl() {
974
		return false;
975
	}
976
}
977