Completed
Push — master ( a13d3d...f8f010 )
by Roy
05:23
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +258 added lines, -258 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) {
2
+if ( ! defined('ABSPATH')) {
3 3
 	exit;
4 4
 }
5 5
 
@@ -47,9 +47,9 @@  discard block
 block discarded – undo
47 47
 	public function __construct() {
48 48
 		self::$_this = $this;
49 49
 
50
-		$this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
50
+		$this->_gateway_settings = get_option('woocommerce_stripe_settings', '');
51 51
 
52
-		$this->statement_descriptor = ! empty( $this->_gateway_settings['statement_descriptor'] ) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
52
+		$this->statement_descriptor = ! empty($this->_gateway_settings['statement_descriptor']) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
53 53
 
54 54
 		$this->init();
55 55
 	}
@@ -67,50 +67,50 @@  discard block
 block discarded – undo
67 67
 	 */
68 68
 	public function init() {
69 69
 		// If Apple Pay is not enabled no need to proceed further.
70
-		if ( 'yes' !== $this->_gateway_settings['apple_pay'] ) {
70
+		if ('yes' !== $this->_gateway_settings['apple_pay']) {
71 71
 			return;
72 72
 		}
73 73
 
74
-		add_action( 'wp_enqueue_scripts', array( $this, 'cart_scripts' ) );
75
-		add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) );
74
+		add_action('wp_enqueue_scripts', array($this, 'cart_scripts'));
75
+		add_action('wp_enqueue_scripts', array($this, 'single_scripts'));
76 76
 
77 77
 		/**
78 78
 		 * In order to display the Apple Pay button in the correct position,
79 79
 		 * a new hook was added to WooCommerce 3.0. In older versions of WooCommerce,
80 80
 		 * CSS is used to position the button.
81 81
 		 */
82
-		if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
83
-			add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 );
82
+		if (version_compare(WC_VERSION, '3.0.0', '<')) {
83
+			add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1);
84 84
 		} else {
85
-			add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_apple_pay_button' ), 1 );
86
-		}
87
-
88
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 );
89
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 );
90
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 );
91
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
92
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
93
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
94
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
95
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
96
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
97
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
85
+			add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1);
86
+		}
87
+
88
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1);
89
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2);
90
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1);
91
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2);
92
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
93
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
94
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single'));
95
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
96
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
97
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
98 98
 	}
99 99
 
100 100
 	/**
101 101
 	 * Filters the gateway title to reflect Apple Pay.
102 102
 	 *
103 103
 	 */
104
-	public function filter_gateway_title( $title, $id ) {
104
+	public function filter_gateway_title($title, $id) {
105 105
 		global $post;
106 106
 
107
-		if ( ! is_object( $post ) ) {
107
+		if ( ! is_object($post)) {
108 108
 			return $title;
109 109
 		}
110 110
 
111
-		$method_title = get_post_meta( $post->ID, '_payment_method_title', true );
111
+		$method_title = get_post_meta($post->ID, '_payment_method_title', true);
112 112
 
113
-		if ( 'stripe' === $id && ! empty( $method_title ) ) {
113
+		if ('stripe' === $id && ! empty($method_title)) {
114 114
 			return $method_title;
115 115
 		}
116 116
 
@@ -124,41 +124,41 @@  discard block
 block discarded – undo
124 124
 	 * @version 3.1.0
125 125
 	 */
126 126
 	public function single_scripts() {
127
-		if ( ! is_single() ) {
127
+		if ( ! is_single()) {
128 128
 			return;
129 129
 		}
130 130
 
131 131
 		global $post;
132 132
 
133
-		$product = wc_get_product( $post->ID );
133
+		$product = wc_get_product($post->ID);
134 134
 
135
-		if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
135
+		if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
136 136
 			return;
137 137
 		}
138 138
 
139
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
139
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
140 140
 
141
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
141
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
142 142
 
143
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
144
-		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 );
143
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
144
+		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);
145 145
 
146 146
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
147 147
 
148 148
 		$stripe_params = array(
149 149
 			'key'                                           => $publishable_key,
150 150
 			'currency_code'                                 => get_woocommerce_currency(),
151
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
151
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
152 152
 			'label'                                         => $this->statement_descriptor,
153
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
154
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
155
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
156
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
157
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
153
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
154
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
155
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
156
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
157
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
158 158
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
159 159
 		);
160 160
 
161
-		wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
161
+		wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params));
162 162
 	}
