Completed
Push — master ( 302e86...a53ae1 )
by Roy
01:59
created
includes/class-wc-stripe-order-handler.php 1 patch
Spacing   +173 added lines, -173 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
 
@@ -20,12 +20,12 @@  discard block
 block discarded – undo
20 20
 	public function __construct() {
21 21
 		self::$_this = $this;
22 22
 
23
-		add_action( 'wp', array( $this, 'maybe_process_redirect_order' ) );
24
-		add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'capture_payment' ) );
25
-		add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'capture_payment' ) );
26
-		add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'cancel_payment' ) );
27
-		add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'cancel_payment' ) );
28
-		add_action( 'wc_ajax_wc_stripe_validate_checkout', array( $this, 'validate_checkout' ) );
23
+		add_action('wp', array($this, 'maybe_process_redirect_order'));
24
+		add_action('woocommerce_order_status_on-hold_to_processing', array($this, 'capture_payment'));
25
+		add_action('woocommerce_order_status_on-hold_to_completed', array($this, 'capture_payment'));
26
+		add_action('woocommerce_order_status_on-hold_to_cancelled', array($this, 'cancel_payment'));
27
+		add_action('woocommerce_order_status_on-hold_to_refunded', array($this, 'cancel_payment'));
28
+		add_action('wc_ajax_wc_stripe_validate_checkout', array($this, 'validate_checkout'));
29 29
 	}
30 30
 
31 31
 	/**
@@ -46,25 +46,25 @@  discard block
 block discarded – undo
46 46
 	 * @since 4.0.0
47 47
 	 * @version 4.0.0
48 48
 	 */
