Completed
Push — master ( 013109...33910d )
by Roy
02:10
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +169 added lines, -169 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
 
@@ -40,7 +40,7 @@  discard block
 block discarded – undo
40 40
 	public function __construct() {
41 41
 		self::$_this = $this;
42 42
 
43
-		$this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
43
+		$this->_gateway_settings = get_option('woocommerce_stripe_settings', '');
44 44
 
45 45
 		$this->init();
46 46
 	}
@@ -57,13 +57,13 @@  discard block
 block discarded – undo
57 57
 	 * @version 3.1.0
58 58
 	 */
59 59
 	public function init() {
60
-		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
61
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 20 );
62
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ) );
63
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
64
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
65
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
66
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
60
+		add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
61
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 20);
62
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'));
63
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
64
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
65
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
66
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
67 67
 	}
68 68
 
69 69
 	/**
@@ -73,38 +73,38 @@  discard block
 block discarded – undo
73 73
 	 * @version 3.1.0
74 74
 	 */
75 75
 	public function payment_scripts() {
76
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
76
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
77 77
 			return;
78 78
 		}
79 79
 
80
-		if ( ! $this->is_supported_product_type() ) {
80
+		if ( ! $this->is_supported_product_type()) {
81 81
 			return;
82 82
 		}
83 83
 		
84
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
84
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
85 85
 		
86
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
86
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
87 87
 
88
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
89
-		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 );
88
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
89
+		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);
90 90
 
91 91
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
92 92
 
93 93
 		$stripe_params = array(
94 94
 			'key'                                           => $publishable_key,
95 95
 			'currency_code'                                 => get_woocommerce_currency(),
96
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
97
-			'label'                                         => get_bloginfo( 'name', 'display' ),
98
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
99
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
100
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
101
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
102
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
96
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
97
+			'label'                                         => get_bloginfo('name', 'display'),
98
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
99
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
100
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
101
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
102
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
103 103
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
104 104
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
105 105
 		);
106 106
 
107
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
107
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
108 108
 	}
109 109
 		
110 110
 	/**
@@ -112,8 +112,8 @@  discard block
 block discarded – undo
112 112
 	 *
113 113
 	 */
114 114
 	public function is_supported_product_type() {
115
-		foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
116
-			if ( 'subscription' === $values['data']->product_type ) {
115
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
116
+			if ('subscription' === $values['data']->product_type) {
117 117
 				return false;
118 118
 			}
119 119
 		}
@@ -136,19 +136,19 @@  discard block
 block discarded – undo
136 136
 		 */
137 137
 		if ( 
138 138
 			'yes' !== $this->_gateway_settings['apple_pay']
139
-			|| ! isset( $gateways['stripe'] ) 
139
+			|| ! isset($gateways['stripe']) 
140 140
 		) {
141 141
 			return;
142 142
 		}
143 143
 
144
-		if ( ! $this->is_supported_product_type() ) {
144
+		if ( ! $this->is_supported_product_type()) {
145 145
 			return;
146 146
 		}
147 147
 
148
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
149
-		$country = strtolower( substr( get_option( 'woocommerce_default_country' ), 0, 2 ) );
148
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
149
+		$country = strtolower(substr(get_option('woocommerce_default_country'), 0, 2));
150 150
 		?>
151
-		<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>
151
+		<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>
152 152
 		<?php
153 153
 	}
154 154
 
@@ -159,11 +159,11 @@  discard block
 block discarded – undo
159 159
 	 * @version 3.1.0
160 160
 	 */
161 161
 	public function generate_apple_pay_cart() {
162
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
163
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
162
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
163
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
164 164
 		}
165 165
 
166
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
166
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
167 167
 	}
168 168
 
169 169
 	/**
@@ -173,29 +173,29 @@  discard block
 block discarded – undo
173 173
 	 * @version 3.1.0
174 174
 	 * @param array $address
175 175
 	 */
