Completed
Push — master ( d70a50...be4704 )
by Roy
03:01
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +281 added lines, -281 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
 
@@ -89,27 +89,27 @@  discard block
 block discarded – undo
89 89
 	public function __construct() {
90 90
 		self::$_this = $this;
91 91
 
92
-		$gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
92
+		$gateway_settings = get_option('woocommerce_stripe_settings', '');
93 93
 
94
-		$this->statement_descriptor = ! empty( $gateway_settings['statement_descriptor'] ) ? $gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
94
+		$this->statement_descriptor = ! empty($gateway_settings['statement_descriptor']) ? $gateway_settings['statement_descriptor'] : wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
95 95
 
96 96
 		// If both site title and statement descriptor is not set. Fallback.
97
-		if ( empty( $this->statement_descriptor ) ) {
97
+		if (empty($this->statement_descriptor)) {
98 98
 			$this->statement_descriptor = $_SERVER['SERVER_NAME'];
99 99
 		}
100 100
 
101
-		$this->enabled               = ( ! empty( $gateway_settings['enabled'] ) && 'yes' === $gateway_settings['enabled'] ) ? true : false;
102
-		$this->testmode              = ( ! empty( $gateway_settings['testmode'] ) && 'yes' === $gateway_settings['testmode'] ) ? true : false;
103
-		$this->capture               = ( ! empty( $gateway_settings['capture'] ) && 'yes' === $gateway_settings['capture'] ) ? true : false;
104
-		$this->saved_cards           = ( ! empty( $gateway_settings['saved_cards'] ) && 'yes' === $gateway_settings['saved_cards'] ) ? true : false;
105
-		$this->apple_pay             = ( ! empty( $gateway_settings['apple_pay'] ) && 'yes' === $gateway_settings['apple_pay'] ) ? true : false;
106
-		$this->apple_pay_button      = ! empty( $gateway_settings['apple_pay_button'] ) ? $gateway_settings['apple_pay_button'] : 'black';
107
-		$this->apple_pay_button_lang = ! empty( $gateway_settings['apple_pay_button_lang'] ) ? $gateway_settings['apple_pay_button_lang'] : 'en';
108
-		$this->logging               = ( ! empty( $gateway_settings['logging'] ) && 'yes' === $gateway_settings['logging'] ) ? true : false;
109
-		$this->publishable_key       = ! empty( $gateway_settings['publishable_key'] ) ? $gateway_settings['publishable_key'] : '';
110
-
111
-		if ( $this->testmode ) {
112
-			$this->publishable_key = ! empty( $gateway_settings['test_publishable_key'] ) ? $gateway_settings['test_publishable_key'] : '';
101
+		$this->enabled               = ( ! empty($gateway_settings['enabled']) && 'yes' === $gateway_settings['enabled']) ? true : false;
102
+		$this->testmode              = ( ! empty($gateway_settings['testmode']) && 'yes' === $gateway_settings['testmode']) ? true : false;
103
+		$this->capture               = ( ! empty($gateway_settings['capture']) && 'yes' === $gateway_settings['capture']) ? true : false;
104
+		$this->saved_cards           = ( ! empty($gateway_settings['saved_cards']) && 'yes' === $gateway_settings['saved_cards']) ? true : false;
105
+		$this->apple_pay             = ( ! empty($gateway_settings['apple_pay']) && 'yes' === $gateway_settings['apple_pay']) ? true : false;
106
+		$this->apple_pay_button      = ! empty($gateway_settings['apple_pay_button']) ? $gateway_settings['apple_pay_button'] : 'black';
107
+		$this->apple_pay_button_lang = ! empty($gateway_settings['apple_pay_button_lang']) ? $gateway_settings['apple_pay_button_lang'] : 'en';
108
+		$this->logging               = ( ! empty($gateway_settings['logging']) && 'yes' === $gateway_settings['logging']) ? true : false;
109
+		$this->publishable_key       = ! empty($gateway_settings['publishable_key']) ? $gateway_settings['publishable_key'] : '';
110
+
111
+		if ($this->testmode) {
112
+			$this->publishable_key = ! empty($gateway_settings['test_publishable_key']) ? $gateway_settings['test_publishable_key'] : '';
113 113
 		}
114 114
 
115 115
 		$this->init();
@@ -128,53 +128,53 @@  discard block
 block discarded – undo
128 128
 	 */
129 129
 	public function init() {
130 130
 		// If Apple Pay is not enabled no need to proceed further.
131
-		if ( ! $this->apple_pay ) {
131
+		if ( ! $this->apple_pay) {
132 132
 			return;
133 133
 		}
134 134
 
135
-		add_action( 'wp_enqueue_scripts', array( $this, 'cart_scripts' ) );
136
-		add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) );
135
+		add_action('wp_enqueue_scripts', array($this, 'cart_scripts'));
136
+		add_action('wp_enqueue_scripts', array($this, 'single_scripts'));
137 137
 
138 138
 		/**
139 139
 		 * In order to display the Apple Pay button in the correct position,
140 140
 		 * a new hook was added to WooCommerce 3.0. In older versions of WooCommerce,
141 141
 		 * CSS is used to position the button.
142 142
 		 */
143
-		if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
144
-			add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 );
143
+		if (version_compare(WC_VERSION, '3.0.0', '<')) {
144
+			add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1);
145 145
 		} else {
146
-			add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_apple_pay_button' ), 1 );
146
+			add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1);
147 147
 		}
