Completed
Push — master ( a8a1a1...3b0043 )
by Roy
02:58
created
includes/class-wc-stripe-apple-pay.php 1 patch
Spacing   +200 added lines, -200 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
 	}
@@ -66,14 +66,14 @@  discard block
 block discarded – undo
66 66
 	 * @version 3.1.0
67 67
 	 */
68 68
 	public function init() {
69
-		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
70
-		add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 20 );
71
-		add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ) );
72
-		add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
73
-		add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
74
-		add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
75
-		add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
76
-		add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 );
69
+		add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
70
+		add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 20);
71
+		add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'));
72
+		add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay'));
73
+		add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart'));
74
+		add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods'));
75
+		add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method'));
76
+		add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2);
77 77
 	}
78 78
 
79 79
 	/**
@@ -84,47 +84,47 @@  discard block
 block discarded – undo
84 84
 	 * @version 3.1.0
85 85
 	 */
86 86
 	public static function process_apple_pay_domain() {
87
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_domain_nonce' ) ) {
88
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
87
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_domain_nonce')) {
88
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
89 89
 		}
90 90
 
91
-		$secret_key = wc_clean( $_POST['secret_key'] );
91
+		$secret_key = wc_clean($_POST['secret_key']);
92 92
 
93 93
 		try {
94
-			$path     = untrailingslashit( preg_replace( "!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME'] ) );
94
+			$path     = untrailingslashit(preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']));
95 95
 			$dir      = '.well-known';
96 96
 			$file     = 'apple-developer-merchantid-domain-association';
97 97
 			$fullpath = $path . '/' . $dir . '/' . $file;
98
-			$gateway_settings = get_option( 'woocommerce_stripe_settings', '' );
98
+			$gateway_settings = get_option('woocommerce_stripe_settings', '');
99 99
 
100
-			if ( ! file_exists( $path . '/' . $dir ) || ! file_exists( $fullpath ) ) {
101
-				if ( ! mkdir( $path . '/' . $dir, 0755 ) ) {
102
-					throw new Exception( __( 'Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe' ) );
100
+			if ( ! file_exists($path . '/' . $dir) || ! file_exists($fullpath)) {
101
+				if ( ! mkdir($path . '/' . $dir, 0755)) {
102
+					throw new Exception(__('Unable to create domain association folder to domain root.', 'woocommerce-gateway-stripe'));
103 103
 				}
104 104
 
105
-				if ( ! file_exists( $path . '/' . $dir . '/' . 'apple-developer-merchantid-domain-association' ) ) {
106
-					if ( ! copy( WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath ) ) {
107
-						throw new Exception( __( 'Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe' ) );
105
+				if ( ! file_exists($path . '/' . $dir . '/' . 'apple-developer-merchantid-domain-association')) {
106
+					if ( ! copy(WC_STRIPE_PLUGIN_PATH . '/' . $file, $fullpath)) {
107
+						throw new Exception(__('Unable to copy domain association file to domain root.', 'woocommerce-gateway-stripe'));
108 108
 					}
109 109
 				}
110 110
 			}
111 111
 
112 112
 			// At this point then the domain association folder and file should be available.
113 113
 			// Proceed to verify/and or verify again.
114
-			WC_Stripe_Apple_Pay::_register_apple_pay_domain( $secret_key );
114
+			WC_Stripe_Apple_Pay::_register_apple_pay_domain($secret_key);
115 115
 
116 116
 			// No errors to this point, verification success!
117 117
 			$gateway_settings['apple_pay_domain_set'] = 'yes';
118 118
 
119
-			update_option( 'woocommerce_stripe_settings', $gateway_settings );
119
+			update_option('woocommerce_stripe_settings', $gateway_settings);
120 120
 
121
-			wp_send_json( array( 'success' => true, 'message' => __( 'Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe' ) ) );
122
-		} catch ( Exception $e ) {
121
+			wp_send_json(array('success' => true, 'message' => __('Your domain has been verified with Apple Pay!', 'woocommerce-gateway-stripe')));
122
+		} catch (Exception $e) {
123 123
 			$gateway_settings['apple_pay_domain_set'] = 'no';
124 124
 
125
-			update_option( 'woocommerce_stripe_settings', $gateway_settings );
125
+			update_option('woocommerce_stripe_settings', $gateway_settings);
126 126
 
127
-			wp_send_json( array( 'success' => false, 'message' => $e->getMessage() ) );
127
+			wp_send_json(array('success' => false, 'message' => $e->getMessage()));
128 128
 		}
129 129
 	}
130 130
 
@@ -135,9 +135,9 @@  discard block
 block discarded – undo
135 135
 	 * @version 3.1.0
136 136
 	 * @param string $secret_key
137 137
 	 */
138
-	private static function _register_apple_pay_domain( $secret_key = '' ) {
139
-		if ( empty( $secret_key ) ) {
140
-			throw new Exception( __( 'Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe' ) );
138
+	private static function _register_apple_pay_domain($secret_key = '') {
139
+		if (empty($secret_key)) {
140
+			throw new Exception(__('Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe'));
141 141
 		}
142 142
 
143 143
 		$endpoint = 'https://api.stripe.com/v1/apple_pay/domains';
@@ -151,13 +151,13 @@  discard block
 block discarded – undo
151 151
 			'Authorization' => 'Bearer ' . $secret_key,
152 152
 		);
153 153
 
154
-		$response = wp_remote_post( $endpoint, array(
154
+		$response = wp_remote_post($endpoint, array(
155 155
 			'headers' => $headers,
156
-			'body'    => http_build_query( $data ),
157
-		) );
156
+			'body'    => http_build_query($data),
157
+		));
158 158
 
159
-		if ( 200 !== $response['response']['code'] ) {
160
-			throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $response['response']['message'] ) );
159
+		if (200 !== $response['response']['code']) {
160
+			throw new Exception(sprintf(__('Unable to verify domain - %s', 'woocommerce-gateway-stripe'), $response['response']['message']));
161 161
 		}
162 162
 	}
163 163
 
@@ -165,16 +165,16 @@  discard block
 block discarded – undo
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,38 +188,38 @@  discard block
 block discarded – undo
188 188
 	 * @version 3.1.0
189 189
 	 */
190 190
 	public function payment_scripts() {
191
-		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) {
191
+		if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) {
192 192
 			return;
193 193
 		}
194 194
 
195
-		if ( ! $this->is_supported_product_type() ) {
195
+		if ( ! $this->is_supported_product_type()) {
196 196
 			return;
197 197
 		}
198 198
 
199
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
199
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
200 200
 
201
-		wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION );
201
+		wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION);
202 202
 
203
-		wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
204
-		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 );
203
+		wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true);
204
+		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);
205 205
 
206 206
 		$publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key'];
207 207
 
208 208
 		$stripe_params = array(
209 209
 			'key'                                           => $publishable_key,
210 210
 			'currency_code'                                 => get_woocommerce_currency(),
211
-			'country_code'                                  => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
211
+			'country_code'                                  => substr(get_option('woocommerce_default_country'), 0, 2),
212 212
 			'label'                                         => $this->statement_descriptor,
213
-			'ajaxurl'                                       => WC_AJAX::get_endpoint( '%%endpoint%%' ),
214
-			'stripe_apple_pay_nonce'                        => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ),
215
-			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
216
-			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
217
-			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
213
+			'ajaxurl'                                       => WC_AJAX::get_endpoint('%%endpoint%%'),
214
+			'stripe_apple_pay_nonce'                        => wp_create_nonce('_wc_stripe_apple_pay_nonce'),
215
+			'stripe_apple_pay_cart_nonce'                   => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'),
216
+			'stripe_apple_pay_get_shipping_methods_nonce'   => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'),
217
+			'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'),
218 218
 			'needs_shipping'                                => WC()->cart->needs_shipping() ? 'yes' : 'no',
219 219
 			'is_cart_page'                                  => is_cart() ? 'yes' : 'no',
220 220
 		);
