Completed
Push — master ( ceb300...9b2fd4 )
by Roy
02:01
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +174 added lines, -174 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,26 +66,26 @@  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
 	/**
80 80
 	 * Filters the gateway title to reflect Apple Pay.
81 81
 	 *
82 82
 	 */
83
-	public function filter_gateway_title( $title, $id ) {
83
+	public function filter_gateway_title($title, $id) {
84 84
 		global $post;
85 85
 
86
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
86
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
87 87
 
88
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
88
+		if ('stripe' === $id && ! empty($method_title)) {
89 89
 			return $method_title;
90 90
 		}
91 91
 
@@ -99,38 +99,38 @@  discard block
 block discarded – undo
99 99
 	 * @version 3.1.0
100 100
 	 */
101 101
 	public function payment_scripts() {
102
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
102
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
103 103
 			return;
104 104
 		}
105 105
 
106
-		if ( ! $this->is_supported_product_type() ) {
106
+		if ( ! $this->is_supported_product_type()) {
107 107
 			return;
108 108
 		}
109 109
 		
110
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
110
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
111 111
 		
112
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
112
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
113 113
 
114
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
115
-		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 );
114
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
115
+		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);
116 116
 
117 117
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
118 118
 
119 119
 		$stripe_params = array(
120 120
 			'key'                                           => $publishable_key,
121 121
 			'currency_code'                                 => get_woocommerce_currency(),
122
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
122
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
123 123
 			'label'                                         => $this->statement_descriptor,
124
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
125
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
126
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
127
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
128
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
124
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
125
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
126
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
127
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
128
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
129 129
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
130 130
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
131 131
 		);
132 132
 
133
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
133
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
134 134
 	}
135 135
 		
136 136
 	/**
@@ -138,8 +138,8 @@  discard block
 block discarded – undo
138 138
 	 *
139 139
 	 */
140 140
 	public function is_supported_product_type() {
141
-		foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
142
-			if ( 'subscription' === $values['data']->product_type ) {
141
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
142
+			if ('subscription' === $values['data']->product_type) {
143 143
 				return false;
144 144
 			}
145 145
 		}
@@ -162,19 +162,19 @@  discard block
 block discarded – undo
162 162
 		 */
163 163
 		if ( 
164 164
 			'yes' !== $this->_gateway_settings['apple_pay']
165
-			|| ! isset( $gateways['stripe'] ) 
165
+			|| ! isset($gateways['stripe']) 
166 166
 		) {
167 167
 			return;
168 168
 		}
169 169
 
170
-		if ( ! $this->is_supported_product_type() ) {
170
+		if ( ! $this->is_supported_product_type()) {
171 171
 			return;
172 172
 		}
173 173
 
174
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
175
-		$country = strtolower( substr( get_option( 'woocommerce_default_country' ), 0, 2 ) );
174
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
175
+		$country = strtolower(substr(get_option('woocommerce_default_country'), 0, 2));
176 176
 		?>
177
-		<button class="apple-pay-button" lang="<?php echo esc_attr( $country ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button>
177
+		<button class="apple-pay-button" lang="<?php echo esc_attr($country); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($apple_pay_button); ?>;"></button>
178 178
 		<?php
179 179
 	}
180 180
 
@@ -185,11 +185,11 @@  discard block
 block discarded – undo
185 185
 	 * @version 3.1.0
186 186
 	 */
187 187
 	public function generate_apple_pay_cart() {
188
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
189
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
188
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
189
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
190 190
 		}
191 191
 
192
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
192
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
193 193
 	}
194 194
 
195 195
 	/**
@@ -199,29 +199,29 @@  discard block
 block discarded – undo
199 199
 	 * @version 3.1.0
200 200
 	 * @param array $address
201 201
 	 */
