init
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
<?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
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
|
||||
<?php
|
||||
|
||||
class GHLOAuth2 {
|
||||
private $config;
|
||||
private $baseUrl = 'https://services.leadconnectorhq.com';
|
||||
private $authorizeUrl = 'https://marketplace.gohighlevel.com/oauth/chooselocation';
|
||||
private $scopes = ['calendars.readonly', 'calendars/events.readonly'];
|
||||
private $redirectUri = 'https://ghltool.team-followup.com/ghl/callback';
|
||||
private $clientId = 'xxx';
|
||||
private $clientSecret = 'xxx';
|
||||
|
||||
public function __construct(array $config) {
|
||||
if (!isset($config['gohighlevel_client_id']) || !isset($config['gohighlevel_client_secret'])) {
|
||||
throw new Exception('Client ID and Client Secret are required.');
|
||||
}
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function getAuthorizationUrl() {
|
||||
$params = [
|
||||
'response_type' => 'code',
|
||||
'redirect_uri' => $this->config['gohighlevel_redirect_uri'] ?? $this->redirectUri,
|
||||
'client_id' => $this->config['gohighlevel_client_id'] ?? $this->clientId,
|
||||
'scope' => implode(' ', $this->scopes),
|
||||
'user_type' => 'Location'
|
||||
];
|
||||
|
||||
return $this->authorizeUrl . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
/*
|
||||
Gohighlevel returns the code in the redirect uri ?code=xxxx
|
||||
*/
|
||||
|
||||
public function getAccessToken($code, $redirectUri = null) {
|
||||
$data = [
|
||||
'client_id' => $this->config['gohighlevel_client_id'] ?? $this->clientId,
|
||||
'client_secret' => $this->config['gohighlevel_client_secret'] ?? $this->clientSecret,
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code,
|
||||
'user_type' => 'Location'
|
||||
];
|
||||
|
||||
if ($redirectUri) {
|
||||
$data['redirect_uri'] = $redirectUri;
|
||||
}
|
||||
|
||||
return $this->makeTokenRequest($data);
|
||||
}
|
||||
|
||||
public function refreshToken($refreshToken) {
|
||||
if (empty($refreshToken)) {
|
||||
throw new Exception('Refresh token is required');
|
||||
}
|
||||
|
||||
|
||||
$data = [
|
||||
'client_id' => $this->config['gohighlevel_client_id'] ?? $this->clientId,
|
||||
'client_secret' => $this->config['gohighlevel_client_secret'] ?? $this->clientSecret,
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $refreshToken,
|
||||
'user_type' => 'Location'
|
||||
];
|
||||
|
||||
return $this->makeTokenRequest($data);
|
||||
}
|
||||
|
||||
private function makeTokenRequest($data) {
|
||||
$curl = curl_init();
|
||||
|
||||
$options = [
|
||||
CURLOPT_URL => $this->baseUrl . '/oauth/token',
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => 'POST',
|
||||
CURLOPT_POSTFIELDS => http_build_query($data),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Accept: application/json',
|
||||
'Content-Type: application/x-www-form-urlencoded'
|
||||
],
|
||||
];
|
||||
|
||||
curl_setopt_array($curl, $options);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($curl);
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
if ($error) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Curl error: ' . $error
|
||||
];
|
||||
}
|
||||
|
||||
$responseData = json_decode($response, true);
|
||||
if ($responseData === null) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Error decoding JSON response'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $responseData['message'] ?? 'Unknown error occurred',
|
||||
'code' => $httpCode,
|
||||
'raw_response' => $responseData
|
||||
];
|
||||
}
|
||||
|
||||
// Ensure we always return refresh_token if it exists in the response
|
||||
return [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'access_token' => $responseData['access_token'] ?? null,
|
||||
'refresh_token' => $responseData['refresh_token'] ?? null,
|
||||
'expires_in' => $responseData['expires_in'] ?? null,
|
||||
'locationId' => $responseData['locationId'] ?? null,
|
||||
'companyId' => $responseData['companyId'] ?? null,
|
||||
'userId' => $responseData['userId'] ?? null,
|
||||
'scope' => $responseData['scope'] ?? null,
|
||||
'token_type' => $responseData['token_type'] ?? null
|
||||
],
|
||||
'raw_response' => $responseData
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user