vendor/boldr/mollie-payment-method/src/EventSubscriber/MollieEventSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace Boldr\Shop\MolliePaymentMethodBundle\EventSubscriber;
  3. use Boldr\Shop\ShopBundle\Event\{ InvoiceStatusUpdatedEvent, PaymentStatusUpdatedEvent };
  4. use Boldr\Shop\ShopBundle\Entity\Invoice;
  5. use Boldr\Shop\MolliePaymentMethodBundle\Entity\MolliePayment;
  6. use Boldr\Cms\MollieBundle\Event\MolliePaymentUpdatedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Psr\EventDispatcher\EventDispatcherInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Mollie\Api\MollieApiClient;
  11. class MollieEventSubscriber implements EventSubscriberInterface
  12. {
  13. private EventDispatcherInterface $eventDispatcher;
  14. private EntityManagerInterface $em;
  15. private MollieApiClient $mollieApiClient;
  16. public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $em, MollieApiClient $mollieApiClient)
  17. {
  18. $this->eventDispatcher = $eventDispatcher;
  19. $this->em = $em;
  20. $this->mollieApiClient = $mollieApiClient;
  21. }
  22. public static function getSubscribedEvents(): array
  23. {
  24. return [
  25. MolliePaymentUpdatedEvent::class => [
  26. ['onMolliePaymentUpdated', 10]
  27. ],
  28. InvoiceStatusUpdatedEvent::class => [
  29. ['onInvoiceStateUpdated', 0]
  30. ],
  31. PaymentStatusUpdatedEvent::class => [
  32. ['onPaymentStatusUpdated', 10]
  33. ]
  34. ];
  35. }
  36. public function onPaymentStatusUpdated(PaymentStatusUpdatedEvent $event)
  37. {
  38. $payment = $event->getPayment();
  39. if ($payment instanceof MolliePayment)
  40. {
  41. if ($event->getNewStatus() === MolliePayment::STATUS_CANCELLED)
  42. {
  43. try
  44. {
  45. $this->mollieApiClient->payments->cancel($payment->getMolliePaymentId());
  46. } catch (\Exception $ex) {
  47. // ignore silently
  48. }
  49. }
  50. }
  51. }
  52. public function onInvoiceStateUpdated(InvoiceStatusUpdatedEvent $event)
  53. {
  54. $invoice = $event->getInvoice();
  55. $orders = $invoice->getOrders();
  56. if (count($orders) > 1)
  57. {
  58. return;
  59. }
  60. if (isset($orders[0]))
  61. {
  62. $order = $orders[0];
  63. if ($order->getNumber() !== null)
  64. {
  65. foreach ($invoice->getPayments() as $payment)
  66. {
  67. if ($payment instanceof MolliePayment)
  68. {
  69. try
  70. {
  71. $molliePayment = $this->mollieApiClient->payments->get($payment->getMolliePaymentId());
  72. $molliePayment->description = 'Bestelling #'. $order->getNumber();
  73. $molliePayment->update();
  74. }
  75. catch (\Exception $ex)
  76. {
  77. // ignore silently
  78. }
  79. }
  80. }
  81. }
  82. }
  83. if ($event->getNewStatus() === Invoice::STATUS_PAID)
  84. {
  85. // Cancel pending mollie payments
  86. foreach ($invoice->getPayments() as $payment)
  87. {
  88. if ($payment instanceof MolliePayment)
  89. {
  90. if ($payment->getStatus() === MolliePayment::STATUS_PENDING)
  91. {
  92. try
  93. {
  94. $this->mollieApiClient->payments->cancel($payment->getMolliePaymentId());
  95. }
  96. catch (\Exception $ex)
  97. {
  98. // ignore silently
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. public function onMolliePaymentUpdated(MolliePaymentUpdatedEvent $event)
  106. {
  107. $molliePaymentRepository = $this->em->getRepository(MolliePayment::class);
  108. $payment = $event->getPayment();
  109. $molliePayment = $molliePaymentRepository->findOneBy(['molliePaymentId' => $payment->id]);
  110. if ($molliePayment === null || $molliePayment->getStatus() === MolliePayment::STATUS_FAILED)
  111. {
  112. return;
  113. }
  114. if ($molliePayment !== null)
  115. {
  116. $oldStatus = $molliePayment->getStatus();
  117. $newStatus = $payment->isPaid()
  118. ? MolliePayment::STATUS_PAID
  119. : ($payment->isFailed() || $payment->isCanceled() || $payment->isExpired() || $payment->getAmountRefunded() > 0
  120. ? MolliePayment::STATUS_FAILED
  121. : ($payment->hasChargebacks()
  122. ? MolliePayment::STATUS_CHARGEBACK
  123. : MolliePayment::STATUS_PENDING
  124. )
  125. )
  126. ;
  127. if ($oldStatus !== $newStatus)
  128. {
  129. $molliePayment->setStatus($newStatus);
  130. $this->em->flush();
  131. $event = new PaymentStatusUpdatedEvent($molliePayment, $oldStatus, $newStatus);
  132. $this->eventDispatcher->dispatch($event);
  133. }
  134. }
  135. }
  136. }