202
-	public function calculate_shipping( $address = array() ) {
203
-		$country  = strtoupper( $address['countryCode'] );
204
-		$state    = strtoupper( $address['administrativeArea'] );
202
+	public function calculate_shipping($address = array()) {
203
+		$country  = strtoupper($address['countryCode']);
204
+		$state    = strtoupper($address['administrativeArea']);
205 205
 		$postcode = $address['postalCode'];
206 206
 		$city     = $address['locality'];
207 207
 
208 208
 		WC()->shipping->reset_shipping();
209 209
 
210
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
211
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
212
-		} elseif ( $postcode ) {
213
-			$postcode = wc_format_postcode( $postcode, $country );
210
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
211
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
212
+		} elseif ($postcode) {
213
+			$postcode = wc_format_postcode($postcode, $country);
214 214
 		}
215 215
 
216
-		if ( $country ) {
217
-			WC()->customer->set_location( $country, $state, $postcode, $city );
218
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
216
+		if ($country) {
217
+			WC()->customer->set_location($country, $state, $postcode, $city);
218
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
219 219
 		} else {
220 220
 			WC()->customer->set_to_base();
221 221
 			WC()->customer->set_shipping_to_base();
222 222
 		}
223 223
 
224
-		WC()->customer->calculated_shipping( true );
224
+		WC()->customer->calculated_shipping(true);
225 225
 
226 226
 		/** 
227 227
 		 * Set the shipping package.
@@ -243,17 +243,17 @@  discard block
 block discarded – undo
243 243
 		$packages[0]['destination']['postcode']  = $postcode;
244 244
 		$packages[0]['destination']['city']      = $city;
245 245
 
246
-		foreach ( WC()->cart->get_cart() as $item ) {
247
-			if ( $item['data']->needs_shipping() ) {
248
-				if ( isset( $item['line_total'] ) ) {
246
+		foreach (WC()->cart->get_cart() as $item) {
247
+			if ($item['data']->needs_shipping()) {
248
+				if (isset($item['line_total'])) {
249 249
 					$packages[0]['contents_cost'] += $item['line_total'];
250 250
 				}
251 251
 			}
252 252
 		}
253 253
 
254
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
254
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
255 255
 
256
-		WC()->shipping->calculate_shipping( $packages );
256
+		WC()->shipping->calculate_shipping($packages);
257 257
 	}
258 258
 
259 259
 	/**
@@ -263,30 +263,30 @@  discard block
 block discarded – undo
263 263
 	 * @version 3.1.0
264 264
 	 */
265 265
 	public function get_shipping_methods() {
266
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
267
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
266
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
267
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
268 268
 		}
269 269
 
270
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
271
-			define( 'WOOCOMMERCE_CART', true );
270
+		if ( ! defined('WOOCOMMERCE_CART')) {
271
+			define('WOOCOMMERCE_CART', true);
272 272
 		}
273 273
 
274 274
 		try {
275
-			$address = array_map( 'wc_clean', $_POST['address'] );
275
+			$address = array_map('wc_clean', $_POST['address']);
276 276
 
277
-			$this->calculate_shipping( $address );
277
+			$this->calculate_shipping($address);
278 278
 
279 279
 			// Set the shipping options.
280 280
 			$currency = get_woocommerce_currency();
281 281
 			$data     = array();
282 282
 
283
-			if ( ! empty( WC()->shipping->get_packages() ) && WC()->customer->has_calculated_shipping() ) {
284
-				foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
285
-					if ( empty( $package['rates'] ) ) {
286
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
283
+			if ( ! empty(WC()->shipping->get_packages()) && WC()->customer->has_calculated_shipping()) {
284
+				foreach (WC()->shipping->get_packages() as $package_key => $package) {
285
+					if (empty($package['rates'])) {
286
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
287 287
 					}
288 288
 
289
-					foreach ( $package['rates'] as $key => $rate ) {
289
+					foreach ($package['rates'] as $key => $rate) {
290 290
 						$data[] = array(
291 291
 							'id'       => $rate->id,
292 292
 							'label'    => $rate->label,
@@ -300,16 +300,16 @@  discard block
 block discarded – undo
300 300
 				}
301 301
 
302 302
 				// Auto select the first shipping method.
303
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
303
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
304 304
 
305 305
 				WC()->cart->calculate_totals();
306 306
 				
307
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
307
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
308 308
 			} else {
309
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); 
309
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); 
310 310
 			}
311
-		} catch( Exception $e ) {
312
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );			
311
+		} catch (Exception $e) {
312
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));			
313 313
 		}
314 314
 	}
315 315
 
@@ -320,29 +320,29 @@  discard block
 block discarded – undo
320 320
 	 * @version 3.1.0
321 321
 	 */
322 322
 	public function update_shipping_method() {
323
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
324
-			define( 'WOOCOMMERCE_CART', true );
323
+		if ( ! defined('WOOCOMMERCE_CART')) {
324
+			define('WOOCOMMERCE_CART', true);
325 325
 		}
326 326
 
327
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
328
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
327
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
328
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
329 329
 		}
330 330
 
331
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
331
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
332 332
 
333
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
333
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
334 334
 
335 335
 		WC()->cart->calculate_totals();
336 336
 
337 337
 		// Send back the new cart total.
338 338
 		$currency  = get_woocommerce_currency();
339
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
339
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
340 340
 		$data      = array(
341 341
 			'total' => WC()->cart->total,
342 342
 		);
343 343
 
344 344
 		// Include fees and taxes as displayItems.
345
-		foreach ( WC()->cart->fees as $key => $fee ) {
345
+		foreach (WC()->cart->fees as $key => $fee) {
346 346
 			$data['items'][] = array(
347 347
 				'label'  => $fee->name,
348 348
 				'amount' => array(
@@ -351,9 +351,9 @@  discard block
 block discarded – undo
351 351
 				),
352 352
 			);
353 353
 		}
354
-		if ( 0 < $tax_total ) {
354
+		if (0 < $tax_total) {
355 355
 			$data['items'][] = array(
356
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
356
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
357 357
 				'amount' => array(
358 358
 					'currency' => $currency,
359 359
 					'value'    => $tax_total,
@@ -361,7 +361,7 @@  discard block
 block discarded – undo
361 361
 			);
362 362
 		}
363 363
 
364
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
364
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
365 365
 	}
366 366
 
367 367
 	/**
@@ -372,35 +372,35 @@  discard block
 block discarded – undo
372 372
 	 * @version 3.1.0
373 373
 	 */
374 374
 	public function process_apple_pay() {
375
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
376
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
375
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
376
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
377 377
 		}
378 378
 
379 379
 		try {
380
-			$result = array_map( 'wc_clean', $_POST['result'] );
380
+			$result = array_map('wc_clean', $_POST['result']);
381 381
 
382
-			$order = $this->create_order( $result );
382
+			$order = $this->create_order($result);
383 383
 
384 384
 			// Handle payment.
385
-			if ( $order->get_total() > 0 ) {
385
+			if ($order->get_total() > 0) {
386 386
 
387
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
388
-					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 ) ) );
387
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
388
+					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)));
389 389
 				}
