Completed
Push — master ( 33910d...ceb300 )
by Roy
02:00
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
 
@@ -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,26 +57,26 @@  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' ) );
67
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
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
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
68 68
 	}
69 69
 
70 70
 	/**
71 71
 	 * Filters the gateway title to reflect Apple Pay.
72 72
 	 *
73 73
 	 */
74
-	public function filter_gateway_title( $title, $id ) {
74
+	public function filter_gateway_title($title, $id) {
75 75
 		global $post;
76 76
 
77
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
77
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
78 78
 
79
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
79
+		if ('stripe' === $id && ! empty($method_title)) {
80 80
 			return $method_title;
81 81
 		}
82 82
 
@@ -90,38 +90,38 @@  discard block
 block discarded – undo
90 90
 	 * @version 3.1.0
91 91
 	 */
92 92
 	public function payment_scripts() {
93
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
93
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
94 94
 			return;
95 95
 		}
96 96
 
97
-		if ( ! $this->is_supported_product_type() ) {
97
+		if ( ! $this->is_supported_product_type()) {
98 98
 			return;
99 99
 		}
100 100
 		
101
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
101
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
102 102
 		
103
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
103
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
104 104
 
105
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
106
-		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 );
105
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
106
+		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);
107 107
 
108 108
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
109 109
 
110 110
 		$stripe_params = array(
111 111
 			'key'                                           => $publishable_key,
112 112
 			'currency_code'                                 => get_woocommerce_currency(),
113
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
114
-			'label'                                         => get_bloginfo( 'name', 'display' ),
115
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
116
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
117
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
118
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
119
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
113
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
114
+			'label'                                         => get_bloginfo('name', 'display'),
115
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
116
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
117
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
118
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
119
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
120 120
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
121 121
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
122 122
 		);
123 123
 
124
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
124
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
125 125
 	}
126 126
 		
127 127
 	/**
@@ -129,8 +129,8 @@  discard block
 block discarded – undo
129 129
 	 *
130 130
 	 */
131 131
 	public function is_supported_product_type() {
132
-		foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
133
-			if ( 'subscription' === $values['data']->product_type ) {
132
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
133
+			if ('subscription' === $values['data']->product_type) {
134 134
 				return false;
135 135
 			}
136 136
 		}
@@ -153,19 +153,19 @@  discard block
 block discarded – undo
153 153
 		 */
154 154
 		if ( 
155 155
 			'yes' !== $this->_gateway_settings['apple_pay']
156
-			|| ! isset( $gateways['stripe'] ) 
156
+			|| ! isset($gateways['stripe']) 
157 157
 		) {
158 158
 			return;
159 159
 		}
160 160
 
161
-		if ( ! $this->is_supported_product_type() ) {
161
+		if ( ! $this->is_supported_product_type()) {
162 162
 			return;
163 163
 		}
164 164
 
165
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
166
-		$country = strtolower( substr( get_option( 'woocommerce_default_country' ), 0, 2 ) );
165
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
166
+		$country = strtolower(substr(get_option('woocommerce_default_country'), 0, 2));
167 167
 		?>
168
-		<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>
168
+		<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>
169 169
 		<?php
170 170
 	}
171 171
 
@@ -176,11 +176,11 @@  discard block
 block discarded – undo
176 176
 	 * @version 3.1.0
177 177
 	 */
178 178
 	public function generate_apple_pay_cart() {
179
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
180
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
179
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
180
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
181 181
 		}
182 182
 
183
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
183
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
184 184
 	}
185 185
 
186 186
 	/**
@@ -190,29 +190,29 @@  discard block
 block discarded – undo
190 190
 	 * @version 3.1.0
191 191
 	 * @param array $address
192 192
 	 */
