Completed
Push — master ( 919814...abaa50 )
by Roy
02:23
created
includes/class-wc-gateway-stripe.php 1 patch
Spacing   +282 added lines, -282 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) {
2
+if ( ! defined('ABSPATH')) {
3 3
 	exit;
4 4
 }
5 5
 
@@ -120,8 +120,8 @@  discard block
 block discarded – undo
120 120
 	 */
121 121
 	public function __construct() {
122 122
 		$this->id                   = 'stripe';
123
-		$this->method_title         = __( 'Stripe', 'woocommerce-gateway-stripe' );
124
-		$this->method_description   = __( 'Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe' );
123
+		$this->method_title         = __('Stripe', 'woocommerce-gateway-stripe');
124
+		$this->method_description   = __('Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe');
125 125
 		$this->has_fields           = true;
126 126
 		$this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s';
127 127
 		$this->supports             = array(
@@ -149,43 +149,43 @@  discard block
 block discarded – undo
149 149
 		$this->init_settings();
150 150
 
151 151
 		// Get setting values.
152
-		$this->title                   = $this->get_option( 'title' );
153
-		$this->description             = $this->get_option( 'description' );
154
-		$this->enabled                 = $this->get_option( 'enabled' );
155
-		$this->testmode                = 'yes' === $this->get_option( 'testmode' );
156
-		$this->capture                 = 'yes' === $this->get_option( 'capture', 'yes' );
157
-		$this->statement_descriptor    = $this->get_option( 'statement_descriptor', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
158
-		$this->stripe_checkout         = 'yes' === $this->get_option( 'stripe_checkout' );
159
-		$this->stripe_checkout_locale  = $this->get_option( 'stripe_checkout_locale' );
160
-		$this->stripe_checkout_image   = $this->get_option( 'stripe_checkout_image', '' );
161
-		$this->saved_cards             = 'yes' === $this->get_option( 'saved_cards' );
162
-		$this->secret_key              = $this->testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' );
163
-		$this->publishable_key         = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' );
164
-		$this->bitcoin                 = 'USD' === strtoupper( get_woocommerce_currency() ) && 'yes' === $this->get_option( 'stripe_bitcoin' );
165
-		$this->apple_pay               = 'yes' === $this->get_option( 'apple_pay', 'yes' );
166
-		$this->apple_pay_domain_set    = 'yes' === $this->get_option( 'apple_pay_domain_set', 'no' );
167
-		$this->apple_pay_button        = $this->get_option( 'apple_pay_button', 'black' );
168
-		$this->logging                 = 'yes' === $this->get_option( 'logging' );
152
+		$this->title                   = $this->get_option('title');
153
+		$this->description             = $this->get_option('description');
154
+		$this->enabled                 = $this->get_option('enabled');
155
+		$this->testmode                = 'yes' === $this->get_option('testmode');
156
+		$this->capture                 = 'yes' === $this->get_option('capture', 'yes');
157
+		$this->statement_descriptor    = $this->get_option('statement_descriptor', wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES));
158
+		$this->stripe_checkout         = 'yes' === $this->get_option('stripe_checkout');
159
+		$this->stripe_checkout_locale  = $this->get_option('stripe_checkout_locale');
160
+		$this->stripe_checkout_image   = $this->get_option('stripe_checkout_image', '');
161
+		$this->saved_cards             = 'yes' === $this->get_option('saved_cards');
162
+		$this->secret_key              = $this->testmode ? $this->get_option('test_secret_key') : $this->get_option('secret_key');
163
+		$this->publishable_key         = $this->testmode ? $this->get_option('test_publishable_key') : $this->get_option('publishable_key');
164
+		$this->bitcoin                 = 'USD' === strtoupper(get_woocommerce_currency()) && 'yes' === $this->get_option('stripe_bitcoin');
165
+		$this->apple_pay               = 'yes' === $this->get_option('apple_pay', 'yes');
166
+		$this->apple_pay_domain_set    = 'yes' === $this->get_option('apple_pay_domain_set', 'no');
167
+		$this->apple_pay_button        = $this->get_option('apple_pay_button', 'black');
168
+		$this->logging                 = 'yes' === $this->get_option('logging');
169 169
 		$this->apple_pay_verify_notice = '';
170 170
 
171
-		if ( $this->stripe_checkout ) {
172
-			$this->order_button_text = __( 'Continue to payment', 'woocommerce-gateway-stripe' );
171
+		if ($this->stripe_checkout) {
172
+			$this->order_button_text = __('Continue to payment', 'woocommerce-gateway-stripe');
173 173
 		}
174 174
 
175
-		if ( $this->testmode ) {
176
-			$this->description .= ' ' . sprintf( __( 'TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe' ), 'https://stripe.com/docs/testing' );
177
-			$this->description  = trim( $this->description );
175
+		if ($this->testmode) {
176
+			$this->description .= ' ' . sprintf(__('TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe'), 'https://stripe.com/docs/testing');
177
+			$this->description  = trim($this->description);
178 178
 		}
179 179
 
180
-		WC_Stripe_API::set_secret_key( $this->secret_key );
180
+		WC_Stripe_API::set_secret_key($this->secret_key);
181 181
 
182 182
 		$this->init_apple_pay();
183 183
 
184 184
 		// Hooks.
185
-		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
186
-		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
187
-		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
188
-		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
185
+		add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
186
+		add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
187
+		add_action('admin_notices', array($this, 'admin_notices'));
188
+		add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
189 189
 	}
190 190
 
191 191
 	/**
@@ -195,24 +195,24 @@  discard block
 block discarded – undo
195 195
 	 * @return string
196 196
 	 */
197 197
 	public function get_icon() {
198
-		$ext   = version_compare( WC()->version, '2.6', '>=' ) ? '.svg' : '.png';
199
-		$style = version_compare( WC()->version, '2.6', '>=' ) ? 'style="margin-left: 0.3em"' : '';
198
+		$ext   = version_compare(WC()->version, '2.6', '>=') ? '.svg' : '.png';
199
+		$style = version_compare(WC()->version, '2.6', '>=') ? 'style="margin-left: 0.3em"' : '';
200 200
 
201
-		$icon  = '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext ) . '" alt="Visa" width="32" ' . $style . ' />';
202
-		$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext ) . '" alt="Mastercard" width="32" ' . $style . ' />';
203
-		$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext ) . '" alt="Amex" width="32" ' . $style . ' />';
201
+		$icon  = '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext) . '" alt="Visa" width="32" ' . $style . ' />';
202
+		$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext) . '" alt="Mastercard" width="32" ' . $style . ' />';
203
+		$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext) . '" alt="Amex" width="32" ' . $style . ' />';
204 204
 
205
-		if ( 'USD' === get_woocommerce_currency() ) {
206
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext ) . '" alt="Discover" width="32" ' . $style . ' />';
207
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext ) . '" alt="JCB" width="32" ' . $style . ' />';
208
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext ) . '" alt="Diners" width="32" ' . $style . ' />';
205
+		if ('USD' === get_woocommerce_currency()) {
206
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext) . '" alt="Discover" width="32" ' . $style . ' />';
207
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext) . '" alt="JCB" width="32" ' . $style . ' />';
208
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext) . '" alt="Diners" width="32" ' . $style . ' />';
209 209
 		}
