custom/plugins/RpayPayments/src/Components/Logging/Subscriber/RequestSubscriber.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\Logging\Subscriber;
  10. use Monolog\Logger;
  11. use RatePAY\Model\Response\PaymentRequest;
  12. use Ratepay\RpayPayments\Components\Logging\Service\ApiLogger;
  13. use Ratepay\RpayPayments\Components\PaymentHandler\Event\PaymentFailedEvent;
  14. use Ratepay\RpayPayments\Components\RatepayApi\Event\RequestDoneEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Throwable;
  17. class RequestSubscriber implements EventSubscriberInterface
  18. {
  19.     protected ApiLogger $apiLogger;
  20.     private Logger $fileLogger;
  21.     public function __construct(ApiLogger $apiLoggerLogger $fileLogger)
  22.     {
  23.         $this->apiLogger $apiLogger;
  24.         $this->fileLogger $fileLogger;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             RequestDoneEvent::class => 'onRequestDone',
  30.             PaymentFailedEvent::class => 'onPaymentFailed',
  31.         ];
  32.     }
  33.     public function onPaymentFailed(PaymentFailedEvent $event): void
  34.     {
  35.         $exception $event->getException();
  36.         if ($exception instanceof Throwable) {
  37.             $exception $exception->getPrevious() ?? $exception;
  38.             $message $exception->getMessage();
  39.         } elseif ($event->getResponse() instanceof PaymentRequest) {
  40.             $message $event->getResponse()->getReasonMessage();
  41.         }
  42.         $this->fileLogger->error($message ?? 'Unknown error', [
  43.             'order_id' => $event->getOrder()->getId(),
  44.             'order_number' => $event->getOrder()->getOrderNumber(),
  45.             'request_bag' => $event->getRequestDataBag(),
  46.         ]);
  47.     }
  48.     public function onRequestDone(RequestDoneEvent $event): void
  49.     {
  50.         $this->apiLogger->logRequest($event);
  51.     }
  52. }