49
-	public function process_redirect_payment( $order_id, $retry = true ) {
49
+	public function process_redirect_payment($order_id, $retry = true) {
50 50
 		try {
51
-			$source = wc_clean( $_GET['source'] );
51
+			$source = wc_clean($_GET['source']);
52 52
 
53
-			if ( empty( $source ) ) {
53
+			if (empty($source)) {
54 54
 				return;
55 55
 			}
56 56
 
57
-			if ( empty( $order_id ) ) {
57
+			if (empty($order_id)) {
58 58
 				return;
59 59
 			}
60 60
 
61
-			$order = wc_get_order( $order_id );
61
+			$order = wc_get_order($order_id);
62 62
 
63
-			if ( ! is_object( $order ) ) {
63
+			if ( ! is_object($order)) {
64 64
 				return;
65 65
 			}
66 66
 
67
-			if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() || 'on-hold' === $order->get_status() ) {
67
+			if ('processing' === $order->get_status() || 'completed' === $order->get_status() || 'on-hold' === $order->get_status()) {
68 68
 				return;
69 69
 			}
70 70
 
@@ -72,101 +72,101 @@  discard block
 block discarded – undo
72 72
 			$response = null;
73 73
 
74 74
 			// This will throw exception if not valid.
75
-			$this->validate_minimum_order_amount( $order );
75
+			$this->validate_minimum_order_amount($order);
76 76
 
77
-			WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
77
+			WC_Stripe_Logger::log("Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}");
78 78
 
79 79
 			// Prep source object.
80 80
 			$source_object           = new stdClass();
81 81
 			$source_object->token_id = '';
82
-			$source_object->customer = $this->get_stripe_customer_id( $order );
82
+			$source_object->customer = $this->get_stripe_customer_id($order);
83 83
 			$source_object->source   = $source;
84 84
 
85 85
 			/**
86 86
 			 * First check if the source is chargeable at this time. If not,
87 87
 			 * webhook will take care of it later.
88 88
 			 */
89
-			$source_info = WC_Stripe_API::retrieve( 'sources/' . $source );
89
+			$source_info = WC_Stripe_API::retrieve('sources/' . $source);
90 90
 
91
-			if ( ! empty( $source_info->error ) ) {
92
-				throw new Exception( $source_info->error->message );
91
+			if ( ! empty($source_info->error)) {
92
+				throw new Exception($source_info->error->message);
93 93
 			}
94 94
 
95
-			if ( 'failed' === $source_info->status || 'canceled' === $source_info->status ) {
96
-				throw new Exception( __( 'Unable to process this payment, please try again or use alternative method.', 'woocommerce-gateway-stripe' ) );
95
+			if ('failed' === $source_info->status || 'canceled' === $source_info->status) {
96
+				throw new Exception(__('Unable to process this payment, please try again or use alternative method.', 'woocommerce-gateway-stripe'));
97 97
 			}
98 98
 
99 99
 			// If already consumed, then ignore request.
100
-			if ( 'consumed' === $source_info->status ) {
100
+			if ('consumed' === $source_info->status) {
101 101
 				return;
102 102
 			}
103 103
 
104 104
 			// If not chargeable, then ignore request.
105
-			if ( 'chargeable' !== $source_info->status ) {
105
+			if ('chargeable' !== $source_info->status) {
106 106
 				return;
107 107
 			}
108 108
 
109 109
 			// Make the request.
110
-			$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) );
110
+			$response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object));
111 111
 
112
-			if ( ! empty( $response->error ) ) {
112
+			if ( ! empty($response->error)) {
113 113
 				// If it is an API error such connection or server, let's retry.
114
-				if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) {
115
-					if ( $retry ) {
116
-						sleep( 5 );
117
-						return $this->process_redirect_payment( $order_id, false );
114
+				if ('api_connection_error' === $response->error->type || 'api_error' === $response->error->type) {
115
+					if ($retry) {
116
+						sleep(5);
117
+						return $this->process_redirect_payment($order_id, false);
118 118
 					} else {
119 119
 						$message = 'API connection error and retries exhausted.';
120
-						$order->add_order_note( $message );
121
-						throw new Exception( $message );
120
+						$order->add_order_note($message);
121
+						throw new Exception($message);
122 122
 					}
123 123
 				}
124 124
 
125 125
 				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
126
-				if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) {
127
-					delete_user_meta( WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(), '_stripe_customer_id' );
126
+				if (preg_match('/No such customer/i', $response->error->message) && $retry) {
127
+					delete_user_meta(WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(), '_stripe_customer_id');
128 128
 
129
-					return $this->process_redirect_payment( $order_id, false );
129
+					return $this->process_redirect_payment($order_id, false);
130 130
 
131
-				} elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) {
131
+				} elseif (preg_match('/No such token/i', $response->error->message) && $source_object->token_id) {
132 132
 					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
133 133
 
134
-					$wc_token = WC_Payment_Tokens::get( $source_object->token_id );
134
+					$wc_token = WC_Payment_Tokens::get($source_object->token_id);
135 135
 					$wc_token->delete();
136
-					$message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
137
-					$order->add_order_note( $message );
138
-					throw new Exception( $message );
136
+					$message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe');
137
+					$order->add_order_note($message);
138
+					throw new Exception($message);
139 139
 				}
140 140
 
141 141
 				$localized_messages = WC_Stripe_Helper::get_localized_messages();
142 142
 
143
-				if ( 'card_error' === $response->error->type ) {
144
-					$message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
143
+				if ('card_error' === $response->error->type) {
144
+					$message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message;
145 145
 				} else {
146
-					$message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
146
+					$message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message;
147 147
 				}
148 148
 
149
-				throw new Exception( $message );
149
+				throw new Exception($message);
150 150
 			}
151 151
 
152
-			do_action( 'wc_gateway_stripe_process_redirect_payment', $response, $order );
152
+			do_action('wc_gateway_stripe_process_redirect_payment', $response, $order);
153 153
 
154
-			$this->process_response( $response, $order );
154
+			$this->process_response($response, $order);
155 155
 
156
-		} catch ( Exception $e ) {
157
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
156
+		} catch (Exception $e) {
157
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
158 158
 
159
-			do_action( 'wc_gateway_stripe_process_redirect_payment_error', $e, $order );
159
+			do_action('wc_gateway_stripe_process_redirect_payment_error', $e, $order);
160 160
 
161 161
 			/* translators: error message */
162
-			$order->update_status( 'failed', sprintf( __( 'Stripe payment failed: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );
162
+			$order->update_status('failed', sprintf(__('Stripe payment failed: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
163 163
 
164
-			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
165
-				$this->send_failed_order_email( $order_id );
164
+			if ($order->has_status(array('pending', 'failed'))) {
165
+				$this->send_failed_order_email($order_id);
166 166
 			}
167 167
 
168
-			wc_add_notice( $e->getMessage(), 'error' );
169
-			wp_safe_redirect( wc_get_checkout_url() );
168
+			wc_add_notice($e->getMessage(), 'error');
169
+			wp_safe_redirect(wc_get_checkout_url());
170 170
 			exit;
171 171
 		}
172 172
 	}
@@ -178,13 +178,13 @@  discard block
 block discarded – undo
178 178
 	 * @version 4.0.0
179 179
 	 */
180 180
 	public function maybe_process_redirect_order() {
181
-		if ( ! is_order_received_page() || empty( $_GET['client_secret'] ) || empty( $_GET['source'] ) ) {
181
+		if ( ! is_order_received_page() || empty($_GET['client_secret']) || empty($_GET['source'])) {
182 182
 			return;
183 183
 		}
184 184
 
185
-		$order_id = wc_clean( $_GET['order_id'] );
185
+		$order_id = wc_clean($_GET['order_id']);
186 186
 
187
-		$this->process_redirect_payment( $order_id );
187
+		$this->process_redirect_payment($order_id);
188 188
 	}
189 189
 
190 190
 	/**
@@ -194,52 +194,52 @@  discard block
 block discarded – undo
194 194
 	 * @version 4.0.0
195 195
 	 * @param  int $order_id
196 196
 	 */
197
-	public function capture_payment( $order_id ) {
198
-		$order = wc_get_order( $order_id );
197
+	public function capture_payment($order_id) {
198
+		$order = wc_get_order($order_id);
199 199
 
200
-		if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) {
201
-			$charge   = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
202
-			$captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true );
200
+		if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) {
201
+			$charge   = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
202
+			$captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true);
203 203
 
204
-			if ( $charge && 'no' === $captured ) {
204
+			if ($charge && 'no' === $captured) {
205 205
 				$order_total = $order->get_total();
206 206
 
207
-				if ( 0 < $order->get_total_refunded() ) {
207
+				if (0 < $order->get_total_refunded()) {
208 208
 					$order_total = $order_total - $order->get_total_refunded();
209 209
 				}
210 210
 
211
-				$result = WC_Stripe_API::request( array(
212
-					'amount'   => WC_Stripe_Helper::get_stripe_amount( $order_total ),
211
+				$result = WC_Stripe_API::request(array(
212
+					'amount'   => WC_Stripe_Helper::get_stripe_amount($order_total),
213 213
 					'expand[]' => 'balance_transaction',
214
-				), 'charges/' . $charge . '/capture' );
214
+				), 'charges/' . $charge . '/capture');
215 215
 
216
-				if ( ! empty( $result->error ) ) {
216
+				if ( ! empty($result->error)) {
217 217
 					/* translators: error message */
218
-					$order->update_status( 'failed', sprintf( __( 'Unable to capture charge! %s', 'woocommerce-gateway-stripe' ), $result->error->message ) );
218
+					$order->update_status('failed', sprintf(__('Unable to capture charge! %s', 'woocommerce-gateway-stripe'), $result->error->message));
219 219
 				} else {
220 220
 					/* translators: transaction id */
221
-					$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) );
222
-					WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' );
221
+					$order->add_order_note(sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id));
222
+					WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', 'yes') : $order->update_meta_data('_stripe_charge_captured', 'yes');
223 223
 
224 224
 					// Store other data such as fees
225
-					WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $result->id ) : $order->set_transaction_id( $result->id );
225
+					WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $result->id) : $order->set_transaction_id($result->id);
226 226
 
227
-					if ( isset( $result->balance_transaction ) && isset( $result->balance_transaction->fee ) ) {
227
+					if (isset($result->balance_transaction) && isset($result->balance_transaction->fee)) {
228 228
 						// Fees and Net needs to both come from Stripe to be accurate as the returned
229 229
 						// values are in the local currency of the Stripe account, not from WC.
230
-						$fee = ! empty( $result->balance_transaction->fee ) ? WC_Stripe_Helper::format_balance_fee( $result->balance_transaction, 'fee' ) : 0;
231
-						$net = ! empty( $result->balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $result->balance_transaction, 'net' ) : 0;
232
-						WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, parent::META_NAME_FEE, $fee ) : $order->update_meta_data( parent::META_NAME_FEE, $fee );
233
-						WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, parent::META_NAME_NET, $net ) : $order->update_meta_data( parent::META_NAME_NET, $net );
230
+						$fee = ! empty($result->balance_transaction->fee) ? WC_Stripe_Helper::format_balance_fee($result->balance_transaction, 'fee') : 0;
231
+						$net = ! empty($result->balance_transaction->net) ? WC_Stripe_Helper::format_balance_fee($result->balance_transaction, 'net') : 0;
232
+						WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, parent::META_NAME_FEE, $fee) : $order->update_meta_data(parent::META_NAME_FEE, $fee);
233
+						WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, parent::META_NAME_NET, $net) : $order->update_meta_data(parent::META_NAME_NET, $net);
234 234
 					}
235 235
 
236
-					if ( is_callable( array( $order, 'save' ) ) ) {
236
+					if (is_callable(array($order, 'save'))) {
237 237
 						$order->save();
238 238
 					}
239 239
 				}
240 240
 
241 241
 				// This hook fires when admin manually changes order status to processing or completed.
242
-				do_action( 'woocommerce_stripe_process_manual_capture', $order, $result );
242
+				do_action('woocommerce_stripe_process_manual_capture', $order, $result);
243 243
 			}
244 244
 		}
245 245
 	}
@@ -251,32 +251,32 @@  discard block
 block discarded – undo
251 251
 	 * @version 4.0.0
252 252
 	 * @param  int $order_id
253 253
 	 */
254
-	public function cancel_payment( $order_id ) {
255
-		$order = wc_get_order( $order_id );
254
+	public function cancel_payment($order_id) {
255
+		$order = wc_get_order($order_id);
256 256
 
257
-		if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) {
258
-			$charge_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
257
+		if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) {
258
+			$charge_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
259 259
 
260
-			if ( $charge_id ) {
261
-				$result = WC_Stripe_API::request( array(
262
-					'amount' => WC_Stripe_Helper::get_stripe_amount( $order->get_total() ),
263
-				), 'charges/' . $charge_id . '/refund' );
260
+			if ($charge_id) {
261
+				$result = WC_Stripe_API::request(array(
262
+					'amount' => WC_Stripe_Helper::get_stripe_amount($order->get_total()),
263
+				), 'charges/' . $charge_id . '/refund');
264 264
 
265
-				if ( ! empty( $result->error ) ) {
266
-					$order->add_order_note( __( 'Unable to refund charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->error->message );
265
+				if ( ! empty($result->error)) {
266
+					$order->add_order_note(__('Unable to refund charge!', 'woocommerce-gateway-stripe') . ' ' . $result->error->message);
267 267
 				} else {
268 268
 					/* translators: transaction id */
269
-					$order->add_order_note( sprintf( __( 'Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) );
270
-					WC_Stripe_Helper::is_pre_30() ? delete_post_meta( $order_id, '_stripe_charge_captured' ) : $order->delete_meta_data( '_stripe_charge_captured' );
271
-					WC_Stripe_Helper::is_pre_30() ? delete_post_meta( $order_id, '_transaction_id' ) : $order->delete_meta_data( '_stripe_transaction_id' );
269
+					$order->add_order_note(sprintf(__('Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id));
270
+					WC_Stripe_Helper::is_pre_30() ? delete_post_meta($order_id, '_stripe_charge_captured') : $order->delete_meta_data('_stripe_charge_captured');
271
+					WC_Stripe_Helper::is_pre_30() ? delete_post_meta($order_id, '_transaction_id') : $order->delete_meta_data('_stripe_transaction_id');
272 272
 
273
-					if ( is_callable( array( $order, 'save' ) ) ) {
273
+					if (is_callable(array($order, 'save'))) {
274 274
 						$order->save();
275 275
 					}
276 276
 				}
277 277
 
278 278
 				// This hook fires when admin manually changes order status to cancel.
279
-				do_action( 'woocommerce_stripe_process_manual_cancel', $order, $result );
279
+				do_action('woocommerce_stripe_process_manual_cancel', $order, $result);
280 280
 			}
281 281
 		}
282 282
 	}
@@ -289,21 +289,21 @@  discard block
 block discarded – undo
289 289
 	 * @param string $field
290 290
 	 * @return string $error_field
291 291
 	 */
292
-	public function normalize_field( $field ) {
292
+	public function normalize_field($field) {
293 293
 		$checkout_fields = WC()->checkout->get_checkout_fields();
294 294
 		$org_str         = array();
295 295
 		$replace_str     = array();
296 296
 
297
-		if ( array_key_exists( $field, $checkout_fields['billing'] ) ) {
298
-			$error_field = $checkout_fields['billing'][ $field ]['label'];
299
-		} elseif ( array_key_exists( $field, $checkout_fields['shipping'] ) ) {
300
-			$error_field = $checkout_fields['shipping'][ $field ]['label'];
301
-		} elseif ( array_key_exists( $field, $checkout_fields['order'] ) ) {
302
-			$error_field = $checkout_fields['order'][ $field ]['label'];
303
-		} elseif ( array_key_exists( $field, $checkout_fields['account'] ) ) {
304
-			$error_field = $checkout_fields['account'][ $field ]['label'];
297
+		if (array_key_exists($field, $checkout_fields['billing'])) {
298
+			$error_field = $checkout_fields['billing'][$field]['label'];
299
+		} elseif (array_key_exists($field, $checkout_fields['shipping'])) {
300
+			$error_field = $checkout_fields['shipping'][$field]['label'];
301
+		} elseif (array_key_exists($field, $checkout_fields['order'])) {
302
+			$error_field = $checkout_fields['order'][$field]['label'];
303
+		} elseif (array_key_exists($field, $checkout_fields['account'])) {
304
+			$error_field = $checkout_fields['account'][$field]['label'];
305 305
 		} else {
306
-			$error_field = str_replace( '_', ' ', $field );
306
+			$error_field = str_replace('_', ' ', $field);
307 307
 
308 308
 			$org_str[]     = 'stripe';
309 309
 			$replace_str[] = '';
@@ -318,9 +318,9 @@  discard block
 block discarded – undo
318 318
 			$replace_str[] = 'SOFORT';
319 319
 
320 320
 			$org_str[]     = 'owner';
321
-			$replace_str[] = __( 'Owner', 'woocommerce-gateway-stripe' );
321
+			$replace_str[] = __('Owner', 'woocommerce-gateway-stripe');
322 322
 
323
-			$error_field   = str_replace( $org_str, $replace_str, $error_field );
323
+			$error_field   = str_replace($org_str, $replace_str, $error_field);
324 324
 		}
325 325
 
326 326
 		return $error_field;
@@ -333,154 +333,154 @@  discard block
 block discarded – undo
333 333
 	 * @version 4.0.0
334 334
 	 */
335 335
 	public function validate_checkout() {
336
-		if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_nonce' ) ) {
337
-			wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce-gateway-stripe' ) );
336
+		if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_nonce')) {
337
+			wp_die(__('Cheatin&#8217; huh?', 'woocommerce-gateway-stripe'));
338 338
 		}
339 339
 
340 340
 		$errors = new WP_Error();
341
-		parse_str( $_POST['required_fields'], $required_fields );
342
-		parse_str( $_POST['all_fields'], $all_fields );
343
-		$source_type = isset( $_POST['source_type'] ) ? wc_clean( $_POST['source_type'] ) : '';
341
+		parse_str($_POST['required_fields'], $required_fields);
342
+		parse_str($_POST['all_fields'], $all_fields);
343
+		$source_type = isset($_POST['source_type']) ? wc_clean($_POST['source_type']) : '';
344 344
 		$validate_shipping_fields = false;
345 345
 		$create_account = false;
346 346
 
347
-		array_walk_recursive( $required_fields, 'wc_clean' );
348
-		array_walk_recursive( $all_fields, 'wc_clean' );
347
+		array_walk_recursive($required_fields, 'wc_clean');
348
+		array_walk_recursive($all_fields, 'wc_clean');
349 349
 
350 350
 		// Remove unneeded required fields depending on source type.
351
-		if ( 'stripe_sepa' !== $source_type ) {
352
-			unset( $required_fields['stripe_sepa_owner'] );
353
-			unset( $required_fields['stripe_sepa_iban'] );
351
+		if ('stripe_sepa' !== $source_type) {
352
+			unset($required_fields['stripe_sepa_owner']);
353
+			unset($required_fields['stripe_sepa_iban']);
354 354
 		}
355 355
 
356
-		if ( 'stripe_sofort' !== $source_type ) {
357
-			unset( $required_fields['stripe_sofort_bank_country'] );
356
+		if ('stripe_sofort' !== $source_type) {
357
+			unset($required_fields['stripe_sofort_bank_country']);
358 358
 		}
359 359
 
360 360
 		/**
361 361
 		 * If ship to different address checkbox is checked then we need
362 362
 		 * to validate shipping fields too.
363 363
 		 */
364
-		if ( isset( $all_fields['ship_to_different_address'] ) ) {
364
+		if (isset($all_fields['ship_to_different_address'])) {
365 365
 			$validate_shipping_fields = true;
366 366
 		}
367 367
 
368 368
 		// Check if createaccount is checked.
369
-		if ( isset( $all_fields['createaccount'] ) ) {
369
+		if (isset($all_fields['createaccount'])) {
370 370
 			$create_account = true;
371 371
 		}
372 372
 
373 373
 		// Check if required fields are empty.
374
-		foreach ( $required_fields as $field => $field_value ) {
374
+		foreach ($required_fields as $field => $field_value) {
375 375
 			// Check for shipping field.
376
-			if ( preg_match( '/^shipping_/', $field ) && ! $validate_shipping_fields ) {
376
+			if (preg_match('/^shipping_/', $field) && ! $validate_shipping_fields) {
377 377
 				continue;
378 378
 			}
379 379
 
380 380
 			// Check create account name.
381
-			if ( 'account_username' === $field && ! $create_account ) {
381
+			if ('account_username' === $field && ! $create_account) {
382 382
 				continue;
383 383
 			}
384 384
 
385 385
 			// Check create account password.
386
-			if ( 'account_password' === $field && ! $create_account ) {
386
+			if ('account_password' === $field && ! $create_account) {
387 387
 				continue;
388 388
 			}
389 389
 
390 390
 			// Check if is SEPA.
391
-			if ( 'stripe_sepa' !== $source_type && 'stripe_sepa_owner' === $field ) {
391
+			if ('stripe_sepa' !== $source_type && 'stripe_sepa_owner' === $field) {
392 392
 				continue;
393 393
 			}
394 394
 
395
-			if ( 'stripe_sepa' !== $source_type && 'stripe_sepa_iban' === $field ) {
395
+			if ('stripe_sepa' !== $source_type && 'stripe_sepa_iban' === $field) {
396 396
 				$continue;
397 397
 			}
398 398
 
399
-			if ( empty( $field_value ) || '-1' === $field_value ) {
400
-				$error_field = $this->normalize_field( $field );
399
+			if (empty($field_value) || '-1' === $field_value) {
400
+				$error_field = $this->normalize_field($field);
401 401
 				/* translators: error field name */
402
-				$errors->add( 'validation', sprintf( __( '%s cannot be empty', 'woocommerce-gateway-stripe' ), $error_field ) );
402
+				$errors->add('validation', sprintf(__('%s cannot be empty', 'woocommerce-gateway-stripe'), $error_field));
403 403
 			}
404 404
 		}
405 405
 
406 406
 		// Check if email is valid format.
407
-		if ( ! empty( $required_fields['billing_email'] ) && ! is_email( $required_fields['billing_email'] ) ) {
408
-			$errors->add( 'validation', __( 'Email is not valid', 'woocommerce-gateway-stripe' ) );
407
+		if ( ! empty($required_fields['billing_email']) && ! is_email($required_fields['billing_email'])) {
408
+			$errors->add('validation', __('Email is not valid', 'woocommerce-gateway-stripe'));
409 409
 		}
410 410
 
411 411
 		// Check if phone number is valid format.
412
-		if ( ! empty( $required_fields['billing_phone'] ) ) {
413
-			$phone = wc_format_phone_number( $required_fields['billing_phone'] );
412
+		if ( ! empty($required_fields['billing_phone'])) {
413
+			$phone = wc_format_phone_number($required_fields['billing_phone']);
414 414
 
415
-			if ( '' !== $phone && ! WC_Validation::is_phone( $phone ) ) {
415
+			if ('' !== $phone && ! WC_Validation::is_phone($phone)) {
416 416
 				/* translators: %s: phone number */
417
-				$errors->add( 'validation', __( 'Please enter a valid phone number.', 'woocommerce-gateway-stripe' ) );
417
+				$errors->add('validation', __('Please enter a valid phone number.', 'woocommerce-gateway-stripe'));
418 418
 			}
419 419
 		}
420 420
 
421 421
 		// Check if postal code is valid format.
422
-		if ( ! empty( $required_fields['billing_postcode'] ) ) {
423
-			$country = isset( $required_fields['billing_country'] ) ? $required_fields['billing_country'] : WC()->customer->get_billing_country();
424
-			$postcode = wc_format_postcode( $required_fields['billing_postcode'], $country );
422
+		if ( ! empty($required_fields['billing_postcode'])) {
423
+			$country = isset($required_fields['billing_country']) ? $required_fields['billing_country'] : WC()->customer->get_billing_country();
424
+			$postcode = wc_format_postcode($required_fields['billing_postcode'], $country);
425 425
 
426
-			if ( '' !== $required_fields['billing_postcode'] && ! WC_Validation::is_postcode( $postcode, $country ) ) {
427
-				$errors->add( 'validation', __( 'Please enter a valid billing postcode / ZIP.', 'woocommerce-gateway-stripe' ) );
426
+			if ('' !== $required_fields['billing_postcode'] && ! WC_Validation::is_postcode($postcode, $country)) {
427
+				$errors->add('validation', __('Please enter a valid billing postcode / ZIP.', 'woocommerce-gateway-stripe'));
428 428
 			}
429 429
 		}
430 430
 
431 431
 		// Don't check this on add payment method page.
432
-		if ( ( isset( $_POST['is_add_payment_page'] ) && 'no' === $_POST['is_add_payment_page'] ) ) {
433
-			if ( empty( $all_fields['woocommerce_checkout_update_totals'] ) && empty( $all_fields['terms'] ) && apply_filters( 'woocommerce_checkout_show_terms', wc_get_page_id( 'terms' ) > 0 ) ) {
434
-				$errors->add( 'terms', __( 'You must accept our Terms &amp; Conditions.', 'woocommerce-gateway-stripe' ) );
432
+		if ((isset($_POST['is_add_payment_page']) && 'no' === $_POST['is_add_payment_page'])) {
433
+			if (empty($all_fields['woocommerce_checkout_update_totals']) && empty($all_fields['terms']) && apply_filters('woocommerce_checkout_show_terms', wc_get_page_id('terms') > 0)) {
434
+				$errors->add('terms', __('You must accept our Terms &amp; Conditions.', 'woocommerce-gateway-stripe'));
435 435
 			}
436 436
 		}
437 437
 
438
-		if ( WC()->cart->needs_shipping() && $validate_shipping_fields ) {
438
+		if (WC()->cart->needs_shipping() && $validate_shipping_fields) {
439 439
 			// Check if postal code is valid format.
440
-			if ( ! empty( $required_fields['shipping_postcode'] ) ) {
441
-				$country = isset( $required_fields['shipping_country'] ) ? $required_fields['shipping_country'] : WC()->customer->get_shipping_country();
442
-				$postcode = wc_format_postcode( $required_fields['shipping_postcode'], $country );
440
+			if ( ! empty($required_fields['shipping_postcode'])) {
441
+				$country = isset($required_fields['shipping_country']) ? $required_fields['shipping_country'] : WC()->customer->get_shipping_country();
442
+				$postcode = wc_format_postcode($required_fields['shipping_postcode'], $country);
443 443
 
444
-				if ( '' !== $required_fields['shipping_postcode'] && ! WC_Validation::is_postcode( $postcode, $country ) ) {
445
-					$errors->add( 'validation', __( 'Please enter a valid shipping postcode / ZIP.', 'woocommerce-gateway-stripe' ) );
444
+				if ('' !== $required_fields['shipping_postcode'] && ! WC_Validation::is_postcode($postcode, $country)) {
445
+					$errors->add('validation', __('Please enter a valid shipping postcode / ZIP.', 'woocommerce-gateway-stripe'));
446 446
 				}
447 447
 			}
448 448
 		}
449 449
 
450
-		if ( WC()->cart->needs_shipping() ) {
450
+		if (WC()->cart->needs_shipping()) {
451 451
 			$shipping_country = WC()->customer->get_shipping_country();
452 452
 
453
-			if ( empty( $shipping_country ) ) {
454
-				$errors->add( 'shipping', __( 'Please enter an address to continue.', 'woocommerce-gateway-stripe' ) );
455
-			} elseif ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) {
453
+			if (empty($shipping_country)) {
454
+				$errors->add('shipping', __('Please enter an address to continue.', 'woocommerce-gateway-stripe'));
455
+			} elseif ( ! in_array(WC()->customer->get_shipping_country(), array_keys(WC()->countries->get_shipping_countries()))) {
456 456
 				/* translators: country name */
457
-				$errors->add( 'shipping', sprintf( __( 'Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce-gateway-stripe' ), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country() ) );
457
+				$errors->add('shipping', sprintf(__('Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce-gateway-stripe'), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country()));
458 458
 			} else {
459
-				$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
459
+				$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
460 460
 
461
-				foreach ( WC()->shipping->get_packages() as $i => $package ) {
462
-					if ( ! isset( $chosen_shipping_methods[ $i ], $package['rates'][ $chosen_shipping_methods[ $i ] ] ) ) {
463
-						$errors->add( 'shipping', __( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce-gateway-stripe' ) );
461
+				foreach (WC()->shipping->get_packages() as $i => $package) {
462
+					if ( ! isset($chosen_shipping_methods[$i], $package['rates'][$chosen_shipping_methods[$i]])) {
463
+						$errors->add('shipping', __('No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce-gateway-stripe'));
464 464
 					}
465 465
 				}
466 466
 			}
467 467
 		}
468 468
 
469
-		if ( WC()->cart->needs_payment() ) {
469
+		if (WC()->cart->needs_payment()) {
470 470
 			$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
471 471
 
472
-			if ( ! isset( $available_gateways[ $all_fields['payment_method'] ] ) ) {
473
-				$errors->add( 'payment', __( 'Invalid payment method.', 'woocommerce-gateway-stripe' ) );
472
+			if ( ! isset($available_gateways[$all_fields['payment_method']])) {
473
+				$errors->add('payment', __('Invalid payment method.', 'woocommerce-gateway-stripe'));
474 474
 			} else {
475
-				$available_gateways[ $all_fields['payment_method'] ]->validate_fields();
475
+				$available_gateways[$all_fields['payment_method']]->validate_fields();
476 476
 			}
477 477
 		}
478 478
 
479
-		if ( 0 === count( $errors->errors ) ) {
480
-			wp_send_json( 'success' );
479
+		if (0 === count($errors->errors)) {
480
+			wp_send_json('success');
481 481
 		} else {
482
-			foreach ( $errors->get_error_messages() as $message ) {
483
-				wc_add_notice( $message, 'error' );
482
+			foreach ($errors->get_error_messages() as $message) {
483
+				wc_add_notice($message, 'error');
484 484
 			}
485 485
 
486 486
 			$this->send_ajax_failure_response();
@@ -494,9 +494,9 @@  discard block
 block discarded – undo
494 494
 	 * @version 4.0.0
495 495
 	 */
496 496
 	public function send_ajax_failure_response() {
497
-		if ( is_ajax() ) {
497
+		if (is_ajax()) {
498 498
 			// only print notices if not reloading the checkout, otherwise they're lost in the page reload.
499
-			if ( ! isset( WC()->session->reload_checkout ) ) {
499
+			if ( ! isset(WC()->session->reload_checkout)) {
500 500
 				ob_start();
501 501
 				wc_print_notices();
502 502
 				$messages = ob_get_clean();
@@ -504,14 +504,14 @@  discard block
 block discarded – undo
504 504
 
505 505
 			$response = array(
506 506
 				'result'   => 'failure',
507
-				'messages' => isset( $messages ) ? $messages : '',
508
-				'refresh'  => isset( WC()->session->refresh_totals ),
509
-				'reload'   => isset( WC()->session->reload_checkout ),
507
+				'messages' => isset($messages) ? $messages : '',
508
+				'refresh'  => isset(WC()->session->refresh_totals),
509
+				'reload'   => isset(WC()->session->reload_checkout),
510 510
 			);
511 511
 
512
-			unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
512
+			unset(WC()->session->refresh_totals, WC()->session->reload_checkout);
513 513
 
514
-			wp_send_json( $response );
514
+			wp_send_json($response);
515 515
 		}
516 516
 	}
517 517
 }
Please login to merge, or discard this patch.