<?php
namespace App\EventHandler\Payment\Stripe;
use App\Entity\ShopOrder;
use App\EventListener\Payment\Stripe\StripeEvent;
use App\Repository\ShopOrderRepository;
use App\Service\Payment\Stripe\StripePaymentService;
use Doctrine\ORM\EntityManagerInterface;
use Stripe\Checkout\Session;
use Stripe\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class StripePayEvent implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var StripePaymentService
*/
private $stripePaymentService;
/**
* @var ShopOrderRepository
*/
private $shopOrderRepository;
public function __construct(
EntityManagerInterface $entityManager,
StripePaymentService $stripePaymentService,
ShopOrderRepository $shopOrderRepository
)
{
$this->entityManager = $entityManager;
$this->stripePaymentService = $stripePaymentService;
$this->shopOrderRepository = $shopOrderRepository;
}
public static function getSubscribedEvents(): array
{
return [
Event::CHECKOUT_SESSION_COMPLETED => 'onCheckoutSessionCompleted'
];
}
public function onCheckoutSessionCompleted(StripeEvent $stripeEvent)
{
/** @var Session $session */
$session = $stripeEvent->getResource();
$paymentData = $this->stripePaymentService->getPaymentDataFromStripeSession($session);
$this->shopOrderRepository->find($paymentData['orderId'])->setPaymentStatus(ShopOrder::PAYMENT_STATUTES['payed']);
$this->entityManager->flush();
}
}