221 221
 
222
-		wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) );
222
+		wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params));
223 223
 	}
224 224
 
225 225
 	/**
@@ -227,8 +227,8 @@  discard block
 block discarded – undo
227 227
 	 *
228 228
 	 */
229 229
 	public function is_supported_product_type() {
230
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
231
-			if ( 'subscription' === $values['data']->product_type ) {
230
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
231
+			if ('subscription' === $values['data']->product_type) {
232 232
 				return false;
233 233
 			}
234 234
 		}
@@ -251,19 +251,19 @@  discard block
 block discarded – undo
251 251
 		 */
252 252
 		if (
253 253
 			'yes' !== $this->_gateway_settings['apple_pay']
254
-			|| ! isset( $gateways['stripe'] )
254
+			|| ! isset($gateways['stripe'])
255 255
 		) {
256 256
 			return;
257 257
 		}
258 258
 
259
-		if ( ! $this->is_supported_product_type() ) {
259
+		if ( ! $this->is_supported_product_type()) {
260 260
 			return;
261 261
 		}
262 262
 
263
-		$apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black';
264
-		$button_lang      = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en';
263
+		$apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black';
264
+		$button_lang      = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en';
265 265
 		?>
266
-		<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>
266
+		<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>
267 267
 		<?php
268 268
 	}
269 269
 
@@ -274,11 +274,11 @@  discard block
 block discarded – undo
274 274
 	 * @version 3.1.0
275 275
 	 */
276 276
 	public function generate_apple_pay_cart() {
277
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) {
278
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
277
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) {
278
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
279 279
 		}
280 280
 
281
-		wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
281
+		wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
282 282
 	}
