Completed
Push — master ( c3ed6b...ba5b7d )
by Claudio
07:30
created

WC_Payment_Gateway::credit_card_form()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 8.439
cc 5
eloc 26
nc 16
nop 2
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
	 * Payment method title.
29
	 * @var string
30
	 */
31
	public $title;
32
33
	/**
34
	 * Chosen payment method id.
35
	 * @var bool
36
	 */
37
	public $chosen;
38
39
	/**
40
	 * True if the gateway shows fields on the checkout.
41
	 * @var bool
42
	 */
43
	public $has_fields;
44
45
	/**
46
	 * Countries this gateway is allowed for.
47
	 * @var array
48
	 */
49
	public $countries;
50
51
	/**
52
	 * Available for all counties or specific.
53
	 * @var string
54
	 */
55
	public $availability;
56
57
	/**
58
	 * Icon for the gateway.
59
	 * @var string
60
	 */
61
	public $icon;
62
63
	/**
64
	 * Description for the gateway.
65
	 * @var string
66
	 */
67
	public $description;
68
69
	/**
70
	 * Supported features such as 'default_credit_card_form', 'refunds'.
71
	 * @var array
72
	 */
73
	public $supports = array( 'products' );
74
75
	/**
76
	 * Maximum transaction amount, zero does not define a maximum.
77
	 * @var int
78
	 */
79
	public $max_amount = 0;
80
81
	/**
82
	 * Optional URL to view a transaction.
83
	 * @var string
84
	 */
85
	public $view_transaction_url = '';
86
87
	/**
88
	 * Get the return url (thank you page).
89
	 *
90
	 * @param WC_Order $order
91
	 * @return string
92
	 */
93
	public function get_return_url( $order = null ) {
94
95
		if ( $order ) {
96
			$return_url = $order->get_checkout_order_received_url();
97
		} else {
98
			$return_url = wc_get_endpoint_url( 'order-received', '', wc_get_page_permalink( 'checkout' ) );
99
		}
100
101 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...
102
			$return_url = str_replace( 'http:', 'https:', $return_url );
103
		}
104
105
		return apply_filters( 'woocommerce_get_return_url', $return_url, $order );
106
	}
107
108
	/**
109
	 * Get a link to the transaction on the 3rd party gateway size (if applicable).
110
	 *
111
	 * @param  WC_Order $order the order object.
112
	 * @return string transaction URL, or empty string.
113
	 */
114
	public function get_transaction_url( $order ) {
115
116
		$return_url = '';
117
		$transaction_id = $order->get_transaction_id();
118
119
		if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) {
120
			$return_url = sprintf( $this->view_transaction_url, $transaction_id );
121
		}
122
123
		return apply_filters( 'woocommerce_get_transaction_url', $return_url, $order, $this );
124
	}
125
126
	/**
127
	 * Get the order total in checkout and pay_for_order.
128
	 *
129
	 * @return float
130
	 */
131
	protected function get_order_total() {
132
133
		$total = 0;
134
		$order_id = absint( get_query_var( 'order-pay' ) );
135
136
		// Gets order total from "pay for order" page.
137
		if ( 0 < $order_id ) {
138
			$order = wc_get_order( $order_id );
139
			$total = (float) $order->get_total();
140
141
		// Gets order total from cart/checkout.
142
		} elseif ( 0 < WC()->cart->total ) {
143
			$total = (float) WC()->cart->total;
144
		}
145
146
		return $total;
147
	}
148
149
	/**
150
	 * Check if the gateway is available for use.
151
	 *
152
	 * @return bool
153
	 */
154
	public function is_available() {
155
156
		$is_available = ( 'yes' === $this->enabled );
157
158
		if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) {
159
			$is_available = false;
160
		}
161
162
		return $is_available;
163
	}
164
165
	/**
166
	 * Check if the gateway has fields on the checkout.
167
	 *
168
	 * @return bool
169
	 */
170
	public function has_fields() {
171
		return $this->has_fields ? true : false;
172
	}
173
174
	/**
175
	 * Return the gateway's title.
176
	 *
177
	 * @return string
178
	 */
179
	public function get_title() {
180
		return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id );
181
	}
182
183
	/**
184
	 * Return the gateway's description.
185
	 *
186
	 * @return string
187
	 */
188
	public function get_description() {
189
		return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id );
190
	}
191
192
	/**
193
	 * Return the gateway's icon.
194
	 *
195
	 * @return string
196
	 */