390 390
 
391
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
391
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
392 392
 
393 393
 				// Make the request.
394
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
394
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
395 395
 
396
-				if ( is_wp_error( $response ) ) {
396
+				if (is_wp_error($response)) {
397 397
 					$localized_messages = $this->get_localized_messages();
398 398
 
399
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
399
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
400 400
 				}
401 401
 
402 402
 				// Process valid response.
403
-				$this->process_response( $response, $order );
403
+				$this->process_response($response, $order);
404 404
 			} else {
405 405
 				$order->payment_complete();
406 406
 			}
@@ -408,24 +408,24 @@  discard block
 block discarded – undo
408 408
 			// Remove cart.
409 409
 			WC()->cart->empty_cart();
410 410
 
411
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
412
-			update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
411
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
412
+			update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
413 413
 
414 414
 			// Return thank you page redirect.
415
-			wp_send_json( array(
415
+			wp_send_json(array(
416 416
 				'success'  => 'true',
417
-				'redirect' => $this->get_return_url( $order ),
418
-			) );
417
+				'redirect' => $this->get_return_url($order),
418
+			));
419 419
 
420
-		} catch ( Exception $e ) {
421
-			WC()->session->set( 'refresh_totals', true );
422
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
420
+		} catch (Exception $e) {
421
+			WC()->session->set('refresh_totals', true);
422
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
423 423
 
424
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
425
-				$this->send_failed_order_email( $order->id );
424
+			if ($order->has_status(array('pending', 'failed'))) {
425
+				$this->send_failed_order_email($order->id);
426 426
 			}
427 427
 
428
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
428
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
429 429
 		}
430 430
 	}
431 431
 
@@ -435,14 +435,14 @@  discard block
 block discarded – undo
435 435
 	 * @param string $source token
436 436
 	 * @return array()
437 437
 	 */