283 283
 
284 284
 	/**
@@ -288,29 +288,29 @@  discard block
 block discarded – undo
288 288
 	 * @version 3.1.0
289 289
 	 * @param array $address
290 290
 	 */
291
-	public function calculate_shipping( $address = array() ) {
292
-		$country  = strtoupper( $address['countryCode'] );
293
-		$state    = strtoupper( $address['administrativeArea'] );
291
+	public function calculate_shipping($address = array()) {
292
+		$country  = strtoupper($address['countryCode']);
293
+		$state    = strtoupper($address['administrativeArea']);
294 294
 		$postcode = $address['postalCode'];
295 295
 		$city     = $address['locality'];
296 296
 
297 297
 		WC()->shipping->reset_shipping();
298 298
 
299
-		if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
300
-			throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) );
301
-		} elseif ( $postcode ) {
302
-			$postcode = wc_format_postcode( $postcode, $country );
299
+		if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) {
300
+			throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe'));
301
+		} elseif ($postcode) {
302
+			$postcode = wc_format_postcode($postcode, $country);
303 303
 		}
304 304
 
305
-		if ( $country ) {
306
-			WC()->customer->set_location( $country, $state, $postcode, $city );
307
-			WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
305
+		if ($country) {
306
+			WC()->customer->set_location($country, $state, $postcode, $city);
307
+			WC()->customer->set_shipping_location($country, $state, $postcode, $city);
308 308
 		} else {
309 309
 			WC()->customer->set_to_base();
310 310
 			WC()->customer->set_shipping_to_base();
311 311
 		}
312 312
 
313
-		WC()->customer->calculated_shipping( true );
313
+		WC()->customer->calculated_shipping(true);
314 314
 
315 315
 		/**
316 316
 		 * Set the shipping package.
@@ -332,17 +332,17 @@  discard block
 block discarded – undo
332 332
 		$packages[0]['destination']['postcode']  = $postcode;
333 333
 		$packages[0]['destination']['city']      = $city;
334 334
 
335
-		foreach ( WC()->cart->get_cart() as $item ) {
336
-			if ( $item['data']->needs_shipping() ) {
337
-				if ( isset( $item['line_total'] ) ) {
335
+		foreach (WC()->cart->get_cart() as $item) {
336
+			if ($item['data']->needs_shipping()) {
337
+				if (isset($item['line_total'])) {
338 338
 					$packages[0]['contents_cost'] += $item['line_total'];
339 339
 				}
340 340
 			}
341 341
 		}
342 342
 
343
-		$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages );
343
+		$packages = apply_filters('woocommerce_cart_shipping_packages', $packages);
344 344
 
345
-		WC()->shipping->calculate_shipping( $packages );
345
+		WC()->shipping->calculate_shipping($packages);
346 346
 	}
347 347
 
348 348
 	/**
@@ -352,18 +352,18 @@  discard block
 block discarded – undo
352 352
 	 * @version 3.1.0
353 353
 	 */
354 354
 	public function get_shipping_methods() {
355
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) {
356
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
355
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) {
356
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
357 357
 		}
358 358
 
359
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
360
-			define( 'WOOCOMMERCE_CART', true );
359
+		if ( ! defined('WOOCOMMERCE_CART')) {
360
+			define('WOOCOMMERCE_CART', true);
361 361
 		}
362 362
 
363 363
 		try {
364
-			$address = array_map( 'wc_clean', $_POST['address'] );
364
+			$address = array_map('wc_clean', $_POST['address']);
365 365
 
366
-			$this->calculate_shipping( $address );
366
+			$this->calculate_shipping($address);
367 367
 
368 368
 			// Set the shipping options.
369 369
 			$currency = get_woocommerce_currency();
@@ -371,13 +371,13 @@  discard block
 block discarded – undo
371 371
 
372 372
 			$packages = WC()->shipping->get_packages();
373 373
 
374
-			if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) {
375
-				foreach ( $packages as $package_key => $package ) {
376
-					if ( empty( $package['rates'] ) ) {
377
-						throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
374
+			if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) {
375
+				foreach ($packages as $package_key => $package) {
376
+					if (empty($package['rates'])) {
377
+						throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
378 378
 					}
379 379
 
380
-					foreach ( $package['rates'] as $key => $rate ) {
380
+					foreach ($package['rates'] as $key => $rate) {
381 381
 						$data[] = array(
382 382
 							'id'       => $rate->id,
383 383
 							'label'    => $rate->label,
@@ -391,16 +391,16 @@  discard block
 block discarded – undo
391 391
 				}
392 392
 
393 393
 				// Auto select the first shipping method.
394
-				WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) );
394
+				WC()->session->set('chosen_shipping_methods', array($data[0]['id']));
395 395
 
396 396
 				WC()->cart->calculate_totals();
397 397
 
398
-				wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
398
+				wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
399 399
 			} else {
400
-				throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) );
400
+				throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe'));
401 401
 			}
402
-		} catch ( Exception $e ) {
403
-			wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
402
+		} catch (Exception $e) {
403
+			wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
404 404
 		}
405 405
 	}
