src/Services/PaymentBundle/Event/PaymentSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace Services\PaymentBundle\Event;
  3. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use App\BackOffice\ConfigurationBundle\Services\ApiPayment\BillingService;
  6. use Psr\Log\LoggerInterface;
  7. use Services\PaymentBundle\Entity\PaymentStatus;
  8. use Services\PaymentBundle\Repository\BillingRepository;
  9. class PaymentSubscriber implements EventSubscriberInterface
  10. {
  11.     private BillingRepository $billingRepository;
  12.     private BillingService $billingService;
  13.     private LoggerInterface $logger;
  14.     public function __construct(
  15.         BillingRepository $billingRepository,
  16.         BillingService $billingService,
  17.         LoggerInterface $logger
  18.     ) {
  19.         $this->billingRepository $billingRepository;
  20.         $this->billingService $billingService;
  21.         $this->logger $logger;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             PaymentEvent::PAYMENT_MODIFICATION_EVENT     => array('onPaymentUpdated'0),
  27.         ];
  28.     }
  29.     /**
  30.      * Update the billing table with the information of the updated Payment
  31.      */
  32.     public function onPaymentUpdated(PaymentEvent $event)
  33.     {
  34.         $payment $event->getPayment();
  35.         $user $payment->getUser();
  36.         $campaignType $user->getUserInfos()->getCampaignType();
  37.         $periodMonths $campaignType->getDays();
  38.         $pdate $payment->getDebtPeriodDate() ?? $payment->getSubscriptionDate();
  39.         $subtotal $campaignType->getPrice();
  40.         $paymentStatusId $payment->getPaymentStatus()->getId();
  41.         if ($payment->getIsFirst()) {
  42.             $subtotal $campaignType->getFirstPrice();
  43.         } elseif ($campaignType->getSecondPrice()) {
  44.             $billingsList $this->billingRepository->getBillingsByUser($user);
  45.             if (count($billingsList) == 1) { // This is the second period
  46.                 $subtotal $campaignType->getSecondPrice();
  47.             }
  48.         }
  49.         $currentPeriod $this->billingService->getCurrentPeriodStartEnd(
  50.             $user,
  51.             $pdate,
  52.             $periodMonths,
  53.             $payment->getDebtPeriodDate() ? false true
  54.         );
  55.         $billing $currentPeriod['billing'] ?? null;
  56.         if (!$billing && $currentPeriod) {
  57.             $billing $this->billingRepository->findByUserAndPeriodStart($user$currentPeriod['start']);
  58.         }
  59.         if (!$billing) {
  60.             if (PaymentStatus::STATUS_PAIEMENT_SIMPLE == $paymentStatusId) {
  61.                 $billing $this->billingRepository->add(
  62.                     $user,
  63.                     $subtotal,
  64.                     false,
  65.                     $currentPeriod['start'],
  66.                     $currentPeriod['end'],
  67.                     $payment->getSubscriptionDate()
  68.                 );
  69.             } elseif (PaymentStatus::STATUS_RETRY_DEBT == $paymentStatusId) {
  70.                 $start = (clone $payment->getDebtPeriodDate())->modify('first day of this month');
  71.                 $end $payment->getDebtPeriodDate();
  72.                 $billing $this->billingRepository->findByUserAndDebtMonth($user$start);
  73.                 if (!$billing) {
  74.                     $billing $this->billingRepository->add(
  75.                         $user,
  76.                         $subtotal,
  77.                         false,
  78.                         $start,
  79.                         $end->add(new \DateInterval('P' $campaignType->getDays() . 'M'))->modify('last day of this month'),
  80.                         $payment->getSubscriptionDate()
  81.                     );
  82.                 }
  83.             } else {
  84.                 $billingList $this->billingRepository->generateFromUserPayments($user);
  85.                 foreach ($billingList as $b) {
  86.                     if ($currentPeriod['start'] == $b->getPeriodStart()) {
  87.                         $billing $b;
  88.                         break;
  89.                     }
  90.                 }
  91.             }
  92.             if ($billing) {
  93.                 $payment->setBilling($billing);
  94.             }
  95.         } else {
  96.             $fullQuotasPaid $payment->getTransactionPrice() >= $subtotal 0;
  97.             $splitQuotasPaid $fullQuotasPaid 1;
  98.             $paidAmountSplit $fullQuotasPaid $payment->getTransactionPrice();
  99.             $paidAmountFull $fullQuotasPaid $payment->getTransactionPrice() : 0;
  100.             if (
  101.                 in_array(
  102.                     $event->getEventType(),
  103.                     [PaymentEvent::PAYMENT_SUCCESSPaymentEvent::PAYMENT_CHARGEBACK_REVERSED]
  104.                 )
  105.             ) {
  106.                 $billing->setFullQuotasPaid($billing->getFullQuotasPaid() + $fullQuotasPaid);
  107.                 $billing->setSplitQuotasPaid($billing->getSplitQuotasPaid() + $splitQuotasPaid);
  108.                 $billing->setPaidAmountFull($billing->getPaidAmountFull() + $paidAmountFull);
  109.                 $billing->setPaidAmountSplit($billing->getPaidAmountSplit() + $paidAmountSplit);
  110.                 $billing->setPaidAmount($billing->getPaidAmount() + $payment->getTransactionPrice());
  111.             } elseif (
  112.                 in_array($event->getEventType(), [PaymentEvent::PAYMENT_REFUNDPaymentEvent::PAYMENT_CHARGEBACK])
  113.             ) {
  114.                 $fullQuotasPaid $billing->getFullQuotasPaid() - $fullQuotasPaid;
  115.                 $billing->setFullQuotasPaid($fullQuotasPaid 0.9 $fullQuotasPaid);
  116.                 $splitQuotasPaid $billing->getSplitQuotasPaid() - $splitQuotasPaid;
  117.                 $billing->setSplitQuotasPaid($splitQuotasPaid 0.9 $splitQuotasPaid);
  118.                 $billing->setPaidAmountFull($billing->getPaidAmountFull() - $paidAmountFull);
  119.                 $billing->setPaidAmountSplit($billing->getPaidAmountSplit() - $paidAmountSplit);
  120.                 $billing->setPaidAmount($billing->getPaidAmount() - $payment->getTransactionPrice());
  121.             }
  122.             if (!$billing->getSubscriptionDate()) {
  123.                 $billing->setSubscriptionDate($payment->getSubscriptionDate());
  124.             }
  125.             // Link payment to its billing
  126.             $payment->setBilling($billing);
  127.         }
  128.         if ($billing) {
  129.             $billing->setIsPaid(($billing->getPaidAmount()) >= $subtotal);
  130.             $billing->setUpdatedAt(new \DateTime());
  131.             if (!$billing->getSubscriptionDate()) {
  132.                 $billing->setSubscriptionDate($payment->getSubscriptionDate());
  133.             }
  134.         }
  135.         try {
  136.             $this->billingRepository->flush();
  137.         } catch (\Exception $e) {
  138.             $this->logger->error('PaymentSubscriber::onPaymentUpdated: ' $e->getMessage());
  139.         }
  140.         // Clear cache of total debt calculation for this user
  141.         $cache = new FilesystemAdapter();
  142.         $cacheKey BillingService::TOTAL_DEBT_CACHE_KEY '_' $user->getId();
  143.         $cache->delete($cacheKey);
  144.     }
  145. }