From 83b08e805ae923353f500e2c0bd38614136e3ad5 Mon Sep 17 00:00:00 2001 From: Tamara Date: Fri, 31 Jan 2025 21:02:56 +0100 Subject: [PATCH] Fix braking issue with no available shipping options AD4CR22I-28 --- .../ExpressCheckoutController.php | 10 +++ .../page/checkout/cart/index.html.twig | 6 +- .../product-detail/buy-widget-form.html.twig | 2 +- src/Service/ExpressCheckoutService.php | 85 ++++++++++--------- src/Subscriber/PaymentSubscriber.php | 38 ++++++--- 5 files changed, 89 insertions(+), 52 deletions(-) diff --git a/src/Controller/StoreApi/ExpressCheckout/ExpressCheckoutController.php b/src/Controller/StoreApi/ExpressCheckout/ExpressCheckoutController.php index 0c27e3f6..1b82505c 100755 --- a/src/Controller/StoreApi/ExpressCheckout/ExpressCheckoutController.php +++ b/src/Controller/StoreApi/ExpressCheckout/ExpressCheckoutController.php @@ -97,6 +97,16 @@ public function getExpressCheckoutConfig( $formattedHandlerIdentifier ); + if (array_key_exists('error', $config)) { + if ($config['error'] === 'ResolveCountryException') { + throw new ResolveCountryException($config['message']); + } + + if ($config['error'] === 'ResolveShippingMethodException') { + throw new ResolveShippingMethodException($config['message']); + } + } + return new JsonResponse($config); } catch (ResolveCountryException $e) { return new JsonResponse([ diff --git a/src/Resources/views/storefront/page/checkout/cart/index.html.twig b/src/Resources/views/storefront/page/checkout/cart/index.html.twig index 0dab7a49..87d342ac 100644 --- a/src/Resources/views/storefront/page/checkout/cart/index.html.twig +++ b/src/Resources/views/storefront/page/checkout/cart/index.html.twig @@ -10,6 +10,8 @@ {% block page_checkout_cart_action_proceed %} {{ parent() }} - {% sw_include '@AdyenPaymentShopware6/storefront/component/adyencheckout.html.twig' %} - {% sw_include '@AdyenPaymentShopware6/storefront/component/checkout/expresscheckout.html.twig' %} + {% if adyenFrontendData.expressCheckoutConfigurationAvailable %} + {% sw_include '@AdyenPaymentShopware6/storefront/component/adyencheckout.html.twig' %} + {% sw_include '@AdyenPaymentShopware6/storefront/component/checkout/expresscheckout.html.twig' %} + {% endif %} {% endblock %} \ No newline at end of file diff --git a/src/Resources/views/storefront/page/product-detail/buy-widget-form.html.twig b/src/Resources/views/storefront/page/product-detail/buy-widget-form.html.twig index bd840d28..1f93f281 100755 --- a/src/Resources/views/storefront/page/product-detail/buy-widget-form.html.twig +++ b/src/Resources/views/storefront/page/product-detail/buy-widget-form.html.twig @@ -3,7 +3,7 @@ {% block page_product_detail_buy_container %} {{ parent() }} - {% if buyable %} + {% if buyable and adyenFrontendData.expressCheckoutConfigurationAvailable %} {% sw_include '@AdyenPaymentShopware6/storefront/component/adyencheckout.html.twig' %} {% sw_include '@AdyenPaymentShopware6/storefront/component/checkout/expresscheckout.html.twig' %} {% endif %} diff --git a/src/Service/ExpressCheckoutService.php b/src/Service/ExpressCheckoutService.php index 1e2c10c7..ac4616ec 100755 --- a/src/Service/ExpressCheckoutService.php +++ b/src/Service/ExpressCheckoutService.php @@ -106,8 +106,6 @@ public function __construct( * @param array $newShipping Optional new shipping method details. * @param string $formattedHandlerIdentifier * @return array The configuration for express checkout. - * @throws ResolveCountryException - * @throws ResolveShippingMethodException */ public function getExpressCheckoutConfig( string $productId, @@ -117,47 +115,58 @@ public function getExpressCheckoutConfig( array $newShipping = [], string $formattedHandlerIdentifier = '' ): array { - // Creating new cart - $cartData = $this->createCart( - $productId, - $quantity, - $salesChannelContext, - $newAddress, - $newShipping, - $formattedHandlerIdentifier - ); + try { + $cartData = $this->createCart( + $productId, + $quantity, + $salesChannelContext, + $newAddress, + $newShipping, + $formattedHandlerIdentifier + ); - /** @var Cart $cart */ - $cart = $cartData['cart']; + /** @var Cart $cart */ + $cart = $cartData['cart']; - $currency = $salesChannelContext->getCurrency()->getIsoCode(); - $amountInMinorUnits = $this->currencyUtil->sanitize($cart->getPrice()->getTotalPrice(), $currency); + $currency = $salesChannelContext->getCurrency()->getIsoCode(); + $amountInMinorUnits = $this->currencyUtil->sanitize($cart->getPrice()->getTotalPrice(), $currency); - // Available shipping methods for the given address - $shippingMethods = $this->getFormatedShippingMethods($cartData, $currency); + // Available shipping methods for the given address + $shippingMethods = $this->getFormatedShippingMethods($cartData, $currency); - if (empty($shippingMethods)) { - throw new ResolveShippingMethodException('No shipping method found!'); - } + if (empty($shippingMethods)) { + throw new ResolveShippingMethodException('No shipping method found!'); + } - // Available payment methods - $paymentMethods = $cartData['paymentMethods']; + // Available payment methods + $paymentMethods = $cartData['paymentMethods']; - // Delete temporary cart for product - if ($productId !== '-1') { - $this->cartService->deleteCart($cartData['updatedSalesChannelContext']); - } + // Delete temporary cart for product + if ($productId !== '-1') { + $this->cartService->deleteCart($cartData['updatedSalesChannelContext']); + } - return [ - 'currency' => $currency, - 'amount' => $amountInMinorUnits, - 'countryCode' => $this->getCountryCode( - $salesChannelContext->getCustomer(), - $salesChannelContext - ), - 'paymentMethodsResponse' => json_encode($paymentMethods), - 'shippingMethodsResponse' => array_values($shippingMethods), - ]; + return [ + 'currency' => $currency, + 'amount' => $amountInMinorUnits, + 'countryCode' => $this->getCountryCode( + $salesChannelContext->getCustomer(), + $salesChannelContext + ), + 'paymentMethodsResponse' => json_encode($paymentMethods), + 'shippingMethodsResponse' => array_values($shippingMethods), + ]; + } catch (ResolveCountryException $exception) { + return [ + 'error' => 'ResolveCountryException', + 'message' => $exception->getMessage() + ]; + } catch (ResolveShippingMethodException $exception) { + return [ + 'error' => 'ResolveShippingMethodException', + 'message' => $exception->getMessage() + ]; + } } /** @@ -756,8 +765,8 @@ private function getFormatedShippingMethods(array $cartData, string $currency): $cart = $this->cartService->recalculate($cart, $salesChannelWithCurrentShippingMethod); /** @var Delivery $delivery */ - $delivery =$cart->getDeliveries()->first(); - $price = $delivery->getShippingCosts()->getTotalPrice(); + $delivery = $cart->getDeliveries()->first(); + $price = $delivery ? $delivery->getShippingCosts()->getTotalPrice() : 0; $value = $this->currencyUtil->sanitize($price, $currency); return [ diff --git a/src/Subscriber/PaymentSubscriber.php b/src/Subscriber/PaymentSubscriber.php index aaf26dff..2bc0144c 100644 --- a/src/Subscriber/PaymentSubscriber.php +++ b/src/Subscriber/PaymentSubscriber.php @@ -280,6 +280,17 @@ public function onShoppingCartLoaded(PageLoadedEvent $event) $page->getCart()->getPrice()->getTotalPrice() ); + $expressCheckoutConfigurationAvailable = true; + $expressCheckoutConfiguration = $this->expressCheckoutService->getExpressCheckoutConfig( + '-1', + -1, + $salesChannelContext + ); + if (array_key_exists('error', $expressCheckoutConfiguration)) { + $expressCheckoutConfigurationAvailable = false; + $expressCheckoutConfiguration = []; + } + $page->addExtension( self::ADYEN_DATA_EXTENSION_ID, new ArrayEntity( @@ -342,12 +353,9 @@ public function onShoppingCartLoaded(PageLoadedEvent $event) 'userLoggedIn' => json_encode($userLoggedIn), 'affiliateCode' => $affiliateCode, 'campaignCode' => $campaignCode, + 'expressCheckoutConfigurationAvailable' => $expressCheckoutConfigurationAvailable ], - $this->expressCheckoutService->getExpressCheckoutConfig( - '-1', - -1, - $salesChannelContext - ) + $expressCheckoutConfiguration ) ) ); @@ -371,6 +379,17 @@ public function onProductPageLoaded(PageLoadedEvent $event): void $affiliateCode = $this->requestStack->getSession()->get(AffiliateTrackingListener::AFFILIATE_CODE_KEY); $campaignCode = $this->requestStack->getSession()->get(AffiliateTrackingListener::CAMPAIGN_CODE_KEY); + $expressCheckoutConfigurationAvailable = true; + $expressCheckoutConfiguration = $this->expressCheckoutService->getExpressCheckoutConfig( + '-1', + -1, + $salesChannelContext + ); + if (array_key_exists('error', $expressCheckoutConfiguration)) { + $expressCheckoutConfigurationAvailable = false; + $expressCheckoutConfiguration = []; + } + $page->addExtension( self::ADYEN_DATA_EXTENSION_ID, new ArrayEntity( @@ -413,13 +432,10 @@ public function onProductPageLoaded(PageLoadedEvent $event): void 'googleMerchantId' => $this->configurationService ->getGooglePayMerchantId($salesChannelContext->getSalesChannelId()), 'gatewayMerchantId' => $this->configurationService - ->getMerchantAccount($salesChannelContext->getSalesChannelId()) + ->getMerchantAccount($salesChannelContext->getSalesChannelId()), + 'expressCheckoutConfigurationAvailable' => $expressCheckoutConfigurationAvailable ], - $this->expressCheckoutService->getExpressCheckoutConfig( - $productId, - 1, - $salesChannelContext - ) + $expressCheckoutConfiguration ) ) );