vendor/store.shopware.com/swagpaypal/src/Checkout/Cart/Validation/CartValidator.php line 76

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.     public function __construct(
  31.         CartPriceService $cartPriceService,
  32.         PaymentMethodDataRegistry $methodDataRegistry,
  33.         SettingsValidationServiceInterface $settingsValidationService,
  34.         RequestStack $requestStack,
  35.         ExcludedProductValidator $excludedProductValidator,
  36.         availabilityService $availabilityService
  37.     ) {
  38.         $this->cartPriceService $cartPriceService;
  39.         $this->methodDataRegistry $methodDataRegistry;
  40.         $this->settingsValidationService $settingsValidationService;
  41.         $this->requestStack $requestStack;
  42.         $this->excludedProductValidator $excludedProductValidator;
  43.         $this->availabilityService $availabilityService;
  44.     }
  45.     public function validate(Cart $cartErrorCollection $errorsSalesChannelContext $context): void
  46.     {
  47.         if (!$this->methodDataRegistry->isPayPalPaymentMethod($context->getPaymentMethod())) {
  48.             return;
  49.         }
  50.         try {
  51.             $this->settingsValidationService->validate($context->getSalesChannelId());
  52.         } catch (PayPalSettingsInvalidException $e) {
  53.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  54.             return;
  55.         }
  56.         if ($this->cartPriceService->isZeroValueCart($cart)) {
  57.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  58.             return;
  59.         }
  60.         try {
  61.             $ineligiblePaymentMethods $this->requestStack->getSession()->get(MethodEligibilityRoute::SESSION_KEY);
  62.             if (\is_array($ineligiblePaymentMethods) && \in_array($context->getPaymentMethod()->getHandlerIdentifier(), $ineligiblePaymentMethodstrue)) {
  63.                 $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  64.                 return;
  65.             }
  66.         } catch (SessionNotFoundException $e) {
  67.             return;
  68.         }
  69.         if ($this->excludedProductValidator->cartContainsExcludedProduct($cart$context)) {
  70.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  71.             return;
  72.         }
  73.         if (!$this->availabilityService->isPaymentMethodAvailable($context->getPaymentMethod(), $cart$context)) {
  74.             $errors->add(new PaymentMethodBlockedError((string) $context->getPaymentMethod()->getTranslation('name')));
  75.         }
  76.     }
  77. }