406 406
 
@@ -411,29 +411,29 @@  discard block
 block discarded – undo
411 411
 	 * @version 3.1.0
412 412
 	 */
413 413
 	public function update_shipping_method() {
414
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
415
-			define( 'WOOCOMMERCE_CART', true );
414
+		if ( ! defined('WOOCOMMERCE_CART')) {
415
+			define('WOOCOMMERCE_CART', true);
416 416
 		}
417 417
 
418
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) {
419
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
418
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) {
419
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
420 420
 		}
421 421
 
422
-		$selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] );
422
+		$selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']);
423 423
 
424
-		WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) );
424
+		WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier']));
425 425
 
426 426
 		WC()->cart->calculate_totals();
427 427
 
428 428
 		// Send back the new cart total.
429 429
 		$currency  = get_woocommerce_currency();
430
-		$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) );
430
+		$tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp));
431 431
 		$data      = array(
432 432
 			'total' => WC()->cart->total,
433 433
 		);
434 434
 
435 435
 		// Include fees and taxes as displayItems.
436
-		foreach ( WC()->cart->fees as $key => $fee ) {
436
+		foreach (WC()->cart->fees as $key => $fee) {
437 437
 			$data['items'][] = array(
438 438
 				'label'  => $fee->name,
439 439
 				'amount' => array(
@@ -442,9 +442,9 @@  discard block
 block discarded – undo
442 442
 				),
443 443
 			);
444 444
 		}
445
-		if ( 0 < $tax_total ) {
445
+		if (0 < $tax_total) {
446 446
 			$data['items'][] = array(
447
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
447
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
448 448
 				'amount' => array(
449 449
 					'currency' => $currency,
450 450
 					'value'    => $tax_total,
@@ -452,7 +452,7 @@  discard block
 block discarded – undo
452 452
 			);
453 453
 		}
454 454
 
455
-		wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
455
+		wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total));
456 456
 	}
