Completed
Push — master ( cf995f...f5187d )
by Roy
02:21
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +271 added lines, -271 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,53 +72,53 @@  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 );
90
+			add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1);
91 91
 		}
92 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_log_apple_pay_errors', array( $this, 'log_apple_pay_errors' ) );
98
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
99
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
100
-		add_action( 'wc_ajax_wc_stripe_apple_pay_clear_cart', array( $this, 'clear_cart' ) );
101
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
102
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
103
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
104
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
105
-		add_filter( 'woocommerce_validate_postcode', array( $this, 'postal_code_validation' ), 10, 3 );
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_log_apple_pay_errors', array($this, 'log_apple_pay_errors'));
98
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
99
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
100
+		add_action('wc_ajax_wc_stripe_apple_pay_clear_cart', array($this, 'clear_cart'));
101
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single'));
102
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
103
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
104
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
105
+		add_filter('woocommerce_validate_postcode', array($this, 'postal_code_validation'), 10, 3);
106 106
 	}
107 107
 
108 108
 	/**
109 109
 	 * Filters the gateway title to reflect Apple Pay.
110 110
 	 *
111 111
 	 */
112
-	public function filter_gateway_title( $title, $id ) {
112
+	public function filter_gateway_title($title, $id) {
113 113
 		global $post;
114 114
 
115
-		if ( ! is_object( $post ) ) {
115
+		if ( ! is_object($post)) {
116 116
 			return $title;
117 117
 		}
118 118
 
119
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
119
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
120 120
 
121
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
121
+		if ('stripe' === $id && ! empty($method_title)) {
122 122
 			return $method_title;
123 123
 		}
124 124
 
@@ -132,13 +132,13 @@  discard block
 block discarded – undo
132 132
 	 * @version 3.1.4
133 133
 	 */
134 134
 	public function log_apple_pay_errors() {
135
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) && ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
136
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
135
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce') && ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
136
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
137 137
 		}
138 138
 
139
-		$errors = wc_clean( stripslashes( $_POST['errors'] ) );
139
+		$errors = wc_clean(stripslashes($_POST['errors']));
140 140
 
141
-		WC_Stripe::log( 'Apple Pay: ' . $errors );
141
+		WC_Stripe::log('Apple Pay: ' . $errors);
142 142
 
143 143
 		exit;
144 144
 	}
@@ -149,13 +149,13 @@  discard block
 block discarded – undo
149 149
 	 * @since 3.1.4
150 150
 	 * @version 3.1.4
151 151
 	 */
152
-	public function postal_code_validation( $valid, $postcode, $country ) {
152
+	public function postal_code_validation($valid, $postcode, $country) {
153 153
 		$gateways = WC()->payment_gateways->get_available_payment_gateways();
154 154
 
155 155
 		if (
156
-			empty( $this->_gateway_settings['apple_pay'] ) ||
156
+			empty($this->_gateway_settings['apple_pay']) ||
157 157
 			'yes' !== $this->_gateway_settings['apple_pay'] ||
158
-			! isset( $gateways['stripe'] )
158
+			! isset($gateways['stripe'])
159 159
 		) {
160 160
 			return $valid;
161 161
 		}
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 		 * when passing it back from the shippingcontactselected object. This causes WC to invalidate
166 166
 		 * the order and not let it go through. The remedy for now is just to remove this validation.
167 167
 		 */
168
-		if ( 'GB' === $country || 'CA' === $country ) {
168
+		if ('GB' === $country || 'CA' === $country) {
169 169
 			return true;
170 170
 		}
171 171
 
@@ -179,44 +179,44 @@  discard block
 block discarded – undo
179 179
 	 * @version 3.1.4
180 180
 	 */
181 181
 	public function single_scripts() {
182
-		if ( ! is_single() ) {
182
+		if ( ! is_single()) {
183 183
 			return;
184 184
 		}
185 185
 
186 186
 		global $post;
187 187
 
188
-		$product = wc_get_product( $post->ID );
188
+		$product = wc_get_product($post->ID);
189 189
 
190
-		if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
190
+		if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
191 191
 			return;
192 192
 		}
193 193
 
194
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
194
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
195 195
 
196
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
196
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
197 197
 
198
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
199
-		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 );
198
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
199
+		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);
200 200
 
201 201
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
202 202
 
203 203
 		$stripe_params = array(
204 204
 			'key'                                           => $publishable_key,
205 205
 			'currency_code'                                 => get_woocommerce_currency(),
206
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
206
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
207 207
 			'label'                                         => $this->statement_descriptor,
208
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
209
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
210
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
211
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
212
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
208
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
209
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
210
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
211
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
212
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
213 213
 			'needs_shipping'                                => $product->needs_shipping() ? 'yes' : 'no',
214 214
 			'i18n'                                          => array(
215
-				'sub_total' => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
215
+				'sub_total' => __('Sub-Total', 'woocommerce-gateway-stripe'),
216 216
 			),
217 217
 		);
218 218
 
219
-		wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
219
+		wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params));
220 220
 	}
