1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* WooCommerce Payment Gateway class. |
9
|
|
|
* |
10
|
|
|
* Extended by individual payment gateways to handle payments. |
11
|
|
|
* |
12
|
|
|
* @class WC_Payment_Gateway |
13
|
|
|
* @extends WC_Settings_API |
14
|
|
|
* @version 2.1.0 |
15
|
|
|
* @package WooCommerce/Abstracts |
16
|
|
|
* @category Abstract Class |
17
|
|
|
* @author WooThemes |
18
|
|
|
*/ |
19
|
|
|
abstract class WC_Payment_Gateway extends WC_Settings_API { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set if the place order button should be renamed on selection. |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $order_button_text; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* yes or no based on whether the method is enabled. |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $enabled = 'yes'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Payment method title for the frontend. |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $title; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Payment method description for the frontend. |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $description; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Chosen payment method id. |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public $chosen; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gateway title. |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public $method_title = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gateway description. |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
public $method_description = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* True if the gateway shows fields on the checkout. |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
public $has_fields; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Countries this gateway is allowed for. |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
public $countries; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Available for all counties or specific. |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
public $availability; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Icon for the gateway. |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
public $icon; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Supported features such as 'default_credit_card_form', 'refunds'. |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
public $supports = array( 'products' ); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Maximum transaction amount, zero does not define a maximum. |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
public $max_amount = 0; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Optional URL to view a transaction. |
101
|
|
|
* @var string |
102
|
|
|
*/ |
103
|
|
|
public $view_transaction_url = ''; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the title for admin screens. |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function get_method_title() { |
110
|
|
|
return apply_filters( 'woocommerce_gateway_method_title', $this->method_title, $this ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the description for admin screens. |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function get_method_description() { |
118
|
|
|
return apply_filters( 'woocommerce_gateway_method_description', $this->method_description, $this ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Output the gateway settings screen. |
123
|
|
|
*/ |
124
|
|
|
public function admin_options() { |
125
|
|
|
echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>'; |
126
|
|
|
echo wp_kses_post( wpautop( $this->get_method_description() ) ); |
127
|
|
|
parent::admin_options(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Init settings for gateways. |
132
|
|
|
*/ |
133
|
|
|
public function init_settings() { |
134
|
|
|
parent::init_settings(); |
135
|
|
|
$this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the return url (thank you page). |
140
|
|
|
* |
141
|
|
|
* @param WC_Order $order |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function get_return_url( $order = null ) { |
145
|
|
|
if ( $order ) { |
146
|
|
|
$return_url = $order->get_checkout_order_received_url(); |
147
|
|
|
} else { |
148
|
|
|
$return_url = wc_get_endpoint_url( 'order-received', '', wc_get_page_permalink( 'checkout' ) ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' ) { |
|
|
|
|
152
|
|
|
$return_url = str_replace( 'http:', 'https:', $return_url ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return apply_filters( 'woocommerce_get_return_url', $return_url, $order ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get a link to the transaction on the 3rd party gateway size (if applicable). |
160
|
|
|
* |
161
|
|
|
* @param WC_Order $order the order object. |
162
|
|
|
* @return string transaction URL, or empty string. |
163
|
|
|
*/ |
164
|
|
|
public function get_transaction_url( $order ) { |
165
|
|
|
|
166
|
|
|
$return_url = ''; |
167
|
|
|
$transaction_id = $order->get_transaction_id(); |
168
|
|
|
|
169
|
|
|
if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
170
|
|
|
$return_url = sprintf( $this->view_transaction_url, $transaction_id ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return apply_filters( 'woocommerce_get_transaction_url', $return_url, $order, $this ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the order total in checkout and pay_for_order. |
178
|
|
|
* |
179
|
|
|
* @return float |
180
|
|
|
*/ |
181
|
|
|
protected function get_order_total() { |
182
|
|
|
|
183
|
|
|
$total = 0; |
184
|
|
|
$order_id = absint( get_query_var( 'order-pay' ) ); |
185
|
|
|
|
186
|
|
|
// Gets order total from "pay for order" page. |
187
|
|
|
if ( 0 < $order_id ) { |
188
|
|
|
$order = wc_get_order( $order_id ); |
189
|
|
|
$total = (float) $order->get_total(); |
190
|
|
|
|
191
|
|
|
// Gets order total from cart/checkout. |
192
|
|
|
} elseif ( 0 < WC()->cart->total ) { |
193
|
|
|
$total = (float) WC()->cart->total; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $total; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Check if the gateway is available for use. |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function is_available() { |
205
|
|
|
$is_available = ( 'yes' === $this->enabled ); |
206
|
|
|
|
207
|
|
|
if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) { |
208
|
|
|
$is_available = false; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $is_available; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Check if the gateway has fields on the checkout. |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function has_fields() { |
220
|
|
|
return $this->has_fields ? true : false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return the gateway's title. |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function get_title() { |
229
|
|
|
return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Return the gateway's description. |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function get_description() { |
238
|
|
|
return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Return the gateway's icon. |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function get_icon() { |
247
|
|
|
|
248
|
|
|
$icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : ''; |
249
|
|
|
|
250
|
|
|
return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Set as current gateway. |
255
|
|
|
* |
256
|
|
|
* Set this as the current gateway. |
257
|
|
|
*/ |
258
|
|
|
public function set_current() { |
259
|
|
|
$this->chosen = true; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Process Payment. |
264
|
|
|
* |
265
|
|
|
* Process the payment. Override this in your gateway. When implemented, this should. |
266
|
|
|
* return the success and redirect in an array. e.g: |
267
|
|
|
* |
268
|
|
|
* return array( |
269
|
|
|
* 'result' => 'success', |
270
|
|
|
* 'redirect' => $this->get_return_url( $order ) |
271
|
|
|
* ); |
272
|
|
|
* |
273
|
|
|
* @param int $order_id |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
public function process_payment( $order_id ) { |
277
|
|
|
return array(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Process refund. |
282
|
|
|
* |
283
|
|
|
* If the gateway declares 'refunds' support, this will allow it to refund. |
284
|
|
|
* a passed in amount. |
285
|
|
|
* |
286
|
|
|
* @param int $order_id |
287
|
|
|
* @param float $amount |
288
|
|
|
* @param string $reason |
289
|
|
|
* @return bool|WP_Error True or false based on success, or a WP_Error object. |
290
|
|
|
*/ |
291
|
|
|
public function process_refund( $order_id, $amount = null, $reason = '' ) { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Validate frontend fields. |
297
|
|
|
* |
298
|
|
|
* Validate payment fields on the frontend. |
299
|
|
|
* |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
public function validate_fields() { return true; } |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* If There are no payment fields show the description if set. |
306
|
|
|
* Override this in your gateway if you have some. |
307
|
|
|
*/ |
308
|
|
|
public function payment_fields() { |
309
|
|
|
|
310
|
|
|
if ( $description = $this->get_description() ) { |
311
|
|
|
echo wpautop( wptexturize( $description ) ); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
if ( $this->supports( 'default_credit_card_form' ) ) { |
315
|
|
|
$this->credit_card_form(); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Check if a gateway supports a given feature. |
321
|
|
|
* |
322
|
|
|
* Gateways should override this to declare support (or lack of support) for a feature. |
323
|
|
|
* For backward compatibility, gateways support 'products' by default, but nothing else. |
324
|
|
|
* |
325
|
|
|
* @param string $feature string The name of a feature to test support for. |
326
|
|
|
* @return bool True if the gateway supports the feature, false otherwise. |
327
|
|
|
* @since 1.5.7 |
328
|
|
|
*/ |
329
|
|
|
public function supports( $feature ) { |
330
|
|
|
return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Core credit card form which gateways can used if needed. |
335
|
|
|
* |
336
|
|
|
* @param array $args |
337
|
|
|
* @param array $fields |
338
|
|
|
*/ |
339
|
|
|
public function credit_card_form( $args = array(), $fields = array() ) { |
340
|
|
|
|
341
|
|
|
wp_enqueue_script( 'wc-credit-card-form' ); |
342
|
|
|
|
343
|
|
|
$default_args = array( |
344
|
|
|
'fields_have_names' => true, // Some gateways like stripe don't need names as the form is tokenized. |
345
|
|
|
); |
346
|
|
|
|
347
|
|
|
$args = wp_parse_args( $args, apply_filters( 'woocommerce_credit_card_form_args', $default_args, $this->id ) ); |
348
|
|
|
|
349
|
|
|
$default_fields = array( |
350
|
|
|
'card-number-field' => '<p class="form-row form-row-wide"> |
351
|
|
|
<label for="' . esc_attr( $this->id ) . '-card-number">' . __( 'Card Number', 'woocommerce' ) . ' <span class="required">*</span></label> |
352
|
|
|
<input id="' . esc_attr( $this->id ) . '-card-number" class="input-text wc-credit-card-form-card-number" type="text" maxlength="20" autocomplete="off" placeholder="•••• •••• •••• ••••" name="' . ( $args['fields_have_names'] ? $this->id . '-card-number' : '' ) . '" /> |
353
|
|
|
</p>', |
354
|
|
|
'card-expiry-field' => '<p class="form-row form-row-first"> |
355
|
|
|
<label for="' . esc_attr( $this->id ) . '-card-expiry">' . __( 'Expiry (MM/YY)', 'woocommerce' ) . ' <span class="required">*</span></label> |
356
|
|
|
<input id="' . esc_attr( $this->id ) . '-card-expiry" class="input-text wc-credit-card-form-card-expiry" type="text" autocomplete="off" placeholder="' . esc_attr__( 'MM / YY', 'woocommerce' ) . '" name="' . ( $args['fields_have_names'] ? $this->id . '-card-expiry' : '' ) . '" /> |
357
|
|
|
</p>', |
358
|
|
|
'card-cvc-field' => '<p class="form-row form-row-last"> |
359
|
|
|
<label for="' . esc_attr( $this->id ) . '-card-cvc">' . __( 'Card Code', 'woocommerce' ) . ' <span class="required">*</span></label> |
360
|
|
|
<input id="' . esc_attr( $this->id ) . '-card-cvc" class="input-text wc-credit-card-form-card-cvc" type="text" autocomplete="off" placeholder="' . esc_attr__( 'CVC', 'woocommerce' ) . '" name="' . ( $args['fields_have_names'] ? $this->id . '-card-cvc' : '' ) . '" /> |
361
|
|
|
</p>' |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
$fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) ); |
365
|
|
|
?> |
366
|
|
|
<fieldset id="<?php echo $this->id; ?>-cc-form"> |
367
|
|
|
<?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?> |
368
|
|
|
<?php |
369
|
|
|
foreach ( $fields as $field ) { |
370
|
|
|
echo $field; |
371
|
|
|
} |
372
|
|
|
?> |
373
|
|
|
<?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?> |
374
|
|
|
<div class="clear"></div> |
375
|
|
|
</fieldset> |
376
|
|
|
<?php |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.