src/EventSubscriber/GoCardlessMandateSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the CosaVostra, TrackPay package.
  4.  *
  5.  * (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber;
  12. use App\Entity\GoCardlessAccount;
  13. use App\Entity\GoCardlessMandate;
  14. use App\Event\GoCardlessMandateEvent;
  15. use App\Manager\PaymentManager;
  16. use App\Manager\ProcedureManager;
  17. use App\Repository\GoCardlessAccountRepository;
  18. use App\Service\GoCardlessService;
  19. use DateTime;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. class GoCardlessMandateSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var PaymentManager
  27.      */
  28.     protected $paymentManager;
  29.     /**
  30.      * @var GoCardlessService
  31.      */
  32.     protected $gocardlessService;
  33.     /**
  34.      * @var GoCardlessAccountRepository
  35.      */
  36.     protected $accountRepository;
  37.     /**
  38.      * @var ProcedureManager
  39.      */
  40.     protected $procedureManager;
  41.     /**
  42.      * @var EventDispatcherInterface
  43.      */
  44.     protected $eventDispatcher;
  45.     /**
  46.      * @var EntityManagerInterface
  47.      */
  48.     protected $entityManager;
  49.     public function __construct(PaymentManager $paymentManagerGoCardlessService $gocardlessServiceGoCardlessAccountRepository $accountRepositoryProcedureManager $procedureManagerEventDispatcherInterface $eventDispatcherEntityManagerInterface $entityManager)
  50.     {
  51.         $this->paymentManager $paymentManager;
  52.         $this->gocardlessService $gocardlessService;
  53.         $this->accountRepository $accountRepository;
  54.         $this->procedureManager $procedureManager;
  55.         $this->eventDispatcher $eventDispatcher;
  56.         $this->entityManager $entityManager;
  57.     }
  58.     public function onMandate(GoCardlessMandateEvent $event): void
  59.     {
  60.         $mandate $event->getMandate();
  61.         $invoice $mandate->getInvoice();
  62.         // Create a new payment record with pending status
  63.         $procedure $invoice->getProcedure();
  64.         if ($mandate->getChargeDate() <= new DateTime()) {
  65.             $account $this->accountRepository->findOneByUser($invoice->getUser());
  66.             // Pay now
  67.             $gcPayment $this->gocardlessService->connect($account->getCredential(GoCardlessAccount::CREDENTIAL_ACCESS_TOKEN))
  68.                     ->payments()->create([
  69.                             'params' => [
  70.                                     'amount' => $invoice->getDueAmount(),
  71.                                     'currency' => 'EUR',
  72.                                     'links' => [
  73.                                             'mandate' => $mandate->getMandateId()
  74.                                     ],
  75.                                     'metadata' => [
  76.                                             'invoice_number' => $invoice->getName()
  77.                                     ]
  78.                             ]
  79.                     ]);
  80.             $this->paymentManager->order($invoice $mandate->getInvoice(), $gcPayment->id$mandate);
  81.             $mandate->setStatus(GoCardlessMandate::STATUS_PROCESSING);
  82.             $this->entityManager->flush();
  83.             // Stop.
  84.             return;
  85.         }
  86.         // Pause the procedure related to the invoice.
  87.         $this->procedureManager->pause($procedure);
  88.     }
  89.     public static function getSubscribedEvents()
  90.     {
  91.         return [
  92.                 GoCardlessMandateEvent::class => 'onMandate'
  93.         ];
  94.     }
  95. }