197
	public function get_icon() {
198
199
		$icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
200
201
		return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
202
	}
203
204
	/**
205
	 * Set as current gateway.
206
	 *
207
	 * Set this as the current gateway.
208
	 */
209
	public function set_current() {
210
		$this->chosen = true;
211
	}
212
213
	/**
214
	 * Process Payment.
215
	 *
216
	 * Process the payment. Override this in your gateway. When implemented, this should.
217
	 * return the success and redirect in an array. e.g:
218
	 *
219
	 *        return array(
220
	 *            'result'   => 'success',
221
	 *            'redirect' => $this->get_return_url( $order )
222
	 *        );
223
	 *
224
	 * @param int $order_id
225
	 * @return array
226
	 */
227
	public function process_payment( $order_id ) {
228
		return array();
229
	}
230
231
	/**
232
	 * Process refund.
233
	 *
234
	 * If the gateway declares 'refunds' support, this will allow it to refund.
235
	 * a passed in amount.
236
	 *
237
	 * @param  int $order_id
238
	 * @param  float $amount
239
	 * @param  string $reason
240
	 * @return bool|WP_Error True or false based on success, or a WP_Error object.
241
	 */
242
	public function process_refund( $order_id, $amount = null, $reason = '' ) {
243
		return false;
244
	}
245
246
	/**
247
	 * Validate frontend fields.
248
	 *
249
	 * Validate payment fields on the frontend.
250
	 *
251
	 * @return bool
252
	 */
253
	public function validate_fields() { return true; }
254
255
	/**
256
	 * If There are no payment fields show the description if set.
257
	 * Override this in your gateway if you have some.
258
	 */
259
	public function payment_fields() {
260
261
		if ( $description = $this->get_description() ) {
262
			echo wpautop( wptexturize( $description ) );
263
		}
264
265
		if ( $this->supports( 'default_credit_card_form' ) ) {
266
			$this->credit_card_form();
267
		}
268
	}
269
270
	/**
271
	 * Check if a gateway supports a given feature.
272
	 *
273
	 * Gateways should override this to declare support (or lack of support) for a feature.
274
	 * For backward compatibility, gateways support 'products' by default, but nothing else.
275
	 *
276
	 * @param string $feature string The name of a feature to test support for.
277
	 * @return bool True if the gateway supports the feature, false otherwise.
278
	 * @since 1.5.7
279
	 */
280
	public function supports( $feature ) {
281
		return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this );
282
	}
283
284
	/**
285
	 * Core credit card form which gateways can used if needed.
286
	 *
287
	 * @param  array $args
288
	 * @param  array $fields
289
	 */
290
	public function credit_card_form( $args = array(), $fields = array() ) {
291
292
		wp_enqueue_script( 'wc-credit-card-form' );
293
294
		$default_args = array(
295
			'fields_have_names' => true, // Some gateways like stripe don't need names as the form is tokenized.
296
		);
297
298
		$args = wp_parse_args( $args, apply_filters( 'woocommerce_credit_card_form_args', $default_args, $this->id ) );
299
300
		$default_fields = array(
301
			'card-number-field' => '<p class="form-row form-row-wide">
302
				<label for="' . esc_attr( $this->id ) . '-card-number">' . __( 'Card Number', 'woocommerce' ) . ' <span class="required">*</span></label>
303
				<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' : '' ) . '" />
304
			</p>',
305
			'card-expiry-field' => '<p class="form-row form-row-first">
306
				<label for="' . esc_attr( $this->id ) . '-card-expiry">' . __( 'Expiry (MM/YY)', 'woocommerce' ) . ' <span class="required">*</span></label>
307
				<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' : '' ) . '" />
308
			</p>',
309
			'card-cvc-field' => '<p class="form-row form-row-last">
310
				<label for="' . esc_attr( $this->id ) . '-card-cvc">' . __( 'Card Code', 'woocommerce' ) . ' <span class="required">*</span></label>
311
				<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' : '' ) . '" />
312
			</p>'
313
		);
314
315
		$fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) );
316
		?>
317
		<fieldset id="<?php echo $this->id; ?>-cc-form">
318
			<?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
319
			<?php
320
				foreach ( $fields as $field ) {
321
					echo $field;
322
				}
323
			?>
324
			<?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
325
			<div class="clear"></div>
326
		</fieldset>
327
		<?php
328
	}
329
}
330