Completed
Push — master ( 6b672e...cf995f )
by Roy
02:22
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +268 added lines, -268 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,12 +47,12 @@  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
 		// If both site title and statement descriptor is not set. Fallback.
55
-		if ( empty( $this->statement_descriptor ) ) {
55
+		if (empty($this->statement_descriptor)) {
56 56
 			$this->statement_descriptor = $_SERVER['SERVER_NAME'];
57 57
 		}
58 58
 
@@ -72,52 +72,52 @@  discard block
 block discarded – undo
72 72
 	 */
73 73
 	public function init() {
74 74
 		// If Apple Pay is not enabled no need to proceed further.
75
-		if ( empty( $this->_gateway_settings['apple_pay'] ) || 'yes' !== $this->_gateway_settings['apple_pay'] ) {
75
+		if (empty($this->_gateway_settings['apple_pay']) || 'yes' !== $this->_gateway_settings['apple_pay']) {
76 76
 			return;
77 77
 		}
78 78
 
79
-		add_action( 'wp_enqueue_scripts', array( $this, 'cart_scripts' ) );
80
-		add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) );
79
+		add_action('wp_enqueue_scripts', array($this, 'cart_scripts'));
80
+		add_action('wp_enqueue_scripts', array($this, 'single_scripts'));
81 81
 
82 82
 		/**
83 83
 		 * In order to display the Apple Pay button in the correct position,
84 84
 		 * a new hook was added to WooCommerce 3.0. In older versions of WooCommerce,
85 85
 		 * CSS is used to position the button.
86 86
 		 */
87
-		if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
88
-			add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 );
87
+		if (version_compare(WC_VERSION, '3.0.0', '<')) {
88
+			add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1);
89 89
 		} else {
90
-			add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_apple_pay_button' ), 1 );
91
-		}
92
-
93
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 );
94
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 );
95
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 );
96
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
97
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
98
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
99
-		add_action( 'wc_ajax_wc_stripe_apple_pay_clear_cart', array( $this, 'clear_cart' ) );
100
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
101
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
102
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
103
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
104
-		add_filter( 'woocommerce_validate_postcode', array( $this, 'postal_code_validation' ), 10, 3 );
90
+			add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1);
91
+		}
92
+
93
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1);
94
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2);
95
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1);
96
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2);
97
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
98
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
99
+		add_action('wc_ajax_wc_stripe_apple_pay_clear_cart', array($this, 'clear_cart'));
100
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single'));
101
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
102
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
103
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
104
+		add_filter('woocommerce_validate_postcode', array($this, 'postal_code_validation'), 10, 3);
105 105
 	}
106 106
 
107 107
 	/**
108 108
 	 * Filters the gateway title to reflect Apple Pay.
109 109
 	 *
110 110
 	 */
