From eb4fc639fcff330d4ab5ca9e3c782e4cfd0c4176 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 20 Jul 2022 12:16:33 +0200 Subject: [PATCH] Fix psalm --- .../src/Endpoint/ReturnUrlEndpoint.php | 2 +- .../ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php | 4 +- .../src/Handler/PaymentCapturePending.php | 58 +++++++++---------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php b/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php index 0270dae48..0fdc07954 100644 --- a/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php +++ b/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php @@ -69,7 +69,7 @@ class ReturnUrlEndpoint { } $wc_order = wc_get_order( $wc_order_id ); - if ( ! $wc_order ) { + if ( ! is_a( $wc_order, \WC_Order::class ) ) { exit(); } diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php index 7fbcd450d..4d7936286 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php @@ -114,7 +114,7 @@ class OXXO { add_filter( 'ppcp_payment_capture_reversed_webhook_update_status_note', - function( $note, $wc_order, $event_type ) { + function( string $note, WC_Order $wc_order, string $event_type ): string { if ( $wc_order->get_payment_method() === OXXOGateway::ID && $event_type === 'PAYMENT.CAPTURE.DENIED' ) { $note = __( 'OXXO voucher has expired or the buyer didn\'t complete the payment successfully.', 'woocommerce-paypal-payments' ); } @@ -122,7 +122,7 @@ class OXXO { return $note; }, 10, - 2 + 3 ); } diff --git a/modules/ppcp-webhooks/src/Handler/PaymentCapturePending.php b/modules/ppcp-webhooks/src/Handler/PaymentCapturePending.php index 92507c9f3..1467e663f 100644 --- a/modules/ppcp-webhooks/src/Handler/PaymentCapturePending.php +++ b/modules/ppcp-webhooks/src/Handler/PaymentCapturePending.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Webhooks\Handler; use Psr\Log\LoggerInterface; +use WP_REST_Request; use WP_REST_Response; /** @@ -60,12 +61,35 @@ class PaymentCapturePending implements RequestHandler { /** * Responsible for handling the request. * - * @param \WP_REST_Request $request The request. + * @param WP_REST_Request $request The request. * * @return WP_REST_Response */ - public function handle_request( \WP_REST_Request $request ): WP_REST_Response { + public function handle_request( WP_REST_Request $request ): WP_REST_Response { $response = array( 'success' => false ); + $order_id = $request['resource'] !== null && isset( $request['resource']['custom_id'] ) + ? $this->sanitize_custom_id( $request['resource']['custom_id'] ) + : 0; + if ( ! $order_id ) { + $message = sprintf( + // translators: %s is the PayPal webhook Id. + __( + 'No order for webhook event %s was found.', + 'woocommerce-paypal-payments' + ), + $request['id'] !== null && isset( $request['id'] ) ? $request['id'] : '' + ); + $this->logger->log( + 'warning', + $message, + array( + 'request' => $request, + ) + ); + $response['message'] = $message; + return new WP_REST_Response( $response ); + } + $resource = $request['resource']; if ( ! is_array( $resource ) ) { $message = 'Resource data not found in webhook request.'; @@ -74,36 +98,12 @@ class PaymentCapturePending implements RequestHandler { return new WP_REST_Response( $response ); } - $order_id = isset( $request['resource']['custom_id'] ) - ? $this->sanitize_custom_id( $request['resource']['custom_id'] ) - : 0; - - if ( ! $order_id ) { - $message = sprintf( - // translators: %s is the PayPal webhook Id. - __( - 'No order for webhook event %s was found.', - 'woocommerce-paypal-payments' - ), - isset( $request['id'] ) ? $request['id'] : '' - ); - $this->logger->log( - 'warning', - $message, - array( - 'request' => $request, - ) - ); - $response['message'] = $message; - return rest_ensure_response( $response ); - } - $wc_order = wc_get_order( $order_id ); if ( ! is_a( $wc_order, \WC_Order::class ) ) { $message = sprintf( // translators: %s is the PayPal refund Id. __( 'Order for PayPal refund %s not found.', 'woocommerce-paypal-payments' ), - isset( $request['resource']['id'] ) ? $request['resource']['id'] : '' + $request['resource'] !== null && isset( $request['resource']['id'] ) ? $request['resource']['id'] : '' ); $this->logger->log( 'warning', @@ -113,11 +113,11 @@ class PaymentCapturePending implements RequestHandler { ) ); $response['message'] = $message; - return rest_ensure_response( $response ); + return new WP_REST_Response( $response ); } if ( $wc_order->get_status() === 'pending' ) { - $wc_order->update_status('on-hold', __('Payment initiation was successful, and is waiting for the buyer to complete the payment.', 'woocommerce-paypal-payments')); + $wc_order->update_status( 'on-hold', __( 'Payment initiation was successful, and is waiting for the buyer to complete the payment.', 'woocommerce-paypal-payments' ) ); }