457 457
 
458 458
 	/**
@@ -463,35 +463,35 @@  discard block
 block discarded – undo
463 463
 	 * @version 3.1.0
464 464
 	 */
465 465
 	public function process_apple_pay() {
466
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) {
467
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
466
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) {
467
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
468 468
 		}
469 469
 
470 470
 		try {
471
-			$result = array_map( 'wc_clean', $_POST['result'] );
471
+			$result = array_map('wc_clean', $_POST['result']);
472 472
 
473
-			$order = $this->create_order( $result );
473
+			$order = $this->create_order($result);
474 474
 
475 475
 			// Handle payment.
476
-			if ( $order->get_total() > 0 ) {
476
+			if ($order->get_total() > 0) {
477 477
 
478
-				if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) {
479
-					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 ) ) );
478
+				if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) {
479
+					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)));
480 480
 				}
481 481
 
482
-				WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" );
482
+				WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}");
483 483
 
484 484
 				// Make the request.
485
-				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) );
485
+				$response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id']));
486 486
 
487
-				if ( is_wp_error( $response ) ) {
487
+				if (is_wp_error($response)) {
488 488
 					$localized_messages = $this->get_localized_messages();
489 489
 
490
-					throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) );
490
+					throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message()));
491 491
 				}
492 492
 
493 493
 				// Process valid response.
494
-				$this->process_response( $response, $order );
494
+				$this->process_response($response, $order);
495 495
 			} else {
496 496
 				$order->payment_complete();
497 497
 			}
@@ -499,24 +499,24 @@  discard block
 block discarded – undo
499 499
 			// Remove cart.
500 500
 			WC()->cart->empty_cart();
501 501
 
502
-			update_post_meta( $order->id, '_customer_user', get_current_user_id() );
503
-			update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) );
502
+			update_post_meta($order->id, '_customer_user', get_current_user_id());
503
+			update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe'));
504 504
 
505 505
 			// Return thank you page redirect.
506
-			wp_send_json( array(
506
+			wp_send_json(array(
507 507
 				'success'  => 'true',
508
-				'redirect' => $this->get_return_url( $order ),
509
-			) );
508
+				'redirect' => $this->get_return_url($order),
509
+			));
510 510
 
511
-		} catch ( Exception $e ) {
512
-			WC()->session->set( 'refresh_totals', true );
513
-			WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
511
+		} catch (Exception $e) {
512
+			WC()->session->set('refresh_totals', true);
513
+			WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
514 514
 
515
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
516
-				$this->send_failed_order_email( $order->id );
515
+			if ($order->has_status(array('pending', 'failed'))) {
516
+				$this->send_failed_order_email($order->id);
517 517
 			}
518 518
 
519
-			wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) );
519
+			wp_send_json(array('success' => 'false', 'msg' => $e->getMessage()));
520 520
 		}
521 521
 	}
522 522
 
@@ -526,14 +526,14 @@  discard block
 block discarded – undo
526 526
 	 * @param string $source token
527 527
 	 * @return array()
528 528
 	 */
