src/Controller/StripeController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Entity\Compra;
  9. use App\Entity\HistorialCompra;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. /**
  12.  * @IsGranted("IS_AUTHENTICATED_FULLY")
  13.  */
  14. class StripeController extends AbstractController {
  15.     /**
  16.      * @Route("/loks/detalles/{compra}", name="loks_tpv_detalles")
  17.      */
  18.     public function detallescompra(Request $requestCompra $compra): Response {
  19.         $scheme $request->getSchemeAndHttpHost();
  20.         $urlOK $scheme $this->generateUrl('loks_compra_ok');
  21.         
  22.         return $this->render('web/pasarela.html.twig', [
  23.                     'compra' => $compra,
  24.                     'importe' => $compra->getPrecio(),
  25.                     'urlOK' => $urlOK,
  26.                     'fetch_url' => '/loks/comprar/' $compra->getId() . '/stripe-api',
  27.                     'public_key' => $this->getParameter("stripe_public_key")
  28.         ]);
  29.     }
  30.     /**
  31.      * @Route("/loks/compraOK", name="loks_compra_ok")
  32.      */
  33.     public function lokscompraok(): Response {
  34.         return $this->render('web/compraOK.html.twig');
  35.     }
  36.     /**
  37.      * @Route("/loks/comprar/{compra}/stripe-api", name="loks_tpv_stripe_api")
  38.      */
  39.     public function stripeApi(Request $requestCompra $compraEntityManagerInterface $entityManager): Response {
  40.         $total_a_pagar $compra->getPrecio();
  41.         \Stripe\Stripe::setApiKey($this->getParameter("stripe_secret_key"));
  42.         try {
  43.             // retrieve JSON from POST body
  44.             $jsonStr file_get_contents('php://input');
  45.             $jsonObj json_decode($jsonStr);
  46.             // Create a PaymentIntent with amount and currency
  47.             $paymentIntent = \Stripe\PaymentIntent::create([
  48.                         'amount' => $total_a_pagar 100,
  49.                         'currency' => 'eur',
  50.                         'payment_method_types' => ['card']
  51.             ]);
  52.             $output = [
  53.                 'clientSecret' => $paymentIntent->client_secret,
  54.             ];
  55.             $h = new HistorialCompra();
  56.             $h->setUser($this->getUser());
  57.             $h->setSecret($paymentIntent->client_secret);
  58.             $h->setFecha(new \DateTime());
  59.             $h->setHora(new \DateTime());
  60.             $h->setLoks($compra->getCreditos());
  61.             $entityManager->persist($h);
  62.             $entityManager->flush();
  63.             return new Response(json_encode($output));
  64.         } catch (Error $e) {
  65.             http_response_code(500);
  66.             $error json_encode(['error' => $e->getMessage()]);
  67.             return new Response($error);
  68.         }
  69.     }
  70. }