Completed
Pull Request — master (#500)
by Roy
02:10
created
includes/class-wc-stripe-webhook-handler.php 1 patch
Spacing   +153 added lines, -153 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
 
@@ -17,8 +17,8 @@  discard block
 block discarded – undo
17 17
 	 * @version 4.0.0
18 18
 	 */
19 19
 	public function __construct() {
20
-		add_action( 'woocommerce_api_wc_stripe', array( $this, 'check_for_webhook' ) );
21
-		add_action( 'wc_stripe_webhook_event', array( $this, 'process_cron_job' ), 10, 2 );
20
+		add_action('woocommerce_api_wc_stripe', array($this, 'check_for_webhook'));
21
+		add_action('wc_stripe_webhook_event', array($this, 'process_cron_job'), 10, 2);
22 22
 	}
23 23
 
24 24
 	/**
@@ -28,8 +28,8 @@  discard block
 block discarded – undo
28 28
 	 * @param JSON $notification
29 29
 	 * @param string $idempotency_key
30 30
 	 */
31
-	public function process_cron_job( $notification, $idempotency_key ) {
32
-		$this->process_webhook( $notification );
31
+	public function process_cron_job($notification, $idempotency_key) {
32
+		$this->process_webhook($notification);
33 33
 	}
34 34
 
35 35
 	/**
@@ -38,8 +38,8 @@  discard block
 block discarded – undo
38 38
 	 * @since 4.0.5
39 39
 	 * @param array $notification
40 40
 	 */
41
-	public function schedule_cron( $notification ) {
42
-		wp_schedule_single_event( time() + 10, 'wc_stripe_webhook_event', array( $notification, uniqid() ) );
41
+	public function schedule_cron($notification) {
42
+		wp_schedule_single_event(time() + 10, 'wc_stripe_webhook_event', array($notification, uniqid()));
43 43
 	}
44 44
 
45 45
 	/**
@@ -49,25 +49,25 @@  discard block
 block discarded – undo
49 49
 	 * @version 4.0.0
50 50
 	 */
51 51
 	public function check_for_webhook() {
52
-		if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] )
53
-			|| ! isset( $_GET['wc-api'] )
54
-			|| ( 'wc_stripe' !== $_GET['wc-api'] )
52
+		if (('POST' !== $_SERVER['REQUEST_METHOD'])
53
+			|| ! isset($_GET['wc-api'])
54
+			|| ('wc_stripe' !== $_GET['wc-api'])
55 55
 		) {
56 56
 			return;
57 57
 		}
58 58
 
59
-		$request_body    = file_get_contents( 'php://input' );
60
-		$request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER );
59
+		$request_body    = file_get_contents('php://input');
60
+		$request_headers = array_change_key_case($this->get_request_headers(), CASE_UPPER);
61 61
 
62 62
 		// Validate it to make sure it is legit.
63
-		if ( $this->is_valid_request( $request_headers, $request_body ) ) {
64
-			$notification = json_decode( $request_body );
65
-			$this->schedule_cron( $notification );
66
-			status_header( 200 );
63
+		if ($this->is_valid_request($request_headers, $request_body)) {
64
+			$notification = json_decode($request_body);
65
+			$this->schedule_cron($notification);
66
+			status_header(200);
67 67
 			exit;
68 68
 		} else {
69
-			WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) );
70
-			status_header( 400 );
69
+			WC_Stripe_Logger::log('Incoming webhook failed validation: ' . print_r($request_body, true));
70
+			status_header(400);
71 71
 			exit;
72 72
 		}
73 73
 	}
@@ -82,12 +82,12 @@  discard block
 block discarded – undo
82 82
 	 * @param string $request_body The request body from Stripe.
83 83
 	 * @return bool
84 84
 	 */
