Completed
Push — master ( 15aa29...17da96 )
by Claudio
18:39 queued 11s
created

includes/abstracts/abstract-wc-payment-gateway.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Abstract payment gateway
4
 *
5
 * Hanldes generic payment gateway functionality which is extended by idividual payment gateways.
6
 *
7
 * @class WC_Payment_Gateway
8
 * @version 2.1.0
9
 * @package WooCommerce/Abstracts
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * WooCommerce Payment Gateway class.
18
 *
19
 * Extended by individual payment gateways to handle payments.
20
 *
21
 * @class       WC_Payment_Gateway
22
 * @extends     WC_Settings_API
23
 * @version     2.1.0
24
 * @package     WooCommerce/Abstracts
25
 */
26
abstract class WC_Payment_Gateway extends WC_Settings_API {
27
28
	/**
29
	 * Set if the place order button should be renamed on selection.
30
	 *
31
	 * @var string
32
	 */
33
	public $order_button_text;
34
35
	/**
36
	 * Yes or no based on whether the method is enabled.
37
	 *
38
	 * @var string
39
	 */
40
	public $enabled = 'yes';
41
42
	/**
43
	 * Payment method title for the frontend.
44
	 *
45
	 * @var string
46
	 */
47
	public $title;
48
49
	/**
50
	 * Payment method description for the frontend.
51
	 *
52
	 * @var string
53
	 */
54
	public $description;
55
56
	/**
57
	 * Chosen payment method id.
58
	 *
59
	 * @var bool
60
	 */
61
	public $chosen;
62
63
	/**
64
	 * Gateway title.
65
	 *
66
	 * @var string
67
	 */
68
	public $method_title = '';
69
70
	/**
71
	 * Gateway description.
72
	 *
73
	 * @var string
74
	 */
75
	public $method_description = '';
76
77
	/**
78
	 * True if the gateway shows fields on the checkout.
79
	 *
80
	 * @var bool
81
	 */
82
	public $has_fields;
83
84
	/**
85
	 * Countries this gateway is allowed for.
86
	 *
87
	 * @var array
88
	 */
89
	public $countries;
90
91
	/**
92
	 * Available for all counties or specific.
93
	 *
94
	 * @var string
95
	 */
96
	public $availability;
97
98
	/**
99
	 * Icon for the gateway.
100
	 *
101
	 * @var string
102
	 */
103
	public $icon;
104
105
	/**
106
	 * Supported features such as 'default_credit_card_form', 'refunds'.
107
	 *
108
	 * @var array
109
	 */
110
	public $supports = array( 'products' );
111
112
	/**
113
	 * Maximum transaction amount, zero does not define a maximum.
114
	 *
115
	 * @var int
116
	 */
117
	public $max_amount = 0;
118
119
	/**
120
	 * Optional URL to view a transaction.
121
	 *
122
	 * @var string
123
	 */
124
	public $view_transaction_url = '';
125
126
	/**
127
	 * Optional label to show for "new payment method" in the payment
128
	 * method/token selection radio selection.
129
	 *
130
	 * @var string
131
	 */
132
	public $new_method_label = '';
133
134
	/**
135
	 * Contains a users saved tokens for this gateway.
136
	 *
137
	 * @var array
138
	 */
139
	protected $tokens = array();
140
141
	/**
142
	 * Returns a users saved tokens for this gateway.
143
	 *
144
	 * @since 2.6.0
145
	 * @return array
146
	 */
147
	public function get_tokens() {
148
		if ( count( $this->tokens ) > 0 ) {
149
			return $this->tokens;
150
		}
151
152
		if ( is_user_logged_in() && $this->supports( 'tokenization' ) ) {
153
			$this->tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), $this->id );
154
		}
155
156
		return $this->tokens;
157
	}
158
159
	/**
160
	 * Return the title for admin screens.
161
	 *
162
	 * @return string
163
	 */
164 6
	public function get_method_title() {
165 6
		return apply_filters( 'woocommerce_gateway_method_title', $this->method_title, $this );
166
	}
167
168
	/**
169
	 * Return the description for admin screens.
170
	 *
171
	 * @return string
172
	 */
173 6
	public function get_method_description() {
174 6
		return apply_filters( 'woocommerce_gateway_method_description', $this->method_description, $this );
175
	}
