custom/plugins/RpayPayments/src/Components/PaymentLock/Subscriber/CheckoutValidationSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Ratepay GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Ratepay\RpayPayments\Components\PaymentLock\Subscriber;
  10. use Ratepay\RpayPayments\Components\PaymentHandler\AbstractPaymentHandler;
  11. use Ratepay\RpayPayments\Components\PaymentLock\Service\LockService;
  12. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  13. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  14. use Shopware\Core\PlatformRequest;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\Validator\ConstraintViolation;
  19. use Symfony\Component\Validator\ConstraintViolationList;
  20. class CheckoutValidationSubscriber implements EventSubscriberInterface
  21. {
  22.     private LockService $lockService;
  23.     private RequestStack $requestStack;
  24.     public function __construct(RequestStack $requestStackLockService $lockService)
  25.     {
  26.         $this->requestStack $requestStack;
  27.         $this->lockService $lockService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             'framework.validation.order.create' => ['validatePaymentQuery'15],
  33.         ];
  34.     }
  35.     public function validatePaymentQuery(BuildValidationEvent $event): void
  36.     {
  37.         $request $this->requestStack->getCurrentRequest();
  38.         if (null === $request) {
  39.             return;
  40.         }
  41.         $salesChannelContext $this->getContextFromRequest($request);
  42.         $paymentMethod $salesChannelContext->getPaymentMethod();
  43.         $paymentHandlerIdentifier $paymentMethod->getHandlerIdentifier();
  44.         if (strpos($paymentHandlerIdentifier'RpayPayments') !== false && $salesChannelContext->getCustomer()) {
  45.             $isPaymentMethodLocked $this->lockService->isPaymentLocked(
  46.                 $paymentMethod->getId(),
  47.                 $salesChannelContext->getCustomer()->getId(),
  48.                 $salesChannelContext->getContext()
  49.             );
  50.             if ($isPaymentMethodLocked) {
  51.                 throw new ConstraintViolationException(new ConstraintViolationList([new ConstraintViolation('''', [], null'/ratepay'$salesChannelContext->getPaymentMethod()->getName(), nullAbstractPaymentHandler::ERROR_SNIPPET_VIOLATION_PREFIX 'METHOD_NOT_AVAILABLE')]), $request->request->all());
  52.             }
  53.         }
  54.     }
  55.     private function getContextFromRequest($request): SalesChannelContext
  56.     {
  57.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  58.     }
  59. }