custom/plugins/RpayPayments/src/Components/Checkout/Subscriber/PaymentFailedSubscriber.php line 47

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\Checkout\Subscriber;
  10. use Ratepay\RpayPayments\Components\Checkout\Model\Extension\OrderExtension;
  11. use Ratepay\RpayPayments\Components\Checkout\Model\RatepayOrderDataEntity;
  12. use Ratepay\RpayPayments\Components\Logging\Model\ApiRequestLogEntity;
  13. use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentRequestService;
  14. use Ratepay\RpayPayments\Components\RatepayApi\Util\ResponseConverter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  19. use Symfony\Component\DependencyInjection\ContainerInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class PaymentFailedSubscriber implements EventSubscriberInterface
  22. {
  23.     private EntityRepositoryInterface $ratepayApiLogRepository;
  24.     private ContainerInterface $container;
  25.     public function __construct(
  26.         EntityRepositoryInterface $ratepayApiLogRepository,
  27.         ContainerInterface $container
  28.     ) {
  29.         $this->ratepayApiLogRepository $ratepayApiLogRepository;
  30.         $this->container $container;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             CheckoutFinishPageLoadedEvent::class => 'onFinishPage',
  36.         ];
  37.     }
  38.     public function onFinishPage(CheckoutFinishPageLoadedEvent $event): void
  39.     {
  40.         if ($event->getPage()->isPaymentFailed() === false) {
  41.             return;
  42.         }
  43.         $order $event->getPage()->getOrder();
  44.         /** @var RatepayOrderDataEntity $ratepayData */
  45.         $ratepayData $order->getExtension(OrderExtension::EXTENSION_NAME);
  46.         if ($ratepayData && $ratepayData->isSuccessful() === false) {
  47.             $transactionId $ratepayData->getTransactionId();
  48.             $criteria = new Criteria();
  49.             $criteria->addFilter(new EqualsFilter(ApiRequestLogEntity::FIELD_OPERATION'PAYMENT_REQUEST'));
  50.             $criteria->addFilter(new EqualsFilter(ApiRequestLogEntity::FIELD_ADDITIONAL_DATA '.transactionId'$transactionId));
  51.             $criteria->addFilter(new EqualsFilter(ApiRequestLogEntity::FIELD_ADDITIONAL_DATA '.orderNumber'$order->getOrderNumber()));
  52.             $criteria->setLimit(1);
  53.             $logEntries $this->ratepayApiLogRepository->search($criteria$event->getContext());
  54.             /** @var ApiRequestLogEntity $logEntry */
  55.             $logEntry $logEntries->first();
  56.             if ($logEntry === null) {
  57.                 // log entry was not found.
  58.                 return;
  59.             }
  60.             try {
  61.                 $response ResponseConverter::getResponseObjectByXml(
  62.                     PaymentRequestService::CALL_PAYMENT_REQUEST,
  63.                     $logEntry->getResponse()
  64.                 );
  65.             } catch (\Exception $e) {
  66.                 // response can not be converted.
  67.                 return;
  68.             }
  69.             $session $this->container->get('session');
  70.             if ($response && $session) {
  71.                 $message = !empty($response->getCustomerMessage()) ? $response->getCustomerMessage() : $response->getResultMessage();
  72.                 $session->getFlashBag()->add('danger'$message);
  73.             }
  74.         }
  75.     }
  76. }