176
177
	/**
178
	 * Output the gateway settings screen.
179
	 */
180
	public function admin_options() {
181
		echo '<h2>' . esc_html( $this->get_method_title() );
182
		wc_back_link( __( 'Return to payments', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) );
183
		echo '</h2>';
184
		echo wp_kses_post( wpautop( $this->get_method_description() ) );
185
		parent::admin_options();
186
	}
187
188
	/**
189
	 * Init settings for gateways.
190
	 */
191 430
	public function init_settings() {
192 430
		parent::init_settings();
193 430
		$this->enabled  = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
194
	}
195
196
	/**
197
	 * Return whether or not this gateway still requires setup to function.
198
	 *
199
	 * When this gateway is toggled on via AJAX, if this returns true a
200
	 * redirect will occur to the settings page instead.
201
	 *
202
	 * @since 3.4.0
203
	 * @return bool
204
	 */
205
	public function needs_setup() {
206
		return false;
207
	}
208
209
	/**
210
	 * Get the return url (thank you page).
211
	 *
212
	 * @param WC_Order $order Order object.
213
	 * @return string
214
	 */
215
	public function get_return_url( $order = null ) {
216
		if ( $order ) {
217
			$return_url = $order->get_checkout_order_received_url();
218
		} else {
219
			$return_url = wc_get_endpoint_url( 'order-received', '', wc_get_page_permalink( 'checkout' ) );
220
		}
221
222 View Code Duplication
		if ( is_ssl() || get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) {
0 ignored issues
show
Found "== '". Use Yoda Condition checks, you must
Loading history...
223
			$return_url = str_replace( 'http:', 'https:', $return_url );
224
		}
225
226
		return apply_filters( 'woocommerce_get_return_url', $return_url, $order );
227
	}
228
229
	/**
230
	 * Get a link to the transaction on the 3rd party gateway size (if applicable).
231
	 *
232
	 * @param  WC_Order $order the order object.
233
	 * @return string transaction URL, or empty string.
234
	 */
235
	public function get_transaction_url( $order ) {
236
237
		$return_url = '';
238
		$transaction_id = $order->get_transaction_id();
239
240
		if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) {
241
			$return_url = sprintf( $this->view_transaction_url, $transaction_id );
242
		}
243
244
		return apply_filters( 'woocommerce_get_transaction_url', $return_url, $order, $this );
245
	}
246
247
	/**
248
	 * Get the order total in checkout and pay_for_order.
249
	 *
250
	 * @return float
251
	 */
252 1
	protected function get_order_total() {
253
254 1
		$total = 0;
255 1
		$order_id = absint( get_query_var( 'order-pay' ) );
256
257
		// Gets order total from "pay for order" page.
258 1
		if ( 0 < $order_id ) {
259
			$order = wc_get_order( $order_id );
260
			$total = (float) $order->get_total();
261
262
			// Gets order total from cart/checkout.
263 1
		} elseif ( 0 < WC()->cart->total ) {
264
			$total = (float) WC()->cart->total;
265
		}
266
267 1
		return $total;
268
	}
269
270
	/**
271
	 * Check if the gateway is available for use.
272
	 *
273
	 * @return bool
274
	 */
275 1
	public function is_available() {
276 1
		$is_available = ( 'yes' === $this->enabled );
277
278 1
		if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) {
279
			$is_available = false;
280
		}
281
282 1
		return $is_available;
283
	}
284
285
	/**
286
	 * Check if the gateway has fields on the checkout.
287
	 *
288
	 * @return bool
289
	 */
290
	public function has_fields() {
291
		return (bool) $this->has_fields;
292
	}
293
294
	/**
295
	 * Return the gateway's title.
296
	 *
297
	 * @return string
298
	 */
299 86
	public function get_title() {
300 86
		return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id );
301
	}
302
303
	/**
304
	 * Return the gateway's description.
305
	 *
306
	 * @return string
307
	 */
308
	public function get_description() {
309
		return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id );
310
	}
311
312
	/**
313
	 * Return the gateway's icon.
314
	 *
315
	 * @return string
316
	 */
317
	public function get_icon() {
318
319
		$icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
320
321
		return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
322
	}