176
-	public function calculate_shipping( $address = array() ) {
177
-		$country  = strtoupper( $address['countryCode'] );
178
-		$state    = strtoupper( $address['administrativeArea'] );
176
+	public function calculate_shipping($address = array()) {
177
+		$country  = strtoupper($address['countryCode']);
178
+		$state    = strtoupper($address['administrativeArea']);
179 179
 		$postcode = $address['postalCode'];
180 180
 		$city     = $address['locality'];
181 181
 
182 182
 		WC()->shipping->reset_shipping();
183 183
 
184
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
185
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
186
-		} elseif ( $postcode ) {
187
-			$postcode = wc_format_postcode( $postcode, $country );
184
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
185
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
186
+		} elseif ($postcode) {
187
+			$postcode = wc_format_postcode($postcode, $country);
188 188
 		}
189 189
 
190
-		if ( $country ) {
191
-			WC()->customer->set_location( $country, $state, $postcode, $city );
192
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
190
+		if ($country) {
191
+			WC()->customer->set_location($country, $state, $postcode, $city);
192
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
193 193
 		} else {
194 194
 			WC()->customer->set_to_base();
195 195
 			WC()->customer->set_shipping_to_base();
196 196
 		}
197 197
 
198
-		WC()->customer->calculated_shipping( true );
198
+		WC()->customer->calculated_shipping(true);
199 199
 
200 200
 		/** 
201 201
 		 * Set the shipping package.
@@ -217,17 +217,17 @@  discard block
 block discarded – undo
217 217
 		$packages[0]['destination']['postcode']  = $postcode;
218 218
 		$packages[0]['destination']['city']      = $city;
219 219
 
220
-		foreach ( WC()->cart->get_cart() as $item ) {
221
-			if ( $item['data']->needs_shipping() ) {
222
-				if ( isset( $item['line_total'] ) ) {
220
+		foreach (WC()->cart->get_cart() as $item) {
221
+			if ($item['data']->needs_shipping()) {
222
+				if (isset($item['line_total'])) {
223 223
 					$packages[0]['contents_cost'] += $item['line_total'];
224 224
 				}
225 225
 			}
226 226
 		}
227 227
 
228
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
228
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
229 229
 
230
-		WC()->shipping->calculate_shipping( $packages );
230
+		WC()->shipping->calculate_shipping($packages);
231 231
 	}
232 232
 
233 233
 	/**
@@ -237,30 +237,30 @@  discard block
 block discarded – undo
237 237
 	 * @version 3.1.0
238 238
 	 */
239 239
 	public function get_shipping_methods() {
240
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
241
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
240
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
241
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
242 242
 		}
243 243
 
244
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
245
-			define( 'WOOCOMMERCE_CART', true );
244
+		if ( ! defined('WOOCOMMERCE_CART')) {
245
+			define('WOOCOMMERCE_CART', true);
246 246
 		}
247 247
 
248 248
 		try {
249
-			$address = array_map( 'wc_clean', $_POST['address'] );
249
+			$address = array_map('wc_clean', $_POST['address']);
250 250
 
251
-			$this->calculate_shipping( $address );
251
+			$this->calculate_shipping($address);
252 252
 
253 253
 			// Set the shipping options.
254 254
 			$currency = get_woocommerce_currency();
255 255
 			$data     = array();
256 256
 
257
-			if ( ! empty( WC()->shipping->get_packages() ) && WC()->customer->has_calculated_shipping() ) {
258
-				foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
259
-					if ( empty( $package['rates'] ) ) {
260
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
257
+			if ( ! empty(WC()->shipping->get_packages()) && WC()->customer->has_calculated_shipping()) {
258
+				foreach (WC()->shipping->get_packages() as $package_key => $package) {
259
+					if (empty($package['rates'])) {
260
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
261 261
 					}
262 262
 
263
-					foreach ( $package['rates'] as $key => $rate ) {
263
+					foreach ($package['rates'] as $key => $rate) {
264 264
 						$data[] = array(
265 265
 							'id'       => $rate->id,
266 266
 							'label'    => $rate->label,
@@ -274,16 +274,16 @@  discard block
 block discarded – undo
274 274
 				}
275 275
 
276 276
 				// Auto select the first shipping method.
277
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
277
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
278 278
 
279 279
 				WC()->cart->calculate_totals();
280 280
 				
281
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
281
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
282 282
 			} else {
283
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); 
283
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); 
284 284
 			}
285
-		} catch( Exception $e ) {
286
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );			
285
+		} catch (Exception $e) {
286
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));			
287 287
 		}
288 288
 	}
289 289
 
@@ -294,29 +294,29 @@  discard block
 block discarded – undo
294 294
 	 * @version 3.1.0
295 295
 	 */
296 296
 	public function update_shipping_method() {
297
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
298
-			define( 'WOOCOMMERCE_CART', true );
297
+		if ( ! defined('WOOCOMMERCE_CART')) {
298
+			define('WOOCOMMERCE_CART', true);
299 299
 		}
300 300
 
301
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
302
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
301
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
302
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
303 303
 		}
304 304
 
305
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
305
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
306 306
 
307
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
307
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
308 308
 
309 309
 		WC()->cart->calculate_totals();
310 310
 
311 311
 		// Send back the new cart total.
312 312
 		$currency  = get_woocommerce_currency();
313
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
313
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
314 314
 		$data      = array(
315 315
 			'total' => WC()->cart->total,
316 316
 		);
317 317
 
318 318
 		// Include fees and taxes as displayItems.
319
-		foreach ( WC()->cart->fees as $key => $fee ) {
319
+		foreach (WC()->cart->fees as $key => $fee) {
320 320
 			$data['items'][] = array(
321 321
 				'label'  => $fee->name,
322 322
 				'amount' => array(
@@ -325,9 +325,9 @@  discard block
 block discarded – undo
325 325
 				),
326 326
 			);
327 327
 		}
328
-		if ( 0 < $tax_total ) {
328
+		if (0 < $tax_total) {
329 329
 			$data['items'][] = array(
330
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
330
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
331 331
 				'amount' => array(
332 332
 					'currency' => $currency,
333 333
 					'value'    => $tax_total,
@@ -335,7 +335,7 @@  discard block
 block discarded – undo
335 335
 			);
336 336
 		}
337 337
 
338
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
338
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
339 339
 	}
340 340
 
341 341
 	/**
@@ -346,35 +346,35 @@  discard block
 block discarded – undo
346 346
 	 * @version 3.1.0
347 347
 	 */
348 348
 	public function process_apple_pay() {
349
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
350
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
349
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
350
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
351 351
 		}
352 352
 
353 353
 		try {
354
-			$result = array_map( 'wc_clean', $_POST['result'] );
354
+			$result = array_map('wc_clean', $_POST['result']);
355 355
 
356
-			$order = $this->create_order( $result );
356
+			$order = $this->create_order($result);
357 357
 
358 358
 			// Handle payment.
359
-			if ( $order->get_total() > 0 ) {
359
+			if ($order->get_total() > 0) {
360 360
 
361
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
362
-					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 ) ) );
361
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
362
+					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)));
363 363
 				}
