<?php
namespace App\Controller;
use App\Entity\Order;
use App\Entity\HistoriquePaiement;
use App\Repository\CartRepository;
use App\Repository\OrderRepository;
use App\Repository\ForecastRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\EtatPaiementRepository;
use App\Repository\TypepaiementRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @Route("/commande", name="app_paiement_")
* @IsGranted("ROLE_USER")
*/
class PaiementMobileBankigController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em){
$this->em = $em;
}
/**
* @Route("/paiement/mobile/bankig/success/{reference}", name="mobile_bankig_success")
*/
public function success(
EtatPaiementRepository $repo,
$reference = null,
CartRepository $cartRepo,
TypepaiementRepository $typepaiementRepo,
OrderRepository $orderRepo
): Response
{
$order = $orderRepo->findOneByReference($reference);
return $this->render('paiement_mobile_bankig/success.html.twig', [
'order' => $order,
]);
}
/**
* @Route("/paiement/mobile/bankig/annuler/{reference}", name="mobile_bankig_cancel")
*/
public function annuler(
$reference = null,
OrderRepository $orderRepo
): Response
{
$order = $orderRepo->findOneByReference($reference);
return $this->render('paiement_mobile_bankig/annuler.html.twig', [
'order' => $order,
]);
}
/**
* @Route("/paiement/mobile/bankig/notifier", name="mobile_bankig_notifier")
*/
public function notifier(
EtatPaiementRepository $repo,
CartRepository $cartRepo,
TypepaiementRepository $typepaiementRepo,
OrderRepository $orderRepo
): Response
{
$data = json_decode(file_get_contents("php://input"));
if ($data->status == "SUCCESS") {
$carts = $cartRepo->findByUser($this->getUser());
foreach ($carts as $cart) {
$this->em->remove($cart);
}
$hitorique_paiement = new HistoriquePaiement();
$etatPaiement = $repo->findOneById(1);
$order = $orderRepo->findOneBystripeSessionId($data->notif_token);
$order->setEtatpaiement($etatPaiement)
->setMontantPaye($order->getTotal())
->setDateLastPaiement(new \DateTime());
$hitorique_paiement->setUser($this->getUser())
->setMyOrder($order)
->setMotantPaye($order->getTotal())
->setTypepaiement($order->getTypepaiement())
->setDatePaiement(new \DateTime())
->setIsValid(true);
$this->em->persist($hitorique_paiement);
$this->em->flush();
return $this->render('paiement_mobile_bankig/notifier.html.twig', [
'order' => $order,
]);
}
}
/***********************************************
* FORECAST
***********************************************/
/**
* @Route("/paiement/mobile/bankig/forecast/success/{reference}", name="mobile_bankig_forecast_success")
*/
public function successForecast(
EtatPaiementRepository $repo,
$reference = null,
CartRepository $cartRepo,
TypepaiementRepository $typepaiementRepo,
ForecastRepository $forecastRepo
): Response
{
$forecast = $forecastRepo->findOneByReference($reference);
return $this->render('paiement_mobile_bankig/success_forecast.html.twig', [
'order' => $forecast,
]);
}
/**
* @Route("/paiement/mobile/bankig/forecast/annuler/{reference}", name="mobile_bankig_forecast_cancel")
*/
public function annulerForecast(
$reference = null,
ForecastRepository $forecastRepo
): Response
{
$forecast = $forecastRepo->findOneByReference($reference);
return $this->render('paiement_mobile_bankig/annuler_forecast.html.twig', [
'order' => $forecast,
]);
}
/**
* @Route("/paiement/mobile/bankig/forecast/notifier", name="mobile_bankig_forecast_notifier")
*/
public function notifierForecast(
EtatPaiementRepository $repo,
CartRepository $cartRepo,
TypepaiementRepository $typepaiementRepo,
ForecastRepository $forecastRepo
): Response
{
$data = json_decode(file_get_contents("php://input"));
if ($data->status == "SUCCESS") {
$carts = $cartRepo->findByUser($this->getUser());
foreach ($carts as $cart) {
$this->em->remove($cart);
}
$hitorique_paiement = new HistoriquePaiement();
$etatPaiement = $repo->findOneById(1);
$forecast = $forecastRepo->findOneByReference($data->notif_token);
$forecast->setEtatpaiement($etatPaiement);
$hitorique_paiement->setUser($this->getUser())
->setForecast($forecast)
->setMotantPaye($forecast->getTotal())
->setTypepaiement($forecast->getTypepaiement())
->setDatePaiement(new \DateTime())
->setIsValid(true);
$this->em->persist($hitorique_paiement);
$this->em->flush();
return $this->render('paiement_mobile_bankig/notifier_forecast.html.twig', [
'order' => $order,
]);
}
}
}