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

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\Request;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\Validator\ConstraintViolation;
  20. use Symfony\Component\Validator\ConstraintViolationList;
  21. class CheckoutValidationSubscriber implements EventSubscriberInterface
  22. {
  23.     private LockService $lockService;
  24.     private RequestStack $requestStack;
  25.     public function __construct(RequestStack $requestStackLockService $lockService)
  26.     {
  27.         $this->requestStack $requestStack;
  28.         $this->lockService $lockService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'framework.validation.order.create' => ['validatePaymentQuery'15],
  34.         ];
  35.     }
  36.     public function validatePaymentQuery(BuildValidationEvent $event): void
  37.     {
  38.         $request $this->requestStack->getCurrentRequest();
  39.         if (!$request instanceof Request) {
  40.             return;
  41.         }
  42.         $salesChannelContext $this->getContextFromRequest($request);
  43.         $paymentMethod $salesChannelContext->getPaymentMethod();
  44.         $paymentHandlerIdentifier $paymentMethod->getHandlerIdentifier();
  45.         if (strpos($paymentHandlerIdentifier'RpayPayments') !== false && $salesChannelContext->getCustomer()) {
  46.             $isPaymentMethodLocked $this->lockService->isPaymentLocked(
  47.                 $paymentMethod->getId(),
  48.                 $salesChannelContext->getCustomer()->getId(),
  49.                 $salesChannelContext->getContext()
  50.             );
  51.             if ($isPaymentMethodLocked) {
  52.                 throw new ConstraintViolationException(new ConstraintViolationList([new ConstraintViolation('''', [], null'/ratepay'$salesChannelContext->getPaymentMethod()->getName(), nullAbstractPaymentHandler::ERROR_SNIPPET_VIOLATION_PREFIX 'METHOD_NOT_AVAILABLE')]), $request->request->all());
  53.             }
  54.         }
  55.     }
  56.     private function getContextFromRequest(Request $request): SalesChannelContext
  57.     {
  58.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  59.     }
  60. }