Completed
Pull Request — master (#535)
by Roy
02:21
created
includes/class-wc-stripe-webhook-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,7 +20,7 @@  discard block
 block discarded – undo
20 20
 	 */
21 21
 	public function __construct() {
22 22
 		$this->retry_interval = 2;
23
-		add_action( 'woocommerce_api_wc_stripe', array( $this, 'check_for_webhook' ) );
23
+		add_action('woocommerce_api_wc_stripe', array($this, 'check_for_webhook'));
24 24
 	}
25 25
 
26 26
 	/**
@@ -30,24 +30,24 @@  discard block
 block discarded – undo
30 30
 	 * @version 4.0.0
31 31
 	 */
32 32
 	public function check_for_webhook() {
33
-		if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] )
34
-			|| ! isset( $_GET['wc-api'] )
35
-			|| ( 'wc_stripe' !== $_GET['wc-api'] )
33
+		if (('POST' !== $_SERVER['REQUEST_METHOD'])
34
+			|| ! isset($_GET['wc-api'])
35
+			|| ('wc_stripe' !== $_GET['wc-api'])
36 36
 		) {
37 37
 			return;
38 38
 		}
39 39
 
40
-		$request_body    = file_get_contents( 'php://input' );
41
-		$request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER );
40
+		$request_body    = file_get_contents('php://input');
41
+		$request_headers = array_change_key_case($this->get_request_headers(), CASE_UPPER);
42 42
 
43 43
 		// Validate it to make sure it is legit.
44
-		if ( $this->is_valid_request( $request_headers, $request_body ) ) {
45
-			$this->process_webhook( $request_body );
46
-			status_header( 200 );
44
+		if ($this->is_valid_request($request_headers, $request_body)) {
45
+			$this->process_webhook($request_body);
46
+			status_header(200);
47 47
 			exit;
48 48
 		} else {
49
-			WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) );
50
-			status_header( 400 );
49
+			WC_Stripe_Logger::log('Incoming webhook failed validation: ' . print_r($request_body, true));
50
+			status_header(400);
51 51
 			exit;
52 52
 		}
53 53
 	}
@@ -62,12 +62,12 @@  discard block
 block discarded – undo
62 62
 	 * @param string $request_body The request body from Stripe.
63 63
 	 * @return bool
64 64
 	 */
