<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Compra;
use App\Entity\HistorialCompra;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
/**
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
class StripeController extends AbstractController {
/**
* @Route("/loks/detalles/{compra}", name="loks_tpv_detalles")
*/
public function detallescompra(Request $request, Compra $compra): Response {
$scheme = $request->getSchemeAndHttpHost();
$urlOK = $scheme . $this->generateUrl('loks_compra_ok');
return $this->render('web/pasarela.html.twig', [
'compra' => $compra,
'importe' => $compra->getPrecio(),
'urlOK' => $urlOK,
'fetch_url' => '/loks/comprar/' . $compra->getId() . '/stripe-api',
'public_key' => $this->getParameter("stripe_public_key")
]);
}
/**
* @Route("/loks/compraOK", name="loks_compra_ok")
*/
public function lokscompraok(): Response {
return $this->render('web/compraOK.html.twig');
}
/**
* @Route("/loks/comprar/{compra}/stripe-api", name="loks_tpv_stripe_api")
*/
public function stripeApi(Request $request, Compra $compra, EntityManagerInterface $entityManager): Response {
$total_a_pagar = $compra->getPrecio();
\Stripe\Stripe::setApiKey($this->getParameter("stripe_secret_key"));
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
// Create a PaymentIntent with amount and currency
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => $total_a_pagar * 100,
'currency' => 'eur',
'payment_method_types' => ['card']
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
$h = new HistorialCompra();
$h->setUser($this->getUser());
$h->setSecret($paymentIntent->client_secret);
$h->setFecha(new \DateTime());
$h->setHora(new \DateTime());
$h->setLoks($compra->getCreditos());
$entityManager->persist($h);
$entityManager->flush();
return new Response(json_encode($output));
} catch (Error $e) {
http_response_code(500);
$error = json_encode(['error' => $e->getMessage()]);
return new Response($error);
}
}
}