210 210
 
211
-		if ( $this->bitcoin && $this->stripe_checkout ) {
212
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE ) ) . '" alt="Bitcoin" width="24" ' . $style . ' />';
211
+		if ($this->bitcoin && $this->stripe_checkout) {
212
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(plugins_url('/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE)) . '" alt="Bitcoin" width="24" ' . $style . ' />';
213 213
 		}
214 214
 
215
-		return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
215
+		return apply_filters('woocommerce_gateway_icon', $icon, $this->id);
216 216
 	}
217 217
 
218 218
 	/**
@@ -223,11 +223,11 @@  discard block
 block discarded – undo
223 223
 	 *
224 224
 	 * @return float|int
225 225
 	 */
226
-	public function get_stripe_amount( $total, $currency = '' ) {
227
-		if ( ! $currency ) {
226
+	public function get_stripe_amount($total, $currency = '') {
227
+		if ( ! $currency) {
228 228
 			$currency = get_woocommerce_currency();
229 229
 		}
230
-		switch ( strtoupper( $currency ) ) {
230
+		switch (strtoupper($currency)) {
231 231
 			// Zero decimal currencies.
232 232
 			case 'BIF' :
233 233
 			case 'CLP' :
@@ -244,10 +244,10 @@  discard block
 block discarded – undo
244 244
 			case 'XAF' :
245 245
 			case 'XOF' :
246 246
 			case 'XPF' :
247
-				$total = absint( $total );
247
+				$total = absint($total);
248 248
 				break;
249 249
 			default :
250
-				$total = round( $total, 2 ) * 100; // In cents.
250
+				$total = round($total, 2) * 100; // In cents.
251 251
 				break;
252 252
 		}
253 253
 		return $total;
@@ -262,9 +262,9 @@  discard block
 block discarded – undo
262 262
 	public function init_apple_pay() {
263 263
 		if ( 
264 264
 			is_admin() && 
265
-			isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && 
266
-			isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'] &&
267
-			isset( $_GET['section'] ) && 'stripe' === $_GET['section']
265
+			isset($_GET['page']) && 'wc-settings' === $_GET['page'] && 
266
+			isset($_GET['tab']) && 'checkout' === $_GET['tab'] &&
267
+			isset($_GET['section']) && 'stripe' === $_GET['section']
268 268
 		) {
269 269
 			$this->process_apple_pay_verification();
270 270
 		}
@@ -277,9 +277,9 @@  discard block
 block discarded – undo
277 277
 	 * @version 3.1.0
278 278
 	 * @param string $secret_key
279 279
 	 */
280
-	private function register_apple_pay_domain( $secret_key = '' ) {
281
-		if ( empty( $secret_key ) ) {
282
-			throw new Exception( __( 'Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe' ) );
280
+	private function register_apple_pay_domain($secret_key = '') {
281
+		if (empty($secret_key)) {
282
+			throw new Exception(__('Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe'));
283 283
 		}
284 284
 
285 285
 		$endpoint = 'https://api.stripe.com/v1/apple_pay/domains';
@@ -293,17 +293,17 @@  discard block
 block discarded – undo
293 293
 			'Authorization' => 'Bearer ' . $secret_key,
294 294
 		);
295 295
 
296
-		$response = wp_remote_post( $endpoint, array(
296
+		$response = wp_remote_post($endpoint, array(
297 297
 			'headers' => $headers,
298
-			'body'    => http_build_query( $data ),
299
-		) );
298
+			'body'    => http_build_query($data),
299
+		));
300 300
 
301
-		if ( 200 !== $response['response']['code'] ) {
302
-			$parsed_response = json_decode( $response['body'] );
301
+		if (200 !== $response['response']['code']) {
302
+			$parsed_response = json_decode($response['body']);
303 303
 
304 304
 			$this->apple_pay_verify_notice = $parsed_response->error->message;
305 305
 
306
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $parsed_response->error->message ) );
306
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $parsed_response->error->message));
307 307
 		}
308 308
 	}
309 309
 
@@ -314,48 +314,48 @@  discard block
 block discarded – undo
314 314
 	 * @version 3.1.0
315 315
 	 */
316 316
 	public function process_apple_pay_verification() {
317
-		$gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
317
+		$gateway_settings = get_option('woocommerce_stripe_settings', '');
318 318
 
319 319
 		try {
320
-			$path     = untrailingslashit( preg_replace( "!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME'] ) );
320
+			$path     = untrailingslashit(preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']));
321 321
 			$dir      = '.well-known';
322 322
 			$file     = 'apple-developer-merchantid-domain-association';
323 323
 			$fullpath = $path . '/' . $dir . '/' . $file;
324 324
 
325
-			if ( ! empty( $gateway_settings['apple_pay_domain_set'] ) && 'yes' === $gateway_settings['apple_pay_domain_set'] && file_exists( $fullpath ) ) {
325
+			if ( ! empty($gateway_settings['apple_pay_domain_set']) && 'yes' === $gateway_settings['apple_pay_domain_set'] && file_exists($fullpath)) {
326 326
 				return;
327 327
 			}
328 328
 
329
-			if ( ! file_exists( $path . '/' . $dir ) ) {
330
-				if ( ! @mkdir( $path . '/' . $dir, 0755 ) ) {
331
-					throw new Exception( __( 'Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe' ) );
329
+			if ( ! file_exists($path . '/' . $dir)) {
330
+				if ( ! @mkdir($path . '/' . $dir, 0755)) {
331
+					throw new Exception(__('Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe'));
332 332
 				}
333 333
 			}
334 334
 
335
-			if ( ! file_exists( $fullpath ) ) {
336
-				if ( ! @copy( WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath ) ) {
337
-					throw new Exception( __( 'Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe' ) );
335
+			if ( ! file_exists($fullpath)) {
336
+				if ( ! @copy(WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath)) {
337
+					throw new Exception(__('Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe'));
338 338
 				}
339 339
 			}
340 340
 
341 341
 			// At this point then the domain association folder and file should be available.
342 342
 			// Proceed to verify/and or verify again.
343
-			$this->register_apple_pay_domain( $this->secret_key );
343
+			$this->register_apple_pay_domain($this->secret_key);
344 344
 
345 345
 			// No errors to this point, verification success!
346 346
 			$gateway_settings['apple_pay_domain_set'] = 'yes';
347 347
 			$this->apple_pay_domain_set = true;
348 348
 
349
-			update_option( 'woocommerce_stripe_settings', $gateway_settings );
349
+			update_option('woocommerce_stripe_settings', $gateway_settings);
350 350
 
351
-			$this->log( __( 'Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe' ) );
351
+			$this->log(__('Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe'));
352 352
 
353
-		} catch ( Exception $e ) {
353
+		} catch (Exception $e) {
354 354
 			$gateway_settings['apple_pay_domain_set'] = 'no';
355 355
 
356
-			update_option( 'woocommerce_stripe_settings', $gateway_settings );
356
+			update_option('woocommerce_stripe_settings', $gateway_settings);
357 357
 
358
-			$this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
358
+			$this->log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
359 359
 		}
360 360
 	}
361 361
 
@@ -363,12 +363,12 @@  discard block
 block discarded – undo
363 363
 	 * Check if SSL is enabled and notify the user
364 364
 	 */
365 365
 	public function admin_notices() {
366
-		if ( 'no' === $this->enabled ) {
366
+		if ('no' === $this->enabled) {
367 367
 			return;
368 368
 		}
369 369
 
370
-		if ( $this->apple_pay && ! empty( $this->apple_pay_verify_notice ) ) {
371
-			echo '<div class="error stripe-apple-pay-message"><p>' . wp_kses( make_clickable( $this->apple_pay_verify_notice ) ) . '</p></div>';
370
+		if ($this->apple_pay && ! empty($this->apple_pay_verify_notice)) {
371
+			echo '<div class="error stripe-apple-pay-message"><p>' . wp_kses(make_clickable($this->apple_pay_verify_notice)) . '</p></div>';
372 372
 		}
373 373
 
374 374
 		/**
@@ -376,13 +376,13 @@  discard block
 block discarded – undo
376 376
 		 * when setting screen is displayed. So if domain verification is not set,
377 377
 		 * something went wrong so lets notify user.
378 378
 		 */
379
-		if ( ! empty( $this->secret_key ) && $this->apple_pay && ! $this->apple_pay_domain_set ) {
380
-			echo '<div class="error stripe-apple-pay-message"><p>' . sprintf( __( 'Apple Pay domain verification failed. Please check the %1$slog%2$s to see the issue. (Logging must be enabled to see recorded logs)', 'woocommerce-gateway-stripe' ), '<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '">', '</a>' ) . '</p></div>';
379
+		if ( ! empty($this->secret_key) && $this->apple_pay && ! $this->apple_pay_domain_set) {
380
+			echo '<div class="error stripe-apple-pay-message"><p>' . sprintf(__('Apple Pay domain verification failed. Please check the %1$slog%2$s to see the issue. (Logging must be enabled to see recorded logs)', 'woocommerce-gateway-stripe'), '<a href="' . admin_url('admin.php?page=wc-status&tab=logs') . '">', '</a>') . '</p></div>';
381 381
 		}
382 382
 
383 383
 		// Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected.
384
-		if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) {
385
-			echo '<div class="error stripe-ssl-message"><p>' . sprintf( __( 'Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) . '</p></div>';
384
+		if ((function_exists('wc_site_is_https') && ! wc_site_is_https()) && ('no' === get_option('woocommerce_force_ssl_checkout') && ! class_exists('WordPressHTTPS'))) {
385
+			echo '<div class="error stripe-ssl-message"><p>' . sprintf(__('Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe'), admin_url('admin.php?page=wc-settings&tab=checkout')) . '</p></div>';
386 386
 		}
387 387
 	}
388 388
 
@@ -390,11 +390,11 @@  discard block
 block discarded – undo
390 390
 	 * Check if this gateway is enabled
391 391
 	 */
392 392
 	public function is_available() {
393
-		if ( 'yes' === $this->enabled ) {
394
-			if ( ! $this->testmode && is_checkout() && ! is_ssl() ) {
393
+		if ('yes' === $this->enabled) {
394
+			if ( ! $this->testmode && is_checkout() && ! is_ssl()) {
395 395
 				return false;
396 396
 			}
397
-			if ( ! $this->secret_key || ! $this->publishable_key ) {
397
+			if ( ! $this->secret_key || ! $this->publishable_key) {
398 398
 				return false;
399 399
 			}
400 400
 			return true;
@@ -406,7 +406,7 @@  discard block
 block discarded – undo
406 406
 	 * Initialise Gateway Settings Form Fields
407 407
 	 */
408 408
 	public function init_form_fields() {
409
-		$this->form_fields = include( 'settings-stripe.php' );
409
+		$this->form_fields = include('settings-stripe.php');
410 410
 	}
411 411
 
412 412
 	/**
@@ -414,54 +414,54 @@  discard block
 block discarded – undo
414 414
 	 */
415 415
 	public function payment_fields() {
416 416
 		$user                 = wp_get_current_user();
417
-		$display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards;
417
+		$display_tokenization = $this->supports('tokenization') && is_checkout() && $this->saved_cards;
418 418
 		$total                = WC()->cart->total;
419 419
 
420 420
 		// If paying from order, we need to get total from order not cart.
421
-		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
422
-			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
421
+		if (isset($_GET['pay_for_order']) && ! empty($_GET['key'])) {
422
+			$order = wc_get_order(wc_get_order_id_by_order_key(wc_clean($_GET['key'])));
423 423
 			$total = $order->get_total();
424 424
 		}
425 425
 
426
-		if ( $user->ID ) {
427
-			$user_email = get_user_meta( $user->ID, 'billing_email', true );
426
+		if ($user->ID) {
427
+			$user_email = get_user_meta($user->ID, 'billing_email', true);
428 428
 			$user_email = $user_email ? $user_email : $user->user_email;
429 429
 		} else {
430 430
 			$user_email = '';
431 431
 		}
432 432
 
433
-		if ( is_add_payment_method_page() ) {
434
-			$pay_button_text = __( 'Add Card', 'woocommerce-gateway-stripe' );
433
+		if (is_add_payment_method_page()) {
434
+			$pay_button_text = __('Add Card', 'woocommerce-gateway-stripe');
435 435
 		} else {
436 436
 			$pay_button_text = '';
437 437
 		}
438 438
 
439 439
 		echo '<div
440 440
 			id="stripe-payment-data"
441
-			data-panel-label="' . esc_attr( $pay_button_text ) . '"
441
+			data-panel-label="' . esc_attr($pay_button_text) . '"
442 442
 			data-description=""
443
-			data-email="' . esc_attr( $user_email ) . '"
444
-			data-amount="' . esc_attr( $this->get_stripe_amount( $total ) ) . '"
445
-			data-name="' . esc_attr( $this->statement_descriptor ) . '"
446
-			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '"
447
-			data-image="' . esc_attr( $this->stripe_checkout_image ) . '"
448
-			data-bitcoin="' . esc_attr( $this->bitcoin ? 'true' : 'false' ) . '"
449
-			data-locale="' . esc_attr( $this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en' ) . '"
450
-			data-allow-remember-me="' . esc_attr( $this->saved_cards ? 'true' : 'false' ) . '">';
443
+			data-email="' . esc_attr($user_email) . '"
444
+			data-amount="' . esc_attr($this->get_stripe_amount($total)) . '"
445
+			data-name="' . esc_attr($this->statement_descriptor) . '"
446
+			data-currency="' . esc_attr(strtolower(get_woocommerce_currency())) . '"
447
+			data-image="' . esc_attr($this->stripe_checkout_image) . '"
448
+			data-bitcoin="' . esc_attr($this->bitcoin ? 'true' : 'false') . '"
449
+			data-locale="' . esc_attr($this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en') . '"
450
+			data-allow-remember-me="' . esc_attr($this->saved_cards ? 'true' : 'false') . '">';
451 451
 
452
-		if ( $this->description ) {
453
-			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
452
+		if ($this->description) {
453
+			echo apply_filters('wc_stripe_description', wpautop(wp_kses_post($this->description)));
454 454
 		}
455 455
 
456
-		if ( $display_tokenization ) {
456
+		if ($display_tokenization) {
457 457
 			$this->tokenization_script();
458 458
 			$this->saved_payment_methods();
459 459
 		}
460 460
 
461
-		if ( ! $this->stripe_checkout ) {
461
+		if ( ! $this->stripe_checkout) {
462 462
 			$this->form();
463 463
 
464
-			if ( $display_tokenization ) {
464
+			if ($display_tokenization) {
465 465
 				$this->save_payment_method_checkbox();
466 466
 			}
467 467
 		}
@@ -477,20 +477,20 @@  discard block
 block discarded – undo
477 477
 	 * @return array
478 478
 	 */
479 479
 	public function get_localized_messages() {
480
-		return apply_filters( 'wc_stripe_localized_messages', array(
481
-			'invalid_number'        => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ),
482
-			'invalid_expiry_month'  => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ),
483
-			'invalid_expiry_year'   => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ),
484
-			'invalid_cvc'           => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ),
485
-			'incorrect_number'      => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ),
486
-			'expired_card'          => __( 'The card has expired.', 'woocommerce-gateway-stripe' ),
487
-			'incorrect_cvc'         => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ),
488
-			'incorrect_zip'         => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ),
489
-			'card_declined'         => __( 'The card was declined.', 'woocommerce-gateway-stripe' ),
490
-			'missing'               => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ),
491
-			'processing_error'      => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ),
492
-			'invalid_request_error' => __( 'Could not find payment information.', 'woocommerce-gateway-stripe' ),
493
-		) );
480
+		return apply_filters('wc_stripe_localized_messages', array(
481
+			'invalid_number'        => __('The card number is not a valid credit card number.', 'woocommerce-gateway-stripe'),
482
+			'invalid_expiry_month'  => __('The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe'),
483
+			'invalid_expiry_year'   => __('The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe'),
484
+			'invalid_cvc'           => __('The card\'s security code is invalid.', 'woocommerce-gateway-stripe'),
485
+			'incorrect_number'      => __('The card number is incorrect.', 'woocommerce-gateway-stripe'),
486
+			'expired_card'          => __('The card has expired.', 'woocommerce-gateway-stripe'),
487
+			'incorrect_cvc'         => __('The card\'s security code is incorrect.', 'woocommerce-gateway-stripe'),
488
+			'incorrect_zip'         => __('The card\'s zip code failed validation.', 'woocommerce-gateway-stripe'),
489
+			'card_declined'         => __('The card was declined.', 'woocommerce-gateway-stripe'),
490
+			'missing'               => __('There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe'),
491
+			'processing_error'      => __('An error occurred while processing the card.', 'woocommerce-gateway-stripe'),
492
+			'invalid_request_error' => __('Could not find payment information.', 'woocommerce-gateway-stripe'),
493
+		));
494 494
 	}
495 495
 
496 496
 	/**
@@ -500,28 +500,28 @@  discard block
 block discarded – undo
500 500
 	 * @version 3.1.0
501 501
 	 */
502 502
 	public function admin_scripts() {
503
-		if ( 'woocommerce_page_wc-settings' !== get_current_screen()->id ) {
503
+		if ('woocommerce_page_wc-settings' !== get_current_screen()->id) {
504 504
 			return;
505 505
 		}
506 506
 
507
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
507
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
508 508
 
509
-		wp_enqueue_script( 'woocommerce_stripe_admin', plugins_url( 'assets/js/stripe-admin' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION, true );
509
+		wp_enqueue_script('woocommerce_stripe_admin', plugins_url('assets/js/stripe-admin' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION, true);
510 510
 
511 511
 		$stripe_admin_params = array(
512 512
 			'localized_messages' => array(
513
-				'not_valid_live_key_msg' => __( 'This is not a valid live key. Live keys start with "sk_live_" and "pk_live_".', 'woocommerce-gateway-stripe' ),
514
-				'not_valid_test_key_msg' => __( 'This is not a valid test key. Test keys start with "sk_test_" and "pk_test_".', 'woocommerce-gateway-stripe' ),
515
-				're_verify_button_text'  => __( 'Re-verify Domain', 'woocommerce-gateway-stripe' ),
516
-				'missing_secret_key'     => __( 'Missing Secret Key. Please set the secret key field above and re-try.', 'woocommerce-gateway-stripe' ),
513
+				'not_valid_live_key_msg' => __('This is not a valid live key. Live keys start with "sk_live_" and "pk_live_".', 'woocommerce-gateway-stripe'),
514
+				'not_valid_test_key_msg' => __('This is not a valid test key. Test keys start with "sk_test_" and "pk_test_".', 'woocommerce-gateway-stripe'),
515
+				're_verify_button_text'  => __('Re-verify Domain', 'woocommerce-gateway-stripe'),
516
+				'missing_secret_key'     => __('Missing Secret Key. Please set the secret key field above and re-try.', 'woocommerce-gateway-stripe'),
517 517
 			),
518
-			'ajaxurl'            => admin_url( 'admin-ajax.php' ),
518
+			'ajaxurl'            => admin_url('admin-ajax.php'),
519 519
 			'nonce'              => array( 
520
-				'apple_pay_domain_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_domain_nonce' ),
520
+				'apple_pay_domain_nonce' => wp_create_nonce('_wc_stripe_apple_pay_domain_nonce'),
521 521
 			),
522 522
 		);
523 523
 
524
-		wp_localize_script( 'woocommerce_stripe_admin', 'wc_stripe_admin_params', apply_filters( 'wc_stripe_admin_params', $stripe_admin_params ) );
524
+		wp_localize_script('woocommerce_stripe_admin', 'wc_stripe_admin_params', apply_filters('wc_stripe_admin_params', $stripe_admin_params));
525 525
 	}
526 526
 
527 527
 	/**
@@ -532,49 +532,49 @@  discard block
 block discarded – undo
532 532
 	 * @access public
533 533
 	 */
534 534
 	public function payment_scripts() {
535
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
535
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order']) && ! is_add_payment_method_page()) {
536 536
 			return;
537 537
 		}
538 538
 
539
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
539
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
540 540
 
541
-		if ( $this->stripe_checkout ) {
542
-			wp_enqueue_script( 'stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true );
543
-			wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe-checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe_checkout' ), WC_STRIPE_VERSION, true );
541
+		if ($this->stripe_checkout) {
542
+			wp_enqueue_script('stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true);
543
+			wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe-checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe_checkout'), WC_STRIPE_VERSION, true);
544 544
 		} else {
545
-			wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
546
-			wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery-payment', 'stripe' ), WC_STRIPE_VERSION, true );
545
+			wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
546
+			wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('jquery-payment', 'stripe'), WC_STRIPE_VERSION, true);
547 547
 		}
548 548
 
549 549
 		$stripe_params = array(
550 550
 			'key'                  => $this->publishable_key,
551
-			'i18n_terms'           => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ),
552
-			'i18n_required_fields' => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ),
551
+			'i18n_terms'           => __('Please accept the terms and conditions first', 'woocommerce-gateway-stripe'),
552
+			'i18n_required_fields' => __('Please fill in required checkout fields first', 'woocommerce-gateway-stripe'),
553 553
 		);
554 554
 
555 555
 		// If we're on the pay page we need to pass stripe.js the address of the order.
556
-		if ( isset( $_GET['pay_for_order'] ) && 'true' === $_GET['pay_for_order'] ) {
557
-			$order_id = wc_get_order_id_by_order_key( urldecode( $_GET['key'] ) );
558
-			$order    = wc_get_order( $order_id );
556
+		if (isset($_GET['pay_for_order']) && 'true' === $_GET['pay_for_order']) {
557
+			$order_id = wc_get_order_id_by_order_key(urldecode($_GET['key']));
558
+			$order    = wc_get_order($order_id);
559 559
 
560
-			$stripe_params['billing_first_name'] = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_first_name : $order->get_billing_first_name();
561
-			$stripe_params['billing_last_name']  = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_last_name : $order->get_billing_last_name();
562
-			$stripe_params['billing_address_1']  = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_address_1 : $order->get_billing_address_1();
563
-			$stripe_params['billing_address_2']  = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_address_2 : $order->get_billing_address_2();
564
-			$stripe_params['billing_state']      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_state : $order->get_billing_state();
565
-			$stripe_params['billing_city']       = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_city : $order->get_billing_city();
566
-			$stripe_params['billing_postcode']   = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_postcode : $order->get_billing_postcode();
567
-			$stripe_params['billing_country']    = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_country : $order->get_billing_country();
560
+			$stripe_params['billing_first_name'] = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_first_name : $order->get_billing_first_name();
561
+			$stripe_params['billing_last_name']  = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_last_name : $order->get_billing_last_name();
562
+			$stripe_params['billing_address_1']  = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_address_1 : $order->get_billing_address_1();
563
+			$stripe_params['billing_address_2']  = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_address_2 : $order->get_billing_address_2();
564
+			$stripe_params['billing_state']      = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_state : $order->get_billing_state();
565
+			$stripe_params['billing_city']       = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_city : $order->get_billing_city();
566
+			$stripe_params['billing_postcode']   = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_postcode : $order->get_billing_postcode();
567
+			$stripe_params['billing_country']    = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_country : $order->get_billing_country();
568 568
 		}
569 569
 
570
-		$stripe_params['no_prepaid_card_msg']                     = __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' );
571
-		$stripe_params['allow_prepaid_card']                      = apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no';
572
-		$stripe_params['stripe_checkout_require_billing_address'] = apply_filters( 'wc_stripe_checkout_require_billing_address', false ) ? 'yes' : 'no';
570
+		$stripe_params['no_prepaid_card_msg']                     = __('Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe');
571
+		$stripe_params['allow_prepaid_card']                      = apply_filters('wc_stripe_allow_prepaid_card', true) ? 'yes' : 'no';
572
+		$stripe_params['stripe_checkout_require_billing_address'] = apply_filters('wc_stripe_checkout_require_billing_address', false) ? 'yes' : 'no';
573 573
 
574 574
 		// merge localized messages to be use in JS
575
-		$stripe_params = array_merge( $stripe_params, $this->get_localized_messages() );
575
+		$stripe_params = array_merge($stripe_params, $this->get_localized_messages());
576 576
 
577
-		wp_localize_script( 'woocommerce_stripe', 'wc_stripe_params', apply_filters( 'wc_stripe_params', $stripe_params ) );
577
+		wp_localize_script('woocommerce_stripe', 'wc_stripe_params', apply_filters('wc_stripe_params', $stripe_params));
578 578
 	}
579 579
 
580 580
 	/**
@@ -583,35 +583,35 @@  discard block
 block discarded – undo
583 583
 	 * @param  object $source
584 584
 	 * @return array()
585 585
 	 */
586
-	protected function generate_payment_request( $order, $source ) {
586
+	protected function generate_payment_request($order, $source) {
587 587
 		$post_data                = array();
588
-		$post_data['currency']    = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() );
589
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
590
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
588
+		$post_data['currency']    = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency());
589
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
590
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
591 591
 		$post_data['capture']     = $this->capture ? 'true' : 'false';
592 592
 
593
-		$billing_email      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email();
594
-		$billing_first_name = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_first_name : $order->get_billing_first_name();
595
-		$billing_last_name  = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_last_name : $order->get_billing_last_name();
593
+		$billing_email      = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email();
594
+		$billing_first_name = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_first_name : $order->get_billing_first_name();
595
+		$billing_last_name  = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_last_name : $order->get_billing_last_name();
596 596
 
597
-		if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
597
+		if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
598 598
 			$post_data['receipt_email'] = $billing_email;
599 599
 		}
600 600
 
601
-		$post_data['expand[]']    = 'balance_transaction';
601
+		$post_data['expand[]'] = 'balance_transaction';
602 602
 
603 603
 		$metadata = array(
604
-			__( 'Customer Name', 'woocommerce-gateway-stripe' ) => sanitize_text_field( $billing_first_name ) . ' ' . sanitize_text_field( $billing_last_name ),
605
-			__( 'Customer Email', 'woocommerce-gateway-stripe' ) => sanitize_email( $billing_email ),
604
+			__('Customer Name', 'woocommerce-gateway-stripe') => sanitize_text_field($billing_first_name) . ' ' . sanitize_text_field($billing_last_name),
605
+			__('Customer Email', 'woocommerce-gateway-stripe') => sanitize_email($billing_email),
606 606
 		);
607 607
 
608
-		$post_data['metadata'] = apply_filters( 'wc_stripe_payment_metadata', $metadata, $order, $source );
608
+		$post_data['metadata'] = apply_filters('wc_stripe_payment_metadata', $metadata, $order, $source);
609 609
 
610
-		if ( $source->customer ) {
610
+		if ($source->customer) {
611 611
 			$post_data['customer'] = $source->customer;
612 612
 		}
613 613
 
614
-		if ( $source->source ) {
614
+		if ($source->source) {
615 615
 			$post_data['source'] = $source->source;
616 616
 		}
617 617
 
@@ -623,7 +623,7 @@  discard block
 block discarded – undo
623 623
 		 * @param WC_Order $order
624 624
 		 * @param object $source
625 625
 		 */
626
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order, $source );
626
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order, $source);
627 627
 	}
628 628
 
629 629
 	/**
@@ -635,37 +635,37 @@  discard block
 block discarded – undo
635 635
 	 * @throws Exception When card was not added or for and invalid card.
636 636
 	 * @return object
637 637
 	 */
638
-	protected function get_source( $user_id, $force_customer = false ) {
639
-		$stripe_customer = new WC_Stripe_Customer( $user_id );
638
+	protected function get_source($user_id, $force_customer = false) {
639
+		$stripe_customer = new WC_Stripe_Customer($user_id);
640 640
 		$stripe_source   = false;
641 641
 		$token_id        = false;
642 642
 
643 643
 		// New CC info was entered and we have a new token to process
644
-		if ( isset( $_POST['stripe_token'] ) ) {
645
-			$stripe_token     = wc_clean( $_POST['stripe_token'] );
646
-			$maybe_saved_card = isset( $_POST['wc-stripe-new-payment-method'] ) && ! empty( $_POST['wc-stripe-new-payment-method'] );
644
+		if (isset($_POST['stripe_token'])) {
645
+			$stripe_token     = wc_clean($_POST['stripe_token']);
646
+			$maybe_saved_card = isset($_POST['wc-stripe-new-payment-method']) && ! empty($_POST['wc-stripe-new-payment-method']);
647 647
 
648 648
 			// This is true if the user wants to store the card to their account.
649
-			if ( ( $user_id && $this->saved_cards && $maybe_saved_card ) || $force_customer ) {
650
-				$stripe_source = $stripe_customer->add_card( $stripe_token );
649
+			if (($user_id && $this->saved_cards && $maybe_saved_card) || $force_customer) {
650
+				$stripe_source = $stripe_customer->add_card($stripe_token);
651 651
 
652
-				if ( is_wp_error( $stripe_source ) ) {
653
-					throw new Exception( $stripe_source->get_error_message() );
652
+				if (is_wp_error($stripe_source)) {
653
+					throw new Exception($stripe_source->get_error_message());
654 654
 				}
655 655
 			} else {
656 656
 				// Not saving token, so don't define customer either.
657 657
 				$stripe_source   = $stripe_token;
658 658
 				$stripe_customer = false;
659 659
 			}
660
-		} elseif ( isset( $_POST['wc-stripe-payment-token'] ) && 'new' !== $_POST['wc-stripe-payment-token'] ) {
660
+		} elseif (isset($_POST['wc-stripe-payment-token']) && 'new' !== $_POST['wc-stripe-payment-token']) {
661 661
 			// Use an existing token, and then process the payment
662 662
 
663
-			$token_id = wc_clean( $_POST['wc-stripe-payment-token'] );
664
-			$token    = WC_Payment_Tokens::get( $token_id );
663
+			$token_id = wc_clean($_POST['wc-stripe-payment-token']);
664
+			$token    = WC_Payment_Tokens::get($token_id);
665 665
 
666
-			if ( ! $token || $token->get_user_id() !== get_current_user_id() ) {
667
-				WC()->session->set( 'refresh_totals', true );
668
-				throw new Exception( __( 'Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe' ) );
666
+			if ( ! $token || $token->get_user_id() !== get_current_user_id()) {
667
+				WC()->session->set('refresh_totals', true);
668
+				throw new Exception(__('Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe'));
669 669
 			}
670 670
 
671 671
 			$stripe_source = $token->get_token();
@@ -689,19 +689,19 @@  discard block
 block discarded – undo
689 689
 	 * @param object $order
690 690
 	 * @return object
691 691
 	 */
692
-	protected function get_order_source( $order = null ) {
692
+	protected function get_order_source($order = null) {
693 693
 		$stripe_customer = new WC_Stripe_Customer();
694 694
 		$stripe_source   = false;
695 695
 		$token_id        = false;
696 696
 
697
-		if ( $order ) {
698
-			$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
697
+		if ($order) {
698
+			$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
699 699
 
700
-			if ( $meta_value = get_post_meta( $order_id, '_stripe_customer_id', true ) ) {
701
-				$stripe_customer->set_id( $meta_value );
700
+			if ($meta_value = get_post_meta($order_id, '_stripe_customer_id', true)) {
701
+				$stripe_customer->set_id($meta_value);
702 702
 			}
703 703
 
704
-			if ( $meta_value = get_post_meta( $order_id, '_stripe_card_id', true ) ) {
704
+			if ($meta_value = get_post_meta($order_id, '_stripe_card_id', true)) {
705 705
 				$stripe_source = $meta_value;
706 706
 			}
707 707
 		}
@@ -724,57 +724,57 @@  discard block
 block discarded – undo
724 724
 	 *
725 725
 	 * @return array|void
726 726
 	 */
727
-	public function process_payment( $order_id, $retry = true, $force_customer = false ) {
727
+	public function process_payment($order_id, $retry = true, $force_customer = false) {
728 728
 		try {
729
-			$order  = wc_get_order( $order_id );
730
-			$source = $this->get_source( get_current_user_id(), $force_customer );
729
+			$order  = wc_get_order($order_id);
730
+			$source = $this->get_source(get_current_user_id(), $force_customer);
731 731
 
732
-			if ( empty( $source->source ) && empty( $source->customer ) ) {
733
-				$error_msg = __( 'Please enter your card details to make a payment.', 'woocommerce-gateway-stripe' );
734
-				$error_msg .= ' ' . __( 'Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe' );
735
-				throw new Exception( $error_msg );
732
+			if (empty($source->source) && empty($source->customer)) {
733
+				$error_msg = __('Please enter your card details to make a payment.', 'woocommerce-gateway-stripe');
734
+				$error_msg .= ' ' . __('Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe');
735
+				throw new Exception($error_msg);
736 736
 			}
737 737
 
738 738
 			// Store source to order meta.
739
-			$this->save_source( $order, $source );
739
+			$this->save_source($order, $source);
740 740
 
741 741
 			// Handle payment.
742
-			if ( $order->get_total() > 0 ) {
742
+			if ($order->get_total() > 0) {
743 743
 
744
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
745
-					throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) );
744
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
745
+					throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100)));
746 746
 				}
747 747
 
748
-				$this->log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
748
+				$this->log("Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}");
749 749
 
750 750
 				// Make the request.
751
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) );
751
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $source));
752 752
 
753
-				if ( is_wp_error( $response ) ) {
753
+				if (is_wp_error($response)) {
754 754
 					// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
755
-					if ( 'customer' === $response->get_error_code() && $retry ) {
756
-						delete_user_meta( get_current_user_id(), '_stripe_customer_id' );
757
-						return $this->process_payment( $order_id, false, $force_customer );
755
+					if ('customer' === $response->get_error_code() && $retry) {
756
+						delete_user_meta(get_current_user_id(), '_stripe_customer_id');
757
+						return $this->process_payment($order_id, false, $force_customer);
758 758
 						// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
759
-					} elseif ( 'source' === $response->get_error_code() && $source->token_id ) {
760
-						$token = WC_Payment_Tokens::get( $source->token_id );
759
+					} elseif ('source' === $response->get_error_code() && $source->token_id) {
760
+						$token = WC_Payment_Tokens::get($source->token_id);
761 761
 						$token->delete();
762
-						$message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
763
-						$order->add_order_note( $message );
764
-						throw new Exception( $message );
762
+						$message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe');
763
+						$order->add_order_note($message);
764
+						throw new Exception($message);
765 765
 					}
766 766
 
767 767
 					$localized_messages = $this->get_localized_messages();
768 768
 
769
-					$message = isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message();
769
+					$message = isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message();
770 770
 
771
-					$order->add_order_note( $message );
771
+					$order->add_order_note($message);
772 772
 
773
-					throw new Exception( $message );
773
+					throw new Exception($message);
774 774
 				}
775 775
 
776 776
 				// Process valid response.
777
-				$this->process_response( $response, $order );
777
+				$this->process_response($response, $order);
778 778
 			} else {
779 779
 				$order->payment_complete();
780 780
 			}
@@ -782,23 +782,23 @@  discard block
 block discarded – undo
782 782
 			// Remove cart.
783 783
 			WC()->cart->empty_cart();
784 784
 
785
-			do_action( 'wc_gateway_stripe_process_payment', $response, $order );
785
+			do_action('wc_gateway_stripe_process_payment', $response, $order);
786 786
 
787 787
 			// Return thank you page redirect.
788 788
 			return array(
789 789
 				'result'   => 'success',
790
-				'redirect' => $this->get_return_url( $order ),
790
+				'redirect' => $this->get_return_url($order),
791 791
 			);
792 792
 
793
-		} catch ( Exception $e ) {
794
-			wc_add_notice( $e->getMessage(), 'error' );
795
-			$this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
793
+		} catch (Exception $e) {
794
+			wc_add_notice($e->getMessage(), 'error');
795
+			$this->log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
796 796
 
797
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
798
-				$this->send_failed_order_email( $order_id );
797
+			if ($order->has_status(array('pending', 'failed'))) {
798
+				$this->send_failed_order_email($order_id);
799 799
 			}
800 800
 
801
-			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
801
+			do_action('wc_gateway_stripe_process_payment_error', $e, $order);
802 802
 
803 803
 			return array(
804 804
 				'result'   => 'fail',
@@ -813,56 +813,56 @@  discard block
 block discarded – undo
813 813
 	 * @param WC_Order $order For to which the source applies.
814 814
 	 * @param stdClass $source Source information.
815 815
 	 */
816
-	protected function save_source( $order, $source ) {
817
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
816
+	protected function save_source($order, $source) {
817
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
818 818
 
819 819
 		// Store source in the order.
820
-		if ( $source->customer ) {
821
-			update_post_meta( $order_id, '_stripe_customer_id', $source->customer );
820
+		if ($source->customer) {
821
+			update_post_meta($order_id, '_stripe_customer_id', $source->customer);
822 822
 		}
823
-		if ( $source->source ) {
824
-			update_post_meta( $order_id, '_stripe_card_id', $source->source );
823
+		if ($source->source) {
824
+			update_post_meta($order_id, '_stripe_card_id', $source->source);
825 825
 		}
826 826
 	}
827 827
 
828 828
 	/**
829 829
 	 * Store extra meta data for an order from a Stripe Response.
830 830
 	 */
831
-	public function process_response( $response, $order ) {
832
-		$this->log( 'Processing response: ' . print_r( $response, true ) );
831
+	public function process_response($response, $order) {
832
+		$this->log('Processing response: ' . print_r($response, true));
833 833
 
834
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
834
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
835 835
 
836 836
 		// Store charge data
837
-		update_post_meta( $order_id, '_stripe_charge_id', $response->id );
838
-		update_post_meta( $order_id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no' );
837
+		update_post_meta($order_id, '_stripe_charge_id', $response->id);
838
+		update_post_meta($order_id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no');
839 839
 
840 840
 		// Store other data such as fees
841
-		if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) {
841
+		if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) {
842 842
 			// Fees and Net needs to both come from Stripe to be accurate as the returned
843 843
 			// values are in the local currency of the Stripe account, not from WC.
844
-			$fee = ! empty( $response->balance_transaction->fee ) ? WC_Stripe::format_number( $response->balance_transaction, 'fee' ) : 0;
845
-			$net = ! empty( $response->balance_transaction->net ) ? WC_Stripe::format_number( $response->balance_transaction, 'net' ) : 0;
846
-			update_post_meta( $order_id, 'Stripe Fee', $fee );
847
-			update_post_meta( $order_id, 'Net Revenue From Stripe', $net );
844
+			$fee = ! empty($response->balance_transaction->fee) ? WC_Stripe::format_number($response->balance_transaction, 'fee') : 0;
845
+			$net = ! empty($response->balance_transaction->net) ? WC_Stripe::format_number($response->balance_transaction, 'net') : 0;
846
+			update_post_meta($order_id, 'Stripe Fee', $fee);
847
+			update_post_meta($order_id, 'Net Revenue From Stripe', $net);
848 848
 		}
849 849
 
850
-		if ( $response->captured ) {
851
-			$order->payment_complete( $response->id );
850
+		if ($response->captured) {
851
+			$order->payment_complete($response->id);
852 852
 
853
-			$message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id );
854
-			$order->add_order_note( $message );
855
-			$this->log( 'Success: ' . $message );
853
+			$message = sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $response->id);
854
+			$order->add_order_note($message);
855
+			$this->log('Success: ' . $message);
856 856
 
857 857
 		} else {
858
-			add_post_meta( $order_id, '_transaction_id', $response->id, true );
858
+			add_post_meta($order_id, '_transaction_id', $response->id, true);
859 859
 
860
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
861
-				version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->reduce_order_stock() : wc_reduce_stock_levels( $order_id );
860
+			if ($order->has_status(array('pending', 'failed'))) {
861
+				version_compare(WC_VERSION, '3.0.0', '<') ? $order->reduce_order_stock() : wc_reduce_stock_levels($order_id);
862 862
 			}
863 863
 
864
-			$order->update_status( 'on-hold', sprintf( __( 'Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe' ), $response->id ) );
865
-			$this->log( "Successful auth: $response->id" );
864
+			$order->update_status('on-hold', sprintf(__('Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe'), $response->id));
865
+			$this->log("Successful auth: $response->id");
866 866
 		}
867 867
 
868 868
 		return $response;
@@ -874,32 +874,32 @@  discard block
 block discarded – undo
874 874
 	 * @since 3.0.0
875 875
 	 */
876 876
 	public function add_payment_method() {
877
-		if ( empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) {
878
-			wc_add_notice( __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ), 'error' );
877
+		if (empty($_POST['stripe_token']) || ! is_user_logged_in()) {
878
+			wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error');
879 879
 			return;
880 880
 		}
881 881
 
882
-		$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
883
-		$card            = $stripe_customer->add_card( wc_clean( $_POST['stripe_token'] ) );
882
+		$stripe_customer = new WC_Stripe_Customer(get_current_user_id());
883
+		$card            = $stripe_customer->add_card(wc_clean($_POST['stripe_token']));
884 884
 
885
-		if ( is_wp_error( $card ) ) {
885
+		if (is_wp_error($card)) {
886 886
 			$localized_messages = $this->get_localized_messages();
887
-			$error_msg = __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' );
887
+			$error_msg = __('There was a problem adding the card.', 'woocommerce-gateway-stripe');
888 888
 
889 889
 			// loop through the errors to find matching localized message
890
-			foreach ( $card->errors as $error => $msg ) {
891
-				if ( isset( $localized_messages[ $error ] ) ) {
892
-					$error_msg = $localized_messages[ $error ];
890
+			foreach ($card->errors as $error => $msg) {
891
+				if (isset($localized_messages[$error])) {
892
+					$error_msg = $localized_messages[$error];
893 893
 				}
894 894
 			}
895 895
 
896
-			wc_add_notice( $error_msg, 'error' );
896
+			wc_add_notice($error_msg, 'error');
897 897
 			return;
898 898
 		}
899 899
 
900 900
 		return array(
901 901
 			'result'   => 'success',
902
-			'redirect' => wc_get_endpoint_url( 'payment-methods' ),
902
+			'redirect' => wc_get_endpoint_url('payment-methods'),
903 903
 		);
904 904
 	}
905 905
 
@@ -909,36 +909,36 @@  discard block
 block discarded – undo
909 909
 	 * @param  float $amount
910 910
 	 * @return bool
911 911
 	 */
912
-	public function process_refund( $order_id, $amount = null, $reason = '' ) {
913
-		$order = wc_get_order( $order_id );
912
+	public function process_refund($order_id, $amount = null, $reason = '') {
913
+		$order = wc_get_order($order_id);
914 914
 
915
-		if ( ! $order || ! $order->get_transaction_id() ) {
915
+		if ( ! $order || ! $order->get_transaction_id()) {
916 916
 			return false;
917 917
 		}
918 918
 
919 919
 		$body = array();
920 920
 
921
-		if ( ! is_null( $amount ) ) {
922
-			$body['amount']	= $this->get_stripe_amount( $amount );
921
+		if ( ! is_null($amount)) {
922
+			$body['amount'] = $this->get_stripe_amount($amount);
923 923
 		}
924 924
 
925
-		if ( $reason ) {
925
+		if ($reason) {
926 926
 			$body['metadata'] = array(
927 927
 				'reason'	=> $reason,
928 928
 			);
929 929
 		}
930 930
 
931
-		$this->log( "Info: Beginning refund for order $order_id for the amount of {$amount}" );
931
+		$this->log("Info: Beginning refund for order $order_id for the amount of {$amount}");
932 932
 
933
-		$response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' );
933
+		$response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds');
934 934
 
935
-		if ( is_wp_error( $response ) ) {
936
-			$this->log( 'Error: ' . $response->get_error_message() );
935
+		if (is_wp_error($response)) {
936
+			$this->log('Error: ' . $response->get_error_message());
937 937
 			return $response;
938
-		} elseif ( ! empty( $response->id ) ) {
939
-			$refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), wc_price( $response->amount / 100 ), $response->id, $reason );
940
-			$order->add_order_note( $refund_message );
941
-			$this->log( 'Success: ' . html_entity_decode( strip_tags( $refund_message ) ) );
938
+		} elseif ( ! empty($response->id)) {
939
+			$refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason);
940
+			$order->add_order_note($refund_message);
941
+			$this->log('Success: ' . html_entity_decode(strip_tags($refund_message)));
942 942
 			return true;
943 943
 		}
944 944
 	}
@@ -951,10 +951,10 @@  discard block
 block discarded – undo
951 951
 	 * @param int $order_id
952 952
 	 * @return null
953 953
 	 */
954
-	public function send_failed_order_email( $order_id ) {
954
+	public function send_failed_order_email($order_id) {
955 955
 		$emails = WC()->mailer()->get_emails();
956
-		if ( ! empty( $emails ) && ! empty( $order_id ) ) {
957
-			$emails['WC_Email_Failed_Order']->trigger( $order_id );
956
+		if ( ! empty($emails) && ! empty($order_id)) {
957
+			$emails['WC_Email_Failed_Order']->trigger($order_id);
958 958
 		}
959 959
 	}
960 960
 
@@ -966,9 +966,9 @@  discard block
 block discarded – undo
966 966
 	 *
967 967
 	 * @param string $message
968 968
 	 */
969
-	public function log( $message ) {
970
-		if ( $this->logging ) {
971
-			WC_Stripe::log( $message );
969
+	public function log($message) {
970
+		if ($this->logging) {
971
+			WC_Stripe::log($message);
972 972
 		}
973 973
 	}
974 974
 }
Please login to merge, or discard this patch.