529
-	protected function generate_payment_request( $order, $source ) {
529
+	protected function generate_payment_request($order, $source) {
530 530
 		$post_data                = array();
531
-		$post_data['currency']    = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
532
-		$post_data['amount']      = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] );
533
-		$post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() );
531
+		$post_data['currency']    = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency());
532
+		$post_data['amount']      = $this->get_stripe_amount($order->get_total(), $post_data['currency']);
533
+		$post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number());
534 534
 		$post_data['capture']     = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false';
535 535
 
536
-		if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
536
+		if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) {
537 537
 			$post_data['receipt_email'] = $order->billing_email;
538 538
 		}
539 539
 
@@ -548,7 +548,7 @@  discard block
 block discarded – undo
548 548
 		 * @param WC_Order $order
549 549
 		 * @param object $source
550 550
 		 */
551
-		return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order );
551
+		return apply_filters('wc_stripe_generate_payment_request', $post_data, $order);
552 552
 	}
553 553
 
554 554
 	/**
@@ -557,14 +557,14 @@  discard block
 block discarded – undo
557 557
 	 * @since 3.1.0
558 558
 	 * @version 3.1.0
559 559
 	 */
560
-	public function build_shipping_methods( $shipping_methods ) {
561
-		if ( empty( $shipping_methods ) ) {
560
+	public function build_shipping_methods($shipping_methods) {
561
+		if (empty($shipping_methods)) {
562 562
 			return array();
563 563
 		}
564 564
 
565 565
 		$shipping = array();
566 566
 
567
-		foreach ( $shipping_methods as $method ) {
567
+		foreach ($shipping_methods as $method) {
568 568
 			$shipping[] = array(
569 569
 				'label'      => $method['label'],
570 570
 				'detail'     => '',
@@ -583,53 +583,53 @@  discard block
 block discarded – undo
583 583
 	 * @version 3.1.0
584 584
 	 */
585 585
 	public function build_line_items() {
586
-		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
587
-			define( 'WOOCOMMERCE_CART', true );
586
+		if ( ! defined('WOOCOMMERCE_CART')) {
587
+			define('WOOCOMMERCE_CART', true);
588 588
 		}
589 589
 
590
-		$decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 );
590
+		$decimals = apply_filters('wc_stripe_apple_pay_decimals', 2);
591 591
 
592 592
 		$items = array();
593 593
 
594
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
595
-			$amount         = wc_format_decimal( $values['line_subtotal'], $decimals );
594
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
595
+			$amount         = wc_format_decimal($values['line_subtotal'], $decimals);
596 596
 			$quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : '';
597 597
 
598 598
 			$item = array(
599 599
 				'type'   => 'final',
600 600
 				'label'  => $values['data']->post->post_title . $quantity_label,
601
-				'amount' => wc_format_decimal( $amount, $decimals ),
601
+				'amount' => wc_format_decimal($amount, $decimals),
602 602
 			);
603 603
 
604 604
 			$items[] = $item;
605 605
 		}
606 606
 
607
-		$discounts   = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals );
608
-		$tax         = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals );
609
-		$shipping    = wc_format_decimal( WC()->cart->shipping_total, $decimals );
610
-		$item_total  = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts;
611
-		$order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals );
607
+		$discounts   = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals);
608
+		$tax         = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals);
609
+		$shipping    = wc_format_decimal(WC()->cart->shipping_total, $decimals);
610
+		$item_total  = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts;
611
+		$order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals);
612 612
 
