Completed
Push — master ( 92e0cd...602bf5 )
by Roy
02:15
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +223 added lines, -223 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,46 +66,46 @@  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' ) );
69
+		add_action('wp_enqueue_scripts', array($this, 'cart_scripts'));
70
+		add_action('wp_enqueue_scripts', array($this, 'single_scripts'));
71 71
 
72 72
 		/**
73 73
 		 * In order to display the Apple Pay button in the correct position,
74 74
 		 * a new hook was added to WooCommerce 2.7. In older versions of WooCommerce,
75 75
 		 * CSS is used to position the button.
76 76
 		 */
77
-		if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
78
-			add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 );
77
+		if (version_compare(WC_VERSION, '3.0.0', '<')) {
78
+			add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1);
79 79
 		} else {
80
-			add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_apple_pay_button' ), 1 );
81
-		}
82
-
83
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 );
84
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 );
85
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 );
86
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
87
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
88
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
89
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
90
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
91
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
92
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
80
+			add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1);
81
+		}
82
+
83
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1);
84
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2);
85
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1);
86
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2);
87
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
88
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
89
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single'));
90
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
91
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
92
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
93 93
 	}
94 94
 
95 95
 	/**
96 96
 	 * Filters the gateway title to reflect Apple Pay.
97 97
 	 *
98 98
 	 */
99
-	public function filter_gateway_title( $title, $id ) {
99
+	public function filter_gateway_title($title, $id) {
100 100
 		global $post;
101 101
 
102
-		if ( ! is_object( $post ) ) {
102
+		if ( ! is_object($post)) {
103 103
 			return $title;
104 104
 		}
105 105
 
106
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
106
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
107 107
 
108
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
108
+		if ('stripe' === $id && ! empty($method_title)) {
109 109
 			return $method_title;
110 110
 		}
111 111
 
@@ -119,41 +119,41 @@  discard block
 block discarded – undo
119 119
 	 * @version 3.1.0
120 120
 	 */
121 121
 	public function single_scripts() {
122
-		if ( ! is_single() ) {
122
+		if ( ! is_single()) {
123 123
 			return;
124 124
 		}
125 125
 
126 126
 		global $post;
127 127
 
128
-		$product = wc_get_product( $post->ID );
128
+		$product = wc_get_product($post->ID);
129 129
 
130
-		if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
130
+		if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
131 131
 			return;
132 132
 		}
133 133
 
134
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
134
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
135 135
 
136
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
136
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
137 137
 
138
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
139
-		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 );
138
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
139
+		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);
140 140
 
141 141
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
142 142
 
143 143
 		$stripe_params = array(
144 144
 			'key'                                           => $publishable_key,
145 145
 			'currency_code'                                 => get_woocommerce_currency(),
146
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
146
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
147 147
 			'label'                                         => $this->statement_descriptor,
148
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
149
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
150
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
151
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
152
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
148
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
149
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
150
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
151
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
152
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
153 153
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
154 154
 		);
155 155
 
156
-		wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
156
+		wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params));
157 157
 	}
158 158
 
159 159
 	/**
@@ -163,34 +163,34 @@  discard block
 block discarded – undo
163 163
 	 * @version 3.1.0
164 164
 	 */
165 165
 	public function cart_scripts() {
166
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
166
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
167 167
 			return;
168 168
 		}
169 169
 
170
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
170
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
171 171
 
172
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
172
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
173 173
 
174
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
175
-		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 );
174
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
175
+		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);
176 176
 
177 177
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
178 178
 
179 179
 		$stripe_params = array(
180 180
 			'key'                                           => $publishable_key,
181 181
 			'currency_code'                                 => get_woocommerce_currency(),
182
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
182
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
183 183
 			'label'                                         => $this->statement_descriptor,
184
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
185
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
186
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
187
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
188
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
184
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
185
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
186
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
187
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
188
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
189 189
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
190 190
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
191 191
 		);
192 192
 
193
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
193
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
194 194
 	}
195 195
 
196 196
 	/**
@@ -219,26 +219,26 @@  discard block
 block discarded – undo
219 219
 		 */
220 220
 		if (
221 221
 			'yes' !== $this->_gateway_settings['apple_pay']
222
-			|| ! isset( $gateways['stripe'] )
222
+			|| ! isset($gateways['stripe'])
223 223
 		) {
224 224
 			return;
225 225
 		}
226 226
 
