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

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\Lifecycle\Method\PaymentMethodDataRegistry;
  19. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. class CartValidator implements CartValidatorInterface
  22. {
  23.     private CartPriceService $cartPriceService;
  24.     private PaymentMethodDataRegistry $methodDataRegistry;
  25.     private SettingsValidationServiceInterface $settingsValidationService;
  26.     private RequestStack $requestStack;
  27.     private ExcludedProductValidator $excludedProductValidator;
  28.     public function __construct(
  29.         CartPriceService $cartPriceService,
  30.         PaymentMethodDataRegistry $methodDataRegistry,
  31.         SettingsValidationServiceInterface $settingsValidationService,
  32.         RequestStack $requestStack,
  33.         ExcludedProductValidator $excludedProductValidator
  34.     ) {
  35.         $this->cartPriceService $cartPriceService;
  36.         $this->methodDataRegistry $methodDataRegistry;
  37.         $this->settingsValidationService $settingsValidationService;
  38.         $this->requestStack $requestStack;
  39.         $this->excludedProductValidator $excludedProductValidator;
  40.     }
  41.     public function validate(Cart $cartErrorCollection $errorsSalesChannelContext $context): void
  42.     {
  43.         if (!$this->methodDataRegistry->isPayPalPaymentMethod($context->getPaymentMethod())) {
  44.             return;
  45.         }
  46.         try {
  47.             $this->settingsValidationService->validate($context->getSalesChannelId());
  48.         } catch (PayPalSettingsInvalidException $e) {
  49.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  50.             return;
  51.         }
  52.         if ($this->cartPriceService->isZeroValueCart($cart)) {
  53.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  54.             return;
  55.         }
  56.         try {
  57.             $ineligiblePaymentMethods $this->requestStack->getSession()->get(MethodEligibilityRoute::SESSION_KEY);
  58.             if (\is_array($ineligiblePaymentMethods) && \in_array($context->getPaymentMethod()->getHandlerIdentifier(), $ineligiblePaymentMethodstrue)) {
  59.                 $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  60.                 return;
  61.             }
  62.         } catch (SessionNotFoundException $e) {
  63.             return;
  64.         }
  65.         if ($this->excludedProductValidator->cartContainsExcludedProduct($cart$context)) {
  66.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  67.         }
  68.     }
  69. }