Files
backend_vib360_laravel/app/Http/Helpers/Webhelper.php

373 lines
11 KiB
PHP
Raw Normal View History

2025-03-11 17:53:58 +05:30
<?php
use App\Models;
use App\Models\User;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Tymon\JWTAuth\Facades\JWTAuth;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
2025-03-26 16:49:43 +05:30
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
2025-03-11 17:53:58 +05:30
if (!function_exists('jsonResponseWithSuccessMessageApi')) {
function jsonResponseWithSuccessMessageApi($message, $data = [], $statusCode = 200)
{
// Set the HTTP status code
http_response_code($statusCode);
// Prepare the response array
$response = [
'status' => 'success',
'status_code' => $statusCode,
'message' => $message,
'data' => $data,
];
return response()->json($response, $statusCode);
// Stop further execution (optional)
exit();
}
}
if (!function_exists('jsonResponseWithErrorMessageApi')) {
function jsonResponseWithErrorMessageApi($errorMessage, $statusCode = 500)
{
// Set the HTTP status code
http_response_code($statusCode);
// Prepare the response array
$response = [
'status' => 'error',
'status_code' => $statusCode,
'message' => $errorMessage,
];
return response()->json($response, $statusCode);
// Stop further execution (optional)
exit();
}
}
if (!function_exists('jsonResponseWithDataErrorMessageApi')) {
function jsonResponseWithDataErrorMessageApi($errorMessage, $data = [], $statusCode = 406)
{
// Set the HTTP status code
http_response_code($statusCode);
// Prepare the response array
$response = [
'status' => 'error',
'status_code' => $statusCode,
'message' => $errorMessage,
'data' => $data,
];
return response()->json($response, $statusCode);
// Stop further execution (optional)
exit();
}
}
if (!function_exists('jsonResponseWithErrorMessage')) {
function jsonResponseWithErrorMessage($errorMessage, $statusCode)
2025-03-11 17:53:58 +05:30
{
$response = [
'status' => 'error',
'message' => $errorMessage,
'status_code' => $statusCode,
2025-03-11 17:53:58 +05:30
];
return response()->json($response);
// Stop further execution (optional)
exit();
}
}
if (!function_exists('jsonResponseWithSuccessMessage')) {
function jsonResponseWithSuccessMessage($message, $data = [])
{
$statusCode = 200;
// Prepare the response array
$response = [
'status' => 'success',
'status_code' => $statusCode,
'message' => $message,
'data' => $data,
];
return response()->json($response, $statusCode);
// Stop further execution (optional)
exit();
}
}
if (!function_exists('fullSearchQuery')) {
function fullSearchQuery($query, $word, $columns)
{
$orwords = explode('|', $columns);
$query = $query->where(function ($query) use ($word, $orwords) {
foreach ($orwords as $key) {
$query->orWhere($key, 'like', '%' . $word . '%');
}
});
return $query;
}
}
if (!function_exists('readHeaderToken')) {
function readHeaderToken()
{
$tokenData = Session::get('vendorToken');
2025-03-26 16:49:43 +05:30
$token = JWTAuth::setToken($tokenData)->getPayload();
2025-03-12 19:19:13 +05:30
// dd([
// 'tokenData' => $tokenData,
// 'sub' => $token['sub'], // This should be your UUID
// 'sub_type' => gettype($token['sub']),
// 'sub_length' => strlen($token['sub']),
// 'iat' => date('Y-m-d H:i:s', $token['iat']),
// ]);
2025-03-11 17:53:58 +05:30
//convert iat to readable format
$iat = date('Y-m-d H:i:s', $token['iat']);
// check token issued time for single device login
// ['last_login_datetime', $iat]
2025-03-12 19:19:13 +05:30
$check_iat = User::find((string) $token['sub']);
2025-03-11 17:53:58 +05:30
// dd($check_iat);
if ($check_iat) {
return $token;
} else {
return false;
}
}
}
if (!function_exists('readRestHeaderToken')) {
function readRestHeaderToken()
{
$tokenData = Session::get('RestToken');
$token = JWTAuth::setToken($tokenData)->getPayload();
//convert iat to readable format
$iat = date('Y-m-d H:i:s', $token['iat']);
// check token issued time for single device login
$check_iat = User::where([['id', $token['sub']], ['is_active', '1']])->first();
if ($check_iat) {
return $token;
} else {
return false;
}
}
if (!function_exists('generateOTP')) {
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);
}
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);
}
if (!function_exists('getOperatingHours')) {
function getOperatingHours($placeName, $address)
{
$client = new Client();
$googlePlaceApiKey = config('constants.googlePlaces.api_key');
$cacheKey = 'restaurant_hours_' . $placeName;
$cacheKeyIsRestaurantOpen = 'is_restaurant_open_now_' . $placeName;
if (Cache::has($cacheKey)) {
return [
'operating_hours' => Cache::get($cacheKey),
'is_open_now' => Cache::get($cacheKeyIsRestaurantOpen),
];
}
try {
// Get place_id
$placeResponse = $client->get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', [
'query' => [
'fields' => 'place_id',
'input' => $placeName . ' ' . $address,
'inputtype' => 'textquery',
'key' => $googlePlaceApiKey
]
]);
$placeData = json_decode($placeResponse->getBody(), true);
if (isset($placeData['candidates'][0]['place_id'])) {
$placeId = $placeData['candidates'][0]['place_id'];
// Get operating hours
$detailResponse = $client->get('https://maps.googleapis.com/maps/api/place/details/json', [
'query' => [
'fields' => 'opening_hours',
'place_id' => $placeId,
'key' => $googlePlaceApiKey
]
]);
$detailData = json_decode($detailResponse->getBody(), true);
if (isset($detailData['result']['opening_hours']['weekday_text'])) {
$hours = $detailData['result']['opening_hours']['weekday_text'];
$isOpenNow = $detailData['result']['opening_hours']['open_now'];
Cache::put($cacheKey, $hours, now()->addHours(2));
Cache::put($cacheKeyIsRestaurantOpen, $isOpenNow, now()->addHours(2));
return [
'operating_hours' => $hours,
'is_open_now' => $isOpenNow,
];
}
}
} catch (\Exception $e) {
Log::error('Restaurant Get service failed : ' . $e->getMessage());
}
return [
'operating_hours' => "N/A",
'is_open_now' => false,
];
}
}
}
2025-03-18 15:45:21 +05:30
if (!function_exists('getThingsboardToken')) {
function getThingsboardToken()
{
if (Cache::has('thingsboard_token')) {
return Cache::get('thingsboard_token');
}
// Get a new token if not available
$response = Http::post('http://thingsboard-url/api/auth/login', [
'username' => 'your_username',
'password' => 'your_password',
]);
if ($response->successful()) {
$token = $response->json('token');
Cache::put('thingsboard_token', $token, now()->addMinutes(15));
return $token;
} else {
throw new Exception('Unable to authenticate with ThingsBoard');
}
}
2025-03-26 16:49:43 +05:30
if (!function_exists('getTokenFromHeader')) {
/**
* Function to get the token from the header and validate it.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
function getTokenFromHeader($request)
{
// Extract the token from the Authorization header
$token = $request->bearerToken();
if (!$token) {
return response()->json(['error' => 'Invalid or missing token'], 401);
}
// Try to parse the token and return the payload (user information)
try {
$payload = JWTAuth::parseToken()->getPayload(); // Get the payload of the token
return $payload; // Return the payload (you can use this in controllers)
} catch (TokenExpiredException $e) {
return response()->json(['error' => 'Token expired'], 401);
} catch (TokenInvalidException $e) {
return response()->json(['error' => 'Token invalid'], 401);
} catch (\Exception $e) {
return response()->json(['error' => 'Failed to authenticate token'], 401);
}
}
}
2025-03-18 15:45:21 +05:30
}