221 221
 
222 222
 	/**
@@ -226,34 +226,34 @@  discard block
 block discarded – undo
226 226
 	 * @version 3.1.0
227 227
 	 */
228 228
 	public function cart_scripts() {
229
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
229
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
230 230
 			return;
231 231
 		}
232 232
 
233
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
233
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
234 234
 
235
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
235
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
236 236
 
237
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
238
-		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 );
237
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
238
+		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);
239 239
 
240 240
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
241 241
 
242 242
 		$stripe_params = array(
243 243
 			'key'                                           => $publishable_key,
244 244
 			'currency_code'                                 => get_woocommerce_currency(),
245
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
245
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
246 246
 			'label'                                         => $this->statement_descriptor,
247
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
248
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
249
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
250
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
251
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
247
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
248
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
249
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
250
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
251
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
252 252
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
253 253
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
254 254
 		);
255 255
 
256
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
256
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
257 257
 	}
258 258
 
259 259
 	/**
@@ -281,28 +281,28 @@  discard block
 block discarded – undo
281 281
 		 * Apple Pay must be enabled and Stripe gateway must be enabled.
282 282
 		 */
283 283
 		if (
284
-			empty( $this->_gateway_settings['apple_pay'] ) ||
284
+			empty($this->_gateway_settings['apple_pay']) ||
285 285
 			'yes' !== $this->_gateway_settings['apple_pay'] ||
286
-			! isset( $gateways['stripe'] )
286
+			! isset($gateways['stripe'])
287 287
 		) {
288 288
 			return;
289 289
 		}
290 290
 
291
-		if ( is_single() ) {
291
+		if (is_single()) {
292 292
 			global $post;
293 293
 
294
-			$product = wc_get_product( $post->ID );
294
+			$product = wc_get_product($post->ID);
295 295
 
296
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
296
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
297 297
 				return;
298 298
 			}
299 299
 		}
300 300
 
301
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
302
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
301
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
302
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
303 303
 		?>
304 304
 		<div class="apple-pay-button-wrapper">
305
-			<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>
305
+			<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>
306 306
 		</div>
307 307
 		<?php
308 308
 	}
@@ -321,24 +321,24 @@  discard block
 block discarded – undo
321 321
 		 * Apple Pay must be enabled and Stripe gateway must be enabled.
322 322
 		 */
323 323
 		if (
324
-			empty( $this->_gateway_settings['apple_pay'] ) ||
324
+			empty($this->_gateway_settings['apple_pay']) ||
325 325
 			'yes' !== $this->_gateway_settings['apple_pay'] ||
326
-			! isset( $gateways['stripe'] )
326
+			! isset($gateways['stripe'])
327 327
 		) {
328 328
 			return;
329 329
 		}
330 330
 
331
-		if ( is_single() ) {
331
+		if (is_single()) {
332 332
 			global $post;
333 333
 
334
-			$product = wc_get_product( $post->ID );
334
+			$product = wc_get_product($post->ID);
335 335
 
336
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
336
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
337 337
 				return;
338 338
 			}
339 339
 		}
340 340
 		?>
341
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
341
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
342 342
 		<?php
343 343
 	}
344 344
 
@@ -349,51 +349,51 @@  discard block
 block discarded – undo
349 349
 	 * @version 3.1.0
350 350
 	 */
351 351
 	public function generate_apple_pay_single() {
352
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
353
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
352
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
353
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
354 354
 		}
355 355
 
356
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
357
-			define( 'WOOCOMMERCE_CART', true );
356
+		if ( ! defined('WOOCOMMERCE_CART')) {
357
+			define('WOOCOMMERCE_CART', true);
358 358
 		}
359 359
 
360 360
 		WC()->shipping->reset_shipping();
361 361
 
362 362
 		global $post;
