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