Completed
Push — master ( 6b3970...b3f03c )
by Roy
04:23
created
includes/class-wc-stripe-webhook-handler.php 1 patch
Spacing   +180 added lines, -180 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
 
@@ -32,9 +32,9 @@  discard block
 block discarded – undo
32 32
 	 */
33 33
 	public function __construct() {
34 34
 		$this->retry_interval = 2;
35
-		$stripe_settings      = get_option( 'woocommerce_stripe_settings', array() );
36
-		$this->testmode       = ( ! empty( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'] ) ? true : false;
37
-		add_action( 'woocommerce_api_wc_stripe', array( $this, 'check_for_webhook' ) );
35
+		$stripe_settings      = get_option('woocommerce_stripe_settings', array());
36
+		$this->testmode       = ( ! empty($stripe_settings['testmode']) && 'yes' === $stripe_settings['testmode']) ? true : false;
37
+		add_action('woocommerce_api_wc_stripe', array($this, 'check_for_webhook'));
38 38
 	}
39 39
 
40 40
 	/**
@@ -44,24 +44,24 @@  discard block
 block discarded – undo
44 44
 	 * @version 4.0.0
45 45
 	 */
46 46
 	public function check_for_webhook() {
47
-		if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] )
48
-			|| ! isset( $_GET['wc-api'] )
49
-			|| ( 'wc_stripe' !== $_GET['wc-api'] )
47
+		if (('POST' !== $_SERVER['REQUEST_METHOD'])
48
+			|| ! isset($_GET['wc-api'])
49
+			|| ('wc_stripe' !== $_GET['wc-api'])
50 50
 		) {
51 51
 			return;
52 52
 		}
53 53
 
54
-		$request_body    = file_get_contents( 'php://input' );
55
-		$request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER );
54
+		$request_body    = file_get_contents('php://input');
55
+		$request_headers = array_change_key_case($this->get_request_headers(), CASE_UPPER);
56 56
 
57 57
 		// Validate it to make sure it is legit.
58
-		if ( $this->is_valid_request( $request_headers, $request_body ) ) {
59
-			$this->process_webhook( $request_body );
60
-			status_header( 200 );
58
+		if ($this->is_valid_request($request_headers, $request_body)) {
59
+			$this->process_webhook($request_body);
60
+			status_header(200);
61 61
 			exit;
62 62
 		} else {
63
-			WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) );
64
-			status_header( 400 );
63
+			WC_Stripe_Logger::log('Incoming webhook failed validation: ' . print_r($request_body, true));
64
+			status_header(400);
65 65
 			exit;
66 66
 		}
67 67
 	}
@@ -76,12 +76,12 @@  discard block
 block discarded – undo
76 76
 	 * @param string $request_body The request body from Stripe.
77 77
 	 * @return bool
78 78
 	 */
