Files
cheerstothe_season_2.0/app/Http/Helpers/Webhelper.php
Hritikkk9 6a00d7119f server up
2024-07-16 12:10:31 +00:00

223 lines
6.1 KiB
PHP

<?php
use App\Models;
use App\Models\IamPrincipal;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Tymon\JWTAuth\Facades\JWTAuth;
use GuzzleHttp\Client;
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('jsonResponseWithErrorMessage')) {
function jsonResponseWithErrorMessage($errorMessage)
{
$response = [
'status' => 'error',
'message' => $errorMessage,
];
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');
$token = JWTAuth::setToken($tokenData)->getPayload();
// dd("tokendata",$tokenData,$token['sub'],$token['iat']);
//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]
$check_iat = IamPrincipal::where([['id', $token['sub']],])->first();
// 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 = IamPrincipal::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);
}
}