148 148
 
149
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 );
150
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 );
151
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 );
152
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
153
-		add_action( 'wc_ajax_wc_stripe_log_apple_pay_errors', array( $this, 'log_apple_pay_errors' ) );
154
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
155
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
156
-		add_action( 'wc_ajax_wc_stripe_apple_pay_clear_cart', array( $this, 'clear_cart' ) );
157
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
158
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
159
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
160
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
161
-		add_filter( 'woocommerce_validate_postcode', array( $this, 'postal_code_validation' ), 10, 3 );
149
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1);
150
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2);
151
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1);
152
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2);
153
+		add_action('wc_ajax_wc_stripe_log_apple_pay_errors', array($this, 'log_apple_pay_errors'));
154
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
155
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
156
+		add_action('wc_ajax_wc_stripe_apple_pay_clear_cart', array($this, 'clear_cart'));
157
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single'));
158
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
159
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
160
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
161
+		add_filter('woocommerce_validate_postcode', array($this, 'postal_code_validation'), 10, 3);
162 162
 	}
163 163
 
164 164
 	/**
165 165
 	 * Filters the gateway title to reflect Apple Pay.
166 166
 	 *
167 167
 	 */
168
-	public function filter_gateway_title( $title, $id ) {
168
+	public function filter_gateway_title($title, $id) {
169 169
 		global $post;
170 170
 
171
-		if ( ! is_object( $post ) ) {
171
+		if ( ! is_object($post)) {
172 172
 			return $title;
173 173
 		}
174 174
 
175
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
175
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
176 176
 
177
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
177
+		if ('stripe' === $id && ! empty($method_title)) {
178 178
 			return $method_title;
179 179
 		}
180 180
 
@@ -188,13 +188,13 @@  discard block
 block discarded – undo
188 188
 	 * @version 3.1.4
189 189
 	 */
190 190
 	public function log_apple_pay_errors() {
191
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) && ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
192
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
191
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce') && ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
192
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
193 193
 		}
194 194
 
195
-		$errors = wc_clean( stripslashes( $_POST['errors'] ) );
195
+		$errors = wc_clean(stripslashes($_POST['errors']));
196 196
 
197
-		$this->log( $errors );
197
+		$this->log($errors);
198 198
 
199 199
 		exit;
200 200
 	}
@@ -205,10 +205,10 @@  discard block
 block discarded – undo
205 205
 	 * @since 3.1.4
206 206
 	 * @version 3.1.4
207 207
 	 */
208
-	public function postal_code_validation( $valid, $postcode, $country ) {
208
+	public function postal_code_validation($valid, $postcode, $country) {
209 209
 		$gateways = WC()->payment_gateways->get_available_payment_gateways();
210 210
 
211
-		if ( ! $this->apple_pay || ! isset( $gateways['stripe'] ) ) {
211
+		if ( ! $this->apple_pay || ! isset($gateways['stripe'])) {
212 212
 			return $valid;
213 213
 		}
214 214
 
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
 		 * when passing it back from the shippingcontactselected object. This causes WC to invalidate
218 218
 		 * the order and not let it go through. The remedy for now is just to remove this validation.
219 219
 		 */
220
-		if ( 'GB' === $country || 'CA' === $country ) {
220
+		if ('GB' === $country || 'CA' === $country) {
221 221
 			return true;
222 222
 		}
223 223
 
@@ -231,42 +231,42 @@  discard block
 block discarded – undo
231 231
 	 * @version 3.1.4
232 232
 	 */
233 233
 	public function single_scripts() {
234
-		if ( ! is_single() ) {
234
+		if ( ! is_single()) {
235 235
 			return;
236 236
 		}
237 237
 
238 238
 		global $post;
239 239
 
240
-		$product = wc_get_product( $post->ID );
240
+		$product = wc_get_product($post->ID);
241 241
 
242
-		if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
242
+		if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
243 243
 			return;
244 244
 		}
245 245
 
246
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
246
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
247 247
 
248
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
248
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
249 249
 
250
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
251
-		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 );
250
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
251
+		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);
252 252
 
253 253
 		$stripe_params = array(
254 254
 			'key'                                           => $this->publishable_key,
255 255
 			'currency_code'                                 => get_woocommerce_currency(),
256
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
256
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
257 257
 			'label'                                         => $this->statement_descriptor,
258
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
259
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
260
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
261
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
262
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
258
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
259
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
260
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
261
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
262
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
263 263
 			'needs_shipping'                                => $product->needs_shipping() ? 'yes' : 'no',
264 264
 			'i18n'                                          => array(
265
-				'sub_total' => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
265
+				'sub_total' => __('Sub-Total', 'woocommerce-gateway-stripe'),
266 266
 			),
267 267
 		);
268 268
 
269
-		wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
269
+		wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params));
270 270
 	}
271 271
 
272 272
 	/**
@@ -276,32 +276,32 @@  discard block
 block discarded – undo
276 276
 	 * @version 3.1.0
277 277
 	 */
278 278
 	public function cart_scripts() {
279
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
279
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
280 280
 			return;
281 281
 		}
282 282
 
283
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
283
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
284 284
 
285
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
285
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
286 286
 
287
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
288
-		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 );
287
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
288
+		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);
289 289
 