163 163
 
164 164
 	/**
@@ -168,34 +168,34 @@  discard block
 block discarded – undo
168 168
 	 * @version 3.1.0
169 169
 	 */
170 170
 	public function cart_scripts() {
171
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
171
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
172 172
 			return;
173 173
 		}
174 174
 
175
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
175
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
176 176
 
177
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
177
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
178 178
 
179
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
180
-		wp_enqueue_script( 'woocommerce_stripe_apple_pay', plugins_url( 'assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true );
179
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
180
+		wp_enqueue_script('woocommerce_stripe_apple_pay', plugins_url('assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true);
181 181
 
182 182
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
183 183
 
184 184
 		$stripe_params = array(
185 185
 			'key'                                           => $publishable_key,
186 186
 			'currency_code'                                 => get_woocommerce_currency(),
187
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
187
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
188 188
 			'label'                                         => $this->statement_descriptor,
189
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
190
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
191
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
192
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
193
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
189
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
190
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
191
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
192
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
193
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
194 194
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
195 195
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
196 196
 		);
197 197
 
198
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
198
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
199 199
 	}
200 200
 
201 201
 	/**
@@ -224,26 +224,26 @@  discard block
 block discarded – undo
224 224
 		 */
225 225
 		if (
226 226
 			'yes' !== $this->_gateway_settings['apple_pay']
227
-			|| ! isset( $gateways['stripe'] )
227
+			|| ! isset($gateways['stripe'])
228 228
 		) {
229 229
 			return;
230 230
 		}
231 231
 
232
-		if ( is_single() ) {
232
+		if (is_single()) {
233 233
 			global $post;
234 234
 
235
-			$product = wc_get_product( $post->ID );
235
+			$product = wc_get_product($post->ID);
236 236
 
237
-			if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
237
+			if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
238 238
 				return;
239 239
 			}
240 240
 		}
241 241
 
242
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
243
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
242
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
243
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
244 244
 		?>
245 245
 		<div class="apple-pay-button-wrapper">
246
-			<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>
246
+			<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>
247 247
 		</div>
248 248
 		<?php
249 249
 	}
@@ -263,22 +263,22 @@  discard block
 block discarded – undo
263 263
 		 */
264 264
 		if (
265 265
 			'yes' !== $this->_gateway_settings['apple_pay']
266
-			|| ! isset( $gateways['stripe'] )
266
+			|| ! isset($gateways['stripe'])
267 267
 		) {
268 268
 			return;
269 269
 		}
270 270
 
271
-		if ( is_single() ) {
271
+		if (is_single()) {
272 272
 			global $post;
273 273
 
274
-			$product = wc_get_product( $post->ID );
274
+			$product = wc_get_product($post->ID);
275 275
 
276
-			if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
276
+			if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) {
277 277
 				return;
278 278
 			}
279 279
 		}
280 280
 		?>
281
-		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p>
281
+		<p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p>
282 282
 		<?php
283 283
 	}
284 284
 
@@ -289,51 +289,51 @@  discard block
 block discarded – undo
289 289
 	 * @version 3.1.0
290 290
 	 */
291 291
 	public function generate_apple_pay_single() {
292
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
293
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
292
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
293
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
294 294
 		}
295 295
 
296
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
297
-			define( 'WOOCOMMERCE_CART', true );
296
+		if ( ! defined('WOOCOMMERCE_CART')) {
297
+			define('WOOCOMMERCE_CART', true);
298 298
 		}
299 299
 
300 300
 		WC()->shipping->reset_shipping();
301 301
 
302 302
 		global $post;
303 303
 
304
-		$product = wc_get_product( $post->ID );
305
-		$qty     = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] );
304
+		$product = wc_get_product($post->ID);
305
+		$qty     = ! isset($_POST['qty']) ? 1 : absint($_POST['qty']);
306 306
 
307 307
 		/**
308 308
 		 * If this page is single product page, we need to simulate
309 309
 		 * adding the product to the cart taken account if it is a
310 310
 		 * simple or variable product.
311 311
 		 */