85
-	public function is_valid_request( $request_headers = null, $request_body = null ) {
86
-		if ( null === $request_headers || null === $request_body ) {
85
+	public function is_valid_request($request_headers = null, $request_body = null) {
86
+		if (null === $request_headers || null === $request_body) {
87 87
 			return false;
88 88
 		}
89 89
 
90
-		if ( ! empty( $request_headers['USER-AGENT'] ) && ! preg_match( '/Stripe/', $request_headers['USER-AGENT'] ) ) {
90
+		if ( ! empty($request_headers['USER-AGENT']) && ! preg_match('/Stripe/', $request_headers['USER-AGENT'])) {
91 91
 			return false;
92 92
 		}
93 93
 
@@ -103,11 +103,11 @@  discard block
 block discarded – undo
103 103
 	 * @version 4.0.0
104 104
 	 */
105 105
 	public function get_request_headers() {
106
-		if ( ! function_exists( 'getallheaders' ) ) {
106
+		if ( ! function_exists('getallheaders')) {
107 107
 			$headers = [];
108
-			foreach ( $_SERVER as $name => $value ) {
109
-				if ( 'HTTP_' === substr( $name, 0, 5 ) ) {
110
-					$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value;
108
+			foreach ($_SERVER as $name => $value) {
109
+				if ('HTTP_' === substr($name, 0, 5)) {
110
+					$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
111 111
 				}
112 112
 			}
113 113
 
@@ -126,30 +126,30 @@  discard block
 block discarded – undo
126 126
 	 * @param object $notification
127 127
 	 * @param bool $retry
128 128
 	 */
129
-	public function process_webhook_payment( $notification, $retry = true ) {
129
+	public function process_webhook_payment($notification, $retry = true) {
130 130
 		// The following 2 payment methods are synchronous so does not need to be handle via webhook.
131
-		if ( 'card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type ) {
131
+		if ('card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type) {
132 132
 			return;
133 133
 		}
134 134
 
135
-		$order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id );
135
+		$order = WC_Stripe_Helper::get_order_by_source_id($notification->data->object->id);
136 136
 
137
-		if ( ! $order ) {
138
-			WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id );
137
+		if ( ! $order) {
138
+			WC_Stripe_Logger::log('Could not find order via source ID: ' . $notification->data->object->id);
139 139
 			return;
140 140
 		}
141 141
 
142 142
 		$order_id  = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
143 143
 		$source_id = $notification->data->object->id;
144 144
 
145
-		$is_pending_receiver = ( 'receiver' === $notification->data->object->flow );
145
+		$is_pending_receiver = ('receiver' === $notification->data->object->flow);
146 146
 
147 147
 		try {
148
-			if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() ) {
148
+			if ('processing' === $order->get_status() || 'completed' === $order->get_status()) {
149 149
 				return;
150 150
 			}
151 151
 
152
-			if ( 'on-hold' === $order->get_status() && ! $is_pending_receiver ) {
152
+			if ('on-hold' === $order->get_status() && ! $is_pending_receiver) {
153 153
 				return;
154 154
 			}
155 155
 
@@ -157,80 +157,80 @@  discard block
 block discarded – undo
157 157
 			$response = null;
158 158
 
159 159
 			// This will throw exception if not valid.
160
-			$this->validate_minimum_order_amount( $order );
160
+			$this->validate_minimum_order_amount($order);
161 161
 
162
-			WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
162
+			WC_Stripe_Logger::log("Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}");
163 163
 
164 164
 			// Prep source object.
165 165
 			$source_object           = new stdClass();
166 166
 			$source_object->token_id = '';
167
-			$source_object->customer = $this->get_stripe_customer_id( $order );
167
+			$source_object->customer = $this->get_stripe_customer_id($order);
168 168
 			$source_object->source   = $source_id;
169 169
 
170 170
 			// Make the request.
171
-			$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) );
171
+			$response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object));
172 172
 
173
-			if ( ! empty( $response->error ) ) {
173
+			if ( ! empty($response->error)) {
174 174
 				// If it is an API error such connection or server, let's retry.
175
-				if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) {
176
-					if ( $retry ) {
177
-						sleep( 5 );
178
-						return $this->process_payment( $order_id, false );
175
+				if ('api_connection_error' === $response->error->type || 'api_error' === $response->error->type) {
176
+					if ($retry) {
177
+						sleep(5);
178
+						return $this->process_payment($order_id, false);
179 179
 					} else {
180 180
 						$localized_message = 'API connection error and retries exhausted.';
181
-						$order->add_order_note( $localized_message );
182
-						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
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_payment( $order_id, false );
197
+					return $this->process_payment($order_id, 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
 	}
@@ -459,7 +459,7 @@  discard block
 block discarded – undo
459 459
 	 * @version 4.0.0
460 460
 	 * @param object $notification
461 461
 	 */
462
-	public function is_partial_capture( $notification ) {
462
+	public function is_partial_capture($notification) {
463 463
 		return 0 < $notification->data->object->amount_refunded;
464 464
 	}
465 465
 
@@ -470,11 +470,11 @@  discard block
 block discarded – undo
470 470
 	 * @version 4.0.0
471 471
 	 * @param object $notification
472 472
 	 */
473
-	public function get_refund_amount( $notification ) {
474
-		if ( $this->is_partial_capture( $notification ) ) {
473
+	public function get_refund_amount($notification) {
474
+		if ($this->is_partial_capture($notification)) {
475 475
 			$amount = $notification->data->object->amount_refunded / 100;
476 476
 
477
-			if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
477
+			if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) {
478 478
 				$amount = $notification->data->object->amount_refunded;
479 479
 			}
480 480
 
@@ -491,12 +491,12 @@  discard block
 block discarded – undo
491 491
 	 * @version 4.0.0
492 492
 	 * @param object $notification
493 493
 	 */
494
-	public function get_partial_amount_to_charge( $notification ) {
495
-		if ( $this->is_partial_capture( $notification ) ) {
496
-			$amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ) / 100;
494
+	public function get_partial_amount_to_charge($notification) {
495
+		if ($this->is_partial_capture($notification)) {
496
+			$amount = ($notification->data->object->amount - $notification->data->object->amount_refunded) / 100;
497 497
 
498
-			if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
499
-				$amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded );
498
+			if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) {
499
+				$amount = ($notification->data->object->amount - $notification->data->object->amount_refunded);
500 500
 			}
501 501
 
502 502
 			return $amount;
@@ -512,34 +512,34 @@  discard block
 block discarded – undo
512 512
 	 * @version 4.0.0
513 513
 	 * @param string $notification
514 514
 	 */
515
-	public function process_webhook( $notification ) {
516
-		switch ( $notification->type ) {
515
+	public function process_webhook($notification) {
516
+		switch ($notification->type) {
517 517
 			case 'source.chargeable':
518
-				$this->process_webhook_payment( $notification );
518
+				$this->process_webhook_payment($notification);
519 519
 				break;
520 520
 
521 521
 			case 'source.canceled':
522
-				$this->process_webhook_source_canceled( $notification );
522
+				$this->process_webhook_source_canceled($notification);
523 523
 				break;
524 524
 
525 525
 			case 'charge.succeeded':
526
-				$this->process_webhook_charge_succeeded( $notification );
526
+				$this->process_webhook_charge_succeeded($notification);
527 527
 				break;
528 528
 
529 529
 			case 'charge.failed':
530
-				$this->process_webhook_charge_failed( $notification );
530
+				$this->process_webhook_charge_failed($notification);
531 531
 				break;
532 532
 
533 533
 			case 'charge.captured':
534
-				$this->process_webhook_capture( $notification );
534
+				$this->process_webhook_capture($notification);
535 535
 				break;
536 536
 
537 537
 			case 'charge.dispute.created':
538
-				$this->process_webhook_dispute( $notification );
538
+				$this->process_webhook_dispute($notification);
539 539
 				break;
540 540
 
541 541
 			case 'charge.refunded':
542
-				$this->process_webhook_refund( $notification );
542
+				$this->process_webhook_refund($notification);
543 543
 				break;
544 544
 
545 545
 		}
Please login to merge, or discard this patch.
includes/class-wc-stripe-api.php 1 patch
Spacing   +26 added lines, -26 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
 
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 	 * Set secret API Key.
27 27
 	 * @param string $key
28 28
 	 */
29
-	public static function set_secret_key( $secret_key ) {
29
+	public static function set_secret_key($secret_key) {
30 30
 		self::$secret_key = $secret_key;
31 31
 	}
32 32
 
@@ -35,11 +35,11 @@  discard block
 block discarded – undo
35 35
 	 * @return string
36 36
 	 */
37 37
 	public static function get_secret_key() {
38
-		if ( ! self::$secret_key ) {
39
-			$options = get_option( 'woocommerce_stripe_settings' );
38
+		if ( ! self::$secret_key) {
39
+			$options = get_option('woocommerce_stripe_settings');
40 40
 
41
-			if ( isset( $options['testmode'], $options['secret_key'], $options['test_secret_key'] ) ) {
42
-				self::set_secret_key( 'yes' === $options['testmode'] ? $options['test_secret_key'] : $options['secret_key'] );
41
+			if (isset($options['testmode'], $options['secret_key'], $options['test_secret_key'])) {
42
+				self::set_secret_key('yes' === $options['testmode'] ? $options['test_secret_key'] : $options['secret_key']);
43 43
 			}
44 44
 		}
45 45
 		return self::$secret_key;
@@ -78,12 +78,12 @@  discard block
 block discarded – undo
78 78
 		$user_agent = self::get_user_agent();
79 79
 		$app_info   = $user_agent['application'];
80 80
 
81
-		return apply_filters( 'woocommerce_stripe_request_headers', array(
82
-			'Authorization'              => 'Basic ' . base64_encode( self::get_secret_key() . ':' ),
81
+		return apply_filters('woocommerce_stripe_request_headers', array(
82
+			'Authorization'              => 'Basic ' . base64_encode(self::get_secret_key() . ':'),
83 83
 			'Stripe-Version'             => self::STRIPE_API_VERSION,
84 84
 			'User-Agent'                 => $app_info['name'] . '/' . $app_info['version'] . ' (' . $app_info['url'] . ')',
85
-			'X-Stripe-Client-User-Agent' => json_encode( $user_agent ),
86
-		) );
85
+			'X-Stripe-Client-User-Agent' => json_encode($user_agent),
86
+		));
87 87
 	}
88 88
 
89 89
 	/**
@@ -95,14 +95,14 @@  discard block
 block discarded – undo
95 95
 	 * @param string $api
96 96
 	 * @return array|WP_Error
97 97
 	 */
98
-	public static function request( $request, $api = 'charges', $method = 'POST' ) {
99
-		WC_Stripe_Logger::log( "{$api} request: " . print_r( $request, true ) );
98
+	public static function request($request, $api = 'charges', $method = 'POST') {
99
+		WC_Stripe_Logger::log("{$api} request: " . print_r($request, true));
100 100
 
101 101
 		$headers = self::get_headers();
102 102
 
103
-		if ( 'charges' === $api && 'POST' === $method ) {
104
-			$customer = ! empty( $request['customer'] ) ? $request['customer'] : '';
105
-			$source   = ! empty( $request['source'] ) ? $request['source'] : $customer;
103
+		if ('charges' === $api && 'POST' === $method) {
104
+			$customer = ! empty($request['customer']) ? $request['customer'] : '';
105
+			$source   = ! empty($request['source']) ? $request['source'] : $customer;
106 106
 
107 107
 			$headers['Idempotency-Key'] = $request['metadata']['order_id'] . '-' . $source;
108 108
 		}
@@ -112,17 +112,17 @@  discard block
 block discarded – undo
112 112
 			array(
113 113
 				'method'  => $method,
114 114
 				'headers' => $headers,
115
-				'body'    => apply_filters( 'woocommerce_stripe_request_body', $request, $api ),
115
+				'body'    => apply_filters('woocommerce_stripe_request_body', $request, $api),
116 116
 				'timeout' => 70,
117 117
 			)
118 118
 		);
119 119
 
120
-		if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
121
-			WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) );
122
-			throw new WC_Stripe_Exception( print_r( $response, true ), __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) );
120
+		if (is_wp_error($response) || empty($response['body'])) {
121
+			WC_Stripe_Logger::log('Error Response: ' . print_r($response, true));
122
+			throw new WC_Stripe_Exception(print_r($response, true), __('There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe'));
123 123
 		}
124 124
 
125
-		return json_decode( $response['body'] );
125
+		return json_decode($response['body']);
126 126
 	}
127 127
 
128 128
 	/**
@@ -132,8 +132,8 @@  discard block
 block discarded – undo
132 132
 	 * @version 4.0.0
133 133
 	 * @param string $api
134 134
 	 */
135
-	public static function retrieve( $api ) {
136
-		WC_Stripe_Logger::log( "{$api}" );
135
+	public static function retrieve($api) {
136
+		WC_Stripe_Logger::log("{$api}");
137 137
 
138 138
 		$response = wp_safe_remote_get(
139 139
 			self::ENDPOINT . $api,
@@ -144,11 +144,11 @@  discard block
 block discarded – undo
144 144
 			)
145 145
 		);
146 146
 
147
-		if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
148
-			WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) );
149
-			return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) );
147
+		if (is_wp_error($response) || empty($response['body'])) {
148
+			WC_Stripe_Logger::log('Error Response: ' . print_r($response, true));
149
+			return new WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe'));
150 150
 		}
151 151
 
152
-		return json_decode( $response['body'] );
152
+		return json_decode($response['body']);
153 153
 	}
154 154
 }
Please login to merge, or discard this patch.