src/Mm/Escmid/PaymentBundle/EventSubscriber/MembershipSubscriber.php line 103

Open in your IDE?
  1. <?php
  2. namespace Mm\Escmid\PaymentBundle\EventSubscriber;
  3. use Mm\Escmid\NewsletterBundle\Service\RemoteProcessorServiceInterface;
  4. use Mm\Escmid\PaymentBundle\Entity\Payment;
  5. use Mm\Escmid\PaymentBundle\Event\LmicApproveEvent;
  6. use Mm\Escmid\PaymentBundle\Event\LmicClarifyEvent;
  7. use Mm\Escmid\PaymentBundle\Event\LmicRejectEvent;
  8. use Mm\Escmid\PaymentBundle\Event\PaymentCompleteEvent;
  9. use Mm\Escmid\PaymentBundle\Event\PaymentFailureEvent;
  10. use Mm\Escmid\PaymentBundle\Event\YsmApproveEvent;
  11. use Mm\Escmid\PaymentBundle\Event\YsmRejectEvent;
  12. use Mm\Escmid\PaymentBundle\Service\PaymentMailServiceInterface;
  13. use Mm\Escmid\PaymentBundle\Service\PaymentService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class MembershipSubscriber implements EventSubscriberInterface
  16. {
  17. public function __construct(
  18. private readonly PaymentMailServiceInterface $paymentMailService,
  19. private readonly PaymentService $paymentService,
  20. private readonly RemoteProcessorServiceInterface $remoteProcessorService
  21. ) {
  22. }
  23. public static function getSubscribedEvents(): array
  24. {
  25. return [
  26. PaymentCompleteEvent::class => 'onPaymentComplete',
  27. PaymentFailureEvent::class => 'onPaymentFailure',
  28. YsmApproveEvent::class => 'onYsmApprove',
  29. YsmRejectEvent::class => 'onYsmReject',
  30. LmicApproveEvent::class => 'onLmicApprove',
  31. LmicRejectEvent::class => 'onLmicReject',
  32. LmicClarifyEvent::class => 'onLmicClarify',
  33. ];
  34. }
  35. public function onPaymentComplete(PaymentCompleteEvent $event): void
  36. {
  37. $paymentData = $event->getPaymentData();
  38. $payment = $this->paymentService->findByPaymentId($paymentData['id'], true);
  39. $this->paymentService->refreshInvoice($payment);
  40. $invoiceMailTemplate = null;
  41. if ($this->isYsmMembershipCandidate($payment)) {
  42. $this->paymentMailService->sendYsmAdminNotification($payment);
  43. $invoiceMailTemplate = ('renew' === $payment->getMemberStatus()) ?
  44. 'invoiceRenew_ysm_candidate.html.twig' :
  45. 'invoiceNew_ysm_candidate.html.twig'
  46. ;
  47. }
  48. // send email
  49. $this->paymentMailService->sendInvoice($payment, $invoiceMailTemplate);
  50. $this->paymentMailService->sendPaymentConfirmation($payment);
  51. // add default categories
  52. $this->remoteProcessorService->addDefaultCategoriesToSubscriberByEscmidUserId($payment->getUser()->getId(), true);
  53. // send email notification for admin if user bought a charity pin
  54. if ($this->paymentService->charityPinWasOrdered($payment)) {
  55. $this->paymentMailService->sendCharityPinAdminNotification($payment);
  56. }
  57. }
  58. private function isYsmMembershipCandidate(Payment $payment): bool
  59. {
  60. $paymentItems = $payment->getPaymentItemsList();
  61. return
  62. in_array(Payment::BASIC_MEMBERSHIP_ID, $paymentItems)
  63. && Payment::MEMBERSHIP_REMARK_YSM_CANDIDATE === $payment->getMembershipRemark()
  64. ;
  65. }
  66. public function onPaymentFailure(PaymentFailureEvent $event): void
  67. {
  68. $this->paymentMailService->sendPaymentFailedMail($event->getPayment());
  69. }
  70. public function onYsmApprove(YsmApproveEvent $event): void
  71. {
  72. $this->paymentMailService->sendYsmApprovalNotification($event->getPayment());
  73. }
  74. public function onYsmReject(YsmRejectEvent $event): void
  75. {
  76. $payment = $event->getPayment();
  77. $reason = $event->getRejectReason();
  78. $this->paymentMailService->sendYsmRejectionNotification($payment, $reason);
  79. }
  80. public function onLmicApprove(LmicApproveEvent $event): void
  81. {
  82. $this->paymentMailService->sendLmicApprovalNotification($event->getPayment());
  83. }
  84. public function onLmicReject(LmicRejectEvent $event): void
  85. {
  86. $payment = $event->getPayment();
  87. $reason = $event->getRejectReason();
  88. $this->paymentMailService->sendLmicRejectionNotification($payment, $reason);
  89. }
  90. public function onLmicClarify(LmicClarifyEvent $event): void
  91. {
  92. $payment = $event->getPayment();
  93. $reason = $event->getRejectReason();
  94. $this->paymentMailService->sendLmicClarifyNotification($payment, $reason);
  95. }
  96. }