Completed
Push — master ( 77bb9a...31cdbb )
by Roy
03:33
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +212 added lines, -212 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,35 +66,35 @@  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, 'cart_scripts' ) );
70
-		add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) );
71
-		add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 );
72
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 );
73
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 );
74
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 );
75
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
76
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
77
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
78
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
79
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
80
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
81
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
69
+		add_action('wp_enqueue_scripts', array($this, 'cart_scripts'));
70
+		add_action('wp_enqueue_scripts', array($this, 'single_scripts'));
71
+		add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1);
72
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1);
73
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2);
74
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1);
75
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2);
76
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
77
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
78
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single'));
79
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
80
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
81
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
82 82
 	}
83 83
 
84 84
 	/**
85 85
 	 * Filters the gateway title to reflect Apple Pay.
86 86
 	 *
87 87
 	 */
88
-	public function filter_gateway_title( $title, $id ) {
88
+	public function filter_gateway_title($title, $id) {
89 89
 		global $post;
90 90
 
91
-		if ( ! is_object( $post ) ) {
91
+		if ( ! is_object($post)) {
92 92
 			return $title;
93 93
 		}
94 94
 
95
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
95
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
96 96
 
97
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
97
+		if ('stripe' === $id && ! empty($method_title)) {
98 98
 			return $method_title;
99 99
 		}
100 100
 
@@ -108,41 +108,41 @@  discard block
 block discarded – undo
108 108
 	 * @version 3.1.0
109 109
 	 */
110 110
 	public function single_scripts() {
111
-		if ( ! is_single() ) {
111
+		if ( ! is_single()) {
112 112
 			return;
113 113
 		}
114 114
 
115 115
 		global $post;
116 116
 
117
-		$product = wc_get_product( $post->ID );
117
+		$product = wc_get_product($post->ID);
118 118
 
119
-		if ( ! in_array( $product->product_type, $this->supported_product_types() ) ) {
119
+		if ( ! in_array($product->product_type, $this->supported_product_types())) {
120 120
 			return;
121 121
 		}
122 122
 
123
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
123
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
124 124
 
125
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
125
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
126 126
 
127
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
128
-		wp_enqueue_script( 'woocommerce_stripe_apple_pay_single', plugins_url( 'assets/js/stripe-apple-pay-single' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true );
127
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
128
+		wp_enqueue_script('woocommerce_stripe_apple_pay_single', plugins_url('assets/js/stripe-apple-pay-single' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true);
129 129
 
130 130
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
131 131
 
132 132
 		$stripe_params = array(
133 133
 			'key'                                           => $publishable_key,
134 134
 			'currency_code'                                 => get_woocommerce_currency(),
135
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
135
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
136 136
 			'label'                                         => $this->statement_descriptor,
137
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
138
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
139
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
140
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
141
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
137
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
138
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
139
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
140
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
141
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
142 142
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
143 143
 		);
144 144
 
145
-		wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
145
+		wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params));
146 146
 	}
147 147
 
148 148
 	/**
@@ -152,34 +152,34 @@  discard block
 block discarded – undo
152 152
 	 * @version 3.1.0
153 153
 	 */
154 154
 	public function cart_scripts() {
155
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
155
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
156 156
 			return;
157 157
 		}
158 158
 
159
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
159
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
160 160
 
161
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
161
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
162 162
 
163
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
164
-		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 );
163
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
164
+		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);
165 165
 
166 166
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
167 167
 
168 168
 		$stripe_params = array(
169 169
 			'key'                                           => $publishable_key,
170 170
 			'currency_code'                                 => get_woocommerce_currency(),
171
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
171
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
172 172
 			'label'                                         => $this->statement_descriptor,
173
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
174
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
175
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
176
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
177
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
173
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
174
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
175
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
176
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
177
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
178 178
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
179 179
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
180 180
 		);
181 181
 
182
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
182
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
183 183
 	}
184 184
 
185 185
 	/**
@@ -208,25 +208,25 @@  discard block
 block discarded – undo
208 208
 		 */
209 209
 		if (
210 210
 			'yes' !== $this->_gateway_settings['apple_pay']
211
-			|| ! isset( $gateways['stripe'] )
211
+			|| ! isset($gateways['stripe'])
212 212
 		) {
213 213
 			return;
214 214
 		}
215 215
 
216
-		if ( is_single() ) {
216
+		if (is_single()) {
217 217
 			global $post;
218 218
 
219
-			$product = wc_get_product( $post->ID );
219
+			$product = wc_get_product($post->ID);
220 220
 
221
-			if ( ! in_array( $product->product_type, $this->supported_product_types() ) ) {
221
+			if ( ! in_array($product->product_type, $this->supported_product_types())) {
222 222
 				return;
223 223
 			}
224 224
 		}
225 225
 
226
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
227
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
226
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
227
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
228 228
 		?>
229
-		<button class="apple-pay-button" lang="<?php echo esc_attr( $button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button>
229
+		<button class="apple-pay-button" lang="<?php echo esc_attr($button_lang); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($apple_pay_button); ?>;"></button>
230 230
 		<?php
231 231
 	}
232 232
 
@@ -245,22 +245,22 @@  discard block
 block discarded – undo
245 245
 		 */
246 246
 		if (
247 247
 			'yes' !== $this->_gateway_settings['apple_pay']
248
-			|| ! isset( $gateways['stripe'] )
248
+			|| ! isset($gateways['stripe'])
249 249
 		) {
250 250
 			return;
251 251
 		}
252 252
 
253
-		if ( is_single() ) {
253
+		if (is_single()) {
254 254
 			global $post;
255 255
 
256
-			$product = wc_get_product( $post->ID );
256
+			$product = wc_get_product($post->ID);
257 257
 
258
-			if ( ! in_array( $product->product_type, $this->supported_product_types() ) ) {
258
+			if ( ! in_array($product->product_type, $this->supported_product_types())) {
259 259
 				return;
260 260
 			}
261 261
 		}
262 262
 		?>
263
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
263
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
264 264
 		<?php
265 265
 	}
266 266
 
@@ -271,44 +271,44 @@  discard block
 block discarded – undo
271 271
 	 * @version 3.1.0
272 272
 	 */
273 273
 	public function generate_apple_pay_single() {
274
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
275
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
274
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
275
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
276 276
 		}
277 277
 
278
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
279
-			define( 'WOOCOMMERCE_CART', true );
278
+		if ( ! defined('WOOCOMMERCE_CART')) {
279
+			define('WOOCOMMERCE_CART', true);
280 280
 		}
281 281
 
282 282
 		global $post;
283 283
 
284
-		$product = wc_get_product( $post->ID );
285
-		$qty     = absint( $_POST['qty'] );
284
+		$product = wc_get_product($post->ID);
285
+		$qty     = absint($_POST['qty']);
286 286
 
287 287
 		/**
288 288
 		 * If this page is single product page, we need to simulate
289 289
 		 * adding the product to the cart taken account if it is a
290 290
 		 * simple or variable product.
291 291
 		 */
292
-		if ( is_single() ) {
292
+		if (is_single()) {
293 293
 			// First empty the cart to prevent wrong calculation.
294 294
 			WC()->cart->empty_cart();
295 295
 
296
-			if ( 'variable' === $product->product_type && isset( $_POST['attributes'] ) ) {
297
-				$attributes = array_map( 'wc_clean', $_POST['attributes'] );
296
+			if ('variable' === $product->product_type && isset($_POST['attributes'])) {
297
+				$attributes = array_map('wc_clean', $_POST['attributes']);
298 298
 
299
-				$variation_id = $product->get_matching_variation( $attributes );
299
+				$variation_id = $product->get_matching_variation($attributes);
300 300
 
301
-				WC()->cart->add_to_cart( $product->id, $qty, $variation_id, $attributes );
301
+				WC()->cart->add_to_cart($product->id, $qty, $variation_id, $attributes);
302 302
 			}
303 303
 
304
-			if ( 'simple' === $product->product_type ) {
305
-				WC()->cart->add_to_cart( $product->id, $qty );
304
+			if ('simple' === $product->product_type) {
305
+				WC()->cart->add_to_cart($product->id, $qty);
306 306
 			}
307 307
 		}
308 308
 
309 309
 		WC()->cart->calculate_totals();
310 310
 
311
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
311
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
312 312
 	}
313 313
 
314 314
 	/**
@@ -318,11 +318,11 @@  discard block
 block discarded – undo
318 318
 	 * @version 3.1.0
319 319
 	 */
320 320
 	public function generate_apple_pay_cart() {
321
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
322
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
321
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
322
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
323 323
 		}
324 324
 
325
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
325
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
326 326
 	}
327 327
 
328 328
 	/**
@@ -332,29 +332,29 @@  discard block
 block discarded – undo
332 332
 	 * @version 3.1.0
333 333
 	 * @param array $address
334 334
 	 */
335
-	public function calculate_shipping( $address = array() ) {
336
-		$country  = strtoupper( $address['countryCode'] );
337
-		$state    = strtoupper( $address['administrativeArea'] );
335
+	public function calculate_shipping($address = array()) {
336
+		$country  = strtoupper($address['countryCode']);
337
+		$state    = strtoupper($address['administrativeArea']);
338 338
 		$postcode = $address['postalCode'];
339 339
 		$city     = $address['locality'];
340 340
 
341 341
 		WC()->shipping->reset_shipping();
342 342
 
343
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
344
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
345
-		} elseif ( $postcode ) {
346
-			$postcode = wc_format_postcode( $postcode, $country );
343
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
344
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
345
+		} elseif ($postcode) {
346
+			$postcode = wc_format_postcode($postcode, $country);
347 347
 		}
348 348
 
349
-		if ( $country ) {
350
-			WC()->customer->set_location( $country, $state, $postcode, $city );
351
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
349
+		if ($country) {
350
+			WC()->customer->set_location($country, $state, $postcode, $city);
351
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
352 352
 		} else {
353 353
 			WC()->customer->set_to_base();
354 354
 			WC()->customer->set_shipping_to_base();
355 355
 		}
356 356
 
357
-		WC()->customer->calculated_shipping( true );
357
+		WC()->customer->calculated_shipping(true);
358 358
 
359 359
 		/**
360 360
 		 * Set the shipping package.
@@ -376,17 +376,17 @@  discard block
 block discarded – undo
376 376
 		$packages[0]['destination']['postcode']  = $postcode;
377 377
 		$packages[0]['destination']['city']      = $city;
378 378
 
379
-		foreach ( WC()->cart->get_cart() as $item ) {
380
-			if ( $item['data']->needs_shipping() ) {
381
-				if ( isset( $item['line_total'] ) ) {
379
+		foreach (WC()->cart->get_cart() as $item) {
380
+			if ($item['data']->needs_shipping()) {
381
+				if (isset($item['line_total'])) {
382 382
 					$packages[0]['contents_cost'] += $item['line_total'];
383 383
 				}
384 384
 			}
385 385
 		}
386 386
 
387
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
387
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
388 388
 
389
-		WC()->shipping->calculate_shipping( $packages );
389
+		WC()->shipping->calculate_shipping($packages);
390 390
 	}
391 391
 
392 392
 	/**
@@ -396,18 +396,18 @@  discard block
 block discarded – undo
396 396
 	 * @version 3.1.0
397 397
 	 */
398 398
 	public function get_shipping_methods() {
399
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
400
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
399
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
400
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
401 401
 		}
402 402
 
403
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
404
-			define( 'WOOCOMMERCE_CART', true );
403
+		if ( ! defined('WOOCOMMERCE_CART')) {
404
+			define('WOOCOMMERCE_CART', true);
405 405
 		}
406 406
 
407 407
 		try {
408
-			$address = array_map( 'wc_clean', $_POST['address'] );
408
+			$address = array_map('wc_clean', $_POST['address']);
409 409
 
410
-			$this->calculate_shipping( $address );
410
+			$this->calculate_shipping($address);
411 411
 
412 412
 			// Set the shipping options.
413 413
 			$currency = get_woocommerce_currency();
@@ -415,13 +415,13 @@  discard block
 block discarded – undo
415 415
 
416 416
 			$packages = WC()->shipping->get_packages();
417 417
 
418
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
419
-				foreach ( $packages as $package_key => $package ) {
420
-					if ( empty( $package['rates'] ) ) {
421
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
418
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
419
+				foreach ($packages as $package_key => $package) {
420
+					if (empty($package['rates'])) {
421
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
422 422
 					}
423 423
 
424
-					foreach ( $package['rates'] as $key => $rate ) {
424
+					foreach ($package['rates'] as $key => $rate) {
425 425
 						$data[] = array(
426 426
 							'id'       => $rate->id,
427 427
 							'label'    => $rate->label,
@@ -435,16 +435,16 @@  discard block
 block discarded – undo
435 435
 				}
436 436
 
437 437
 				// Auto select the first shipping method.
438
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
438
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
439 439
 
440 440
 				WC()->cart->calculate_totals();
441 441
 
442
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
442
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
443 443
 			} else {
444
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
444
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
445 445
 			}
446
-		} catch ( Exception $e ) {
447
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
446
+		} catch (Exception $e) {
447
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
448 448
 		}
449 449
 	}
450 450
 
@@ -455,29 +455,29 @@  discard block
 block discarded – undo
455 455
 	 * @version 3.1.0
456 456
 	 */
457 457
 	public function update_shipping_method() {
458
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
459
-			define( 'WOOCOMMERCE_CART', true );
458
+		if ( ! defined('WOOCOMMERCE_CART')) {
459
+			define('WOOCOMMERCE_CART', true);
460 460
 		}
461 461
 
462
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
463
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
462
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
463
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
464 464
 		}
465 465
 
466
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
466
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
467 467
 
468
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
468
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
469 469
 
470 470
 		WC()->cart->calculate_totals();
471 471
 
472 472
 		// Send back the new cart total.
473 473
 		$currency  = get_woocommerce_currency();
474
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
474
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
475 475
 		$data      = array(
476 476
 			'total' => WC()->cart->total,
477 477
 		);
478 478
 
479 479
 		// Include fees and taxes as displayItems.
480
-		foreach ( WC()->cart->fees as $key => $fee ) {
480
+		foreach (WC()->cart->fees as $key => $fee) {
481 481
 			$data['items'][] = array(
482 482
 				'label'  => $fee->name,
483 483
 				'amount' => array(
@@ -486,9 +486,9 @@  discard block
 block discarded – undo
486 486
 				),
487 487
 			);
488 488
 		}
489
-		if ( 0 < $tax_total ) {
489
+		if (0 < $tax_total) {
490 490
 			$data['items'][] = array(
491
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
491
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
492 492
 				'amount' => array(
493 493
 					'currency' => $currency,
494 494
 					'value'    => $tax_total,
@@ -496,7 +496,7 @@  discard block
 block discarded – undo
496 496
 			);
497 497
 		}
498 498
 
499
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
499
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
500 500
 	}
501 501
 
502 502
 	/**
@@ -507,35 +507,35 @@  discard block
 block discarded – undo
507 507
 	 * @version 3.1.0
508 508
 	 */
509 509
 	public function process_apple_pay() {
510
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
511
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
510
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
511
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
512 512
 		}
513 513
 
514 514
 		try {
515
-			$result = array_map( 'wc_clean', $_POST['result'] );
515
+			$result = array_map('wc_clean', $_POST['result']);
516 516
 
517
-			$order = $this->create_order( $result );
517
+			$order = $this->create_order($result);
518 518
 
519 519
 			// Handle payment.
520
-			if ( $order->get_total() > 0 ) {
520
+			if ($order->get_total() > 0) {
521 521
 
522
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
523
-					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 ) ) );
522
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
523
+					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)));
524 524
 				}
525 525
 
526
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
526
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
527 527
 
528 528
 				// Make the request.
529
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
529
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
530 530
 
531
-				if ( is_wp_error( $response ) ) {
531
+				if (is_wp_error($response)) {
532 532
 					$localized_messages = $this->get_localized_messages();
533 533
 
534
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
534
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
535 535
 				}
536 536
 
537 537
 				// Process valid response.
538
-				$this->process_response( $response, $order );
538
+				$this->process_response($response, $order);
539 539
 			} else {
540 540
 				$order->payment_complete();
541 541
 			}
@@ -543,24 +543,24 @@  discard block
 block discarded – undo
543 543
 			// Remove cart.
544 544
 			WC()->cart->empty_cart();
545 545
 
546
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
547
-			update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
546
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
547
+			update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
548 548
 
549 549
 			// Return thank you page redirect.
550
-			wp_send_json( array(
550
+			wp_send_json(array(
551 551
 				'success'  => 'true',
552
-				'redirect' => $this->get_return_url( $order ),
553
-			) );
552
+				'redirect' => $this->get_return_url($order),
553
+			));
554 554
 
555
-		} catch ( Exception $e ) {
556
-			WC()->session->set( 'refresh_totals', true );
557
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
555
+		} catch (Exception $e) {
556
+			WC()->session->set('refresh_totals', true);
557
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
558 558
 
559
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
560
-				$this->send_failed_order_email( $order->id );
559
+			if ($order->has_status(array('pending', 'failed'))) {
560
+				$this->send_failed_order_email($order->id);
561 561
 			}
562 562
 
563
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
563
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
564 564
 		}
565 565
 	}
566 566
 
@@ -570,14 +570,14 @@  discard block
 block discarded – undo
570 570
 	 * @param string $source token
571 571
 	 * @return array()
572 572
 	 */
573
-	protected function generate_payment_request( $order, $source ) {
573
+	protected function generate_payment_request($order, $source) {
574 574
 		$post_data                = array();
575
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
576
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
577
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
575
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
576
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
577
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
578 578
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
579 579
 
580
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
580
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
581 581
 			$post_data['receipt_email'] = $order->billing_email;
582 582
 		}
583 583
 
@@ -592,7 +592,7 @@  discard block
 block discarded – undo
592 592
 		 * @param WC_Order $order
593 593
 		 * @param object $source
594 594
 		 */
595
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
595
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
596 596
 	}
597 597
 
598 598
 	/**
@@ -601,14 +601,14 @@  discard block
 block discarded – undo
601 601
 	 * @since 3.1.0
602 602
 	 * @version 3.1.0
603 603
 	 */
604
-	public function build_shipping_methods( $shipping_methods ) {
605
-		if ( empty( $shipping_methods ) ) {
604
+	public function build_shipping_methods($shipping_methods) {
605
+		if (empty($shipping_methods)) {
606 606
 			return array();
607 607
 		}
608 608
 
609 609
 		$shipping = array();
610 610
 
611
-		foreach ( $shipping_methods as $method ) {
611
+		foreach ($shipping_methods as $method) {
612 612
 			$shipping[] = array(
613 613
 				'label'      => $method['label'],
614 614
 				'detail'     => '',
@@ -627,53 +627,53 @@  discard block
 block discarded – undo
627 627
 	 * @version 3.1.0
628 628
 	 */
629 629
 	public function build_line_items() {
630
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
631
-			define( 'WOOCOMMERCE_CART', true );
630
+		if ( ! defined('WOOCOMMERCE_CART')) {
631
+			define('WOOCOMMERCE_CART', true);
632 632
 		}
633 633
 
634
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
634
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
635 635
 
636 636
 		$items = array();
637 637
 
638
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
639
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
638
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
639
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
640 640
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
641 641
 
642 642
 			$item = array(
643 643
 				'type'   => 'final',
644 644
 				'label'  => $values['data']->post->post_title . $quantity_label,
645
-				'amount' => wc_format_decimal( $amount, $decimals ),
645
+				'amount' => wc_format_decimal($amount, $decimals),
646 646
 			);
647 647
 
648 648
 			$items[] = $item;
649 649
 		}
650 650
 
651
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
652
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
653
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
654
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
655
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
651
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
652
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
653
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
654
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
655
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
656 656
 
657
-		if ( wc_tax_enabled() ) {
657
+		if (wc_tax_enabled()) {
658 658
 			$items[] = array(
659 659
 				'type'   => 'final',
660
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
660
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
661 661
 				'amount' => $tax,
662 662
 			);
663 663
 		}
664 664
 
665
-		if ( WC()->cart->needs_shipping() ) {
665
+		if (WC()->cart->needs_shipping()) {
666 666
 			$items[] = array(
667 667
 				'type'   => 'final',
668
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
668
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
669 669
 				'amount' => $shipping,
670 670
 			);
671 671
 		}
672 672
 
673
-		if ( WC()->cart->has_discount() ) {
673
+		if (WC()->cart->has_discount()) {
674 674
 			$items[] = array(
675 675
 				'type'   => 'final',
676
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
676
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
677 677
 				'amount' => $discounts,
678 678
 			);
679 679
 		}
@@ -689,24 +689,24 @@  discard block
 block discarded – undo
689 689
 	 * @param array $data
690 690
 	 * @return object $order
691 691
 	 */
692
-	public function create_order( $data = array() ) {
693
-		if ( empty( $data ) ) {
694
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
692
+	public function create_order($data = array()) {
693
+		if (empty($data)) {
694
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
695 695
 		}
696 696
 
697 697
 		$order = wc_create_order();
698 698
 
699
-		if ( is_wp_error( $order ) ) {
700
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
701
-		} elseif ( false === $order ) {
702
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
699
+		if (is_wp_error($order)) {
700
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
701
+		} elseif (false === $order) {
702
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
703 703
 		} else {
704 704
 			$order_id = $order->id;
705
-			do_action( 'woocommerce_new_order', $order_id );
705
+			do_action('woocommerce_new_order', $order_id);
706 706
 		}
707 707
 
708 708
 		// Store the line items to the new/resumed order
709
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
709
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
710 710
 			$item_id = $order->add_product(
711 711
 				$values['data'],
712 712
 				$values['quantity'],
@@ -722,47 +722,47 @@  discard block
 block discarded – undo
722 722
 				)
723 723
 			);
724 724
 
725
-			if ( ! $item_id ) {
726
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
725
+			if ( ! $item_id) {
726
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
727 727
 			}
728 728
 
729 729
 			// Allow plugins to add order item meta
730
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
730
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
731 731
 		}
732 732
 
733 733
 		// Store fees
734
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
735
-			$item_id = $order->add_fee( $fee );
734
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
735
+			$item_id = $order->add_fee($fee);
736 736
 
737
-			if ( ! $item_id ) {
738
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
737
+			if ( ! $item_id) {
738
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
739 739
 			}
740 740
 
741 741
 			// Allow plugins to add order item meta to fees
742
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
742
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
743 743
 		}
744 744
 
745 745
 		// Store tax rows
746
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
747
-			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 ) {
748
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
746
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
747
+			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) {
748
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
749 749
 			}
750 750
 		}
751 751
 
752 752
 		// Store coupons
753
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
754
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
755
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
753
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
754
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
755
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
756 756
 			}
757 757
 		}
758 758
 
759 759
 		// Billing address
760 760
 		$billing_address = array();
761
-		if ( ! empty( $data['token']['card'] ) ) {
761
+		if ( ! empty($data['token']['card'])) {
762 762
 			// Name from Stripe is a full name string.
763
-			$name                          = explode( ' ', $data['token']['card']['name'] );
764
-			$lastname                      = array_pop( $name );
765
-			$firstname                     = implode( ' ', $name );
763
+			$name                          = explode(' ', $data['token']['card']['name']);
764
+			$lastname                      = array_pop($name);
765
+			$firstname                     = implode(' ', $name);
766 766
 			$billing_address['first_name'] = $firstname;
767 767
 			$billing_address['last_name']  = $lastname;
768 768
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -777,7 +777,7 @@  discard block
 block discarded – undo
777 777
 
778 778
 		// Shipping address.
779 779
 		$shipping_address = array();
780
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
780
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
781 781
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
782 782
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
783 783
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -788,7 +788,7 @@  discard block
 block discarded – undo
788 788
 			$shipping_address['city']       = $data['shippingContact']['locality'];
789 789
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
790 790
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
791
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
791
+		} elseif ( ! empty($data['shippingContact'])) {
792 792
 			$shipping_address['first_name'] = $firstname;
793 793
 			$shipping_address['last_name']  = $lastname;
794 794
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -801,34 +801,34 @@  discard block
 block discarded – undo
801 801
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
802 802
 		}
803 803
 
804
-		$order->set_address( $billing_address, 'billing' );
805
-		$order->set_address( $shipping_address, 'shipping' );
804
+		$order->set_address($billing_address, 'billing');
805
+		$order->set_address($shipping_address, 'shipping');
806 806
 
807
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
807
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
808 808
 
809 809
 		// Get the rate object selected by user.
810
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
811
-			foreach ( $package['rates'] as $key => $rate ) {
810
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
811
+			foreach ($package['rates'] as $key => $rate) {
812 812
 				// Loop through user chosen shipping methods.
813
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
814
-					if ( $method === $key ) {
815
-						$order->add_shipping( $rate );
813
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
814
+					if ($method === $key) {
815
+						$order->add_shipping($rate);
816 816
 					}
817 817
 				}
818 818
 			}
819 819
 		}
820 820
 
821 821
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
822
-		$order->set_payment_method( $available_gateways['stripe'] );
823
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
824
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
825
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
826
-		$order->set_total( WC()->cart->tax_total, 'tax' );
827
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
828
-		$order->set_total( WC()->cart->total );
822
+		$order->set_payment_method($available_gateways['stripe']);
823
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
824
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
825
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
826
+		$order->set_total(WC()->cart->tax_total, 'tax');
827
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
828
+		$order->set_total(WC()->cart->total);
829 829
 
830 830
 		// If we got here, the order was created without problems!
831
-		wc_transaction_query( 'commit' );
831
+		wc_transaction_query('commit');
832 832
 
833 833
 		return $order;
834 834
 	}
Please login to merge, or discard this patch.