<?php
namespace Mm\Escmid\PaymentBundle\EventSubscriber;
use Mm\Escmid\NewsletterBundle\Service\RemoteProcessorServiceInterface;
use Mm\Escmid\PaymentBundle\Entity\Payment;
use Mm\Escmid\PaymentBundle\Event\LmicApproveEvent;
use Mm\Escmid\PaymentBundle\Event\LmicClarifyEvent;
use Mm\Escmid\PaymentBundle\Event\LmicRejectEvent;
use Mm\Escmid\PaymentBundle\Event\PaymentCompleteEvent;
use Mm\Escmid\PaymentBundle\Event\PaymentFailureEvent;
use Mm\Escmid\PaymentBundle\Event\YsmApproveEvent;
use Mm\Escmid\PaymentBundle\Event\YsmRejectEvent;
use Mm\Escmid\PaymentBundle\Service\PaymentMailServiceInterface;
use Mm\Escmid\PaymentBundle\Service\PaymentService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MembershipSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly PaymentMailServiceInterface $paymentMailService,
private readonly PaymentService $paymentService,
private readonly RemoteProcessorServiceInterface $remoteProcessorService
) {
}
public static function getSubscribedEvents(): array
{
return [
PaymentCompleteEvent::class => 'onPaymentComplete',
PaymentFailureEvent::class => 'onPaymentFailure',
YsmApproveEvent::class => 'onYsmApprove',
YsmRejectEvent::class => 'onYsmReject',
LmicApproveEvent::class => 'onLmicApprove',
LmicRejectEvent::class => 'onLmicReject',
LmicClarifyEvent::class => 'onLmicClarify',
];
}
public function onPaymentComplete(PaymentCompleteEvent $event): void
{
$paymentData = $event->getPaymentData();
$payment = $this->paymentService->findByPaymentId($paymentData['id'], true);
$this->paymentService->refreshInvoice($payment);
$invoiceMailTemplate = null;
if ($this->isYsmMembershipCandidate($payment)) {
$this->paymentMailService->sendYsmAdminNotification($payment);
$invoiceMailTemplate = ('renew' === $payment->getMemberStatus()) ?
'invoiceRenew_ysm_candidate.html.twig' :
'invoiceNew_ysm_candidate.html.twig'
;
}
// send email
$this->paymentMailService->sendInvoice($payment, $invoiceMailTemplate);
$this->paymentMailService->sendPaymentConfirmation($payment);
// add default categories
$this->remoteProcessorService->addDefaultCategoriesToSubscriberByEscmidUserId($payment->getUser()->getId(), true);
// send email notification for admin if user bought a charity pin
if ($this->paymentService->charityPinWasOrdered($payment)) {
$this->paymentMailService->sendCharityPinAdminNotification($payment);
}
}
private function isYsmMembershipCandidate(Payment $payment): bool
{
$paymentItems = $payment->getPaymentItemsList();
return
in_array(Payment::BASIC_MEMBERSHIP_ID, $paymentItems)
&& Payment::MEMBERSHIP_REMARK_YSM_CANDIDATE === $payment->getMembershipRemark()
;
}
public function onPaymentFailure(PaymentFailureEvent $event): void
{
$this->paymentMailService->sendPaymentFailedMail($event->getPayment());
}
public function onYsmApprove(YsmApproveEvent $event): void
{
$this->paymentMailService->sendYsmApprovalNotification($event->getPayment());
}
public function onYsmReject(YsmRejectEvent $event): void
{
$payment = $event->getPayment();
$reason = $event->getRejectReason();
$this->paymentMailService->sendYsmRejectionNotification($payment, $reason);
}
public function onLmicApprove(LmicApproveEvent $event): void
{
$this->paymentMailService->sendLmicApprovalNotification($event->getPayment());
}
public function onLmicReject(LmicRejectEvent $event): void
{
$payment = $event->getPayment();
$reason = $event->getRejectReason();
$this->paymentMailService->sendLmicRejectionNotification($payment, $reason);
}
public function onLmicClarify(LmicClarifyEvent $event): void
{
$payment = $event->getPayment();
$reason = $event->getRejectReason();
$this->paymentMailService->sendLmicClarifyNotification($payment, $reason);
}
}