227
-		if ( is_single() ) {
227
+		if (is_single()) {
228 228
 			global $post;
229 229
 
230
-			$product = wc_get_product( $post->ID );
230
+			$product = wc_get_product($post->ID);
231 231
 
232
-			if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
232
+			if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
233 233
 				return;
234 234
 			}
235 235
 		}
236 236
 
237
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
238
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
237
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
238
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
239 239
 		?>
240 240
 		<div class="apple-pay-button-wrapper">
241
-			<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>
241
+			<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>
242 242
 		</div>
243 243
 		<?php
244 244
 	}
@@ -258,22 +258,22 @@  discard block
 block discarded – undo
258 258
 		 */
259 259
 		if (
260 260
 			'yes' !== $this->_gateway_settings['apple_pay']
261
-			|| ! isset( $gateways['stripe'] )
261
+			|| ! isset($gateways['stripe'])
262 262
 		) {
263 263
 			return;
264 264
 		}
265 265
 
266
-		if ( is_single() ) {
266
+		if (is_single()) {
267 267
 			global $post;
268 268
 
269
-			$product = wc_get_product( $post->ID );
269
+			$product = wc_get_product($post->ID);
270 270
 
271
-			if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
271
+			if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
272 272
 				return;
273 273
 			}
274 274
 		}
275 275
 		?>
276
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
276
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
277 277
 		<?php
278 278
 	}
279 279
 
@@ -284,46 +284,46 @@  discard block
 block discarded – undo
284 284
 	 * @version 3.1.0
285 285
 	 */
286 286
 	public function generate_apple_pay_single() {
287
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
288
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
287
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
288
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
289 289
 		}
290 290
 
291
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
292
-			define( 'WOOCOMMERCE_CART', true );
291
+		if ( ! defined('WOOCOMMERCE_CART')) {
292
+			define('WOOCOMMERCE_CART', true);
293 293
 		}
294 294
 
295 295
 		WC()->shipping->reset_shipping();
296 296
 
297 297
 		global $post;
298 298
 
299
-		$product = wc_get_product( $post->ID );
300
-		$qty     = absint( $_POST['qty'] );
299
+		$product = wc_get_product($post->ID);
300
+		$qty     = absint($_POST['qty']);
301 301
 
302 302
 		/**
303 303
 		 * If this page is single product page, we need to simulate
304 304
 		 * adding the product to the cart taken account if it is a
305 305
 		 * simple or variable product.
306 306
 		 */
307
-		if ( is_single() ) {
307
+		if (is_single()) {
308 308
 			// First empty the cart to prevent wrong calculation.
309 309
 			WC()->cart->empty_cart();
310 310
 
311
-			if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) {
312
-				$attributes = array_map( 'wc_clean', $_POST['attributes'] );
311
+			if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) {
312
+				$attributes = array_map('wc_clean', $_POST['attributes']);
313 313
 
314
-				$variation_id = $product->get_matching_variation( $attributes );
314
+				$variation_id = $product->get_matching_variation($attributes);
315 315
 
316
-				WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
316
+				WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes);
317 317
 			}
318 318
 
319
-			if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) {
320
-				WC()->cart->add_to_cart( $product->get_id(), $qty );
319
+			if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) {
320
+				WC()->cart->add_to_cart($product->get_id(), $qty);
321 321
 			}
322 322
 		}
323 323
 
324 324
 		WC()->cart->calculate_totals();
325 325
 
326
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
326
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
327 327
 	}
328 328
 
329 329
 	/**
@@ -333,11 +333,11 @@  discard block
 block discarded – undo
333 333
 	 * @version 3.1.0
334 334
 	 */
335 335
 	public function generate_apple_pay_cart() {
336
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
337
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
336
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
337
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
338 338
 		}
339 339
 
340
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
340
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
341 341
 	}
342 342
 
343 343
 	/**
@@ -347,29 +347,29 @@  discard block
 block discarded – undo
347 347
 	 * @version 3.1.0
348 348
 	 * @param array $address
349 349
 	 */