363 363
 
364
-		$product = wc_get_product( $post->ID );
365
-		$qty     = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] );
364
+		$product = wc_get_product($post->ID);
365
+		$qty     = ! isset($_POST['qty']) ? 1 : absint($_POST['qty']);
366 366
 
367 367
 		/**
368 368
 		 * If this page is single product page, we need to simulate
369 369
 		 * adding the product to the cart taken account if it is a
370 370
 		 * simple or variable product.
371 371
 		 */
372
-		if ( is_single() ) {
372
+		if (is_single()) {
373 373
 			// First empty the cart to prevent wrong calculation.
374 374
 			WC()->cart->empty_cart();
375 375
 
376
-			if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) {
377
-				$attributes = array_map( 'wc_clean', $_POST['attributes'] );
376
+			if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) {
377
+				$attributes = array_map('wc_clean', $_POST['attributes']);
378 378
 
379
-				if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
380
-					$variation_id = $product->get_matching_variation( $attributes );
379
+				if (version_compare(WC_VERSION, '3.0.0', '<')) {
380
+					$variation_id = $product->get_matching_variation($attributes);
381 381
 				} else {
382
-					$data_store = WC_Data_Store::load( 'product' );
383
-					$variation_id = $data_store->find_matching_product_variation( $product, $attributes );
382
+					$data_store = WC_Data_Store::load('product');
383
+					$variation_id = $data_store->find_matching_product_variation($product, $attributes);
384 384
 				}
385 385
 
386
-				WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
386
+				WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes);
387 387
 			}
388 388
 
389
-			if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) {
390
-				WC()->cart->add_to_cart( $product->get_id(), $qty );
389
+			if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) {
390
+				WC()->cart->add_to_cart($product->get_id(), $qty);
391 391
 			}
392 392
 		}
393 393
 
394 394
 		WC()->cart->calculate_totals();
395 395
 
396
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
396
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
397 397
 	}
398 398
 
399 399
 	/**
@@ -403,11 +403,11 @@  discard block
 block discarded – undo
403 403
 	 * @version 3.1.0
404 404
 	 */
405 405
 	public function generate_apple_pay_cart() {
406
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
407
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
406
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
407
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
408 408
 		}
409 409
 
410
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
410
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
411 411
 	}
412 412
 
413 413
 	/**
@@ -428,29 +428,29 @@  discard block
 block discarded – undo
428 428
 	 * @version 3.1.0
429 429
 	 * @param array $address
430 430
 	 */
