src/EventSubscriber/Payment/StripePaymentSubscriber.php line 37

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  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\Payment;
  12. use App\Entity\Payment\Payment;
  13. use App\PaymentStates;
  14. use App\Repository\Payment\PaymentRepository;
  15. use FluxSE\PayumStripe\Action\NotifyAction;
  16. use Payum\Core\Bridge\Symfony\Event\ExecuteEvent;
  17. use Payum\Core\Bridge\Symfony\PayumEvents;
  18. use Payum\Core\Request\GetHumanStatus;
  19. use Payum\Core\Request\Notify;
  20. use Psr\Log\LoggerInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Workflow\WorkflowInterface;
  23. class StripePaymentSubscriber implements EventSubscriberInterface
  24. {
  25.     public function __construct(
  26.         private LoggerInterface $logger,
  27.         private PaymentRepository $paymentRepository,
  28.         private WorkflowInterface $paymentStateMachine,
  29.     ) {
  30.     }
  31.     public function onPostExecute(ExecuteEvent $event)
  32.     {
  33.         $context $event->getContext();
  34.         $action $context->getAction();
  35.         $request $context->getRequest();
  36.         if ($action instanceof NotifyAction && $request instanceof Notify) {
  37.             $payment $request->getFirstModel();
  38.             if (!$payment instanceof Payment) {
  39.                 return;
  40.             }
  41.             $context->getGateway()->execute($status = new GetHumanStatus($payment));
  42.             $this->logger->debug(sprintf("Stripe payment status is '%s'."$status->getValue()));
  43.             /** @var Payment $payment */
  44.             $payment $status->getFirstModel();
  45.             if ($status->isCaptured() && $this->paymentStateMachine->can($paymentPaymentStates::TRANSITION_COMPLETE)) {
  46.                 $this->paymentStateMachine->apply($paymentPaymentStates::TRANSITION_COMPLETE);
  47.                 $this->paymentRepository->add($payment);
  48.             } elseif ($status->isCanceled() && $this->paymentStateMachine->can($paymentPaymentStates::TRANSITION_CANCEL)) {
  49.                 $this->paymentStateMachine->apply($paymentPaymentStates::TRANSITION_CANCEL);
  50.                 $this->paymentRepository->add($payment);
  51.             }
  52.         }
  53.     }
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             PayumEvents::GATEWAY_POST_EXECUTE => 'onPostExecute',
  58.         ];
  59.     }
  60. }