65
-	public function is_valid_request( $request_headers = null, $request_body = null ) {
66
-		if ( null === $request_headers || null === $request_body ) {
65
+	public function is_valid_request($request_headers = null, $request_body = null) {
66
+		if (null === $request_headers || null === $request_body) {
67 67
 			return false;
68 68
 		}
69 69
 
70
-		if ( ! empty( $request_headers['USER-AGENT'] ) && ! preg_match( '/Stripe/', $request_headers['USER-AGENT'] ) ) {
70
+		if ( ! empty($request_headers['USER-AGENT']) && ! preg_match('/Stripe/', $request_headers['USER-AGENT'])) {
71 71
 			return false;
72 72
 		}
73 73
 
@@ -83,11 +83,11 @@  discard block
 block discarded – undo
83 83
 	 * @version 4.0.0
84 84
 	 */
85 85
 	public function get_request_headers() {
86
-		if ( ! function_exists( 'getallheaders' ) ) {
86
+		if ( ! function_exists('getallheaders')) {
87 87
 			$headers = [];
88
-			foreach ( $_SERVER as $name => $value ) {
89
-				if ( 'HTTP_' === substr( $name, 0, 5 ) ) {
90
-					$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value;
88
+			foreach ($_SERVER as $name => $value) {
89
+				if ('HTTP_' === substr($name, 0, 5)) {
90
+					$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
91 91
 				}
92 92
 			}
93 93
 
@@ -106,30 +106,30 @@  discard block
 block discarded – undo
106 106
 	 * @param object $notification
107 107
 	 * @param bool $retry
108 108
 	 */
109
-	public function process_webhook_payment( $notification, $retry = true ) {
109
+	public function process_webhook_payment($notification, $retry = true) {
110 110
 		// The following 2 payment methods are synchronous so does not need to be handle via webhook.
111
-		if ( 'card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type ) {
111
+		if ('card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type) {
112 112
 			return;
113 113
 		}
114 114
 
115
-		$order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id );
115
+		$order = WC_Stripe_Helper::get_order_by_source_id($notification->data->object->id);
116 116
 
117
-		if ( ! $order ) {
118
-			WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id );
117
+		if ( ! $order) {
118
+			WC_Stripe_Logger::log('Could not find order via source ID: ' . $notification->data->object->id);
119 119
 			return;
120 120
 		}
121 121
 
122 122
 		$order_id  = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
123 123
 		$source_id = $notification->data->object->id;
124 124
 
125
-		$is_pending_receiver = ( 'receiver' === $notification->data->object->flow );
125
+		$is_pending_receiver = ('receiver' === $notification->data->object->flow);
126 126
 
127 127
 		try {
128
-			if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() ) {
128
+			if ('processing' === $order->get_status() || 'completed' === $order->get_status()) {
129 129
 				return;
130 130
 			}
131 131
 
132
-			if ( 'on-hold' === $order->get_status() && ! $is_pending_receiver ) {
132
+			if ('on-hold' === $order->get_status() && ! $is_pending_receiver) {
133 133
 				return;
134 134
 			}
135 135
 
@@ -137,100 +137,100 @@  discard block
 block discarded – undo
137 137
 			$response = null;
138 138
 
139 139
 			// This will throw exception if not valid.
140
-			$this->validate_minimum_order_amount( $order );
140
+			$this->validate_minimum_order_amount($order);
141 141
 
142
-			WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
142
+			WC_Stripe_Logger::log("Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}");
143 143
 
144 144
 			// Prep source object.
145 145
 			$source_object           = new stdClass();
146 146
 			$source_object->token_id = '';
147
-			$source_object->customer = $this->get_stripe_customer_id( $order );
147
+			$source_object->customer = $this->get_stripe_customer_id($order);
148 148
 			$source_object->source   = $source_id;
149 149
 
150 150
 			// Make the request.
151
-			$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) );
151
+			$response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object));
152 152
 
153
-			if ( ! empty( $response->error ) ) {
153
+			if ( ! empty($response->error)) {
154 154
 				// If it is an API error such connection or server, let's retry.
155
-				if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) {
156
-					if ( $retry ) {
157
-						sleep( 5 );
158
-						return $this->process_webhook_payment( $notification, false );
155
+				if ('api_connection_error' === $response->error->type || 'api_error' === $response->error->type) {
156
+					if ($retry) {
157
+						sleep(5);
158
+						return $this->process_webhook_payment($notification, false);
159 159
 					} else {
160 160
 						$localized_message = 'API connection error and retries exhausted.';
161
-						$order->add_order_note( $localized_message );
162
-						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
161
+						$order->add_order_note($localized_message);
162
+						throw new WC_Stripe_Exception(print_r($response, true), $localized_message);
163 163
 					}
164 164
 				}
165 165
 
166 166
 				// We want to retry.
167
-				if ( $this->is_retryable_error( $response->error ) ) {
168
-					if ( $retry ) {
167
+				if ($this->is_retryable_error($response->error)) {
168
+					if ($retry) {
169 169
 						// Don't do anymore retries after this.
170
-						if ( 5 <= $this->retry_interval ) {
170
+						if (5 <= $this->retry_interval) {
171 171
 
172
-							return $this->process_webhook_payment( $notification, false );
172
+							return $this->process_webhook_payment($notification, false);
173 173
 						}
174 174
 
175
-						sleep( $this->retry_interval );
175
+						sleep($this->retry_interval);
176 176
 
177 177
 						$this->retry_interval++;
178
-						return $this->process_webhook_payment( $notification, true );
178
+						return $this->process_webhook_payment($notification, true);
179 179
 					} else {
180
-						$localized_message = __( 'On going requests error and retries exhausted.', 'woocommerce-gateway-stripe' );
181
-						$order->add_order_note( $localized_message );
182
-						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
180
+						$localized_message = __('On going requests error and retries exhausted.', 'woocommerce-gateway-stripe');
181
+						$order->add_order_note($localized_message);
182
+						throw new WC_Stripe_Exception(print_r($response, true), $localized_message);
183 183
 					}
184 184
 				}
185 185
 
186 186
 				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
187
-				if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) {
188
-					if ( WC_Stripe_Helper::is_pre_30() ) {
189
-						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
190
-						delete_post_meta( $order_id, '_stripe_customer_id' );
187
+				if (preg_match('/No such customer/i', $response->error->message) && $retry) {
188
+					if (WC_Stripe_Helper::is_pre_30()) {
189
+						delete_user_meta($order->customer_user, '_stripe_customer_id');
190
+						delete_post_meta($order_id, '_stripe_customer_id');
191 191
 					} else {
192
-						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
193
-						$order->delete_meta_data( '_stripe_customer_id' );
192
+						delete_user_meta($order->get_customer_id(), '_stripe_customer_id');
193
+						$order->delete_meta_data('_stripe_customer_id');
194 194
 						$order->save();
195 195
 					}
196 196
 
197
-					return $this->process_webhook_payment( $notification, false );
197
+					return $this->process_webhook_payment($notification, false);
198 198
 
199
-				} elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) {
199
+				} elseif (preg_match('/No such token/i', $response->error->message) && $source_object->token_id) {
200 200
 					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
201
-					$wc_token = WC_Payment_Tokens::get( $source_object->token_id );
201
+					$wc_token = WC_Payment_Tokens::get($source_object->token_id);
202 202
 					$wc_token->delete();
203
-					$message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
204
-					$order->add_order_note( $message );
205
-					throw new WC_Stripe_Exception( print_r( $response, true ), $message );
203
+					$message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe');
204
+					$order->add_order_note($message);
205
+					throw new WC_Stripe_Exception(print_r($response, true), $message);
206 206
 				}
207 207
 
208 208
 				$localized_messages = WC_Stripe_Helper::get_localized_messages();
209 209
 
210
-				if ( 'card_error' === $response->error->type ) {
211
-					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
210
+				if ('card_error' === $response->error->type) {
211
+					$localized_message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message;
212 212
 				} else {
213
-					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
213
+					$localized_message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message;
214 214
 				}
215 215
 
216
-				$order->add_order_note( $localized_message );
216
+				$order->add_order_note($localized_message);
217 217
 
218
-				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
218
+				throw new WC_Stripe_Exception(print_r($response, true), $localized_message);
219 219
 			}
220 220
 
221
-			do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order );
221
+			do_action('wc_gateway_stripe_process_webhook_payment', $response, $order);
222 222
 
223
-			$this->process_response( $response, $order );
223
+			$this->process_response($response, $order);
224 224
 
225
-		} catch ( WC_Stripe_Exception $e ) {
226
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
225
+		} catch (WC_Stripe_Exception $e) {
226
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
227 227
 
228
-			do_action( 'wc_gateway_stripe_process_webhook_payment_error', $e, $order );
228
+			do_action('wc_gateway_stripe_process_webhook_payment_error', $e, $order);
229 229
 
230
-			$statuses = array( 'pending', 'failed' );
230
+			$statuses = array('pending', 'failed');
231 231
 
232
-			if ( $order->has_status( $statuses ) ) {
233
-				$this->send_failed_order_email( $order_id );
232
+			if ($order->has_status($statuses)) {
233
+				$this->send_failed_order_email($order_id);
234 234
 			}
235 235
 		}
236 236
 	}
@@ -243,20 +243,20 @@  discard block
 block discarded – undo
243 243
 	 * @since 4.0.0
244 244
 	 * @param object $notification
245 245
 	 */
246
-	public function process_webhook_dispute( $notification ) {
247
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge );
246
+	public function process_webhook_dispute($notification) {
247
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge);
248 248
 
249
-		if ( ! $order ) {
250
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge );
249
+		if ( ! $order) {
250
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge);
251 251
 			return;
252 252
 		}
253 253
 
254
-		$order->update_status( 'on-hold', __( 'A dispute was created for this order. Response is needed. Please go to your Stripe Dashboard to review this dispute.', 'woocommerce-gateway-stripe' ) );
254
+		$order->update_status('on-hold', __('A dispute was created for this order. Response is needed. Please go to your Stripe Dashboard to review this dispute.', 'woocommerce-gateway-stripe'));
255 255
 
256
-		do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification );
256
+		do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification);
257 257
 
258 258
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
259
-		$this->send_failed_order_email( $order_id );
259
+		$this->send_failed_order_email($order_id);
260 260
 	}
261 261
 
262 262
 	/**
@@ -267,41 +267,41 @@  discard block
 block discarded – undo
267 267
 	 * @version 4.0.0
268 268
 	 * @param object $notification
269 269
 	 */
270
-	public function process_webhook_capture( $notification ) {
271
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
270
+	public function process_webhook_capture($notification) {
271
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
272 272
 
273
-		if ( ! $order ) {
274
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
273
+		if ( ! $order) {
274
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
275 275
 			return;
276 276
 		}
277 277
 
278 278
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
279 279
 
280
-		if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) {
281
-			$charge   = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
282
-			$captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true );
280
+		if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) {
281
+			$charge   = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
282
+			$captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true);
283 283
 
284
-			if ( $charge && 'no' === $captured ) {
285
-				WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' );
284
+			if ($charge && 'no' === $captured) {
285
+				WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', 'yes') : $order->update_meta_data('_stripe_charge_captured', 'yes');
286 286
 
287 287
 				// Store other data such as fees
288
-				WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $notification->data->object->id ) : $order->set_transaction_id( $notification->data->object->id );
288
+				WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $notification->data->object->id) : $order->set_transaction_id($notification->data->object->id);
289 289
 
290
-				if ( isset( $notification->data->object->balance_transaction ) ) {
291
-					$this->update_fees( $order, $notification->data->object->balance_transaction );
290
+				if (isset($notification->data->object->balance_transaction)) {
291
+					$this->update_fees($order, $notification->data->object->balance_transaction);
292 292
 				}
293 293
 
294
-				if ( is_callable( array( $order, 'save' ) ) ) {
294
+				if (is_callable(array($order, 'save'))) {
295 295
 					$order->save();
296 296
 				}
297 297
 
298 298
 				/* translators: transaction id */
299
-				$order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) );
299
+				$order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id));
300 300
 
301 301
 				// Check and see if capture is partial.
302
-				if ( $this->is_partial_capture( $notification ) ) {
303
-					$order->set_total( $this->get_partial_amount_to_charge( $notification ) );
304
-					$order->add_note( __( 'This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe' ) );
302
+				if ($this->is_partial_capture($notification)) {
303
+					$order->set_total($this->get_partial_amount_to_charge($notification));
304
+					$order->add_note(__('This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe'));
305 305
 					$order->save();
306 306
 				}
307 307
 			}
@@ -316,38 +316,38 @@  discard block
 block discarded – undo
316 316
 	 * @version 4.0.0
317 317
 	 * @param object $notification
318 318
 	 */
319
-	public function process_webhook_charge_succeeded( $notification ) {
319
+	public function process_webhook_charge_succeeded($notification) {
320 320
 		// The following payment methods are synchronous so does not need to be handle via webhook.
321
-		if ( ( isset( $notification->data->object->source->type ) && 'card' === $notification->data->object->source->type ) || ( isset( $notification->data->object->source->type ) && 'three_d_secure' === $notification->data->object->source->type ) ) {
321
+		if ((isset($notification->data->object->source->type) && 'card' === $notification->data->object->source->type) || (isset($notification->data->object->source->type) && 'three_d_secure' === $notification->data->object->source->type)) {
322 322
 			return;
323 323
 		}
324 324
 
325
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
325
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
326 326
 
327
-		if ( ! $order ) {
328
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
327
+		if ( ! $order) {
328
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
329 329
 			return;
330 330
 		}
331 331
 
332 332
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
333 333
 
334
-		if ( 'on-hold' !== $order->get_status() ) {
334
+		if ('on-hold' !== $order->get_status()) {
335 335
 			return;
336 336
 		}
337 337
 
338 338
 		// Store other data such as fees
339
-		WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $notification->data->object->id ) : $order->set_transaction_id( $notification->data->object->id );
339
+		WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $notification->data->object->id) : $order->set_transaction_id($notification->data->object->id);
340 340
 
341
-		if ( isset( $notification->data->object->balance_transaction ) ) {
342
-			$this->update_fees( $order, $notification->data->object->balance_transaction );
341
+		if (isset($notification->data->object->balance_transaction)) {
342
+			$this->update_fees($order, $notification->data->object->balance_transaction);
343 343
 		}
344 344
 
345
-		if ( is_callable( array( $order, 'save' ) ) ) {
345
+		if (is_callable(array($order, 'save'))) {
346 346
 			$order->save();
347 347
 		}
348 348
 
349 349
 		/* translators: transaction id */
350
-		$order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) );
350
+		$order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id));
351 351
 	}
352 352
 
353 353
 	/**
@@ -358,23 +358,23 @@  discard block
 block discarded – undo
358 358
 	 * @version 4.0.0
359 359
 	 * @param object $notification
360 360
 	 */
361
-	public function process_webhook_charge_failed( $notification ) {
362
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
361
+	public function process_webhook_charge_failed($notification) {
362
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
363 363
 
364
-		if ( ! $order ) {
365
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
364
+		if ( ! $order) {
365
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
366 366
 			return;
367 367
 		}
368 368
 
369 369
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
370 370
 
371
-		if ( 'on-hold' !== $order->get_status() ) {
371
+		if ('on-hold' !== $order->get_status()) {
372 372
 			return;
373 373
 		}
374 374
 
375
-		$order->update_status( 'failed', __( 'This payment failed to clear.', 'woocommerce-gateway-stripe' ) );
375
+		$order->update_status('failed', __('This payment failed to clear.', 'woocommerce-gateway-stripe'));
376 376
 
377
-		do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification );
377
+		do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification);
378 378
 	}
379 379
 
380 380
 	/**
@@ -385,23 +385,23 @@  discard block
 block discarded – undo
385 385
 	 * @version 4.0.0
386 386
 	 * @param object $notification
387 387
 	 */
388
-	public function process_webhook_source_canceled( $notification ) {
389
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
388
+	public function process_webhook_source_canceled($notification) {
389
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
390 390
 
391
-		if ( ! $order ) {
392
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
391
+		if ( ! $order) {
392
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
393 393
 			return;
394 394
 		}
395 395
 
396 396
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
397 397
 
398
-		if ( 'on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status() ) {
398
+		if ('on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status()) {
399 399
 			return;
400 400
 		}
401 401
 
402
-		$order->update_status( 'cancelled', __( 'This payment has cancelled.', 'woocommerce-gateway-stripe' ) );
402
+		$order->update_status('cancelled', __('This payment has cancelled.', 'woocommerce-gateway-stripe'));
403 403
 
404
-		do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification );
404
+		do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification);
405 405
 	}
406 406
 
407 407
 	/**
@@ -412,42 +412,42 @@  discard block
 block discarded – undo
412 412
 	 * @version 4.0.0
413 413
 	 * @param object $notification
414 414
 	 */
415
-	public function process_webhook_refund( $notification ) {
416
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
415
+	public function process_webhook_refund($notification) {
416
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
417 417
 
418
-		if ( ! $order ) {
419
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
418
+		if ( ! $order) {
419
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
420 420
 			return;
421 421
 		}
422 422
 
423 423
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
424 424
 
425
-		if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) {
426
-			$charge    = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
427
-			$captured  = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true );
428
-			$refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_refund_id', true ) : $order->get_meta( '_stripe_refund_id', true );
425
+		if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) {
426
+			$charge    = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
427
+			$captured  = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true);
428
+			$refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_refund_id', true) : $order->get_meta('_stripe_refund_id', true);
429 429
 
430 430
 			// If the refund ID matches, don't continue to prevent double refunding.
431
-			if ( $notification->data->object->refunds->data[0]->id === $refund_id ) {
431
+			if ($notification->data->object->refunds->data[0]->id === $refund_id) {
432 432
 				return;
433 433
 			}
434 434
 
435 435
 			// Only refund captured charge.
436
-			if ( $charge ) {
437
-				$reason = ( isset( $captured ) && 'yes' === $captured ) ? __( 'Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe' ) : __( 'Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe' );
436
+			if ($charge) {
437
+				$reason = (isset($captured) && 'yes' === $captured) ? __('Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe') : __('Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe');
438 438
 
439 439
 				// Create the refund.
440
-				$refund = wc_create_refund( array(
440
+				$refund = wc_create_refund(array(
441 441
 					'order_id'       => $order_id,
442
-					'amount'         => $this->get_refund_amount( $notification ),
442
+					'amount'         => $this->get_refund_amount($notification),
443 443
 					'reason'         => $reason,
444
-				) );
444
+				));
445 445
 
446
-				if ( is_wp_error( $refund ) ) {
447
-					WC_Stripe_Logger::log( $refund->get_error_message() );
446
+				if (is_wp_error($refund)) {
447
+					WC_Stripe_Logger::log($refund->get_error_message());
448 448
 				}
449 449
 
450
-				$order->add_order_note( $reason );
450
+				$order->add_order_note($reason);
451 451
 			}
452 452
 		}
453 453
 	}
@@ -458,17 +458,17 @@  discard block
 block discarded – undo
458 458
 	 * @since 4.0.6
459 459
 	 * @param object $notification
460 460
 	 */
461
-	public function process_review_opened( $notification ) {
462
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge );
461
+	public function process_review_opened($notification) {
462
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge);
463 463
 
464
-		if ( ! $order ) {
465
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge );
464
+		if ( ! $order) {
465
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge);
466 466
 			return;
467 467
 		}
468 468
 
469
-		if ( $notification->data->object->open ) {
469
+		if ($notification->data->object->open) {
470 470
 			/* translators: token is the reason returned. */
471
-			$order->update_status( 'on-hold', sprintf( __( 'A review has been opened for this order. Action is needed. Please go to your Stripe Dashboard to review the issue. Reason: (%s)', 'woocommerce-gateway-stripe' ), $notification->data->object->reason ) );
471
+			$order->update_status('on-hold', sprintf(__('A review has been opened for this order. Action is needed. Please go to your Stripe Dashboard to review the issue. Reason: (%s)', 'woocommerce-gateway-stripe'), $notification->data->object->reason));
472 472
 		}
473 473
 	}
474 474
 
@@ -478,21 +478,21 @@  discard block
 block discarded – undo
478 478
 	 * @since 4.0.6
479 479
 	 * @param object $notification
480 480
 	 */
481
-	public function process_review_closed( $notification ) {
482
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge );
481
+	public function process_review_closed($notification) {
482
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge);
483 483
 
484
-		if ( ! $order ) {
485
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge );
484
+		if ( ! $order) {
485
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge);
486 486
 			return;
487 487
 		}
488 488
 
489 489
 		/* translators: token is the reason returned. */
490
-		$message = sprintf( __( 'The opened review for this order is now closed. Reason: (%s)', 'woocommerce-gateway-stripe' ), $notification->data->object->reason );
490
+		$message = sprintf(__('The opened review for this order is now closed. Reason: (%s)', 'woocommerce-gateway-stripe'), $notification->data->object->reason);
491 491
 
492
-		if ( 'on-hold' === $order->get_status() ) {
493
-			$order->update_status( 'processing', $message );
492
+		if ('on-hold' === $order->get_status()) {
493
+			$order->update_status('processing', $message);
494 494
 		} else {
495
-			$order->add_note( $message );
495
+			$order->add_note($message);
496 496
 		}
497 497
 	}
498 498
 
@@ -503,7 +503,7 @@  discard block
 block discarded – undo
503 503
 	 * @version 4.0.0
504 504
 	 * @param object $notification
505 505
 	 */
506
-	public function is_partial_capture( $notification ) {
506
+	public function is_partial_capture($notification) {
507 507
 		return 0 < $notification->data->object->amount_refunded;
508 508
 	}
509 509
 
@@ -514,11 +514,11 @@  discard block
 block discarded – undo
514 514
 	 * @version 4.0.0
515 515
 	 * @param object $notification
516 516
 	 */
517
-	public function get_refund_amount( $notification ) {
518
-		if ( $this->is_partial_capture( $notification ) ) {
517
+	public function get_refund_amount($notification) {
518
+		if ($this->is_partial_capture($notification)) {
519 519
 			$amount = $notification->data->object->amount_refunded / 100;
520 520
 
521
-			if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
521
+			if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) {
522 522
 				$amount = $notification->data->object->amount_refunded;
523 523
 			}
524 524
 
@@ -535,12 +535,12 @@  discard block
 block discarded – undo
535 535
 	 * @version 4.0.0
536 536
 	 * @param object $notification
537 537
 	 */
538
-	public function get_partial_amount_to_charge( $notification ) {
539
-		if ( $this->is_partial_capture( $notification ) ) {
540
-			$amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ) / 100;
538
+	public function get_partial_amount_to_charge($notification) {
539
+		if ($this->is_partial_capture($notification)) {
540
+			$amount = ($notification->data->object->amount - $notification->data->object->amount_refunded) / 100;
541 541
 
542
-			if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
543
-				$amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded );
542
+			if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) {
543
+				$amount = ($notification->data->object->amount - $notification->data->object->amount_refunded);
544 544
 			}
545 545
 
546 546
 			return $amount;
@@ -556,44 +556,44 @@  discard block
 block discarded – undo
556 556
 	 * @version 4.0.0
557 557
 	 * @param string $request_body
558 558
 	 */
559
-	public function process_webhook( $request_body ) {
560
-		$notification = json_decode( $request_body );
559
+	public function process_webhook($request_body) {
560
+		$notification = json_decode($request_body);
561 561
 
562
-		switch ( $notification->type ) {
562
+		switch ($notification->type) {
563 563
 			case 'source.chargeable':
564
-				$this->process_webhook_payment( $notification );
564
+				$this->process_webhook_payment($notification);
565 565
 				break;
566 566
 
567 567
 			case 'source.canceled':
568
-				$this->process_webhook_source_canceled( $notification );
568
+				$this->process_webhook_source_canceled($notification);
569 569
 				break;
570 570
 
571 571
 			case 'charge.succeeded':
572
-				$this->process_webhook_charge_succeeded( $notification );
572
+				$this->process_webhook_charge_succeeded($notification);
573 573
 				break;
574 574
 
575 575
 			case 'charge.failed':
576
-				$this->process_webhook_charge_failed( $notification );
576
+				$this->process_webhook_charge_failed($notification);
577 577
 				break;
578 578
 
579 579
 			case 'charge.captured':
580
-				$this->process_webhook_capture( $notification );
580
+				$this->process_webhook_capture($notification);
581 581
 				break;
582 582
 
583 583
 			case 'charge.dispute.created':
584
-				$this->process_webhook_dispute( $notification );
584
+				$this->process_webhook_dispute($notification);
585 585
 				break;
586 586
 
587 587
 			case 'charge.refunded':
588
-				$this->process_webhook_refund( $notification );
588
+				$this->process_webhook_refund($notification);
589 589
 				break;
590 590
 
591 591
 			case 'review.opened':
592
-				$this->process_review_opened( $notification );
592
+				$this->process_review_opened($notification);
593 593
 				break;
594 594
 
595 595
 			case 'review.closed':
596
-				$this->process_review_closed( $notification );
596
+				$this->process_review_closed($notification);
597 597
 				break;
598 598
 
599 599
 		}
Please login to merge, or discard this patch.