<?php
namespace App\Service\App\Merchant\Integrations\Quickbooks\Listener;
use App\Background\Data\QuickbooksRefundPaymentData;
use App\Core\BackgroundWork\Contract\IClient;
use App\Core\MerchantSelector\Contract\IMerchantSelector;
use App\Service\App\Merchant\CustomFeature\Implementation\QuickbooksCustomFeature;
use App\Service\App\Merchant\Integrations\Quickbooks\Contract\IQuickbooksIntegration;
use App\Service\App\Merchant\Payment\Event\RefundPaymentEvent;
class RefundPaymentListener
{
/**
* Background job client
*
* @var IClient
*/
private IClient $backgroundClient;
/**
* Merchant selector
*
* @var IMerchantSelector
*/
private IMerchantSelector $merchantSelector;
/**
* Quickbooks custom feature instance
*
* @var QuickbooksCustomFeature
*/
private QuickbooksCustomFeature $quickbooksCustomFeature;
/**
* Quickbooks integration
*
* @var IQuickbooksIntegration
*/
private IQuickbooksIntegration $quickbooksIntegration;
/**
* Constructor
*
* @param IClient $backgroundClient
* @param IMerchantSelector $merchantSelector
* @param QuickbooksCustomFeature $quickbooksCustomFeature
* @param IQuickbooksIntegration $quickbooksIntegration
*/
public function __construct(
IClient $backgroundClient, IMerchantSelector $merchantSelector, QuickbooksCustomFeature $quickbooksCustomFeature,
IQuickbooksIntegration $quickbooksIntegration
)
{
$this->backgroundClient = $backgroundClient;
$this->merchantSelector = $merchantSelector;
$this->quickbooksCustomFeature = $quickbooksCustomFeature;
$this->quickbooksIntegration = $quickbooksIntegration;
}
/**
* On refund payment action - create quickbooks credit memo
*
* @param RefundPaymentEvent $event
* @return void
*/
public function onAction(RefundPaymentEvent $event): void
{
if (!$this->quickbooksCustomFeature->isActive()) {
return;
}
if (!$this->quickbooksIntegration->isRefundEnabled()) {
return;
}
$this->backgroundClient->runJob(
new QuickbooksRefundPaymentData($event->getRequest()->getId(), $this->merchantSelector->getMerchant())
);
}
}