src/Service/App/Merchant/Invoice/Listener/RefundPaymentListener.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Service\App\Merchant\Invoice\Listener;
  3. use App\Repository\Merchant\Contract\IOrderRepository;
  4. use App\Service\App\Merchant\CustomFeature\Implementation\InvoiceCustomFeature;
  5. use App\Service\App\Merchant\Invoice\Contract\ICreateCreditNoteByInvoiceIdService;
  6. use App\Service\App\Merchant\Invoice\Contract\IPayInvoiceService;
  7. use App\Service\App\Merchant\Payment\Event\RefundPaymentEvent;
  8. class RefundPaymentListener
  9. {
  10.     /**
  11.      * Create credit invoice service
  12.      *
  13.      * @var ICreateCreditNoteByInvoiceIdService
  14.      */
  15.     private ICreateCreditNoteByInvoiceIdService $createCreditNoteByInvoiceIdService;
  16.     /**
  17.      * Pay invoice service
  18.      *
  19.      * @var IPayInvoiceService
  20.      */
  21.     private IPayInvoiceService $payInvoiceService;
  22.     /**
  23.      * Invoice custom feature instance
  24.      *
  25.      * @var InvoiceCustomFeature
  26.      */
  27.     private InvoiceCustomFeature $invoiceCustomFeature;
  28.     /**
  29.      * Order repository
  30.      *
  31.      * @var IOrderRepository
  32.      */
  33.     private IOrderRepository $orderRepository;
  34.     /**
  35.      * Constructor
  36.      *
  37.      * @param ICreateCreditNoteByInvoiceIdService $createCreditNoteByInvoiceIdService
  38.      * @param IPayInvoiceService $payInvoiceService
  39.      * @param InvoiceCustomFeature $invoiceCustomFeature
  40.      * @param IOrderRepository $orderRepository
  41.      */
  42.     public function __construct(
  43.         ICreateCreditNoteByInvoiceIdService $createCreditNoteByInvoiceIdService,
  44.         IPayInvoiceService $payInvoiceService,
  45.         InvoiceCustomFeature $invoiceCustomFeature,
  46.         IOrderRepository $orderRepository
  47.     )
  48.     {
  49.         $this->createCreditNoteByInvoiceIdService $createCreditNoteByInvoiceIdService;
  50.         $this->payInvoiceService $payInvoiceService;
  51.         $this->invoiceCustomFeature $invoiceCustomFeature;
  52.         $this->orderRepository $orderRepository;
  53.     }
  54.     /**
  55.      * Listen on refund payment event
  56.      *
  57.      * @param RefundPaymentEvent $event
  58.      * @return void
  59.      */
  60.     public function onAction(RefundPaymentEvent $event): void
  61.     {
  62.         if ($this->invoiceCustomFeature->isEnabled() && $event->getRequest()->isMakeCreditInvoice()) {
  63.             $order $this->orderRepository->getOrder($event->getRequest()->getId());
  64.             if ($order && $order->getInvoice()) {
  65.                 $invoice $this->createCreditNoteByInvoiceIdService->perform(
  66.                     $order->getInvoice()->getId()
  67.                 );
  68.                 $this->payInvoiceService->perform($invoice->getId(), $event->getRequest()->getReason());
  69.             }
  70.         }
  71.     }
  72. }