312
-		if ( is_single() ) {
312
+		if (is_single()) {
313 313
 			// First empty the cart to prevent wrong calculation.
314 314
 			WC()->cart->empty_cart();
315 315
 
316
-			if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) {
317
-				$attributes = array_map( 'wc_clean', $_POST['attributes'] );
316
+			if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) {
317
+				$attributes = array_map('wc_clean', $_POST['attributes']);
318 318
 
319
-				if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) {
320
-					$variation_id = $product->get_matching_variation( $attributes );
319
+				if (version_compare(WC_VERSION, '3.0.0', '<')) {
320
+					$variation_id = $product->get_matching_variation($attributes);
321 321
 				} else {
322
-					$data_store = WC_Data_Store::load( 'product' );
323
-					$variation_id = $data_store->find_matching_product_variation( $product, $attributes );
322
+					$data_store = WC_Data_Store::load('product');
323
+					$variation_id = $data_store->find_matching_product_variation($product, $attributes);
324 324
 				}
325 325
 
326
-				WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
326
+				WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes);
327 327
 			}
328 328
 
329
-			if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) {
330
-				WC()->cart->add_to_cart( $product->get_id(), $qty );
329
+			if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) {
330
+				WC()->cart->add_to_cart($product->get_id(), $qty);
331 331
 			}
332 332
 		}
333 333
 
334 334
 		WC()->cart->calculate_totals();
335 335
 
336
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
336
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
337 337
 	}
338 338
 
339 339
 	/**
@@ -343,11 +343,11 @@  discard block
 block discarded – undo
343 343
 	 * @version 3.1.0
344 344
 	 */
345 345
 	public function generate_apple_pay_cart() {
346
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
347
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
346
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
347
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
348 348
 		}
349 349
 
350
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
350
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
351 351
 	}
352 352
 
353 353
 	/**
@@ -357,29 +357,29 @@  discard block
 block discarded – undo
357 357
 	 * @version 3.1.0
358 358
 	 * @param array $address
359 359
 	 */
