src/Subscriber/PaygreenSubscriber.php line 121

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Entity\Order;
  4. use App\Enum\Company\Config;
  5. use App\Enum\Order\Status;
  6. use App\Enum\PaymentEnum;
  7. use App\Repository\OrderRepository;
  8. use App\Repository\QuotationRepository;
  9. use App\Service\Paygreen\Exception\PaygreenPaymentException;
  10. use App\Service\PaygreenV3\API\PaygreenV3ServiceInterface;
  11. use App\Service\PaygreenV3\Core\PaygreenV3Payment;
  12. use App\Service\PaygreenV3\Event\PaygreenErrorPaymentEvent;
  13. use App\Service\PaygreenV3\Event\PaygreenSuccessPaymentEvent;
  14. use App\Service\PaygreenV3\Event\PaygreenWebhookEvent;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Ramsey\Uuid\Uuid;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class PaygreenSubscriber implements EventSubscriberInterface
  20. {
  21.     private PaygreenV3ServiceInterface $paygreenV3Service;
  22.     private EntityManagerInterface $entityManager;
  23.     private OrderRepository $orderRepository;
  24.     private QuotationRepository $quotationRepository;
  25.     private EventDispatcherInterface $eventDispatcher;
  26.     public function __construct(
  27.         EventDispatcherInterface $eventDispatcher,
  28.         EntityManagerInterface $entityManager,
  29.         PaygreenV3ServiceInterface $paygreenV3Service,
  30.         OrderRepository $orderRepository,
  31.         QuotationRepository $quotationRepository
  32.     ) {
  33.         $this->eventDispatcher $eventDispatcher;
  34.         $this->entityManager $entityManager;
  35.         $this->paygreenV3Service $paygreenV3Service;
  36.         $this->orderRepository $orderRepository;
  37.         $this->quotationRepository $quotationRepository;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             'paygreen.payment.webhook'   => 'onWebhook',
  43.             'paygreen.payment.successed' => 'onSuccessPayment',
  44.             'paygreen.payment.error'     => 'onErrorPayment'
  45.         ];
  46.     }
  47.     /**
  48.      * @throws PaygreenPaymentException
  49.      */
  50.     public function onWebhook(PaygreenWebhookEvent $event): void
  51.     {
  52.         /** @var Order $order */
  53.         $order $this->orderRepository->find(Uuid::fromString(explode('_'$event->data()['reference'])[0]));
  54.         if ($order === null || $order->getStatus()->getValue() !== Status::PENDING) {
  55.             return;
  56.         }
  57.         $company $order->getCompany();
  58.         if ($company === null) {
  59.             return;
  60.         }
  61.         $config $company->getConfigs()->get(Config::PAYGREEN_V3);
  62.         if ($config === null) {
  63.             return;
  64.         }
  65.         $client $this->paygreenV3Service->clientOf($company);
  66.         if (!$client->validate($event->data(), $event->sign(), $event->data()['status'], $event->url())) {
  67.             return;
  68.         }
  69.         $paygreenPayment null;
  70.         $payment null;
  71.         foreach ($order->getPayments() as $currentPayment) {
  72.             if (
  73.                 $currentPayment->getType() === PaymentEnum::PAYGREEN
  74.                     && $currentPayment->getStatus()->getValue() === Status::PENDING
  75.             ) {
  76.                 $paygreenPayment = new PaygreenV3Payment($currentPayment->getInfo());
  77.                 if ($paygreenPayment->id() === $event->data()['id']) {
  78.                     $payment $currentPayment;
  79.                     break;
  80.                 }
  81.             }
  82.         }
  83.         if ($payment === null || $paygreenPayment === null) {
  84.             return;
  85.         }
  86.         $paygreenPayment $client->transaction()->update($paygreenPayment);
  87.         switch ($event->data()['status']) {
  88.             case 'payment_order.authorized':
  89.             case 'payment_order.successed':
  90.                 $this->eventDispatcher->dispatch(
  91.                     new PaygreenSuccessPaymentEvent($order$paygreenPayment$payment),
  92.                     'paygreen.payment.successed'
  93.                 );
  94.                 break;
  95.             default:
  96.                 $this->eventDispatcher->dispatch(
  97.                     new PaygreenErrorPaymentEvent($order$paygreenPayment$payment),
  98.                     'paygreen.payment.error'
  99.                 );
  100.         }
  101.     }
  102.     public function onSuccessPayment(PaygreenSuccessPaymentEvent $event): void
  103.     {
  104.         try {
  105.             $this->entityManager->beginTransaction();
  106.             $event->orderPayment()
  107.                 ->setInfo($event->paygreenPayment()->toArray())
  108.                 ->setStatus(Status::SUCCESSED());
  109.             $event->order()->setStatus(Status::SUCCESSED());
  110.             $this->entityManager->persist($event->orderPayment());
  111.             $this->entityManager->persist($event->order());
  112.             $this->entityManager->flush();
  113.             $this->entityManager->commit();
  114.         } catch (\Throwable $throwable) {
  115.             $this->entityManager->rollback();
  116.         }
  117.     }
  118.     public function onErrorPayment(PaygreenErrorPaymentEvent $event): void
  119.     {
  120.         try {
  121.             $this->entityManager->beginTransaction();
  122.             $event->orderPayment()
  123.                 ->setInfo($event->paygreenPayment()->toArray())
  124.                 ->setStatus(Status::REFUSED());
  125.             $quotation $this->quotationRepository->findOneBy(['order' => $event->order()]);
  126.             if ($quotation === null) {
  127.                 $event->order()->setStatus(Status::ERROR());
  128.             }
  129.             $this->entityManager->persist($event->orderPayment());
  130.             $this->entityManager->persist($event->order());
  131.             $this->entityManager->flush();
  132.             $this->entityManager->commit();
  133.         } catch (\Throwable $throwable) {
  134.             $this->entityManager->rollback();
  135.         }
  136.     }
  137. }