290 290
 		$stripe_params = array(
291 291
 			'key'                                           => $this->publishable_key,
292 292
 			'currency_code'                                 => get_woocommerce_currency(),
293
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
293
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
294 294
 			'label'                                         => $this->statement_descriptor,
295
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
296
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
297
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
298
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
299
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
295
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
296
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
297
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
298
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
299
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
300 300
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
301 301
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
302 302
 		);
303 303
 
304
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
304
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
305 305
 	}
306 306
 
307 307
 	/**
@@ -328,23 +328,23 @@  discard block
 block discarded – undo
328 328
 		 * In order for the Apple Pay button to show on product detail page,
329 329
 		 * Apple Pay must be enabled and Stripe gateway must be enabled.
330 330
 		 */
331
-		if ( ! $this->apple_pay || ! isset( $gateways['stripe'] ) ) {
331
+		if ( ! $this->apple_pay || ! isset($gateways['stripe'])) {
332 332
 			return;
333 333
 		}
334 334
 
335
-		if ( is_single() ) {
335
+		if (is_single()) {
336 336
 			global $post;
337 337
 
338
-			$product = wc_get_product( $post->ID );
338
+			$product = wc_get_product($post->ID);
339 339
 
340
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
340
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
341 341
 				return;
342 342
 			}
343 343
 		}
344 344
 
345 345
 		?>
346 346
 		<div class="apple-pay-button-wrapper">
347
-			<button class="apple-pay-button" lang="<?php echo esc_attr( $this->apple_pay_button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $this->apple_pay_button ); ?>;"></button>
347
+			<button class="apple-pay-button" lang="<?php echo esc_attr($this->apple_pay_button_lang); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($this->apple_pay_button); ?>;"></button>
348 348
 		</div>
349 349
 		<?php
350 350
 	}
@@ -362,21 +362,21 @@  discard block
 block discarded – undo
362 362
 		 * In order for the Apple Pay button to show on cart page,
363 363
 		 * Apple Pay must be enabled and Stripe gateway must be enabled.
364 364
 		 */
365
-		if ( ! $this->apple_pay || ! isset( $gateways['stripe'] ) ) {
365
+		if ( ! $this->apple_pay || ! isset($gateways['stripe'])) {
366 366
 			return;
367 367
 		}
368 368
 
369
-		if ( is_single() ) {
369
+		if (is_single()) {
370 370
 			global $post;
371 371
 
372
-			$product = wc_get_product( $post->ID );
372
+			$product = wc_get_product($post->ID);
373 373
 
374
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
374
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
375 375
 				return;
376 376
 			}
377 377
 		}
378 378
 		?>
379
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
379
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
380 380
 		<?php
381 381
 	}
382 382
 
@@ -387,51 +387,51 @@  discard block
 block discarded – undo
387 387
 	 * @version 3.1.0
388 388
 	 */
389 389
 	public function generate_apple_pay_single() {
390
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
391
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
390
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
391
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
392 392
 		}
393 393
 
394
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
395
-			define( 'WOOCOMMERCE_CART', true );
394
+		if ( ! defined('WOOCOMMERCE_CART')) {
395
+			define('WOOCOMMERCE_CART', true);
396 396
 		}
397 397
 
398 398
 		WC()->shipping->reset_shipping();
399 399
 
400 400
 		global $post;
401 401
 
402
-		$product = wc_get_product( $post->ID );
403
-		$qty     = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] );
402
+		$product = wc_get_product($post->ID);
403
+		$qty     = ! isset($_POST['qty']) ? 1 : absint($_POST['qty']);
404 404
 
405 405
 		/**
406 406
 		 * If this page is single product page, we need to simulate
407 407
 		 * adding the product to the cart taken account if it is a
408 408
 		 * simple or variable product.
409 409
 		 */
410
-		if ( is_single() ) {
410
+		if (is_single()) {
411 411
 			// First empty the cart to prevent wrong calculation.
412 412
 			WC()->cart->empty_cart();
413 413
 
414
-			if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) {
415
-				$attributes = array_map( 'wc_clean', $_POST['attributes'] );
414
+			if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) {
415
+				$attributes = array_map('wc_clean', $_POST['attributes']);
416 416
 
417
-				if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
418
-					$variation_id = $product->get_matching_variation( $attributes );
417
+				if (version_compare(WC_VERSION, '3.0.0', '<')) {
418
+					$variation_id = $product->get_matching_variation($attributes);
419 419
 				} else {
420
-					$data_store = WC_Data_Store::load( 'product' );
421
-					$variation_id = $data_store->find_matching_product_variation( $product, $attributes );
420
+					$data_store = WC_Data_Store::load('product');
421
+					$variation_id = $data_store->find_matching_product_variation($product, $attributes);
422 422
 				}
423 423
 
424
-				WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
424
+				WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes);
425 425
 			}
426 426
 
427
-			if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) {
428
-				WC()->cart->add_to_cart( $product->get_id(), $qty );
427
+			if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) {
428
+				WC()->cart->add_to_cart($product->get_id(), $qty);
429 429
 			}