364 364
 
365
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
365
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
366 366
 
367 367
 				// Make the request.
368
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
368
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
369 369
 
370
-				if ( is_wp_error( $response ) ) {
370
+				if (is_wp_error($response)) {
371 371
 					$localized_messages = $this->get_localized_messages();
372 372
 
373
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
373
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
374 374
 				}
375 375
 
376 376
 				// Process valid response.
377
-				$this->process_response( $response, $order );
377
+				$this->process_response($response, $order);
378 378
 			} else {
379 379
 				$order->payment_complete();
380 380
 			}
@@ -382,23 +382,23 @@  discard block
 block discarded – undo
382 382
 			// Remove cart.
383 383
 			WC()->cart->empty_cart();
384 384
 
385
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
385
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
386 386
 
387 387
 			// Return thank you page redirect.
388
-			wp_send_json( array(
388
+			wp_send_json(array(
389 389
 				'success'  => 'true',
390
-				'redirect' => $this->get_return_url( $order ),
391
-			) );
390
+				'redirect' => $this->get_return_url($order),
391
+			));
392 392
 
393
-		} catch ( Exception $e ) {
394
-			WC()->session->set( 'refresh_totals', true );
395
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
393
+		} catch (Exception $e) {
394
+			WC()->session->set('refresh_totals', true);
395
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
396 396
 
397
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
398
-				$this->send_failed_order_email( $order->id );
397
+			if ($order->has_status(array('pending', 'failed'))) {
398
+				$this->send_failed_order_email($order->id);
399 399
 			}
400 400
 
401
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
401
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
402 402
 		}
403 403
 	}
404 404
 
@@ -408,14 +408,14 @@  discard block
 block discarded – undo
408 408
 	 * @param string $source token
409 409
 	 * @return array()
410 410
 	 */