613
-		if ( wc_tax_enabled() ) {
613
+		if (wc_tax_enabled()) {
614 614
 			$items[] = array(
615 615
 				'type'   => 'final',
616
-				'label'  => __( 'Tax', 'woocommerce-gateway-stripe' ),
616
+				'label'  => __('Tax', 'woocommerce-gateway-stripe'),
617 617
 				'amount' => $tax,
618 618
 			);
619 619
 		}
620 620
 
621
-		if ( WC()->cart->needs_shipping() ) {
621
+		if (WC()->cart->needs_shipping()) {
622 622
 			$items[] = array(
623 623
 				'type'   => 'final',
624
-				'label'  => __( 'Shipping', 'woocommerce-gateway-stripe' ),
624
+				'label'  => __('Shipping', 'woocommerce-gateway-stripe'),
625 625
 				'amount' => $shipping,
626 626
 			);
627 627
 		}
628 628
 
629
-		if ( WC()->cart->has_discount() ) {
629
+		if (WC()->cart->has_discount()) {
630 630
 			$items[] = array(
631 631
 				'type'   => 'final',
632
-				'label'  => __( 'Discount', 'woocommerce-gateway-stripe' ),
632
+				'label'  => __('Discount', 'woocommerce-gateway-stripe'),
633 633
 				'amount' => $discounts,
634 634
 			);
635 635
 		}
@@ -645,24 +645,24 @@  discard block
 block discarded – undo
645 645
 	 * @param array $data
646 646
 	 * @return object $order
647 647
 	 */
648
-	public function create_order( $data = array() ) {
649
-		if ( empty( $data ) ) {
650
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
648
+	public function create_order($data = array()) {
649
+		if (empty($data)) {
650
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
651 651
 		}
652 652
 
653 653
 		$order = wc_create_order();
654 654
 
655
-		if ( is_wp_error( $order ) ) {
656
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) );
657
-		} elseif ( false === $order ) {
658
-			throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) );
655
+		if (is_wp_error($order)) {
656
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520));
657
+		} elseif (false === $order) {
658
+			throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521));
659 659
 		} else {
660 660
 			$order_id = $order->id;
661
-			do_action( 'woocommerce_new_order', $order_id );
661
+			do_action('woocommerce_new_order', $order_id);
662 662
 		}
663 663
 
664 664
 		// Store the line items to the new/resumed order
665
-		foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
665
+		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
666 666
 			$item_id = $order->add_product(
667 667
 				$values['data'],
668 668
 				$values['quantity'],
@@ -678,47 +678,47 @@  discard block
 block discarded – undo
678 678
 				)
679 679
 			);
680 680
 
681
-			if ( ! $item_id ) {
682
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) );
681
+			if ( ! $item_id) {
682
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525));
683 683
 			}
684 684
 
685 685
 			// Allow plugins to add order item meta
686
-			do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
686
+			do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key);
687 687
 		}
688 688
 
689 689
 		// Store fees
690
-		foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
691
-			$item_id = $order->add_fee( $fee );
690
+		foreach (WC()->cart->get_fees() as $fee_key => $fee) {
691
+			$item_id = $order->add_fee($fee);
692 692
 
693
-			if ( ! $item_id ) {
694
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) );
693
+			if ( ! $item_id) {
694
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526));
695 695
 			}
696 696
 
697 697
 			// Allow plugins to add order item meta to fees
698
-			do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
698
+			do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key);
699 699
 		}
700 700
 
701 701
 		// Store tax rows
702
-		foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) {
703
-			if ( $tax_rate_id && ! $order->add_tax( $tax_rate_id, WC()->cart->get_tax_amount( $tax_rate_id ), WC()->cart->get_shipping_tax_amount( $tax_rate_id ) ) && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
704
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) );
702
+		foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
703
+			if ($tax_rate_id && ! $order->add_tax($tax_rate_id, WC()->cart->get_tax_amount($tax_rate_id), WC()->cart->get_shipping_tax_amount($tax_rate_id)) && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
704
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528));
705 705
 			}
706 706
 		}
707 707
 
708 708
 		// Store coupons
709
-		foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
710
-			if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) {
711
-				throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) );
709
+		foreach (WC()->cart->get_coupons() as $code => $coupon) {
710
+			if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) {
711
+				throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529));
712 712
 			}
713 713
 		}
714 714
 
715 715
 		// Billing address
716 716
 		$billing_address = array();
