From ce81b1fca3c843627cdb7dbc8a14b02095144792 Mon Sep 17 00:00:00 2001 From: Frederik Rommel <15031079+rommelfreddy@users.noreply.github.com> Date: Wed, 18 Dec 2024 19:16:21 +0100 Subject: [PATCH] PAYOSWXP-158: make methods more abstract (#341) --- .../Controller/GenericExpressController.php | 104 ++++++++---------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/src/Storefront/Controller/GenericExpressController.php b/src/Storefront/Controller/GenericExpressController.php index a96a0d0b..2595fc1b 100644 --- a/src/Storefront/Controller/GenericExpressController.php +++ b/src/Storefront/Controller/GenericExpressController.php @@ -13,10 +13,12 @@ use PayonePayment\Payone\Client\PayoneClientInterface; use PayonePayment\Payone\RequestParameter\RequestParameterFactory; use RuntimeException; +use Shopware\Core\Checkout\Cart\CartException; use Shopware\Core\Checkout\Cart\SalesChannel\CartService; use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRegisterRoute; use Shopware\Core\Checkout\Customer\SalesChannel\AccountService; use Shopware\Core\Framework\Validation\DataBag\DataBag; +use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory; use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService; use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher; @@ -56,39 +58,14 @@ public function __construct( )] public function createSessionAction(SalesChannelContext $context, string $paymentMethodId): Response { - if (!\array_key_exists($paymentMethodId, PaymentHandlerGroups::GENERIC_EXPRESS)) { - throw $this->createNotFoundException(); - } - - $cart = $this->cartService->getCart($context->getToken(), $context); - - $salesChannelDataBag = new DataBag([ - SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId, - ]); - - $this->salesChannelContextSwitcher->update($salesChannelDataBag, $context); - - $setRequest = $this->requestParameterFactory->getRequestParameter( - new CreateExpressCheckoutSessionStruct( - $context, - PaymentHandlerGroups::GENERIC_EXPRESS[$paymentMethodId] - ) - ); - - try { - $response = $this->client->request($setRequest); - } catch (PayoneRequestException) { - throw new RuntimeException($this->trans('PayonePayment.errorMessages.genericError')); - } + $response = $this->createSession($context, $paymentMethodId); - if (!isset($response['addpaydata']['orderId'])) { + if (!isset($response['orderId'])) { throw new RuntimeException('generic express checkout: No orderId has been given for payment method id ' . $paymentMethodId); } - $this->cartExtensionService->addCartExtensionForExpressCheckout($cart, $context, $paymentMethodId, $response['workorderid']); - return new JsonResponse([ - 'orderId' => $response['addpaydata']['orderId'], + 'orderId' => $response['orderId'], ]); } @@ -100,43 +77,20 @@ public function createSessionAction(SalesChannelContext $context, string $paymen )] public function redirectAction(SalesChannelContext $context, string $paymentMethodId): Response { - if (!\array_key_exists($paymentMethodId, PaymentHandlerGroups::GENERIC_EXPRESS)) { - throw $this->createNotFoundException(); - } - - $cart = $this->cartService->getCart($context->getToken(), $context); - try { - $salesChannelDataBag = new DataBag([ - SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId, - ]); - - $this->salesChannelContextSwitcher->update($salesChannelDataBag, $context); - } catch (\Throwable $exception) { + $response = $this->createSession($context, $paymentMethodId); + } catch (ConstraintViolationException $constraintViolationException) { return $this->forwardToRoute('frontend.checkout.confirm.page', [ - 'formViolations' => $exception, + 'formViolations' => $constraintViolationException, ]); - } - - $setRequest = $this->requestParameterFactory->getRequestParameter( - new CreateExpressCheckoutSessionStruct( - $context, - PaymentHandlerGroups::GENERIC_EXPRESS[$paymentMethodId] - ) - ); - - try { - $response = $this->client->request($setRequest); - } catch (PayoneRequestException) { - throw new RuntimeException($this->trans('PayonePayment.errorMessages.genericError')); + } catch (CartException) { + return $this->forwardToRoute('frontend.checkout.confirm.page'); } if (!\array_key_exists('redirecturl', $response)) { throw new RuntimeException('generic express checkout: No redirect URL has been given for payment method id ' . $paymentMethodId); } - $this->cartExtensionService->addCartExtensionForExpressCheckout($cart, $context, $paymentMethodId, $response['workorderid']); - return new RedirectResponse($response['redirecturl']); } @@ -206,4 +160,42 @@ public function returnAction(SalesChannelContext $context, string $paymentMethod return $this->redirectToRoute('frontend.checkout.confirm.page'); } + + /** + * @return array{orderId: string|null, redirectUrl: string|null} + */ + private function createSession(SalesChannelContext $context, string $paymentMethodId): array + { + if (!\array_key_exists($paymentMethodId, PaymentHandlerGroups::GENERIC_EXPRESS)) { + throw $this->createNotFoundException(); + } + + $cart = $this->cartService->getCart($context->getToken(), $context); + + $salesChannelDataBag = new DataBag([ + SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId, + ]); + + $this->salesChannelContextSwitcher->update($salesChannelDataBag, $context); + + $setRequest = $this->requestParameterFactory->getRequestParameter( + new CreateExpressCheckoutSessionStruct( + $context, + PaymentHandlerGroups::GENERIC_EXPRESS[$paymentMethodId] + ) + ); + + try { + $response = $this->client->request($setRequest); + } catch (PayoneRequestException) { + throw new RuntimeException($this->trans('PayonePayment.errorMessages.genericError')); + } + + $this->cartExtensionService->addCartExtensionForExpressCheckout($cart, $context, $paymentMethodId, $response['workorderid']); + + return [ + 'orderId' => $response['addpaydata']['orderId'] ?? null, + 'redirectUrl' => $response['redirecturl'] ?? null, + ]; + } }