111
-	public function filter_gateway_title( $title, $id ) {
111
+	public function filter_gateway_title($title, $id) {
112 112
 		global $post;
113 113
 
114
-		if ( ! is_object( $post ) ) {
114
+		if ( ! is_object($post)) {
115 115
 			return $title;
116 116
 		}
117 117
 
118
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
118
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
119 119
 
120
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
120
+		if ('stripe' === $id && ! empty($method_title)) {
121 121
 			return $method_title;
122 122
 		}
123 123
 
@@ -130,13 +130,13 @@  discard block
 block discarded – undo
130 130
 	 * @since 3.1.4
131 131
 	 * @version 3.1.4
132 132
 	 */
133
-	public function postal_code_validation( $valid, $postcode, $country ) {
133
+	public function postal_code_validation($valid, $postcode, $country) {
134 134
 		$gateways = WC()->payment_gateways->get_available_payment_gateways();
135 135
 
136 136
 		if (
137
-			empty( $this->_gateway_settings['apple_pay'] ) ||
137
+			empty($this->_gateway_settings['apple_pay']) ||
138 138
 			'yes' !== $this->_gateway_settings['apple_pay'] ||
139
-			! isset( $gateways['stripe'] )
139
+			! isset($gateways['stripe'])
140 140
 		) {
141 141
 			return $valid;
142 142
 		}
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 		 * when passing it back from the shippingcontactselected object. This causes WC to invalidate
147 147
 		 * the order and not let it go through. The remedy for now is just to remove this validation.
148 148
 		 */
149
-		if ( 'GB' === $country || 'CA' === $country ) {
149
+		if ('GB' === $country || 'CA' === $country) {
150 150
 			return true;
151 151
 		}
152 152
 
@@ -160,44 +160,44 @@  discard block
 block discarded – undo
160 160
 	 * @version 3.1.4
161 161
 	 */
162 162
 	public function single_scripts() {
163
-		if ( ! is_single() ) {
163
+		if ( ! is_single()) {
164 164
 			return;
165 165
 		}
166 166
 
167 167
 		global $post;
168 168
 
169
-		$product = wc_get_product( $post->ID );
169
+		$product = wc_get_product($post->ID);
170 170
 
171
-		if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
171
+		if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
172 172
 			return;
173 173
 		}
174 174
 
175
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
175
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
176 176
 
177
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
177
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
178 178
 
179
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
180
-		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 );
179
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
180
+		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);
181 181
 
182 182
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
183 183
 
184 184
 		$stripe_params = array(
185 185
 			'key'                                           => $publishable_key,
186 186
 			'currency_code'                                 => get_woocommerce_currency(),
187
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
187
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
188 188
 			'label'                                         => $this->statement_descriptor,
189
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
190
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
191
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
192
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
193
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
189
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
190
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
191
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
192
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
193
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
194 194
 			'needs_shipping'                                => $product->needs_shipping() ? 'yes' : 'no',
195 195
 			'i18n'                                          => array(
196
-				'sub_total' => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
196
+				'sub_total' => __('Sub-Total', 'woocommerce-gateway-stripe'),
197 197
 			),
198 198
 		);
199 199
 
200
-		wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
200
+		wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params));
201 201
 	}
202 202
 
203 203
 	/**
@@ -207,34 +207,34 @@  discard block
 block discarded – undo
207 207
 	 * @version 3.1.0
208 208
 	 */
209 209
 	public function cart_scripts() {
210
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
210
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
211 211
 			return;
212 212
 		}
213 213
 
214
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
214
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
215 215
 
216
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
216
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
217 217
 
218
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
219
-		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 );
218
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
219
+		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);
220 220
 
221 221
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
222 222
 
223 223
 		$stripe_params = array(
224 224
 			'key'                                           => $publishable_key,
225 225
 			'currency_code'                                 => get_woocommerce_currency(),
226
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
226
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
227 227
 			'label'                                         => $this->statement_descriptor,
228
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
229
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
230
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
231
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
232
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
228
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
229
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
230
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
231
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
232
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
233 233
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
234 234
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
235 235
 		);
236 236
 
237
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
237
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
238 238
 	}
239 239
 
240 240
 	/**
@@ -262,28 +262,28 @@  discard block
 block discarded – undo
262 262
 		 * Apple Pay must be enabled and Stripe gateway must be enabled.
263 263
 		 */
264 264
 		if (
265
-			empty( $this->_gateway_settings['apple_pay'] ) ||
265
+			empty($this->_gateway_settings['apple_pay']) ||
266 266
 			'yes' !== $this->_gateway_settings['apple_pay'] ||
267
-			! isset( $gateways['stripe'] )
267
+			! isset($gateways['stripe'])
268 268
 		) {
269 269
 			return;
270 270
 		}
271 271
 
272
-		if ( is_single() ) {
272
+		if (is_single()) {
273 273
 			global $post;
274 274
 
275
-			$product = wc_get_product( $post->ID );
275
+			$product = wc_get_product($post->ID);
276 276
 
277
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
277
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
278 278
 				return;
279 279
 			}
280 280
 		}
281 281
 
282
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
283
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
282
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
283
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
284 284
 		?>
285 285
 		<div class="apple-pay-button-wrapper">
286
-			<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>
286
+			<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>
287 287
 		</div>
288 288
 		<?php
289 289
 	}
@@ -302,24 +302,24 @@  discard block
 block discarded – undo
302 302
 		 * Apple Pay must be enabled and Stripe gateway must be enabled.
303 303
 		 */
304 304
 		if (
305
-			empty( $this->_gateway_settings['apple_pay'] ) ||
305
+			empty($this->_gateway_settings['apple_pay']) ||
306 306
 			'yes' !== $this->_gateway_settings['apple_pay'] ||
307
-			! isset( $gateways['stripe'] )
307
+			! isset($gateways['stripe'])
308 308
 		) {
309 309
 			return;
310 310
 		}
311 311
 
312
-		if ( is_single() ) {
312
+		if (is_single()) {
313 313
 			global $post;
314 314
 
315
-			$product = wc_get_product( $post->ID );
315
+			$product = wc_get_product($post->ID);
316 316
 
317
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
317
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
318 318
 				return;
319 319
 			}
320 320
 		}