323
324
	/**
325
	 * Set as current gateway.
326
	 *
327
	 * Set this as the current gateway.
328
	 */
329
	public function set_current() {
330
		$this->chosen = true;
331
	}
332
333
	/**
334
	 * Process Payment.
335
	 *
336
	 * Process the payment. Override this in your gateway. When implemented, this should.
337
	 * return the success and redirect in an array. e.g:
338
	 *
339
	 *        return array(
340
	 *            'result'   => 'success',
341
	 *            'redirect' => $this->get_return_url( $order )
342
	 *        );
343
	 *
344
	 * @param int $order_id Order ID.
345
	 * @return array
346
	 */
347
	public function process_payment( $order_id ) {
348
		return array();
349
	}
350
351
	/**
352
	 * Process refund.
353
	 *
354
	 * If the gateway declares 'refunds' support, this will allow it to refund.
355
	 * a passed in amount.
356
	 *
357
	 * @param  int    $order_id Order ID.
358
	 * @param  float  $amount Refund amount.
359
	 * @param  string $reason Refund reason.
360
	 * @return boolean True or false based on success, or a WP_Error object.
361
	 */
362
	public function process_refund( $order_id, $amount = null, $reason = '' ) {
363
		return false;
364
	}
365
366
	/**
367
	 * Validate frontend fields.
368
	 *
369
	 * Validate payment fields on the frontend.
370
	 *
371
	 * @return bool
372
	 */
373
	public function validate_fields() {
374
		return true;
375
	}
376
377
	/**
378
	 * If There are no payment fields show the description if set.
379
	 * Override this in your gateway if you have some.
380
	 */
381
	public function payment_fields() {
382
		$description = $this->get_description();
383
		if ( $description ) {
384
			echo wpautop( wptexturize( $description ) ); // @codingStandardsIgnoreLine.
385
		}
386
387
		if ( $this->supports( 'default_credit_card_form' ) ) {
388
			$this->credit_card_form(); // Deprecated, will be removed in a future version.
389
		}
390
	}
391
392
	/**
393
	 * Check if a gateway supports a given feature.
394
	 *
395
	 * Gateways should override this to declare support (or lack of support) for a feature.
396
	 * For backward compatibility, gateways support 'products' by default, but nothing else.
397
	 *
398
	 * @param string $feature string The name of a feature to test support for.
399
	 * @return bool True if the gateway supports the feature, false otherwise.
400
	 * @since 1.5.7
401
	 */
402 2
	public function supports( $feature ) {
403 2
		return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this );
404
	}
405
406
	/**
407
	 * Can the order be refunded via this gateway?
408
	 *
409
	 * Should be extended by gateways to do their own checks.
410
	 *
411
	 * @param  WC_Order $order Order object.
412
	 * @return bool If false, the automatic refund button is hidden in the UI.
413
	 */
414 1
	public function can_refund_order( $order ) {
415 1
		return $order && $this->supports( 'refunds' );
416
	}
417
418
	/**
419
	 * Core credit card form which gateways can used if needed. Deprecated - inherit WC_Payment_Gateway_CC instead.
420
	 *
421
	 * @param  array $args Arguments.
422
	 * @param  array $fields Fields.
423
	 */
424
	public function credit_card_form( $args = array(), $fields = array() ) {
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
425
		wc_deprecated_function( 'credit_card_form', '2.6', 'WC_Payment_Gateway_CC->form' );
426
		$cc_form           = new WC_Payment_Gateway_CC();
427
		$cc_form->id       = $this->id;
428
		$cc_form->supports = $this->supports;
429
		$cc_form->form();
430
	}
431
432
	/**
433
	 * Enqueues our tokenization script to handle some of the new form options.
434
	 *
435
	 * @since 2.6.0
436
	 */