717
-		if ( ! empty( $data['token']['card'] ) ) {
717
+		if ( ! empty($data['token']['card'])) {
718 718
 			// Name from Stripe is a full name string.
719
-			$name                          = explode( ' ', $data['token']['card']['name'] );
720
-			$lastname                      = array_pop( $name );
721
-			$firstname                     = implode( ' ', $name );
719
+			$name                          = explode(' ', $data['token']['card']['name']);
720
+			$lastname                      = array_pop($name);
721
+			$firstname                     = implode(' ', $name);
722 722
 			$billing_address['first_name'] = $firstname;
723 723
 			$billing_address['last_name']  = $lastname;
724 724
 			$billing_address['email']      = $data['shippingContact']['emailAddress'];
@@ -733,7 +733,7 @@  discard block
 block discarded – undo
733 733
 
734 734
 		// Shipping address.
735 735
 		$shipping_address = array();
736
-		if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) {
736
+		if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) {
737 737
 			$shipping_address['first_name'] = $data['shippingContact']['givenName'];
738 738
 			$shipping_address['last_name']  = $data['shippingContact']['familyName'];
739 739
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -744,7 +744,7 @@  discard block
 block discarded – undo
744 744
 			$shipping_address['city']       = $data['shippingContact']['locality'];
745 745
 			$shipping_address['state']      = $data['shippingContact']['administrativeArea'];
746 746
 			$shipping_address['postcode']   = $data['shippingContact']['postalCode'];
747
-		} elseif ( ! empty( $data['shippingContact'] ) ) {
747
+		} elseif ( ! empty($data['shippingContact'])) {
748 748
 			$shipping_address['first_name'] = $firstname;
749 749
 			$shipping_address['last_name']  = $lastname;
750 750
 			$shipping_address['email']      = $data['shippingContact']['emailAddress'];
@@ -757,34 +757,34 @@  discard block
 block discarded – undo
757 757
 			$shipping_address['postcode']   = $data['token']['card']['address_zip'];
758 758
 		}
759 759
 
760
-		$order->set_address( $billing_address, 'billing' );
761
-		$order->set_address( $shipping_address, 'shipping' );
760
+		$order->set_address($billing_address, 'billing');
761
+		$order->set_address($shipping_address, 'shipping');
762 762
 
763
-		WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
763
+		WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages());
764 764
 
765 765
 		// Get the rate object selected by user.
766
-		foreach ( WC()->shipping->get_packages() as $package_key => $package ) {
767
-			foreach ( $package['rates'] as $key => $rate ) {
766
+		foreach (WC()->shipping->get_packages() as $package_key => $package) {
767
+			foreach ($package['rates'] as $key => $rate) {
768 768
 				// Loop through user chosen shipping methods.
769
-				foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) {
770
-					if ( $method === $key ) {
771
-						$order->add_shipping( $rate );
769
+				foreach (WC()->session->get('chosen_shipping_methods') as $method) {
770
+					if ($method === $key) {
771
+						$order->add_shipping($rate);
772 772
 					}
773 773
 				}
774 774
 			}
775 775
 		}
776 776
 
777 777
 		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
778
-		$order->set_payment_method( $available_gateways['stripe'] );
779
-		$order->set_total( WC()->cart->shipping_total, 'shipping' );
780
-		$order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' );
781
-		$order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' );
782
-		$order->set_total( WC()->cart->tax_total, 'tax' );
783
-		$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
784
-		$order->set_total( WC()->cart->total );
778
+		$order->set_payment_method($available_gateways['stripe']);
779
+		$order->set_total(WC()->cart->shipping_total, 'shipping');
780
+		$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
781
+		$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
782
+		$order->set_total(WC()->cart->tax_total, 'tax');
783
+		$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
784
+		$order->set_total(WC()->cart->total);
785 785
 
786 786
 		// If we got here, the order was created without problems!
787
-		wc_transaction_query( 'commit' );
787
+		wc_transaction_query('commit');
788 788
 
789 789
 		return $order;
790 790
 	}
Please login to merge, or discard this patch.