<?php
namespace App\Service\App\Merchant\Integrations\Freshbooks\Listener;
use App\Background\Data\FreshbooksCreateRecordData;
use App\Core\BackgroundWork\Contract\IClient;
use App\Core\MerchantSelector\Contract\IMerchantSelector;
use App\Service\App\Merchant\CustomFeature\Implementation\FreshbooksCustomFeature;
use App\Service\App\Merchant\Payment\Event\ConfirmPaymentEvent;
use App\Types\ReexportMode;
class ConfirmPaymentListener
{
/**
* Background job client
*
* @var IClient
*/
private IClient $backgroundClient;
/**
* Merchant selector
*
* @var IMerchantSelector
*/
private IMerchantSelector $merchantSelector;
/**
* Freshbooks custom feature instance
*
* @var FreshbooksCustomFeature
*/
private FreshbooksCustomFeature $freshbooksCustomFeature;
/**
* Constructor
*
* @param IClient $backgroundClient
* @param IMerchantSelector $merchantSelector
* @param FreshbooksCustomFeature $freshbooksCustomFeature
*/
public function __construct(IClient $backgroundClient, IMerchantSelector $merchantSelector, FreshbooksCustomFeature $freshbooksCustomFeature)
{
$this->backgroundClient = $backgroundClient;
$this->merchantSelector = $merchantSelector;
$this->freshbooksCustomFeature = $freshbooksCustomFeature;
}
/**
* On confirm payment action - create freshbooks payment and link it to invoice
*
* @param ConfirmPaymentEvent $event
* @return void
*/
public function onAction(ConfirmPaymentEvent $event): void
{
if (!$this->freshbooksCustomFeature->isActive()) {
return;
}
$this->backgroundClient->runJob(
new FreshbooksCreateRecordData(
$event->getRequest()->getId(), $this->merchantSelector->getMerchant(),
ReexportMode::RESYNC_MODE_NONE
)
);
}
}