src/Controller/Api/CheckoutController.php line 144

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Env;
  4. use App\Func;
  5. use stdClass;
  6. use App\Entity\City;
  7. use App\Entity\Page;
  8. use App\Entity\User;
  9. use App\Entity\Region;
  10. use App\Entity\Esystem;
  11. use App\Entity\Delivery;
  12. use App\Service\Auth\Auth;
  13. use App\Service\Cart\Cart;
  14. use App\Repository\CityRepository;
  15. use App\Repository\UserRepository;
  16. use App\Service\Checkout\Checkout;
  17. use App\Repository\RegionRepository;
  18. use App\Repository\EsystemRepository;
  19. use App\Repository\DeliveryRepository;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Security\Core\Security;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. class CheckoutController extends AbstractController
  27. {
  28.     protected EntityManagerInterface $em;
  29.     protected $default_region_id '0c5b2444-70a0-4932-980c-b4dc0d3f02b5';
  30.     protected $default_city_id '0c5b2444-70a0-4932-980c-b4dc0d3f02b5';
  31.     protected Checkout $Checkout;
  32.     protected Cart $Cart;
  33.     protected User $User;
  34.     protected Auth $Auth;
  35.     //Repositories    
  36.     protected RegionRepository $Regions;
  37.     protected CityRepository $Cities;
  38.     protected DeliveryRepository $Deliveries;
  39.     protected EsystemRepository $Esystems;
  40.     protected UserRepository $Users;
  41.     public function __construct(EntityManagerInterface $emCheckout $CheckoutCart $CartAuth $AuthSecurity $security)
  42.     {
  43.         if (Env::site() == Env::DOM || Env::site() == Env::OPT) {
  44.             $this->default_region_id 'dcaadb64-4b33-11e4-ab6d-005056801329';
  45.             $this->default_city_id 'e718a680-4b33-11e4-ab6d-005056801329';
  46.         }
  47.         $this->em $em;
  48.         $this->Checkout $Checkout;
  49.         $this->Cart $Cart;
  50.         $this->Auth $Auth;
  51.         $this->Auth->setUser($security->getUser());
  52.         $this->Regions $this->em->getRepository(Region::class);
  53.         $this->Cities $this->em->getRepository(City::class);
  54.         $this->Deliveries $this->em->getRepository(Delivery::class);
  55.         $this->Esystems $this->em->getRepository(Esystem::class);
  56.         $this->Users $this->em->getRepository(User::class);
  57.     }
  58.     // Разделение строки на фамилию, имя, определение пола
  59.     #[Route(path'/api/checkout/cleanname'name'api_checkout_cleanname_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  60.     #[Route(path'/{_locale}/api/checkout/cleanname'name'api_checkout_cleanname'requirements: ['_locale' => '%app.langs%'])]
  61.     public function cleanname(Request $request): Response
  62.     {
  63.         $fullname = (string) $request->query->get('fullname');
  64.         $curl curl_init();
  65.         $headers[] = 'Content-Type: application/json';
  66.         $headers[] = 'Authorization: Token ' Env::code_dadata();
  67.         $headers[] = 'X-Secret: ' Env::code_dadata_2();
  68.         $request = [$fullname];
  69.         curl_setopt($curlCURLOPT_URL"https://cleaner.dadata.ru/api/v1/clean/name");
  70.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  71.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  72.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($requestJSON_UNESCAPED_UNICODE));
  73.         $response curl_exec($curl);
  74.         $data json_decode($responsetrue)[0];
  75.         $response = [];
  76.         if (($data['name']) && ($data['surname'])) {
  77.             $usersurname $data['surname'];
  78.             $username $data['name'];
  79.             switch ($data['gender']) {
  80.                 case 'Ж':
  81.                     $usergender 'F';
  82.                     break;
  83.                 case 'М':
  84.                     $usergender 'M';
  85.                     break;
  86.                 default:
  87.                     $usergender null;
  88.             }
  89.         }
  90.         $response['username'] = $username;
  91.         $response['usersurname'] = $usersurname;
  92.         $response['usergender'] = $usergender;
  93.         return $this->json($response);
  94.     }
  95.     // Подсказки при вводе ФИО
  96.     #[Route(path'/api/checkout/fiohint'name'api_checkout_fiohint_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  97.     #[Route(path'/{_locale}/api/checkout/fiohint'name'api_checkout_fiohint'requirements: ['_locale' => '%app.langs%'])]
  98.     public function fiohint(Request $request): Response
  99.     {
  100.         $fullname = (string) $request->query->get('fullname');
  101.         $curl curl_init();
  102.         $headers[] = 'Content-Type: application/json';
  103.         $headers[] = 'Authorization: Token ' Env::code_dadata();
  104.         $request = ['query' => $fullname'parts' => ['surname''name'], 'count' => 10];
  105.         curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/fio");
  106.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  107.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  108.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  109.         $response curl_exec($curl);
  110.         $suggestions json_decode($response);
  111.         //var_dump($suggestions);
  112.         $apiResponce = [];
  113.         foreach ($suggestions->suggestions as $suggestion) {
  114.             $obj = new stdClass();
  115.             $obj->value $suggestion->value;
  116.             $obj->name $suggestion->data->name;
  117.             $obj->surname $suggestion->data->surname;
  118.             $obj->gender $suggestion->data->gender;
  119.             $apiResponce[] = $obj;
  120.         }
  121.         return $this->json($apiResponce);
  122.     }
  123.     #[Route(path'/api/checkout/citydetect'name'api_checkout_citydetect_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  124.     #[Route(path'/{_locale}/api/checkout/citydetect'name'api_checkout_citydetect'requirements: ['_locale' => '%app.langs%'])]
  125.     public function citydetect(Request $request): Response
  126.     {
  127.         $city_fias_id $this->Checkout->getCity_fias_id();
  128.         if ($city_fias_id) {
  129.         } else {
  130.             if (Env::site() == env::DOM || Env::site() == env::OPT) {
  131.                 $ip $_SERVER["REMOTE_ADDR"];
  132.                 $request = @file_get_contents("https://api.sypexgeo.net/json/" $ip);
  133.                 $array json_decode($requesttrue);
  134.                 $city_name = isset($array['city']['name_ru']) ? $array['city']['name_ru'] : "";
  135.                 $city $this->Cities->findOneBy(["name" => $city_name]);
  136.                 if (!$city) {
  137.                     $region $this->Regions->findOneBy(['fias_id' => $this->default_region_id]);
  138.                     $city $this->Cities->findOneBy(['fias_id' => $this->default_city_id]);
  139.                     $this->Checkout->setRegion_fias_id($region->getFiasId());
  140.                     $this->Checkout->setRegion_name($region->getName());
  141.                     $this->Checkout->setCity_fias_id($city->getFiasId());
  142.                     $this->Checkout->setCity_name($city->getName());
  143.                     $this->Checkout->setCity_post_code($city->getPostalCode());
  144.                 } else {
  145.                     $region $this->Regions->find($city->getRegion());
  146.                     $this->Checkout->setRegion_fias_id($region->getFiasId());
  147.                     $this->Checkout->setRegion_name($region->getName());
  148.                     $this->Checkout->setCity_fias_id($city->getFiasId());
  149.                     $this->Checkout->setCity_name($city->getName());
  150.                     $this->Checkout->setCity_post_code($city->getPostalCode());
  151.                 }
  152.             } elseif (Env::site() == env::MIR || Env::site() == env::OPT_MIR) {
  153.                 $curl curl_init();
  154.                 $headers[] = 'Accept: application/json';
  155.                 $headers[] = 'Authorization: Token ' Env::code_dadata();
  156.                 curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/iplocate/address?ip=" $_SERVER['REMOTE_ADDR']);
  157.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  158.                 curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  159.                 $response json_decode(curl_exec($curl));
  160.                 if ($response->location == null) {
  161.                     $region $this->Regions->findOneBy(['fias_id' => $this->default_region_id]);
  162.                     $city $this->Cities->findOneBy(['fias_id' => $this->default_city_id]);
  163.                     $this->Checkout->setRegion_fias_id($region->getFiasId());
  164.                     $this->Checkout->setRegion_name($region->getName());
  165.                     $this->Checkout->setCity_fias_id($city->getFiasId());
  166.                     $this->Checkout->setCity_name($city->getName());
  167.                     $this->Checkout->setCity_post_code($city->getPostalCode());
  168.                 } else {
  169.                     $this->Checkout->setRegion_fias_id($response->location->data->region_fias_id);
  170.                     $this->Checkout->setRegion_name($response->location->data->region_with_type);
  171.                     $this->Checkout->setCity_fias_id(($response->location->data->settlement_fias_id $response->location->data->settlement_fias_id $response->location->data->city_fias_id));
  172.                     $this->Checkout->setCity_name(($response->location->data->settlement_with_type $response->location->data->settlement_with_type $response->location->data->city_with_type));
  173.                     $this->Checkout->setCity_post_code($response->location->data->postal_code);
  174.                 }
  175.             } else {
  176.                 $this->Checkout->setRegion_fias_id('default');
  177.             }
  178.         }
  179.         return $this->json($this->Checkout->getCheckout());
  180.     }
  181.     // Подсказки при вводе города
  182.     #[Route(path'/api/checkout/citylist'name'api_checkout_citylist_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  183.     #[Route(path'/{_locale}/api/checkout/citylist'name'api_checkout_citylist'requirements: ['_locale' => '%app.langs%'])]
  184.     public function citylist(Request $request): Response
  185.     {
  186.         $q = (string) $request->query->get('string');
  187.         if (Env::site() == env::DOM || Env::site() == env::OPT) {
  188.             /** @var \App\Entity\City[] $cities */
  189.             $cities $this->em->createQuery("SELECT c FROM App\Entity\City c WHERE c.name LIKE '" $q "%' ORDER BY c.type ASC")->setMaxResults(10)->getResult(); // Check
  190.             if (count($cities)) {
  191.                 $response = new \stdClass();
  192.                 $response->suggestions = [];
  193.                 foreach ($cities as $city) {
  194.                     $region $this->Regions->find($city->getRegion());
  195.                     if ($region) {
  196.                         $suggestion = new \stdClass();
  197.                         $suggestion->value $city->getType() . ' ' $city->getName() . ", " $region->getName();
  198.                         $suggestion->data = new \stdClass();
  199.                         $suggestion->data->city_fias_id $city->getFiasId();
  200.                         array_push($response->suggestions$suggestion);
  201.                     }
  202.                 }
  203.             }
  204.         } else {
  205.             $curl curl_init();
  206.             $headers[] = 'Content-Type: application/json';
  207.             $headers[] = 'Accept: application/json';
  208.             $headers[] = 'Authorization: Token ' Env::code_dadata();
  209.             $request = ['query' => $q'from_bound' => ['value' => 'city'], 'to_bound' => ['value' => 'settlement'], 'count' => 10];
  210.             curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address");
  211.             curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  212.             curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  213.             curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  214.             $response curl_exec($curl);
  215.         }
  216.         return $this->json($response);
  217.     }
  218.     // Выбор города
  219.     #[Route(path'/api/checkout/selectcity'name'api_checkout_selectcity_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  220.     #[Route(path'/{_locale}/api/checkout/selectcity'name'api_checkout_selectcity'requirements: ['_locale' => '%app.langs%'])]
  221.     public function selectcity(Request $requestCheckout $Checkout): Response
  222.     {
  223.         $fias_id = (string) $request->query->get('fias_id');
  224.         if ($fias_id) {
  225.             if (Env::site() == env::DOM || Env::site() == env::OPT) {
  226.                 $city $this->Cities->findOneBy(['fias_id' => $fias_id]); // Check
  227.                 if ($city->getId()) {
  228.                     $region $this->Regions->find($city->getRegion());
  229.                     if ($region) {
  230.                         $Checkout->setRegion_fias_id($region->getFiasId());
  231.                         $Checkout->setRegion_name($region->getName());
  232.                         $Checkout->setCity_fias_id($city->getFiasId());
  233.                         $Checkout->setCity_name($city->getName());
  234.                         $Checkout->setCity_post_code($city->getPostalCode());
  235.                         $Checkout->setUseraddr('');
  236.                         $Checkout->setUserlat('');
  237.                         $Checkout->setUserlon('');
  238.                         $Checkout->setDelivery_post_code('');
  239.                         $Checkout->setDelivery_post_name('');
  240.                         $Checkout->setDelivery_post_worktime('');
  241.                         $Checkout->setDelivery_post_phone('');
  242.                         $Checkout->setDelivery_post_email('');
  243.                         $Checkout->setDelivery_sub_id(0);
  244.                         $Checkout->setDelivery_sub_name('');
  245.                         $Checkout->setDelivery_cost(0);
  246.                     }
  247.                 }
  248.             } else {
  249.                 $curl curl_init();
  250.                 $headers[] = 'Content-Type: application/json';
  251.                 $headers[] = 'Accept: application/json';
  252.                 $headers[] = 'Authorization: Token ' Env::code_dadata();
  253.                 $request = ['query' => $fias_id];
  254.                 curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/address");
  255.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  256.                 curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  257.                 curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  258.                 $response json_decode(curl_exec($curl));
  259.                 if (!count($response->suggestions)) {
  260.                     $region $this->Regions->findOneBy(['fias_id' => $this->default_region_id]);
  261.                     $city $this->Cities->findOneBy(['fias_id' => $this->default_city_id]);
  262.                     $Checkout->setRegion_fias_id($region->getFiasId());
  263.                     $Checkout->setRegion_name($region->getName());
  264.                     $Checkout->setCity_fias_id($city->getFiasId());
  265.                     $Checkout->setCity_name($city->getName());
  266.                     $Checkout->setCity_post_code($city->getPostalCode());
  267.                     $Checkout->setUseraddr('');
  268.                     $Checkout->setUserlat('');
  269.                     $Checkout->setUserlon('');
  270.                     $Checkout->setDelivery_post_code('');
  271.                     $Checkout->setDelivery_post_name('');
  272.                     $Checkout->setDelivery_post_worktime('');
  273.                     $Checkout->setDelivery_post_phone('');
  274.                     $Checkout->setDelivery_post_email('');
  275.                     $Checkout->setDelivery_sub_id(0);
  276.                     $Checkout->setDelivery_sub_name('');
  277.                     $Checkout->setDelivery_cost(0);
  278.                 } else {
  279.                     $Checkout->setRegion_fias_id($response->suggestions[0]->data->region_fias_id);
  280.                     $Checkout->setRegion_name($response->suggestions[0]->data->region_with_type);
  281.                     $Checkout->setCity_fias_id(($response->suggestions[0]->data->settlement_fias_id $response->suggestions[0]->data->settlement_fias_id $response->suggestions[0]->data->city_fias_id));
  282.                     $Checkout->setCity_name(($response->suggestions[0]->data->settlement_with_type $response->suggestions[0]->data->settlement_with_type $response->suggestions[0]->data->city_with_type));
  283.                     $Checkout->setCity_post_code($response->suggestions[0]->data->postal_code);
  284.                     $city $this->Cities->findOneBy(['fias_id' => $Checkout->getCity_fias_id()]);
  285.                     if ($city->getPostalCode()) {
  286.                         $Checkout->setCity_post_code($city->getPostalCode());
  287.                     }
  288.                     $Checkout->setUseraddr('');
  289.                     $Checkout->setUserlat('');
  290.                     $Checkout->setUserlon('');
  291.                     $Checkout->setDelivery_post_code('');
  292.                     $Checkout->setDelivery_post_name('');
  293.                     $Checkout->setDelivery_post_worktime('');
  294.                     $Checkout->setDelivery_post_phone('');
  295.                     $Checkout->setDelivery_post_email('');
  296.                     $Checkout->setDelivery_sub_id(0);
  297.                     $Checkout->setDelivery_sub_name('');
  298.                     $Checkout->setDelivery_cost(0);
  299.                 }
  300.             }
  301.         }
  302.         return $this->json($Checkout->getCheckout());
  303.     }
  304.     // Подсказки при вводе адреса
  305.     #[Route(path'/api/checkout/addresslist'name'api_checkout_addresslist_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  306.     #[Route(path'/{_locale}/api/checkout/addresslist'name'api_checkout_addresslist'requirements: ['_locale' => '%app.langs%'])]
  307.     public function addressList(Request $requestCheckout $Checkout): Response
  308.     {
  309.         $q = (string) $request->query->get('string');
  310.         $curl curl_init();
  311.         $headers[] = 'Content-Type: application/json';
  312.         $headers[] = 'Accept: application/json';
  313.         $headers[] = 'Authorization: Token ' Env::code_dadata();
  314.         $request = ['query' => $q'locations' => ['city_fias_id' => $Checkout->getCity_fias_id(), ['settlement_fias_id' => $Checkout->getCity_fias_id()]], 'from_bound' => ['value' => 'street'], 'to_bound' => ['value' => 'flat'], 'count' => 10];
  315.         curl_setopt($curlCURLOPT_URL"https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address");
  316.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  317.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  318.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($request));
  319.         $response json_decode(curl_exec($curl), true);
  320.         return $this->json($response);
  321.     }
  322.     // Get Session Vars
  323.     #[Route(path'/api/checkout/get-session-vars'name'api_checkout_get_session_vars_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  324.     #[Route(path'/{_locale}/api/checkout/get-session-vars'name'api_checkout_get_session_vars'requirements: ['_locale' => '%app.langs%'])]
  325.     public function getSessionVars(): Response
  326.     {
  327.         $result = [
  328.             'amount_delivery' => Func::fmtmoney($this->Cart->getAmountDelivery()),
  329.             'amount_with_delivery' => Func::fmtmoney($this->Cart->getAmountWithDelivery()),
  330.             'userlat' => $this->Checkout->getUserlat(),
  331.             'userlon' => $this->Checkout->getUserlon(),
  332.             'delivery_id' => (int) $this->Checkout->getDelivery_id(),
  333.             'delivery_intname' => $this->Checkout->getDelivery_intname(),
  334.             'delivery_name' => $this->Checkout->getDelivery_name(),
  335.             'delivery_post_code' => $this->Checkout->getDelivery_post_code(),
  336.         ];
  337.         return $this->json($result);
  338.     }
  339.     // Выбор сервиса доставки
  340.     #[Route(path'/api/checkout/select-type/{delivery_id}'name'api_checkout_select_type_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  341.     #[Route(path'/{_locale}/api/checkout/select-type/{delivery_id}'name'api_checkout_select_type'requirements: ['_locale' => '%app.langs%'])]
  342.     public function selectType(int $delivery_id 0): Response
  343.     {
  344.         $delivery $this->Deliveries->find($delivery_id);
  345.         if ($delivery) {
  346.             $this->Checkout->setDelivery_id($delivery->getId());
  347.             $this->Checkout->setDelivery_intname($delivery->getIntname());
  348.             $this->Checkout->setDelivery_name($delivery->getName());
  349.             $result = [
  350.                 'error' => null,
  351.                 'delivery_id' => $delivery->getId(),
  352.                 'delivery_intname' => $delivery->getIntname(),
  353.                 'delivery_name' => $delivery->getName(),
  354.             ];
  355.         } else {
  356.             $result = [
  357.                 "error" => "wrong delivery id",
  358.             ];
  359.         }
  360.         return $this->json($result);
  361.     }
  362.     // Выбор варианта доставки
  363.     #[Route(path'/api/checkout/select-price/{sub_id}/{sub_code}/{sub_name}/{cost}'name'api_checkout_select_price_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  364.     #[Route(path'/{_locale}/api/checkout/select-price/{sub_id}/{sub_code}/{sub_name}/{cost}'name'api_checkout_select_price'requirements: ['_locale' => '%app.langs%'])]
  365.     public function selectPrice(int $sub_id 0string $sub_code ''string $sub_name ''string $cost ''): Response
  366.     {
  367.         $this->Checkout->setDelivery_sub_id($sub_id);
  368.         $this->Checkout->setDelivery_sub_code($sub_code);
  369.         $this->Checkout->setDelivery_sub_name($sub_name);
  370.         $this->Checkout->setDelivery_cost($cost);
  371.         $result = [
  372.             'error' => null,
  373.             'delivery_sub_id' => $sub_id,
  374.             'delivery_sub_code' => $sub_code,
  375.             'delivery_sub_name' => $sub_name,
  376.             'delivery_cost' => $cost,
  377.         ];
  378.         return $this->json($result);
  379.     }
  380.     // Изменение цены доставки
  381.     #[Route(path'/api/checkout/set-price/{cost}'name'api_checkout_set_price_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  382.     #[Route(path'/{_locale}/api/checkout/set-price/{cost}'name'api_checkout_set_price'requirements: ['_locale' => '%app.langs%'])]
  383.     public function setPrice($cost ''): Response
  384.     {
  385.         $this->Checkout->setDelivery_cost($cost);
  386.         $result = [
  387.             'delivery_cost' => $cost,
  388.         ];
  389.         return $this->json($result);
  390.     }
  391.     // Выбор отделения доставки
  392.     #[Route(path'/api/checkout/select-code/{code}/{name}/{worktime}/{phone}/{email}'name'api_checkout_select_code_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  393.     #[Route(path'/{_locale}/api/checkout/select-code/{code}/{name}/{worktime}/{phone}/{email}'name'api_checkout_select_code'requirements: ['_locale' => '%app.langs%'])]
  394.     public function selectCode(string $code ''string $name ''string $worktime ''string $phone ''string $email ''): Response
  395.     {
  396.         $this->Checkout->setUserlat('');
  397.         $this->Checkout->setDelivery_post_code($code);
  398.         $this->Checkout->setDelivery_post_name($name);
  399.         $this->Checkout->setDelivery_post_worktime($worktime);
  400.         $this->Checkout->setDelivery_post_phone($phone);
  401.         $this->Checkout->setDelivery_post_email($email);
  402.         $result = [
  403.             'error' => null,
  404.             'delivery_post_code' => $code,
  405.             'delivery_post_name' => $name,
  406.             'delivery_post_worktime' => $worktime,
  407.             'delivery_post_phone' => $phone,
  408.             'delivery_post_email' => $email,
  409.         ];
  410.         return $this->json($result);
  411.     }
  412.     // Выбор отделения доставки
  413.     #[Route(path'/api/checkout/select-address/{addr}/{lat}/{lon}'name'api_checkout_select-address_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  414.     #[Route(path'/{_locale}/api/checkout/select-address/{addr}/{lat}/{lon}'name'api_checkout_select-address'requirements: ['_locale' => '%app.langs%'])]
  415.     public function selectAddress(string $addr ''string $lat ''string $lon ''): Response
  416.     {
  417.         $this->Checkout->setUseraddr($addr);
  418.         $this->Checkout->setUserlat($lat);
  419.         $this->Checkout->setUserlon($lon);
  420.         $result = [
  421.             'useraddr' => $addr,
  422.             'userlat' => $lat,
  423.             'userlon' => $lon,
  424.         ];
  425.         return $this->json($result);
  426.     }
  427.     // Выбор способа оплаты
  428.     #[Route(path'/api/checkout/payment/select-type/{id}'name'checkout_payment_select_type_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  429.     #[Route(path'/{_locale}/api/checkout/payment/select-type/{id}'name'checkout_payment_select_type'requirements: ['_locale' => '%app.langs%'])]
  430.     public function payment(int $id): Response
  431.     {
  432.         $esystem $this->Esystems->find($id);
  433.         if ($esystem) {
  434.             $this->Checkout->setPayment_id($id);
  435.             $this->Checkout->setPayment_name($esystem->getName());
  436.             $result = [
  437.                 'payment_id' => $id,
  438.                 'payment_name' => $esystem->getName(),
  439.             ];
  440.         } else {
  441.             $result = [
  442.                 'error' => "wrong id",
  443.             ];
  444.         }
  445.         return $this->json($result);
  446.     }
  447.     // Данные компании
  448.     #[Route(path'/api/checkout/payment/company-data'name'checkout_payment_company_data_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  449.     #[Route(path'/{_locale}/api/checkout/payment/company-data'name'checkout_payment_company_data'requirements: ['_locale' => '%app.langs%'])]
  450.     public function companyData(Request $request): Response
  451.     {      
  452.         $company_nip $request->get('company_nip');
  453.         $company_name $request->get('company_name');
  454.         $company_index $request->get('company_index');
  455.         $company_city $request->get('company_city');
  456.         $company_street $request->get('company_street');
  457.         $company_house $request->get('company_house');
  458.         $company_flat $request->get('company_flat');
  459.         $this->Checkout->setCompanyNip($company_nip);
  460.         $this->Checkout->setCompanyName($company_name);
  461.         $this->Checkout->setCompanyIndex($company_index);
  462.         $this->Checkout->setCompanyCity($company_city);
  463.         $this->Checkout->setCompanyStreet($company_street);
  464.         $this->Checkout->setCompanyHouse($company_house);
  465.         $this->Checkout->setCompanyFlat($company_flat);
  466.         $user $this->Users->find($this->Auth->getUserId());
  467.         $user->setCompanyNip($company_nip);
  468.         $user->setCompanyName($company_name);
  469.         $user->setCompanyIndex($company_index);
  470.         $user->setCompanyCity($company_city);
  471.         $user->setCompanyStreet($company_street);
  472.         $user->setCompanyHouse($company_house);
  473.         $user->setCompanyFlat($company_flat);
  474.         $this->em->persist($user);
  475.         $this->em->flush();
  476.         return $this->json(true);
  477.     }
  478.     // Очистка данных компании
  479.     #[Route(path'/api/checkout/payment/clear-company-data'name'checkout_payment_clear_company_data_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  480.     #[Route(path'/{_locale}/api/checkout/payment/clear-company-data'name'checkout_payment_clear_company_data'requirements: ['_locale' => '%app.langs%'])]
  481.     public function clearCompanyData(): Response
  482.     {
  483.         $this->Checkout->setCompanyNip('');
  484.         $this->Checkout->setCompanyName('');
  485.         $this->Checkout->setCompanyIndex('');
  486.         $this->Checkout->setCompanyCity('');
  487.         $this->Checkout->setCompanyStreet('');
  488.         $this->Checkout->setCompanyHouse('');
  489.         $this->Checkout->setCompanyFlat('');
  490.         return $this->json(true);
  491.     }
  492. }