193
-	public function calculate_shipping( $address = array() ) {
194
-		$country  = strtoupper( $address['countryCode'] );
195
-		$state    = strtoupper( $address['administrativeArea'] );
193
+	public function calculate_shipping($address = array()) {
194
+		$country  = strtoupper($address['countryCode']);
195
+		$state    = strtoupper($address['administrativeArea']);
196 196
 		$postcode = $address['postalCode'];
197 197
 		$city     = $address['locality'];
198 198
 
199 199
 		WC()->shipping->reset_shipping();
200 200
 
201
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
202
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
203
-		} elseif ( $postcode ) {
204
-			$postcode = wc_format_postcode( $postcode, $country );
201
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
202
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
203
+		} elseif ($postcode) {
204
+			$postcode = wc_format_postcode($postcode, $country);
205 205
 		}
206 206
 
207
-		if ( $country ) {
208
-			WC()->customer->set_location( $country, $state, $postcode, $city );
209
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
207
+		if ($country) {
208
+			WC()->customer->set_location($country, $state, $postcode, $city);
209
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
210 210
 		} else {
211 211
 			WC()->customer->set_to_base();
212 212
 			WC()->customer->set_shipping_to_base();
213 213
 		}
214 214
 
215
-		WC()->customer->calculated_shipping( true );
215
+		WC()->customer->calculated_shipping(true);
216 216
 
217 217
 		/** 
218 218
 		 * Set the shipping package.
@@ -234,17 +234,17 @@  discard block
 block discarded – undo
234 234
 		$packages[0]['destination']['postcode']  = $postcode;
235 235
 		$packages[0]['destination']['city']      = $city;
236 236
 
237
-		foreach ( WC()->cart->get_cart() as $item ) {
238
-			if ( $item['data']->needs_shipping() ) {
239
-				if ( isset( $item['line_total'] ) ) {
237
+		foreach (WC()->cart->get_cart() as $item) {
238
+			if ($item['data']->needs_shipping()) {
239
+				if (isset($item['line_total'])) {
240 240
 					$packages[0]['contents_cost'] += $item['line_total'];
241 241
 				}
242 242
 			}
243 243
 		}
244 244
 
245
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
245
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
246 246
 
247
-		WC()->shipping->calculate_shipping( $packages );
247
+		WC()->shipping->calculate_shipping($packages);
248 248
 	}
249 249
 
250 250
 	/**
@@ -254,30 +254,30 @@  discard block
 block discarded – undo
254 254
 	 * @version 3.1.0
255 255
 	 */
256 256
 	public function get_shipping_methods() {
257
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
258
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
257
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
258
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
259 259
 		}
260 260
 
261
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
262
-			define( 'WOOCOMMERCE_CART', true );
261
+		if ( ! defined('WOOCOMMERCE_CART')) {
262
+			define('WOOCOMMERCE_CART', true);
263 263
 		}
264 264
 
265 265
 		try {
266
-			$address = array_map( 'wc_clean', $_POST['address'] );
266
+			$address = array_map('wc_clean', $_POST['address']);
267 267
 
268
-			$this->calculate_shipping( $address );
268
+			$this->calculate_shipping($address);
269 269
 
270 270
 			// Set the shipping options.
271 271
 			$currency = get_woocommerce_currency();
272 272
 			$data     = array();
273 273
 
274
-			if ( ! empty( WC()->shipping->get_packages() ) && WC()->customer->has_calculated_shipping() ) {
275
-				foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
276
-					if ( empty( $package['rates'] ) ) {
277
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
274
+			if ( ! empty(WC()->shipping->get_packages()) && WC()->customer->has_calculated_shipping()) {
275
+				foreach (WC()->shipping->get_packages() as $package_key => $package) {
276
+					if (empty($package['rates'])) {
277
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
278 278
 					}
279 279
 
280
-					foreach ( $package['rates'] as $key => $rate ) {
280
+					foreach ($package['rates'] as $key => $rate) {
281 281
 						$data[] = array(
282 282
 							'id'       => $rate->id,
283 283
 							'label'    => $rate->label,
@@ -291,16 +291,16 @@  discard block
 block discarded – undo
291 291
 				}
292 292
 
293 293
 				// Auto select the first shipping method.
294
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
294
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
295 295
 
296 296
 				WC()->cart->calculate_totals();
297 297
 				
298
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
298
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
299 299
 			} else {
300
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); 
300
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); 
301 301
 			}
302
-		} catch( Exception $e ) {
303
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );			
302
+		} catch (Exception $e) {
303
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));			
304 304
 		}
305 305
 	}
306 306
 
@@ -311,29 +311,29 @@  discard block
 block discarded – undo
311 311
 	 * @version 3.1.0
312 312
 	 */
313 313
 	public function update_shipping_method() {
314
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
315
-			define( 'WOOCOMMERCE_CART', true );
314
+		if ( ! defined('WOOCOMMERCE_CART')) {
315
+			define('WOOCOMMERCE_CART', true);
316 316
 		}
317 317
 
318
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
319
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
318
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
319
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
320 320
 		}
321 321
 
322
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
322
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
323 323
 
324
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
324
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
325 325
 
326 326
 		WC()->cart->calculate_totals();
327 327
 
328 328
 		// Send back the new cart total.
329 329
 		$currency  = get_woocommerce_currency();
330
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
330
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
331 331
 		$data      = array(
332 332
 			'total' => WC()->cart->total,
333 333
 		);
334 334
 
335 335
 		// Include fees and taxes as displayItems.
336
-		foreach ( WC()->cart->fees as $key => $fee ) {
336
+		foreach (WC()->cart->fees as $key => $fee) {
337 337
 			$data['items'][] = array(
338 338
 				'label'  => $fee->name,
339 339
 				'amount' => array(
@@ -342,9 +342,9 @@  discard block
 block discarded – undo
342 342
 				),
343 343
 			);
344 344
 		}
345
-		if ( 0 < $tax_total ) {
345
+		if (0 < $tax_total) {
346 346
 			$data['items'][] = array(
347
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
347
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
348 348
 				'amount' => array(
349 349
 					'currency' => $currency,
350 350
 					'value'    => $tax_total,
@@ -352,7 +352,7 @@  discard block
 block discarded – undo
352 352
 			);
353 353
 		}
354 354
 
355
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
355
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
356 356
 	}
357 357
 
358 358
 	/**
@@ -363,35 +363,35 @@  discard block
 block discarded – undo
363 363
 	 * @version 3.1.0
364 364
 	 */
365 365
 	public function process_apple_pay() {
366
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
367
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
366
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
367
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
368 368
 		}
369 369
 
370 370
 		try {
371
-			$result = array_map( 'wc_clean', $_POST['result'] );
371
+			$result = array_map('wc_clean', $_POST['result']);
372 372
 
373
-			$order = $this->create_order( $result );
373
+			$order = $this->create_order($result);
374 374
 
375 375
 			// Handle payment.
376
-			if ( $order->get_total() > 0 ) {
376
+			if ($order->get_total() > 0) {
377 377
 
378
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
379
-					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 ) ) );
378
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
379
+					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)));
380 380
 				}
