-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGateway.php
409 lines (346 loc) · 8.38 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
<?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;
use ModeTrait;
/**
* 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.
*/
use SupportsTrait;
/**
* Payment methods.
*
* @var PaymentMethod[]
*/
private $payment_methods = [];
/**
* Construct gateway.
*/
public function __construct() {
}
/**
* Register payment method.
*
* @param PaymentMethod $payment_method Payment method.
* @return void
*/
protected function register_payment_method( PaymentMethod $payment_method ) {
$id = $payment_method->get_id();
$this->payment_methods[ $id ] = $payment_method;
}
/**
* Get payment method by ID.
*
* @param string $id ID
* @return PaymentMethod|null
*/
public function get_payment_method( $id ) {
if ( array_key_exists( $id, $this->payment_methods ) ) {
return $this->payment_methods[ $id ];
}
return null;
}
/**
* Get payment methods.
*
* @return PaymentMethod[]
*/
public function get_payment_methods() {
return $this->payment_methods;
}
/**
* 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;
}
/**
* Custom payment redirect.
* Intended to be overridden by gateway.
*
* @param Payment $payment Payment.
*
* @return void
*/
public function payment_redirect( Payment $payment ) {
}
/**
* 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
* @deprecated
*/
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
* @deprecated
*/
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;
}
/**
* 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 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;
}
}