src/Controller/CartController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Product;
  4. use App\Entity\PromoCode;
  5. use App\Entity\Tax;
  6. use App\Libs\Constantes;
  7. use App\Libs\PromoDiscounter;
  8. use App\Libs\Taxer;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Stripe\StripeClient;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class CartController extends AbstractController
  19. {
  20. /**
  21. * @var EntityManagerInterface
  22. */
  23. private $em;
  24. /**
  25. * @var RequestStack
  26. */
  27. private $stack;
  28. public function __construct(EntityManagerInterface $em, RequestStack $stack)
  29. {
  30. $this->em = $em;
  31. $this->stack = $stack->getSession();
  32. }
  33. /**
  34. * @Route("/cart", name="app_cart")
  35. */
  36. public function index(Request $request): Response
  37. {
  38. $promo_code = $this->stack->get('promo_code');
  39. // if($promo_code) return $this->redirectToRoute('app_checkout_aplicado');
  40. $em = $this->em;
  41. $productos = $em->getRepository(Product::class)->findBy(['isPublico' => 1, 'isActivo' => 1]);
  42. $rs = [];
  43. $form = $this->createFormBuilder()->setAction($this->generateUrl('app_checkout'))
  44. ->add('ids', HiddenType::class)
  45. ->getForm();
  46. $form->handleRequest($request);
  47. if($form->isSubmitted() && $form->isValid()){
  48. $data = $form->getData();
  49. if($data){
  50. $subtotal = 0;
  51. $ids = explode(',', $data['ids']);
  52. if($ids)
  53. $pcd = new PromoDiscounter($this->em);
  54. $pcd->setCodeNombre($promo_code);
  55. foreach ($ids as $id){
  56. $p = $this->em->getRepository(Product::class)->find($id);
  57. if(!$p) return $this->redirectToRoute('app_cart');
  58. $amt = $pcd->getDiscountedAmt($p->getRatePrecio());
  59. $rs[] = ['id' => $id, 'content' => $p->getProductName(), 'precio' => $amt];
  60. $subtotal += $amt;
  61. }
  62. }else{
  63. return $this->redirectToRoute('app_cart');
  64. }
  65. //dump($subtotal);exit;
  66. $t = new Taxer();
  67. $t->setAmt($subtotal)->setTaxRepository($this->em->getRepository(Tax::class))->build();
  68. $tax = $t->getAddedTax();
  69. $stripe = new StripeClient(Constantes::API_KEY);
  70. $intent = $stripe->paymentIntents->create([
  71. 'automatic_payment_methods' => ['enabled' => true],
  72. 'amount' => ($subtotal + $tax)*100,
  73. 'currency' => 'usd',
  74. 'metadata' => ['items' => $data['ids'], 'promoCode' => $pcd->getCodeNombre()]
  75. ]);
  76. $this->stack->set('intent', $intent->id);
  77. $this->stack->set('secret', $intent->client_secret);
  78. //dump($intent);exit();
  79. $form2 = $this->createFormBuilder()->setAction($this->generateUrl('app_checkout'))
  80. ->add('ids', HiddenType::class, [
  81. 'data' => $data['ids']
  82. ])->add('promoCode', TextType::class, [
  83. 'data' => $promo_code,
  84. 'label' => false,
  85. 'attr' => ['class'=>'uk-input', 'placeholder' => 'Código de Promoción']
  86. ])->add('secret', HiddenType::class, [
  87. 'data' => $intent->client_secret
  88. ])->add('intent', HiddenType::class, [
  89. 'data' => $intent->id
  90. ])->getForm();
  91. $payForm = $this->createFormBuilder()
  92. ->add('secret', HiddenType::class, [
  93. 'data' => $this->stack->get('secret')
  94. ])->add('intent', HiddenType::class, [
  95. 'data' => $this->stack->get('intent')
  96. ])->getForm();
  97. return $this->render('home/checkout.html.twig', [
  98. 'controller_name' => 'HomeController',
  99. 'prods' => $rs,
  100. 'subtotal' => $subtotal,
  101. 'payForm' => $payForm->createView(),
  102. 'listForm' => $form2->createView(),
  103. 'tax' => $tax,
  104. 'ids' => $data['ids'],
  105. 'codigoStatus' => $pcd->getStatus(),
  106. 'codigoMessage' => $pcd->getMessage(),
  107. 'code' => $promo_code,
  108. 'client_secret' => $intent->client_secret,
  109. 'intent' => $intent->id,
  110. 'api' => Constantes::API_KEY_PK,
  111. 'promo_code' => strtoupper($promo_code)
  112. ]);
  113. }
  114. return $this->render('home/cart.html.twig', [
  115. 'controller_name' => 'HomeController',
  116. 'prods' => $productos,
  117. 'form' => $form->createView(),
  118. 'promo_code' => strtoupper($promo_code)
  119. ]);
  120. }
  121. /**
  122. * @Route("/cart/checkout", name="app_checkout")
  123. */
  124. public function checkout(Request $request)
  125. {
  126. $promo_code = $this->stack->get('promo_code');
  127. if(!$this->stack->get('secret')) return $this->redirectToRoute('app_cart');
  128. // if(!$this->stack->get('promo_code')) $promo_code = $this->stack->getSession()->get('promo_code');
  129. $rs = []; $codigoAplicado = 0; $subtotal = 0;$tax = 0;
  130. $stripe = new StripeClient(Constantes::API_KEY);
  131. $form2 = $this->createFormBuilder()->setAction($this->generateUrl('app_checkout'))
  132. ->add('ids', HiddenType::class, [
  133. ])->add('promoCode', TextType::class, [
  134. 'data' => $promo_code,
  135. 'label' => false,
  136. 'attr' => ['class'=>'uk-input', 'placeholder' => 'Código de Promoción']
  137. ])->add('secret', HiddenType::class, [
  138. ])->add('intent', HiddenType::class, [
  139. ])
  140. ->getForm();
  141. $form2->handleRequest($request);
  142. if($form2->isSubmitted() && $form2->isValid()){
  143. $data = $form2->getData();
  144. if($data){
  145. $subtotal = 0;
  146. $ids = explode(',', $data['ids']);
  147. $pcd = new PromoDiscounter($this->em);
  148. $pcd->setCodeNombre($data['promoCode']);
  149. foreach ($ids as $id){
  150. $p = $this->em->getRepository(Product::class)->find($id);
  151. $amt = $pcd->getDiscountedAmt($p->getRatePrecio());
  152. $rs[] = ['id' => $id, 'content' => $p->getProductName(), 'precio' => $amt, 'idPromo' => $pcd->getCodeId()];
  153. $subtotal += $amt;
  154. }
  155. $t = new Taxer();
  156. $t->setAmt($subtotal)->setTaxRepository($this->em->getRepository(Tax::class))->build();
  157. $tax = $t->getAddedTax();
  158. $form = $this->createFormBuilder()
  159. ->add('ids', HiddenType::class, [
  160. 'data' => $data['ids']
  161. ])
  162. ->add('promoCode', TextType::class, [
  163. 'label' => false,
  164. 'attr' => ['class'=>'uk-input'],
  165. 'disabled' => false
  166. ])
  167. ->getForm();
  168. $intent = $stripe->paymentIntents->update($data['intent'],[
  169. 'amount' => ($subtotal + $tax)*100,
  170. 'currency' => 'usd',
  171. 'metadata' => ['items' => $data['ids'], 'promoCode' => $pcd->getCodeNombre()]
  172. ]);
  173. }
  174. }
  175. //dump($pcd);exit();
  176. $payForm = $this->createFormBuilder()
  177. ->add('secret', HiddenType::class, [
  178. 'data' => $this->stack->get('secret')
  179. ])->add('intent', HiddenType::class, [
  180. 'data' => $this->stack->get('intent')
  181. ])
  182. ->getForm();
  183. $payForm->handleRequest($request);
  184. if($payForm->isSubmitted() && $payForm->isValid()){
  185. $data = $payForm->getData();
  186. $confirm = $stripe->paymentIntents->confirm($data['intent'],
  187. ['payment_method' => 'pm_card_visa']
  188. );
  189. }
  190. return $this->render('home/checkout.html.twig', [
  191. 'controller_name' => 'HomeController',
  192. 'prods' => $rs,
  193. 'subtotal' => $subtotal,
  194. 'tax' => $tax,
  195. 'form' => $form2->createView(),
  196. 'listForm' => $form2->createView(),
  197. 'payForm' => $payForm->createView(),
  198. 'codigoStatus' => $pcd->getStatus(),
  199. 'codigoMessage' => $pcd->getMessage(),
  200. 'code' => strtoupper($pcd->getCodeNombre()),
  201. 'promo_code' => null,
  202. 'api' => Constantes::API_KEY_PK,
  203. 'client_secret' => $this->stack->get('secret')
  204. ]);
  205. }
  206. }