@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -17,7 +17,7 @@ discard block |
||
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' ) ); |
|
20 | + add_action('woocommerce_api_wc_stripe', array($this, 'check_for_webhook')); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
@@ -27,24 +27,24 @@ discard block |
||
27 | 27 | * @version 4.0.0 |
28 | 28 | */ |
29 | 29 | public function check_for_webhook() { |
30 | - if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) |
|
31 | - || ! isset( $_GET['wc-api'] ) |
|
32 | - || ( 'wc_stripe' !== $_GET['wc-api'] ) |
|
30 | + if (('POST' !== $_SERVER['REQUEST_METHOD']) |
|
31 | + || ! isset($_GET['wc-api']) |
|
32 | + || ('wc_stripe' !== $_GET['wc-api']) |
|
33 | 33 | ) { |
34 | 34 | return; |
35 | 35 | } |
36 | 36 | |
37 | - $request_body = file_get_contents( 'php://input' ); |
|
38 | - $request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER ); |
|
37 | + $request_body = file_get_contents('php://input'); |
|
38 | + $request_headers = array_change_key_case($this->get_request_headers(), CASE_UPPER); |
|
39 | 39 | |
40 | 40 | // Validate it to make sure it is legit. |
41 | - if ( $this->is_valid_request( $request_headers, $request_body ) ) { |
|
42 | - $this->process_webhook( $request_body ); |
|
43 | - status_header( 200 ); |
|
41 | + if ($this->is_valid_request($request_headers, $request_body)) { |
|
42 | + $this->process_webhook($request_body); |
|
43 | + status_header(200); |
|
44 | 44 | exit; |
45 | 45 | } else { |
46 | - WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) ); |
|
47 | - status_header( 400 ); |
|
46 | + WC_Stripe_Logger::log('Incoming webhook failed validation: ' . print_r($request_body, true)); |
|
47 | + status_header(400); |
|
48 | 48 | exit; |
49 | 49 | } |
50 | 50 | } |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | * @param string $request_body The request body from Stripe. |
60 | 60 | * @return bool |
61 | 61 | */ |
62 | - public function is_valid_request( $request_headers = null, $request_body = null ) { |
|
63 | - if ( null === $request_headers || null === $request_body ) { |
|
62 | + public function is_valid_request($request_headers = null, $request_body = null) { |
|
63 | + if (null === $request_headers || null === $request_body) { |
|
64 | 64 | return false; |
65 | 65 | } |
66 | 66 | |
67 | - if ( ! empty( $request_headers['USER-AGENT'] ) && ! preg_match( '/Stripe/', $request_headers['USER-AGENT'] ) ) { |
|
67 | + if ( ! empty($request_headers['USER-AGENT']) && ! preg_match('/Stripe/', $request_headers['USER-AGENT'])) { |
|
68 | 68 | return false; |
69 | 69 | } |
70 | 70 | |
@@ -80,11 +80,11 @@ discard block |
||
80 | 80 | * @version 4.0.0 |
81 | 81 | */ |
82 | 82 | public function get_request_headers() { |
83 | - if ( ! function_exists( 'getallheaders' ) ) { |
|
83 | + if ( ! function_exists('getallheaders')) { |
|
84 | 84 | $headers = []; |
85 | - foreach ( $_SERVER as $name => $value ) { |
|
86 | - if ( 'HTTP_' === substr( $name, 0, 5 ) ) { |
|
87 | - $headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value; |
|
85 | + foreach ($_SERVER as $name => $value) { |
|
86 | + if ('HTTP_' === substr($name, 0, 5)) { |
|
87 | + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
@@ -103,30 +103,30 @@ discard block |
||
103 | 103 | * @param object $notification |
104 | 104 | * @param bool $retry |
105 | 105 | */ |
106 | - public function process_webhook_payment( $notification, $retry = true ) { |
|
106 | + public function process_webhook_payment($notification, $retry = true) { |
|
107 | 107 | // The following 2 payment methods are synchronous so does not need to be handle via webhook. |
108 | - if ( 'card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type ) { |
|
108 | + if ('card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type) { |
|
109 | 109 | return; |
110 | 110 | } |
111 | 111 | |
112 | - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); |
|
112 | + $order = WC_Stripe_Helper::get_order_by_source_id($notification->data->object->id); |
|
113 | 113 | |
114 | - if ( ! $order ) { |
|
115 | - WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id ); |
|
114 | + if ( ! $order) { |
|
115 | + WC_Stripe_Logger::log('Could not find order via source ID: ' . $notification->data->object->id); |
|
116 | 116 | return; |
117 | 117 | } |
118 | 118 | |
119 | 119 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
120 | 120 | $source_id = $notification->data->object->id; |
121 | 121 | |
122 | - $is_pending_receiver = ( 'receiver' === $notification->data->object->flow ); |
|
122 | + $is_pending_receiver = ('receiver' === $notification->data->object->flow); |
|
123 | 123 | |
124 | 124 | try { |
125 | - if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() ) { |
|
125 | + if ('processing' === $order->get_status() || 'completed' === $order->get_status()) { |
|
126 | 126 | return; |
127 | 127 | } |
128 | 128 | |
129 | - if ( 'on-hold' === $order->get_status() && ! $is_pending_receiver ) { |
|
129 | + if ('on-hold' === $order->get_status() && ! $is_pending_receiver) { |
|
130 | 130 | return; |
131 | 131 | } |
132 | 132 | |
@@ -134,80 +134,80 @@ discard block |
||
134 | 134 | $response = null; |
135 | 135 | |
136 | 136 | // This will throw exception if not valid. |
137 | - $this->validate_minimum_order_amount( $order ); |
|
137 | + $this->validate_minimum_order_amount($order); |
|
138 | 138 | |
139 | - WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
139 | + WC_Stripe_Logger::log("Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
140 | 140 | |
141 | 141 | // Prep source object. |
142 | 142 | $source_object = new stdClass(); |
143 | 143 | $source_object->token_id = ''; |
144 | - $source_object->customer = $this->get_stripe_customer_id( $order ); |
|
144 | + $source_object->customer = $this->get_stripe_customer_id($order); |
|
145 | 145 | $source_object->source = $source_id; |
146 | 146 | |
147 | 147 | // Make the request. |
148 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) ); |
|
148 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object)); |
|
149 | 149 | |
150 | - if ( ! empty( $response->error ) ) { |
|
150 | + if ( ! empty($response->error)) { |
|
151 | 151 | // If it is an API error such connection or server, let's retry. |
152 | - if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) { |
|
153 | - if ( $retry ) { |
|
154 | - sleep( 5 ); |
|
155 | - return $this->process_payment( $order_id, false ); |
|
152 | + if ('api_connection_error' === $response->error->type || 'api_error' === $response->error->type) { |
|
153 | + if ($retry) { |
|
154 | + sleep(5); |
|
155 | + return $this->process_payment($order_id, false); |
|
156 | 156 | } else { |
157 | 157 | $localized_message = 'API connection error and retries exhausted.'; |
158 | - $order->add_order_note( $localized_message ); |
|
159 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
158 | + $order->add_order_note($localized_message); |
|
159 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
163 | 163 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
164 | - if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) { |
|
165 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
166 | - delete_user_meta( $order->customer_user, '_stripe_customer_id' ); |
|
167 | - delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
164 | + if (preg_match('/No such customer/i', $response->error->message) && $retry) { |
|
165 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
166 | + delete_user_meta($order->customer_user, '_stripe_customer_id'); |
|
167 | + delete_post_meta($order_id, '_stripe_customer_id'); |
|
168 | 168 | } else { |
169 | - delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' ); |
|
170 | - $order->delete_meta_data( '_stripe_customer_id' ); |
|
169 | + delete_user_meta($order->get_customer_id(), '_stripe_customer_id'); |
|
170 | + $order->delete_meta_data('_stripe_customer_id'); |
|
171 | 171 | $order->save(); |
172 | 172 | } |
173 | 173 | |
174 | - return $this->process_payment( $order_id, false ); |
|
174 | + return $this->process_payment($order_id, false); |
|
175 | 175 | |
176 | - } elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) { |
|
176 | + } elseif (preg_match('/No such token/i', $response->error->message) && $source_object->token_id) { |
|
177 | 177 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
178 | - $wc_token = WC_Payment_Tokens::get( $source_object->token_id ); |
|
178 | + $wc_token = WC_Payment_Tokens::get($source_object->token_id); |
|
179 | 179 | $wc_token->delete(); |
180 | - $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
|
181 | - $order->add_order_note( $message ); |
|
182 | - throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
180 | + $message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe'); |
|
181 | + $order->add_order_note($message); |
|
182 | + throw new WC_Stripe_Exception(print_r($response, true), $message); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
186 | 186 | |
187 | - if ( 'card_error' === $response->error->type ) { |
|
188 | - $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
187 | + if ('card_error' === $response->error->type) { |
|
188 | + $localized_message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message; |
|
189 | 189 | } else { |
190 | - $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
190 | + $localized_message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message; |
|
191 | 191 | } |
192 | 192 | |
193 | - $order->add_order_note( $localized_message ); |
|
193 | + $order->add_order_note($localized_message); |
|
194 | 194 | |
195 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
195 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
196 | 196 | } |
197 | 197 | |
198 | - do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order ); |
|
198 | + do_action('wc_gateway_stripe_process_webhook_payment', $response, $order); |
|
199 | 199 | |
200 | - $this->process_response( $response, $order ); |
|
200 | + $this->process_response($response, $order); |
|
201 | 201 | |
202 | - } catch ( WC_Stripe_Exception $e ) { |
|
203 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
202 | + } catch (WC_Stripe_Exception $e) { |
|
203 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
204 | 204 | |
205 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $e, $order ); |
|
205 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $e, $order); |
|
206 | 206 | |
207 | - $statuses = array( 'pending', 'failed' ); |
|
207 | + $statuses = array('pending', 'failed'); |
|
208 | 208 | |
209 | - if ( $order->has_status( $statuses ) ) { |
|
210 | - $this->send_failed_order_email( $order_id ); |
|
209 | + if ($order->has_status($statuses)) { |
|
210 | + $this->send_failed_order_email($order_id); |
|
211 | 211 | } |
212 | 212 | } |
213 | 213 | } |
@@ -221,18 +221,18 @@ discard block |
||
221 | 221 | * @version 4.0.0 |
222 | 222 | * @param object $notification |
223 | 223 | */ |
224 | - public function process_webhook_dispute( $notification ) { |
|
225 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
224 | + public function process_webhook_dispute($notification) { |
|
225 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
226 | 226 | |
227 | - if ( ! $order ) { |
|
228 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
227 | + if ( ! $order) { |
|
228 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
229 | 229 | return; |
230 | 230 | } |
231 | 231 | |
232 | - $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' ) ); |
|
232 | + $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')); |
|
233 | 233 | |
234 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification ); |
|
235 | - $this->send_failed_order_email( $order_id ); |
|
234 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification); |
|
235 | + $this->send_failed_order_email($order_id); |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | /** |
@@ -243,41 +243,41 @@ discard block |
||
243 | 243 | * @version 4.0.0 |
244 | 244 | * @param object $notification |
245 | 245 | */ |
246 | - public function process_webhook_capture( $notification ) { |
|
247 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
246 | + public function process_webhook_capture($notification) { |
|
247 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
248 | 248 | |
249 | - if ( ! $order ) { |
|
250 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
249 | + if ( ! $order) { |
|
250 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
251 | 251 | return; |
252 | 252 | } |
253 | 253 | |
254 | 254 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
255 | 255 | |
256 | - if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) { |
|
257 | - $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id(); |
|
258 | - $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true ); |
|
256 | + if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) { |
|
257 | + $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id(); |
|
258 | + $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true); |
|
259 | 259 | |
260 | - if ( $charge && 'no' === $captured ) { |
|
261 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' ); |
|
260 | + if ($charge && 'no' === $captured) { |
|
261 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', 'yes') : $order->update_meta_data('_stripe_charge_captured', 'yes'); |
|
262 | 262 | |
263 | 263 | // Store other data such as fees |
264 | - 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 ); |
|
264 | + 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); |
|
265 | 265 | |
266 | - if ( isset( $notification->data->object->balance_transaction ) ) { |
|
267 | - $this->update_fees( $order, $notification->data->object->balance_transaction ); |
|
266 | + if (isset($notification->data->object->balance_transaction)) { |
|
267 | + $this->update_fees($order, $notification->data->object->balance_transaction); |
|
268 | 268 | } |
269 | 269 | |
270 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
270 | + if (is_callable(array($order, 'save'))) { |
|
271 | 271 | $order->save(); |
272 | 272 | } |
273 | 273 | |
274 | 274 | /* translators: transaction id */ |
275 | - $order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) ); |
|
275 | + $order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id)); |
|
276 | 276 | |
277 | 277 | // Check and see if capture is partial. |
278 | - if ( $this->is_partial_capture( $notification ) ) { |
|
279 | - $order->set_total( $this->get_partial_amount_to_charge( $notification ) ); |
|
280 | - $order->add_note( __( 'This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe' ) ); |
|
278 | + if ($this->is_partial_capture($notification)) { |
|
279 | + $order->set_total($this->get_partial_amount_to_charge($notification)); |
|
280 | + $order->add_note(__('This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe')); |
|
281 | 281 | $order->save(); |
282 | 282 | } |
283 | 283 | } |
@@ -292,38 +292,38 @@ discard block |
||
292 | 292 | * @version 4.0.0 |
293 | 293 | * @param object $notification |
294 | 294 | */ |
295 | - public function process_webhook_charge_succeeded( $notification ) { |
|
295 | + public function process_webhook_charge_succeeded($notification) { |
|
296 | 296 | // The following payment methods are synchronous so does not need to be handle via webhook. |
297 | - 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 ) ) { |
|
297 | + 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)) { |
|
298 | 298 | return; |
299 | 299 | } |
300 | 300 | |
301 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
301 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
302 | 302 | |
303 | - if ( ! $order ) { |
|
304 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
303 | + if ( ! $order) { |
|
304 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
305 | 305 | return; |
306 | 306 | } |
307 | 307 | |
308 | 308 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
309 | 309 | |
310 | - if ( 'on-hold' !== $order->get_status() ) { |
|
310 | + if ('on-hold' !== $order->get_status()) { |
|
311 | 311 | return; |
312 | 312 | } |
313 | 313 | |
314 | 314 | // Store other data such as fees |
315 | - 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 ); |
|
315 | + 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); |
|
316 | 316 | |
317 | - if ( isset( $notification->data->object->balance_transaction ) ) { |
|
318 | - $this->update_fees( $order, $notification->data->object->balance_transaction ); |
|
317 | + if (isset($notification->data->object->balance_transaction)) { |
|
318 | + $this->update_fees($order, $notification->data->object->balance_transaction); |
|
319 | 319 | } |
320 | 320 | |
321 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
321 | + if (is_callable(array($order, 'save'))) { |
|
322 | 322 | $order->save(); |
323 | 323 | } |
324 | 324 | |
325 | 325 | /* translators: transaction id */ |
326 | - $order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) ); |
|
326 | + $order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id)); |
|
327 | 327 | } |
328 | 328 | |
329 | 329 | /** |
@@ -334,23 +334,23 @@ discard block |
||
334 | 334 | * @version 4.0.0 |
335 | 335 | * @param object $notification |
336 | 336 | */ |
337 | - public function process_webhook_charge_failed( $notification ) { |
|
338 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
337 | + public function process_webhook_charge_failed($notification) { |
|
338 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
339 | 339 | |
340 | - if ( ! $order ) { |
|
341 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
340 | + if ( ! $order) { |
|
341 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
342 | 342 | return; |
343 | 343 | } |
344 | 344 | |
345 | 345 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
346 | 346 | |
347 | - if ( 'on-hold' !== $order->get_status() ) { |
|
347 | + if ('on-hold' !== $order->get_status()) { |
|
348 | 348 | return; |
349 | 349 | } |
350 | 350 | |
351 | - $order->update_status( 'failed', __( 'This payment failed to clear.', 'woocommerce-gateway-stripe' ) ); |
|
351 | + $order->update_status('failed', __('This payment failed to clear.', 'woocommerce-gateway-stripe')); |
|
352 | 352 | |
353 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification ); |
|
353 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification); |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | /** |
@@ -361,23 +361,23 @@ discard block |
||
361 | 361 | * @version 4.0.0 |
362 | 362 | * @param object $notification |
363 | 363 | */ |
364 | - public function process_webhook_source_canceled( $notification ) { |
|
365 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
364 | + public function process_webhook_source_canceled($notification) { |
|
365 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
366 | 366 | |
367 | - if ( ! $order ) { |
|
368 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
367 | + if ( ! $order) { |
|
368 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
369 | 369 | return; |
370 | 370 | } |
371 | 371 | |
372 | 372 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
373 | 373 | |
374 | - if ( 'on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status() ) { |
|
374 | + if ('on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status()) { |
|
375 | 375 | return; |
376 | 376 | } |
377 | 377 | |
378 | - $order->update_status( 'cancelled', __( 'This payment has cancelled.', 'woocommerce-gateway-stripe' ) ); |
|
378 | + $order->update_status('cancelled', __('This payment has cancelled.', 'woocommerce-gateway-stripe')); |
|
379 | 379 | |
380 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification ); |
|
380 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | /** |
@@ -388,37 +388,37 @@ discard block |
||
388 | 388 | * @version 4.0.0 |
389 | 389 | * @param object $notification |
390 | 390 | */ |
391 | - public function process_webhook_refund( $notification ) { |
|
392 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
391 | + public function process_webhook_refund($notification) { |
|
392 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
393 | 393 | |
394 | - if ( ! $order ) { |
|
395 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
394 | + if ( ! $order) { |
|
395 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
396 | 396 | return; |
397 | 397 | } |
398 | 398 | |
399 | 399 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
400 | 400 | |
401 | - if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) { |
|
402 | - $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id(); |
|
403 | - $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true ); |
|
404 | - $refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_refund_id', true ) : $order->get_meta( '_stripe_refund_id', true ); |
|
401 | + if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) { |
|
402 | + $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id(); |
|
403 | + $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true); |
|
404 | + $refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_refund_id', true) : $order->get_meta('_stripe_refund_id', true); |
|
405 | 405 | |
406 | 406 | // If the refund ID matches, don't continue to prevent double refunding. |
407 | - if ( $notification->data->object->refunds->data[0]->id === $refund_id ) { |
|
407 | + if ($notification->data->object->refunds->data[0]->id === $refund_id) { |
|
408 | 408 | return; |
409 | 409 | } |
410 | 410 | |
411 | 411 | // Only refund captured charge. |
412 | - if ( $charge && 'yes' === $captured ) { |
|
412 | + if ($charge && 'yes' === $captured) { |
|
413 | 413 | // Create the refund. |
414 | - $refund = wc_create_refund( array( |
|
414 | + $refund = wc_create_refund(array( |
|
415 | 415 | 'order_id' => $order_id, |
416 | - 'amount' => $this->get_refund_amount( $notification ), |
|
417 | - 'reason' => __( 'Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe' ), |
|
418 | - ) ); |
|
416 | + 'amount' => $this->get_refund_amount($notification), |
|
417 | + 'reason' => __('Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe'), |
|
418 | + )); |
|
419 | 419 | |
420 | - if ( is_wp_error( $refund ) ) { |
|
421 | - WC_Stripe_Logger::log( $refund->get_error_message() ); |
|
420 | + if (is_wp_error($refund)) { |
|
421 | + WC_Stripe_Logger::log($refund->get_error_message()); |
|
422 | 422 | } |
423 | 423 | } |
424 | 424 | } |
@@ -431,7 +431,7 @@ discard block |
||
431 | 431 | * @version 4.0.0 |
432 | 432 | * @param object $notification |
433 | 433 | */ |
434 | - public function is_partial_capture( $notification ) { |
|
434 | + public function is_partial_capture($notification) { |
|
435 | 435 | return 0 < $notification->data->object->amount_refunded; |
436 | 436 | } |
437 | 437 | |
@@ -442,11 +442,11 @@ discard block |
||
442 | 442 | * @version 4.0.0 |
443 | 443 | * @param object $notification |
444 | 444 | */ |
445 | - public function get_refund_amount( $notification ) { |
|
446 | - if ( $this->is_partial_capture( $notification ) ) { |
|
445 | + public function get_refund_amount($notification) { |
|
446 | + if ($this->is_partial_capture($notification)) { |
|
447 | 447 | $amount = $notification->data->object->amount_refunded / 100; |
448 | 448 | |
449 | - if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
449 | + if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) { |
|
450 | 450 | $amount = $notification->data->object->amount_refunded; |
451 | 451 | } |
452 | 452 | |
@@ -463,12 +463,12 @@ discard block |
||
463 | 463 | * @version 4.0.0 |
464 | 464 | * @param object $notification |
465 | 465 | */ |
466 | - public function get_partial_amount_to_charge( $notification ) { |
|
467 | - if ( $this->is_partial_capture( $notification ) ) { |
|
468 | - $amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ) / 100; |
|
466 | + public function get_partial_amount_to_charge($notification) { |
|
467 | + if ($this->is_partial_capture($notification)) { |
|
468 | + $amount = ($notification->data->object->amount - $notification->data->object->amount_refunded) / 100; |
|
469 | 469 | |
470 | - if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
471 | - $amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ); |
|
470 | + if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) { |
|
471 | + $amount = ($notification->data->object->amount - $notification->data->object->amount_refunded); |
|
472 | 472 | } |
473 | 473 | |
474 | 474 | return $amount; |
@@ -484,43 +484,43 @@ discard block |
||
484 | 484 | * @version 4.0.0 |
485 | 485 | * @param string $request_body |
486 | 486 | */ |
487 | - public function process_webhook( $request_body ) { |
|
488 | - $notification = json_decode( $request_body ); |
|
487 | + public function process_webhook($request_body) { |
|
488 | + $notification = json_decode($request_body); |
|
489 | 489 | |
490 | 490 | /* |
491 | 491 | * Hacky way to possibly prevent duplicate requests due to |
492 | 492 | * frontend request and webhook payment firing at the same |
493 | 493 | * time. |
494 | 494 | */ |
495 | - sleep( 10 ); |
|
495 | + sleep(10); |
|
496 | 496 | |
497 | - switch ( $notification->type ) { |
|
497 | + switch ($notification->type) { |
|
498 | 498 | case 'source.chargeable': |
499 | - $this->process_webhook_payment( $notification ); |
|
499 | + $this->process_webhook_payment($notification); |
|
500 | 500 | break; |
501 | 501 | |
502 | 502 | case 'source.canceled': |
503 | - $this->process_webhook_source_canceled( $notification ); |
|
503 | + $this->process_webhook_source_canceled($notification); |
|
504 | 504 | break; |
505 | 505 | |
506 | 506 | case 'charge.succeeded': |
507 | - $this->process_webhook_charge_succeeded( $notification ); |
|
507 | + $this->process_webhook_charge_succeeded($notification); |
|
508 | 508 | break; |
509 | 509 | |
510 | 510 | case 'charge.failed': |
511 | - $this->process_webhook_charge_failed( $notification ); |
|
511 | + $this->process_webhook_charge_failed($notification); |
|
512 | 512 | break; |
513 | 513 | |
514 | 514 | case 'charge.captured': |
515 | - $this->process_webhook_capture( $notification ); |
|
515 | + $this->process_webhook_capture($notification); |
|
516 | 516 | break; |
517 | 517 | |
518 | 518 | case 'charge.dispute.created': |
519 | - $this->process_webhook_dispute( $notification ); |
|
519 | + $this->process_webhook_dispute($notification); |
|
520 | 520 | break; |
521 | 521 | |
522 | 522 | case 'charge.refunded': |
523 | - $this->process_webhook_refund( $notification ); |
|
523 | + $this->process_webhook_refund($notification); |
|
524 | 524 | break; |
525 | 525 | |
526 | 526 | } |