411
-	protected function generate_payment_request( $order, $source ) {
411
+	protected function generate_payment_request($order, $source) {
412 412
 		$post_data                = array();
413
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
414
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
415
-		$post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() );
413
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
414
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
415
+		$post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), $order->get_order_number());
416 416
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
417 417
 
418
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
418
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
419 419
 			$post_data['receipt_email'] = $order->billing_email;
420 420
 		}
421 421
 
@@ -430,7 +430,7 @@  discard block
 block discarded – undo
430 430
 		 * @param WC_Order $order
431 431
 		 * @param object $source
432 432
 		 */
433
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
433
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
434 434
 	}
435 435
 
436 436
 	/**
@@ -439,14 +439,14 @@  discard block
 block discarded – undo
439 439
 	 * @since 3.1.0
440 440
 	 * @version 3.1.0
441 441
 	 */	
442
-	public function build_shipping_methods( $shipping_methods ) {
443
-		if ( empty( $shipping_methods ) ) {
442
+	public function build_shipping_methods($shipping_methods) {
443
+		if (empty($shipping_methods)) {
444 444
 			return array();
445 445
 		}
446 446
 
447 447
 		$shipping = array();
448 448
 
449
-		foreach( $shipping_methods as $method ) {
449
+		foreach ($shipping_methods as $method) {
450 450
 			$shipping[] = array(
451 451
 				'label'      => $method['label'],
452 452
 				'detail'     => '',
@@ -465,53 +465,53 @@  discard block
 block discarded – undo
465 465
 	 * @version 3.1.0
466 466
 	 */	
467 467
 	public function build_line_items() {
468
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
469
-			define( 'WOOCOMMERCE_CART', true );
468
+		if ( ! defined('WOOCOMMERCE_CART')) {
469
+			define('WOOCOMMERCE_CART', true);
470 470
 		}
471 471
 
472
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
472
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
473 473
 
474 474
 		$items = array();
475 475
 
476
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
477
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
476
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
477
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
478 478
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
479 479
 
480 480
 			$item = array(
481 481
 				'type'   => 'final',
482 482
 				'label'  => $values['data']->post->post_title . $quantity_label,
483
-				'amount' => wc_format_decimal( $amount, $decimals ),
483
+				'amount' => wc_format_decimal($amount, $decimals),
484 484
 			);
485 485
 
486 486
 			$items[] = $item;
487 487
 		}
488 488
 
489
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
490
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
491
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
492
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
493
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
489
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
490
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
491
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
492
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
493
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
494 494
 
495
-		if ( wc_tax_enabled() ) {
495
+		if (wc_tax_enabled()) {
496 496
 			$items[] = array(
497 497
 				'type'   => 'final',
498
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
498
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
499 499
 				'amount' => $tax,
500 500
 			);
501 501
 		}
502 502
 
503
-		if ( WC()->cart->needs_shipping() ) {
503
+		if (WC()->cart->needs_shipping()) {
504 504
 			$items[] = array(
505 505
 				'type'   => 'final',
506
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
506
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
507 507
 				'amount' => $shipping,
508 508
 			);
509 509
 		}
510 510
 
511
-		if ( WC()->cart->has_discount() ) {
511
+		if (WC()->cart->has_discount()) {
512 512
 			$items[] = array(
513 513
 				'type'   => 'final',
514
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
514
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
515 515
 				'amount' => $discounts,
516 516
 			);
517 517
 		}
@@ -527,24 +527,24 @@  discard block
 block discarded – undo
527 527
 	 * @param array $data
528 528
 	 * @return object $order
529 529
 	 */
530
-	public function create_order( $data = array() ) {
531
-		if ( empty( $data ) ) {
532
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
530
+	public function create_order($data = array()) {
531
+		if (empty($data)) {
532
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
533 533
 		}
534 534
 
535 535
 		$order = wc_create_order();
536 536
 
537
-		if ( is_wp_error( $order ) ) {
538
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
539
-		} elseif ( false === $order ) {
540
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
537
+		if (is_wp_error($order)) {
538
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
539
+		} elseif (false === $order) {
540
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
541 541
 		} else {
542 542
 			$order_id = $order->id;
543
-			do_action( 'woocommerce_new_order', $order_id );
543
+			do_action('woocommerce_new_order', $order_id);
544 544
 		}
545 545
 
546 546
 		// Store the line items to the new/resumed order
547
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
547
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
548 548
 			$item_id = $order->add_product(
549 549
 				$values['data'],
550 550
 				$values['quantity'],
@@ -560,47 +560,47 @@  discard block
 block discarded – undo
560 560
 				)
561 561
 			);
562 562
 
563
-			if ( ! $item_id ) {
564
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
563
+			if ( ! $item_id) {
564
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
565 565
 			}
566 566
 
567 567
 			// Allow plugins to add order item meta
568
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
568
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
569 569
 		}
570 570
 
571 571
 		// Store fees
572
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
573
-			$item_id = $order->add_fee( $fee );
572
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
573
+			$item_id = $order->add_fee($fee);
574 574
 
575
-			if ( ! $item_id ) {
576
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
575
+			if ( ! $item_id) {
576
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
577 577
 			}
578 578
 
579 579
 			// Allow plugins to add order item meta to fees
580
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
580
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
581 581
 		}
582 582
 
583 583
 		// Store tax rows
584
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
585
-			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 ) {
586
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
584
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
585
+			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) {
586
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
587 587
 			}
588 588
 		}
589 589
 
590 590
 		// Store coupons
591
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
592
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
593
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
591
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
592
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
593
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
594 594
 			}
595 595
 		}
596 596
 
597 597
 		// Billing address
598 598
 		$billing_address = array();
599
-		if ( ! empty( $data['token']['card'] ) ) {
599
+		if ( ! empty($data['token']['card'])) {
600 600
 			// Name from Stripe is a full name string.
601
-			$name                          = explode( ' ', $data['token']['card']['name'] );
602
-			$lastname                      = array_pop( $name );
603
-			$firstname                     = implode( ' ', $name );
601
+			$name                          = explode(' ', $data['token']['card']['name']);
602
+			$lastname                      = array_pop($name);
603
+			$firstname                     = implode(' ', $name);
604 604
 			$billing_address['first_name'] = $firstname;
605 605
 			$billing_address['last_name']  = $lastname;
606 606
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -615,7 +615,7 @@  discard block
 block discarded – undo
615 615
 
616 616
 		// Shipping address.
617 617
 		$shipping_address = array();
618
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
618
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
619 619
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
620 620
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
621 621
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -626,7 +626,7 @@  discard block
 block discarded – undo
626 626
 			$shipping_address['city']       = $data['shippingContact']['locality'];
627 627
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
628 628
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
629
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
629
+		} elseif ( ! empty($data['shippingContact'])) {
630 630
 			$shipping_address['first_name'] = $firstname;
631 631
 			$shipping_address['last_name']  = $lastname;
632 632
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -639,34 +639,34 @@  discard block
 block discarded – undo
639 639
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
640 640
 		}
641 641
 
642
-		$order->set_address( $billing_address, 'billing' );
643
-		$order->set_address( $shipping_address, 'shipping' );
642
+		$order->set_address($billing_address, 'billing');
643
+		$order->set_address($shipping_address, 'shipping');
644 644
 
645
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
645
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
646 646
 		
647 647
 		// Get the rate object selected by user.
648
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
649
-			foreach ( $package['rates'] as $key => $rate ) {
648
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
649
+			foreach ($package['rates'] as $key => $rate) {
650 650
 				// Loop through user chosen shipping methods.
651
-				foreach( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
652
-					if ( $method === $key ) {
653
-						$order->add_shipping( $rate );
651
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
652
+					if ($method === $key) {
653
+						$order->add_shipping($rate);
654 654
 					}
655 655
 				}
656 656
 			}
657 657
 		}
658 658
 
659 659
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
660
-		$order->set_payment_method( $available_gateways['stripe'] );
661
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
662
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
663
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
664
-		$order->set_total( WC()->cart->tax_total, 'tax' );
665
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
666
-		$order->set_total( WC()->cart->total );
660
+		$order->set_payment_method($available_gateways['stripe']);
661
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
662
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
663
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
664
+		$order->set_total(WC()->cart->tax_total, 'tax');
665
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
666
+		$order->set_total(WC()->cart->total);
667 667
 
668 668
 		// If we got here, the order was created without problems!
669
-		wc_transaction_query( 'commit' );
669
+		wc_transaction_query('commit');
670 670
 
671 671
 		return $order;
672 672
 	}
Please login to merge, or discard this patch.