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

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