437
	public function tokenization_script() {
438
		wp_enqueue_script(
439
			'woocommerce-tokenization-form',
440
			plugins_url( '/assets/js/frontend/tokenization-form' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', WC_PLUGIN_FILE ),
441
			array( 'jquery' ),
442
			WC()->version
443
		);
444
445
		wp_localize_script(
446
			'woocommerce-tokenization-form', 'wc_tokenization_form_params', array(
447
				'is_registration_required' => WC()->checkout()->is_registration_required(),
448
				'is_logged_in'             => is_user_logged_in(),
449
			)
450
		);
451
	}
452
453
	/**
454
	 * Grab and display our saved payment methods.
455
	 *
456
	 * @since 2.6.0
457
	 */
458
	public function saved_payment_methods() {
459
		$html = '<ul class="woocommerce-SavedPaymentMethods wc-saved-payment-methods" data-count="' . esc_attr( count( $this->get_tokens() ) ) . '">';
460
461
		foreach ( $this->get_tokens() as $token ) {
462
			$html .= $this->get_saved_payment_method_option_html( $token );
463
		}
464
465
		$html .= $this->get_new_payment_method_option_html();
466
		$html .= '</ul>';
467
468
		echo apply_filters( 'wc_payment_gateway_form_saved_payment_methods_html', $html, $this ); // @codingStandardsIgnoreLine
469
	}
470
471
	/**
472
	 * Gets saved payment method HTML from a token.
473
	 *
474
	 * @since 2.6.0
475
	 * @param  WC_Payment_Token $token Payment Token.
476
	 * @return string Generated payment method HTML
477
	 */
478
	public function get_saved_payment_method_option_html( $token ) {
479
		$html = sprintf(
480
			'<li class="woocommerce-SavedPaymentMethods-token">
481
				<input id="wc-%1$s-payment-token-%2$s" type="radio" name="wc-%1$s-payment-token" value="%2$s" style="width:auto;" class="woocommerce-SavedPaymentMethods-tokenInput" %4$s />
482
				<label for="wc-%1$s-payment-token-%2$s">%3$s</label>
483
			</li>',
484
			esc_attr( $this->id ),
485
			esc_attr( $token->get_id() ),
486
			esc_html( $token->get_display_name() ),
487
			checked( $token->is_default(), true, false )
488
		);
489
490
		return apply_filters( 'woocommerce_payment_gateway_get_saved_payment_method_option_html', $html, $token, $this );
491
	}
492
493
	/**
494
	 * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method.
495
	 * Only displayed when a gateway supports tokenization.
496
	 *
497
	 * @since 2.6.0
498
	 */
499
	public function get_new_payment_method_option_html() {
500
		$label = apply_filters( 'woocommerce_payment_gateway_get_new_payment_method_option_html_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'woocommerce' ), $this );
501
		$html  = sprintf(
502
			'<li class="woocommerce-SavedPaymentMethods-new">
503
				<input id="wc-%1$s-payment-token-new" type="radio" name="wc-%1$s-payment-token" value="new" style="width:auto;" class="woocommerce-SavedPaymentMethods-tokenInput" />
504
				<label for="wc-%1$s-payment-token-new">%2$s</label>
505
			</li>',
506
			esc_attr( $this->id ),
507
			esc_html( $label )
508
		);
509
510
		return apply_filters( 'woocommerce_payment_gateway_get_new_payment_method_option_html', $html, $this );
511
	}
512
513
	/**
514
	 * Outputs a checkbox for saving a new payment method to the database.
515
	 *
516
	 * @since 2.6.0
517
	 */
518
	public function save_payment_method_checkbox() {
519
		$html = sprintf(
520
			'<p class="form-row woocommerce-SavedPaymentMethods-saveNew">
521
				<input id="wc-%1$s-new-payment-method" name="wc-%1$s-new-payment-method" type="checkbox" value="true" style="width:auto;" />
522
				<label for="wc-%1$s-new-payment-method" style="display:inline;">%2$s</label>
523
			</p>',
524
			esc_attr( $this->id ),
525
			esc_html__( 'Save to account', 'woocommerce' )
526
		);
527
		
528
		echo apply_filters( 'woocommerce_payment_gateway_save_new_payment_method_option_html', $html, $this );
529
	}
530
531
	/**
532
	 * Add payment method via account screen. This should be extended by gateway plugins.
533
	 *
534
	 * @since 3.2.0 Included here from 3.2.0, but supported from 3.0.0.
535
	 * @return array
536
	 */
537
	public function add_payment_method() {
538
		return array(
539
			'result'   => 'failure',
540
			'redirect' => wc_get_endpoint_url( 'payment-methods' ),
541
		);
542
	}
543
}
544