360
-	public function calculate_shipping( $address = array() ) {
361
-		$country  = strtoupper( $address['countryCode'] );
362
-		$state    = strtoupper( $address['administrativeArea'] );
360
+	public function calculate_shipping($address = array()) {
361
+		$country  = strtoupper($address['countryCode']);
362
+		$state    = strtoupper($address['administrativeArea']);
363 363
 		$postcode = $address['postalCode'];
364 364
 		$city     = $address['locality'];
365 365
 
366 366
 		WC()->shipping->reset_shipping();
367 367
 
368
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
369
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
370
-		} elseif ( $postcode ) {
371
-			$postcode = wc_format_postcode( $postcode, $country );
368
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
369
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
370
+		} elseif ($postcode) {
371
+			$postcode = wc_format_postcode($postcode, $country);
372 372
 		}
373 373
 
374
-		if ( $country ) {
375
-			WC()->customer->set_location( $country, $state, $postcode, $city );
376
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
374
+		if ($country) {
375
+			WC()->customer->set_location($country, $state, $postcode, $city);
376
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
377 377
 		} else {
378 378
 			WC()->customer->set_to_base();
379 379
 			WC()->customer->set_shipping_to_base();
380 380
 		}
381 381
 
382
-		WC()->customer->calculated_shipping( true );
382
+		WC()->customer->calculated_shipping(true);
383 383
 
384 384
 		/**
385 385
 		 * Set the shipping package.
@@ -401,17 +401,17 @@  discard block
 block discarded – undo
401 401
 		$packages[0]['destination']['postcode']  = $postcode;
402 402
 		$packages[0]['destination']['city']      = $city;
403 403
 
404
-		foreach ( WC()->cart->get_cart() as $item ) {
405
-			if ( $item['data']->needs_shipping() ) {
406
-				if ( isset( $item['line_total'] ) ) {
404
+		foreach (WC()->cart->get_cart() as $item) {
405
+			if ($item['data']->needs_shipping()) {
406
+				if (isset($item['line_total'])) {
407 407
 					$packages[0]['contents_cost'] += $item['line_total'];
408 408
 				}
409 409
 			}
410 410
 		}
411 411
 
412
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
412
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
413 413
 
414
-		WC()->shipping->calculate_shipping( $packages );
414
+		WC()->shipping->calculate_shipping($packages);
415 415
 	}
416 416
 
417 417
 	/**
@@ -421,18 +421,18 @@  discard block
 block discarded – undo
421 421
 	 * @version 3.1.0
422 422
 	 */
423 423
 	public function get_shipping_methods() {
424
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
425
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
424
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
425
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
426 426
 		}
427 427
 
428
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
429
-			define( 'WOOCOMMERCE_CART', true );
428
+		if ( ! defined('WOOCOMMERCE_CART')) {
429
+			define('WOOCOMMERCE_CART', true);
430 430
 		}
431 431
 
432 432
 		try {
433
-			$address = array_map( 'wc_clean', $_POST['address'] );
433
+			$address = array_map('wc_clean', $_POST['address']);
434 434
 
435
-			$this->calculate_shipping( $address );
435
+			$this->calculate_shipping($address);
436 436
 
437 437
 			// Set the shipping options.
438 438
 			$currency = get_woocommerce_currency();
@@ -440,13 +440,13 @@  discard block
 block discarded – undo
440 440
 
441 441
 			$packages = WC()->shipping->get_packages();
442 442
 
443
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
444
-				foreach ( $packages as $package_key => $package ) {
445
-					if ( empty( $package['rates'] ) ) {
446
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
443
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
444
+				foreach ($packages as $package_key => $package) {
445
+					if (empty($package['rates'])) {
446
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
447 447
 					}
448 448
 
449
-					foreach ( $package['rates'] as $key => $rate ) {
449
+					foreach ($package['rates'] as $key => $rate) {
450 450
 						$data[] = array(
451 451
 							'id'       => $rate->id,
452 452
 							'label'    => $rate->label,
@@ -460,16 +460,16 @@  discard block
 block discarded – undo
460 460
 				}
461 461
 
462 462
 				// Auto select the first shipping method.
463
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
463
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
464 464
 
465 465
 				WC()->cart->calculate_totals();
466 466
 
467
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
467
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
468 468
 			} else {
469
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
469
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
470 470
 			}
471
-		} catch ( Exception $e ) {
472
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
471
+		} catch (Exception $e) {
472
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
473 473
 		}
474 474
 	}
475 475
 
@@ -480,29 +480,29 @@  discard block
 block discarded – undo
480 480
 	 * @version 3.1.0
481 481
 	 */
482 482
 	public function update_shipping_method() {
483
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
484
-			define( 'WOOCOMMERCE_CART', true );
483
+		if ( ! defined('WOOCOMMERCE_CART')) {
484
+			define('WOOCOMMERCE_CART', true);
485 485
 		}
486 486
 
487
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
488
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
487
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
488
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
489 489
 		}
490 490
 
491
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
491
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
492 492
 
493
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
493
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
494 494
 
495 495
 		WC()->cart->calculate_totals();
496 496
 
497 497
 		// Send back the new cart total.
498 498
 		$currency  = get_woocommerce_currency();
499
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
499
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
500 500
 		$data      = array(
501 501
 			'total' => WC()->cart->total,
502 502
 		);
503 503
 
504 504
 		// Include fees and taxes as displayItems.
505
-		foreach ( WC()->cart->fees as $key => $fee ) {
505
+		foreach (WC()->cart->fees as $key => $fee) {
506 506
 			$data['items'][] = array(
507 507
 				'label'  => $fee->name,
508 508
 				'amount' => array(
@@ -511,9 +511,9 @@  discard block
 block discarded – undo
511 511
 				),
512 512
 			);
513 513
 		}
514
-		if ( 0 < $tax_total ) {
514
+		if (0 < $tax_total) {
515 515
 			$data['items'][] = array(
516
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
516
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
517 517
 				'amount' => array(
518 518
 					'currency' => $currency,
519 519
 					'value'    => $tax_total,
@@ -521,7 +521,7 @@  discard block
 block discarded – undo
521 521
 			);
522 522
 		}
523 523
 
524
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
524
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
525 525
 	}
526 526
 
527 527
 	/**
@@ -532,37 +532,37 @@  discard block
 block discarded – undo
532 532
 	 * @version 3.1.0
533 533
 	 */
534 534
 	public function process_apple_pay() {
535
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
536
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
535
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
536
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
537 537
 		}
538 538
 
539 539
 		try {
540
-			$result = array_map( 'wc_clean', $_POST['result'] );
540
+			$result = array_map('wc_clean', $_POST['result']);
541 541
 
542
-			$order = $this->create_order( $result );
542
+			$order = $this->create_order($result);
543 543
 
544
-			$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
544
+			$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
545 545
 
546 546
 			// Handle payment.
547
-			if ( $order->get_total() > 0 ) {
547
+			if ($order->get_total() > 0) {
548 548
 
549
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
550
-					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 ) ) );
549
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
550
+					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)));
551 551
 				}
552 552
 
553
-				WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" );
553
+				WC_Stripe::log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}");
554 554
 
555 555
 				// Make the request.
556
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
556
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
557 557
 
558
-				if ( is_wp_error( $response ) ) {
558
+				if (is_wp_error($response)) {
559 559
 					$localized_messages = $this->get_localized_messages();
560 560
 
561
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
561
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
562 562
 				}
563 563
 
564 564
 				// Process valid response.
565
-				$this->process_response( $response, $order );
565
+				$this->process_response($response, $order);
566 566
 			} else {
567 567
 				$order->payment_complete();
568 568
 			}
@@ -570,24 +570,24 @@  discard block
 block discarded – undo
570 570
 			// Remove cart.
571 571
 			WC()->cart->empty_cart();
572 572
 
573
-			update_post_meta( $order_id, '_customer_user', get_current_user_id() );
574
-			update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
573
+			update_post_meta($order_id, '_customer_user', get_current_user_id());
574
+			update_post_meta($order_id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
575 575
 
576 576
 			// Return thank you page redirect.
577
-			wp_send_json( array(
577
+			wp_send_json(array(
578 578
 				'success'  => 'true',
579
-				'redirect' => $this->get_return_url( $order ),
580
-			) );
579
+				'redirect' => $this->get_return_url($order),
580
+			));
581 581
 
582
-		} catch ( Exception $e ) {
583
-			WC()->session->set( 'refresh_totals', true );
584
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
582
+		} catch (Exception $e) {
583
+			WC()->session->set('refresh_totals', true);
584
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
585 585
 
586
-			if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) {
587
-				$this->send_failed_order_email( $order_id );
586
+			if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) {
587
+				$this->send_failed_order_email($order_id);
588 588
 			}
589 589
 
590
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
590
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
591 591
 		}
592 592
 	}
