custom/plugins/EasyCreditRatenkauf/src/Subscriber/CheckoutValidationSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Netzkollektiv\EasyCredit\Subscriber;
  9. use Netzkollektiv\EasyCredit\Payment\Handler\AbstractHandler as EasyCreditPaymentHandler;
  10. use Netzkollektiv\EasyCredit\Api\IntegrationFactory;
  11. use Netzkollektiv\EasyCredit\Helper\Payment as PaymentHelper;
  12. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\PaymentHandlerRegistry;
  13. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  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. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  22. class CheckoutValidationSubscriber implements EventSubscriberInterface
  23. {
  24.     private RequestStack $requestStack;
  25.     private PaymentHelper $paymentHelper;
  26.     private IntegrationFactory $integrationFactory;
  27.     public function __construct(
  28.         RequestStack $requestStack,
  29.         PaymentHelper $paymentHelper,
  30.         IntegrationFactory $integrationFactory
  31.     ) {
  32.         $this->requestStack $requestStack;
  33.         $this->paymentHelper $paymentHelper;
  34.         $this->integrationFactory $integrationFactory;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             'framework.validation.order.create' => 'validateTransaction',
  40.         ];
  41.     }
  42.     public function validateTransaction(BuildValidationEvent $event): void
  43.     {
  44.         $request $this->requestStack->getCurrentRequest();
  45.         if ($request === null) {
  46.             return;
  47.         }
  48.         $salesChannelContext $this->getSalesChannelContextFromRequest($request);
  49.         $paymentHandler $this->paymentHelper->getHandlerByPaymentMethod($salesChannelContext->getPaymentMethod());
  50.         $checkout $this->integrationFactory->createCheckout(
  51.             $salesChannelContext->getSalesChannel()->getId()
  52.         );
  53.         if ($paymentHandler instanceof EasyCreditPaymentHandler) {
  54.             if (!$checkout->isApproved()) {
  55.                 throw new ConstraintViolationException(new ConstraintViolationList([
  56.                     new ConstraintViolation(
  57.                         '',
  58.                         '',
  59.                         [],
  60.                         null,
  61.                         '/easycredit',
  62.                         $salesChannelContext->getPaymentMethod()->getName(),
  63.                         null,
  64.                         'EASYCREDIT_TRANSATION_NOT_APRROVED'
  65.                     )
  66.                 ]), $request->request->all());
  67.             }
  68.         }
  69.     }
  70.     private function getSalesChannelContextFromRequest(Request $request): SalesChannelContext
  71.     {
  72.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  73.     }
  74. }