321 321
 		?>
322
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
322
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
323 323
 		<?php
324 324
 	}
325 325
 
@@ -330,51 +330,51 @@  discard block
 block discarded – undo
330 330
 	 * @version 3.1.0
331 331
 	 */
332 332
 	public function generate_apple_pay_single() {
333
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
334
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
333
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
334
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
335 335
 		}
336 336
 
337
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
338
-			define( 'WOOCOMMERCE_CART', true );
337
+		if ( ! defined('WOOCOMMERCE_CART')) {
338
+			define('WOOCOMMERCE_CART', true);
339 339
 		}
340 340
 
341 341
 		WC()->shipping->reset_shipping();
342 342
 
343 343
 		global $post;
344 344
 
345
-		$product = wc_get_product( $post->ID );
346
-		$qty     = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] );
345
+		$product = wc_get_product($post->ID);
346
+		$qty     = ! isset($_POST['qty']) ? 1 : absint($_POST['qty']);
347 347
 
348 348
 		/**
349 349
 		 * If this page is single product page, we need to simulate
350 350
 		 * adding the product to the cart taken account if it is a
351 351
 		 * simple or variable product.
352 352
 		 */
353
-		if ( is_single() ) {
353
+		if (is_single()) {
354 354
 			// First empty the cart to prevent wrong calculation.
355 355
 			WC()->cart->empty_cart();
356 356
 
357
-			if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) {
358
-				$attributes = array_map( 'wc_clean', $_POST['attributes'] );
357
+			if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) {
358
+				$attributes = array_map('wc_clean', $_POST['attributes']);
359 359
 
360
-				if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
361
-					$variation_id = $product->get_matching_variation( $attributes );
360
+				if (version_compare(WC_VERSION, '3.0.0', '<')) {
361
+					$variation_id = $product->get_matching_variation($attributes);
362 362
 				} else {
363
-					$data_store = WC_Data_Store::load( 'product' );
364
-					$variation_id = $data_store->find_matching_product_variation( $product, $attributes );
363
+					$data_store = WC_Data_Store::load('product');
364
+					$variation_id = $data_store->find_matching_product_variation($product, $attributes);
365 365
 				}
366 366
 
367
-				WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
367
+				WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes);
368 368
 			}
369 369
 
370
-			if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) {
371
-				WC()->cart->add_to_cart( $product->get_id(), $qty );
370
+			if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) {
371
+				WC()->cart->add_to_cart($product->get_id(), $qty);
372 372
 			}
373 373
 		}
374 374
 
375 375
 		WC()->cart->calculate_totals();
376 376
 
377
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
377
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
378 378
 	}
379 379
 
380 380
 	/**
@@ -384,11 +384,11 @@  discard block
 block discarded – undo
384 384
 	 * @version 3.1.0
385 385
 	 */
386 386
 	public function generate_apple_pay_cart() {
387
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
388
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
387
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
388
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
389 389
 		}
390 390
 
391
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
391
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
392 392
 	}
393 393
 
394 394
 	/**
@@ -409,29 +409,29 @@  discard block
 block discarded – undo
409 409
 	 * @version 3.1.0
410 410
 	 * @param array $address
411 411
 	 */
