From 2d48166eb15bb4f9c27d86c3998943c6db64a1d4 Mon Sep 17 00:00:00 2001 From: David Remer Date: Tue, 25 Aug 2020 15:13:56 +0300 Subject: [PATCH] phpcs --- .../ppcp-button/src/Assets/SmartButton.php | 5 +- .../ppcp-button/src/ButtonModule.php | 9 ++-- .../src/Endpoint/CreateOrderEndpoint.php | 53 ++++++++++++------- .../src/Helper/EarlyOrderHandler.php | 35 ++++++------ .../src/Gateway/PayPalGateway.php | 2 + .../src/Processor/OrderProcessor.php | 5 +- .../src/Handler/CheckoutOrderApproved.php | 13 ++--- .../src/Handler/CheckoutOrderCompleted.php | 2 +- 8 files changed, 77 insertions(+), 47 deletions(-) diff --git a/modules.local/ppcp-button/src/Assets/SmartButton.php b/modules.local/ppcp-button/src/Assets/SmartButton.php index c537f5110..3d2ee9620 100644 --- a/modules.local/ppcp-button/src/Assets/SmartButton.php +++ b/modules.local/ppcp-button/src/Assets/SmartButton.php @@ -533,7 +533,10 @@ class SmartButton implements SmartButtonInterface 'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture', ]; - if (defined('WP_DEBUG') && \WP_DEBUG && WC()->customer && WC()->customer->get_billing_country()) { + if ( + defined('WP_DEBUG') && \WP_DEBUG + && WC()->customer && WC()->customer->get_billing_country() + ) { $params['buyer-country'] = WC()->customer->get_billing_country(); } $payee = $this->payeeRepository->payee(); diff --git a/modules.local/ppcp-button/src/ButtonModule.php b/modules.local/ppcp-button/src/ButtonModule.php index cc21f935a..595a9bb00 100644 --- a/modules.local/ppcp-button/src/ButtonModule.php +++ b/modules.local/ppcp-button/src/ButtonModule.php @@ -51,10 +51,13 @@ class ButtonModule implements ModuleInterface $smartButton->enqueue(); }); - add_action( + add_filter( 'woocommerce_create_order', static function ($value) use ($container) { $earlyOrderHelper = $container->get('button.helper.early-order-handler'); + if (! is_null($value)) { + $value = (int) $value; + } /** * @var EarlyOrderHandler $earlyOrderHelper */ @@ -65,8 +68,8 @@ class ButtonModule implements ModuleInterface $this->registerAjaxEndpoints($container); } - private function registerAjaxEndpoints(ContainerInterface $container) { - + private function registerAjaxEndpoints(ContainerInterface $container) + { add_action( 'wc_ajax_' . DataClientIdEndpoint::ENDPOINT, static function () use ($container) { diff --git a/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index b7a78efd5..f51e31ec9 100644 --- a/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -29,6 +29,8 @@ class CreateOrderEndpoint implements EndpointInterface private $sessionHandler; private $settings; private $earlyOrderHandler; + + private $order; public function __construct( RequestData $requestData, CartRepository $repository, @@ -92,36 +94,28 @@ class CreateOrderEndpoint implements EndpointInterface return true; } catch (\RuntimeException $error) { wp_send_json_error( - __('Something went wrong. Please try again or choose another payment source.', 'woocommerce-paypal-commerce-gateway') + __( + 'Something went wrong. Please try again or choose another payment source.', + 'woocommerce-paypal-commerce-gateway' + ) ); return false; } } - private function validateForm(string $formValues, Order $order) { + private function validateForm(string $formValues, Order $order) + { + $this->order = $order; $parsedValues = wp_parse_args($formValues); $_POST = $parsedValues; $_REQUEST = $parsedValues; add_filter( 'woocommerce_after_checkout_validation', - function($data, \WP_Error $errors) use ($order) { - if (! $errors->errors) { - - /** - * In case we are onboarded and everything is fine with the \WC_Order - * we want this order to be created. We will intercept it and leave it - * in the "Pending payment" status though, which than later will change - * during the "onApprove"-JS callback or the webhook listener. - */ - if (! $this->earlyOrderHandler->shouldCreateEarlyOrder()) { - wp_send_json_success($order->toArray()); - } - $this->earlyOrderHandler->registerForOrder($order); - return $data; - } - wp_send_json_error($errors->get_error_message()); - }, + [ + $this, + 'afterCheckoutValidation', + ], 10, 2 ); @@ -129,4 +123,25 @@ class CreateOrderEndpoint implements EndpointInterface $checkout->process_checkout(); } + public function afterCheckoutValidation(array $data, \WP_Error $errors): array + { + + $order = $this->order; + if (! $errors->errors) { + + /** + * In case we are onboarded and everything is fine with the \WC_Order + * we want this order to be created. We will intercept it and leave it + * in the "Pending payment" status though, which than later will change + * during the "onApprove"-JS callback or the webhook listener. + */ + if (! $this->earlyOrderHandler->shouldCreateEarlyOrder()) { + wp_send_json_success($order->toArray()); + } + $this->earlyOrderHandler->registerForOrder($order); + return $data; + } + wp_send_json_error($errors->get_error_message()); + return $data; + } } diff --git a/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php b/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php index 051aac20f..ad9760ecc 100644 --- a/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php +++ b/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php @@ -14,13 +14,12 @@ use Inpsyde\PayPalCommerce\Webhooks\Handler\PrefixTrait; class EarlyOrderHandler { - use PrefixTrait; + private $state; private $orderProcessor; private $sessionHandler; - public function __construct( State $state, OrderProcessor $orderProcessor, @@ -34,15 +33,15 @@ class EarlyOrderHandler $this->prefix = $prefix; } - public function shouldCreateEarlyOrder() : bool + public function shouldCreateEarlyOrder(): bool { return $this->state->currentState() === State::STATE_ONBOARDED; } - public function determineWcOrderId($value) : ?int { - if (! is_null($value)) { - $value = (int) $value; - } + //phpcs:disable WordPress.Security.NonceVerification.Recommended + public function determineWcOrderId(int $value = null): ?int + { + if (! isset($_REQUEST['ppcp-resume-order'])) { return $value; } @@ -54,31 +53,35 @@ class EarlyOrderHandler return $value; } + $orderId = false; foreach ($order->purchaseUnits() as $purchaseUnit) { if ($purchaseUnit->customId() === sanitize_text_field(wp_unslash($_REQUEST['ppcp-resume-order']))) { $orderId = (int) $this->sanitizeCustomId($purchaseUnit->customId()); - if ($orderId === $resumeOrderId) { - $value = $orderId; - } } } + if ($orderId === $resumeOrderId) { + $value = $orderId; + } return $value; } + //phpcs:enable WordPress.Security.NonceVerification.Recommended - public function registerForOrder(Order $order): bool { + public function registerForOrder(Order $order): bool + { $success = (bool) add_action( 'woocommerce_checkout_order_processed', - function($orderId) use ($order) { + function ($orderId) use ($order) { try { $order = $this->configureSessionAndOrder((int) $orderId, $order); wp_send_json_success($order->toArray()); - return true; - } catch (\RuntimeException $error) { + } catch (\RuntimeException $error) { wp_send_json_error( - __('Something went wrong. Please try again or choose another payment source.', 'woocommerce-paypal-commerce-gateway') + __( + 'Something went wrong. Please try again or choose another payment source.', + 'woocommerce-paypal-commerce-gateway' + ) ); - return false; } } ); diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 95a36dad8..1341b2236 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -125,12 +125,14 @@ class PayPalGateway extends \WC_Payment_Gateway /** * If the WC_Order is payed through the approved webhook. */ + //phpcs:disable WordPress.Security.NonceVerification.Recommended if (isset($_REQUEST['ppcp-resume-order']) && $wcOrder->has_status('processing')) { return [ 'result' => 'success', 'redirect' => $this->get_return_url($wcOrder), ]; } + //phpcs:enable WordPress.Security.NonceVerification.Recommended if ($this->orderProcessor->process($wcOrder, $woocommerce)) { return [ diff --git a/modules.local/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules.local/ppcp-wc-gateway/src/Processor/OrderProcessor.php index dcc80254f..334b5869f 100644 --- a/modules.local/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules.local/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -107,7 +107,10 @@ class OrderProcessor private function captureAuthorizedDownloads(Order $order): bool { - if (! $this->settings->has('capture_for_virtual_only') || ! $this->settings->get('capture_for_virtual_only')) { + if ( + ! $this->settings->has('capture_for_virtual_only') + || ! $this->settings->get('capture_for_virtual_only') + ) { return false; } diff --git a/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php b/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php index ece024ad6..56d712087 100644 --- a/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php +++ b/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php @@ -1,17 +1,15 @@ eventTypes(), true); } + //phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong public function handleRequest(\WP_REST_Request $request): \WP_REST_Response { $response = ['success' => false]; @@ -74,7 +73,8 @@ class CheckoutOrderApproved implements RequestHandler } try { - $order = isset($request['resource']['id']) ? $this->orderEndpoint->order($request['resource']['id']) : null; + $order = isset($request['resource']['id']) ? + $this->orderEndpoint->order($request['resource']['id']) : null; if (! $order) { $message = sprintf( // translators: %s is the PayPal webhook Id. @@ -152,7 +152,7 @@ class CheckoutOrderApproved implements RequestHandler __('Payment received.', 'woocommerce-paypal-commerce-gateway') : __('Payment can be captured.', 'woocommerce-paypal-commerce-gateway'); foreach ($wcOrders as $wcOrder) { - if (! in_array($wcOrder->get_status(),['pending', 'on-hold'])) { + if (! in_array($wcOrder->get_status(), ['pending', 'on-hold'], true)) { continue; } /** @@ -181,4 +181,5 @@ class CheckoutOrderApproved implements RequestHandler $response['success'] = true; return rest_ensure_response($response); } -} \ No newline at end of file + //phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong +} diff --git a/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php b/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php index 6030d5fad..5489ac411 100644 --- a/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php +++ b/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php @@ -97,7 +97,7 @@ class CheckoutOrderCompleted implements RequestHandler } foreach ($wcOrders as $wcOrder) { - if (! in_array($wcOrder->get_status(),['pending', 'on-hold'])) { + if (! in_array($wcOrder->get_status(), ['pending', 'on-hold'], true)) { continue; } /**