381 381
 
382
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
382
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
383 383
 
384 384
 				// Make the request.
385
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
385
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
386 386
 
387
-				if ( is_wp_error( $response ) ) {
387
+				if (is_wp_error($response)) {
388 388
 					$localized_messages = $this->get_localized_messages();
389 389
 
390
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
390
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
391 391
 				}
392 392
 
393 393
 				// Process valid response.
394
-				$this->process_response( $response, $order );
394
+				$this->process_response($response, $order);
395 395
 			} else {
396 396
 				$order->payment_complete();
397 397
 			}
@@ -399,24 +399,24 @@  discard block
 block discarded – undo
399 399
 			// Remove cart.
400 400
 			WC()->cart->empty_cart();
401 401
 
402
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
403
-			update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
402
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
403
+			update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
404 404
 
405 405
 			// Return thank you page redirect.
406
-			wp_send_json( array(
406
+			wp_send_json(array(
407 407
 				'success'  => 'true',
408
-				'redirect' => $this->get_return_url( $order ),
409
-			) );
408
+				'redirect' => $this->get_return_url($order),
409
+			));
410 410
 
411
-		} catch ( Exception $e ) {
412
-			WC()->session->set( 'refresh_totals', true );
413
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
411
+		} catch (Exception $e) {
412
+			WC()->session->set('refresh_totals', true);
413
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
414 414
 
415
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
416
-				$this->send_failed_order_email( $order->id );
415
+			if ($order->has_status(array('pending', 'failed'))) {
416
+				$this->send_failed_order_email($order->id);
417 417
 			}
418 418
 
419
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
419
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
420 420
 		}
421 421
 	}
422 422
 
@@ -426,14 +426,14 @@  discard block
 block discarded – undo
426 426
 	 * @param string $source token
427 427
 	 * @return array()
428 428
 	 */