412
-	public function calculate_shipping( $address = array() ) {
413
-		$country  = strtoupper( $address['countryCode'] );
414
-		$state    = strtoupper( $address['administrativeArea'] );
412
+	public function calculate_shipping($address = array()) {
413
+		$country  = strtoupper($address['countryCode']);
414
+		$state    = strtoupper($address['administrativeArea']);
415 415
 		$postcode = $address['postalCode'];
416 416
 		$city     = $address['locality'];
417 417
 
418 418
 		WC()->shipping->reset_shipping();
419 419
 
420
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
421
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
422
-		} elseif ( $postcode ) {
423
-			$postcode = wc_format_postcode( $postcode, $country );
420
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
421
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
422
+		} elseif ($postcode) {
423
+			$postcode = wc_format_postcode($postcode, $country);
424 424
 		}
425 425
 
426
-		if ( $country ) {
427
-			WC()->customer->set_location( $country, $state, $postcode, $city );
428
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
426
+		if ($country) {
427
+			WC()->customer->set_location($country, $state, $postcode, $city);
428
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
429 429
 		} else {
430 430
 			WC()->customer->set_to_base();
431 431
 			WC()->customer->set_shipping_to_base();
432 432
 		}
433 433
 
434
-		WC()->customer->calculated_shipping( true );
434
+		WC()->customer->calculated_shipping(true);
435 435
 
436 436
 		/**
437 437
 		 * Set the shipping package.
@@ -453,17 +453,17 @@  discard block
 block discarded – undo
453 453
 		$packages[0]['destination']['postcode']  = $postcode;
454 454
 		$packages[0]['destination']['city']      = $city;
455 455
 
456
-		foreach ( WC()->cart->get_cart() as $item ) {
457
-			if ( $item['data']->needs_shipping() ) {
458
-				if ( isset( $item['line_total'] ) ) {
456
+		foreach (WC()->cart->get_cart() as $item) {
457
+			if ($item['data']->needs_shipping()) {
458
+				if (isset($item['line_total'])) {
459 459
 					$packages[0]['contents_cost'] += $item['line_total'];
460 460
 				}
461 461
 			}
462 462
 		}
463 463
 
464
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
464
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
465 465
 
466
-		WC()->shipping->calculate_shipping( $packages );
466
+		WC()->shipping->calculate_shipping($packages);
467 467
 	}
468 468
 
469 469
 	/**
@@ -473,18 +473,18 @@  discard block
 block discarded – undo
473 473
 	 * @version 3.1.0
474 474
 	 */
475 475
 	public function get_shipping_methods() {
476
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
477
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
476
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
477
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
478 478
 		}
479 479
 
480
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
481
-			define( 'WOOCOMMERCE_CART', true );
480
+		if ( ! defined('WOOCOMMERCE_CART')) {
481
+			define('WOOCOMMERCE_CART', true);
482 482
 		}
483 483
 
484 484
 		try {
485
-			$address = array_map( 'wc_clean', $_POST['address'] );
485
+			$address = array_map('wc_clean', $_POST['address']);
486 486
 
487
-			$this->calculate_shipping( $address );
487
+			$this->calculate_shipping($address);
488 488
 
489 489
 			// Set the shipping options.
490 490
 			$currency = get_woocommerce_currency();
@@ -492,13 +492,13 @@  discard block
 block discarded – undo
492 492
 
493 493
 			$packages = WC()->shipping->get_packages();
494 494
 
495
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
496
-				foreach ( $packages as $package_key => $package ) {
497
-					if ( empty( $package['rates'] ) ) {
498
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
495
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
496
+				foreach ($packages as $package_key => $package) {
497
+					if (empty($package['rates'])) {
498
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
499 499
 					}
500 500
 
501
-					foreach ( $package['rates'] as $key => $rate ) {
501
+					foreach ($package['rates'] as $key => $rate) {
502 502
 						$data[] = array(
503 503
 							'id'       => $rate->id,
504 504
 							'label'    => $rate->label,
@@ -512,16 +512,16 @@  discard block
 block discarded – undo
512 512
 				}
513 513
 
514 514
 				// Auto select the first shipping method.
515
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
515
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
516 516
 
517 517
 				WC()->cart->calculate_totals();
518 518
 
519
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
519
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
520 520
 			} else {
521
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
521
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
522 522
 			}
523
-		} catch ( Exception $e ) {
524
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
523
+		} catch (Exception $e) {
524
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
525 525
 		}
526 526
 	}
527 527
 
@@ -532,29 +532,29 @@  discard block
 block discarded – undo
532 532
 	 * @version 3.1.0
533 533
 	 */
534 534
 	public function update_shipping_method() {
535
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
536
-			define( 'WOOCOMMERCE_CART', true );
535
+		if ( ! defined('WOOCOMMERCE_CART')) {
536
+			define('WOOCOMMERCE_CART', true);
537 537
 		}
538 538
 
539
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
540
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
539
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
540
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
541 541
 		}
542 542
 
543
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
543
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
544 544
 
545
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
545
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
546 546
 
547 547
 		WC()->cart->calculate_totals();
548 548
 
549 549
 		// Send back the new cart total.
550 550
 		$currency  = get_woocommerce_currency();
551
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
551
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
552 552
 		$data      = array(
553 553
 			'total' => WC()->cart->total,
554 554
 		);
555 555
 
556 556
 		// Include fees and taxes as displayItems.
557
-		foreach ( WC()->cart->fees as $key => $fee ) {
557
+		foreach (WC()->cart->fees as $key => $fee) {
558 558
 			$data['items'][] = array(
559 559
 				'label'  => $fee->name,
560 560
 				'amount' => array(
@@ -563,9 +563,9 @@  discard block
 block discarded – undo
563 563
 				),
564 564
 			);
565 565
 		}
566
-		if ( 0 < $tax_total ) {
566
+		if (0 < $tax_total) {
567 567
 			$data['items'][] = array(
568
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
568
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
569 569
 				'amount' => array(
570 570
 					'currency' => $currency,
571 571
 					'value'    => $tax_total,
@@ -573,7 +573,7 @@  discard block
 block discarded – undo
573 573
 			);
574 574
 		}
575 575
 
576
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
576
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
577 577
 	}
578 578
 
579 579
 	/**
@@ -584,37 +584,37 @@  discard block
 block discarded – undo
584 584
 	 * @version 3.1.0
585 585
 	 */
586 586
 	public function process_apple_pay() {
587
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
588
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
587
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
588
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
589 589
 		}
590 590
 
591 591
 		try {
592
-			$result = array_map( 'wc_clean', $_POST['result'] );
592
+			$result = array_map('wc_clean', $_POST['result']);
593 593
 
594
-			$order = $this->create_order( $result );
594
+			$order = $this->create_order($result);
595 595
 
596
-			$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
596
+			$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
597 597
 
598 598
 			// Handle payment.
599
-			if ( $order->get_total() > 0 ) {
599
+			if ($order->get_total() > 0) {
600 600
 
601
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
602
-					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 ) ) );
601
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
602
+					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)));
603 603
 				}
604 604
 
605
-				WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" );
605
+				WC_Stripe::log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}");
606 606
 
607 607
 				// Make the request.
608
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
608
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
609 609
 
610
-				if ( is_wp_error( $response ) ) {
610
+				if (is_wp_error($response)) {
611 611
 					$localized_messages = $this->get_localized_messages();
612 612
 
613
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
613
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
614 614
 				}
615 615
 
616 616
 				// Process valid response.
617
-				$this->process_response( $response, $order );
617
+				$this->process_response($response, $order);
618 618
 			} else {
619 619
 				$order->payment_complete();
620 620
 			}
@@ -622,24 +622,24 @@  discard block
 block discarded – undo
622 622
 			// Remove cart.
623 623
 			WC()->cart->empty_cart();
624 624
 
625
-			update_post_meta( $order_id, '_customer_user', get_current_user_id() );
626
-			update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
625
+			update_post_meta($order_id, '_customer_user', get_current_user_id());
626
+			update_post_meta($order_id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
627 627
 
628 628
 			// Return thank you page redirect.
629
-			wp_send_json( array(
629
+			wp_send_json(array(
630 630
 				'success'  => 'true',
631
-				'redirect' => $this->get_return_url( $order ),
632
-			) );
631
+				'redirect' => $this->get_return_url($order),
632
+			));
633 633
 
634
-		} catch ( Exception $e ) {
635
-			WC()->session->set( 'refresh_totals', true );
636
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
634
+		} catch (Exception $e) {
635
+			WC()->session->set('refresh_totals', true);
636
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
637 637
 
638
-			if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) {
639
-				$this->send_failed_order_email( $order_id );
638
+			if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) {
639
+				$this->send_failed_order_email($order_id);
640 640
 			}
641 641
 
642
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
642
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
643 643
 		}
644 644
 	}
