Completed
Push — master ( a8a1a1...3b0043 )
by Roy
02:58
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +200 added lines, -200 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
 
@@ -47,9 +47,9 @@  discard block
 block discarded – undo
47 47
 	public function __construct() {
48 48
 		self::$_this = $this;
49 49
 
50
-		$this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
50
+		$this->_gateway_settings = get_option('woocommerce_stripe_settings', '');
51 51
 
52
-		$this->statement_descriptor = ! empty( $this->_gateway_settings['statement_descriptor'] ) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
52
+		$this->statement_descriptor = ! empty($this->_gateway_settings['statement_descriptor']) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
53 53
 
54 54
 		$this->init();
55 55
 	}
@@ -66,14 +66,14 @@  discard block
 block discarded – undo
66 66
 	 * @version 3.1.0
67 67
 	 */
68 68
 	public function init() {
69
-		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
70
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 20 );
71
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ) );
72
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
73
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
74
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
75
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
76
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
69
+		add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
70
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 20);
71
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'));
72
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
73
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
74
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
75
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
76
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
77 77
 	}
78 78
 
79 79
 	/**
@@ -84,47 +84,47 @@  discard block
 block discarded – undo
84 84
 	 * @version 3.1.0
85 85
 	 */
86 86
 	public static function process_apple_pay_domain() {
87
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_domain_nonce' ) ) {
88
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
87
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_domain_nonce')) {
88
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
89 89
 		}
90 90
 
91
-		$secret_key = wc_clean( $_POST['secret_key'] );
91
+		$secret_key = wc_clean($_POST['secret_key']);
92 92
 
