src/Controller/DefaultController.php line 327

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\Contracts\HttpClient\HttpClientInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. class DefaultController extends AbstractController
  10. {
  11.     private $client;
  12.     public function __construct(HttpClientInterface $client)
  13.     {
  14.         $this->client $client;
  15.     }
  16.     public function index(): Response
  17.     {
  18.         $apiToken $this->getParameter('api_token');
  19.         $apiBaseUrl $this->getParameter('api_url');
  20.         
  21.         try {
  22.             $response $this->client->request(
  23.                 'GET',
  24.                 $apiBaseUrl.'products?access_token='.$apiToken.'&orderby=price'
  25.             );
  26.             $slug = array();
  27.             $i 0;
  28.             $content $response->toArray();
  29.             /*
  30.             foreach($content as $key => $value) {
  31.                 if($value){
  32.                   
  33.                     switch($value["id"]){
  34.                         case 611: // Cromas Carmenere 2020 los vascos
  35.                             $slug[0] =  $value["slug"];
  36.                             break;
  37.                         case 612: // Cromas Cabernet Franc Gran Reserva 2019 Los Vascos
  38.                             $slug[1] =  $value["slug"];
  39.                             break;
  40.                         case 624: // Cromas Cabernet Sauvignon Gran Reserva 2018 Los Vascos
  41.                             $slug[2] =  $value["slug"];
  42.                             break;
  43.                         case 614: // Cromas Syrah Gran Reserva 2019 Los Vascos
  44.                             $slug[3] =  $value["slug"];
  45.                             break;
  46.                         case 615: // Chardonnay 2021 Los Vascos
  47.                             $slug[4] =  $value["slug"];
  48.                             break;
  49.                         case 616: // Cabernet Sauvignon 2019 Los Vascos
  50.                             $slug[5] =  $value["slug"];
  51.                             break;
  52.                         case 617: // Rosé 2022 Los Vascos
  53.                             $slug[6] =  $value["slug"];
  54.                             break;
  55.                         case 618: // Sauvignon Blanc 2021 Los Vascos
  56.                             $slug[7] =  $value["slug"];
  57.                             break;
  58.                         case 547: // Le Dix 2017
  59.                             $slug[8] =  $value["slug"];
  60.                             break;
  61.                         case 620: // Magnum Cromas Cabernet Sauvignon Gran Reserva 2018 Los Vascos
  62.                             $slug[9] =  $value["slug"];
  63.                             break;
  64.                         case 621: // Magnum Cromas Carmenere 2020 Los Vascos
  65.                             $slug[10] =  $value["slug"];
  66.                             break;
  67.                        
  68.                     
  69.                     }
  70.                     $i++;
  71.                 }
  72.                 
  73.             }*/
  74.             $parameters["slug"] = $content
  75.             
  76.         } catch (Exception $e) {
  77.             dump("f");
  78.         }
  79.         
  80.         return $this->render('home.html.twig',$parameters);
  81.     }
  82.     public function contacto(): Response
  83.     {
  84.         return $this->render('contacto.html.twig');
  85.     }
  86.     public function vinos(Request $request): Response
  87.     {
  88.         $apiToken $this->getParameter('api_token');
  89.         $apiBaseUrl $this->getParameter('api_url');
  90.         $valmin $request->get('valmin');
  91.         $valmax $request->get('valmax');
  92.         $tags $request->get('etiquetas');
  93.        
  94.         try {
  95.             $response $this->client->request(
  96.                 'GET',
  97.                 $apiBaseUrl.'products?access_token='.$apiToken.'&tag='.$tags.'&price_min='.$valmin.'&price_max='.$valmax.'&orderby=price'
  98.             );
  99.             $content $response->toArray();
  100.             $parameters["products"] = $content;
  101.             $i 0;
  102.             foreach ($parameters["products"] as $data) {
  103.                 if($data["tags"] != null ){
  104.                     $tag explode(","$data['tags']);
  105.                     foreach ($tag as $t){
  106.                         if($t == "linea1"){
  107.                             $data["linea"] = 1;
  108.                             $parameters["products"][$i] = $data;
  109.                         }
  110.                         if($t == "linea2"){
  111.                             $data["linea"] = 2;
  112.                             $parameters["products"][$i] = $data;
  113.                         }
  114.                         if($t == "linea3"){
  115.                             $data["linea"] = 3;
  116.                             $parameters["products"][$i] = $data;
  117.                         }
  118.                         if($t == "Mendoza"){
  119.                             $data["linea"] = 4;
  120.                             $parameters["products"][$i] = $data;
  121.                         }                        
  122.                     }                   
  123.                 }
  124.                 $i++;
  125.             }          
  126.             $parameters["valmin"] = $valmin;
  127.             $parameters["valmax"] = $valmax;
  128.             $parameters["tags"] = $tags;
  129.         } catch (Exception $e) {
  130.             dump("f");
  131.         }
  132.         return $this->render('vinos.html.twig'$parameters);
  133.     }
  134.     public function producto(Request $request$slug): Response
  135.     {
  136.         $apiToken $this->getParameter('api_token');
  137.         $apiBaseUrl $this->getParameter('api_url');
  138.         
  139.         $tag =  $request->query->get('tag');     
  140.      
  141.         try {
  142.             $response $this->client->request(
  143.                 'GET',
  144.                 $apiBaseUrl.'products/slug/'.$slug.'?access_token='.$apiToken
  145.             );
  146.             $response2 $this->client->request(
  147.                 'GET',
  148.                 $apiBaseUrl.'products?access_token='.$apiToken.'&tag='.$tag
  149.             );
  150.           
  151.             $content $response->toArray();
  152.             $content2 $response2->toArray();
  153.            
  154.             $tags explode(","$content['tags']);
  155.             
  156.             $parameters["product"] = $content;
  157.             $parameters["products"] = $content2;
  158.             $parameters["tags"] = $tags;
  159.             //$parameters["metadata"]= $this->metadata($content);
  160.         } catch (Exception $e) {
  161.             dump("f");
  162.         }
  163.         return $this->render('producto.html.twig'$parameters);
  164.     }
  165.     public function productoId(Request $request$id): Response
  166.     {
  167.         $apiToken $this->getParameter('api_token');
  168.         $apiBaseUrl $this->getParameter('api_url');
  169.         try {
  170.             $response $this->client->request(
  171.                 'GET',
  172.                 $apiBaseUrl.'products/'.$id.'?access_token='.$apiToken
  173.             );
  174.             $response2 $this->client->request(
  175.                 'GET',
  176.                 $apiBaseUrl.'marketplace/products?access_token='.$apiToken
  177.             );
  178.             $content $response->toArray();
  179.             $content2 $response2->toArray();
  180.             $tags explode(","$content['tags']);
  181.             $parameters["product"] = $content;
  182.             $parameters["products"] = $content2;
  183.             $parameters["tags"] = $tags;
  184.             //$parameters["metadata"]= $this->metadata($content);
  185.         } catch (Exception $e) {
  186.             dump("f");
  187.         }
  188.         return $this->render('producto.html.twig'$parameters);
  189.     }
  190.     
  191.     public function addToCart(Request $request)
  192.     {   
  193.         
  194.         $productObject["product_id"] = intval($request->get('product_id'));
  195.         $productObject["variant_id"] = intval($request->get('variant_id'));
  196.         $productObject["quantity_products"] = intval($request->get('quantity_products'));
  197.       
  198.         
  199.         if($request->getSession()->get('cartId') == null){
  200.             
  201.             $productObject["cart_id"] = null;
  202.         }
  203.         else {
  204.             $productObject["cart_id"] = intval($request->getSession()->get('cartId'));
  205.         }
  206.         $product json_encode($productObject);
  207.         
  208.         $apiToken $this->getParameter('api_token');
  209.         $apiBaseUrl $this->getParameter('api_url');
  210.         
  211.         $response $this->client->request(
  212.             'POST',
  213.             $apiBaseUrl.'products/shopping-cart?access_token='.$apiToken,
  214.             [
  215.                 'body' => $product,
  216.             ]
  217.         );
  218.         $content $response->toArray()[0];
  219.         
  220.         $request->getSession()->set('cartId',$content['cart_id']);
  221.         return $this->redirectToRoute('carrito');
  222.     }
  223.     public function currentItems(Request $request): Response
  224.     {
  225.         $apiToken $this->getParameter('api_token');
  226.         $apiBaseUrl $this->getParameter('api_url');
  227.         $cartId $request->getSession()->get('cartId');
  228.         
  229.         if ($cartId != null){
  230.             $response $this->client->request(
  231.                 'GET',
  232.                 $apiBaseUrl.'products/shopping-cart/'.$cartId.'?access_token='.$apiToken
  233.             );
  234.             $cart $response->toArray();
  235.             if(count($cart) == 1){
  236.                 $cart $cart[0];
  237.             }
  238.             $total_tours_apalta 0;
  239.             foreach($cart["bookings"] as $booking){
  240.                 if($booking['tour_subcategoria'] == "Apalta"){
  241.                     $total_tours_apalta++;
  242.                 }
  243.             }            
  244.             $cart['total_tours_apalta'] = $total_tours_apalta;
  245.             
  246.             return new JsonResponse($cart);
  247.         }else{
  248.             return new JsonResponse(null);
  249.         }
  250.     }
  251.     public function deleteFromCart(Request $request,$saleId)
  252.     {
  253.         $code $request->get('code');
  254.         $apiToken $this->getParameter('api_token');
  255.         $apiBaseUrl $this->getParameter('api_url');
  256.        
  257.         
  258.         $cartId $request->getSession()->get('cartId');
  259.         if ($cartId != null)
  260.         {
  261.             $response $this->client->request(
  262.                 'DELETE',
  263.                 $apiBaseUrl.'products/shopping-cart/sale/'.strval($saleId).'?access_token='.$apiToken
  264.             );
  265.         $cart $response->toArray(); 
  266.        
  267.          if($cart[0]['products_sales']==[] && $cart[0]['bookings']==[]){
  268.             $request->getSession()->remove('cartId');
  269.          }
  270.         }
  271.         return $this->redirectToRoute('carrito', array('code'=>$code));
  272.     }
  273.     public function checkProductVariant(Request $request)
  274.     {
  275.         $apiToken $this->getParameter('api_token');
  276.         $apiBaseUrl $this->getParameter('api_url');
  277.         $variantName $request->query->get('variantName');
  278.         $productId $request->query->get('productId');
  279.         
  280.         try {
  281.             $response $this->client->request(
  282.                 'GET',
  283.                 $apiBaseUrl.'products/check/variant?variantName='.$variantName.'&productId='.$productId.'&access_token='.$apiToken
  284.             );
  285.             $content $response->toArray();
  286.             
  287.             $parameters["product"] = $content;
  288.         } catch (Exception $e) {
  289.             dump("f");
  290.         }
  291.           return new JsonResponse($content);
  292.     }
  293.     public function carrito(Request $request): Response
  294.     {
  295.         $apiToken $this->getParameter('api_token');
  296.         $apiBaseUrl $this->getParameter('api_url');
  297.         $cartId $request->getSession()->get('cartId');
  298.         
  299.         
  300.         if ($cartId != null){
  301.             $response $this->client->request(
  302.                 'GET',
  303.                 $apiBaseUrl.'products/shopping-cart/'.$cartId.'?access_token='.$apiToken
  304.             );
  305.             $cart $response->toArray();
  306.             if(count($cart) == 1){
  307.                 $cart $cart[0];
  308.             }
  309.                 
  310.             $parameters["cart"] = $cart;
  311.             $parameters["products"] = $cart['products_sales'];
  312.             $parameters["code"] = $request->get('coupon_code');
  313.             
  314.         }else{
  315.             return $this->redirectToRoute('vinos');
  316.         }
  317.        
  318.         return $this->render('carrito.html.twig'$parameters);
  319.     }
  320.     public function comunas(Request $request): Response
  321.     {
  322.         $apiToken $this->getParameter('api_token');
  323.         $apiBaseUrl $this->getParameter('api_url');
  324.         $regionId $request->get('regionId');
  325.             $response $this->client->request(
  326.                 'GET',
  327.                 $apiBaseUrl.'api-comunas/'.$regionId.'?access_token='.$apiToken
  328.             );
  329.             $cart $response->toArray();       
  330.         return new JsonResponse($cart);
  331.     }
  332.     public function checkout(Request $request): Response
  333.     {
  334.         $apiToken $this->getParameter('api_token');
  335.         $apiBaseUrl $this->getParameter('api_url');
  336.         $shippingResponse $this->client->request(
  337.             'GET',
  338.             $apiBaseUrl.'shipping-methods?access_token='.$apiToken
  339.         );
  340.     
  341.         $shippingMethods $shippingResponse->toArray();
  342.   
  343.         $cartId $request->getSession()->get('cartId');
  344.         if($cartId == null){
  345.             return $this->redirectToRoute('index');
  346.         }
  347.         
  348.         if ($request->isMethod('POST')) {
  349.            
  350.             $customerObject["formaPago"] = 7;
  351.             $customerObject["currency"] = "CLP";
  352.             $customerObject["method_shipping_id"] = intval($request->get('method_shipping_id'));
  353.           
  354.             $customerObject["costumerName"] = $request->get('costumerName');
  355.             $customerObject["costumerPhone"] = $request->get('costumerPhone');
  356.             $customerObject["costumerEmail"] = $request->get('costumerEmail');
  357.             $customerObject["shippingName"] = $request->get('shippingName');
  358.             $customerObject["shippingRut"] = $request->get('shippingRut');
  359.             $customerObject["billingName"] = $request->get('shippingName');
  360.             $customerObject["billingRut"] =  $request->get('shippingRut');
  361.             if($request->get('method_region_id')){
  362.                 $customerObject["shippingRegion"] = $request->get('method_region_id');
  363.             }else{
  364.                 $customerObject["shippingRegion"] = 13;
  365.             }
  366.             if($request->get('shippingCity')){
  367.                 $customerObject["shippingCity"] = $request->get('shippingCity');
  368.                 $customerObject["billingCity"] = $request->get('shippingCity');
  369.             }else{
  370.                 $customerObject["billingCity"] = "Santiago";
  371.                 $customerObject["shippingCity"] = "Santiago";
  372.             }
  373.             if($request->get('shippingCommune')){
  374.                 $customerObject["shippingCommune"] = $request->get('shippingCommune');
  375.                 $customerObject["billingCommune"] = $request->get('shippingCommune');
  376.             }else{
  377.                 $customerObject["billingCommune"] = "Santiago";
  378.                 $customerObject["shippingCommune"] = "Santiago";
  379.             }
  380.             if($request->get('shippingAddress')){
  381.                 $customerObject["billingAddress"] = $request->get('shippingAddress');
  382.                 $customerObject["shippingAddress"] = $request->get('shippingAddress');
  383.             }else{
  384.                 $customerObject["billingAddress"] = "Santiago";
  385.                 $customerObject["shippingAddress"] = "Santiago";
  386.             }            
  387.             $customerObject["costo-envio"] = $request->get('costo-envio');   
  388.             $customer json_encode($customerObject);
  389.           
  390.             $response $this->client->request(
  391.                 'POST',
  392.                 $apiBaseUrl.'products/shopping-cart/checkout/'.$cartId.'?access_token='.$apiToken,
  393.                 [
  394.                     'body' => $customer,
  395.                 ]
  396.                 
  397.             );
  398.             
  399.             $response $response->toArray();
  400.             $response $response[0];     
  401.             $cartId $response['cart_id'];
  402.             $cartCode $response['cart_code'];            
  403.             $price $response['converted_total_price'];  
  404.            
  405.             $pagoBase64 base64_encode($cartId."@;".$cartCode."@;".$price);
  406.             //$request->getSession()->remove('cartId');
  407.             //$webpayURL = 'https://webpay-colchagua.tourpay.cl/sdk/tbk-normal.php?data='.$pagoBase64;
  408.             //$urlBase = $response['test_url'];
  409.             $urlBase $response['payment_url'] ?? null;
  410.             $webpayURL null;
  411.             
  412.             if (isset($response['payment_url_mall'])) {
  413.                 $webpayURL $response['payment_url_mall'];
  414.                 return $this->redirect($webpayURL);
  415.             }
  416.         }
  417.         $response $this->client->request(
  418.             'GET',
  419.             $apiBaseUrl.'products/shopping-cart/'.$cartId.'?access_token='.$apiToken
  420.         );
  421.     
  422.         $cart $response->toArray();
  423.         
  424.         if(count($cart) == 1){
  425.             $cart $cart[0];
  426.         }
  427.         $parameters["cart"] = $cart;
  428.         $parameters["shippingMethods"] = $shippingMethods;
  429.         return $this->render('checkout.html.twig'$parameters);
  430.     }
  431.     public function pagado(Request $request$id): Response
  432.     {
  433.         $apiToken $this->getParameter('api_token');
  434.         $apiBaseUrl $this->getParameter('api_url');
  435.         $response $this->client->request(
  436.             'GET',
  437.             $apiBaseUrl.'products/shopping-cart/'.$id.'?access_token='.$apiToken
  438.         );
  439.     
  440.         $cart $response->toArray();
  441.         
  442.         $parameters["cart"] = $cart[0];
  443.         $request->getSession()->remove('cartId');
  444.         return $this->render('pago/pagado.html.twig'$parameters);
  445.     }
  446.     public function rechazo(Request $request$id): Response
  447.     {
  448.         $apiToken $this->getParameter('api_token');
  449.         $apiBaseUrl $this->getParameter('api_url');
  450.         $cartId $request->getSession()->get('cartId');
  451.         $request->getSession()->remove('cartId');
  452.         return $this->render('pago/rechazo.html.twig');
  453.     }
  454.     public function error(Request $request$id$id1): Response
  455.     {
  456.         $apiToken $this->getParameter('api_token');
  457.         $apiBaseUrl $this->getParameter('api_url');
  458.         $cartId $request->getSession()->get('cartId');
  459.         $request->getSession()->remove('cartId');
  460.         return $this->render('pago/error.html.twig');
  461.     }
  462.     public function changeQuantityShoppingCart(Request $request)
  463.     {
  464.         $apiToken $this->getParameter('api_token');
  465.         $apiUrl $this->getParameter('api_url');
  466.         $locale $request->getLocale();
  467.         $variant_Id $request->query->get('variantId');
  468.         $product_Id $request->query->get('productId');
  469.         $quantity_products $request->query->get('quantity_products');
  470.         $cart_Id $request->query->get('cartId');
  471.         $discounted_price $request->query->get('discountedPrice');
  472.         if($discounted_price == 0){
  473.             $discounted_price null;
  474.         }
  475.         $response $this->client->request(
  476.             'GET',
  477.             $apiUrl.'products/shopping-cart/change/quantity?variant_Id='.$variant_Id.'&product_Id='.$product_Id.'&quantity_products='.$quantity_products.'&cart_Id='.$cart_Id.'&discounted_price='.$discounted_price.'&access_token='.$apiToken
  478.         );
  479.           return new JsonResponse($response->getContent());
  480.     }
  481.    
  482.     public function validarCupon($cartId,$code): Response
  483.     {
  484.         $apiToken $this->getParameter('api_token');
  485.         $apiBaseUrl $this->getParameter('api_url');
  486.         
  487.         $response $this->client->request(
  488.             'GET',
  489.             $apiBaseUrl.'products/applicate-coupon/'.$cartId.'/'.$code.'?access_token='.$apiToken
  490.         );        
  491.         
  492.         $statuCodeP $response->getStatusCode();
  493.        
  494.       
  495.         if($statuCodeP == 200 ){
  496.             $parametros['producto'] = $response->toArray();
  497.         }else{
  498.             $parametros['producto'] = [];
  499.         }
  500.      
  501.         return new JsonResponse($parametros);
  502.     }
  503.     public function productosTag(Request $request$tag): Response
  504.     {
  505.         $apiToken $this->getParameter('api_token');
  506.         $apiBaseUrl $this->getParameter('api_url');
  507.         
  508.         try {
  509.             $response $this->client->request(
  510.                 'GET',
  511.                 $apiBaseUrl.'products?access_token='.$apiToken.'&tag='.$tag.'&orderby=name'
  512.             );
  513.             $content $response->toArray();            
  514.             $parameters["products"] = $content;    
  515.             $i 0;         
  516.             foreach ($parameters["products"] as $data) {
  517.                 if($data["tags"] != null ){
  518.                     $tags explode(","$data['tags']);
  519.                     foreach ($tags as $t){
  520.                         if($t == $tag){
  521.                             $data["linea"] = 1;
  522.                             $parameters["products"][$i] = $data;
  523.                         }                                            
  524.                     }                   
  525.                 }
  526.                 $i++;
  527.             }           
  528.             $parameters["tag"] = $tag;
  529.         } catch (Exception $e) {
  530.             dump("f");
  531.         }
  532.         return $this->render('vinos.html.twig'$parameters);
  533.     }
  534.     public function nosotros(): Response
  535.     {
  536.         return $this->render('nosotros.html.twig');
  537.     }
  538.     public function tyc(): Response
  539.     {
  540.         return $this->render('TyC.html.twig');
  541.     }
  542.     public function infoPractica(): Response
  543.     {
  544.         return $this->render('infoPractica.html.twig');
  545.     }
  546.     
  547.     public function calculateDynamicShipping(Request $request): Response
  548.     {
  549.         $apiToken $this->getParameter('api_token');
  550.         $apiBaseUrl $this->getParameter('api_url');
  551.         $cartId $request->query->get('cartId');
  552.         $shippingMethod $request->query->get('shippingMethod');
  553.         $regionId $request->query->get('regionId')??0;
  554.         try {
  555.            
  556.             $response $this->client->request(
  557.                 'GET',                
  558.                 $apiBaseUrl.'products/shopping-cart/calculate-dynamic-shipping/'.$cartId.'/'.$shippingMethod.'/'.$regionId.'?access_token='.$apiToken
  559.             );
  560.             
  561.             $content_array $response->toArray();
  562.             
  563.             $parameters["shipping_value"] = $content_array;
  564.           } catch (Exception $e) {
  565.             
  566.           }
  567.           return new JsonResponse($content_array);
  568.     }
  569.     
  570. }