79
-	public function is_valid_request( $request_headers = null, $request_body = null ) {
80
-		if ( null === $request_headers || null === $request_body ) {
79
+	public function is_valid_request($request_headers = null, $request_body = null) {
80
+		if (null === $request_headers || null === $request_body) {
81 81
 			return false;
82 82
 		}
83 83
 
84
-		if ( ! empty( $request_headers['USER-AGENT'] ) && ! preg_match( '/Stripe/', $request_headers['USER-AGENT'] ) ) {
84
+		if ( ! empty($request_headers['USER-AGENT']) && ! preg_match('/Stripe/', $request_headers['USER-AGENT'])) {
85 85
 			return false;
86 86
 		}
87 87
 
@@ -97,11 +97,11 @@  discard block
 block discarded – undo
97 97
 	 * @version 4.0.0
98 98
 	 */
99 99
 	public function get_request_headers() {
100
-		if ( ! function_exists( 'getallheaders' ) ) {
100
+		if ( ! function_exists('getallheaders')) {
101 101
 			$headers = [];
102
-			foreach ( $_SERVER as $name => $value ) {
103
-				if ( 'HTTP_' === substr( $name, 0, 5 ) ) {
104
-					$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value;
102
+			foreach ($_SERVER as $name => $value) {
103
+				if ('HTTP_' === substr($name, 0, 5)) {
104
+					$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
105 105
 				}
106 106
 			}
107 107
 
@@ -120,30 +120,30 @@  discard block
 block discarded – undo
120 120
 	 * @param object $notification
121 121
 	 * @param bool $retry
122 122
 	 */
123
-	public function process_webhook_payment( $notification, $retry = true ) {
123
+	public function process_webhook_payment($notification, $retry = true) {
124 124
 		// The following 2 payment methods are synchronous so does not need to be handle via webhook.
125
-		if ( 'card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type ) {
125
+		if ('card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type) {
126 126
 			return;
127 127
 		}
128 128
 
129
-		$order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id );
129
+		$order = WC_Stripe_Helper::get_order_by_source_id($notification->data->object->id);
130 130
 
131
-		if ( ! $order ) {
132
-			WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id );
131
+		if ( ! $order) {
132
+			WC_Stripe_Logger::log('Could not find order via source ID: ' . $notification->data->object->id);
133 133
 			return;
134 134
 		}
135 135
 
136 136
 		$order_id  = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
137 137
 		$source_id = $notification->data->object->id;
138 138
 
139
-		$is_pending_receiver = ( 'receiver' === $notification->data->object->flow );
139
+		$is_pending_receiver = ('receiver' === $notification->data->object->flow);
140 140
 
141 141
 		try {
142
-			if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() ) {
142
+			if ('processing' === $order->get_status() || 'completed' === $order->get_status()) {
143 143
 				return;
144 144
 			}
145 145
 
146
-			if ( 'on-hold' === $order->get_status() && ! $is_pending_receiver ) {
146
+			if ('on-hold' === $order->get_status() && ! $is_pending_receiver) {
147 147
 				return;
148 148
 			}
149 149
 
@@ -151,94 +151,94 @@  discard block
 block discarded – undo
151 151
 			$response = null;
152 152
 
153 153
 			// This will throw exception if not valid.
154
-			$this->validate_minimum_order_amount( $order );
154
+			$this->validate_minimum_order_amount($order);
155 155
 
156
-			WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
156
+			WC_Stripe_Logger::log("Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}");
157 157
 
158 158
 			// Prep source object.
159 159
 			$source_object           = new stdClass();
160 160
 			$source_object->token_id = '';
161
-			$source_object->customer = $this->get_stripe_customer_id( $order );
161
+			$source_object->customer = $this->get_stripe_customer_id($order);
162 162
 			$source_object->source   = $source_id;
163 163
 
164 164
 			// Make the request.
165
-			$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ), 'charges', 'POST', true );
165
+			$response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object), 'charges', 'POST', true);
166 166
 			$headers  = $response['headers'];
167 167
 			$response = $response['body'];
168 168
 
169
-			if ( ! empty( $response->error ) ) {
169
+			if ( ! empty($response->error)) {
170 170
 				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
171
-				if ( $this->is_no_such_customer_error( $response->error ) ) {
172
-					if ( WC_Stripe_Helper::is_pre_30() ) {
173
-						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
174
-						delete_post_meta( $order_id, '_stripe_customer_id' );
171
+				if ($this->is_no_such_customer_error($response->error)) {
172
+					if (WC_Stripe_Helper::is_pre_30()) {
173
+						delete_user_meta($order->customer_user, '_stripe_customer_id');
174
+						delete_post_meta($order_id, '_stripe_customer_id');
175 175
 					} else {
176
-						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
177
-						$order->delete_meta_data( '_stripe_customer_id' );
176
+						delete_user_meta($order->get_customer_id(), '_stripe_customer_id');
177
+						$order->delete_meta_data('_stripe_customer_id');
178 178
 						$order->save();
179 179
 					}
180 180
 				}
181 181
 
182
-				if ( $this->is_no_such_token_error( $response->error ) && $prepared_source->token_id ) {
182
+				if ($this->is_no_such_token_error($response->error) && $prepared_source->token_id) {
183 183
 					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
184
-					$wc_token = WC_Payment_Tokens::get( $prepared_source->token_id );
184
+					$wc_token = WC_Payment_Tokens::get($prepared_source->token_id);
185 185
 					$wc_token->delete();
186
-					$localized_message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
187
-					$order->add_order_note( $localized_message );
188
-					throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
186
+					$localized_message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe');
187
+					$order->add_order_note($localized_message);
188
+					throw new WC_Stripe_Exception(print_r($response, true), $localized_message);
189 189
 				}
190 190
 
191 191
 				// We want to retry.
192
-				if ( $this->is_retryable_error( $response->error ) ) {
193
-					if ( $retry ) {
192
+				if ($this->is_retryable_error($response->error)) {
193
+					if ($retry) {
194 194
 						// Don't do anymore retries after this.
195
-						if ( 5 <= $this->retry_interval ) {
195
+						if (5 <= $this->retry_interval) {
196 196
 
197
-							return $this->process_webhook_payment( $notification, false );
197
+							return $this->process_webhook_payment($notification, false);
198 198
 						}
199 199
 
200
-						sleep( $this->retry_interval );
200
+						sleep($this->retry_interval);
201 201
 
202 202
 						$this->retry_interval++;
203
-						return $this->process_webhook_payment( $notification, true );
203
+						return $this->process_webhook_payment($notification, true);
204 204
 					} else {
205
-						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
206
-						$order->add_order_note( $localized_message );
207
-						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
205
+						$localized_message = __('Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe');
206
+						$order->add_order_note($localized_message);
207
+						throw new WC_Stripe_Exception(print_r($response, true), $localized_message);
208 208
 					}
209 209
 				}
210 210
 
211 211
 				$localized_messages = WC_Stripe_Helper::get_localized_messages();
212 212
 
213
-				if ( 'card_error' === $response->error->type ) {
214
-					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
213
+				if ('card_error' === $response->error->type) {
214
+					$localized_message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message;
215 215
 				} else {
216
-					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
216
+					$localized_message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message;
217 217
 				}
218 218
 
219
-				$order->add_order_note( $localized_message );
219
+				$order->add_order_note($localized_message);
220 220
 
221
-				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
221
+				throw new WC_Stripe_Exception(print_r($response, true), $localized_message);
222 222
 			}
223 223
 
224 224
 			// To prevent double processing the order on WC side.
225
-			if ( ! $this->is_original_request( $headers ) ) {
225
+			if ( ! $this->is_original_request($headers)) {
226 226
 				return;
227 227
 			}
228 228
 
229
-			do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order );
229
+			do_action('wc_gateway_stripe_process_webhook_payment', $response, $order);
230 230
 
231
-			$this->process_response( $response, $order );
231
+			$this->process_response($response, $order);
232 232
 
233
-		} catch ( WC_Stripe_Exception $e ) {
234
-			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
233
+		} catch (WC_Stripe_Exception $e) {
234
+			WC_Stripe_Logger::log('Error: ' . $e->getMessage());
235 235
 
236
-			do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification, $e );
236
+			do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification, $e);
237 237
 
238
-			$statuses = array( 'pending', 'failed' );
238
+			$statuses = array('pending', 'failed');
239 239
 
240
-			if ( $order->has_status( $statuses ) ) {
241
-				$this->send_failed_order_email( $order_id );
240
+			if ($order->has_status($statuses)) {
241
+				$this->send_failed_order_email($order_id);
242 242
 			}
243 243
 		}
244 244
 	}
@@ -251,21 +251,21 @@  discard block
 block discarded – undo
251 251
 	 * @since 4.0.0
252 252
 	 * @param object $notification
253 253
 	 */
254
-	public function process_webhook_dispute( $notification ) {
255
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge );
254
+	public function process_webhook_dispute($notification) {
255
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge);
256 256
 
257
-		if ( ! $order ) {
258
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge );
257
+		if ( ! $order) {
258
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge);
259 259
 			return;
260 260
 		}
261 261
 
262 262
 		/* translators: 1) The URL to the order. */
263
-		$order->update_status( 'on-hold', sprintf( __( 'A dispute was created for this order. Response is needed. Please go to your <a href="%s" title="Stripe Dashboard" target="_blank">Stripe Dashboard</a> to review this dispute.', 'woocommerce-gateway-stripe' ), $this->get_transaction_url( $order ) ) );
263
+		$order->update_status('on-hold', sprintf(__('A dispute was created for this order. Response is needed. Please go to your <a href="%s" title="Stripe Dashboard" target="_blank">Stripe Dashboard</a> to review this dispute.', 'woocommerce-gateway-stripe'), $this->get_transaction_url($order)));
264 264
 
265
-		do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification );
265
+		do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification);
266 266
 
267 267
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
268
-		$this->send_failed_order_email( $order_id );
268
+		$this->send_failed_order_email($order_id);
269 269
 	}
270 270
 
271 271
 	/**
@@ -276,41 +276,41 @@  discard block
 block discarded – undo
276 276
 	 * @version 4.0.0
277 277
 	 * @param object $notification
278 278
 	 */
279
-	public function process_webhook_capture( $notification ) {
280
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
279
+	public function process_webhook_capture($notification) {
280
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
281 281
 
282
-		if ( ! $order ) {
283
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
282
+		if ( ! $order) {
283
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
284 284
 			return;
285 285
 		}
286 286
 
287 287
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
288 288
 
289
-		if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) {
290
-			$charge   = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
291
-			$captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true );
289
+		if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) {
290
+			$charge   = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
291
+			$captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true);
292 292
 
293
-			if ( $charge && 'no' === $captured ) {
294
-				WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' );
293
+			if ($charge && 'no' === $captured) {
294
+				WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', 'yes') : $order->update_meta_data('_stripe_charge_captured', 'yes');
295 295
 
296 296
 				// Store other data such as fees
297
-				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 );
297
+				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);
298 298
 
299
-				if ( isset( $notification->data->object->balance_transaction ) ) {
300
-					$this->update_fees( $order, $notification->data->object->balance_transaction );
299
+				if (isset($notification->data->object->balance_transaction)) {
300
+					$this->update_fees($order, $notification->data->object->balance_transaction);
301 301
 				}
302 302
 
303
-				if ( is_callable( array( $order, 'save' ) ) ) {
303
+				if (is_callable(array($order, 'save'))) {
304 304
 					$order->save();
305 305
 				}
306 306
 
307 307
 				/* translators: transaction id */
308
-				$order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) );
308
+				$order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id));
309 309
 
310 310
 				// Check and see if capture is partial.
311
-				if ( $this->is_partial_capture( $notification ) ) {
312
-					$order->set_total( $this->get_partial_amount_to_charge( $notification ) );
313
-					$order->add_order_note( __( 'This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe' ) );
311
+				if ($this->is_partial_capture($notification)) {
312
+					$order->set_total($this->get_partial_amount_to_charge($notification));
313
+					$order->add_order_note(__('This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe'));
314 314
 					$order->save();
315 315
 				}
316 316
 			}
@@ -325,38 +325,38 @@  discard block
 block discarded – undo
325 325
 	 * @version 4.0.0
326 326
 	 * @param object $notification
327 327
 	 */
328
-	public function process_webhook_charge_succeeded( $notification ) {
328
+	public function process_webhook_charge_succeeded($notification) {
329 329
 		// The following payment methods are synchronous so does not need to be handle via webhook.
330
-		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 ) ) {
330
+		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)) {
331 331
 			return;
332 332
 		}
333 333
 
334
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
334
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
335 335
 
336
-		if ( ! $order ) {
337
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
336
+		if ( ! $order) {
337
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
338 338
 			return;
339 339
 		}
340 340
 
341 341
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
342 342
 
343
-		if ( 'on-hold' !== $order->get_status() ) {
343
+		if ('on-hold' !== $order->get_status()) {
344 344
 			return;
345 345
 		}
346 346
 
347 347
 		// Store other data such as fees
348
-		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 );
348
+		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);
349 349
 
350
-		if ( isset( $notification->data->object->balance_transaction ) ) {
351
-			$this->update_fees( $order, $notification->data->object->balance_transaction );
350
+		if (isset($notification->data->object->balance_transaction)) {
351
+			$this->update_fees($order, $notification->data->object->balance_transaction);
352 352
 		}
353 353
 
354
-		if ( is_callable( array( $order, 'save' ) ) ) {
354
+		if (is_callable(array($order, 'save'))) {
355 355
 			$order->save();
356 356
 		}
357 357
 
358 358
 		/* translators: transaction id */
359
-		$order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) );
359
+		$order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id));
360 360
 	}
361 361
 
362 362
 	/**
@@ -367,23 +367,23 @@  discard block
 block discarded – undo
367 367
 	 * @version 4.0.0
368 368
 	 * @param object $notification
369 369
 	 */
370
-	public function process_webhook_charge_failed( $notification ) {
371
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
370
+	public function process_webhook_charge_failed($notification) {
371
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
372 372
 
373
-		if ( ! $order ) {
374
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
373
+		if ( ! $order) {
374
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
375 375
 			return;
376 376
 		}
377 377
 
378 378
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
379 379
 
380
-		if ( 'on-hold' !== $order->get_status() ) {
380
+		if ('on-hold' !== $order->get_status()) {
381 381
 			return;
382 382
 		}
383 383
 
384
-		$order->update_status( 'failed', __( 'This payment failed to clear.', 'woocommerce-gateway-stripe' ) );
384
+		$order->update_status('failed', __('This payment failed to clear.', 'woocommerce-gateway-stripe'));
385 385
 
386
-		do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification );
386
+		do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification);
387 387
 	}
388 388
 
389 389
 	/**
@@ -394,23 +394,23 @@  discard block
 block discarded – undo
394 394
 	 * @version 4.0.0
395 395
 	 * @param object $notification
396 396
 	 */
397
-	public function process_webhook_source_canceled( $notification ) {
398
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
397
+	public function process_webhook_source_canceled($notification) {
398
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
399 399
 
400
-		if ( ! $order ) {
401
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
400
+		if ( ! $order) {
401
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
402 402
 			return;
403 403
 		}
404 404
 
405 405
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
406 406
 
407
-		if ( 'on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status() ) {
407
+		if ('on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status()) {
408 408
 			return;
409 409
 		}
410 410
 
411
-		$order->update_status( 'cancelled', __( 'This payment has cancelled.', 'woocommerce-gateway-stripe' ) );
411
+		$order->update_status('cancelled', __('This payment has cancelled.', 'woocommerce-gateway-stripe'));
412 412
 
413
-		do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification );
413
+		do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification);
414 414
 	}
415 415
 
416 416
 	/**
@@ -420,57 +420,57 @@  discard block
 block discarded – undo
420 420
 	 * @version 4.0.0
421 421
 	 * @param object $notification
422 422
 	 */
423
-	public function process_webhook_refund( $notification ) {
424
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id );
423
+	public function process_webhook_refund($notification) {
424
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id);
425 425
 
426
-		if ( ! $order ) {
427
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id );
426
+		if ( ! $order) {
427
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id);
428 428
 			return;
429 429
 		}
430 430
 
431 431
 		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
432 432
 
433
-		if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) {
434
-			$charge    = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
435
-			$captured  = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true );
436
-			$refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_refund_id', true ) : $order->get_meta( '_stripe_refund_id', true );
433
+		if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) {
434
+			$charge    = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
435
+			$captured  = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true);
436
+			$refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_refund_id', true) : $order->get_meta('_stripe_refund_id', true);
437 437
 
438 438
 			// If the refund ID matches, don't continue to prevent double refunding.
439
-			if ( $notification->data->object->refunds->data[0]->id === $refund_id ) {
439
+			if ($notification->data->object->refunds->data[0]->id === $refund_id) {
440 440
 				return;
441 441
 			}
442 442
 
443 443
 			// Only refund captured charge.
444
-			if ( $charge ) {
445
-				$reason = ( isset( $captured ) && 'yes' === $captured ) ? __( 'Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe' ) : __( 'Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe' );
444
+			if ($charge) {
445
+				$reason = (isset($captured) && 'yes' === $captured) ? __('Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe') : __('Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe');
446 446
 
447 447
 				// Create the refund.
448
-				$refund = wc_create_refund( array(
448
+				$refund = wc_create_refund(array(
449 449
 					'order_id' => $order_id,
450
-					'amount'   => $this->get_refund_amount( $notification ),
450
+					'amount'   => $this->get_refund_amount($notification),
451 451
 					'reason'   => $reason,
452
-				) );
452
+				));
453 453
 
454
-				if ( is_wp_error( $refund ) ) {
455
-					WC_Stripe_Logger::log( $refund->get_error_message() );
454
+				if (is_wp_error($refund)) {
455
+					WC_Stripe_Logger::log($refund->get_error_message());
456 456
 				}
457 457
 
458
-				WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_refund_id', $notification->data->object->refunds->data[0]->id ) : $order->update_meta_data( '_stripe_refund_id', $notification->data->object->refunds->data[0]->id );
458
+				WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_refund_id', $notification->data->object->refunds->data[0]->id) : $order->update_meta_data('_stripe_refund_id', $notification->data->object->refunds->data[0]->id);
459 459
 
460
-				$amount = wc_price( $notification->data->object->refunds->data[0]->amount / 100 );
460
+				$amount = wc_price($notification->data->object->refunds->data[0]->amount / 100);
461 461
 
462
-				if ( in_array( strtolower( $order->get_currency() ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
463
-					$amount = wc_price( $notification->data->object->refunds->data[0]->amount );
462
+				if (in_array(strtolower($order->get_currency()), WC_Stripe_Helper::no_decimal_currencies())) {
463
+					$amount = wc_price($notification->data->object->refunds->data[0]->amount);
464 464
 				}
465 465
 
466
-				if ( isset( $notification->data->object->refunds->data[0]->balance_transaction ) ) {
467
-					$this->update_fees( $order, $notification->data->object->refunds->data[0]->balance_transaction );
466
+				if (isset($notification->data->object->refunds->data[0]->balance_transaction)) {
467
+					$this->update_fees($order, $notification->data->object->refunds->data[0]->balance_transaction);
468 468
 				}
469 469
 
470 470
 				/* translators: 1) dollar amount 2) transaction id 3) refund message */
471
-				$refund_message = ( isset( $captured ) && 'yes' === $captured ) ? sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $notification->data->object->refunds->data[0]->id, $reason ) : __( 'Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe' );
471
+				$refund_message = (isset($captured) && 'yes' === $captured) ? sprintf(__('Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe'), $amount, $notification->data->object->refunds->data[0]->id, $reason) : __('Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe');
472 472
 
473
-				$order->add_order_note( $refund_message );
473
+				$order->add_order_note($refund_message);
474 474
 			}
475 475
 		}
476 476
 	}
@@ -481,21 +481,21 @@  discard block
 block discarded – undo
481 481
 	 * @since 4.0.6
482 482
 	 * @param object $notification
483 483
 	 */
484
-	public function process_review_opened( $notification ) {
485
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge );
484
+	public function process_review_opened($notification) {
485
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge);
486 486
 
487
-		if ( ! $order ) {
488
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge );
487
+		if ( ! $order) {
488
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge);
489 489
 			return;
490 490
 		}
491 491
 
492 492
 		/* translators: 1) The URL to the order. 2) The reason type. */
493
-		$message = sprintf( __( 'A review has been opened for this order. Action is needed. Please go to your <a href="%1$s" title="Stripe Dashboard" target="_blank">Stripe Dashboard</a> to review the issue. Reason: (%2$s)', 'woocommerce-gateway-stripe' ), $this->get_transaction_url( $order ), $notification->data->object->reason );
493
+		$message = sprintf(__('A review has been opened for this order. Action is needed. Please go to your <a href="%1$s" title="Stripe Dashboard" target="_blank">Stripe Dashboard</a> to review the issue. Reason: (%2$s)', 'woocommerce-gateway-stripe'), $this->get_transaction_url($order), $notification->data->object->reason);
494 494
 
495
-		if ( apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) ) {
496
-			$order->update_status( 'on-hold', $message );
495
+		if (apply_filters('wc_stripe_webhook_review_change_order_status', true, $order, $notification)) {
496
+			$order->update_status('on-hold', $message);
497 497
 		} else {
498
-			$order->add_order_note( $message );
498
+			$order->add_order_note($message);
499 499
 		}
500 500
 	}
501 501
 
@@ -505,25 +505,25 @@  discard block
 block discarded – undo
505 505
 	 * @since 4.0.6
506 506
 	 * @param object $notification
507 507
 	 */
508
-	public function process_review_closed( $notification ) {
509
-		$order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge );
508
+	public function process_review_closed($notification) {
509
+		$order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge);
510 510
 
511
-		if ( ! $order ) {
512
-			WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge );
511
+		if ( ! $order) {
512
+			WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge);
513 513
 			return;
514 514
 		}
515 515
 
516 516
 		/* translators: 1) The reason type. */
517
-		$message = sprintf( __( 'The opened review for this order is now closed. Reason: (%s)', 'woocommerce-gateway-stripe' ), $notification->data->object->reason );
517
+		$message = sprintf(__('The opened review for this order is now closed. Reason: (%s)', 'woocommerce-gateway-stripe'), $notification->data->object->reason);
518 518
 
519
-		if ( 'on-hold' === $order->get_status() ) {
520
-			if ( apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) ) {
521
-				$order->update_status( 'processing', $message );
519
+		if ('on-hold' === $order->get_status()) {
520
+			if (apply_filters('wc_stripe_webhook_review_change_order_status', true, $order, $notification)) {
521
+				$order->update_status('processing', $message);
522 522
 			} else {
523
-				$order->add_order_note( $message );
523
+				$order->add_order_note($message);
524 524
 			}
525 525
 		} else {
526
-			$order->add_order_note( $message );
526
+			$order->add_order_note($message);
527 527
 		}
528 528
 	}
529 529
 
@@ -534,7 +534,7 @@  discard block
 block discarded – undo
534 534
 	 * @version 4.0.0
535 535
 	 * @param object $notification
536 536
 	 */
537
-	public function is_partial_capture( $notification ) {
537
+	public function is_partial_capture($notification) {
538 538
 		return 0 < $notification->data->object->amount_refunded;
539 539
 	}
540 540
 
@@ -545,11 +545,11 @@  discard block
 block discarded – undo
545 545
 	 * @version 4.0.0
546 546
 	 * @param object $notification
547 547
 	 */
548
-	public function get_refund_amount( $notification ) {
549
-		if ( $this->is_partial_capture( $notification ) ) {
548
+	public function get_refund_amount($notification) {
549
+		if ($this->is_partial_capture($notification)) {
550 550
 			$amount = $notification->data->object->refunds->data[0]->amount / 100;
551 551
 
552
-			if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
552
+			if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) {
553 553
 				$amount = $notification->data->object->refunds->data[0]->amount;
554 554
 			}
555 555
 
@@ -566,12 +566,12 @@  discard block
 block discarded – undo
566 566
 	 * @version 4.0.0
567 567
 	 * @param object $notification
568 568
 	 */
569
-	public function get_partial_amount_to_charge( $notification ) {
570
-		if ( $this->is_partial_capture( $notification ) ) {
571
-			$amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ) / 100;
569
+	public function get_partial_amount_to_charge($notification) {
570
+		if ($this->is_partial_capture($notification)) {
571
+			$amount = ($notification->data->object->amount - $notification->data->object->amount_refunded) / 100;
572 572
 
573
-			if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
574
-				$amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded );
573
+			if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) {
574
+				$amount = ($notification->data->object->amount - $notification->data->object->amount_refunded);
575 575
 			}
576 576
 
577 577
 			return $amount;
@@ -587,44 +587,44 @@  discard block
 block discarded – undo
587 587
 	 * @version 4.0.0
588 588
 	 * @param string $request_body
589 589
 	 */
590
-	public function process_webhook( $request_body ) {
591
-		$notification = json_decode( $request_body );
590
+	public function process_webhook($request_body) {
591
+		$notification = json_decode($request_body);
592 592
 
593
-		switch ( $notification->type ) {
593
+		switch ($notification->type) {
594 594
 			case 'source.chargeable':
595
-				$this->process_webhook_payment( $notification );
595
+				$this->process_webhook_payment($notification);
596 596
 				break;
597 597
 
598 598
 			case 'source.canceled':
599
-				$this->process_webhook_source_canceled( $notification );
599
+				$this->process_webhook_source_canceled($notification);
600 600
 				break;
601 601
 
602 602
 			case 'charge.succeeded':
603
-				$this->process_webhook_charge_succeeded( $notification );
603
+				$this->process_webhook_charge_succeeded($notification);
604 604
 				break;
605 605
 
606 606
 			case 'charge.failed':
607
-				$this->process_webhook_charge_failed( $notification );
607
+				$this->process_webhook_charge_failed($notification);
608 608
 				break;
609 609
 
610 610
 			case 'charge.captured':
611
-				$this->process_webhook_capture( $notification );
611
+				$this->process_webhook_capture($notification);
612 612
 				break;
613 613
 
614 614
 			case 'charge.dispute.created':
615
-				$this->process_webhook_dispute( $notification );
615
+				$this->process_webhook_dispute($notification);
616 616
 				break;
617 617
 
618 618
 			case 'charge.refunded':
619
-				$this->process_webhook_refund( $notification );
619
+				$this->process_webhook_refund($notification);
620 620
 				break;
621 621
 
622 622
 			case 'review.opened':
623
-				$this->process_review_opened( $notification );
623
+				$this->process_review_opened($notification);
624 624
 				break;
625 625
 
626 626
 			case 'review.closed':
627
-				$this->process_review_closed( $notification );
627
+				$this->process_review_closed($notification);
628 628
 				break;
629 629
 
630 630
 		}
Please login to merge, or discard this patch.