93 93
 		try {
94
-			$path     = untrailingslashit( preg_replace( "!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME'] ) );
94
+			$path     = untrailingslashit(preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']));
95 95
 			$dir      = '.well-known';
96 96
 			$file     = 'apple-developer-merchantid-domain-association';
97 97
 			$fullpath = $path . '/' . $dir . '/' . $file;
98
-			$gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
98
+			$gateway_settings = get_option('woocommerce_stripe_settings', '');
99 99
 
100
-			if ( ! file_exists( $path . '/' . $dir ) || ! file_exists( $fullpath ) ) {
101
-				if ( ! mkdir( $path . '/' . $dir, 0755 ) ) {
102
-					throw new Exception( __( 'Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe' ) );
100
+			if ( ! file_exists($path . '/' . $dir) || ! file_exists($fullpath)) {
101
+				if ( ! mkdir($path . '/' . $dir, 0755)) {
102
+					throw new Exception(__('Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe'));
103 103
 				}
104 104
 
105
-				if ( ! file_exists( $path . '/' . $dir . '/' . 'apple-developer-merchantid-domain-association' ) ) {
106
-					if ( ! copy( WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath ) ) {
107
-						throw new Exception( __( 'Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe' ) );
105
+				if ( ! file_exists($path . '/' . $dir . '/' . 'apple-developer-merchantid-domain-association')) {
106
+					if ( ! copy(WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath)) {
107
+						throw new Exception(__('Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe'));
108 108
 					}
109 109
 				}
110 110
 			}
111 111
 
112 112
 			// At this point then the domain association folder and file should be available.
113 113
 			// Proceed to verify/and or verify again.
114
-			WC_Stripe_Apple_Pay::_register_apple_pay_domain( $secret_key );
114
+			WC_Stripe_Apple_Pay::_register_apple_pay_domain($secret_key);
115 115
 
116 116
 			// No errors to this point, verification success!
117 117
 			$gateway_settings['apple_pay_domain_set'] = 'yes';
118 118
 
119
-			update_option( 'woocommerce_stripe_settings', $gateway_settings );
119
+			update_option('woocommerce_stripe_settings', $gateway_settings);
120 120
 
121
-			wp_send_json( array( 'success' => true, 'message' => __( 'Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe' ) ) );
122
-		} catch ( Exception $e ) {
121
+			wp_send_json(array('success' => true, 'message' => __('Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe')));
122
+		} catch (Exception $e) {
123 123
 			$gateway_settings['apple_pay_domain_set'] = 'no';
124 124
 
125
-			update_option( 'woocommerce_stripe_settings', $gateway_settings );
125
+			update_option('woocommerce_stripe_settings', $gateway_settings);
126 126
 
127
-			wp_send_json( array( 'success' => false, 'message' => $e->getMessage() ) );
127
+			wp_send_json(array('success' => false, 'message' => $e->getMessage()));
128 128
 		}
129 129
 	}
130 130
 
@@ -135,9 +135,9 @@  discard block
 block discarded – undo
135 135
 	 * @version 3.1.0
136 136
 	 * @param string $secret_key
137 137
 	 */
138
-	private static function _register_apple_pay_domain( $secret_key = '' ) {
139
-		if ( empty( $secret_key ) ) {
140
-			throw new Exception( __( 'Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe' ) );
138
+	private static function _register_apple_pay_domain($secret_key = '') {
139
+		if (empty($secret_key)) {
140
+			throw new Exception(__('Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe'));
141 141
 		}
142 142
 
143 143
 		$endpoint = 'https://api.stripe.com/v1/apple_pay/domains';
@@ -151,13 +151,13 @@  discard block
 block discarded – undo
151 151
 			'Authorization' => 'Bearer ' . $secret_key,
152 152
 		);
153 153
 
154
-		$response = wp_remote_post( $endpoint, array(
154
+		$response = wp_remote_post($endpoint, array(
155 155
 			'headers' => $headers,
156
-			'body'    => http_build_query( $data ),
157
-		) );
156
+			'body'    => http_build_query($data),
157
+		));
158 158
 
159
-		if ( 200 !== $response['response']['code'] ) {
160
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $response['response']['message'] ) );
159
+		if (200 !== $response['response']['code']) {
160
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $response['response']['message']));
161 161
 		}
162 162
 	}
163 163
 
@@ -165,16 +165,16 @@  discard block
 block discarded – undo
165 165
 	 * Filters the gateway title to reflect Apple Pay.
166 166
 	 *
167 167
 	 */
168
-	public function filter_gateway_title( $title, $id ) {
168
+	public function filter_gateway_title($title, $id) {
169 169
 		global $post;
170 170
 
171
-		if ( ! is_object( $post ) ) {
171
+		if ( ! is_object($post)) {
172 172
 			return $title;
173 173
 		}
174 174
 
175
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
175
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
176 176
 
177
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
177
+		if ('stripe' === $id && ! empty($method_title)) {
178 178
 			return $method_title;
179 179
 		}
180 180
 
@@ -188,38 +188,38 @@  discard block
 block discarded – undo
188 188
 	 * @version 3.1.0
189 189
 	 */
190 190
 	public function payment_scripts() {
191
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
191
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
192 192
 			return;
193 193
 		}
194 194
 
195
-		if ( ! $this->is_supported_product_type() ) {
195
+		if ( ! $this->is_supported_product_type()) {
196 196
 			return;
197 197
 		}
198 198
 
199
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
199
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
200 200
 
201
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
201
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
202 202
 
203
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
204
-		wp_enqueue_script( 'woocommerce_stripe_apple_pay', plugins_url( 'assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true );
203
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
204
+		wp_enqueue_script('woocommerce_stripe_apple_pay', plugins_url('assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true);
205 205
 
206 206
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
207 207
 
208 208
 		$stripe_params = array(
209 209
 			'key'                                           => $publishable_key,
210 210
 			'currency_code'                                 => get_woocommerce_currency(),
211
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
211
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
212 212
 			'label'                                         => $this->statement_descriptor,
213
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
214
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
215
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
216
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
217
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
213
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
214
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
215
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
216
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
217
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
218 218
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
219 219
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
220 220
 		);
221 221
 
222
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
222
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
223 223
 	}
224 224
 
225 225
 	/**
@@ -227,8 +227,8 @@  discard block
 block discarded – undo
227 227
 	 *
228 228
 	 */
229 229
 	public function is_supported_product_type() {
230
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
231
-			if ( 'subscription' === $values['data']->product_type ) {
230
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
231
+			if ('subscription' === $values['data']->product_type) {
232 232
 				return false;
233 233
 			}
234 234
 		}
@@ -251,19 +251,19 @@  discard block
 block discarded – undo
251 251
 		 */
252 252
 		if (
253 253
 			'yes' !== $this->_gateway_settings['apple_pay']
254
-			|| ! isset( $gateways['stripe'] )
254
+			|| ! isset($gateways['stripe'])
255 255
 		) {
256 256
 			return;
257 257
 		}
258 258
 
259
-		if ( ! $this->is_supported_product_type() ) {
259
+		if ( ! $this->is_supported_product_type()) {
260 260
 			return;
261 261
 		}
262 262
 
263
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
264
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
263
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
264
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
265 265
 		?>
266
-		<button class="apple-pay-button" lang="<?php echo esc_attr( $button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button>
266
+		<button class="apple-pay-button" lang="<?php echo esc_attr($button_lang); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($apple_pay_button); ?>;"></button>
267 267
 		<?php
268 268
 	}
269 269
 
@@ -274,11 +274,11 @@  discard block
 block discarded – undo
274 274
 	 * @version 3.1.0
275 275
 	 */
276 276
 	public function generate_apple_pay_cart() {
277
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
278
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
277
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
278
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
279 279
 		}
280 280
 
281
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
281
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
282 282
 	}
283 283
 
284 284
 	/**
@@ -288,29 +288,29 @@  discard block
 block discarded – undo
288 288
 	 * @version 3.1.0
289 289
 	 * @param array $address
290 290
 	 */
291
-	public function calculate_shipping( $address = array() ) {
292
-		$country  = strtoupper( $address['countryCode'] );
293
-		$state    = strtoupper( $address['administrativeArea'] );
291
+	public function calculate_shipping($address = array()) {
292
+		$country  = strtoupper($address['countryCode']);
293
+		$state    = strtoupper($address['administrativeArea']);
294 294
 		$postcode = $address['postalCode'];
295 295
 		$city     = $address['locality'];
296 296
 
297 297
 		WC()->shipping->reset_shipping();
298 298
 
299
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
300
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
301
-		} elseif ( $postcode ) {
302
-			$postcode = wc_format_postcode( $postcode, $country );
299
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
300
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
301
+		} elseif ($postcode) {
302
+			$postcode = wc_format_postcode($postcode, $country);
303 303
 		}
304 304
 
305
-		if ( $country ) {
306
-			WC()->customer->set_location( $country, $state, $postcode, $city );
307
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
305
+		if ($country) {
306
+			WC()->customer->set_location($country, $state, $postcode, $city);
307
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
308 308
 		} else {
309 309
 			WC()->customer->set_to_base();
310 310
 			WC()->customer->set_shipping_to_base();
311 311
 		}
312 312
 
313
-		WC()->customer->calculated_shipping( true );
313
+		WC()->customer->calculated_shipping(true);
314 314
 
315 315
 		/**
316 316
 		 * Set the shipping package.
@@ -332,17 +332,17 @@  discard block
 block discarded – undo
332 332
 		$packages[0]['destination']['postcode']  = $postcode;
333 333
 		$packages[0]['destination']['city']      = $city;
334 334
 
335
-		foreach ( WC()->cart->get_cart() as $item ) {
336
-			if ( $item['data']->needs_shipping() ) {
337
-				if ( isset( $item['line_total'] ) ) {
335
+		foreach (WC()->cart->get_cart() as $item) {
336
+			if ($item['data']->needs_shipping()) {
337
+				if (isset($item['line_total'])) {
338 338
 					$packages[0]['contents_cost'] += $item['line_total'];
339 339
 				}
340 340
 			}
341 341
 		}
342 342
 
343
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
343
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
344 344
 
345
-		WC()->shipping->calculate_shipping( $packages );
345
+		WC()->shipping->calculate_shipping($packages);
346 346
 	}
347 347
 
348 348
 	/**
@@ -352,18 +352,18 @@  discard block
 block discarded – undo
352 352
 	 * @version 3.1.0
353 353
 	 */
354 354
 	public function get_shipping_methods() {
355
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
356
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
355
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
356
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
357 357
 		}
358 358
 
359
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
360
-			define( 'WOOCOMMERCE_CART', true );
359
+		if ( ! defined('WOOCOMMERCE_CART')) {
360
+			define('WOOCOMMERCE_CART', true);
361 361
 		}
362 362
 
363 363
 		try {
364
-			$address = array_map( 'wc_clean', $_POST['address'] );
364
+			$address = array_map('wc_clean', $_POST['address']);
365 365
 
366
-			$this->calculate_shipping( $address );
366
+			$this->calculate_shipping($address);
367 367
 
368 368
 			// Set the shipping options.
369 369
 			$currency = get_woocommerce_currency();
@@ -371,13 +371,13 @@  discard block
 block discarded – undo
371 371
 
372 372
 			$packages = WC()->shipping->get_packages();
373 373
 
374
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
375
-				foreach ( $packages as $package_key => $package ) {
376
-					if ( empty( $package['rates'] ) ) {
377
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
374
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
375
+				foreach ($packages as $package_key => $package) {
376
+					if (empty($package['rates'])) {
377
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
378 378
 					}
379 379
 
380
-					foreach ( $package['rates'] as $key => $rate ) {
380
+					foreach ($package['rates'] as $key => $rate) {
381 381
 						$data[] = array(
382 382
 							'id'       => $rate->id,
383 383
 							'label'    => $rate->label,
@@ -391,16 +391,16 @@  discard block
 block discarded – undo
391 391
 				}
392 392
 
393 393
 				// Auto select the first shipping method.
394
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
394
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
395 395
 
396 396
 				WC()->cart->calculate_totals();
397 397
 
398
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
398
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
399 399
 			} else {
400
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
400
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
401 401
 			}
402
-		} catch ( Exception $e ) {
403
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
402
+		} catch (Exception $e) {
403
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
404 404
 		}
405 405
 	}
406 406
 
@@ -411,29 +411,29 @@  discard block
 block discarded – undo
411 411
 	 * @version 3.1.0
412 412
 	 */
413 413
 	public function update_shipping_method() {
414
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
415
-			define( 'WOOCOMMERCE_CART', true );
414
+		if ( ! defined('WOOCOMMERCE_CART')) {
415
+			define('WOOCOMMERCE_CART', true);
416 416
 		}
417 417
 
418
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
419
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
418
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
419
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
420 420
 		}
421 421
 
422
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
422
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
423 423
 
424
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
424
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
425 425
 
426 426
 		WC()->cart->calculate_totals();
427 427
 
428 428
 		// Send back the new cart total.
429 429
 		$currency  = get_woocommerce_currency();
430
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
430
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
431 431
 		$data      = array(
432 432
 			'total' => WC()->cart->total,
433 433
 		);
434 434
 
435 435
 		// Include fees and taxes as displayItems.
436
-		foreach ( WC()->cart->fees as $key => $fee ) {
436
+		foreach (WC()->cart->fees as $key => $fee) {
437 437
 			$data['items'][] = array(
438 438
 				'label'  => $fee->name,
439 439
 				'amount' => array(
@@ -442,9 +442,9 @@  discard block
 block discarded – undo
442 442
 				),
443 443
 			);
444 444
 		}
445
-		if ( 0 < $tax_total ) {
445
+		if (0 < $tax_total) {
446 446
 			$data['items'][] = array(
447
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
447
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
448 448
 				'amount' => array(
449 449
 					'currency' => $currency,
450 450
 					'value'    => $tax_total,
@@ -452,7 +452,7 @@  discard block
 block discarded – undo
452 452
 			);
453 453
 		}
454 454
 
455
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
455
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
456 456
 	}
457 457
 
458 458
 	/**
@@ -463,35 +463,35 @@  discard block
 block discarded – undo
463 463
 	 * @version 3.1.0
464 464
 	 */
465 465
 	public function process_apple_pay() {
466
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
467
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
466
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
467
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
468 468
 		}
469 469
 
470 470
 		try {
471
-			$result = array_map( 'wc_clean', $_POST['result'] );
471
+			$result = array_map('wc_clean', $_POST['result']);
472 472
 
473
-			$order = $this->create_order( $result );
473
+			$order = $this->create_order($result);
474 474
 
475 475
 			// Handle payment.
476
-			if ( $order->get_total() > 0 ) {
476
+			if ($order->get_total() > 0) {
477 477
 
478
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
479
-					return new WP_Error( 'stripe_error', 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 ) ) );
478
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
479
+					return new WP_Error('stripe_error', 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)));
480 480
 				}
481 481
 
482
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
482
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
483 483
 
484 484
 				// Make the request.
485
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
485
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
486 486
 
487
-				if ( is_wp_error( $response ) ) {
487
+				if (is_wp_error($response)) {
488 488
 					$localized_messages = $this->get_localized_messages();
489 489
 
490
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
490
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
491 491
 				}
492 492
 
493 493
 				// Process valid response.
494
-				$this->process_response( $response, $order );
494
+				$this->process_response($response, $order);
495 495
 			} else {
496 496
 				$order->payment_complete();
497 497
 			}
@@ -499,24 +499,24 @@  discard block
 block discarded – undo
499 499
 			// Remove cart.
500 500
 			WC()->cart->empty_cart();
501 501
 
502
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
503
-			update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
502
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
503
+			update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
504 504
 
505 505
 			// Return thank you page redirect.
506
-			wp_send_json( array(
506
+			wp_send_json(array(
507 507
 				'success'  => 'true',
508
-				'redirect' => $this->get_return_url( $order ),
509
-			) );
508
+				'redirect' => $this->get_return_url($order),
509
+			));
510 510
 
511
-		} catch ( Exception $e ) {
512
-			WC()->session->set( 'refresh_totals', true );
513
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
511
+		} catch (Exception $e) {
512
+			WC()->session->set('refresh_totals', true);
513
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
514 514
 
515
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
516
-				$this->send_failed_order_email( $order->id );
515
+			if ($order->has_status(array('pending', 'failed'))) {
516
+				$this->send_failed_order_email($order->id);
517 517
 			}
518 518
 
519
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
519
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
520 520
 		}
521 521
 	}
522 522
 
@@ -526,14 +526,14 @@  discard block
 block discarded – undo
526 526
 	 * @param string $source token
527 527
 	 * @return array()
528 528
 	 */
529
-	protected function generate_payment_request( $order, $source ) {
529
+	protected function generate_payment_request($order, $source) {
530 530
 		$post_data                = array();
531
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
532
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
533
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
531
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
532
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
533
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
534 534
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
535 535
 
536
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
536
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
537 537
 			$post_data['receipt_email'] = $order->billing_email;
538 538
 		}
539 539
 
@@ -548,7 +548,7 @@  discard block
 block discarded – undo
548 548
 		 * @param WC_Order $order
549 549
 		 * @param object $source
550 550
 		 */
551
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
551
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
552 552
 	}
553 553
 
554 554
 	/**
@@ -557,14 +557,14 @@  discard block
 block discarded – undo
557 557
 	 * @since 3.1.0
558 558
 	 * @version 3.1.0
559 559
 	 */
560
-	public function build_shipping_methods( $shipping_methods ) {
561
-		if ( empty( $shipping_methods ) ) {
560
+	public function build_shipping_methods($shipping_methods) {
561
+		if (empty($shipping_methods)) {
562 562
 			return array();
563 563
 		}
564 564
 
565 565
 		$shipping = array();
566 566
 
567
-		foreach ( $shipping_methods as $method ) {
567
+		foreach ($shipping_methods as $method) {
568 568
 			$shipping[] = array(
569 569
 				'label'      => $method['label'],
570 570
 				'detail'     => '',
@@ -583,53 +583,53 @@  discard block
 block discarded – undo
583 583
 	 * @version 3.1.0
584 584
 	 */
585 585
 	public function build_line_items() {
586
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
587
-			define( 'WOOCOMMERCE_CART', true );
586
+		if ( ! defined('WOOCOMMERCE_CART')) {
587
+			define('WOOCOMMERCE_CART', true);
588 588
 		}
589 589
 
590
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
590
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
591 591
 
592 592
 		$items = array();
593 593
 
594
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
595
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
594
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
595
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
596 596
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
597 597
 
598 598
 			$item = array(
599 599
 				'type'   => 'final',
600 600
 				'label'  => $values['data']->post->post_title . $quantity_label,
601
-				'amount' => wc_format_decimal( $amount, $decimals ),
601
+				'amount' => wc_format_decimal($amount, $decimals),
602 602
 			);
603 603
 
604 604
 			$items[] = $item;
605 605
 		}
606 606
 
607
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
608
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
609
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
610
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
611
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
607
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
608
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
609
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
610
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
611
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
612 612
 
613
-		if ( wc_tax_enabled() ) {
613
+		if (wc_tax_enabled()) {
614 614
 			$items[] = array(
615 615
 				'type'   => 'final',
616
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
616
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
617 617
 				'amount' => $tax,
618 618
 			);
619 619
 		}
620 620
 
621
-		if ( WC()->cart->needs_shipping() ) {
621
+		if (WC()->cart->needs_shipping()) {
622 622
 			$items[] = array(
623 623
 				'type'   => 'final',
624
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
624
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
625 625
 				'amount' => $shipping,
626 626
 			);
627 627
 		}
628 628
 
629
-		if ( WC()->cart->has_discount() ) {
629
+		if (WC()->cart->has_discount()) {
630 630
 			$items[] = array(
631 631
 				'type'   => 'final',
632
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
632
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
633 633
 				'amount' => $discounts,
634 634
 			);
635 635
 		}
@@ -645,24 +645,24 @@  discard block
 block discarded – undo
645 645
 	 * @param array $data
646 646
 	 * @return object $order
647 647
 	 */
648
-	public function create_order( $data = array() ) {
649
-		if ( empty( $data ) ) {
650
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
648
+	public function create_order($data = array()) {
649
+		if (empty($data)) {
650
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
651 651
 		}
652 652
 
653 653
 		$order = wc_create_order();
654 654
 
655
-		if ( is_wp_error( $order ) ) {
656
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
657
-		} elseif ( false === $order ) {
658
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
655
+		if (is_wp_error($order)) {
656
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
657
+		} elseif (false === $order) {
658
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
659 659
 		} else {
660 660
 			$order_id = $order->id;
661
-			do_action( 'woocommerce_new_order', $order_id );
661
+			do_action('woocommerce_new_order', $order_id);
662 662
 		}
663 663
 
664 664
 		// Store the line items to the new/resumed order
665
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
665
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
666 666
 			$item_id = $order->add_product(
667 667
 				$values['data'],
668 668
 				$values['quantity'],
@@ -678,47 +678,47 @@  discard block
 block discarded – undo
678 678
 				)
679 679
 			);
680 680
 
681
-			if ( ! $item_id ) {
682
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
681
+			if ( ! $item_id) {
682
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
683 683
 			}
684 684
 
685 685
 			// Allow plugins to add order item meta
686
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
686
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
687 687
 		}
688 688
 
689 689
 		// Store fees
690
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
691
-			$item_id = $order->add_fee( $fee );
690
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
691
+			$item_id = $order->add_fee($fee);
692 692
 
693
-			if ( ! $item_id ) {
694
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
693
+			if ( ! $item_id) {
694
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
695 695
 			}
696 696
 
697 697
 			// Allow plugins to add order item meta to fees
698
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
698
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
699 699
 		}
700 700
 
701 701
 		// Store tax rows
702
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
703
-			if ( $tax_rate_id && ! $order->add_tax( $tax_rate_id, WC()->cart->get_tax_amount( $tax_rate_id ), WC()->cart->get_shipping_tax_amount( $tax_rate_id ) ) && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
704
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
702
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
703
+			if ($tax_rate_id && ! $order->add_tax($tax_rate_id, WC()->cart->get_tax_amount($tax_rate_id), WC()->cart->get_shipping_tax_amount($tax_rate_id)) && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
704
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
705 705
 			}
706 706
 		}
707 707
 
708 708
 		// Store coupons
709
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
710
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
711
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
709
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
710
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
711
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
712 712
 			}
713 713
 		}
714 714
 
715 715
 		// Billing address
716 716
 		$billing_address = array();
717
-		if ( ! empty( $data['token']['card'] ) ) {
717
+		if ( ! empty($data['token']['card'])) {
718 718
 			// Name from Stripe is a full name string.
719
-			$name                          = explode( ' ', $data['token']['card']['name'] );
720
-			$lastname                      = array_pop( $name );
721
-			$firstname                     = implode( ' ', $name );
719
+			$name                          = explode(' ', $data['token']['card']['name']);
720
+			$lastname                      = array_pop($name);
721
+			$firstname                     = implode(' ', $name);
722 722
 			$billing_address['first_name'] = $firstname;
723 723
 			$billing_address['last_name']  = $lastname;
724 724
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -733,7 +733,7 @@  discard block
 block discarded – undo
733 733
 
734 734
 		// Shipping address.
735 735
 		$shipping_address = array();
736
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
736
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
737 737
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
738 738
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
739 739
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -744,7 +744,7 @@  discard block
 block discarded – undo
744 744
 			$shipping_address['city']       = $data['shippingContact']['locality'];
745 745
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
746 746
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
747
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
747
+		} elseif ( ! empty($data['shippingContact'])) {
748 748
 			$shipping_address['first_name'] = $firstname;
749 749
 			$shipping_address['last_name']  = $lastname;
750 750
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -757,34 +757,34 @@  discard block
 block discarded – undo
757 757
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
758 758
 		}
759 759
 
760
-		$order->set_address( $billing_address, 'billing' );
761
-		$order->set_address( $shipping_address, 'shipping' );
760
+		$order->set_address($billing_address, 'billing');
761
+		$order->set_address($shipping_address, 'shipping');
762 762
 
763
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
763
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
764 764
 
765 765
 		// Get the rate object selected by user.
766
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
767
-			foreach ( $package['rates'] as $key => $rate ) {
766
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
767
+			foreach ($package['rates'] as $key => $rate) {
768 768
 				// Loop through user chosen shipping methods.
769
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
770
-					if ( $method === $key ) {
771
-						$order->add_shipping( $rate );
769
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
770
+					if ($method === $key) {
771
+						$order->add_shipping($rate);
772 772
 					}
773 773
 				}
774 774
 			}
775 775
 		}
776 776
 
777 777
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
778
-		$order->set_payment_method( $available_gateways['stripe'] );
779
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
780
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
781
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
782
-		$order->set_total( WC()->cart->tax_total, 'tax' );
783
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
784
-		$order->set_total( WC()->cart->total );
778
+		$order->set_payment_method($available_gateways['stripe']);
779
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
780
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
781
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
782
+		$order->set_total(WC()->cart->tax_total, 'tax');
783
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
784
+		$order->set_total(WC()->cart->total);
785 785
 
786 786
 		// If we got here, the order was created without problems!
787
-		wc_transaction_query( 'commit' );
787
+		wc_transaction_query('commit');
788 788
 
789 789
 		return $order;
790 790
 	}
Please login to merge, or discard this patch.
includes/class-wc-gateway-stripe.php 1 patch
Spacing   +250 added lines, -250 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,41 +149,41 @@  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' );
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
-		$this->allow_remember_me      = 'yes' === $this->get_option( 'allow_remember_me', 'no' );
170
-
171
-		if ( $this->stripe_checkout ) {
172
-			$this->order_button_text = __( 'Continue to payment', 'woocommerce-gateway-stripe' );
173
-		}
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 );
178
-		}
179
-
180
-		WC_Stripe_API::set_secret_key( $this->secret_key );
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');
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
+		$this->allow_remember_me      = 'yes' === $this->get_option('allow_remember_me', 'no');
170
+
171
+		if ($this->stripe_checkout) {
172
+			$this->order_button_text = __('Continue to payment', 'woocommerce-gateway-stripe');
173
+		}
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);
178
+		}
179
+
180
+		WC_Stripe_API::set_secret_key($this->secret_key);
181 181
 
182 182
 		// Hooks.
183
-		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
184
-		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
185
-		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
186
-		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
183
+		add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
184
+		add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
185
+		add_action('admin_notices', array($this, 'admin_notices'));
186
+		add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
187 187
 	}
188 188
 
189 189
 	/**
@@ -197,15 +197,15 @@  discard block
 block discarded – undo
197 197
 		?>
198 198
 		<tr valign="top">
199 199
 			<th scope="row" class="titledesc">
200
-				<label for="wc-to-square"><?php esc_html_e( 'Apple Pay Domain Verification', 'woocommerce-gateway-stripe' ); ?></label>
201
-				<?php echo wc_help_tip( __( 'Click button to verify your domain with Apple Pay. This is required.', 'woocommerce-gateway-stripe' ) ); ?>
200
+				<label for="wc-to-square"><?php esc_html_e('Apple Pay Domain Verification', 'woocommerce-gateway-stripe'); ?></label>
201
+				<?php echo wc_help_tip(__('Click button to verify your domain with Apple Pay. This is required.', 'woocommerce-gateway-stripe')); ?>
202 202
 			</th>
203 203
 			<td class="forminp">
204
-				<?php if ( ! empty( $this->secret_key ) && ! $this->apple_pay_domain_set ) { ?>
205
-					<a href="#" id="wc-gateway-stripe-apple-pay-domain" class="button button-secondary"><?php esc_html_e( 'Verify Domain', 'woocommerce-gateway-stripe' ); ?></a>
204
+				<?php if ( ! empty($this->secret_key) && ! $this->apple_pay_domain_set) { ?>
205
+					<a href="#" id="wc-gateway-stripe-apple-pay-domain" class="button button-secondary"><?php esc_html_e('Verify Domain', 'woocommerce-gateway-stripe'); ?></a>
206 206
 				<?php } else { ?>
207
-					<a href="#" id="wc-gateway-stripe-apple-pay-domain" class="button button-secondary"><?php esc_html_e( 'Re-verify Domain', 'woocommerce-gateway-stripe' ); ?></a>
208
-					<p class="wc-stripe-apple-pay-domain-message" style="color:green;"><?php esc_html_e( 'Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe' ); ?></p>
207
+					<a href="#" id="wc-gateway-stripe-apple-pay-domain" class="button button-secondary"><?php esc_html_e('Re-verify Domain', 'woocommerce-gateway-stripe'); ?></a>
208
+					<p class="wc-stripe-apple-pay-domain-message" style="color:green;"><?php esc_html_e('Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe'); ?></p>
209 209
 				<?php } ?>
210 210
 				<input type="hidden" name="woocommerce_stripe_apple_pay_domain_set" class="wc-gateway-stripe-apple-pay-domain-set" />
211 211
 			</td>
@@ -221,8 +221,8 @@  discard block
 block discarded – undo
221 221
 	 * @version 3.1.0
222 222
 	 * @param string $key
223 223
 	 */
224
-	public function validate_apple_pay_domain_field( $key ) {
225
-		$value = isset( $_POST['woocommerce_stripe_apple_pay_domain_set'] ) ? 'yes' : 'no';
224
+	public function validate_apple_pay_domain_field($key) {
225
+		$value = isset($_POST['woocommerce_stripe_apple_pay_domain_set']) ? 'yes' : 'no';
226 226
 
227 227
 		return $value;
228 228
 	}
@@ -234,24 +234,24 @@  discard block
 block discarded – undo
234 234
 	 * @return string
235 235
 	 */
236 236
 	public function get_icon() {
237
-		$ext   = version_compare( WC()->version, '2.6', '>=' ) ? '.svg' : '.png';
238
-		$style = version_compare( WC()->version, '2.6', '>=' ) ? 'style="margin-left: 0.3em"' : '';
237
+		$ext   = version_compare(WC()->version, '2.6', '>=') ? '.svg' : '.png';
238
+		$style = version_compare(WC()->version, '2.6', '>=') ? 'style="margin-left: 0.3em"' : '';
239 239
 
240
-		$icon  = '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext ) . '" alt="Visa" width="32" ' . $style . ' />';
241
-		$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext ) . '" alt="Mastercard" width="32" ' . $style . ' />';
242
-		$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext ) . '" alt="Amex" width="32" ' . $style . ' />';
240
+		$icon  = '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext) . '" alt="Visa" width="32" ' . $style . ' />';
241
+		$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext) . '" alt="Mastercard" width="32" ' . $style . ' />';
242
+		$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext) . '" alt="Amex" width="32" ' . $style . ' />';
243 243
 
244
-		if ( 'USD' === get_woocommerce_currency() ) {
245
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext ) . '" alt="Discover" width="32" ' . $style . ' />';
246
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext ) . '" alt="JCB" width="32" ' . $style . ' />';
247
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext ) . '" alt="Diners" width="32" ' . $style . ' />';
244
+		if ('USD' === get_woocommerce_currency()) {
245
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext) . '" alt="Discover" width="32" ' . $style . ' />';
246
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext) . '" alt="JCB" width="32" ' . $style . ' />';
247
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext) . '" alt="Diners" width="32" ' . $style . ' />';
248 248
 		}
249 249
 
250
-		if ( $this->bitcoin && $this->stripe_checkout ) {
251
-			$icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE ) ) . '" alt="Bitcoin" width="24" ' . $style . ' />';
250
+		if ($this->bitcoin && $this->stripe_checkout) {
251
+			$icon .= '<img src="' . WC_HTTPS::force_https_url(plugins_url('/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE)) . '" alt="Bitcoin" width="24" ' . $style . ' />';
252 252
 		}
253 253
 
254
-		return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
254
+		return apply_filters('woocommerce_gateway_icon', $icon, $this->id);
255 255
 	}
256 256
 
257 257
 	/**
@@ -262,11 +262,11 @@  discard block
 block discarded – undo
262 262
 	 *
263 263
 	 * @return float|int
264 264
 	 */
265
-	public function get_stripe_amount( $total, $currency = '' ) {
266
-		if ( ! $currency ) {
265
+	public function get_stripe_amount($total, $currency = '') {
266
+		if ( ! $currency) {
267 267
 			$currency = get_woocommerce_currency();
268 268
 		}
269
-		switch ( strtoupper( $currency ) ) {
269
+		switch (strtoupper($currency)) {
270 270
 			// Zero decimal currencies.
271 271
 			case 'BIF' :
272 272
 			case 'CLP' :
@@ -283,10 +283,10 @@  discard block
 block discarded – undo
283 283
 			case 'XAF' :
284 284
 			case 'XOF' :
285 285
 			case 'XPF' :
286
-				$total = absint( $total );
286
+				$total = absint($total);
287 287
 				break;
288 288
 			default :
289
-				$total = round( $total, 2 ) * 100; // In cents.
289
+				$total = round($total, 2) * 100; // In cents.
290 290
 				break;
291 291
 		}
292 292
 		return $total;
@@ -296,13 +296,13 @@  discard block
 block discarded – undo
296 296
 	 * Check if SSL is enabled and notify the user
297 297
 	 */
298 298
 	public function admin_notices() {
299
-		if ( 'no' === $this->enabled ) {
299
+		if ('no' === $this->enabled) {
300 300
 			return;
301 301
 		}
302 302
 
303 303
 		// Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected.
304
-		if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) {
305
-			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>';
304
+		if ((function_exists('wc_site_is_https') && ! wc_site_is_https()) && ('no' === get_option('woocommerce_force_ssl_checkout') && ! class_exists('WordPressHTTPS'))) {
305
+			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>';
306 306
 		}
307 307
 	}
308 308
 
@@ -310,11 +310,11 @@  discard block
 block discarded – undo
310 310
 	 * Check if this gateway is enabled
311 311
 	 */
312 312
 	public function is_available() {
313
-		if ( 'yes' === $this->enabled ) {
314
-			if ( ! $this->testmode && is_checkout() && ! is_ssl() ) {
313
+		if ('yes' === $this->enabled) {
314
+			if ( ! $this->testmode && is_checkout() && ! is_ssl()) {
315 315
 				return false;
316 316
 			}
317
-			if ( ! $this->secret_key || ! $this->publishable_key ) {
317
+			if ( ! $this->secret_key || ! $this->publishable_key) {
318 318
 				return false;
319 319
 			}
320 320
 			return true;
@@ -326,7 +326,7 @@  discard block
 block discarded – undo
326 326
 	 * Initialise Gateway Settings Form Fields
327 327
 	 */
328 328
 	public function init_form_fields() {
329
-		$this->form_fields = include( 'settings-stripe.php' );
329
+		$this->form_fields = include('settings-stripe.php');
330 330
 	}
331 331
 
332 332
 	/**
@@ -334,54 +334,54 @@  discard block
 block discarded – undo
334 334
 	 */
335 335
 	public function payment_fields() {
336 336
 		$user                 = wp_get_current_user();
337
-		$display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards;
337
+		$display_tokenization = $this->supports('tokenization') && is_checkout() && $this->saved_cards;
338 338
 		$total                = WC()->cart->total;
339 339
 
340 340
 		// If paying from order, we need to get total from order not cart.
341
-		if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) ) {
342
-			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
341
+		if (isset($_GET['pay_for_order']) && isset($_GET['key'])) {
342
+			$order = wc_get_order(wc_get_order_id_by_order_key(wc_clean($_GET['key'])));
343 343
 			$total = $order->get_total();
344 344
 		}
345 345
 
346
-		if ( $user->ID ) {
347
-			$user_email = get_user_meta( $user->ID, 'billing_email', true );
346
+		if ($user->ID) {
347
+			$user_email = get_user_meta($user->ID, 'billing_email', true);
348 348
 			$user_email = $user_email ? $user_email : $user->user_email;
349 349
 		} else {
350 350
 			$user_email = '';
351 351
 		}
352 352
 
353
-		if ( is_add_payment_method_page() ) {
354
-			$pay_button_text = __( 'Add Card', 'woocommerce-gateway-stripe' );
353
+		if (is_add_payment_method_page()) {
354
+			$pay_button_text = __('Add Card', 'woocommerce-gateway-stripe');
355 355
 		} else {
356 356
 			$pay_button_text = '';
357 357
 		}
358 358
 
359 359
 		echo '<div
360 360
 			id="stripe-payment-data"
361
-			data-panel-label="' . esc_attr( $pay_button_text ) . '"
361
+			data-panel-label="' . esc_attr($pay_button_text) . '"
362 362
 			data-description=""
363
-			data-email="' . esc_attr( $user_email ) . '"
364
-			data-amount="' . esc_attr( $this->get_stripe_amount( $total ) ) . '"
365
-			data-name="' . esc_attr( $this->statement_descriptor ) . '"
366
-			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '"
367
-			data-image="' . esc_attr( $this->stripe_checkout_image ) . '"
368
-			data-bitcoin="' . esc_attr( $this->bitcoin ? 'true' : 'false' ) . '"
369
-			data-locale="' . esc_attr( $this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en' ) . '"
370
-			data-allow-remember-me="' . esc_attr( $this->allow_remember_me ? 'true' : 'false' ) . '">';
363
+			data-email="' . esc_attr($user_email) . '"
364
+			data-amount="' . esc_attr($this->get_stripe_amount($total)) . '"
365
+			data-name="' . esc_attr($this->statement_descriptor) . '"
366
+			data-currency="' . esc_attr(strtolower(get_woocommerce_currency())) . '"
367
+			data-image="' . esc_attr($this->stripe_checkout_image) . '"
368
+			data-bitcoin="' . esc_attr($this->bitcoin ? 'true' : 'false') . '"
369
+			data-locale="' . esc_attr($this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en') . '"
370
+			data-allow-remember-me="' . esc_attr($this->allow_remember_me ? 'true' : 'false') . '">';
371 371
 
372
-		if ( $this->description ) {
373
-			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
372
+		if ($this->description) {
373
+			echo apply_filters('wc_stripe_description', wpautop(wp_kses_post($this->description)));
374 374
 		}
375 375
 
376
-		if ( $display_tokenization ) {
376
+		if ($display_tokenization) {
377 377
 			$this->tokenization_script();
378 378
 			$this->saved_payment_methods();
379 379
 		}
380 380
 
381
-		if ( ! $this->stripe_checkout ) {
381
+		if ( ! $this->stripe_checkout) {
382 382
 			$this->form();
383 383
 
384
-			if ( $display_tokenization ) {
384
+			if ($display_tokenization) {
385 385
 				$this->save_payment_method_checkbox();
386 386
 			}
387 387
 		}
@@ -397,20 +397,20 @@  discard block
 block discarded – undo
397 397
 	 * @return array
398 398
 	 */
399 399
 	public function get_localized_messages() {
400
-		return apply_filters( 'wc_stripe_localized_messages', array(
401
-			'invalid_number'        => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ),
402
-			'invalid_expiry_month'  => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ),
403
-			'invalid_expiry_year'   => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ),
404
-			'invalid_cvc'           => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ),
405
-			'incorrect_number'      => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ),
406
-			'expired_card'          => __( 'The card has expired.', 'woocommerce-gateway-stripe' ),
407
-			'incorrect_cvc'         => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ),
408
-			'incorrect_zip'         => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ),
409
-			'card_declined'         => __( 'The card was declined.', 'woocommerce-gateway-stripe' ),
410
-			'missing'               => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ),
411
-			'processing_error'      => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ),
412
-			'invalid_request_error' => __( 'Could not find payment information.', 'woocommerce-gateway-stripe' ),
413
-		) );
400
+		return apply_filters('wc_stripe_localized_messages', array(
401
+			'invalid_number'        => __('The card number is not a valid credit card number.', 'woocommerce-gateway-stripe'),
402
+			'invalid_expiry_month'  => __('The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe'),
403
+			'invalid_expiry_year'   => __('The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe'),
404
+			'invalid_cvc'           => __('The card\'s security code is invalid.', 'woocommerce-gateway-stripe'),
405
+			'incorrect_number'      => __('The card number is incorrect.', 'woocommerce-gateway-stripe'),
406
+			'expired_card'          => __('The card has expired.', 'woocommerce-gateway-stripe'),
407
+			'incorrect_cvc'         => __('The card\'s security code is incorrect.', 'woocommerce-gateway-stripe'),
408
+			'incorrect_zip'         => __('The card\'s zip code failed validation.', 'woocommerce-gateway-stripe'),
409
+			'card_declined'         => __('The card was declined.', 'woocommerce-gateway-stripe'),
410
+			'missing'               => __('There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe'),
411
+			'processing_error'      => __('An error occurred while processing the card.', 'woocommerce-gateway-stripe'),
412
+			'invalid_request_error' => __('Could not find payment information.', 'woocommerce-gateway-stripe'),
413
+		));
414 414
 	}
415 415
 
416 416
 	/**
@@ -420,28 +420,28 @@  discard block
 block discarded – undo
420 420
 	 * @version 3.1.0
421 421
 	 */
422 422
 	public function admin_scripts() {
423
-		if ( 'woocommerce_page_wc-settings' !== get_current_screen()->id ) {
423
+		if ('woocommerce_page_wc-settings' !== get_current_screen()->id) {
424 424
 			return;
425 425
 		}
426 426
 
427
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
427
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
428 428
 
429
-		wp_enqueue_script( 'woocommerce_stripe_admin', plugins_url( 'assets/js/stripe-admin' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION, true );
429
+		wp_enqueue_script('woocommerce_stripe_admin', plugins_url('assets/js/stripe-admin' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION, true);
430 430
 
431 431
 		$stripe_admin_params = array(
432 432
 			'localized_messages' => array(
433
-				'not_valid_live_key_msg' => __( 'This is not a valid live key. Live keys start with "sk_live_" and "pk_live_".', 'woocommerce-gateway-stripe' ),
434
-				'not_valid_test_key_msg' => __( 'This is not a valid test key. Test keys start with "sk_test_" and "pk_test_".', 'woocommerce-gateway-stripe' ),
435
-				're_verify_button_text'  => __( 'Re-verify Domain', 'woocommerce-gateway-stripe' ),
436
-				'missing_secret_key'     => __( 'Missing Secret Key. Please set the secret key field above and re-try.', 'woocommerce-gateway-stripe' ),
433
+				'not_valid_live_key_msg' => __('This is not a valid live key. Live keys start with "sk_live_" and "pk_live_".', 'woocommerce-gateway-stripe'),
434
+				'not_valid_test_key_msg' => __('This is not a valid test key. Test keys start with "sk_test_" and "pk_test_".', 'woocommerce-gateway-stripe'),
435
+				're_verify_button_text'  => __('Re-verify Domain', 'woocommerce-gateway-stripe'),
436
+				'missing_secret_key'     => __('Missing Secret Key. Please set the secret key field above and re-try.', 'woocommerce-gateway-stripe'),
437 437
 			),
438
-			'ajaxurl'            => admin_url( 'admin-ajax.php' ),
438
+			'ajaxurl'            => admin_url('admin-ajax.php'),
439 439
 			'nonce'              => array( 
440
-				'apple_pay_domain_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_domain_nonce' ),
440
+				'apple_pay_domain_nonce' => wp_create_nonce('_wc_stripe_apple_pay_domain_nonce'),
441 441
 			),
442 442
 		);
443 443
 
444
-		wp_localize_script( 'woocommerce_stripe_admin', 'wc_stripe_admin_params', apply_filters( 'wc_stripe_admin_params', $stripe_admin_params ) );
444
+		wp_localize_script('woocommerce_stripe_admin', 'wc_stripe_admin_params', apply_filters('wc_stripe_admin_params', $stripe_admin_params));
445 445
 	}
446 446
 
447 447
 	/**
@@ -452,30 +452,30 @@  discard block
 block discarded – undo
452 452
 	 * @access public
453 453
 	 */
454 454
 	public function payment_scripts() {
455
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
455
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
456 456
 			return;
457 457
 		}
458 458
 
459
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
459
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
460 460
 
461
-		if ( $this->stripe_checkout ) {
462
-			wp_enqueue_script( 'stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true );
463
-			wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe-checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true );
461
+		if ($this->stripe_checkout) {
462
+			wp_enqueue_script('stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true);
463
+			wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe-checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true);
464 464
 		} else {
465
-			wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
466
-			wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery-payment', 'stripe' ), WC_STRIPE_VERSION, true );
465
+			wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
466
+			wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('jquery-payment', 'stripe'), WC_STRIPE_VERSION, true);
467 467
 		}
468 468
 
469 469
 		$stripe_params = array(
470 470
 			'key'                  => $this->publishable_key,
471
-			'i18n_terms'           => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ),
472
-			'i18n_required_fields' => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ),
471
+			'i18n_terms'           => __('Please accept the terms and conditions first', 'woocommerce-gateway-stripe'),
472
+			'i18n_required_fields' => __('Please fill in required checkout fields first', 'woocommerce-gateway-stripe'),
473 473
 		);
474 474
 
475 475
 		// If we're on the pay page we need to pass stripe.js the address of the order.
476
-		if ( isset( $_GET['pay_for_order'] ) && 'true' === $_GET['pay_for_order'] ) {
477
-			$order_id = wc_get_order_id_by_order_key( urldecode( $_GET['key'] ) );
478
-			$order    = wc_get_order( $order_id );
476
+		if (isset($_GET['pay_for_order']) && 'true' === $_GET['pay_for_order']) {
477
+			$order_id = wc_get_order_id_by_order_key(urldecode($_GET['key']));
478
+			$order    = wc_get_order($order_id);
479 479
 
480 480
 			$stripe_params['billing_first_name'] = $order->billing_first_name;
481 481
 			$stripe_params['billing_last_name']  = $order->billing_last_name;
@@ -487,14 +487,14 @@  discard block
 block discarded – undo
487 487
 			$stripe_params['billing_country']    = $order->billing_country;
488 488
 		}
489 489
 
490
-		$stripe_params['no_prepaid_card_msg']                     = __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' );
491
-		$stripe_params['allow_prepaid_card']                      = apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no';
492
-		$stripe_params['stripe_checkout_require_billing_address'] = apply_filters( 'wc_stripe_checkout_require_billing_address', false ) ? 'yes' : 'no';
490
+		$stripe_params['no_prepaid_card_msg']                     = __('Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe');
491
+		$stripe_params['allow_prepaid_card']                      = apply_filters('wc_stripe_allow_prepaid_card', true) ? 'yes' : 'no';
492
+		$stripe_params['stripe_checkout_require_billing_address'] = apply_filters('wc_stripe_checkout_require_billing_address', false) ? 'yes' : 'no';
493 493
 
494 494
 		// merge localized messages to be use in JS
495
-		$stripe_params = array_merge( $stripe_params, $this->get_localized_messages() );
495
+		$stripe_params = array_merge($stripe_params, $this->get_localized_messages());
496 496
 
497
-		wp_localize_script( 'woocommerce_stripe', 'wc_stripe_params', apply_filters( 'wc_stripe_params', $stripe_params ) );
497
+		wp_localize_script('woocommerce_stripe', 'wc_stripe_params', apply_filters('wc_stripe_params', $stripe_params));
498 498
 	}
499 499
 
500 500
 	/**
@@ -503,31 +503,31 @@  discard block
 block discarded – undo
503 503
 	 * @param  object $source
504 504
 	 * @return array()
505 505
 	 */
506
-	protected function generate_payment_request( $order, $source ) {
506
+	protected function generate_payment_request($order, $source) {
507 507
 		$post_data                = array();
508
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
509
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
510
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
508
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
509
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
510
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
511 511
 		$post_data['capture']     = $this->capture ? 'true' : 'false';
512 512
 
513
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
513
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
514 514
 			$post_data['receipt_email'] = $order->billing_email;
515 515
 		}
516 516
 
517
-		$post_data['expand[]']    = 'balance_transaction';
517
+		$post_data['expand[]'] = 'balance_transaction';
518 518
 
519 519
 		$metadata = array(
520
-			__( 'Customer Name', 'woocommerce-gateway-stripe' ) => sanitize_text_field( $order->billing_first_name ) . ' ' . sanitize_text_field( $order->billing_last_name ),
521
-			__( 'Customer Email', 'woocommerce-gateway-stripe' ) => sanitize_email( $order->billing_email ),
520
+			__('Customer Name', 'woocommerce-gateway-stripe') => sanitize_text_field($order->billing_first_name) . ' ' . sanitize_text_field($order->billing_last_name),
521
+			__('Customer Email', 'woocommerce-gateway-stripe') => sanitize_email($order->billing_email),
522 522
 		);
523 523
 
524
-		$post_data['metadata'] = apply_filters( 'wc_stripe_payment_metadata', $metadata, $order, $source );
524
+		$post_data['metadata'] = apply_filters('wc_stripe_payment_metadata', $metadata, $order, $source);
525 525
 
526
-		if ( $source->customer ) {
526
+		if ($source->customer) {
527 527
 			$post_data['customer'] = $source->customer;
528 528
 		}
529 529
 
530
-		if ( $source->source ) {
530
+		if ($source->source) {
531 531
 			$post_data['source'] = $source->source;
532 532
 		}
533 533
 
@@ -539,7 +539,7 @@  discard block
 block discarded – undo
539 539
 		 * @param WC_Order $order
540 540
 		 * @param object $source
541 541
 		 */
542
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order, $source );
542
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order, $source);
543 543
 	}
544 544
 
545 545
 	/**
@@ -551,37 +551,37 @@  discard block
 block discarded – undo
551 551
 	 * @throws Exception When card was not added or for and invalid card.
552 552
 	 * @return object
553 553
 	 */
554
-	protected function get_source( $user_id, $force_customer = false ) {
555
-		$stripe_customer = new WC_Stripe_Customer( $user_id );
554
+	protected function get_source($user_id, $force_customer = false) {
555
+		$stripe_customer = new WC_Stripe_Customer($user_id);
556 556
 		$stripe_source   = false;
557 557
 		$token_id        = false;
558 558
 
559 559
 		// New CC info was entered and we have a new token to process
560
-		if ( isset( $_POST['stripe_token'] ) ) {
561
-			$stripe_token     = wc_clean( $_POST['stripe_token'] );
562
-			$maybe_saved_card = isset( $_POST['wc-stripe-new-payment-method'] ) && ! empty( $_POST['wc-stripe-new-payment-method'] );
560
+		if (isset($_POST['stripe_token'])) {
561
+			$stripe_token     = wc_clean($_POST['stripe_token']);
562
+			$maybe_saved_card = isset($_POST['wc-stripe-new-payment-method']) && ! empty($_POST['wc-stripe-new-payment-method']);
563 563
 
564 564
 			// This is true if the user wants to store the card to their account.
565
-			if ( ( $user_id && $this->saved_cards && $maybe_saved_card ) || $force_customer ) {
566
-				$stripe_source = $stripe_customer->add_card( $stripe_token );
565
+			if (($user_id && $this->saved_cards && $maybe_saved_card) || $force_customer) {
566
+				$stripe_source = $stripe_customer->add_card($stripe_token);
567 567
 
568
-				if ( is_wp_error( $stripe_source ) ) {
569
-					throw new Exception( $stripe_source->get_error_message() );
568
+				if (is_wp_error($stripe_source)) {
569
+					throw new Exception($stripe_source->get_error_message());
570 570
 				}
571 571
 			} else {
572 572
 				// Not saving token, so don't define customer either.
573 573
 				$stripe_source   = $stripe_token;
574 574
 				$stripe_customer = false;
575 575
 			}
576
-		} elseif ( isset( $_POST['wc-stripe-payment-token'] ) && 'new' !== $_POST['wc-stripe-payment-token'] ) {
576
+		} elseif (isset($_POST['wc-stripe-payment-token']) && 'new' !== $_POST['wc-stripe-payment-token']) {
577 577
 			// Use an existing token, and then process the payment
578 578
 
579
-			$token_id = wc_clean( $_POST['wc-stripe-payment-token'] );
580
-			$token    = WC_Payment_Tokens::get( $token_id );
579
+			$token_id = wc_clean($_POST['wc-stripe-payment-token']);
580
+			$token    = WC_Payment_Tokens::get($token_id);
581 581
 
582
-			if ( ! $token || $token->get_user_id() !== get_current_user_id() ) {
583
-				WC()->session->set( 'refresh_totals', true );
584
-				throw new Exception( __( 'Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe' ) );
582
+			if ( ! $token || $token->get_user_id() !== get_current_user_id()) {
583
+				WC()->session->set('refresh_totals', true);
584
+				throw new Exception(__('Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe'));
585 585
 			}
586 586
 
587 587
 			$stripe_source = $token->get_token();
@@ -605,16 +605,16 @@  discard block
 block discarded – undo
605 605
 	 * @param object $order
606 606
 	 * @return object
607 607
 	 */
608
-	protected function get_order_source( $order = null ) {
608
+	protected function get_order_source($order = null) {
609 609
 		$stripe_customer = new WC_Stripe_Customer();
610 610
 		$stripe_source   = false;
611 611
 		$token_id        = false;
612 612
 
613
-		if ( $order ) {
614
-			if ( $meta_value = get_post_meta( $order->id, '_stripe_customer_id', true ) ) {
615
-				$stripe_customer->set_id( $meta_value );
613
+		if ($order) {
614
+			if ($meta_value = get_post_meta($order->id, '_stripe_customer_id', true)) {
615
+				$stripe_customer->set_id($meta_value);
616 616
 			}
617
-			if ( $meta_value = get_post_meta( $order->id, '_stripe_card_id', true ) ) {
617
+			if ($meta_value = get_post_meta($order->id, '_stripe_card_id', true)) {
618 618
 				$stripe_source = $meta_value;
619 619
 			}
620 620
 		}
@@ -637,57 +637,57 @@  discard block
 block discarded – undo
637 637
 	 *
638 638
 	 * @return array|void
639 639
 	 */
640
-	public function process_payment( $order_id, $retry = true, $force_customer = false ) {
640
+	public function process_payment($order_id, $retry = true, $force_customer = false) {
641 641
 		try {
642
-			$order  = wc_get_order( $order_id );
643
-			$source = $this->get_source( get_current_user_id(), $force_customer );
642
+			$order  = wc_get_order($order_id);
643
+			$source = $this->get_source(get_current_user_id(), $force_customer);
644 644
 
645
-			if ( empty( $source->source ) && empty( $source->customer ) ) {
646
-				$error_msg = __( 'Please enter your card details to make a payment.', 'woocommerce-gateway-stripe' );
647
-				$error_msg .= ' ' . __( 'Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe' );
648
-				throw new Exception( $error_msg );
645
+			if (empty($source->source) && empty($source->customer)) {
646
+				$error_msg = __('Please enter your card details to make a payment.', 'woocommerce-gateway-stripe');
647
+				$error_msg .= ' ' . __('Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe');
648
+				throw new Exception($error_msg);
649 649
 			}
650 650
 
651 651
 			// Store source to order meta.
652
-			$this->save_source( $order, $source );
652
+			$this->save_source($order, $source);
653 653
 
654 654
 			// Handle payment.
655
-			if ( $order->get_total() > 0 ) {
655
+			if ($order->get_total() > 0) {
656 656
 
657
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
658
-					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 ) ) );
657
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
658
+					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)));
659 659
 				}
660 660
 
661
-				$this->log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
661
+				$this->log("Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}");
662 662
 
663 663
 				// Make the request.
664
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) );
664
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $source));
665 665
 
666
-				if ( is_wp_error( $response ) ) {
666
+				if (is_wp_error($response)) {
667 667
 					// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
668
-					if ( 'customer' === $response->get_error_code() && $retry ) {
669
-						delete_user_meta( get_current_user_id(), '_stripe_customer_id' );
670
-						return $this->process_payment( $order_id, false, $force_customer );
668
+					if ('customer' === $response->get_error_code() && $retry) {
669
+						delete_user_meta(get_current_user_id(), '_stripe_customer_id');
670
+						return $this->process_payment($order_id, false, $force_customer);
671 671
 						// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
672
-					} elseif ( 'source' === $response->get_error_code() && $source->token_id ) {
673
-						$token = WC_Payment_Tokens::get( $source->token_id );
672
+					} elseif ('source' === $response->get_error_code() && $source->token_id) {
673
+						$token = WC_Payment_Tokens::get($source->token_id);
674 674
 						$token->delete();
675
-						$message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
676
-						$order->add_order_note( $message );
677
-						throw new Exception( $message );
675
+						$message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe');
676
+						$order->add_order_note($message);
677
+						throw new Exception($message);
678 678
 					}
679 679
 
680 680
 					$localized_messages = $this->get_localized_messages();
681 681
 
682
-					$message = isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message();
682
+					$message = isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message();
683 683
 
684
-					$order->add_order_note( $message );
684
+					$order->add_order_note($message);
685 685
 
686
-					throw new Exception( $message );
686
+					throw new Exception($message);
687 687
 				}
688 688
 
689 689
 				// Process valid response.
690
-				$this->process_response( $response, $order );
690
+				$this->process_response($response, $order);
691 691
 			} else {
692 692
 				$order->payment_complete();
693 693
 			}
@@ -695,23 +695,23 @@  discard block
 block discarded – undo
695 695
 			// Remove cart.
696 696
 			WC()->cart->empty_cart();
697 697
 
698
-			do_action( 'wc_gateway_stripe_process_payment', $response, $order );
698
+			do_action('wc_gateway_stripe_process_payment', $response, $order);
699 699
 
700 700
 			// Return thank you page redirect.
701 701
 			return array(
702 702
 				'result'   => 'success',
703
-				'redirect' => $this->get_return_url( $order ),
703
+				'redirect' => $this->get_return_url($order),
704 704
 			);
705 705
 
706
-		} catch ( Exception $e ) {
707
-			wc_add_notice( $e->getMessage(), 'error' );
708
-			$this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
706
+		} catch (Exception $e) {
707
+			wc_add_notice($e->getMessage(), 'error');
708
+			$this->log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
709 709
 
710
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
711
-				$this->send_failed_order_email( $order_id );
710
+			if ($order->has_status(array('pending', 'failed'))) {
711
+				$this->send_failed_order_email($order_id);
712 712
 			}
713 713
 
714
-			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
714
+			do_action('wc_gateway_stripe_process_payment_error', $e, $order);
715 715
 
716 716
 			return array(
717 717
 				'result'   => 'fail',
@@ -726,52 +726,52 @@  discard block
 block discarded – undo
726 726
 	 * @param WC_Order $order For to which the source applies.
727 727
 	 * @param stdClass $source Source information.
728 728
 	 */
729
-	protected function save_source( $order, $source ) {
729
+	protected function save_source($order, $source) {
730 730
 		// Store source in the order.
731
-		if ( $source->customer ) {
732
-			update_post_meta( $order->id, '_stripe_customer_id', $source->customer );
731
+		if ($source->customer) {
732
+			update_post_meta($order->id, '_stripe_customer_id', $source->customer);
733 733
 		}
734
-		if ( $source->source ) {
735
-			update_post_meta( $order->id, '_stripe_card_id', $source->source );
734
+		if ($source->source) {
735
+			update_post_meta($order->id, '_stripe_card_id', $source->source);
736 736
 		}
737 737
 	}
738 738
 
739 739
 	/**
740 740
 	 * Store extra meta data for an order from a Stripe Response.
741 741
 	 */
742
-	public function process_response( $response, $order ) {
743
-		$this->log( 'Processing response: ' . print_r( $response, true ) );
742
+	public function process_response($response, $order) {
743
+		$this->log('Processing response: ' . print_r($response, true));
744 744
 
745 745
 		// Store charge data
746
-		update_post_meta( $order->id, '_stripe_charge_id', $response->id );
747
-		update_post_meta( $order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no' );
746
+		update_post_meta($order->id, '_stripe_charge_id', $response->id);
747
+		update_post_meta($order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no');
748 748
 
749 749
 		// Store other data such as fees
750
-		if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) {
750
+		if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) {
751 751
 			// Fees and Net needs to both come from Stripe to be accurate as the returned
752 752
 			// values are in the local currency of the Stripe account, not from WC.
753
-			$fee = ! empty( $response->balance_transaction->fee ) ? number_format( $response->balance_transaction->fee / 100, 2, '.', '' ) : 0;
754
-			$net = ! empty( $response->balance_transaction->net ) ? number_format( $response->balance_transaction->net / 100, 2, '.', '' ) : 0;
755
-			update_post_meta( $order->id, 'Stripe Fee', $fee );
756
-			update_post_meta( $order->id, 'Net Revenue From Stripe', $net );
753
+			$fee = ! empty($response->balance_transaction->fee) ? number_format($response->balance_transaction->fee / 100, 2, '.', '') : 0;
754
+			$net = ! empty($response->balance_transaction->net) ? number_format($response->balance_transaction->net / 100, 2, '.', '') : 0;
755
+			update_post_meta($order->id, 'Stripe Fee', $fee);
756
+			update_post_meta($order->id, 'Net Revenue From Stripe', $net);
757 757
 		}
758 758
 
759
-		if ( $response->captured ) {
760
-			$order->payment_complete( $response->id );
759
+		if ($response->captured) {
760
+			$order->payment_complete($response->id);
761 761
 
762
-			$message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id );
763
-			$order->add_order_note( $message );
764
-			$this->log( 'Success: ' . $message );
762
+			$message = sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $response->id);
763
+			$order->add_order_note($message);
764
+			$this->log('Success: ' . $message);
765 765
 
766 766
 		} else {
767
-			add_post_meta( $order->id, '_transaction_id', $response->id, true );
767
+			add_post_meta($order->id, '_transaction_id', $response->id, true);
768 768
 
769
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
769
+			if ($order->has_status(array('pending', 'failed'))) {
770 770
 				$order->reduce_order_stock();
771 771
 			}
772 772
 
773
-			$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 ) );
774
-			$this->log( "Successful auth: $response->id" );
773
+			$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));
774
+			$this->log("Successful auth: $response->id");
775 775
 		}
776 776
 
777 777
 		return $response;
@@ -783,32 +783,32 @@  discard block
 block discarded – undo
783 783
 	 * @since 3.0.0
784 784
 	 */
785 785
 	public function add_payment_method() {
786
-		if ( empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) {
787
-			wc_add_notice( __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ), 'error' );
786
+		if (empty($_POST['stripe_token']) || ! is_user_logged_in()) {
787
+			wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error');
788 788
 			return;
789 789
 		}
790 790
 
791
-		$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
792
-		$card            = $stripe_customer->add_card( wc_clean( $_POST['stripe_token'] ) );
791
+		$stripe_customer = new WC_Stripe_Customer(get_current_user_id());
792
+		$card            = $stripe_customer->add_card(wc_clean($_POST['stripe_token']));
793 793
 
794
-		if ( is_wp_error( $card ) ) {
794
+		if (is_wp_error($card)) {
795 795
 			$localized_messages = $this->get_localized_messages();
796
-			$error_msg = __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' );
796
+			$error_msg = __('There was a problem adding the card.', 'woocommerce-gateway-stripe');
797 797
 
798 798
 			// loop through the errors to find matching localized message
799
-			foreach ( $card->errors as $error => $msg ) {
800
-				if ( isset( $localized_messages[ $error ] ) ) {
801
-					$error_msg = $localized_messages[ $error ];
799
+			foreach ($card->errors as $error => $msg) {
800
+				if (isset($localized_messages[$error])) {
801
+					$error_msg = $localized_messages[$error];
802 802
 				}
803 803
 			}
804 804
 
805
-			wc_add_notice( $error_msg, 'error' );
805
+			wc_add_notice($error_msg, 'error');
806 806
 			return;
807 807
 		}
808 808
 
809 809
 		return array(
810 810
 			'result'   => 'success',
811
-			'redirect' => wc_get_endpoint_url( 'payment-methods' ),
811
+			'redirect' => wc_get_endpoint_url('payment-methods'),
812 812
 		);
813 813
 	}
814 814
 
@@ -818,36 +818,36 @@  discard block
 block discarded – undo
818 818
 	 * @param  float $amount
819 819
 	 * @return bool
820 820
 	 */
821
-	public function process_refund( $order_id, $amount = null, $reason = '' ) {
822
-		$order = wc_get_order( $order_id );
821
+	public function process_refund($order_id, $amount = null, $reason = '') {
822
+		$order = wc_get_order($order_id);
823 823
 
824
-		if ( ! $order || ! $order->get_transaction_id() ) {
824
+		if ( ! $order || ! $order->get_transaction_id()) {
825 825
 			return false;
826 826
 		}
827 827
 
828 828
 		$body = array();
829 829
 
830
-		if ( ! is_null( $amount ) ) {
831
-			$body['amount']	= $this->get_stripe_amount( $amount );
830
+		if ( ! is_null($amount)) {
831
+			$body['amount'] = $this->get_stripe_amount($amount);
832 832
 		}
833 833
 
834
-		if ( $reason ) {
834
+		if ($reason) {
835 835
 			$body['metadata'] = array(
836 836
 				'reason'	=> $reason,
837 837
 			);
838 838
 		}
839 839
 
840
-		$this->log( "Info: Beginning refund for order $order_id for the amount of {$amount}" );
840
+		$this->log("Info: Beginning refund for order $order_id for the amount of {$amount}");
841 841
 
842
-		$response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' );
842
+		$response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds');
843 843
 
844
-		if ( is_wp_error( $response ) ) {
845
-			$this->log( 'Error: ' . $response->get_error_message() );
844
+		if (is_wp_error($response)) {
845
+			$this->log('Error: ' . $response->get_error_message());
846 846
 			return $response;
847
-		} elseif ( ! empty( $response->id ) ) {
848
-			$refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), wc_price( $response->amount / 100 ), $response->id, $reason );
849
-			$order->add_order_note( $refund_message );
850
-			$this->log( 'Success: ' . html_entity_decode( strip_tags( $refund_message ) ) );
847
+		} elseif ( ! empty($response->id)) {
848
+			$refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason);
849
+			$order->add_order_note($refund_message);
850
+			$this->log('Success: ' . html_entity_decode(strip_tags($refund_message)));
851 851
 			return true;
852 852
 		}
853 853
 	}
@@ -860,10 +860,10 @@  discard block
 block discarded – undo
860 860
 	 * @param int $order_id
861 861
 	 * @return null
862 862
 	 */
863
-	public function send_failed_order_email( $order_id ) {
863
+	public function send_failed_order_email($order_id) {
864 864
 		$emails = WC()->mailer()->get_emails();
865
-		if ( ! empty( $emails ) && ! empty( $order_id ) ) {
866
-			$emails['WC_Email_Failed_Order']->trigger( $order_id );
865
+		if ( ! empty($emails) && ! empty($order_id)) {
866
+			$emails['WC_Email_Failed_Order']->trigger($order_id);
867 867
 		}
868 868
 	}
869 869
 
@@ -875,9 +875,9 @@  discard block
 block discarded – undo
875 875
 	 *
876 876
 	 * @param string $message
877 877
 	 */
878
-	public function log( $message ) {
879
-		if ( $this->logging ) {
880
-			WC_Stripe::log( $message );
878
+	public function log($message) {
879
+		if ($this->logging) {
880
+			WC_Stripe::log($message);
881 881
 		}
882 882
 	}
883 883
 }
Please login to merge, or discard this patch.