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