350
-	public function calculate_shipping( $address = array() ) {
351
-		$country  = strtoupper( $address['countryCode'] );
352
-		$state    = strtoupper( $address['administrativeArea'] );
350
+	public function calculate_shipping($address = array()) {
351
+		$country  = strtoupper($address['countryCode']);
352
+		$state    = strtoupper($address['administrativeArea']);
353 353
 		$postcode = $address['postalCode'];
354 354
 		$city     = $address['locality'];
355 355
 
356 356
 		WC()->shipping->reset_shipping();
357 357
 
358
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
359
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
360
-		} elseif ( $postcode ) {
361
-			$postcode = wc_format_postcode( $postcode, $country );
358
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
359
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
360
+		} elseif ($postcode) {
361
+			$postcode = wc_format_postcode($postcode, $country);
362 362
 		}
363 363
 
364
-		if ( $country ) {
365
-			WC()->customer->set_location( $country, $state, $postcode, $city );
366
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
364
+		if ($country) {
365
+			WC()->customer->set_location($country, $state, $postcode, $city);
366
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
367 367
 		} else {
368 368
 			WC()->customer->set_to_base();
369 369
 			WC()->customer->set_shipping_to_base();
370 370
 		}
371 371
 
372
-		WC()->customer->calculated_shipping( true );
372
+		WC()->customer->calculated_shipping(true);
373 373
 
374 374
 		/**
375 375
 		 * Set the shipping package.
@@ -391,17 +391,17 @@  discard block
 block discarded – undo
391 391
 		$packages[0]['destination']['postcode']  = $postcode;
392 392
 		$packages[0]['destination']['city']      = $city;
393 393
 
394
-		foreach ( WC()->cart->get_cart() as $item ) {
395
-			if ( $item['data']->needs_shipping() ) {
396
-				if ( isset( $item['line_total'] ) ) {
394
+		foreach (WC()->cart->get_cart() as $item) {
395
+			if ($item['data']->needs_shipping()) {
396
+				if (isset($item['line_total'])) {
397 397
 					$packages[0]['contents_cost'] += $item['line_total'];
398 398
 				}
399 399
 			}
400 400
 		}
401 401
 
402
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
402
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
403 403
 
404
-		WC()->shipping->calculate_shipping( $packages );
404
+		WC()->shipping->calculate_shipping($packages);
405 405
 	}
406 406
 
407 407
 	/**
@@ -411,18 +411,18 @@  discard block
 block discarded – undo
411 411
 	 * @version 3.1.0
412 412
 	 */
413 413
 	public function get_shipping_methods() {
414
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
415
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
414
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
415
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
416 416
 		}
417 417
 
418
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
419
-			define( 'WOOCOMMERCE_CART', true );
418
+		if ( ! defined('WOOCOMMERCE_CART')) {
419
+			define('WOOCOMMERCE_CART', true);
420 420
 		}
421 421
 
422 422
 		try {
423
-			$address = array_map( 'wc_clean', $_POST['address'] );
423
+			$address = array_map('wc_clean', $_POST['address']);
424 424
 
425
-			$this->calculate_shipping( $address );
425
+			$this->calculate_shipping($address);
426 426
 
427 427
 			// Set the shipping options.
428 428
 			$currency = get_woocommerce_currency();
@@ -430,13 +430,13 @@  discard block
 block discarded – undo
430 430
 
431 431
 			$packages = WC()->shipping->get_packages();
432 432
 
433
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
434
-				foreach ( $packages as $package_key => $package ) {
435
-					if ( empty( $package['rates'] ) ) {
436
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
433
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
434
+				foreach ($packages as $package_key => $package) {
435
+					if (empty($package['rates'])) {
436
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
437 437
 					}
438 438
 
439
-					foreach ( $package['rates'] as $key => $rate ) {
439
+					foreach ($package['rates'] as $key => $rate) {
440 440
 						$data[] = array(
441 441
 							'id'       => $rate->id,
442 442
 							'label'    => $rate->label,
@@ -450,16 +450,16 @@  discard block
 block discarded – undo
450 450
 				}
451 451
 
452 452
 				// Auto select the first shipping method.
453
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
453
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
454 454
 
455 455
 				WC()->cart->calculate_totals();
456 456
 
457
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
457
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
458 458
 			} else {
459
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
459
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
460 460
 			}
461
-		} catch ( Exception $e ) {
462
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
461
+		} catch (Exception $e) {
462
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
463 463
 		}
464 464
 	}
465 465
 
@@ -470,29 +470,29 @@  discard block
 block discarded – undo
470 470
 	 * @version 3.1.0
471 471
 	 */
472 472
 	public function update_shipping_method() {
473
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
474
-			define( 'WOOCOMMERCE_CART', true );
473
+		if ( ! defined('WOOCOMMERCE_CART')) {
474
+			define('WOOCOMMERCE_CART', true);
475 475
 		}
476 476
 
477
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
478
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
477
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
478
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
479 479
 		}
480 480
 
481
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
481
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
482 482
 
483
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
483
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
484 484
 
485 485
 		WC()->cart->calculate_totals();
486 486
 
487 487
 		// Send back the new cart total.
488 488
 		$currency  = get_woocommerce_currency();
489
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
489
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
490 490
 		$data      = array(
491 491
 			'total' => WC()->cart->total,
492 492
 		);
493 493
 
494 494
 		// Include fees and taxes as displayItems.
495
-		foreach ( WC()->cart->fees as $key => $fee ) {
495
+		foreach (WC()->cart->fees as $key => $fee) {
496 496
 			$data['items'][] = array(
497 497
 				'label'  => $fee->name,
498 498
 				'amount' => array(
@@ -501,9 +501,9 @@  discard block
 block discarded – undo
501 501
 				),
502 502
 			);
503 503
 		}
504
-		if ( 0 < $tax_total ) {
504
+		if (0 < $tax_total) {
505 505
 			$data['items'][] = array(
506
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
506
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
507 507
 				'amount' => array(
508 508
 					'currency' => $currency,
509 509
 					'value'    => $tax_total,
@@ -511,7 +511,7 @@  discard block
 block discarded – undo
511 511
 			);
512 512
 		}
513 513
 
514
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
514
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
515 515
 	}
516 516
 
517 517
 	/**
@@ -522,37 +522,37 @@  discard block
 block discarded – undo
522 522
 	 * @version 3.1.0
523 523
 	 */
524 524
 	public function process_apple_pay() {
525
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
526
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
525
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
526
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
527 527
 		}
528 528
 
529 529
 		try {
530
-			$result = array_map( 'wc_clean', $_POST['result'] );
530
+			$result = array_map('wc_clean', $_POST['result']);
531 531
 
532
-			$order = $this->create_order( $result );
532
+			$order = $this->create_order($result);
533 533
 
534
-			$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
534
+			$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
535 535
 
536 536
 			// Handle payment.
537
-			if ( $order->get_total() > 0 ) {
537
+			if ($order->get_total() > 0) {
538 538
 
539
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
540
-					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 ) ) );
539
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
540
+					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)));
541 541
 				}
542 542
 
543
-				WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" );
543
+				WC_Stripe::log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}");
544 544
 
545 545
 				// Make the request.
546
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
546
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
547 547
 
548
-				if ( is_wp_error( $response ) ) {
548
+				if (is_wp_error($response)) {
549 549
 					$localized_messages = $this->get_localized_messages();
550 550
 
551
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
551
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
552 552
 				}
553 553
 
554 554
 				// Process valid response.
555
-				$this->process_response( $response, $order );
555
+				$this->process_response($response, $order);
556 556
 			} else {
557 557
 				$order->payment_complete();
558 558
 			}
@@ -560,24 +560,24 @@  discard block
 block discarded – undo
560 560
 			// Remove cart.
561 561
 			WC()->cart->empty_cart();
562 562
 
563
-			update_post_meta( $order_id, '_customer_user', get_current_user_id() );
564
-			update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
563
+			update_post_meta($order_id, '_customer_user', get_current_user_id());
564
+			update_post_meta($order_id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
565 565
 
566 566
 			// Return thank you page redirect.
567
-			wp_send_json( array(
567
+			wp_send_json(array(
568 568
 				'success'  => 'true',
569
-				'redirect' => $this->get_return_url( $order ),
570
-			) );
569
+				'redirect' => $this->get_return_url($order),
570
+			));
571 571
 
572
-		} catch ( Exception $e ) {
573
-			WC()->session->set( 'refresh_totals', true );
574
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
572
+		} catch (Exception $e) {
573
+			WC()->session->set('refresh_totals', true);
574
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
575 575
 
576
-			if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) {
577
-				$this->send_failed_order_email( $order_id );
576
+			if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) {
577
+				$this->send_failed_order_email($order_id);
578 578
 			}
579 579
 
580
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
580
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
581 581
 		}
582 582
 	}