430 430
 		}
431 431
 
432 432
 		WC()->cart->calculate_totals();
433 433
 
434
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
434
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
435 435
 	}
436 436
 
437 437
 	/**
@@ -441,11 +441,11 @@  discard block
 block discarded – undo
441 441
 	 * @version 3.1.0
442 442
 	 */
443 443
 	public function generate_apple_pay_cart() {
444
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
445
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
444
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
445
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
446 446
 		}
447 447
 
448
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
448
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
449 449
 	}
450 450
 
451 451
 	/**
@@ -466,29 +466,29 @@  discard block
 block discarded – undo
466 466
 	 * @version 3.1.0
467 467
 	 * @param array $address
468 468
 	 */
469
-	public function calculate_shipping( $address = array() ) {
470
-		$country  = strtoupper( $address['countryCode'] );
471
-		$state    = strtoupper( $address['administrativeArea'] );
469
+	public function calculate_shipping($address = array()) {
470
+		$country  = strtoupper($address['countryCode']);
471
+		$state    = strtoupper($address['administrativeArea']);
472 472
 		$postcode = $address['postalCode'];
473 473
 		$city     = $address['locality'];
474 474
 
475 475
 		WC()->shipping->reset_shipping();
476 476
 
477
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
478
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
479
-		} elseif ( $postcode ) {
480
-			$postcode = wc_format_postcode( $postcode, $country );
477
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
478
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
479
+		} elseif ($postcode) {
480
+			$postcode = wc_format_postcode($postcode, $country);
481 481
 		}
482 482
 
483
-		if ( $country ) {
484
-			WC()->customer->set_location( $country, $state, $postcode, $city );
485
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
483
+		if ($country) {
484
+			WC()->customer->set_location($country, $state, $postcode, $city);
485
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
486 486
 		} else {
487 487
 			WC()->customer->set_to_base();
488 488
 			WC()->customer->set_shipping_to_base();
489 489
 		}
490 490
 
491
-		WC()->customer->calculated_shipping( true );
491
+		WC()->customer->calculated_shipping(true);
492 492
 
493 493
 		/**
494 494
 		 * Set the shipping package.
@@ -510,17 +510,17 @@  discard block
 block discarded – undo
510 510
 		$packages[0]['destination']['postcode']  = $postcode;
511 511
 		$packages[0]['destination']['city']      = $city;
512 512
 
513
-		foreach ( WC()->cart->get_cart() as $item ) {
514
-			if ( $item['data']->needs_shipping() ) {
515
-				if ( isset( $item['line_total'] ) ) {
513
+		foreach (WC()->cart->get_cart() as $item) {
514
+			if ($item['data']->needs_shipping()) {
515
+				if (isset($item['line_total'])) {
516 516
 					$packages[0]['contents_cost'] += $item['line_total'];
517 517
 				}
518 518
 			}
519 519
 		}
520 520
 
521
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
521
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
522 522
 
523
-		WC()->shipping->calculate_shipping( $packages );
523
+		WC()->shipping->calculate_shipping($packages);
524 524
 	}
525 525
 
526 526
 	/**
@@ -530,18 +530,18 @@  discard block
 block discarded – undo
530 530
 	 * @version 3.1.0
531 531
 	 */
532 532
 	public function get_shipping_methods() {
533
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
534
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
533
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
534
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
535 535
 		}
536 536
 
537
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
538
-			define( 'WOOCOMMERCE_CART', true );
537
+		if ( ! defined('WOOCOMMERCE_CART')) {
538
+			define('WOOCOMMERCE_CART', true);
539 539
 		}
540 540
 
541 541
 		try {
542
-			$address = array_map( 'wc_clean', $_POST['address'] );
542
+			$address = array_map('wc_clean', $_POST['address']);
543 543
 
544
-			$this->calculate_shipping( $address );
544
+			$this->calculate_shipping($address);
545 545
 
546 546
 			// Set the shipping options.
547 547
 			$currency = get_woocommerce_currency();
@@ -549,13 +549,13 @@  discard block
 block discarded – undo
549 549
 
550 550
 			$packages = WC()->shipping->get_packages();
551 551
 
552
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
553
-				foreach ( $packages as $package_key => $package ) {
554
-					if ( empty( $package['rates'] ) ) {
555
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
552
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
553
+				foreach ($packages as $package_key => $package) {
554
+					if (empty($package['rates'])) {
555
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
556 556
 					}
557 557
 
558
-					foreach ( $package['rates'] as $key => $rate ) {
558
+					foreach ($package['rates'] as $key => $rate) {
559 559
 						$data[] = array(
560 560
 							'id'       => $rate->id,
561 561
 							'label'    => $rate->label,
@@ -569,16 +569,16 @@  discard block
 block discarded – undo
569 569
 				}
570 570
 
571 571
 				// Auto select the first shipping method.
572
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
572
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
573 573
 
574 574
 				WC()->cart->calculate_totals();
575 575
 
576
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
576
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
577 577
 			} else {
578
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
578
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
579 579
 			}
580
-		} catch ( Exception $e ) {
581
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
580
+		} catch (Exception $e) {
581
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
582 582
 		}
583 583
 	}
584 584
 
@@ -589,29 +589,29 @@  discard block
 block discarded – undo
589 589
 	 * @version 3.1.0
590 590
 	 */
591 591
 	public function update_shipping_method() {
592
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
593
-			define( 'WOOCOMMERCE_CART', true );
592
+		if ( ! defined('WOOCOMMERCE_CART')) {
593
+			define('WOOCOMMERCE_CART', true);
594 594
 		}
595 595
 
596
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
597
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
596
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
597
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
598 598
 		}
599 599
 
600
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
600
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
601 601
 
602
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
602
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
603 603
 
604 604
 		WC()->cart->calculate_totals();
605 605
 
606 606
 		// Send back the new cart total.
607 607
 		$currency  = get_woocommerce_currency();
608
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
608
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
609 609
 		$data      = array(
610 610
 			'total' => WC()->cart->total,
611 611
 		);
612 612
 
613 613
 		// Include fees and taxes as displayItems.
614
-		foreach ( WC()->cart->fees as $key => $fee ) {
614
+		foreach (WC()->cart->fees as $key => $fee) {
615 615
 			$data['items'][] = array(
616 616
 				'label'  => $fee->name,
617 617
 				'amount' => array(
@@ -620,9 +620,9 @@  discard block
 block discarded – undo
620 620
 				),
621 621
 			);
622 622
 		}
623
-		if ( 0 < $tax_total ) {
623
+		if (0 < $tax_total) {
624 624
 			$data['items'][] = array(
625
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
625
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
626 626
 				'amount' => array(
627 627
 					'currency' => $currency,
628 628
 					'value'    => $tax_total,
@@ -630,7 +630,7 @@  discard block
 block discarded – undo
630 630
 			);
631 631
 		}
632 632
 
633
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
633
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
634 634
 	}
635 635
 
636 636
 	/**
@@ -641,37 +641,37 @@  discard block
 block discarded – undo
641 641
 	 * @version 3.1.0
642 642
 	 */
643 643
 	public function process_apple_pay() {
644
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
645
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
644
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
645
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
646 646
 		}
647 647
 
648 648
 		try {
649
-			$result = array_map( 'wc_clean', $_POST['result'] );
649
+			$result = array_map('wc_clean', $_POST['result']);
650 650
 
651
-			$order = $this->create_order( $result );
651
+			$order = $this->create_order($result);
652 652
 
653
-			$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
653
+			$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
654 654
 
655 655
 			// Handle payment.
656
-			if ( $order->get_total() > 0 ) {
656
+			if ($order->get_total() > 0) {
657 657
 
658
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
659
-					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 ) ) );
658
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
659
+					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)));
660 660
 				}
661 661
 
662
-				$this->log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" );
662
+				$this->log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}");
663 663
 
664 664
 				// Make the request.
665
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
665
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
666 666
 
667
-				if ( is_wp_error( $response ) ) {
667
+				if (is_wp_error($response)) {
668 668
 					$localized_messages = $this->get_localized_messages();
669 669
 
670
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
670
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
671 671
 				}
672 672
 
673 673
 				// Process valid response.
674
-				$this->process_response( $response, $order );
674
+				$this->process_response($response, $order);
675 675
 			} else {
676 676
 				$order->payment_complete();
677 677
 			}
@@ -679,24 +679,24 @@  discard block
 block discarded – undo
679 679
 			// Remove cart.
680 680
 			WC()->cart->empty_cart();
681 681
 
682
-			update_post_meta( $order_id, '_customer_user', get_current_user_id() );
683
-			update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
682
+			update_post_meta($order_id, '_customer_user', get_current_user_id());
683
+			update_post_meta($order_id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
684 684
 
685 685
 			// Return thank you page redirect.
686
-			wp_send_json( array(
686
+			wp_send_json(array(
687 687
 				'success'  => 'true',
688
-				'redirect' => $this->get_return_url( $order ),
689
-			) );
688
+				'redirect' => $this->get_return_url($order),
689
+			));
690 690
 
691
-		} catch ( Exception $e ) {
692
-			WC()->session->set( 'refresh_totals', true );
693
-			$this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
691
+		} catch (Exception $e) {
692
+			WC()->session->set('refresh_totals', true);
693
+			$this->log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
694 694
 
695
-			if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) {
696
-				$this->send_failed_order_email( $order_id );
695
+			if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) {
696
+				$this->send_failed_order_email($order_id);
697 697
 			}
698 698
 
699
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
699
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
700 700
 		}
701 701
 	}
