src/Service/App/Merchant/Invoice/Listener/ConfirmPaymentListener.php line 70

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\ICreateInvoiceByOrderIdService;
  6. use App\Service\App\Merchant\Invoice\Contract\IPayInvoiceService;
  7. use App\Service\App\Merchant\Payment\Event\ConfirmPaymentEvent;
  8. use App\Types\InvoiceStatus;
  9. class ConfirmPaymentListener
  10. {
  11.     /**
  12.      * Create invoice service
  13.      *
  14.      * @var ICreateInvoiceByOrderIdService
  15.      */
  16.     private ICreateInvoiceByOrderIdService $createInvoiceService;
  17.     /**
  18.      * Pay invoice service
  19.      *
  20.      * @var IPayInvoiceService
  21.      */
  22.     private IPayInvoiceService $payInvoiceService;
  23.     /**
  24.      * Invoice custom feature instance
  25.      *
  26.      * @var InvoiceCustomFeature
  27.      */
  28.     private InvoiceCustomFeature $invoiceCustomFeature;
  29.     /**
  30.      * Order repository
  31.      *
  32.      * @var IOrderRepository
  33.      */
  34.     private IOrderRepository $orderRepository;
  35.     /**
  36.      * Constructor
  37.      *
  38.      * @param ICreateInvoiceByOrderIdService $createInvoiceService
  39.      * @param IPayInvoiceService $payInvoiceService
  40.      * @param InvoiceCustomFeature $invoiceCustomFeature
  41.      * @param IOrderRepository $orderRepository
  42.      */
  43.     public function __construct(
  44.         ICreateInvoiceByOrderIdService $createInvoiceService,
  45.         IPayInvoiceService $payInvoiceService,
  46.         InvoiceCustomFeature $invoiceCustomFeature,
  47.         IOrderRepository $orderRepository
  48.     )
  49.     {
  50.         $this->createInvoiceService $createInvoiceService;
  51.         $this->payInvoiceService $payInvoiceService;
  52.         $this->invoiceCustomFeature $invoiceCustomFeature;
  53.         $this->orderRepository $orderRepository;
  54.     }
  55.     /**
  56.      * Listen on confirm payment event
  57.      *
  58.      * @param ConfirmPaymentEvent $event
  59.      * @return void
  60.      */
  61.     public function onAction(ConfirmPaymentEvent $event): void
  62.     {
  63.         if ($this->invoiceCustomFeature->isEnabled()) {
  64.             $order $this->orderRepository->getOrder($event->getRequest()->getId());
  65.             $invoice $order->getInvoice();
  66.             $reason $event->getRequest()->getReason() ?? '';
  67.             if ($invoice && $invoice->getStatus() != InvoiceStatus::STATUS_PAID) {
  68.                 $this->payInvoiceService->perform($invoice->getId(), $reason);
  69.             } else if ($order->isAutoMakeInvoiceOnReceive()) {
  70.                 // skip for 0 amount orders
  71.                 if (!$order->getAmount()) {
  72.                     return;
  73.                 }
  74.                 $res $this->createInvoiceService->perform($event->getRequest()->getId());
  75.                 $this->payInvoiceService->perform($res->getId(), $reason);
  76.             }
  77.         }
  78.     }
  79. }