custom/plugins/SwagPayPal/src/Checkout/Cart/Validation/CartValidator.php line 79

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout\Cart\Validation;
  8. use Shopware\Core\Checkout\Cart\Cart;
  9. use Shopware\Core\Checkout\Cart\CartValidatorInterface;
  10. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  11. use Shopware\Core\Checkout\Payment\Cart\Error\PaymentMethodBlockedError;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Swag\PayPal\Checkout\Cart\Service\CartPriceService;
  14. use Swag\PayPal\Checkout\Cart\Service\ExcludedProductValidator;
  15. use Swag\PayPal\Checkout\SalesChannel\MethodEligibilityRoute;
  16. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  17. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  18. use Swag\PayPal\Util\Availability\AvailabilityService;
  19. use Swag\PayPal\Util\Lifecycle\Method\PaymentMethodDataRegistry;
  20. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. class CartValidator implements CartValidatorInterface
  23. {
  24.     private CartPriceService $cartPriceService;
  25.     private PaymentMethodDataRegistry $methodDataRegistry;
  26.     private SettingsValidationServiceInterface $settingsValidationService;
  27.     private RequestStack $requestStack;
  28.     private ExcludedProductValidator $excludedProductValidator;
  29.     private AvailabilityService $availabilityService;
  30.     /**
  31.      * @internal
  32.      */
  33.     public function __construct(
  34.         CartPriceService $cartPriceService,
  35.         PaymentMethodDataRegistry $methodDataRegistry,
  36.         SettingsValidationServiceInterface $settingsValidationService,
  37.         RequestStack $requestStack,
  38.         ExcludedProductValidator $excludedProductValidator,
  39.         availabilityService $availabilityService
  40.     ) {
  41.         $this->cartPriceService $cartPriceService;
  42.         $this->methodDataRegistry $methodDataRegistry;
  43.         $this->settingsValidationService $settingsValidationService;
  44.         $this->requestStack $requestStack;
  45.         $this->excludedProductValidator $excludedProductValidator;
  46.         $this->availabilityService $availabilityService;
  47.     }
  48.     public function validate(Cart $cartErrorCollection $errorsSalesChannelContext $context): void
  49.     {
  50.         if (!$this->methodDataRegistry->isPayPalPaymentMethod($context->getPaymentMethod())) {
  51.             return;
  52.         }
  53.         try {
  54.             $this->settingsValidationService->validate($context->getSalesChannelId());
  55.         } catch (PayPalSettingsInvalidException $e) {
  56.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  57.             return;
  58.         }
  59.         if ($this->cartPriceService->isZeroValueCart($cart)) {
  60.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  61.             return;
  62.         }
  63.         try {
  64.             $ineligiblePaymentMethods $this->requestStack->getSession()->get(MethodEligibilityRoute::SESSION_KEY);
  65.             if (\is_array($ineligiblePaymentMethods) && \in_array($context->getPaymentMethod()->getHandlerIdentifier(), $ineligiblePaymentMethodstrue)) {
  66.                 $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  67.                 return;
  68.             }
  69.         } catch (SessionNotFoundException $e) {
  70.             return;
  71.         }
  72.         if ($this->excludedProductValidator->cartContainsExcludedProduct($cart$context)) {
  73.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  74.             return;
  75.         }
  76.         if (!$this->availabilityService->isPaymentMethodAvailable($context->getPaymentMethod(), $cart$context)) {
  77.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  78.         }
  79.     }
  80. }