Completed
Pull Request — master (#9826)
by Mike
10:01
created

WC_Payment_Gateway::get_icon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 4.

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.

Loading history...
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
	 * Get the return url (thank you page).
132
	 *
133
	 * @param WC_Order $order
134
	 * @return string
135
	 */
136
	public function get_return_url( $order = null ) {
137
		if ( $order ) {
138
			$return_url = $order->get_checkout_order_received_url();
139
		} else {
140
			$return_url = wc_get_endpoint_url( 'order-received', '', wc_get_page_permalink( 'checkout' ) );
141
		}
142
143 View Code Duplication
		if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
			$return_url = str_replace( 'http:', 'https:', $return_url );
145
		}
146
147
		return apply_filters( 'woocommerce_get_return_url', $return_url, $order );
148
	}
149
150
	/**
151
	 * Get a link to the transaction on the 3rd party gateway size (if applicable).
152
	 *
153
	 * @param  WC_Order $order the order object.
154
	 * @return string transaction URL, or empty string.
155
	 */
156
	public function get_transaction_url( $order ) {
157
158
		$return_url = '';
159
		$transaction_id = $order->get_transaction_id();
160
161
		if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) {
162
			$return_url = sprintf( $this->view_transaction_url, $transaction_id );
163
		}
164
165
		return apply_filters( 'woocommerce_get_transaction_url', $return_url, $order, $this );
166
	}
167
168
	/**
169
	 * Get the order total in checkout and pay_for_order.
170
	 *
171
	 * @return float
172
	 */
173
	protected function get_order_total() {
174
175
		$total = 0;
176
		$order_id = absint( get_query_var( 'order-pay' ) );
177
178
		// Gets order total from "pay for order" page.
179
		if ( 0 < $order_id ) {
180
			$order = wc_get_order( $order_id );
181
			$total = (float) $order->get_total();
182
183
		// Gets order total from cart/checkout.
184
		} elseif ( 0 < WC()->cart->total ) {
185
			$total = (float) WC()->cart->total;
186
		}
187
188
		return $total;
189
	}
190
191
	/**
192
	 * Check if the gateway is available for use.
193
	 *
194
	 * @return bool
195
	 */
196
	public function is_available() {
197
198
		$is_available = ( 'yes' === $this->enabled ) ? true : false;
199
200
		if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) {
201
			$is_available = false;
202
		}
203
204
		return $is_available;
205
	}
206
207
	/**
208
	 * has_fields function.
209
	 *
210
	 * @return bool
211
	 */
212
	public function has_fields() {
213
		return $this->has_fields ? true : false;
214
	}
215
216
	/**
217
	 * Return the gateway's title.
218
	 *
219
	 * @return string
220
	 */
221
	public function get_title() {
222
		return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id );
223
	}
224
225
	/**
226
	 * Return the gateway's description.
227
	 *
228
	 * @return string
229
	 */
230
	public function get_description() {
231
		return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id );
232
	}
233
234
	/**
235
	 * get_icon function.
236
	 *
237
	 * @return string
238
	 */
239
	public function get_icon() {
240
241
		$icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
242
243
		return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
244
	}
245
246
	/**
247
	 * Set as current gateway.
248
	 *
249
	 * Set this as the current gateway.
250
	 */
251
	public function set_current() {
252
		$this->chosen = true;
253
	}
254
255
	/**
256
	 * Process Payment.
257
	 *
258
	 * Process the payment. Override this in your gateway. When implemented, this should.
259
	 * return the success and redirect in an array. e.g:
260
	 *
261
	 *        return array(
262
	 *            'result'   => 'success',
263
	 *            'redirect' => $this->get_return_url( $order )
264
	 *        );
265
	 *
266
	 * @param int $order_id
267
	 * @return array
268
	 */
269
	public function process_payment( $order_id ) {
270
		return array();
271
	}
272
273
	/**
274
	 * Process refund.
275
	 *
276
	 * If the gateway declares 'refunds' support, this will allow it to refund.
277
	 * a passed in amount.
278
	 *
279
	 * @param  int $order_id
280
	 * @param  float $amount
281
	 * @param  string $reason
282
	 * @return bool|WP_Error True or false based on success, or a WP_Error object.
283
	 */
284
	public function process_refund( $order_id, $amount = null, $reason = '' ) {
285
		return false;
286
	}
287
288
	/**
289
	 * Validate frontend fields.
290
	 *
291
	 * Validate payment fields on the frontend.
292
	 */
293
	public function validate_fields() { return true; }
294
295
	/**
296
	 * If There are no payment fields show the description if set.
297
	 * Override this in your gateway if you have some.
298
	 */
299
	public function payment_fields() {
300
301
		if ( $description = $this->get_description() ) {
302
			echo wpautop( wptexturize( $description ) );
303
		}
304
305
		if ( $this->supports( 'default_credit_card_form' ) ) {
306
			$this->credit_card_form();
307
		}
308
	}
309
310
	/**
311
	 * Check if a gateway supports a given feature.
312
	 *
313
	 * Gateways should override this to declare support (or lack of support) for a feature.
314
	 * For backward compatibility, gateways support 'products' by default, but nothing else.
315
	 *
316
	 * @param string $feature string The name of a feature to test support for.
317
	 * @return bool True if the gateway supports the feature, false otherwise.
318
	 * @since 1.5.7
319
	 */
320
	public function supports( $feature ) {
321
		return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this );
322
	}
323
324
	/**
325
	 * Core credit card form which gateways can used if needed.
326
	 *
327
	 * @param  array $args
328
	 */
329
	public function credit_card_form( $args = array(), $fields = array() ) {
330
331
		wp_enqueue_script( 'wc-credit-card-form' );
332
333
		$default_args = array(
334
			'fields_have_names' => true, // Some gateways like stripe don't need names as the form is tokenized.
335
		);
336
337
		$args = wp_parse_args( $args, apply_filters( 'woocommerce_credit_card_form_args', $default_args, $this->id ) );
338
339
		$default_fields = array(
340
			'card-number-field' => '<p class="form-row form-row-wide">
341
				<label for="' . esc_attr( $this->id ) . '-card-number">' . __( 'Card Number', 'woocommerce' ) . ' <span class="required">*</span></label>
342
				<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' : '' ) . '" />
343
			</p>',
344
			'card-expiry-field' => '<p class="form-row form-row-first">
345
				<label for="' . esc_attr( $this->id ) . '-card-expiry">' . __( 'Expiry (MM/YY)', 'woocommerce' ) . ' <span class="required">*</span></label>
346
				<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' : '' ) . '" />
347
			</p>',
348
			'card-cvc-field' => '<p class="form-row form-row-last">
349
				<label for="' . esc_attr( $this->id ) . '-card-cvc">' . __( 'Card Code', 'woocommerce' ) . ' <span class="required">*</span></label>
350
				<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' : '' ) . '" />
351
			</p>'
352
		);
353
354
		$fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) );
355
		?>
356
		<fieldset id="<?php echo $this->id; ?>-cc-form">
357
			<?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
358
			<?php
359
				foreach ( $fields as $field ) {
360
					echo $field;
361
				}
362
			?>
363
			<?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
364
			<div class="clear"></div>
365
		</fieldset>
366
		<?php
367
	}
368
}
369