<?php
namespace Harmonizely\Integration\Simplypay\Subscribers;
use Harmonizely\CalendarEvents;
use Harmonizely\Model\EventInterface;
use Harmonizely\Model\EventPayment;
use Harmonizely\Model\EventTypeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Harmonizely\Integration\Simplypay\Contract\SimplypayGetIntegrationConfigInterface;
class SimplypayInitBookingPaymentEventSubscriber implements EventSubscriberInterface
{
/**
* @var SimplypayGetIntegrationConfigInterface
*/
private SimplypayGetIntegrationConfigInterface $getIntegrationConfig;
/**
* @param SimplypayGetIntegrationConfigInterface $getIntegrationConfig
*/
public function __construct(SimplypayGetIntegrationConfigInterface $getIntegrationConfig)
{
$this->getIntegrationConfig = $getIntegrationConfig;
}
/**
* Subscribe to events
*
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CalendarEvents::EVENT_PRE_CREATE => 'onEventPreCreate',
];
}
/**
* Before create event
*
* @param GenericEvent $event
*/
public function onEventPreCreate(GenericEvent $event): void
{
$bookedEvent = $event->getSubject();
if (false === $bookedEvent instanceof EventInterface) {
return;
}
$eventType = $bookedEvent->getEventType();
$payment = $eventType->getPayment();
if (null === $payment || EventTypeInterface::ACCEPT_PAYMENTS_SIMPLYPAY !== $eventType->getAcceptPayments()) {
return;
}
$config = $this->getIntegrationConfig->perform($eventType->getUser());
if (!$config) {
return;
}
$amount = $payment->getAmount();
if (!$config->isPriceIncludesTax()) {
if ($eventType->getTaxDTO()) {
$amount *= 1 + ($eventType->getTaxDTO()->getRatio() / 100);
}
}
$bookedEvent->setPayment(new EventPayment(
EventTypeInterface::ACCEPT_PAYMENTS_SIMPLYPAY,
round($amount, 2),
$payment->getCurrency(),
$bookedEvent->getPaymentSystem(),
EventPayment::STATUS_PENDING,
null,
$config && $config->isOverwritePersonal()
));
}
}