Completed
Push — master ( a728c7...6e2c2f )
by Roy
02:12
created
includes/class-wc-stripe-order-handler.php 1 patch
Spacing   +175 added lines, -175 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 WC_Stripe_Exception( print_r( $source_info, true ), $source_info->error->message );
91
+			if ( ! empty($source_info->error)) {
92
+				throw new WC_Stripe_Exception(print_r($source_info, true), $source_info->error->message);
93 93
 			}
94 94
 
95
-			if ( 'failed' === $source_info->status || 'canceled' === $source_info->status ) {
96
-				throw new WC_Stripe_Exception( print_r( $source_info, true ), __( '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 WC_Stripe_Exception(print_r($source_info, true), __('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 WC_Stripe_Exception( print_r( $response, true ), $message );
120
+						$order->add_order_note($message);
121
+						throw new WC_Stripe_Exception(print_r($response, true), $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 WC_Stripe_Exception( print_r( $response, true ), $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 WC_Stripe_Exception(print_r($response, true), $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 WC_Stripe_Exception( print_r( $response, true ), $message );
149
+				throw new WC_Stripe_Exception(print_r($response, true), $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 ( WC_Stripe_Exception $e ) {
157
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
156
+		} catch (WC_Stripe_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->getLocalizedMessage() ) );
162
+			$order->update_status('failed', sprintf(__('Stripe payment failed: %s', 'woocommerce-gateway-stripe'), $e->getLocalizedMessage()));
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->getLocalizedMessage(), 'error' );
169
-			wp_safe_redirect( wc_get_checkout_url() );
168
+			wc_add_notice($e->getLocalizedMessage(), '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 = __( 'Billing', 'woocommerce-gateway-stripe' ) . ' ' . $checkout_fields['billing'][ $field ]['label'];
299
-		} elseif ( array_key_exists( $field, $checkout_fields['shipping'] ) ) {
300
-			$error_field = __( 'Shipping', 'woocommerce-gateway-stripe' ) . ' ' . $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 = __('Billing', 'woocommerce-gateway-stripe') . ' ' . $checkout_fields['billing'][$field]['label'];
299
+		} elseif (array_key_exists($field, $checkout_fields['shipping'])) {
300
+			$error_field = __('Shipping', 'woocommerce-gateway-stripe') . ' ' . $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,157 +333,157 @@  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
-		$all_fields      = apply_filters( 'wc_stripe_validate_checkout_all_fields', $all_fields );
348
-		$required_fields = apply_filters( 'wc_stripe_validate_checkout_required_fields', $required_fields );
347
+		$all_fields      = apply_filters('wc_stripe_validate_checkout_all_fields', $all_fields);
348
+		$required_fields = apply_filters('wc_stripe_validate_checkout_required_fields', $required_fields);
349 349
 
350
-		array_walk_recursive( $required_fields, 'wc_clean' );
351
-		array_walk_recursive( $all_fields, 'wc_clean' );
350
+		array_walk_recursive($required_fields, 'wc_clean');
351
+		array_walk_recursive($all_fields, 'wc_clean');
352 352
 
353 353
 		// Remove unneeded required fields depending on source type.
354
-		if ( 'stripe_sepa' !== $source_type ) {
355
-			unset( $required_fields['stripe_sepa_owner'] );
356
-			unset( $required_fields['stripe_sepa_iban'] );
354
+		if ('stripe_sepa' !== $source_type) {
355
+			unset($required_fields['stripe_sepa_owner']);
356
+			unset($required_fields['stripe_sepa_iban']);
357 357
 		}
358 358
 
359
-		if ( 'stripe_sofort' !== $source_type ) {
360
-			unset( $required_fields['stripe_sofort_bank_country'] );
359
+		if ('stripe_sofort' !== $source_type) {
360
+			unset($required_fields['stripe_sofort_bank_country']);
361 361
 		}
362 362
 
363 363
 		/**
364 364
 		 * If ship to different address checkbox is checked then we need
365 365
 		 * to validate shipping fields too.
366 366
 		 */
367
-		if ( isset( $all_fields['ship_to_different_address'] ) ) {
367
+		if (isset($all_fields['ship_to_different_address'])) {
368 368
 			$validate_shipping_fields = true;
369 369
 		}
370 370
 
371 371
 		// Check if createaccount is checked.
372
-		if ( isset( $all_fields['createaccount'] ) ) {
372
+		if (isset($all_fields['createaccount'])) {
373 373
 			$create_account = true;
374 374
 		}
375 375
 
376 376
 		// Check if required fields are empty.
377
-		foreach ( $required_fields as $field => $field_value ) {
377
+		foreach ($required_fields as $field => $field_value) {
378 378
 			// Check for shipping field.
379
-			if ( preg_match( '/^shipping_/', $field ) && ! $validate_shipping_fields ) {
379
+			if (preg_match('/^shipping_/', $field) && ! $validate_shipping_fields) {
380 380
 				continue;
381 381
 			}
382 382
 
383 383
 			// Check create account name.
384
-			if ( 'account_username' === $field && ! $create_account ) {
384
+			if ('account_username' === $field && ! $create_account) {
385 385
 				continue;
386 386
 			}
387 387
 
388 388
 			// Check create account password.
389
-			if ( 'account_password' === $field && ! $create_account ) {
389
+			if ('account_password' === $field && ! $create_account) {
390 390
 				continue;
391 391
 			}
392 392
 
393 393
 			// Check if is SEPA.
394
-			if ( 'stripe_sepa' !== $source_type && 'stripe_sepa_owner' === $field ) {
394
+			if ('stripe_sepa' !== $source_type && 'stripe_sepa_owner' === $field) {
395 395
 				continue;
396 396
 			}
397 397
 
398
-			if ( 'stripe_sepa' !== $source_type && 'stripe_sepa_iban' === $field ) {
398
+			if ('stripe_sepa' !== $source_type && 'stripe_sepa_iban' === $field) {
399 399
 				$continue;
400 400
 			}
401 401
 
402
-			if ( empty( $field_value ) || '-1' === $field_value ) {
403
-				$error_field = $this->normalize_field( $field );
402
+			if (empty($field_value) || '-1' === $field_value) {
403
+				$error_field = $this->normalize_field($field);
404 404
 				/* translators: error field name */
405
-				$errors->add( 'validation', sprintf( __( '<strong>%s</strong> cannot be empty', 'woocommerce-gateway-stripe' ), $error_field ) );
405
+				$errors->add('validation', sprintf(__('<strong>%s</strong> cannot be empty', 'woocommerce-gateway-stripe'), $error_field));
406 406
 			}
407 407
 		}
408 408
 
409 409
 		// Check if email is valid format.
410
-		if ( ! empty( $required_fields['billing_email'] ) && ! is_email( $required_fields['billing_email'] ) ) {
411
-			$errors->add( 'validation', __( 'Email is not valid', 'woocommerce-gateway-stripe' ) );
410
+		if ( ! empty($required_fields['billing_email']) && ! is_email($required_fields['billing_email'])) {
411
+			$errors->add('validation', __('Email is not valid', 'woocommerce-gateway-stripe'));
412 412
 		}
413 413
 
414 414
 		// Check if phone number is valid format.
415
-		if ( ! empty( $required_fields['billing_phone'] ) ) {
416
-			$phone = wc_format_phone_number( $required_fields['billing_phone'] );
415
+		if ( ! empty($required_fields['billing_phone'])) {
416
+			$phone = wc_format_phone_number($required_fields['billing_phone']);
417 417
 
418
-			if ( '' !== $phone && ! WC_Validation::is_phone( $phone ) ) {
418
+			if ('' !== $phone && ! WC_Validation::is_phone($phone)) {
419 419
 				/* translators: %s: phone number */
420
-				$errors->add( 'validation', __( 'Please enter a valid phone number.', 'woocommerce-gateway-stripe' ) );
420
+				$errors->add('validation', __('Please enter a valid phone number.', 'woocommerce-gateway-stripe'));
421 421
 			}
422 422
 		}
423 423
 
424 424
 		// Check if postal code is valid format.
425
-		if ( ! empty( $required_fields['billing_postcode'] ) ) {
426
-			$country = isset( $required_fields['billing_country'] ) ? $required_fields['billing_country'] : WC()->customer->get_billing_country();
427
-			$postcode = wc_format_postcode( $required_fields['billing_postcode'], $country );
425
+		if ( ! empty($required_fields['billing_postcode'])) {
426
+			$country = isset($required_fields['billing_country']) ? $required_fields['billing_country'] : WC()->customer->get_billing_country();
427
+			$postcode = wc_format_postcode($required_fields['billing_postcode'], $country);
428 428
 
429
-			if ( '' !== $required_fields['billing_postcode'] && ! WC_Validation::is_postcode( $postcode, $country ) ) {
430
-				$errors->add( 'validation', __( 'Please enter a valid billing postcode / ZIP.', 'woocommerce-gateway-stripe' ) );
429
+			if ('' !== $required_fields['billing_postcode'] && ! WC_Validation::is_postcode($postcode, $country)) {
430
+				$errors->add('validation', __('Please enter a valid billing postcode / ZIP.', 'woocommerce-gateway-stripe'));
431 431
 			}
432 432
 		}
433 433
 
434 434
 		// Don't check this on add payment method page.
435
-		if ( ( isset( $_POST['is_add_payment_page'] ) && 'no' === $_POST['is_add_payment_page'] ) ) {
436
-			if ( empty( $all_fields['woocommerce_checkout_update_totals'] ) && empty( $all_fields['terms'] ) && apply_filters( 'woocommerce_checkout_show_terms', wc_get_page_id( 'terms' ) > 0 ) ) {
437
-				$errors->add( 'terms', __( 'You must accept our Terms &amp; Conditions.', 'woocommerce-gateway-stripe' ) );
435
+		if ((isset($_POST['is_add_payment_page']) && 'no' === $_POST['is_add_payment_page'])) {
436
+			if (empty($all_fields['woocommerce_checkout_update_totals']) && empty($all_fields['terms']) && apply_filters('woocommerce_checkout_show_terms', wc_get_page_id('terms') > 0)) {
437
+				$errors->add('terms', __('You must accept our Terms &amp; Conditions.', 'woocommerce-gateway-stripe'));
438 438
 			}
439 439
 		}
440 440
 
441
-		if ( WC()->cart->needs_shipping() && $validate_shipping_fields ) {
441
+		if (WC()->cart->needs_shipping() && $validate_shipping_fields) {
442 442
 			// Check if postal code is valid format.
443
-			if ( ! empty( $required_fields['shipping_postcode'] ) ) {
444
-				$country = isset( $required_fields['shipping_country'] ) ? $required_fields['shipping_country'] : WC()->customer->get_shipping_country();
445
-				$postcode = wc_format_postcode( $required_fields['shipping_postcode'], $country );
443
+			if ( ! empty($required_fields['shipping_postcode'])) {
444
+				$country = isset($required_fields['shipping_country']) ? $required_fields['shipping_country'] : WC()->customer->get_shipping_country();
445
+				$postcode = wc_format_postcode($required_fields['shipping_postcode'], $country);
446 446
 
447
-				if ( '' !== $required_fields['shipping_postcode'] && ! WC_Validation::is_postcode( $postcode, $country ) ) {
448
-					$errors->add( 'validation', __( 'Please enter a valid shipping postcode / ZIP.', 'woocommerce-gateway-stripe' ) );
447
+				if ('' !== $required_fields['shipping_postcode'] && ! WC_Validation::is_postcode($postcode, $country)) {
448
+					$errors->add('validation', __('Please enter a valid shipping postcode / ZIP.', 'woocommerce-gateway-stripe'));
449 449
 				}
450 450
 			}
451 451
 		}
452 452
 
453
-		if ( WC()->cart->needs_shipping() ) {
453
+		if (WC()->cart->needs_shipping()) {
454 454
 			$shipping_country = WC()->customer->get_shipping_country();
455 455
 
456
-			if ( empty( $shipping_country ) ) {
457
-				$errors->add( 'shipping', __( 'Please enter an address to continue.', 'woocommerce-gateway-stripe' ) );
458
-			} elseif ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) {
456
+			if (empty($shipping_country)) {
457
+				$errors->add('shipping', __('Please enter an address to continue.', 'woocommerce-gateway-stripe'));
458
+			} elseif ( ! in_array(WC()->customer->get_shipping_country(), array_keys(WC()->countries->get_shipping_countries()))) {
459 459
 				/* translators: country name */
460
-				$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() ) );
460
+				$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()));
461 461
 			} else {
462
-				$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
462
+				$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
463 463
 
464
-				foreach ( WC()->shipping->get_packages() as $i => $package ) {
465
-					if ( ! isset( $chosen_shipping_methods[ $i ], $package['rates'][ $chosen_shipping_methods[ $i ] ] ) ) {
466
-						$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
+				foreach (WC()->shipping->get_packages() as $i => $package) {
465
+					if ( ! isset($chosen_shipping_methods[$i], $package['rates'][$chosen_shipping_methods[$i]])) {
466
+						$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'));
467 467
 					}
468 468
 				}
469 469
 			}
470 470
 		}
471 471
 
472
-		if ( WC()->cart->needs_payment() ) {
472
+		if (WC()->cart->needs_payment()) {
473 473
 			$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
474 474
 
475
-			if ( ! isset( $available_gateways[ $all_fields['payment_method'] ] ) ) {
476
-				$errors->add( 'payment', __( 'Invalid payment method.', 'woocommerce-gateway-stripe' ) );
475
+			if ( ! isset($available_gateways[$all_fields['payment_method']])) {
476
+				$errors->add('payment', __('Invalid payment method.', 'woocommerce-gateway-stripe'));
477 477
 			} else {
478
-				$available_gateways[ $all_fields['payment_method'] ]->validate_fields();
478
+				$available_gateways[$all_fields['payment_method']]->validate_fields();
479 479
 			}
480 480
 		}
481 481
 
482
-		if ( 0 === count( $errors->errors ) ) {
483
-			wp_send_json( 'success' );
482
+		if (0 === count($errors->errors)) {
483
+			wp_send_json('success');
484 484
 		} else {
485
-			foreach ( $errors->get_error_messages() as $message ) {
486
-				wc_add_notice( $message, 'error' );
485
+			foreach ($errors->get_error_messages() as $message) {
486
+				wc_add_notice($message, 'error');
487 487
 			}
488 488
 
489 489
 			$this->send_ajax_failure_response();
@@ -497,9 +497,9 @@  discard block
 block discarded – undo
497 497
 	 * @version 4.0.0
498 498
 	 */
499 499
 	public function send_ajax_failure_response() {
500
-		if ( is_ajax() ) {
500
+		if (is_ajax()) {
501 501
 			// only print notices if not reloading the checkout, otherwise they're lost in the page reload.
502
-			if ( ! isset( WC()->session->reload_checkout ) ) {
502
+			if ( ! isset(WC()->session->reload_checkout)) {
503 503
 				ob_start();
504 504
 				wc_print_notices();
505 505
 				$messages = ob_get_clean();
@@ -507,14 +507,14 @@  discard block
 block discarded – undo
507 507
 
508 508
 			$response = array(
509 509
 				'result'   => 'failure',
510
-				'messages' => isset( $messages ) ? $messages : '',
511
-				'refresh'  => isset( WC()->session->refresh_totals ),
512
-				'reload'   => isset( WC()->session->reload_checkout ),
510
+				'messages' => isset($messages) ? $messages : '',
511
+				'refresh'  => isset(WC()->session->refresh_totals),
512
+				'reload'   => isset(WC()->session->reload_checkout),
513 513
 			);
514 514
 
515
-			unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
515
+			unset(WC()->session->refresh_totals, WC()->session->reload_checkout);
516 516
 
517
-			wp_send_json( $response );
517
+			wp_send_json($response);
518 518
 		}
519 519
 	}
520 520
 }
Please login to merge, or discard this patch.