custom/plugins/RpayPayments/src/Components/PaymentLock/Subscriber/PaymentFailedSubscriber.php line 42

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\Model\Response\AbstractResponse;
  11. use Ratepay\RpayPayments\Components\PaymentLock\Service\LockService;
  12. use Ratepay\RpayPayments\Components\RatepayApi\Dto\CheckoutOperationInterface;
  13. use Ratepay\RpayPayments\Components\RatepayApi\Event\RequestDoneEvent;
  14. use Shopware\Core\Checkout\Customer\CustomerEntity;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class PaymentFailedSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var int[]
  20.      */
  21.     public const ERROR_CODES = [703720721];
  22.     private LockService $lockService;
  23.     public function __construct(LockService $lockService)
  24.     {
  25.         $this->lockService $lockService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             RequestDoneEvent::class => 'lockPaymentMethod',
  31.         ];
  32.     }
  33.     public function lockPaymentMethod(RequestDoneEvent $event): void
  34.     {
  35.         $requestData $event->getRequestData();
  36.         $response $event->getRequestBuilder()->getResponse();
  37.         if (!$requestData instanceof CheckoutOperationInterface || !$response instanceof AbstractResponse) {
  38.             return;
  39.         }
  40.         if (in_array((int) $response->getReasonCode(), self::ERROR_CODESfalse)) {
  41.             if (!$requestData->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
  42.                 // customer is not logged in - guest order
  43.                 return;
  44.             }
  45.             $this->lockService->lockPaymentMethod(
  46.                 $requestData->getContext(),
  47.                 $requestData->getSalesChannelContext()->getCustomer()->getId(),
  48.                 [$requestData->getPaymentMethodId()]
  49.             );
  50.         }
  51.     }
  52. }