|
|
|
@@ -9,6 +9,9 @@ use Exception;
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
|
use Kevinrob\GuzzleCache\CacheMiddleware;
|
|
|
|
|
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;
|
|
|
|
|
|
|
|
|
|
class Calendly
|
|
|
|
|
{
|
|
|
|
@@ -117,6 +120,24 @@ class Calendly
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public function getUserSlug()
|
|
|
|
|
{
|
|
|
|
|
// 1. Call the /users/me API to get user information
|
|
|
|
|
$client = new Client();
|
|
|
|
|
try {
|
|
|
|
|
$response = $client->request('GET', 'https://api.calendly.com/users/me', [
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken(),
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$data = json_decode($response->getBody(), true);
|
|
|
|
|
return $userUri = $data['resource']['slug'];
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public function eventTypes()
|
|
|
|
|
{
|
|
|
|
|
// 1. Call the /users/me API to get user information
|
|
|
|
@@ -145,7 +166,89 @@ class Calendly
|
|
|
|
|
|
|
|
|
|
$setting->save();
|
|
|
|
|
}
|
|
|
|
|
function getAvailableDates($even_type_url, $month, $tz = "UTC")
|
|
|
|
|
function mapTimezone($tz){
|
|
|
|
|
$timezone_maping = [
|
|
|
|
|
"PST"=> "America/Los_Angeles",
|
|
|
|
|
"EST"=> "America/New_York",
|
|
|
|
|
"CST"=> "America/Chicago",
|
|
|
|
|
"MST"=> "America/Denver",
|
|
|
|
|
"PST"=> "America/Los_Angeles",
|
|
|
|
|
"AST"=> "America/Halifax",
|
|
|
|
|
];
|
|
|
|
|
return data_get($timezone_maping, $tz, $tz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAvailableDates($even_type_url, $month, $tz = "UTC"){
|
|
|
|
|
$user_slug = $this->getUserSlug();
|
|
|
|
|
//https://calendly.com/api/booking/event_types/lookup?event_type_slug=15-minute&profile_slug=vini-peptidewebmd
|
|
|
|
|
//https://api.calendly.com/event_types/527074d7-c194-419a-9575-60de51220a8e/calendar/range?timezone=America/Chicago&diagnostics=false&range_start=2024-10-01&range_end=2024-10-31&scheduling_link_uuid=cmm8-rtf-7q6
|
|
|
|
|
//https://calendly.com/api/booking/event_types/527074d7-c194-419a-9575-60de51220a8e/calendar/range?timezone=Asia%2FKarachi&diagnostics=false&range_start=2024-10-26&range_end=2024-10-31&scheduling_link_uuid=cmm8-rtf-7q6
|
|
|
|
|
// $user_slug = explode('/', $userUri)[4];
|
|
|
|
|
$client = new Client();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$response = $client->request('GET', $even_type_url, [
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken(),
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
// 'query' => $queryParams
|
|
|
|
|
]);
|
|
|
|
|
$event = json_decode($response->getBody(), true);
|
|
|
|
|
$web_event = json_decode($this->makeRequest("https://calendly.com/api/booking/event_types/lookup?event_type_slug={$event['resource']['slug']}&profile_slug={$user_slug}"),true);
|
|
|
|
|
// var_Dump("https://calendly.com/api/booking/event_types/lookup?event_type_slug={$event['resource']['slug']}&profile_slug={$user_slug}");
|
|
|
|
|
$event_slug = data_get($web_event,"uuid");
|
|
|
|
|
// $eventDetails = json_decode($this->makeRequest($eventDetailsUrl));
|
|
|
|
|
// $eventDetailsUrl = "https://calendly.com/api/booking/event_types/lookup?event_type_slug=$event_type&profile_slug=$owner_id";
|
|
|
|
|
$current_month = Carbon::now()->month;
|
|
|
|
|
$current_date = Carbon::now()->day;
|
|
|
|
|
$timezone = $this->mapTimezone($tz);
|
|
|
|
|
// if ($month == $current_month && $current_date > 15) {}
|
|
|
|
|
$date = Carbon::createFromDate(Carbon::now()->year, $month, 1);
|
|
|
|
|
// $start_date = Carbon::now()->startOfMonth()->tz("UTC");
|
|
|
|
|
$start_date = $date->startOfMonth()->format('Y-m-d');
|
|
|
|
|
$end_date = $date->endOfMonth()->format('Y-m-d');
|
|
|
|
|
$uid = data_get($web_event,"scheduling_link.uid");
|
|
|
|
|
$url = "https://calendly.com/api/booking/event_types/{$event_slug}/calendar/range?timezone=$timezone&diagnostics=false&range_start=$start_date&range_end=$end_date&scheduling_link_uuid={$uid}";
|
|
|
|
|
$response = $client->request('GET', $url, [
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken(),
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$eventDetails = json_decode($this->makeRequest($url),true);
|
|
|
|
|
$availableSlots = [];
|
|
|
|
|
foreach ($eventDetails['days'] as $_event) {
|
|
|
|
|
if($_event['status'] == 'available') {
|
|
|
|
|
$slots = [
|
|
|
|
|
// 'date' => $event['date'],
|
|
|
|
|
|
|
|
|
|
];
|
|
|
|
|
foreach ($_event['spots'] as $slot) {
|
|
|
|
|
if($slot['status'] == 'available'){
|
|
|
|
|
$slotDateTime = Carbon::parse($slot['start_time'])->tz($timezone);
|
|
|
|
|
$slot['formatted_datetime'] = $slotDateTime->format('Y-m-d g:i:00');
|
|
|
|
|
$slot['scheduling_url'] = "https://calendly.com/$user_slug/{$event['resource']['slug']}/{$slot['start_time']}";
|
|
|
|
|
}
|
|
|
|
|
$slots[] = $slot;
|
|
|
|
|
}
|
|
|
|
|
$availableSlots[$_event['date']] = $slots;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// var_dump($eventDetails);
|
|
|
|
|
return $availableSlots;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
function getAvailableDatesOld($even_type_url, $month, $tz = "UTC")
|
|
|
|
|
{
|
|
|
|
|
$cacheKey = $even_type_url . $month . $tz;
|
|
|
|
|
return Cache::remember($cacheKey, now()->addMinutes(2), function () use ($even_type_url, $month, $tz) {
|
|
|
|
|
return $this->getAvailableDates2($even_type_url, $month, $tz);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
function getAvailableDates2($even_type_url, $month, $tz = "UTC")
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
@@ -161,7 +264,6 @@ class Calendly
|
|
|
|
|
$start_time = $date->startOfDay()->format('Y-m-d\T24:00:00.000000\Z');
|
|
|
|
|
$end_time = $date->addDays(7)->endOfDay()->format('Y-m-d\T24:00:00.000000\Z');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
@@ -172,6 +274,7 @@ class Calendly
|
|
|
|
|
'start_time' => $start_time,
|
|
|
|
|
'end_time' => $end_time
|
|
|
|
|
];
|
|
|
|
|
// dd($queryParams);
|
|
|
|
|
$str = "event_type=" . urlencode($queryParams['event_type']) . "&" . "start_time=" . urlencode($queryParams['start_time']) . "&" . "end_time=" . urlencode($queryParams['end_time']);
|
|
|
|
|
$eventTypeUrl = $eventTypeUrl . "?" . ($str);
|
|
|
|
|
// Send the request to Calendly
|
|
|
|
@@ -252,12 +355,15 @@ class Calendly
|
|
|
|
|
|
|
|
|
|
function getCrfToken($url)
|
|
|
|
|
{
|
|
|
|
|
$response = $this->makeRequest($url);
|
|
|
|
|
|
|
|
|
|
// var_dump($response);
|
|
|
|
|
//<meta name="csrf-token" content="mTW6eNk5xiDTzby7yHl_iVTO1_pi-CXZ2U9MYPcXGJGMpksW4iGLJniNxs-T4xrmYlLbXFaLuU8FZ75cuorz8w" />
|
|
|
|
|
preg_match('/<meta name="csrf-token" content="(.*?)" \/>/', $response, $matches);
|
|
|
|
|
return $matches[1];
|
|
|
|
|
try {
|
|
|
|
|
$response = $this->makeRequest($url);
|
|
|
|
|
preg_match('/<meta name="csrf-token" content="(.*?)" \/>/', $response, $matches);
|
|
|
|
|
return $matches[1];
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
$response = $this->makeRequest($url);
|
|
|
|
|
preg_match('/<meta name="csrf-token" content="(.*?)" \/>/', $response, $matches);
|
|
|
|
|
return $matches[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getEventDetails($url)
|
|
|
|
@@ -284,14 +390,16 @@ class Calendly
|
|
|
|
|
$bookingDate = $url_parts[5];
|
|
|
|
|
$event_type = $url_parts[4];
|
|
|
|
|
$owner_id = $url_parts[3];
|
|
|
|
|
$eventDetailsUrl = "https://calendly.com/api/booking/profiles/$owner_id/event_types/$event_type";
|
|
|
|
|
$eventDetailsUrl = "https://calendly.com/api/booking/event_types/lookup?event_type_slug=$event_type&profile_slug=$owner_id";
|
|
|
|
|
$eventDetails = json_decode($this->makeRequest($eventDetailsUrl));
|
|
|
|
|
// var_dump($eventDetails);
|
|
|
|
|
$crfToken = $this->getCrfToken($url);
|
|
|
|
|
|
|
|
|
|
$eventUuid = $eventDetails->uuid;
|
|
|
|
|
$link_uuid = $eventDetails->scheduling_link->uid;
|
|
|
|
|
$custom_fields_id = $eventDetails->custom_fields[0]->id;
|
|
|
|
|
$timezone = $this->mapTimezone($timezone);
|
|
|
|
|
// $eventUuid = $eventDetails->uuid;
|
|
|
|
|
$eventUuid = data_get($eventDetails, 'eventDetails.uid');
|
|
|
|
|
$link_uuid = data_get($eventDetails, 'scheduling_link.uid');
|
|
|
|
|
// $custom_fields_id = $eventDetails->custom_fields[0]->id;
|
|
|
|
|
$custom_fields_id = data_get($eventDetails, 'custom_fields.0.id');
|
|
|
|
|
$booking_request_id = urlencode($this->generateRandomString(36) . "|$bookingDate|$eventUuid|$name");
|
|
|
|
|
//convert to php array
|
|
|
|
|
$bookingData = [
|
|
|
|
@@ -305,14 +413,14 @@ class Calendly
|
|
|
|
|
"booking_flow" => "v3",
|
|
|
|
|
"seconds_to_convert" => 200
|
|
|
|
|
],
|
|
|
|
|
"embed" => [],
|
|
|
|
|
"embed" => (object)[],
|
|
|
|
|
"event" => [
|
|
|
|
|
"start_time" => $bookingDate,
|
|
|
|
|
"location_configuration" => [
|
|
|
|
|
"location_configuration" => data_get($eventDetails,"location_configurations.0",[
|
|
|
|
|
"location" => "",
|
|
|
|
|
"phone_number" => "",
|
|
|
|
|
"additional_info" => ""
|
|
|
|
|
],
|
|
|
|
|
]),
|
|
|
|
|
"guests" => []
|
|
|
|
|
],
|
|
|
|
|
"event_fields" => [
|
|
|
|
@@ -333,7 +441,7 @@ class Calendly
|
|
|
|
|
"full_name" => $name,
|
|
|
|
|
"email" => $email
|
|
|
|
|
],
|
|
|
|
|
"payment_token" => [],
|
|
|
|
|
"payment_token" => (object)[],
|
|
|
|
|
"tracking" => [
|
|
|
|
|
"fingerprint" => $this->generateRandomString(32)
|
|
|
|
|
],
|
|
|
|
|