438
-	protected function generate_payment_request( $order, $source ) {
438
+	protected function generate_payment_request($order, $source) {
439 439
 		$post_data                = array();
440
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
441
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
442
-		$post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
440
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
441
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
442
+		$post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
443 443
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
444 444
 
445
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
445
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
446 446
 			$post_data['receipt_email'] = $order->billing_email;
447 447
 		}
448 448
 
@@ -457,7 +457,7 @@  discard block
 block discarded – undo
457 457
 		 * @param WC_Order $order
458 458
 		 * @param object $source
459 459
 		 */
460
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
460
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
461 461
 	}
462 462
 
463 463
 	/**
@@ -466,14 +466,14 @@  discard block
 block discarded – undo
466 466
 	 * @since 3.1.0
467 467
 	 * @version 3.1.0
468 468
 	 */	
469
-	public function build_shipping_methods( $shipping_methods ) {
470
-		if ( empty( $shipping_methods ) ) {
469
+	public function build_shipping_methods($shipping_methods) {
470
+		if (empty($shipping_methods)) {
471 471
 			return array();
472 472
 		}
473 473
 
474 474
 		$shipping = array();
475 475
 
476
-		foreach( $shipping_methods as $method ) {
476
+		foreach ($shipping_methods as $method) {
477 477
 			$shipping[] = array(
478 478
 				'label'      => $method['label'],
479 479
 				'detail'     => '',
@@ -492,53 +492,53 @@  discard block
 block discarded – undo
492 492
 	 * @version 3.1.0
493 493
 	 */	
494 494
 	public function build_line_items() {
495
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
496
-			define( 'WOOCOMMERCE_CART', true );
495
+		if ( ! defined('WOOCOMMERCE_CART')) {
496
+			define('WOOCOMMERCE_CART', true);
497 497
 		}
498 498
 
499
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
499
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
500 500
 
501 501
 		$items = array();
502 502
 
503
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
504
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
503
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
504
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
505 505
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
506 506
 
507 507
 			$item = array(
508 508
 				'type'   => 'final',
509 509
 				'label'  => $values['data']->post->post_title . $quantity_label,
510
-				'amount' => wc_format_decimal( $amount, $decimals ),
510
+				'amount' => wc_format_decimal($amount, $decimals),
511 511
 			);
512 512
 
513 513
 			$items[] = $item;
514 514
 		}
515 515
 
516
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
517
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
518
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
519
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
520
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
516
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
517
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
518
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
519
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
520
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
521 521
 
522
-		if ( wc_tax_enabled() ) {
522
+		if (wc_tax_enabled()) {
523 523
 			$items[] = array(
524 524
 				'type'   => 'final',
525
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
525
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
526 526
 				'amount' => $tax,
527 527
 			);
528 528
 		}
529 529
 
530
-		if ( WC()->cart->needs_shipping() ) {
530
+		if (WC()->cart->needs_shipping()) {
531 531
 			$items[] = array(
532 532
 				'type'   => 'final',
533
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
533
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
534 534
 				'amount' => $shipping,
535 535
 			);
536 536
 		}
537 537
 
538
-		if ( WC()->cart->has_discount() ) {
538
+		if (WC()->cart->has_discount()) {
539 539
 			$items[] = array(
540 540
 				'type'   => 'final',
541
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
541
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
542 542
 				'amount' => $discounts,
543 543
 			);
544 544
 		}
@@ -554,24 +554,24 @@  discard block
 block discarded – undo
554 554
 	 * @param array $data
555 555
 	 * @return object $order
556 556
 	 */
557
-	public function create_order( $data = array() ) {
558
-		if ( empty( $data ) ) {
559
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
557
+	public function create_order($data = array()) {
558
+		if (empty($data)) {
559
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
560 560
 		}
561 561
 
562 562
 		$order = wc_create_order();
563 563
 
564
-		if ( is_wp_error( $order ) ) {
565
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
566
-		} elseif ( false === $order ) {
567
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
564
+		if (is_wp_error($order)) {
565
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
566
+		} elseif (false === $order) {
567
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
568 568
 		} else {
569 569
 			$order_id = $order->id;
570
-			do_action( 'woocommerce_new_order', $order_id );
570
+			do_action('woocommerce_new_order', $order_id);
571 571
 		}
572 572
 
573 573
 		// Store the line items to the new/resumed order
574
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
574
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
575 575
 			$item_id = $order->add_product(
576 576
 				$values['data'],
577 577
 				$values['quantity'],
@@ -587,47 +587,47 @@  discard block
 block discarded – undo
587 587
 				)
588 588
 			);
589 589
 
590
-			if ( ! $item_id ) {
591
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
590
+			if ( ! $item_id) {
591
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
592 592
 			}
593 593
 
594 594
 			// Allow plugins to add order item meta
595
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
595
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
596 596
 		}
597 597
 
598 598
 		// Store fees
599
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
600
-			$item_id = $order->add_fee( $fee );
599
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
600
+			$item_id = $order->add_fee($fee);
601 601
 
602
-			if ( ! $item_id ) {
603
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
602
+			if ( ! $item_id) {
603
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
604 604
 			}
605 605
 
606 606
 			// Allow plugins to add order item meta to fees
607
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
607
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
608 608
 		}
609 609
 
610 610
 		// Store tax rows
611
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
612
-			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 ) {
613
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
611
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
612
+			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) {
613
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
614 614
 			}
615 615
 		}
616 616
 
617 617
 		// Store coupons
618
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
619
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
620
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
618
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
619
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
620
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
621 621
 			}
622 622
 		}
623 623
 
624 624
 		// Billing address
625 625
 		$billing_address = array();
626
-		if ( ! empty( $data['token']['card'] ) ) {
626
+		if ( ! empty($data['token']['card'])) {
627 627
 			// Name from Stripe is a full name string.
628
-			$name                          = explode( ' ', $data['token']['card']['name'] );
629
-			$lastname                      = array_pop( $name );
630
-			$firstname                     = implode( ' ', $name );
628
+			$name                          = explode(' ', $data['token']['card']['name']);
629
+			$lastname                      = array_pop($name);
630
+			$firstname                     = implode(' ', $name);
631 631
 			$billing_address['first_name'] = $firstname;
632 632
 			$billing_address['last_name']  = $lastname;
633 633
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -642,7 +642,7 @@  discard block
 block discarded – undo
642 642
 
643 643
 		// Shipping address.
644 644
 		$shipping_address = array();
645
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
645
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
646 646
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
647 647
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
648 648
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -653,7 +653,7 @@  discard block
 block discarded – undo
653 653
 			$shipping_address['city']       = $data['shippingContact']['locality'];
654 654
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
655 655
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
656
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
656
+		} elseif ( ! empty($data['shippingContact'])) {
657 657
 			$shipping_address['first_name'] = $firstname;
658 658
 			$shipping_address['last_name']  = $lastname;
659 659
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -666,34 +666,34 @@  discard block
 block discarded – undo
666 666
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
667 667
 		}
668 668
 
669
-		$order->set_address( $billing_address, 'billing' );
670
-		$order->set_address( $shipping_address, 'shipping' );
669
+		$order->set_address($billing_address, 'billing');
670
+		$order->set_address($shipping_address, 'shipping');
671 671
 
672
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
672
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
673 673
 		
674 674
 		// Get the rate object selected by user.
675
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
676
-			foreach ( $package['rates'] as $key => $rate ) {
675
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
676
+			foreach ($package['rates'] as $key => $rate) {
677 677
 				// Loop through user chosen shipping methods.
678
-				foreach( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
679
-					if ( $method === $key ) {
680
-						$order->add_shipping( $rate );
678
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
679
+					if ($method === $key) {
680
+						$order->add_shipping($rate);
681 681
 					}
682 682
 				}
683 683
 			}
684 684
 		}
685 685
 
686 686
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
687
-		$order->set_payment_method( $available_gateways['stripe'] );
688
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
689
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
690
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
691
-		$order->set_total( WC()->cart->tax_total, 'tax' );
692
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
693
-		$order->set_total( WC()->cart->total );
687
+		$order->set_payment_method($available_gateways['stripe']);
688
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
689
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
690
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
691
+		$order->set_total(WC()->cart->tax_total, 'tax');
692
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
693
+		$order->set_total(WC()->cart->total);
694 694
 
695 695
 		// If we got here, the order was created without problems!
696
-		wc_transaction_query( 'commit' );
696
+		wc_transaction_query('commit');
697 697
 
698 698
 		return $order;
699 699
 	}
Please login to merge, or discard this patch.