431
-	public function calculate_shipping( $address = array() ) {
432
-		$country  = strtoupper( $address['countryCode'] );
433
-		$state    = strtoupper( $address['administrativeArea'] );
431
+	public function calculate_shipping($address = array()) {
432
+		$country  = strtoupper($address['countryCode']);
433
+		$state    = strtoupper($address['administrativeArea']);
434 434
 		$postcode = $address['postalCode'];
435 435
 		$city     = $address['locality'];
436 436
 
437 437
 		WC()->shipping->reset_shipping();
438 438
 
439
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
440
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
441
-		} elseif ( $postcode ) {
442
-			$postcode = wc_format_postcode( $postcode, $country );
439
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
440
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
441
+		} elseif ($postcode) {
442
+			$postcode = wc_format_postcode($postcode, $country);
443 443
 		}
444 444
 
445
-		if ( $country ) {
446
-			WC()->customer->set_location( $country, $state, $postcode, $city );
447
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
445
+		if ($country) {
446
+			WC()->customer->set_location($country, $state, $postcode, $city);
447
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
448 448
 		} else {
449 449
 			WC()->customer->set_to_base();
450 450
 			WC()->customer->set_shipping_to_base();
451 451
 		}
452 452
 
453
-		WC()->customer->calculated_shipping( true );
453
+		WC()->customer->calculated_shipping(true);
454 454
 
455 455
 		/**
456 456
 		 * Set the shipping package.
@@ -472,17 +472,17 @@  discard block
 block discarded – undo
472 472
 		$packages[0]['destination']['postcode']  = $postcode;
473 473
 		$packages[0]['destination']['city']      = $city;
474 474
 
475
-		foreach ( WC()->cart->get_cart() as $item ) {
476
-			if ( $item['data']->needs_shipping() ) {
477
-				if ( isset( $item['line_total'] ) ) {
475
+		foreach (WC()->cart->get_cart() as $item) {
476
+			if ($item['data']->needs_shipping()) {
477
+				if (isset($item['line_total'])) {
478 478
 					$packages[0]['contents_cost'] += $item['line_total'];
479 479
 				}
480 480
 			}
481 481
 		}
482 482
 
483
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
483
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
484 484
 
485
-		WC()->shipping->calculate_shipping( $packages );
485
+		WC()->shipping->calculate_shipping($packages);
486 486
 	}
487 487
 
488 488
 	/**
@@ -492,18 +492,18 @@  discard block
 block discarded – undo
492 492
 	 * @version 3.1.0
493 493
 	 */
494 494
 	public function get_shipping_methods() {
495
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
496
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
495
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
496
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
497 497
 		}
498 498
 
499
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
500
-			define( 'WOOCOMMERCE_CART', true );
499
+		if ( ! defined('WOOCOMMERCE_CART')) {
500
+			define('WOOCOMMERCE_CART', true);
501 501
 		}
502 502
 
503 503
 		try {
504
-			$address = array_map( 'wc_clean', $_POST['address'] );
504
+			$address = array_map('wc_clean', $_POST['address']);
505 505
 
506
-			$this->calculate_shipping( $address );
506
+			$this->calculate_shipping($address);
507 507
 
508 508
 			// Set the shipping options.
509 509
 			$currency = get_woocommerce_currency();
@@ -511,13 +511,13 @@  discard block
 block discarded – undo
511 511
 
512 512
 			$packages = WC()->shipping->get_packages();
513 513
 
514
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
515
-				foreach ( $packages as $package_key => $package ) {
516
-					if ( empty( $package['rates'] ) ) {
517
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
514
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
515
+				foreach ($packages as $package_key => $package) {
516
+					if (empty($package['rates'])) {
517
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
518 518
 					}
519 519
 
520
-					foreach ( $package['rates'] as $key => $rate ) {
520
+					foreach ($package['rates'] as $key => $rate) {
521 521
 						$data[] = array(
522 522
 							'id'       => $rate->id,
523 523
 							'label'    => $rate->label,
@@ -531,16 +531,16 @@  discard block
 block discarded – undo
531 531
 				}
532 532
 
533 533
 				// Auto select the first shipping method.
534
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
534
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
535 535
 
536 536
 				WC()->cart->calculate_totals();
537 537
 
538
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
538
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
539 539
 			} else {
540
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
540
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
541 541
 			}
542
-		} catch ( Exception $e ) {
543
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
542
+		} catch (Exception $e) {
543
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
544 544
 		}
545 545
 	}
546 546
 
@@ -551,29 +551,29 @@  discard block
 block discarded – undo
551 551
 	 * @version 3.1.0
552 552
 	 */
553 553
 	public function update_shipping_method() {
554
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
555
-			define( 'WOOCOMMERCE_CART', true );
554
+		if ( ! defined('WOOCOMMERCE_CART')) {
555
+			define('WOOCOMMERCE_CART', true);
556 556
 		}
557 557
 
558
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
559
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
558
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
559
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
560 560
 		}
561 561
 
562
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
562
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
563 563
 
564
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
564
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
565 565
 
566 566
 		WC()->cart->calculate_totals();
567 567
 
568 568
 		// Send back the new cart total.
569 569
 		$currency  = get_woocommerce_currency();
570
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
570
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
571 571
 		$data      = array(
572 572
 			'total' => WC()->cart->total,
573 573
 		);
574 574
 
575 575
 		// Include fees and taxes as displayItems.
576
-		foreach ( WC()->cart->fees as $key => $fee ) {
576
+		foreach (WC()->cart->fees as $key => $fee) {
577 577
 			$data['items'][] = array(
578 578
 				'label'  => $fee->name,
579 579
 				'amount' => array(
@@ -582,9 +582,9 @@  discard block
 block discarded – undo
582 582
 				),
583 583
 			);
584 584
 		}
585
-		if ( 0 < $tax_total ) {
585
+		if (0 < $tax_total) {
586 586
 			$data['items'][] = array(
587
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
587
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
588 588
 				'amount' => array(
589 589
 					'currency' => $currency,
590 590
 					'value'    => $tax_total,
@@ -592,7 +592,7 @@  discard block
 block discarded – undo
592 592
 			);
593 593
 		}
594 594
 
595
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
595
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
596 596
 	}
597 597
 
598 598
 	/**
@@ -603,37 +603,37 @@  discard block
 block discarded – undo
603 603
 	 * @version 3.1.0
604 604
 	 */
605 605
 	public function process_apple_pay() {
606
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
607
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
606
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
607
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
608 608
 		}
609 609
 
610 610
 		try {
611
-			$result = array_map( 'wc_clean', $_POST['result'] );
611
+			$result = array_map('wc_clean', $_POST['result']);
612 612
 
613
-			$order = $this->create_order( $result );
613
+			$order = $this->create_order($result);
614 614
 
615
-			$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
615
+			$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
616 616
 
617 617
 			// Handle payment.
618
-			if ( $order->get_total() > 0 ) {
618
+			if ($order->get_total() > 0) {
619 619
 
620
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
621
-					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 ) ) );
620
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
621
+					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)));
622 622
 				}
623 623
 
624
-				WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" );
624
+				WC_Stripe::log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}");
625 625
 
626 626
 				// Make the request.
627
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
627
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
628 628
 
629
-				if ( is_wp_error( $response ) ) {
629
+				if (is_wp_error($response)) {
630 630
 					$localized_messages = $this->get_localized_messages();
631 631
 
632
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
632
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
633 633
 				}
634 634
 
635 635
 				// Process valid response.
636
-				$this->process_response( $response, $order );
636
+				$this->process_response($response, $order);
637 637
 			} else {
638 638
 				$order->payment_complete();
639 639
 			}
@@ -641,24 +641,24 @@  discard block
 block discarded – undo
641 641
 			// Remove cart.
642 642
 			WC()->cart->empty_cart();
643 643
 
644
-			update_post_meta( $order_id, '_customer_user', get_current_user_id() );
645
-			update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
644
+			update_post_meta($order_id, '_customer_user', get_current_user_id());
645
+			update_post_meta($order_id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
646 646
 
647 647
 			// Return thank you page redirect.
648
-			wp_send_json( array(
648
+			wp_send_json(array(
649 649
 				'success'  => 'true',
650
-				'redirect' => $this->get_return_url( $order ),
651
-			) );
650
+				'redirect' => $this->get_return_url($order),
651
+			));
652 652
 
653
-		} catch ( Exception $e ) {
654
-			WC()->session->set( 'refresh_totals', true );
655
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
653
+		} catch (Exception $e) {
654
+			WC()->session->set('refresh_totals', true);
655
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
656 656
 
657
-			if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) {
658
-				$this->send_failed_order_email( $order_id );
657
+			if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) {
658
+				$this->send_failed_order_email($order_id);
659 659
 			}
660 660
 
661
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
661
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
662 662
 		}
663 663
 	}