429
-	protected function generate_payment_request( $order, $source ) {
429
+	protected function generate_payment_request($order, $source) {
430 430
 		$post_data                = array();
431
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
432
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
433
-		$post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() );
431
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
432
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
433
+		$post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), $order->get_order_number());
434 434
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
435 435
 
436
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
436
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
437 437
 			$post_data['receipt_email'] = $order->billing_email;
438 438
 		}
439 439
 
@@ -448,7 +448,7 @@  discard block
 block discarded – undo
448 448
 		 * @param WC_Order $order
449 449
 		 * @param object $source
450 450
 		 */
451
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
451
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
452 452
 	}
453 453
 
454 454
 	/**
@@ -457,14 +457,14 @@  discard block
 block discarded – undo
457 457
 	 * @since 3.1.0
458 458
 	 * @version 3.1.0
459 459
 	 */	
460
-	public function build_shipping_methods( $shipping_methods ) {
461
-		if ( empty( $shipping_methods ) ) {
460
+	public function build_shipping_methods($shipping_methods) {
461
+		if (empty($shipping_methods)) {
462 462
 			return array();
463 463
 		}
464 464
 
465 465
 		$shipping = array();
466 466
 
467
-		foreach( $shipping_methods as $method ) {
467
+		foreach ($shipping_methods as $method) {
468 468
 			$shipping[] = array(
469 469
 				'label'      => $method['label'],
470 470
 				'detail'     => '',
@@ -483,53 +483,53 @@  discard block
 block discarded – undo
483 483
 	 * @version 3.1.0
484 484
 	 */	
485 485
 	public function build_line_items() {
486
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
487
-			define( 'WOOCOMMERCE_CART', true );
486
+		if ( ! defined('WOOCOMMERCE_CART')) {
487
+			define('WOOCOMMERCE_CART', true);
488 488
 		}
489 489
 
490
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
490
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
491 491
 
492 492
 		$items = array();
493 493
 
494
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
495
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
494
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
495
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
496 496
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
497 497
 
498 498
 			$item = array(
499 499
 				'type'   => 'final',
500 500
 				'label'  => $values['data']->post->post_title . $quantity_label,
501
-				'amount' => wc_format_decimal( $amount, $decimals ),
501
+				'amount' => wc_format_decimal($amount, $decimals),
502 502
 			);
503 503
 
504 504
 			$items[] = $item;
505 505
 		}
506 506
 
507
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
508
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
509
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
510
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
511
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
507
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
508
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
509
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
510
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
511
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
512 512
 
513
-		if ( wc_tax_enabled() ) {
513
+		if (wc_tax_enabled()) {
514 514
 			$items[] = array(
515 515
 				'type'   => 'final',
516
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
516
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
517 517
 				'amount' => $tax,
518 518
 			);
519 519
 		}
520 520
 
521
-		if ( WC()->cart->needs_shipping() ) {
521
+		if (WC()->cart->needs_shipping()) {
522 522
 			$items[] = array(
523 523
 				'type'   => 'final',
524
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
524
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
525 525
 				'amount' => $shipping,
526 526
 			);
527 527
 		}
528 528
 
529
-		if ( WC()->cart->has_discount() ) {
529
+		if (WC()->cart->has_discount()) {
530 530
 			$items[] = array(
531 531
 				'type'   => 'final',
532
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
532
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
533 533
 				'amount' => $discounts,
534 534
 			);
535 535
 		}
@@ -545,24 +545,24 @@  discard block
 block discarded – undo
545 545
 	 * @param array $data
546 546
 	 * @return object $order
547 547
 	 */
548
-	public function create_order( $data = array() ) {
549
-		if ( empty( $data ) ) {
550
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
548
+	public function create_order($data = array()) {
549
+		if (empty($data)) {
550
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
551 551
 		}
552 552
 
553 553
 		$order = wc_create_order();
554 554
 
555
-		if ( is_wp_error( $order ) ) {
556
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
557
-		} elseif ( false === $order ) {
558
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
555
+		if (is_wp_error($order)) {
556
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
557
+		} elseif (false === $order) {
558
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
559 559
 		} else {
560 560
 			$order_id = $order->id;
561
-			do_action( 'woocommerce_new_order', $order_id );
561
+			do_action('woocommerce_new_order', $order_id);
562 562
 		}
563 563
 
564 564
 		// Store the line items to the new/resumed order
565
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
565
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
566 566
 			$item_id = $order->add_product(
567 567
 				$values['data'],
568 568
 				$values['quantity'],
@@ -578,47 +578,47 @@  discard block
 block discarded – undo
578 578
 				)
579 579
 			);
580 580
 
581
-			if ( ! $item_id ) {
582
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
581
+			if ( ! $item_id) {
582
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
583 583
 			}
584 584
 
585 585
 			// Allow plugins to add order item meta
586
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
586
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
587 587
 		}
588 588
 
589 589
 		// Store fees
590
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
591
-			$item_id = $order->add_fee( $fee );
590
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
591
+			$item_id = $order->add_fee($fee);
592 592
 
593
-			if ( ! $item_id ) {
594
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
593
+			if ( ! $item_id) {
594
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
595 595
 			}
596 596
 
597 597
 			// Allow plugins to add order item meta to fees
598
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
598
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
599 599
 		}
600 600
 
601 601
 		// Store tax rows
602
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
603
-			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 ) {
604
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
602
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
603
+			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) {
604
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
605 605
 			}
606 606
 		}
607 607
 
608 608
 		// Store coupons
609
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
610
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
611
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
609
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
610
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
611
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
612 612
 			}
613 613
 		}
614 614
 
615 615
 		// Billing address
616 616
 		$billing_address = array();
617
-		if ( ! empty( $data['token']['card'] ) ) {
617
+		if ( ! empty($data['token']['card'])) {
618 618
 			// Name from Stripe is a full name string.
619
-			$name                          = explode( ' ', $data['token']['card']['name'] );
620
-			$lastname                      = array_pop( $name );
621
-			$firstname                     = implode( ' ', $name );
619
+			$name                          = explode(' ', $data['token']['card']['name']);
620
+			$lastname                      = array_pop($name);
621
+			$firstname                     = implode(' ', $name);
622 622
 			$billing_address['first_name'] = $firstname;
623 623
 			$billing_address['last_name']  = $lastname;
624 624
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -633,7 +633,7 @@  discard block
 block discarded – undo
633 633
 
634 634
 		// Shipping address.
635 635
 		$shipping_address = array();
636
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
636
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
637 637
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
638 638
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
639 639
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -644,7 +644,7 @@  discard block
 block discarded – undo
644 644
 			$shipping_address['city']       = $data['shippingContact']['locality'];
645 645
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
646 646
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
647
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
647
+		} elseif ( ! empty($data['shippingContact'])) {
648 648
 			$shipping_address['first_name'] = $firstname;
649 649
 			$shipping_address['last_name']  = $lastname;
650 650
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -657,34 +657,34 @@  discard block
 block discarded – undo
657 657
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
658 658
 		}
659 659
 
660
-		$order->set_address( $billing_address, 'billing' );
661
-		$order->set_address( $shipping_address, 'shipping' );
660
+		$order->set_address($billing_address, 'billing');
661
+		$order->set_address($shipping_address, 'shipping');
662 662
 
663
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
663
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
664 664
 		
665 665
 		// Get the rate object selected by user.
666
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
667
-			foreach ( $package['rates'] as $key => $rate ) {
666
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
667
+			foreach ($package['rates'] as $key => $rate) {
668 668
 				// Loop through user chosen shipping methods.
669
-				foreach( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
670
-					if ( $method === $key ) {
671
-						$order->add_shipping( $rate );
669
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
670
+					if ($method === $key) {
671
+						$order->add_shipping($rate);
672 672
 					}
673 673
 				}
674 674
 			}
675 675
 		}
676 676
 
677 677
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
678
-		$order->set_payment_method( $available_gateways['stripe'] );
679
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
680
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
681
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
682
-		$order->set_total( WC()->cart->tax_total, 'tax' );
683
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
684
-		$order->set_total( WC()->cart->total );
678
+		$order->set_payment_method($available_gateways['stripe']);
679
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
680
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
681
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
682
+		$order->set_total(WC()->cart->tax_total, 'tax');
683
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
684
+		$order->set_total(WC()->cart->total);
685 685
 
686 686
 		// If we got here, the order was created without problems!
687
-		wc_transaction_query( 'commit' );
687
+		wc_transaction_query('commit');
688 688
 
689 689
 		return $order;
690 690
 	}
Please login to merge, or discard this patch.