645 645
 
@@ -649,16 +649,16 @@  discard block
 block discarded – undo
649 649
 	 * @param string $source token
650 650
 	 * @return array()
651 651
 	 */
652
-	protected function generate_payment_request( $order, $source ) {
652
+	protected function generate_payment_request($order, $source) {
653 653
 		$post_data                = array();
654
-		$post_data['currency']    = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() );
655
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
656
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
654
+		$post_data['currency']    = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency());
655
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
656
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
657 657
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
658 658
 
659
-		$billing_email      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email();
659
+		$billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email();
660 660
 
661
-		if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
661
+		if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
662 662
 			$post_data['receipt_email'] = $billing_email;
663 663
 		}
664 664
 
@@ -673,7 +673,7 @@  discard block
 block discarded – undo
673 673
 		 * @param WC_Order $order
674 674
 		 * @param object $source
675 675
 		 */
676
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
676
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
677 677
 	}
678 678
 
679 679
 	/**
@@ -682,14 +682,14 @@  discard block
 block discarded – undo
682 682
 	 * @since 3.1.0
683 683
 	 * @version 3.1.0
684 684
 	 */
685
-	public function build_shipping_methods( $shipping_methods ) {
686
-		if ( empty( $shipping_methods ) ) {
685
+	public function build_shipping_methods($shipping_methods) {
686
+		if (empty($shipping_methods)) {
687 687
 			return array();
688 688
 		}
689 689
 
690 690
 		$shipping = array();
691 691
 
692
-		foreach ( $shipping_methods as $method ) {
692
+		foreach ($shipping_methods as $method) {
693 693
 			$shipping[] = array(
694 694
 				'label'      => $method['label'],
695 695
 				'detail'     => '',
@@ -708,65 +708,65 @@  discard block
 block discarded – undo
708 708
 	 * @version 3.1.0
709 709
 	 */
710 710
 	public function build_line_items() {
711
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
712
-			define( 'WOOCOMMERCE_CART', true );
711
+		if ( ! defined('WOOCOMMERCE_CART')) {
712
+			define('WOOCOMMERCE_CART', true);
713 713
 		}
714 714
 
715
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
715
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
716 716
 		
717 717
 		$items    = array();
718 718
 		$subtotal = 0;
719 719
 
720
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
721
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
722
-			$subtotal       += $values['line_subtotal'];
720
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
721
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
722
+			$subtotal += $values['line_subtotal'];
723 723
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
724 724
 
725 725
 			$item = array(
726 726
 				'type'   => 'final',
727 727
 				'label'  => $values['data']->post->post_title . $quantity_label,
728
-				'amount' => wc_format_decimal( $amount, $decimals ),
728
+				'amount' => wc_format_decimal($amount, $decimals),
729 729
 			);
730 730
 
731 731
 			$items[] = $item;
732 732
 		}
733 733
 
734 734
 		// Default show only subtotal instead of itemization.
735
-		if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) {
735
+		if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) {
736 736
 			$items = array();
737 737
 			$items[] = array(
738 738
 				'type'   => 'final',
739
-				'label'  => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
740
-				'amount' => wc_format_decimal( $subtotal, $decimals ),
739
+				'label'  => __('Sub-Total', 'woocommerce-gateway-stripe'),
740
+				'amount' => wc_format_decimal($subtotal, $decimals),
741 741
 			);
742 742
 		}
743 743
 
744
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
745
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
746
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
747
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
748
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
744
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
745
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
746
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
747
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
748
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
749 749
 
750
-		if ( wc_tax_enabled() ) {
750
+		if (wc_tax_enabled()) {
751 751
 			$items[] = array(
752 752
 				'type'   => 'final',
753
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
753
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
754 754
 				'amount' => $tax,
755 755
 			);
756 756
 		}
757 757
 
758
-		if ( WC()->cart->needs_shipping() ) {
758
+		if (WC()->cart->needs_shipping()) {
759 759
 			$items[] = array(
760 760
 				'type'   => 'final',
761
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
761
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
762 762
 				'amount' => $shipping,
763 763
 			);
764 764
 		}
765 765
 
766
-		if ( WC()->cart->has_discount() ) {
766
+		if (WC()->cart->has_discount()) {
767 767
 			$items[] = array(
768 768
 				'type'   => 'final',
769
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
769
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
770 770
 				'amount' => '-' . $discounts,
771 771
 			);
772 772
 		}
@@ -782,24 +782,24 @@  discard block
 block discarded – undo
782 782
 	 * @param array $data
783 783
 	 * @return object $order
784 784
 	 */
785
-	public function create_order( $data = array() ) {
786
-		if ( empty( $data ) ) {
787
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
785
+	public function create_order($data = array()) {
786
+		if (empty($data)) {
787
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
788 788
 		}
789 789
 
790 790
 		$order = wc_create_order();
791
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
791
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
792 792
 
793
-		if ( is_wp_error( $order ) ) {
794
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
795
-		} elseif ( false === $order ) {
796
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
793
+		if (is_wp_error($order)) {
794
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
795
+		} elseif (false === $order) {
796
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
797 797
 		} else {
798
-			do_action( 'woocommerce_new_order', $order_id );
798
+			do_action('woocommerce_new_order', $order_id);
799 799
 		}
800 800
 
801 801
 		// Store the line items to the new/resumed order
802
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
802
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
803 803
 			$item_id = $order->add_product(
804 804
 				$values['data'],
805 805
 				$values['quantity'],
@@ -815,25 +815,25 @@  discard block
 block discarded – undo
815 815
 				)
816 816
 			);
817 817
 
818
-			if ( ! $item_id ) {
819
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
818
+			if ( ! $item_id) {
819
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
820 820
 			}
821 821
 
822 822
 			// Allow plugins to add order item meta
823
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
824
-				do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
823
+			if (version_compare(WC_VERSION, '3.0', '<')) {
824
+				do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
825 825
 			} else {
826
-				do_action( 'woocommerce_new_order_item', $item_id, wc_get_product( $item_id ), $order->get_id() );
826
+				do_action('woocommerce_new_order_item', $item_id, wc_get_product($item_id), $order->get_id());
827 827
 			}
828 828
 		}
829 829
 
830 830
 		// Store fees
831
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
832
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
833
-				$item_id = $order->add_fee( $fee );
831
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
832
+			if (version_compare(WC_VERSION, '3.0', '<')) {
833
+				$item_id = $order->add_fee($fee);
834 834
 			} else {
835 835
 				$item = new WC_Order_Item_Fee();
836
-				$item->set_props( array(
836
+				$item->set_props(array(
837 837
 					'name'      => $fee->name,
838 838
 					'tax_class' => $fee->taxable ? $fee->tax_class : 0,
839 839
 					'total'     => $fee->amount,
@@ -842,79 +842,79 @@  discard block
 block discarded – undo
842 842
 						'total' => $fee->tax_data,
843 843
 					),
844 844
 					'order_id'  => $order->get_id(),
845
-				) );
845
+				));
846 846
 				$item_id = $item->save();
847
-				$order->add_item( $item );
847
+				$order->add_item($item);
848 848
 			}
849 849
 
850
-			if ( ! $item_id ) {
851
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
850
+			if ( ! $item_id) {
851
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
852 852
 			}
853 853
 
854 854
 			// Allow plugins to add order item meta to fees
855
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
856
-				do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
855
+			if (version_compare(WC_VERSION, '3.0', '<')) {
856
+				do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
857 857
 			} else {
858
-				do_action( 'woocommerce_new_order_item', $item_id, $fee, $order->get_id() );
858
+				do_action('woocommerce_new_order_item', $item_id, $fee, $order->get_id());
859 859
 			}
860 860
 		}
861 861
 
862 862
 		// Store tax rows
863
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
864
-			$tax_amount = WC()->cart->get_tax_amount( $tax_rate_id );
865
-			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount( $tax_rate_id );
863
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
864
+			$tax_amount = WC()->cart->get_tax_amount($tax_rate_id);
865
+			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount($tax_rate_id);
866 866
 
867
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
868
-				$item_id = $order->add_tax( $tax_rate_id, $tax_amount, $shipping_tax_amount );
867
+			if (version_compare(WC_VERSION, '3.0', '<')) {
868
+				$item_id = $order->add_tax($tax_rate_id, $tax_amount, $shipping_tax_amount);
869 869
 			} else {
870 870
 				$item = new WC_Order_Item_Tax();
871
-				$item->set_props( array(
871
+				$item->set_props(array(
872 872
 					'rate_id'            => $tax_rate_id,
873 873
 					'tax_total'          => $tax_amount,
874 874
 					'shipping_tax_total' => $shipping_tax_amount,
875
-				) );
876
-				$item->set_rate( $tax_rate_id );
877
-				$item->set_order_id( $order->get_id() );
875
+				));
876
+				$item->set_rate($tax_rate_id);
877
+				$item->set_order_id($order->get_id());
878 878
 				$item_id = $item->save();
879
-				$order->add_item( $item );
879
+				$order->add_item($item);
880 880
 			}
881 881
 
882
-			if ( $tax_rate_id && ! $item_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
883
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
882
+			if ($tax_rate_id && ! $item_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
883
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
884 884
 			}
885 885
 		}
886 886
 
887 887
 		// Store coupons
888
-		$discount = WC()->cart->get_coupon_discount_amount( $code );
889
-		$discount_tax = WC()->cart->get_coupon_discount_tax_amount( $code );
888
+		$discount = WC()->cart->get_coupon_discount_amount($code);
889
+		$discount_tax = WC()->cart->get_coupon_discount_tax_amount($code);
890 890
 
891
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
892
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
893
-				$coupon_id = $order->add_coupon( $code, $discount, $discount_tax );
891
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
892
+			if (version_compare(WC_VERSION, '3.0', '<')) {
893
+				$coupon_id = $order->add_coupon($code, $discount, $discount_tax);
894 894
 			} else {
895 895
 				$item = new WC_Order_Item_Coupon();
896
-				$item->set_props( array(
896
+				$item->set_props(array(
897 897
 					'code'         => $code,
898 898
 					'discount'     => $discount,
899 899
 					'discount_tax' => $discount_tax,
900 900
 					'order_id'     => $order->get_id(),
901
-				) );
901
+				));
902 902
 				$coupon_id = $item->save();
903
-				$order->add_item( $item );
903
+				$order->add_item($item);
904 904
 			}
905 905
 
906
-			if ( ! $coupon_id ) {
907
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
906
+			if ( ! $coupon_id) {
907
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
908 908
 			}
909 909
 		}
910 910
 
911 911
 		// Billing address
912 912
 		$billing_address = array();
913
-		if ( ! empty( $data['token']['card'] ) ) {
913
+		if ( ! empty($data['token']['card'])) {
914 914
 			// Name from Stripe is a full name string.
915
-			$name                          = explode( ' ', $data['token']['card']['name'] );
916
-			$lastname                      = array_pop( $name );
917
-			$firstname                     = implode( ' ', $name );
915
+			$name                          = explode(' ', $data['token']['card']['name']);
916
+			$lastname                      = array_pop($name);
917
+			$firstname                     = implode(' ', $name);
918 918
 			$billing_address['first_name'] = $firstname;
919 919
 			$billing_address['last_name']  = $lastname;
920 920
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -929,7 +929,7 @@  discard block
 block discarded – undo
929 929
 
930 930
 		// Shipping address.
931 931
 		$shipping_address = array();
932
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
932
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
933 933
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
934 934
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
935 935
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -940,7 +940,7 @@  discard block
 block discarded – undo
940 940
 			$shipping_address['city']       = $data['shippingContact']['locality'];
941 941
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
942 942
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
943
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
943
+		} elseif ( ! empty($data['shippingContact'])) {
944 944
 			$shipping_address['first_name'] = $firstname;
945 945
 			$shipping_address['last_name']  = $lastname;
946 946
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -953,33 +953,33 @@  discard block
 block discarded – undo
953 953
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
954 954
 		}
955 955
 
956
-		$order->set_address( $billing_address, 'billing' );
957
-		$order->set_address( $shipping_address, 'shipping' );
956
+		$order->set_address($billing_address, 'billing');
957
+		$order->set_address($shipping_address, 'shipping');
958 958
 
959
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
959
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
960 960
 
961 961
 		// Get the rate object selected by user.
962
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
963
-			foreach ( $package['rates'] as $key => $rate ) {
962
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
963
+			foreach ($package['rates'] as $key => $rate) {
964 964
 				// Loop through user chosen shipping methods.
965
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
966
-					if ( $method === $key ) {
967
-						if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
968
-							$order->add_shipping( $rate );
965
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
966
+					if ($method === $key) {
967
+						if (version_compare(WC_VERSION, '3.0', '<')) {
968
+							$order->add_shipping($rate);
969 969
 						} else {
970 970
 							$item = new WC_Order_Item_Shipping();
971
-							$item->set_props( array(
971
+							$item->set_props(array(
972 972
 								'method_title' => $rate->label,
973 973
 								'method_id'    => $rate->id,
974
-								'total'        => wc_format_decimal( $rate->cost ),
974
+								'total'        => wc_format_decimal($rate->cost),
975 975
 								'taxes'        => $rate->taxes,
976 976
 								'order_id'     => $order->get_id(),
977
-							) );
978
-							foreach ( $rate->get_meta_data() as $key => $value ) {
979
-								$item->add_meta_data( $key, $value, true );
977
+							));
978
+							foreach ($rate->get_meta_data() as $key => $value) {
979
+								$item->add_meta_data($key, $value, true);
980 980
 							}
981 981
 							$item->save();
982
-							$order->add_item( $item );
982
+							$order->add_item($item);
983 983
 						}
984 984
 					}
985 985
 				}
@@ -987,16 +987,16 @@  discard block
 block discarded – undo
987 987
 		}
988 988
 
989 989
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
990
-		$order->set_payment_method( $available_gateways['stripe'] );
991
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
992
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
993
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
994
-		$order->set_total( WC()->cart->tax_total, 'tax' );
995
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
996
-		$order->set_total( WC()->cart->total );
990
+		$order->set_payment_method($available_gateways['stripe']);
991
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
992
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
993
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
994
+		$order->set_total(WC()->cart->tax_total, 'tax');
995
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
996
+		$order->set_total(WC()->cart->total);
997 997
 
998 998
 		// If we got here, the order was created without problems!
999
-		wc_transaction_query( 'commit' );
999
+		wc_transaction_query('commit');
1000 1000
 
1001 1001
 		return $order;
1002 1002
 	}
Please login to merge, or discard this patch.