Failed Conditions
Push — develop ( 30ce77...58a441 )
by Reüel
03:57
created

src/Gateways/Gateway.php (2 issues)

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