593 593
 
@@ -597,16 +597,16 @@  discard block
 block discarded – undo
597 597
 	 * @param string $source token
598 598
 	 * @return array()
599 599
 	 */
600
-	protected function generate_payment_request( $order, $source ) {
600
+	protected function generate_payment_request($order, $source) {
601 601
 		$post_data                = array();
602
-		$post_data['currency']    = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() );
603
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
604
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
602
+		$post_data['currency']    = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency());
603
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
604
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
605 605
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
606 606
 
607
-		$billing_email      = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email();
607
+		$billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email();
608 608
 
609
-		if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
609
+		if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
610 610
 			$post_data['receipt_email'] = $billing_email;
611 611
 		}
612 612
 
@@ -621,7 +621,7 @@  discard block
 block discarded – undo
621 621
 		 * @param WC_Order $order
622 622
 		 * @param object $source
623 623
 		 */
624
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
624
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
625 625
 	}
626 626
 
627 627
 	/**
@@ -630,14 +630,14 @@  discard block
 block discarded – undo
630 630
 	 * @since 3.1.0
631 631
 	 * @version 3.1.0
632 632
 	 */
633
-	public function build_shipping_methods( $shipping_methods ) {
634
-		if ( empty( $shipping_methods ) ) {
633
+	public function build_shipping_methods($shipping_methods) {
634
+		if (empty($shipping_methods)) {
635 635
 			return array();
636 636
 		}
637 637
 
638 638
 		$shipping = array();
639 639
 
640
-		foreach ( $shipping_methods as $method ) {
640
+		foreach ($shipping_methods as $method) {
641 641
 			$shipping[] = array(
642 642
 				'label'      => $method['label'],
643 643
 				'detail'     => '',
@@ -656,65 +656,65 @@  discard block
 block discarded – undo
656 656
 	 * @version 3.1.0
657 657
 	 */
658 658
 	public function build_line_items() {
659
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
660
-			define( 'WOOCOMMERCE_CART', true );
659
+		if ( ! defined('WOOCOMMERCE_CART')) {
660
+			define('WOOCOMMERCE_CART', true);
661 661
 		}
662 662
 
663
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
663
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
664 664
 		
665 665
 		$items    = array();
666 666
 		$subtotal = 0;
667 667
 
668
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
669
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
670
-			$subtotal       += $values['line_subtotal'];
668
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
669
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
670
+			$subtotal += $values['line_subtotal'];
671 671
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
672 672
 
673 673
 			$item = array(
674 674
 				'type'   => 'final',
675 675
 				'label'  => $values['data']->post->post_title . $quantity_label,
676
-				'amount' => wc_format_decimal( $amount, $decimals ),
676
+				'amount' => wc_format_decimal($amount, $decimals),
677 677
 			);
678 678
 
679 679
 			$items[] = $item;
680 680
 		}
681 681
 
682 682
 		// Default show only subtotal instead of itemization.
683
-		if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) {
683
+		if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) {
684 684
 			$items = array();
685 685
 			$items[] = array(
686 686
 				'type'   => 'final',
687
-				'label'  => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
688
-				'amount' => wc_format_decimal( $subtotal, $decimals ),
687
+				'label'  => __('Sub-Total', 'woocommerce-gateway-stripe'),
688
+				'amount' => wc_format_decimal($subtotal, $decimals),
689 689
 			);
690 690
 		}
691 691
 
692
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
693
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
694
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
695
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
696
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
692
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
693
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
694
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
695
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
696
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
697 697
 
698
-		if ( wc_tax_enabled() ) {
698
+		if (wc_tax_enabled()) {
699 699
 			$items[] = array(
700 700
 				'type'   => 'final',
701
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
701
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
702 702
 				'amount' => $tax,
703 703
 			);
704 704
 		}
705 705
 
706
-		if ( WC()->cart->needs_shipping() ) {
706
+		if (WC()->cart->needs_shipping()) {
707 707
 			$items[] = array(
708 708
 				'type'   => 'final',
709
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
709
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
710 710
 				'amount' => $shipping,
711 711
 			);
712 712
 		}
713 713
 
714
-		if ( WC()->cart->has_discount() ) {
714
+		if (WC()->cart->has_discount()) {
715 715
 			$items[] = array(
716 716
 				'type'   => 'final',
717
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
717
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
718 718
 				'amount' => '-' . $discounts,
719 719
 			);
720 720
 		}
@@ -730,24 +730,24 @@  discard block
 block discarded – undo
730 730
 	 * @param array $data
731 731
 	 * @return object $order
732 732
 	 */
733
-	public function create_order( $data = array() ) {
734
-		if ( empty( $data ) ) {
735
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
733
+	public function create_order($data = array()) {
734
+		if (empty($data)) {
735
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
736 736
 		}
737 737
 
738 738
 		$order = wc_create_order();
739
-		$order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id();
739
+		$order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id();
740 740
 
741
-		if ( is_wp_error( $order ) ) {
742
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
743
-		} elseif ( false === $order ) {
744
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
741
+		if (is_wp_error($order)) {
742
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
743
+		} elseif (false === $order) {
744
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
745 745
 		} else {
746
-			do_action( 'woocommerce_new_order', $order_id );
746
+			do_action('woocommerce_new_order', $order_id);
747 747
 		}
748 748
 
749 749
 		// Store the line items to the new/resumed order
750
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
750
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
751 751
 			$item_id = $order->add_product(
752 752
 				$values['data'],
753 753
 				$values['quantity'],
@@ -763,25 +763,25 @@  discard block
 block discarded – undo
763 763
 				)
764 764
 			);
765 765
 
766
-			if ( ! $item_id ) {
767
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
766
+			if ( ! $item_id) {
767
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
768 768
 			}
769 769
 
770 770
 			// Allow plugins to add order item meta
771
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
772
-				do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
771
+			if (version_compare(WC_VERSION, '3.0', '<')) {
772
+				do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
773 773
 			} else {
774
-				do_action( 'woocommerce_new_order_item', $item_id, wc_get_product( $item_id ), $order->get_id() );
774
+				do_action('woocommerce_new_order_item', $item_id, wc_get_product($item_id), $order->get_id());
775 775
 			}
776 776
 		}
777 777
 
778 778
 		// Store fees
779
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
780
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
781
-				$item_id = $order->add_fee( $fee );
779
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
780
+			if (version_compare(WC_VERSION, '3.0', '<')) {
781
+				$item_id = $order->add_fee($fee);
782 782
 			} else {
783 783
 				$item = new WC_Order_Item_Fee();
784
-				$item->set_props( array(
784
+				$item->set_props(array(
785 785
 					'name'      => $fee->name,
786 786
 					'tax_class' => $fee->taxable ? $fee->tax_class : 0,
787 787
 					'total'     => $fee->amount,
@@ -790,79 +790,79 @@  discard block
 block discarded – undo
790 790
 						'total' => $fee->tax_data,
791 791
 					),
792 792
 					'order_id'  => $order->get_id(),
793
-				) );
793
+				));
794 794
 				$item_id = $item->save();
795
-				$order->add_item( $item );
795
+				$order->add_item($item);
796 796
 			}
797 797
 
798
-			if ( ! $item_id ) {
799
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
798
+			if ( ! $item_id) {
799
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
800 800
 			}
801 801
 
802 802
 			// Allow plugins to add order item meta to fees
803
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
804
-				do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
803
+			if (version_compare(WC_VERSION, '3.0', '<')) {
804
+				do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
805 805
 			} else {
806
-				do_action( 'woocommerce_new_order_item', $item_id, $fee, $order->get_id() );
806
+				do_action('woocommerce_new_order_item', $item_id, $fee, $order->get_id());
807 807
 			}
808 808
 		}
809 809
 
810 810
 		// Store tax rows
811
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
812
-			$tax_amount = WC()->cart->get_tax_amount( $tax_rate_id );
813
-			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount( $tax_rate_id );
811
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
812
+			$tax_amount = WC()->cart->get_tax_amount($tax_rate_id);
813
+			$shipping_tax_amount = WC()->cart->get_shipping_tax_amount($tax_rate_id);
814 814
 
815
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
816
-				$item_id = $order->add_tax( $tax_rate_id, $tax_amount, $shipping_tax_amount );
815
+			if (version_compare(WC_VERSION, '3.0', '<')) {
816
+				$item_id = $order->add_tax($tax_rate_id, $tax_amount, $shipping_tax_amount);
817 817
 			} else {
818 818
 				$item = new WC_Order_Item_Tax();
819
-				$item->set_props( array(
819
+				$item->set_props(array(
820 820
 					'rate_id'            => $tax_rate_id,
821 821
 					'tax_total'          => $tax_amount,
822 822
 					'shipping_tax_total' => $shipping_tax_amount,
823
-				) );
824
-				$item->set_rate( $tax_rate_id );
825
-				$item->set_order_id( $order->get_id() );
823
+				));
824
+				$item->set_rate($tax_rate_id);
825
+				$item->set_order_id($order->get_id());
826 826
 				$item_id = $item->save();
827
-				$order->add_item( $item );
827
+				$order->add_item($item);
828 828
 			}
829 829
 
830
-			if ( $tax_rate_id && ! $item_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
831
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
830
+			if ($tax_rate_id && ! $item_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
831
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
832 832
 			}
833 833
 		}
834 834
 
835 835
 		// Store coupons
836
-		$discount = WC()->cart->get_coupon_discount_amount( $code );
837
-		$discount_tax = WC()->cart->get_coupon_discount_tax_amount( $code );
836
+		$discount = WC()->cart->get_coupon_discount_amount($code);
837
+		$discount_tax = WC()->cart->get_coupon_discount_tax_amount($code);
838 838
 
839
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
840
-			if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
841
-				$coupon_id = $order->add_coupon( $code, $discount, $discount_tax );
839
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
840
+			if (version_compare(WC_VERSION, '3.0', '<')) {
841
+				$coupon_id = $order->add_coupon($code, $discount, $discount_tax);
842 842
 			} else {
843 843
 				$item = new WC_Order_Item_Coupon();
844
-				$item->set_props( array(
844
+				$item->set_props(array(
845 845
 					'code'         => $code,
846 846
 					'discount'     => $discount,
847 847
 					'discount_tax' => $discount_tax,
848 848
 					'order_id'     => $order->get_id(),
849
-				) );
849
+				));
850 850
 				$coupon_id = $item->save();
851
-				$order->add_item( $item );
851
+				$order->add_item($item);
852 852
 			}
853 853
 
854
-			if ( ! $coupon_id ) {
855
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
854
+			if ( ! $coupon_id) {
855
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
856 856
 			}
857 857
 		}
858 858
 
859 859
 		// Billing address
860 860
 		$billing_address = array();
861
-		if ( ! empty( $data['token']['card'] ) ) {
861
+		if ( ! empty($data['token']['card'])) {
862 862
 			// Name from Stripe is a full name string.
863
-			$name                          = explode( ' ', $data['token']['card']['name'] );
864
-			$lastname                      = array_pop( $name );
865
-			$firstname                     = implode( ' ', $name );
863
+			$name                          = explode(' ', $data['token']['card']['name']);
864
+			$lastname                      = array_pop($name);
865
+			$firstname                     = implode(' ', $name);
866 866
 			$billing_address['first_name'] = $firstname;
867 867
 			$billing_address['last_name']  = $lastname;
868 868
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -877,7 +877,7 @@  discard block
 block discarded – undo
877 877
 
878 878
 		// Shipping address.
879 879
 		$shipping_address = array();
880
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
880
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
881 881
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
882 882
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
883 883
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -888,7 +888,7 @@  discard block
 block discarded – undo
888 888
 			$shipping_address['city']       = $data['shippingContact']['locality'];
889 889
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
890 890
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
891
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
891
+		} elseif ( ! empty($data['shippingContact'])) {
892 892
 			$shipping_address['first_name'] = $firstname;
893 893
 			$shipping_address['last_name']  = $lastname;
894 894
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -901,33 +901,33 @@  discard block
 block discarded – undo
901 901
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
902 902
 		}
903 903
 
904
-		$order->set_address( $billing_address, 'billing' );
905
-		$order->set_address( $shipping_address, 'shipping' );
904
+		$order->set_address($billing_address, 'billing');
905
+		$order->set_address($shipping_address, 'shipping');
906 906
 
907
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
907
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
908 908
 
909 909
 		// Get the rate object selected by user.
910
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
911
-			foreach ( $package['rates'] as $key => $rate ) {
910
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
911
+			foreach ($package['rates'] as $key => $rate) {
912 912
 				// Loop through user chosen shipping methods.
913
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
914
-					if ( $method === $key ) {
915
-						if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
916
-							$order->add_shipping( $rate );
913
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
914
+					if ($method === $key) {
915
+						if (version_compare(WC_VERSION, '3.0', '<')) {
916
+							$order->add_shipping($rate);
917 917
 						} else {
918 918
 							$item = new WC_Order_Item_Shipping();
919
-							$item->set_props( array(
919
+							$item->set_props(array(
920 920
 								'method_title' => $rate->label,
921 921
 								'method_id'    => $rate->id,
922
-								'total'        => wc_format_decimal( $rate->cost ),
922
+								'total'        => wc_format_decimal($rate->cost),
923 923
 								'taxes'        => $rate->taxes,
924 924
 								'order_id'     => $order->get_id(),
925
-							) );
926
-							foreach ( $rate->get_meta_data() as $key => $value ) {
927
-								$item->add_meta_data( $key, $value, true );
925
+							));
926
+							foreach ($rate->get_meta_data() as $key => $value) {
927
+								$item->add_meta_data($key, $value, true);
928 928
 							}
929 929
 							$item->save();
930
-							$order->add_item( $item );
930
+							$order->add_item($item);
931 931
 						}
932 932
 					}
933 933
 				}
@@ -935,16 +935,16 @@  discard block
 block discarded – undo
935 935
 		}
936 936
 
937 937
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
938
-		$order->set_payment_method( $available_gateways['stripe'] );
939
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
940
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
941
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
942
-		$order->set_total( WC()->cart->tax_total, 'tax' );
943
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
944
-		$order->set_total( WC()->cart->total );
938
+		$order->set_payment_method($available_gateways['stripe']);
939
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
940
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
941
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
942
+		$order->set_total(WC()->cart->tax_total, 'tax');
943
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
944
+		$order->set_total(WC()->cart->total);
945 945
 
946 946
 		// If we got here, the order was created without problems!
947
-		wc_transaction_query( 'commit' );
947
+		wc_transaction_query('commit');
948 948
 
949 949
 		return $order;
950 950
 	}
Please login to merge, or discard this patch.