84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
|
|
include_once 'lib/ghl/oauth2.php';
|
|
include_once 'config.php';
|
|
include_once 'project-model.php';
|
|
|
|
|
|
$config = MkdConfig::get_instance()->get_config();
|
|
|
|
$oauth = new GHLOAuth2($config);
|
|
|
|
class GHLCalendar {
|
|
private $access_token;
|
|
private $refresh_token;
|
|
private $token_expiry;
|
|
private $oauth;
|
|
private $project_id;
|
|
|
|
public function __construct($project_id, $access_token, $refresh_token) {
|
|
$this->project_id = $project_id;
|
|
$this->access_token = $access_token;
|
|
$this->refresh_token = $refresh_token;
|
|
$this->oauth = $GLOBALS['oauth'];
|
|
}
|
|
|
|
public function refreshToken() {
|
|
$result = $this->oauth->refreshToken($this->refresh_token);
|
|
|
|
|
|
if ($result['success']) {
|
|
$this->access_token = $result['data']['access_token'];
|
|
$this->refresh_token = $result['data']['refresh_token'] ?? $this->refresh_token;
|
|
|
|
$projectModel = new ProjectModel();
|
|
$projectModel->edit([
|
|
'access_token' => $this->access_token,
|
|
'refresh_token' => $this->refresh_token
|
|
], $this->project_id );
|
|
|
|
return [
|
|
'code' => 200,
|
|
'message' => 'Access token refreshed successfully',
|
|
'data' => $result['data']
|
|
];
|
|
} else {
|
|
return [
|
|
'code' => 400,
|
|
'message' => 'Error: ' . $result['error'] . '. Please authorize',
|
|
'data' => null
|
|
];
|
|
}
|
|
}
|
|
|
|
public function getFreeSlots($calendar_id, $start_date, $end_date) {
|
|
|
|
|
|
$url = "https://services.leadconnectorhq.com/calendars/$calendar_id/free-slots?startDate=$start_date&endDate=$end_date";
|
|
$headers = [
|
|
"Accept: application/json",
|
|
"Authorization: Bearer " . $this->access_token,
|
|
"Version: 2021-04-15"
|
|
];
|
|
|
|
// Initialize cURL session
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
// Execute cURL request
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get the HTTP response code
|
|
curl_close($ch);
|
|
|
|
// Decode the response
|
|
$responseData = json_decode($response, true);
|
|
|
|
// Prepare the return array
|
|
return [
|
|
'code' => $httpCode,
|
|
'message' => $responseData['message'] ?? 'Success', // Default message if not present
|
|
'data' => $responseData
|
|
];
|
|
}
|
|
} |