583 583
 
@@ -587,16 +587,16 @@  discard block
 block discarded – undo
587 587
 	 * @param string $source token
588 588
 	 * @return array()
589 589
 	 */
590
-	protected function generate_payment_request( $order, $source ) {
590
+	protected function generate_payment_request($order, $source) {
591 591
 		$post_data                = array();
592
-		$post_data['currency']    = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() );
593
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
594
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
592
+		$post_data['currency']    = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency());
593
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
594
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
595 595
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
596 596
 
597
-		$billing_email      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email();
597
+		$billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email();
598 598
 
599
-		if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
599
+		if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
600 600
 			$post_data['receipt_email'] = $billing_email;
601 601
 		}
602 602
 
@@ -611,7 +611,7 @@  discard block
 block discarded – undo
611 611
 		 * @param WC_Order $order
612 612
 		 * @param object $source
613 613
 		 */
614
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
614
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
615 615
 	}
616 616
 
617 617
 	/**
@@ -620,14 +620,14 @@  discard block
 block discarded – undo
620 620
 	 * @since 3.1.0
621 621
 	 * @version 3.1.0
622 622
 	 */
623
-	public function build_shipping_methods( $shipping_methods ) {
624
-		if ( empty( $shipping_methods ) ) {
623
+	public function build_shipping_methods($shipping_methods) {
624
+		if (empty($shipping_methods)) {
625 625
 			return array();
626 626
 		}
627 627
 
628 628
 		$shipping = array();
629 629
 
630
-		foreach ( $shipping_methods as $method ) {
630
+		foreach ($shipping_methods as $method) {
631 631
 			$shipping[] = array(
632 632
 				'label'      => $method['label'],
633 633
 				'detail'     => '',
@@ -646,65 +646,65 @@  discard block
 block discarded – undo
646 646
 	 * @version 3.1.0
647 647
 	 */
648 648
 	public function build_line_items() {
649
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
650
-			define( 'WOOCOMMERCE_CART', true );
649
+		if ( ! defined('WOOCOMMERCE_CART')) {
650
+			define('WOOCOMMERCE_CART', true);
651 651
 		}
652 652
 
653
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
653
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
654 654
 		
655 655
 		$items    = array();
656 656
 		$subtotal = 0;
657 657
 
658
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
659
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
660
-			$subtotal       += $values['line_subtotal'];
658
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
659
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
660
+			$subtotal += $values['line_subtotal'];
661 661
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
662 662
 
663 663
 			$item = array(
664 664
 				'type'   => 'final',
665 665
 				'label'  => $values['data']->post->post_title . $quantity_label,
666
-				'amount' => wc_format_decimal( $amount, $decimals ),
666
+				'amount' => wc_format_decimal($amount, $decimals),
667 667
 			);
668 668
 
669 669
 			$items[] = $item;
670 670
 		}
671 671
 
672 672
 		// Default show only subtotal instead of itemization.
673
-		if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) {
673
+		if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) {
674 674
 			$items = array();
675 675
 			$items[] = array(
676 676
 				'type'   => 'final',
677
-				'label'  => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
678
-				'amount' => wc_format_decimal( $subtotal, $decimals ),
677
+				'label'  => __('Sub-Total', 'woocommerce-gateway-stripe'),
678
+				'amount' => wc_format_decimal($subtotal, $decimals),
679 679
 			);
680 680
 		}
681 681
 
682
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
683
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
684
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
685
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
686
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
682
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
683
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
684
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
685
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
686
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
687 687
 
688
-		if ( wc_tax_enabled() ) {
688
+		if (wc_tax_enabled()) {
689 689
 			$items[] = array(
690 690
 				'type'   => 'final',
691
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
691
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
692 692
 				'amount' => $tax,
693 693
 			);
694 694
 		}
695 695
 
696
-		if ( WC()->cart->needs_shipping() ) {
696
+		if (WC()->cart->needs_shipping()) {
697 697
 			$items[] = array(
698 698
 				'type'   => 'final',
699
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
699
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
700 700
 				'amount' => $shipping,
701 701
 			);
702 702
 		}
703 703
 
704
-		if ( WC()->cart->has_discount() ) {
704
+		if (WC()->cart->has_discount()) {
705 705
 			$items[] = array(
706 706
 				'type'   => 'final',
707
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
707
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
708 708
 				'amount' => '-' . $discounts,
709 709
 			);
710 710
 		}
@@ -720,24 +720,24 @@  discard block
 block discarded – undo
720 720
 	 * @param array $data
721 721
 	 * @return object $order
722 722
 	 */
723
-	public function create_order( $data = array() ) {
724
-		if ( empty( $data ) ) {
725
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
723
+	public function create_order($data = array()) {
724
+		if (empty($data)) {
725
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
726 726
 		}
727 727
 
728 728
 		$order = wc_create_order();
729
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
729
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
730 730
 
731
-		if ( is_wp_error( $order ) ) {
732
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
733
-		} elseif ( false === $order ) {
734
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
731
+		if (is_wp_error($order)) {
732
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
733
+		} elseif (false === $order) {
734
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
735 735
 		} else {
736
-			do_action( 'woocommerce_new_order', $order_id );
736
+			do_action('woocommerce_new_order', $order_id);
737 737
 		}
738 738
 
739 739
 		// Store the line items to the new/resumed order
740
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
740
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
741 741
 			$item_id = $order->add_product(
742 742
 				$values['data'],
743 743
 				$values['quantity'],
@@ -753,47 +753,47 @@  discard block
 block discarded – undo
753 753
 				)
754 754
 			);
755 755
 
756
-			if ( ! $item_id ) {
757
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
756
+			if ( ! $item_id) {
757
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
758 758
 			}
759 759
 
760 760
 			// Allow plugins to add order item meta
761
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
761
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
762 762
 		}
763 763
 
764 764
 		// Store fees
765
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
766
-			$item_id = $order->add_fee( $fee );
765
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
766
+			$item_id = $order->add_fee($fee);
767 767
 
768
-			if ( ! $item_id ) {
769
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
768
+			if ( ! $item_id) {
769
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
770 770
 			}
771 771
 
772 772
 			// Allow plugins to add order item meta to fees
773
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
773
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
774 774
 		}
775 775
 
776 776
 		// Store tax rows
777
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
778
-			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 ) {
779
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
777
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
778
+			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) {
779
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
780 780
 			}
781 781
 		}
782 782
 
783 783
 		// Store coupons
784
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
785
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
786
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
784
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
785
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
786
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
787 787
 			}
788 788
 		}
789 789
 
790 790
 		// Billing address
791 791
 		$billing_address = array();
792
-		if ( ! empty( $data['token']['card'] ) ) {
792
+		if ( ! empty($data['token']['card'])) {
793 793
 			// Name from Stripe is a full name string.
794
-			$name                          = explode( ' ', $data['token']['card']['name'] );
795
-			$lastname                      = array_pop( $name );
796
-			$firstname                     = implode( ' ', $name );
794
+			$name                          = explode(' ', $data['token']['card']['name']);
795
+			$lastname                      = array_pop($name);
796
+			$firstname                     = implode(' ', $name);
797 797
 			$billing_address['first_name'] = $firstname;
798 798
 			$billing_address['last_name']  = $lastname;
799 799
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -808,7 +808,7 @@  discard block
 block discarded – undo
808 808
 
809 809
 		// Shipping address.
810 810
 		$shipping_address = array();
811
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
811
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
812 812
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
813 813
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
814 814
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -819,7 +819,7 @@  discard block
 block discarded – undo
819 819
 			$shipping_address['city']       = $data['shippingContact']['locality'];
820 820
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
821 821
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
822
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
822
+		} elseif ( ! empty($data['shippingContact'])) {
823 823
 			$shipping_address['first_name'] = $firstname;
824 824
 			$shipping_address['last_name']  = $lastname;
825 825
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -832,34 +832,34 @@  discard block
 block discarded – undo
832 832
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
833 833
 		}
834 834
 
835
-		$order->set_address( $billing_address, 'billing' );
836
-		$order->set_address( $shipping_address, 'shipping' );
835
+		$order->set_address($billing_address, 'billing');
836
+		$order->set_address($shipping_address, 'shipping');
837 837
 
838
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
838
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
839 839
 
840 840
 		// Get the rate object selected by user.
841
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
842
-			foreach ( $package['rates'] as $key => $rate ) {
841
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
842
+			foreach ($package['rates'] as $key => $rate) {
843 843
 				// Loop through user chosen shipping methods.
844
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
845
-					if ( $method === $key ) {
846
-						$order->add_shipping( $rate );
844
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
845
+					if ($method === $key) {
846
+						$order->add_shipping($rate);
847 847
 					}
848 848
 				}
849 849
 			}
850 850
 		}
851 851
 
852 852
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
853
-		$order->set_payment_method( $available_gateways['stripe'] );
854
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
855
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
856
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
857
-		$order->set_total( WC()->cart->tax_total, 'tax' );
858
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
859
-		$order->set_total( WC()->cart->total );
853
+		$order->set_payment_method($available_gateways['stripe']);
854
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
855
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
856
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
857
+		$order->set_total(WC()->cart->tax_total, 'tax');
858
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
859
+		$order->set_total(WC()->cart->total);
860 860
 
861 861
 		// If we got here, the order was created without problems!
862
-		wc_transaction_query( 'commit' );
862
+		wc_transaction_query('commit');
863 863
 
864 864
 		return $order;
865 865
 	}
Please login to merge, or discard this patch.