Completed
Push — master ( 7ec021...209228 )
by Roy
02:52
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +180 added lines, -180 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,32 +66,32 @@  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' ), 1 );
71
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 );
72
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 );
73
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
74
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
75
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
76
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
77
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
78
-		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'), 1);
71
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2);
72
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1);
73
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2);
74
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
75
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
76
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
77
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
78
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
79 79
 	}
80 80
 
81 81
 	/**
82 82
 	 * Filters the gateway title to reflect Apple Pay.
83 83
 	 *
84 84
 	 */
85
-	public function filter_gateway_title( $title, $id ) {
85
+	public function filter_gateway_title($title, $id) {
86 86
 		global $post;
87 87
 
88
-		if ( ! is_object( $post ) ) {
88
+		if ( ! is_object($post)) {
89 89
 			return $title;
90 90
 		}
91 91
 
92
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
92
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
93 93
 
94
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
94
+		if ('stripe' === $id && ! empty($method_title)) {
95 95
 			return $method_title;
96 96
 		}
97 97
 
@@ -105,38 +105,38 @@  discard block
 block discarded – undo
105 105
 	 * @version 3.1.0
106 106
 	 */
107 107
 	public function payment_scripts() {
108
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
108
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
109 109
 			return;
110 110
 		}
111 111
 
112
-		if ( ! $this->is_supported_product_type() ) {
112
+		if ( ! $this->is_supported_product_type()) {
113 113
 			return;
114 114
 		}
115 115
 
116
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
116
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
117 117
 
118
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
118
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
119 119
 
120
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
121
-		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 );
120
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
121
+		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);
122 122
 
123 123
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
124 124
 
125 125
 		$stripe_params = array(
126 126
 			'key'                                           => $publishable_key,
127 127
 			'currency_code'                                 => get_woocommerce_currency(),
128
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
128
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
129 129
 			'label'                                         => $this->statement_descriptor,
130
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
131
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
132
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
133
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
134
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
130
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
131
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
132
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
133
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
134
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
135 135
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
136 136
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
137 137
 		);
138 138
 
139
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
139
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
140 140
 	}
141 141
 
142 142
 	/**
@@ -144,8 +144,8 @@  discard block
 block discarded – undo
144 144
 	 *
145 145
 	 */
146 146
 	public function is_supported_product_type() {
147
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
148
-			if ( 'subscription' === $values['data']->product_type ) {
147
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
148
+			if ('subscription' === $values['data']->product_type) {
149 149
 				return false;
150 150
 			}
151 151
 		}
@@ -168,19 +168,19 @@  discard block
 block discarded – undo
168 168
 		 */
169 169
 		if (
170 170
 			'yes' !== $this->_gateway_settings['apple_pay']
171
-			|| ! isset( $gateways['stripe'] )
171
+			|| ! isset($gateways['stripe'])
172 172
 		) {
173 173
 			return;
174 174
 		}
175 175
 
176
-		if ( ! $this->is_supported_product_type() ) {
176
+		if ( ! $this->is_supported_product_type()) {
177 177
 			return;
178 178
 		}
179 179
 
180
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
181
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
180
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
181
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
182 182
 		?>
183
-		<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>
183
+		<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>
184 184
 		<?php
185 185
 	}
186 186
 
@@ -199,16 +199,16 @@  discard block
 block discarded – undo
199 199
 		 */
200 200
 		if (
201 201
 			'yes' !== $this->_gateway_settings['apple_pay']
202
-			|| ! isset( $gateways['stripe'] )
202
+			|| ! isset($gateways['stripe'])
203 203
 		) {
204 204
 			return;
205 205
 		}
206 206
 
207
-		if ( ! $this->is_supported_product_type() ) {
207
+		if ( ! $this->is_supported_product_type()) {
208 208
 			return;
209 209
 		}
210 210
 		?>
211
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
211
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
212 212
 		<?php
213 213
 	}
214 214
 
@@ -219,11 +219,11 @@  discard block
 block discarded – undo
219 219
 	 * @version 3.1.0
220 220
 	 */
221 221
 	public function generate_apple_pay_cart() {
222
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
223
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
222
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
223
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
224 224
 		}
225 225
 
226
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
226
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
227 227
 	}
228 228
 
229 229
 	/**
@@ -233,29 +233,29 @@  discard block
 block discarded – undo
233 233
 	 * @version 3.1.0
234 234
 	 * @param array $address
235 235
 	 */
236
-	public function calculate_shipping( $address = array() ) {
237
-		$country  = strtoupper( $address['countryCode'] );
238
-		$state    = strtoupper( $address['administrativeArea'] );
236
+	public function calculate_shipping($address = array()) {
237
+		$country  = strtoupper($address['countryCode']);
238
+		$state    = strtoupper($address['administrativeArea']);
239 239
 		$postcode = $address['postalCode'];
240 240
 		$city     = $address['locality'];
241 241
 
242 242
 		WC()->shipping->reset_shipping();
243 243
 
244
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
245
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
246
-		} elseif ( $postcode ) {
247
-			$postcode = wc_format_postcode( $postcode, $country );
244
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
245
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
246
+		} elseif ($postcode) {
247
+			$postcode = wc_format_postcode($postcode, $country);
248 248
 		}
249 249
 
250
-		if ( $country ) {
251
-			WC()->customer->set_location( $country, $state, $postcode, $city );
252
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
250
+		if ($country) {
251
+			WC()->customer->set_location($country, $state, $postcode, $city);
252
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
253 253
 		} else {
254 254
 			WC()->customer->set_to_base();
255 255
 			WC()->customer->set_shipping_to_base();
256 256
 		}
257 257
 
258
-		WC()->customer->calculated_shipping( true );
258
+		WC()->customer->calculated_shipping(true);
259 259
 
260 260
 		/**
261 261
 		 * Set the shipping package.
@@ -277,17 +277,17 @@  discard block
 block discarded – undo
277 277
 		$packages[0]['destination']['postcode']  = $postcode;
278 278
 		$packages[0]['destination']['city']      = $city;
279 279
 
280
-		foreach ( WC()->cart->get_cart() as $item ) {
281
-			if ( $item['data']->needs_shipping() ) {
282
-				if ( isset( $item['line_total'] ) ) {
280
+		foreach (WC()->cart->get_cart() as $item) {
281
+			if ($item['data']->needs_shipping()) {
282
+				if (isset($item['line_total'])) {
283 283
 					$packages[0]['contents_cost'] += $item['line_total'];
284 284
 				}
285 285
 			}
286 286
 		}
287 287
 
288
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
288
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
289 289
 
290
-		WC()->shipping->calculate_shipping( $packages );
290
+		WC()->shipping->calculate_shipping($packages);
291 291
 	}
292 292
 
293 293
 	/**
@@ -297,18 +297,18 @@  discard block
 block discarded – undo
297 297
 	 * @version 3.1.0
298 298
 	 */
299 299
 	public function get_shipping_methods() {
300
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
301
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
300
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
301
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
302 302
 		}
303 303
 
304
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
305
-			define( 'WOOCOMMERCE_CART', true );
304
+		if ( ! defined('WOOCOMMERCE_CART')) {
305
+			define('WOOCOMMERCE_CART', true);
306 306
 		}
307 307
 
308 308
 		try {
309
-			$address = array_map( 'wc_clean', $_POST['address'] );
309
+			$address = array_map('wc_clean', $_POST['address']);
310 310
 
311
-			$this->calculate_shipping( $address );
311
+			$this->calculate_shipping($address);
312 312
 
313 313
 			// Set the shipping options.
314 314
 			$currency = get_woocommerce_currency();
@@ -316,13 +316,13 @@  discard block
 block discarded – undo
316 316
 
317 317
 			$packages = WC()->shipping->get_packages();
318 318
 
319
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
320
-				foreach ( $packages as $package_key => $package ) {
321
-					if ( empty( $package['rates'] ) ) {
322
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
319
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
320
+				foreach ($packages as $package_key => $package) {
321
+					if (empty($package['rates'])) {
322
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
323 323
 					}
324 324
 
325
-					foreach ( $package['rates'] as $key => $rate ) {
325
+					foreach ($package['rates'] as $key => $rate) {
326 326
 						$data[] = array(
327 327
 							'id'       => $rate->id,
328 328
 							'label'    => $rate->label,
@@ -336,16 +336,16 @@  discard block
 block discarded – undo
336 336
 				}
337 337
 
338 338
 				// Auto select the first shipping method.
339
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
339
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
340 340
 
341 341
 				WC()->cart->calculate_totals();
342 342
 
343
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
343
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
344 344
 			} else {
345
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
345
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
346 346
 			}
347
-		} catch ( Exception $e ) {
348
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
347
+		} catch (Exception $e) {
348
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
349 349
 		}
350 350
 	}
351 351
 
@@ -356,29 +356,29 @@  discard block
 block discarded – undo
356 356
 	 * @version 3.1.0
357 357
 	 */
358 358
 	public function update_shipping_method() {
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
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
364
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
363
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
364
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
365 365
 		}
366 366
 
367
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
367
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
368 368
 
369
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
369
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
370 370
 
371 371
 		WC()->cart->calculate_totals();
372 372
 
373 373
 		// Send back the new cart total.
374 374
 		$currency  = get_woocommerce_currency();
375
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
375
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
376 376
 		$data      = array(
377 377
 			'total' => WC()->cart->total,
378 378
 		);
379 379
 
380 380
 		// Include fees and taxes as displayItems.
381
-		foreach ( WC()->cart->fees as $key => $fee ) {
381
+		foreach (WC()->cart->fees as $key => $fee) {
382 382
 			$data['items'][] = array(
383 383
 				'label'  => $fee->name,
384 384
 				'amount' => array(
@@ -387,9 +387,9 @@  discard block
 block discarded – undo
387 387
 				),
388 388
 			);
389 389
 		}
390
-		if ( 0 < $tax_total ) {
390
+		if (0 < $tax_total) {
391 391
 			$data['items'][] = array(
392
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
392
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
393 393
 				'amount' => array(
394 394
 					'currency' => $currency,
395 395
 					'value'    => $tax_total,
@@ -397,7 +397,7 @@  discard block
 block discarded – undo
397 397
 			);
398 398
 		}
399 399
 
400
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
400
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
401 401
 	}
402 402
 
403 403
 	/**
@@ -408,35 +408,35 @@  discard block
 block discarded – undo
408 408
 	 * @version 3.1.0
409 409
 	 */
410 410
 	public function process_apple_pay() {
411
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
412
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
411
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
412
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
413 413
 		}
414 414
 
415 415
 		try {
416
-			$result = array_map( 'wc_clean', $_POST['result'] );
416
+			$result = array_map('wc_clean', $_POST['result']);
417 417
 
418
-			$order = $this->create_order( $result );
418
+			$order = $this->create_order($result);
419 419
 
420 420
 			// Handle payment.
421
-			if ( $order->get_total() > 0 ) {
421
+			if ($order->get_total() > 0) {
422 422
 
423
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
424
-					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 ) ) );
423
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
424
+					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)));
425 425
 				}
426 426
 
427
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
427
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
428 428
 
429 429
 				// Make the request.
430
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
430
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
431 431
 
432
-				if ( is_wp_error( $response ) ) {
432
+				if (is_wp_error($response)) {
433 433
 					$localized_messages = $this->get_localized_messages();
434 434
 
435
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
435
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
436 436
 				}
437 437
 
438 438
 				// Process valid response.
439
-				$this->process_response( $response, $order );
439
+				$this->process_response($response, $order);
440 440
 			} else {
441 441
 				$order->payment_complete();
442 442
 			}
@@ -444,24 +444,24 @@  discard block
 block discarded – undo
444 444
 			// Remove cart.
445 445
 			WC()->cart->empty_cart();
446 446
 
447
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
448
-			update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
447
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
448
+			update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
449 449
 
450 450
 			// Return thank you page redirect.
451
-			wp_send_json( array(
451
+			wp_send_json(array(
452 452
 				'success'  => 'true',
453
-				'redirect' => $this->get_return_url( $order ),
454
-			) );
453
+				'redirect' => $this->get_return_url($order),
454
+			));
455 455
 
456
-		} catch ( Exception $e ) {
457
-			WC()->session->set( 'refresh_totals', true );
458
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
456
+		} catch (Exception $e) {
457
+			WC()->session->set('refresh_totals', true);
458
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
459 459
 
460
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
461
-				$this->send_failed_order_email( $order->id );
460
+			if ($order->has_status(array('pending', 'failed'))) {
461
+				$this->send_failed_order_email($order->id);
462 462
 			}
463 463
 
464
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
464
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
465 465
 		}
466 466
 	}
467 467
 
@@ -471,14 +471,14 @@  discard block
 block discarded – undo
471 471
 	 * @param string $source token
472 472
 	 * @return array()
473 473
 	 */
474
-	protected function generate_payment_request( $order, $source ) {
474
+	protected function generate_payment_request($order, $source) {
475 475
 		$post_data                = array();
476
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
477
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
478
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
476
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
477
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
478
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
479 479
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
480 480
 
481
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
481
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
482 482
 			$post_data['receipt_email'] = $order->billing_email;
483 483
 		}
484 484
 
@@ -493,7 +493,7 @@  discard block
 block discarded – undo
493 493
 		 * @param WC_Order $order
494 494
 		 * @param object $source
495 495
 		 */
496
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
496
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
497 497
 	}
498 498
 
499 499
 	/**
@@ -502,14 +502,14 @@  discard block
 block discarded – undo
502 502
 	 * @since 3.1.0
503 503
 	 * @version 3.1.0
504 504
 	 */
505
-	public function build_shipping_methods( $shipping_methods ) {
506
-		if ( empty( $shipping_methods ) ) {
505
+	public function build_shipping_methods($shipping_methods) {
506
+		if (empty($shipping_methods)) {
507 507
 			return array();
508 508
 		}
509 509
 
510 510
 		$shipping = array();
511 511
 
512
-		foreach ( $shipping_methods as $method ) {
512
+		foreach ($shipping_methods as $method) {
513 513
 			$shipping[] = array(
514 514
 				'label'      => $method['label'],
515 515
 				'detail'     => '',
@@ -528,53 +528,53 @@  discard block
 block discarded – undo
528 528
 	 * @version 3.1.0
529 529
 	 */
530 530
 	public function build_line_items() {
531
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
532
-			define( 'WOOCOMMERCE_CART', true );
531
+		if ( ! defined('WOOCOMMERCE_CART')) {
532
+			define('WOOCOMMERCE_CART', true);
533 533
 		}
534 534
 
535
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
535
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
536 536
 
537 537
 		$items = array();
538 538
 
539
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
540
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
539
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
540
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
541 541
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
542 542
 
543 543
 			$item = array(
544 544
 				'type'   => 'final',
545 545
 				'label'  => $values['data']->post->post_title . $quantity_label,
546
-				'amount' => wc_format_decimal( $amount, $decimals ),
546
+				'amount' => wc_format_decimal($amount, $decimals),
547 547
 			);
548 548
 
549 549
 			$items[] = $item;
550 550
 		}
551 551
 
552
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
553
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
554
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
555
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
556
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
552
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
553
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
554
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
555
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
556
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
557 557
 
558
-		if ( wc_tax_enabled() ) {
558
+		if (wc_tax_enabled()) {
559 559
 			$items[] = array(
560 560
 				'type'   => 'final',
561
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
561
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
562 562
 				'amount' => $tax,
563 563
 			);
564 564
 		}
565 565
 
566
-		if ( WC()->cart->needs_shipping() ) {
566
+		if (WC()->cart->needs_shipping()) {
567 567
 			$items[] = array(
568 568
 				'type'   => 'final',
569
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
569
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
570 570
 				'amount' => $shipping,
571 571
 			);
572 572
 		}
573 573
 
574
-		if ( WC()->cart->has_discount() ) {
574
+		if (WC()->cart->has_discount()) {
575 575
 			$items[] = array(
576 576
 				'type'   => 'final',
577
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
577
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
578 578
 				'amount' => $discounts,
579 579
 			);
580 580
 		}
@@ -590,24 +590,24 @@  discard block
 block discarded – undo
590 590
 	 * @param array $data
591 591
 	 * @return object $order
592 592
 	 */
593
-	public function create_order( $data = array() ) {
594
-		if ( empty( $data ) ) {
595
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
593
+	public function create_order($data = array()) {
594
+		if (empty($data)) {
595
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
596 596
 		}
597 597
 
598 598
 		$order = wc_create_order();
599 599
 
600
-		if ( is_wp_error( $order ) ) {
601
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
602
-		} elseif ( false === $order ) {
603
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
600
+		if (is_wp_error($order)) {
601
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
602
+		} elseif (false === $order) {
603
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
604 604
 		} else {
605 605
 			$order_id = $order->id;
606
-			do_action( 'woocommerce_new_order', $order_id );
606
+			do_action('woocommerce_new_order', $order_id);
607 607
 		}
608 608
 
609 609
 		// Store the line items to the new/resumed order
610
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
610
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
611 611
 			$item_id = $order->add_product(
612 612
 				$values['data'],
613 613
 				$values['quantity'],
@@ -623,47 +623,47 @@  discard block
 block discarded – undo
623 623
 				)
624 624
 			);
625 625
 
626
-			if ( ! $item_id ) {
627
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
626
+			if ( ! $item_id) {
627
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
628 628
 			}
629 629
 
630 630
 			// Allow plugins to add order item meta
631
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
631
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
632 632
 		}
633 633
 
634 634
 		// Store fees
635
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
636
-			$item_id = $order->add_fee( $fee );
635
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
636
+			$item_id = $order->add_fee($fee);
637 637
 
638
-			if ( ! $item_id ) {
639
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
638
+			if ( ! $item_id) {
639
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
640 640
 			}
641 641
 
642 642
 			// Allow plugins to add order item meta to fees
643
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
643
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
644 644
 		}
645 645
 
646 646
 		// Store tax rows
647
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
648
-			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 ) {
649
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
647
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
648
+			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) {
649
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
650 650
 			}
651 651
 		}
652 652
 
653 653
 		// Store coupons
654
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
655
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
656
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
654
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
655
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
656
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
657 657
 			}
658 658
 		}
659 659
 
660 660
 		// Billing address
661 661
 		$billing_address = array();
662
-		if ( ! empty( $data['token']['card'] ) ) {
662
+		if ( ! empty($data['token']['card'])) {
663 663
 			// Name from Stripe is a full name string.
664
-			$name                          = explode( ' ', $data['token']['card']['name'] );
665
-			$lastname                      = array_pop( $name );
666
-			$firstname                     = implode( ' ', $name );
664
+			$name                          = explode(' ', $data['token']['card']['name']);
665
+			$lastname                      = array_pop($name);
666
+			$firstname                     = implode(' ', $name);
667 667
 			$billing_address['first_name'] = $firstname;
668 668
 			$billing_address['last_name']  = $lastname;
669 669
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -678,7 +678,7 @@  discard block
 block discarded – undo
678 678
 
679 679
 		// Shipping address.
680 680
 		$shipping_address = array();
681
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
681
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
682 682
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
683 683
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
684 684
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -689,7 +689,7 @@  discard block
 block discarded – undo
689 689
 			$shipping_address['city']       = $data['shippingContact']['locality'];
690 690
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
691 691
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
692
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
692
+		} elseif ( ! empty($data['shippingContact'])) {
693 693
 			$shipping_address['first_name'] = $firstname;
694 694
 			$shipping_address['last_name']  = $lastname;
695 695
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -702,34 +702,34 @@  discard block
 block discarded – undo
702 702
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
703 703
 		}
704 704
 
705
-		$order->set_address( $billing_address, 'billing' );
706
-		$order->set_address( $shipping_address, 'shipping' );
705
+		$order->set_address($billing_address, 'billing');
706
+		$order->set_address($shipping_address, 'shipping');
707 707
 
708
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
708
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
709 709
 
710 710
 		// Get the rate object selected by user.
711
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
712
-			foreach ( $package['rates'] as $key => $rate ) {
711
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
712
+			foreach ($package['rates'] as $key => $rate) {
713 713
 				// Loop through user chosen shipping methods.
714
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
715
-					if ( $method === $key ) {
716
-						$order->add_shipping( $rate );
714
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
715
+					if ($method === $key) {
716
+						$order->add_shipping($rate);
717 717
 					}
718 718
 				}
719 719
 			}
720 720
 		}
721 721
 
722 722
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
723
-		$order->set_payment_method( $available_gateways['stripe'] );
724
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
725
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
726
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
727
-		$order->set_total( WC()->cart->tax_total, 'tax' );
728
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
729
-		$order->set_total( WC()->cart->total );
723
+		$order->set_payment_method($available_gateways['stripe']);
724
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
725
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
726
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
727
+		$order->set_total(WC()->cart->tax_total, 'tax');
728
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
729
+		$order->set_total(WC()->cart->total);
730 730
 
731 731
 		// If we got here, the order was created without problems!
732
-		wc_transaction_query( 'commit' );
732
+		wc_transaction_query('commit');
733 733
 
734 734
 		return $order;
735 735
 	}
Please login to merge, or discard this patch.