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

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