From 563246bd540060c2d51e504c8a49a3cdcdec7445 Mon Sep 17 00:00:00 2001 From: Hritikkk9 Date: Fri, 12 Jul 2024 19:17:19 +0530 Subject: [PATCH] google place api controller updated --- .../Customer_API/SubscriptionController.php | 2 +- app/Http/Helpers/Webhelper.php | 84 +++++++++++++++++-- .../CustomerAPIs/RestaurantApiServices.php | 7 ++ config/constants.php | 4 + 4 files changed, 87 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php b/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php index be81089..f5bde1d 100644 --- a/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php +++ b/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php @@ -85,7 +85,7 @@ class SubscriptionController extends Controller } catch (\Exception $e) { Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]); - return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + return jsonResponseWithErrorMessage(__('auth.something_went_wrong')); } } diff --git a/app/Http/Helpers/Webhelper.php b/app/Http/Helpers/Webhelper.php index 30aa672..7d87aee 100644 --- a/app/Http/Helpers/Webhelper.php +++ b/app/Http/Helpers/Webhelper.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Session; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Tymon\JWTAuth\Facades\JWTAuth; +use GuzzleHttp\Client; /** * Created By : sayli raut @@ -168,17 +169,82 @@ if (!function_exists('readRestHeaderToken')) { if (!function_exists('generateOTP')) { - function generateOTP() - { - // Define the length of the OTP - $otpLength = 4; + function generateOTP() + { + // Define the length of the OTP + $otpLength = 4; - // Generate a random OTP with $otpLength digits - $otp = ''; - for ($i = 0; $i < $otpLength; $i++) { - $otp .= rand(0, 9); + // Generate a random OTP with $otpLength digits + $otp = ''; + for ($i = 0; $i < $otpLength; $i++) { + $otp .= rand(0, 9); + } + return $otp; } - return $otp; } } + +/** + * Created by : Hritik RD + * Created at : 12 July 2024 + * Use : To Get Opening hours of Restaurant By NAME based on Google Maps using Google Places APIs + */ + +if (!function_exists('getOpeningHoursOfRestaurant')) { + function getOpeningHoursOfRestaurant($restaurantName) + { + + $googlePlaceApiKey = config('constants.googlePlaces.api_key'); // Your webhook secret key + // dd($googlePlaceApiKey); + + $client = new Client(); + $url = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json'; + + $response = $client->get($url, [ + 'query' => [ + 'fields' => 'place_id', + 'input' => $restaurantName, + 'inputtype' => 'textquery', + 'key' => $googlePlaceApiKey + ] + ]); + $placeData = json_decode($response->getBody(), true); + + if (isset($placeData['candidates'][0]['place_id'])) { + $placeId = $placeData['candidates'][0]['place_id']; + // return $placeId; + } else { + $placeId = "N/A"; + // return response()->json($placeId); + return $placeId; + } + + + // $placeId = 'ChIJT3dpYcy35zsRLxY5KTTMqhU'; // You can also pass this as a parameter if needed + $client = new Client(); + $url = 'https://maps.googleapis.com/maps/api/place/details/json'; + + $response = $client->get($url, [ + 'query' => [ + 'fields' => 'name,rating,formatted_phone_number,opening_hours', + 'place_id' => $placeId, + 'key' => $googlePlaceApiKey + ] + ]); + + $data = json_decode($response->getBody(), true); + if (isset($data['result']['opening_hours']['weekday_text'])) { + $hours = $data['result']['opening_hours']['weekday_text']; + // return response()->json(['place_id' => $placeId]); + } else { + $hours = "N/A"; + } + return $hours ; + // dd($data); + // return response()->json($data); + + } } + + + diff --git a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php index 79a7bca..0680e32 100644 --- a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php +++ b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php @@ -43,6 +43,7 @@ class RestaurantApiServices ->where('restaurant_xid', $restaurant['id']) ->exists(); $restaurant['is_favourite'] = $isFavourite; + // $restaurant['operating_hours'] = getOpeningHoursOfRestaurant($restaurant['name']);// will update later } return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $restaurants, 200); @@ -120,6 +121,8 @@ class RestaurantApiServices ->where('restaurant_xid', $rest->id) ->exists(); $rest->is_favourite = $isFavourite; + + $redeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId) ->where('manage_restaurants_xid', $rest->id) @@ -131,6 +134,10 @@ class RestaurantApiServices // $timeIntervalHours = $Timeinterval->time_hours; $restTimeHours = $restTime->time_hours; + //this below code is updated by hritik on 12-07-2024 by adding restaurant opening hours dynamically from Google + + // $rest->operating_hours = getOpeningHoursOfRestaurant($rest->name); //will update later + // $greaterTime = max($timeIntervalHours, $restTimeHours); diff --git a/config/constants.php b/config/constants.php index ff75c52..105380c 100644 --- a/config/constants.php +++ b/config/constants.php @@ -21,5 +21,9 @@ return [ 'stripe_secret_key' => env('STRIPE_SECRET'), 'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'), ], + + 'googlePlaces'=>[ + 'api_key'=>env('GOOGLE_PLACE_API_KEY') + ] ];