src/Subscriber/Payment/EncashmentSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Payment;
  3. use App\Event\Payment\EncashmentEvent;
  4. use App\Lib\Payment\Settlement;
  5. use App\Service\Payment\BookingService;
  6. use App\Service\Payment\EncashmentService;
  7. use App\Service\Payment\PaymentService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class EncashmentSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EncashmentService
  13.      */
  14.     protected $encashmentService;
  15.     /**
  16.      * @var PaymentService
  17.      */
  18.     protected $service;
  19.     /**
  20.      * @var BookingService
  21.      */
  22.     protected $bookingService;
  23.     /**
  24.      * EncashmentSubscriber constructor.
  25.      */
  26.     public function __construct(PaymentService $paymentServiceBookingService $bookingServiceEncashmentService $encashmentService)
  27.     {
  28.         $this->encashmentService $encashmentService;
  29.         $this->service $paymentService;
  30.         $this->bookingService $bookingService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             EncashmentEvent::class => [
  36.                 ['onEncashmentCreated'0],
  37.             ],
  38.         ];
  39.     }
  40.     protected function getBookingService(): BookingService
  41.     {
  42.         return $this->bookingService;
  43.     }
  44.     protected function getPaymentService(): PaymentService
  45.     {
  46.         return $this->service;
  47.     }
  48.     /**
  49.      * @throws \Doctrine\ORM\ORMException
  50.      * @throws \Doctrine\ORM\OptimisticLockException
  51.      */
  52.     public function onEncashmentCreated(EncashmentEvent $event)
  53.     {
  54.         $encashment $event->getEncashment();
  55.         $account $encashment->getAccount();
  56.         $possibleSettlements $this->getPaymentService()->getPossibleSettlementsByAmount($account$encashment->getAmount());
  57.         $cleared 0;
  58.         $fee 0;
  59.         if (count($possibleSettlements)) {
  60.             $fee round($encashment->getFee() / count($possibleSettlements), 2);
  61.         }
  62.         foreach ($possibleSettlements as $chargeback) {
  63.             $settlement = new Settlement();
  64.             $settlement->setBookingId($chargeback->getBookingId());
  65.             $settlement->setAmount(abs($chargeback->getAmount()));
  66.             $settlement->setVendor($chargeback->getVendor());
  67.             $settlement->setCustomerId($chargeback->getCustomerId());
  68.             $settlement->setFee($fee);
  69.             $settlement->setRealamount(abs($chargeback->getAmount()) + $fee);
  70.             $payment $this->getBookingService()->storeConceptObject($settlement$account);
  71.             if ($encashment->paymentProcess) {
  72.                 $payment->setPaymentProcess($encashment->paymentProcess);
  73.             }
  74.             if ($encashment->paymentReference) {
  75.                 $payment->setPaymentReference($encashment->paymentReference);
  76.             }
  77.             $this->getPaymentService()->storePayment($payment, [
  78.                 'conceptObject' => $settlement,
  79.             ]);
  80.             $cleared += abs($chargeback->getAmount());
  81.             $encashment->resultingPayments[] = $payment;
  82.         }
  83.         $encashment->setCleared($cleared);
  84.         $this->encashmentService->storeEntity($encashment);
  85.     }
  86. }