-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGateway.php
780 lines (657 loc) · 16.9 KB
/
Gateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<?php
/**
* Gateway
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Core
*/
namespace Pronamic\WordPress\Pay\Core;
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
use Pronamic\WordPress\Pay\Payments\Payment;
use Pronamic\WordPress\Pay\Plugin;
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
use Pronamic\WordPress\Pay\Util as PayUtil;
use WP_Error;
/**
* Title: Gateway
* Description:
* Copyright: 2005-2022 Pronamic
* Company: Pronamic
*
* @author Remco Tolsma
* @version 2.5.1
* @since 1.0.0
*/
abstract class Gateway {
/**
* Method indicator for an gateway which works through an HTML form
*
* @var int
*/
const METHOD_HTML_FORM = 1;
/**
* Method indicator for an gateway which works through an HTTP redirect
*
* @var int
*/
const METHOD_HTTP_REDIRECT = 2;
/**
* Indicator for test mode
*
* @var string
*/
const MODE_TEST = 'test';
/**
* Indicator for live mode
*
* @var string
*/
const MODE_LIVE = 'live';
/**
* The method of this gateway
*
* @var int
*/
private $method;
/**
* Payment method to use on this gateway.
*
* @since 1.2.3
* @var string|null
*/
private $payment_method;
/**
* Supported features on this gateway.
*
* @since 1.3.9
* @var array
*/
protected $supports;
use ModeTrait;
/**
* Construct gateway.
*/
public function __construct() {
/**
* Supported features.
*
* Possible values:
* - payment_status_request Gateway can request current payment status.
* - recurring_apple_pay Recurring payments through Apple Pay.
* - recurring_credit_card Recurring payments through credit card.
* - recurring_direct_debit Recurring payments through direct debit.
*/
$this->supports = [];
}
/**
* Check if a gateway supports a given feature.
*
* @since 1.3.11
* @param string $feature The feature to check.
* @return bool True if supported, false otherwise.
*/
public function supports( $feature ) {
return in_array( $feature, $this->supports, true );
}
/**
* Set the method.
*
* @param int $method HTML form or HTTP redirect method.
* @return void
*/
public function set_method( $method ) {
$this->method = $method;
}
/**
* Check if this gateway works trough an HTTP redirect
*
* @return boolean true if an HTTP redirect is required, false otherwise
*/
public function is_http_redirect() {
return self::METHOD_HTTP_REDIRECT === $this->method;
}
/**
* Check if this gateway works through an HTML form
*
* @return boolean true if an HTML form is required, false otherwise
*/
public function is_html_form() {
return self::METHOD_HTML_FORM === $this->method;
}
/**
* Get iDEAL issuers.
*
* @return array
*/
public function get_issuers() {
return [];
}
/**
* Get credit card issuers.
*
* @return array|null
*/
public function get_credit_card_issuers() {
return null;
}
/**
* Get the iDEAL issuers transient.
*
* @return array|null
*/
public function get_transient_issuers() {
$issuers = null;
// Transient name.
$transient = 'pronamic_pay_issuers_' . md5( serialize( $this ) );
$result = get_transient( $transient );
if ( is_wp_error( $result ) || false === $result ) {
try {
$issuers = $this->get_issuers();
} catch ( \Exception $e ) {
$issuers = null;
}
if ( ! empty( $issuers ) ) {
// 60 * 60 * 24 = 24 hours = 1 day
set_transient( $transient, $issuers, 60 * 60 * 24 );
}
} elseif ( is_array( $result ) ) {
$issuers = $result;
}
return $issuers;
}
/**
* Get the credit card issuers transient.
*
* @return array|null
*/
public function get_transient_credit_card_issuers() {
$issuers = null;
// Transient name.
$transient = 'pronamic_pay_credit_card_issuers_' . md5( serialize( $this ) );
$result = get_transient( $transient );
if ( is_wp_error( $result ) || false === $result ) {
$issuers = $this->get_credit_card_issuers();
if ( $issuers ) {
// 60 * 60 * 24 = 24 hours = 1 day
set_transient( $transient, $issuers, 60 * 60 * 24 );
}
} elseif ( is_array( $result ) ) {
$issuers = $result;
}
return $issuers;
}
/**
* Custom payment redirect.
* Intended to be overridden by gateway.
*
* @param Payment $payment Payment.
*
* @return void
*/
public function payment_redirect( Payment $payment ) {
}
/**
* Get supported payment providers for gateway.
* Intended to be overridden by gateway.
*
* @return array
*/
public function get_supported_payment_methods() {
return [];
}
/**
* Get available payment methods.
* Intended to be overridden by gateway if active payment methods for account can be determined.
*
* @since 1.3.0
* @return array|null
*/
public function get_available_payment_methods() {
return null;
}
/**
* Get the payment methods transient
*
* @since 1.3.0
* @param bool $update_active_methods Whether active payment methods option should be updated.
* @return array|null
*/
public function get_transient_available_payment_methods( $update_active_methods = true ) {
// Transient name.
$transient = 'pronamic_gateway_payment_methods_' . md5( serialize( $this ) );
$methods = get_transient( $transient );
if ( is_wp_error( $methods ) || false === $methods ) {
$methods = $this->get_available_payment_methods();
if ( is_array( $methods ) ) {
set_transient( $transient, $methods, DAY_IN_SECONDS );
if ( $update_active_methods ) {
PaymentMethods::update_active_payment_methods();
}
}
}
if ( empty( $methods ) ) {
return null;
}
return $methods;
}
/**
* Is payment method required to start transaction?
*
* @since 1.3.0
* @return boolean True if payment method is required, false otherwise.
*/
public function payment_method_is_required() {
return false;
}
/**
* Get payment method field options.
*
* @param bool $other_first Flag to prepend the 'Other' / 'All available methods' option.
* @return array
*/
public function get_payment_method_field_options( $other_first = false ) {
$options = [];
$payment_methods = [];
try {
$available_methods = $this->get_transient_available_payment_methods();
if ( null !== $available_methods ) {
$payment_methods = \array_intersect( $available_methods, $this->get_supported_payment_methods() );
}
} catch ( \Exception $e ) {
$payment_methods = [];
}
// Use all supported payment methods as fallback.
if ( empty( $payment_methods ) ) {
$payment_methods = $this->get_supported_payment_methods();
}
// Set payment methods as options with name.
foreach ( $payment_methods as $payment_method ) {
$options[ $payment_method ] = PaymentMethods::get_name( $payment_method );
}
// Sort options by name.
natcasesort( $options );
// Add option to use all available payment methods.
if ( ! $this->payment_method_is_required() ) {
if ( $other_first ) {
$options = [ _x( 'All available methods', 'Payment method field', 'pronamic_ideal' ) ] + $options;
} else {
$options[] = _x( 'Other', 'Payment method field', 'pronamic_ideal' );
}
}
return $options;
}
/**
* Start transaction/payment
*
* @param Payment $payment The payment to start up at this gateway.
* @return void
*/
public function start( Payment $payment ) {
}
/**
* Handle subscription update.
*
* @param Payment $payment The payment to handle subscription update for.
* @return void
*/
public function update_subscription( Payment $payment ) {
}
/**
* Handle subscription cancellation.
*
* @param Subscription $subscription The subscription to handle cancellation for.
* @return void
*/
public function cancel_subscription( Subscription $subscription ) {
}
/**
* Redirect to the gateway action URL.
*
* @param Payment $payment The payment to redirect for.
* @return void
* @throws \Exception Throws exception when action URL for HTTP redirect is empty.
*/
public function redirect( Payment $payment ) {
// Switch to user locale.
Util::switch_to_user_locale();
switch ( $this->method ) {
case self::METHOD_HTTP_REDIRECT:
$this->redirect_via_http( $payment );
break;
case self::METHOD_HTML_FORM:
$this->redirect_via_html( $payment );
break;
default:
// No idea how to redirect to the gateway.
}
}
/**
* Redirect via HTTP.
*
* @param Payment $payment The payment to redirect for.
* @return void
* @throws \Exception When payment action URL is empty.
*/
public function redirect_via_http( Payment $payment ) {
if ( headers_sent() ) {
$this->redirect_via_html( $payment );
}
$action_url = $payment->get_action_url();
if ( empty( $action_url ) ) {
throw new \Exception( 'Action URL is empty, can not redirect.' );
}
// Redirect, See Other.
// https://en.wikipedia.org/wiki/HTTP_303.
wp_redirect( $action_url, 303 );
exit;
}
/**
* Redirect via HTML.
*
* @param Payment $payment The payment to redirect for.
* @return void
*/
public function redirect_via_html( Payment $payment ) {
if ( headers_sent() ) {
/* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */
echo $this->get_form_html( $payment, true );
} else {
Core_Util::no_cache();
include Plugin::$dirname . '/views/redirect-via-html.php';
}
exit;
}
/**
* Get an issuer field
*
* @return array|null
*/
public function get_issuer_field() {
$field = null;
$payment_method = $this->get_payment_method();
// Set default payment method if needed.
if ( null === $payment_method && $this->payment_method_is_required() ) {
$payment_method = PaymentMethods::IDEAL;
}
// No issuers without payment method.
if ( empty( $payment_method ) ) {
return $field;
}
// Set issuer field for payment method.
switch ( $payment_method ) {
case PaymentMethods::IDEAL:
$issuers = $this->get_transient_issuers();
if ( ! empty( $issuers ) ) {
$field = [
'id' => 'pronamic_ideal_issuer_id',
'name' => 'pronamic_ideal_issuer_id',
'label' => __( 'Choose your bank', 'pronamic_ideal' ),
'type' => 'select',
'choices' => $issuers,
];
}
break;
case PaymentMethods::CREDIT_CARD:
$issuers = $this->get_credit_card_issuers();
if ( ! empty( $issuers ) ) {
$field = [
'id' => 'pronamic_credit_card_issuer_id',
'name' => 'pronamic_credit_card_issuer_id',
'label' => __( 'Choose your credit card issuer', 'pronamic_ideal' ),
'type' => 'select',
'choices' => $issuers,
];
}
break;
}
return $field;
}
/**
* Get a gender field.
*
* @return array|null
*/
public function get_gender_field() {
$payment_methods = [
PaymentMethods::AFTERPAY_NL,
PaymentMethods::FOCUM,
PaymentMethods::IN3,
PaymentMethods::KLARNA_PAY_LATER,
PaymentMethods::SPRAYPAY,
];
$payment_method = $this->get_payment_method();
// Only add field for specified payment methods.
if ( ! in_array( $payment_method, $payment_methods, true ) ) {
return null;
}
// Return field.
return [
'id' => 'pronamic_pay_gender',
'name' => 'pronamic_pay_gender',
'label' => __( 'Gender', 'pronamic_ideal' ),
'type' => 'select',
'choices' => [
[
'options' => [
'' => __( '— Select gender —', 'pronamic_ideal' ),
'F' => __( 'Female', 'pronamic_ideal' ),
'M' => __( 'Male', 'pronamic_ideal' ),
],
],
],
];
}
/**
* Get a date of birth field.
*
* @return array|null
*/
public function get_birth_date_field() {
$payment_methods = [
PaymentMethods::AFTERPAY_NL,
PaymentMethods::FOCUM,
PaymentMethods::IN3,
PaymentMethods::KLARNA_PAY_LATER,
PaymentMethods::SPRAYPAY,
];
$payment_method = $this->get_payment_method();
// Only add field for specified payment methods.
if ( ! in_array( $payment_method, $payment_methods, true ) ) {
return null;
}
// Return field.
return [
'id' => 'pronamic_pay_birth_date',
'name' => 'pronamic_pay_birth_date',
'label' => __( 'Date of birth', 'pronamic_ideal' ),
'type' => 'date',
'max' => gmdate( 'Y-m-d' ),
];
}
/**
* Get a consumer bank details name field.
*
* @return array|null
*/
public function get_consumer_bank_details_name_field() {
$payment_methods = [
PaymentMethods::DIRECT_DEBIT,
];
$payment_method = $this->get_payment_method();
// Only add field for specified payment methods.
if ( ! in_array( $payment_method, $payment_methods, true ) ) {
return null;
}
// Return field.
return [
'id' => 'pronamic_pay_consumer_bank_details_name',
'name' => 'pronamic_pay_consumer_bank_details_name',
'label' => __( 'Account holder name', 'pronamic_ideal' ),
'type' => 'text',
];
}
/**
* Get a consumer bank details IBAN field.
*
* @return array|null
*/
public function get_consumer_bank_details_iban_field() {
$payment_methods = [
PaymentMethods::DIRECT_DEBIT,
];
$payment_method = $this->get_payment_method();
// Only add field for specified payment methods.
if ( ! in_array( $payment_method, $payment_methods, true ) ) {
return null;
}
// Return field.
return [
'id' => 'pronamic_pay_consumer_bank_details_iban',
'name' => 'pronamic_pay_consumer_bank_details_iban',
'label' => __( 'IBAN', 'pronamic_ideal' ),
'type' => 'text',
];
}
/**
* Get the payment method to use on this gateway.
*
* @since 1.2.3
* @return string|null One of the PaymentMethods constants.
*/
public function get_payment_method() {
return $this->payment_method;
}
/**
* Set the payment method to use on this gateway.
*
* @since 1.2.3
*
* @param string|null $payment_method One of the PaymentMethods constants.
* @return void
*/
public function set_payment_method( $payment_method ) {
$this->payment_method = $payment_method;
}
/**
* Get the input fields for this gateway
*
* This function will automatically add the issuer field to the
* input fields array of it's not empty
*
* @return array
*/
public function get_input_fields() {
// Make sure to use input for first payment method.
$payment_method = $this->get_payment_method();
$first_payment_method = PaymentMethods::get_first_payment_method( $payment_method );
$this->set_payment_method( $first_payment_method );
/*
* Fields.
*/
$fields = [];
// Issuer field.
$fields[] = $this->get_issuer_field();
// Gender field.
$fields[] = $this->get_gender_field();
// Birth date field.
$fields[] = $this->get_birth_date_field();
// Consumer bank details name field.
$fields[] = $this->get_consumer_bank_details_name_field();
// Consumer bank details IBAN field.
$fields[] = $this->get_consumer_bank_details_iban_field();
// Remove empty input fields.
$fields = array_filter( $fields );
// Restore payment method to original value.
$this->set_payment_method( $payment_method );
return $fields;
}
/**
* Get the input HTML
*
* This function will convert all input fields to an HTML notation
*
* @return string
*/
public function get_input_html() {
$fields = $this->get_input_fields();
return Util::input_fields_html( $fields );
}
/**
* Get form HTML.
*
* @param Payment $payment Payment to get form HTML for.
* @param bool $auto_submit Flag to auto submit.
* @return string
* @throws \Exception When payment action URL is empty.
*/
public function get_form_html( Payment $payment, $auto_submit = false ) {
$form_inner = $this->get_output_html( $payment );
$form_inner .= sprintf(
'<input class="pronamic-pay-btn" type="submit" name="pay" value="%s" />',
__( 'Pay', 'pronamic_ideal' )
);
$action_url = $payment->get_action_url();
if ( empty( $action_url ) ) {
throw new \Exception( 'Action URL is empty, can not get form HTML.' );
}
$html = sprintf(
'<form id="pronamic_ideal_form" name="pronamic_ideal_form" method="post" action="%s">%s</form>',
esc_attr( $action_url ),
$form_inner
);
if ( $auto_submit ) {
$html .= '<script type="text/javascript">document.pronamic_ideal_form.submit();</script>';
}
return $html;
}
/**
* Get output inputs.
*
* @param Payment $payment Payment.
*
* @return array
* @since 1.2.0
*/
public function get_output_fields( Payment $payment ) {
return [];
}
/**
* Get the output HTML
*
* @param Payment $payment Payment.
*
* @return string
*/
public function get_output_html( Payment $payment ) {
$fields = $this->get_output_fields( $payment );
return PayUtil::html_hidden_fields( $fields );
}
/**
* Update status of the specified payment
*
* @param Payment $payment Payment.
* @return void
*/
public function update_status( Payment $payment ) {
}
/**
* Create invoice.
*
* @param Payment $payment Payment.
* @return bool
*/
public function create_invoice( $payment ) {
return false;
}
/**
* Cancel reservation.
*
* @param Payment $payment Payment.
* @return bool
*/
public function cancel_reservation( $payment ) {
return false;
}
}