702 702
 
@@ -706,16 +706,16 @@  discard block
 block discarded – undo
706 706
 	 * @param string $source token
707 707
 	 * @return array()
708 708
 	 */
709
-	protected function generate_payment_request( $order, $source ) {
709
+	protected function generate_payment_request($order, $source) {
710 710
 		$post_data                = array();
711
-		$post_data['currency']    = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() );
712
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
713
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
711
+		$post_data['currency']    = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency());
712
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
713
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
714 714
 		$post_data['capture']     = $this->capture ? 'true' : 'false';
715 715
 
716
-		$billing_email      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email();
716
+		$billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email();
717 717
 
718
-		if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
718
+		if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
719 719
 			$post_data['receipt_email'] = $billing_email;
720 720
 		}
721 721
 
@@ -730,7 +730,7 @@  discard block
 block discarded – undo
730 730
 		 * @param WC_Order $order
731 731
 		 * @param object $source
732 732
 		 */
733
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
733
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
734 734
 	}
735 735
 
736 736
 	/**
@@ -739,14 +739,14 @@  discard block
 block discarded – undo
739 739
 	 * @since 3.1.0
740 740
 	 * @version 3.1.0
741 741
 	 */
742
-	public function build_shipping_methods( $shipping_methods ) {
743
-		if ( empty( $shipping_methods ) ) {
742
+	public function build_shipping_methods($shipping_methods) {
743
+		if (empty($shipping_methods)) {
744 744
 			return array();
745 745
 		}
746 746
 
747 747
 		$shipping = array();
748 748
 
749
-		foreach ( $shipping_methods as $method ) {
749
+		foreach ($shipping_methods as $method) {
750 750
 			$shipping[] = array(
751 751
 				'label'      => $method['label'],
752 752
 				'detail'     => '',
@@ -765,65 +765,65 @@  discard block
 block discarded – undo
765 765
 	 * @version 3.1.0
766 766
 	 */
767 767
 	public function build_line_items() {
768
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
769
-			define( 'WOOCOMMERCE_CART', true );
768
+		if ( ! defined('WOOCOMMERCE_CART')) {
769
+			define('WOOCOMMERCE_CART', true);
770 770
 		}
771 771
 
772
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
772
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
773 773
 		
774 774
 		$items    = array();
775 775
 		$subtotal = 0;
776 776
 
777
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
778
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
779
-			$subtotal       += $values['line_subtotal'];
777
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
778
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
779
+			$subtotal += $values['line_subtotal'];
780 780
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
781 781
 
782 782
 			$item = array(
783 783
 				'type'   => 'final',
784 784
 				'label'  => $values['data']->post->post_title . $quantity_label,
785
-				'amount' => wc_format_decimal( $amount, $decimals ),
785
+				'amount' => wc_format_decimal($amount, $decimals),
786 786
 			);
787 787
 
788 788
 			$items[] = $item;
789 789
 		}
790 790
 
791 791
 		// Default show only subtotal instead of itemization.
792
-		if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) {
792
+		if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) {
793 793
 			$items = array();
794 794
 			$items[] = array(
795 795
 				'type'   => 'final',
796
-				'label'  => esc_html( __( 'Sub-Total', 'woocommerce-gateway-stripe' ) ),
797
-				'amount' => wc_format_decimal( $subtotal, $decimals ),
796
+				'label'  => esc_html(__('Sub-Total', 'woocommerce-gateway-stripe')),
797
+				'amount' => wc_format_decimal($subtotal, $decimals),
798 798
 			);
799 799
 		}
800 800
 
801
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
802
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
803
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
804
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
805
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
801
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
802
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
803
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
804
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
805
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
806 806
 
807
-		if ( wc_tax_enabled() ) {
807
+		if (wc_tax_enabled()) {
808 808
 			$items[] = array(
809 809
 				'type'   => 'final',
810
-				'label'  => esc_html( __( 'Tax', 'woocommerce-gateway-stripe' ) ),
810
+				'label'  => esc_html(__('Tax', 'woocommerce-gateway-stripe')),
811 811
 				'amount' => $tax,
812 812
 			);
813 813
 		}
814 814
 
815
-		if ( WC()->cart->needs_shipping() ) {
815
+		if (WC()->cart->needs_shipping()) {
816 816
 			$items[] = array(
817 817
 				'type'   => 'final',
818
-				'label'  => esc_html( __( 'Shipping', 'woocommerce-gateway-stripe' ) ),
818
+				'label'  => esc_html(__('Shipping', 'woocommerce-gateway-stripe')),
819 819
 				'amount' => $shipping,
820 820
 			);
821 821
 		}
822 822
 
823
-		if ( WC()->cart->has_discount() ) {
823
+		if (WC()->cart->has_discount()) {
824 824
 			$items[] = array(
825 825
 				'type'   => 'final',
826
-				'label'  => esc_html( __( 'Discount', 'woocommerce-gateway-stripe' ) ),
826
+				'label'  => esc_html(__('Discount', 'woocommerce-gateway-stripe')),
827 827
 				'amount' => '-' . $discounts,
828 828
 			);
829 829
 		}
@@ -839,24 +839,24 @@  discard block
 block discarded – undo
839 839
 	 * @param array $data
840 840
 	 * @return object $order
841 841
 	 */
842
-	public function create_order( $data = array() ) {
843
-		if ( empty( $data ) ) {
844
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
842
+	public function create_order($data = array()) {
843
+		if (empty($data)) {
844
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
845 845
 		}
846 846
 
847 847
 		$order = wc_create_order();
848
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
848
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
849 849
 
850
-		if ( is_wp_error( $order ) ) {
851
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
852
-		} elseif ( false === $order ) {
853
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
850
+		if (is_wp_error($order)) {
851
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
852
+		} elseif (false === $order) {
853
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
854 854
 		} else {
855
-			do_action( 'woocommerce_new_order', $order_id );
855
+			do_action('woocommerce_new_order', $order_id);
856 856
 		}
857 857
 
858 858
 		// Store the line items to the new/resumed order
859
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
859
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
860 860
 			$item_id = $order->add_product(
861 861
 				$values['data'],
862 862
 				$values['quantity'],
@@ -872,25 +872,25 @@  discard block
 block discarded – undo
872 872
 				)
873 873
 			);
874 874
 
875
-			if ( ! $item_id ) {
876
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
875
+			if ( ! $item_id) {
876
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
877 877
 			}
878 878
 
879 879
 			// Allow plugins to add order item meta
880
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
881
-				do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
880
+			if (version_compare(WC_VERSION, '3.0', '<')) {
881
+				do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
882 882
 			} else {
883
-				do_action( 'woocommerce_new_order_item', $item_id, wc_get_product( $item_id ), $order->get_id() );
883
+				do_action('woocommerce_new_order_item', $item_id, wc_get_product($item_id), $order->get_id());
884 884
 			}
885 885
 		}
886 886
 
887 887
 		// Store fees
888
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
889
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
890
-				$item_id = $order->add_fee( $fee );
888
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
889
+			if (version_compare(WC_VERSION, '3.0', '<')) {
890
+				$item_id = $order->add_fee($fee);
891 891
 			} else {
892 892
 				$item = new WC_Order_Item_Fee();
893
-				$item->set_props( array(
893
+				$item->set_props(array(
894 894
 					'name'      => $fee->name,
895 895
 					'tax_class' => $fee->taxable ? $fee->tax_class : 0,
896 896
 					'total'     => $fee->amount,
@@ -899,79 +899,79 @@  discard block
 block discarded – undo
899 899
 						'total' => $fee->tax_data,
900 900
 					),
901 901
 					'order_id'  => $order->get_id(),
902
-				) );
902
+				));
903 903
 				$item_id = $item->save();
904
-				$order->add_item( $item );
904
+				$order->add_item($item);
905 905
 			}
906 906
 
907
-			if ( ! $item_id ) {
908
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
907
+			if ( ! $item_id) {
908
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
909 909
 			}
910 910
 
911 911
 			// Allow plugins to add order item meta to fees
912
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
913
-				do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
912
+			if (version_compare(WC_VERSION, '3.0', '<')) {
913
+				do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
914 914
 			} else {
915
-				do_action( 'woocommerce_new_order_item', $item_id, $fee, $order->get_id() );
915
+				do_action('woocommerce_new_order_item', $item_id, $fee, $order->get_id());
916 916
 			}
917 917
 		}
918 918
 
919 919
 		// Store tax rows
920
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
921
-			$tax_amount = WC()->cart->get_tax_amount( $tax_rate_id );
922
-			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount( $tax_rate_id );
920
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
921
+			$tax_amount = WC()->cart->get_tax_amount($tax_rate_id);
922
+			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount($tax_rate_id);
923 923
 
924
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
925
-				$item_id = $order->add_tax( $tax_rate_id, $tax_amount, $shipping_tax_amount );
924
+			if (version_compare(WC_VERSION, '3.0', '<')) {
925
+				$item_id = $order->add_tax($tax_rate_id, $tax_amount, $shipping_tax_amount);
926 926
 			} else {
927 927
 				$item = new WC_Order_Item_Tax();
928
-				$item->set_props( array(
928
+				$item->set_props(array(
929 929
 					'rate_id'            => $tax_rate_id,
930 930
 					'tax_total'          => $tax_amount,
931 931
 					'shipping_tax_total' => $shipping_tax_amount,
932
-				) );
933
-				$item->set_rate( $tax_rate_id );
934
-				$item->set_order_id( $order->get_id() );
932
+				));
933
+				$item->set_rate($tax_rate_id);
934
+				$item->set_order_id($order->get_id());
935 935
 				$item_id = $item->save();
936
-				$order->add_item( $item );
936
+				$order->add_item($item);
937 937
 			}
938 938
 
939
-			if ( $tax_rate_id && ! $item_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
940
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
939
+			if ($tax_rate_id && ! $item_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
940
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
941 941
 			}
942 942
 		}
943 943
 
944 944
 		// Store coupons
945
-		$discount = WC()->cart->get_coupon_discount_amount( $code );
946
-		$discount_tax = WC()->cart->get_coupon_discount_tax_amount( $code );
945
+		$discount = WC()->cart->get_coupon_discount_amount($code);
946
+		$discount_tax = WC()->cart->get_coupon_discount_tax_amount($code);
947 947
 
948
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
949
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
950
-				$coupon_id = $order->add_coupon( $code, $discount, $discount_tax );
948
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
949
+			if (version_compare(WC_VERSION, '3.0', '<')) {
950
+				$coupon_id = $order->add_coupon($code, $discount, $discount_tax);
951 951
 			} else {
952 952
 				$item = new WC_Order_Item_Coupon();
953
-				$item->set_props( array(
953
+				$item->set_props(array(
954 954
 					'code'         => $code,
955 955
 					'discount'     => $discount,
956 956
 					'discount_tax' => $discount_tax,
957 957
 					'order_id'     => $order->get_id(),
958
-				) );
958
+				));
959 959
 				$coupon_id = $item->save();
960
-				$order->add_item( $item );
960
+				$order->add_item($item);
961 961
 			}
962 962
 
963
-			if ( ! $coupon_id ) {
964
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
963
+			if ( ! $coupon_id) {
964
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
965 965
 			}
966 966
 		}
967 967
 
968 968
 		// Billing address
969 969
 		$billing_address = array();
970
-		if ( ! empty( $data['token']['card'] ) ) {
970
+		if ( ! empty($data['token']['card'])) {
971 971
 			// Name from Stripe is a full name string.
972
-			$name                          = explode( ' ', $data['token']['card']['name'] );
973
-			$lastname                      = array_pop( $name );
974
-			$firstname                     = implode( ' ', $name );
972
+			$name                          = explode(' ', $data['token']['card']['name']);
973
+			$lastname                      = array_pop($name);
974
+			$firstname                     = implode(' ', $name);
975 975
 			$billing_address['first_name'] = $firstname;
976 976
 			$billing_address['last_name']  = $lastname;
977 977
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -986,7 +986,7 @@  discard block
 block discarded – undo
986 986
 
987 987
 		// Shipping address.
988 988
 		$shipping_address = array();
989
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
989
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
990 990
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
991 991
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
992 992
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -997,7 +997,7 @@  discard block
 block discarded – undo
997 997
 			$shipping_address['city']       = $data['shippingContact']['locality'];
998 998
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
999 999
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
1000
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
1000
+		} elseif ( ! empty($data['shippingContact'])) {
1001 1001
 			$shipping_address['first_name'] = $firstname;
1002 1002
 			$shipping_address['last_name']  = $lastname;
1003 1003
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -1010,33 +1010,33 @@  discard block
 block discarded – undo
1010 1010
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
1011 1011
 		}
1012 1012
 
1013
-		$order->set_address( $billing_address, 'billing' );
1014
-		$order->set_address( $shipping_address, 'shipping' );
1013
+		$order->set_address($billing_address, 'billing');
1014
+		$order->set_address($shipping_address, 'shipping');
1015 1015
 
1016
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
1016
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
1017 1017
 
1018 1018
 		// Get the rate object selected by user.
1019
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
1020
-			foreach ( $package['rates'] as $key => $rate ) {
1019
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
1020
+			foreach ($package['rates'] as $key => $rate) {
1021 1021
 				// Loop through user chosen shipping methods.
1022
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
1023
-					if ( $method === $key ) {
1024
-						if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
1025
-							$order->add_shipping( $rate );
1022
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
1023
+					if ($method === $key) {
1024
+						if (version_compare(WC_VERSION, '3.0', '<')) {
1025
+							$order->add_shipping($rate);
1026 1026
 						} else {
1027 1027
 							$item = new WC_Order_Item_Shipping();
1028
-							$item->set_props( array(
1028
+							$item->set_props(array(
1029 1029
 								'method_title' => $rate->label,
1030 1030
 								'method_id'    => $rate->id,
1031
-								'total'        => wc_format_decimal( $rate->cost ),
1031
+								'total'        => wc_format_decimal($rate->cost),
1032 1032
 								'taxes'        => $rate->taxes,
1033 1033
 								'order_id'     => $order->get_id(),
1034
-							) );
1035
-							foreach ( $rate->get_meta_data() as $key => $value ) {
1036
-								$item->add_meta_data( $key, $value, true );
1034
+							));
1035
+							foreach ($rate->get_meta_data() as $key => $value) {
1036
+								$item->add_meta_data($key, $value, true);
1037 1037
 							}
1038 1038
 							$item->save();
1039
-							$order->add_item( $item );
1039
+							$order->add_item($item);
1040 1040
 						}
1041 1041
 					}
1042 1042
 				}
@@ -1044,16 +1044,16 @@  discard block
 block discarded – undo
1044 1044
 		}
1045 1045
 
1046 1046
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
1047
-		$order->set_payment_method( $available_gateways['stripe'] );
1048
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
1049
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
1050
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
1051
-		$order->set_total( WC()->cart->tax_total, 'tax' );
1052
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
1053
-		$order->set_total( WC()->cart->total );
1047
+		$order->set_payment_method($available_gateways['stripe']);
1048
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
1049
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
1050
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
1051
+		$order->set_total(WC()->cart->tax_total, 'tax');
1052
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
1053
+		$order->set_total(WC()->cart->total);
1054 1054
 
1055 1055
 		// If we got here, the order was created without problems!
1056
-		wc_transaction_query( 'commit' );
1056
+		wc_transaction_query('commit');
1057 1057
 
1058 1058
 		return $order;
1059 1059
 	}
@@ -1066,9 +1066,9 @@  discard block
 block discarded – undo
1066 1066
 	 *
1067 1067
 	 * @param string $message
1068 1068
 	 */
1069
-	public function log( $message ) {
1070
-		if ( $this->logging ) {
1071
-			WC_Stripe::log( 'Apple Pay: ' . $message );
1069
+	public function log($message) {
1070
+		if ($this->logging) {
1071
+			WC_Stripe::log('Apple Pay: ' . $message);
1072 1072
 		}
1073 1073
 	}
1074 1074
 }
Please login to merge, or discard this patch.