664 664
 
@@ -668,16 +668,16 @@  discard block
 block discarded – undo
668 668
 	 * @param string $source token
669 669
 	 * @return array()
670 670
 	 */
671
-	protected function generate_payment_request( $order, $source ) {
671
+	protected function generate_payment_request($order, $source) {
672 672
 		$post_data                = array();
673
-		$post_data['currency']    = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() );
674
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
675
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
673
+		$post_data['currency']    = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency());
674
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
675
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
676 676
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
677 677
 
678
-		$billing_email      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email();
678
+		$billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email();
679 679
 
680
-		if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
680
+		if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
681 681
 			$post_data['receipt_email'] = $billing_email;
682 682
 		}
683 683
 
@@ -692,7 +692,7 @@  discard block
 block discarded – undo
692 692
 		 * @param WC_Order $order
693 693
 		 * @param object $source
694 694
 		 */
695
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
695
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
696 696
 	}
697 697
 
698 698
 	/**
@@ -701,14 +701,14 @@  discard block
 block discarded – undo
701 701
 	 * @since 3.1.0
702 702
 	 * @version 3.1.0
703 703
 	 */
704
-	public function build_shipping_methods( $shipping_methods ) {
705
-		if ( empty( $shipping_methods ) ) {
704
+	public function build_shipping_methods($shipping_methods) {
705
+		if (empty($shipping_methods)) {
706 706
 			return array();
707 707
 		}
708 708
 
709 709
 		$shipping = array();
710 710
 
711
-		foreach ( $shipping_methods as $method ) {
711
+		foreach ($shipping_methods as $method) {
712 712
 			$shipping[] = array(
713 713
 				'label'      => $method['label'],
714 714
 				'detail'     => '',
@@ -727,65 +727,65 @@  discard block
 block discarded – undo
727 727
 	 * @version 3.1.0
728 728
 	 */
729 729
 	public function build_line_items() {
730
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
731
-			define( 'WOOCOMMERCE_CART', true );
730
+		if ( ! defined('WOOCOMMERCE_CART')) {
731
+			define('WOOCOMMERCE_CART', true);
732 732
 		}
733 733
 
734
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
734
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
735 735
 		
736 736
 		$items    = array();
737 737
 		$subtotal = 0;
738 738
 
739
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
740
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
741
-			$subtotal       += $values['line_subtotal'];
739
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
740
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
741
+			$subtotal += $values['line_subtotal'];
742 742
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
743 743
 
744 744
 			$item = array(
745 745
 				'type'   => 'final',
746 746
 				'label'  => $values['data']->post->post_title . $quantity_label,
747
-				'amount' => wc_format_decimal( $amount, $decimals ),
747
+				'amount' => wc_format_decimal($amount, $decimals),
748 748
 			);
749 749
 
750 750
 			$items[] = $item;
751 751
 		}
752 752
 
753 753
 		// Default show only subtotal instead of itemization.
754
-		if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) {
754
+		if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) {
755 755
 			$items = array();
756 756
 			$items[] = array(
757 757
 				'type'   => 'final',
758
-				'label'  => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
759
-				'amount' => wc_format_decimal( $subtotal, $decimals ),
758
+				'label'  => __('Sub-Total', 'woocommerce-gateway-stripe'),
759
+				'amount' => wc_format_decimal($subtotal, $decimals),
760 760
 			);
761 761
 		}
762 762
 
763
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
764
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
765
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
766
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
767
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
763
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
764
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
765
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
766
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
767
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
768 768
 
769
-		if ( wc_tax_enabled() ) {
769
+		if (wc_tax_enabled()) {
770 770
 			$items[] = array(
771 771
 				'type'   => 'final',
772
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
772
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
773 773
 				'amount' => $tax,
774 774
 			);
775 775
 		}
776 776
 
777
-		if ( WC()->cart->needs_shipping() ) {
777
+		if (WC()->cart->needs_shipping()) {
778 778
 			$items[] = array(
779 779
 				'type'   => 'final',
780
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
780
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
781 781
 				'amount' => $shipping,
782 782
 			);
783 783
 		}
784 784
 
785
-		if ( WC()->cart->has_discount() ) {
785
+		if (WC()->cart->has_discount()) {
786 786
 			$items[] = array(
787 787
 				'type'   => 'final',
788
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
788
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
789 789
 				'amount' => '-' . $discounts,
790 790
 			);
791 791
 		}
@@ -801,24 +801,24 @@  discard block
 block discarded – undo
801 801
 	 * @param array $data
802 802
 	 * @return object $order
803 803
 	 */
804
-	public function create_order( $data = array() ) {
805
-		if ( empty( $data ) ) {
806
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
804
+	public function create_order($data = array()) {
805
+		if (empty($data)) {
806
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
807 807
 		}
808 808
 
809 809
 		$order = wc_create_order();
810
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
810
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
811 811
 
812
-		if ( is_wp_error( $order ) ) {
813
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
814
-		} elseif ( false === $order ) {
815
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
812
+		if (is_wp_error($order)) {
813
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
814
+		} elseif (false === $order) {
815
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
816 816
 		} else {
817
-			do_action( 'woocommerce_new_order', $order_id );
817
+			do_action('woocommerce_new_order', $order_id);
818 818
 		}
819 819
 
820 820
 		// Store the line items to the new/resumed order
821
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
821
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
822 822
 			$item_id = $order->add_product(
823 823
 				$values['data'],
824 824
 				$values['quantity'],
@@ -834,25 +834,25 @@  discard block
 block discarded – undo
834 834
 				)
835 835
 			);
836 836
 
837
-			if ( ! $item_id ) {
838
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
837
+			if ( ! $item_id) {
838
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
839 839
 			}
840 840
 
841 841
 			// Allow plugins to add order item meta
842
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
843
-				do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
842
+			if (version_compare(WC_VERSION, '3.0', '<')) {
843
+				do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
844 844
 			} else {
845
-				do_action( 'woocommerce_new_order_item', $item_id, wc_get_product( $item_id ), $order->get_id() );
845
+				do_action('woocommerce_new_order_item', $item_id, wc_get_product($item_id), $order->get_id());
846 846
 			}
847 847
 		}
848 848
 
849 849
 		// Store fees
850
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
851
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
852
-				$item_id = $order->add_fee( $fee );
850
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
851
+			if (version_compare(WC_VERSION, '3.0', '<')) {
852
+				$item_id = $order->add_fee($fee);
853 853
 			} else {
854 854
 				$item = new WC_Order_Item_Fee();
855
-				$item->set_props( array(
855
+				$item->set_props(array(
856 856
 					'name'      => $fee->name,
857 857
 					'tax_class' => $fee->taxable ? $fee->tax_class : 0,
858 858
 					'total'     => $fee->amount,
@@ -861,79 +861,79 @@  discard block
 block discarded – undo
861 861
 						'total' => $fee->tax_data,
862 862
 					),
863 863
 					'order_id'  => $order->get_id(),
864
-				) );
864
+				));
865 865
 				$item_id = $item->save();
866
-				$order->add_item( $item );
866
+				$order->add_item($item);
867 867
 			}
868 868
 
869
-			if ( ! $item_id ) {
870
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
869
+			if ( ! $item_id) {
870
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
871 871
 			}
872 872
 
873 873
 			// Allow plugins to add order item meta to fees
874
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
875
-				do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
874
+			if (version_compare(WC_VERSION, '3.0', '<')) {
875
+				do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
876 876
 			} else {
877
-				do_action( 'woocommerce_new_order_item', $item_id, $fee, $order->get_id() );
877
+				do_action('woocommerce_new_order_item', $item_id, $fee, $order->get_id());
878 878
 			}
879 879
 		}
880 880
 
881 881
 		// Store tax rows
882
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
883
-			$tax_amount = WC()->cart->get_tax_amount( $tax_rate_id );
884
-			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount( $tax_rate_id );
882
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
883
+			$tax_amount = WC()->cart->get_tax_amount($tax_rate_id);
884
+			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount($tax_rate_id);
885 885
 
886
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
887
-				$item_id = $order->add_tax( $tax_rate_id, $tax_amount, $shipping_tax_amount );
886
+			if (version_compare(WC_VERSION, '3.0', '<')) {
887
+				$item_id = $order->add_tax($tax_rate_id, $tax_amount, $shipping_tax_amount);
888 888
 			} else {
889 889
 				$item = new WC_Order_Item_Tax();
890
-				$item->set_props( array(
890
+				$item->set_props(array(
891 891
 					'rate_id'            => $tax_rate_id,
892 892
 					'tax_total'          => $tax_amount,
893 893
 					'shipping_tax_total' => $shipping_tax_amount,
894
-				) );
895
-				$item->set_rate( $tax_rate_id );
896
-				$item->set_order_id( $order->get_id() );
894
+				));
895
+				$item->set_rate($tax_rate_id);
896
+				$item->set_order_id($order->get_id());
897 897
 				$item_id = $item->save();
898
-				$order->add_item( $item );
898
+				$order->add_item($item);
899 899
 			}
900 900
 
901
-			if ( $tax_rate_id && ! $item_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
902
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
901
+			if ($tax_rate_id && ! $item_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
902
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
903 903
 			}
904 904
 		}
905 905
 
906 906
 		// Store coupons
907
-		$discount = WC()->cart->get_coupon_discount_amount( $code );
908
-		$discount_tax = WC()->cart->get_coupon_discount_tax_amount( $code );
907
+		$discount = WC()->cart->get_coupon_discount_amount($code);
908
+		$discount_tax = WC()->cart->get_coupon_discount_tax_amount($code);
909 909
 
910
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
911
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
912
-				$coupon_id = $order->add_coupon( $code, $discount, $discount_tax );
910
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
911
+			if (version_compare(WC_VERSION, '3.0', '<')) {
912
+				$coupon_id = $order->add_coupon($code, $discount, $discount_tax);
913 913
 			} else {
914 914
 				$item = new WC_Order_Item_Coupon();
915
-				$item->set_props( array(
915
+				$item->set_props(array(
916 916
 					'code'         => $code,
917 917
 					'discount'     => $discount,
918 918
 					'discount_tax' => $discount_tax,
919 919
 					'order_id'     => $order->get_id(),
920
-				) );
920
+				));
921 921
 				$coupon_id = $item->save();
922
-				$order->add_item( $item );
922
+				$order->add_item($item);
923 923
 			}
924 924
 
925
-			if ( ! $coupon_id ) {
926
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
925
+			if ( ! $coupon_id) {
926
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
927 927
 			}
928 928
 		}
929 929
 
930 930
 		// Billing address
931 931
 		$billing_address = array();
932
-		if ( ! empty( $data['token']['card'] ) ) {
932
+		if ( ! empty($data['token']['card'])) {
933 933
 			// Name from Stripe is a full name string.
934
-			$name                          = explode( ' ', $data['token']['card']['name'] );
935
-			$lastname                      = array_pop( $name );
936
-			$firstname                     = implode( ' ', $name );
934
+			$name                          = explode(' ', $data['token']['card']['name']);
935
+			$lastname                      = array_pop($name);
936
+			$firstname                     = implode(' ', $name);
937 937
 			$billing_address['first_name'] = $firstname;
938 938
 			$billing_address['last_name']  = $lastname;
939 939
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -948,7 +948,7 @@  discard block
 block discarded – undo
948 948
 
949 949
 		// Shipping address.
950 950
 		$shipping_address = array();
951
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
951
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
952 952
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
953 953
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
954 954
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -959,7 +959,7 @@  discard block
 block discarded – undo
959 959
 			$shipping_address['city']       = $data['shippingContact']['locality'];
960 960
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
961 961
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
962
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
962
+		} elseif ( ! empty($data['shippingContact'])) {
963 963
 			$shipping_address['first_name'] = $firstname;
964 964
 			$shipping_address['last_name']  = $lastname;
965 965
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -972,33 +972,33 @@  discard block
 block discarded – undo
972 972
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
973 973
 		}
974 974
 
975
-		$order->set_address( $billing_address, 'billing' );
976
-		$order->set_address( $shipping_address, 'shipping' );
975
+		$order->set_address($billing_address, 'billing');
976
+		$order->set_address($shipping_address, 'shipping');
977 977
 
978
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
978
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
979 979
 
980 980
 		// Get the rate object selected by user.
981
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
982
-			foreach ( $package['rates'] as $key => $rate ) {
981
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
982
+			foreach ($package['rates'] as $key => $rate) {
983 983
 				// Loop through user chosen shipping methods.
984
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
985
-					if ( $method === $key ) {
986
-						if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
987
-							$order->add_shipping( $rate );
984
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
985
+					if ($method === $key) {
986
+						if (version_compare(WC_VERSION, '3.0', '<')) {
987
+							$order->add_shipping($rate);
988 988
 						} else {
989 989
 							$item = new WC_Order_Item_Shipping();
990
-							$item->set_props( array(
990
+							$item->set_props(array(
991 991
 								'method_title' => $rate->label,
992 992
 								'method_id'    => $rate->id,
993
-								'total'        => wc_format_decimal( $rate->cost ),
993
+								'total'        => wc_format_decimal($rate->cost),
994 994
 								'taxes'        => $rate->taxes,
995 995
 								'order_id'     => $order->get_id(),
996
-							) );
997
-							foreach ( $rate->get_meta_data() as $key => $value ) {
998
-								$item->add_meta_data( $key, $value, true );
996
+							));
997
+							foreach ($rate->get_meta_data() as $key => $value) {
998
+								$item->add_meta_data($key, $value, true);
999 999
 							}
1000 1000
 							$item->save();
1001
-							$order->add_item( $item );
1001
+							$order->add_item($item);
1002 1002
 						}
1003 1003
 					}
1004 1004
 				}
@@ -1006,16 +1006,16 @@  discard block
 block discarded – undo
1006 1006
 		}
1007 1007
 
1008 1008
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
1009
-		$order->set_payment_method( $available_gateways['stripe'] );
1010
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
1011
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
1012
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
1013
-		$order->set_total( WC()->cart->tax_total, 'tax' );
1014
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
1015
-		$order->set_total( WC()->cart->total );
1009
+		$order->set_payment_method($available_gateways['stripe']);
1010
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
1011
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
1012
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
1013
+		$order->set_total(WC()->cart->tax_total, 'tax');
1014
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
1015
+		$order->set_total(WC()->cart->total);
1016 1016
 
1017 1017
 		// If we got here, the order was created without problems!
1018
-		wc_transaction_query( 'commit' );
1018
+		wc_transaction_query('commit');
1019 1019
 
1020 1020
 		return $order;
1021 1021
 	}
Please login to merge, or discard this patch.