| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoDesk Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoDesk; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use BitCode\BitForm\Core\Util\IpTool; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for ZohoCrm integration |
| 16 |
*/ |
| 17 |
class ZohoDeskHandler |
| 18 |
{ |
| 19 |
private $_formID; |
| 20 |
private $_integrationID; |
| 21 |
|
| 22 |
public function __construct($integrationID, $formID) |
| 23 |
{ |
| 24 |
$this->_formID = $formID; |
| 25 |
$this->_integrationID = $integrationID; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Helps to register ajax function's with wp |
| 30 |
* |
| 31 |
* @return null |
| 32 |
*/ |
| 33 |
public static function registerAjax() |
| 34 |
{ |
| 35 |
add_action('wp_ajax_bitforms_zdesk_generate_token', [__CLASS__, 'generateTokens']); |
| 36 |
add_action('wp_ajax_bitforms_zdesk_refresh_organizations', [__CLASS__, 'refreshOrganizationsAjaxHelper']); |
| 37 |
add_action('wp_ajax_bitforms_zdesk_refresh_departments', [__CLASS__, 'refreshDepartmentsAjaxHelper']); |
| 38 |
add_action('wp_ajax_bitforms_zdesk_refresh_fields', [__CLASS__, 'refreshFieldsAjaxHelper']); |
| 39 |
add_action('wp_ajax_bitforms_zdesk_refresh_owners', [__CLASS__, 'refreshTicketOwnersAjaxHelper']); |
| 40 |
add_action('wp_ajax_bitforms_zdesk_refresh_products', [__CLASS__, 'refreshProductsAjaxHelper']); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Process ajax request for generate_token |
| 45 |
* |
| 46 |
* @return JSON zoho crm api response and status |
| 47 |
*/ |
| 48 |
public static function generateTokens() |
| 49 |
{ |
| 50 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 51 |
$inputJSON = file_get_contents('php://input'); |
| 52 |
$requestsParams = json_decode($inputJSON); |
| 53 |
if ( |
| 54 |
empty($requestsParams->{'accounts-server'}) |
| 55 |
|| empty($requestsParams->dataCenter) |
| 56 |
|| empty($requestsParams->clientId) |
| 57 |
|| empty($requestsParams->clientSecret) |
| 58 |
|| empty($requestsParams->redirectURI) |
| 59 |
|| empty($requestsParams->code) |
| 60 |
) { |
| 61 |
wp_send_json_error( |
| 62 |
__( |
| 63 |
'Requested parameter is empty', |
| 64 |
'bit-form' |
| 65 |
), |
| 66 |
400 |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; |
| 71 |
$requestParams = [ |
| 72 |
'grant_type' => 'authorization_code', |
| 73 |
'client_id' => $requestsParams->clientId, |
| 74 |
'client_secret' => $requestsParams->clientSecret, |
| 75 |
'redirect_uri' => \urldecode($requestsParams->redirectURI), |
| 76 |
'code' => $requestsParams->code |
| 77 |
]; |
| 78 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 79 |
|
| 80 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 81 |
wp_send_json_error( |
| 82 |
empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 83 |
400 |
| 84 |
); |
| 85 |
} |
| 86 |
$apiResponse->generates_on = \time(); |
| 87 |
wp_send_json_success($apiResponse, 200); |
| 88 |
} else { |
| 89 |
wp_send_json_error( |
| 90 |
__( |
| 91 |
'Token expired', |
| 92 |
'bit-form' |
| 93 |
), |
| 94 |
401 |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public static function refreshOrganizationsAjaxHelper() |
| 100 |
{ |
| 101 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 102 |
$authorizationHeader = null; |
| 103 |
$inputJSON = file_get_contents('php://input'); |
| 104 |
$queryParams = json_decode($inputJSON); |
| 105 |
if ( |
| 106 |
empty($queryParams->tokenDetails) |
| 107 |
|| empty($queryParams->dataCenter) |
| 108 |
|| empty($queryParams->clientId) |
| 109 |
|| empty($queryParams->clientSecret) |
| 110 |
) { |
| 111 |
wp_send_json_error( |
| 112 |
__( |
| 113 |
'Requested parameter is empty', |
| 114 |
'bit-form' |
| 115 |
), |
| 116 |
400 |
| 117 |
); |
| 118 |
} |
| 119 |
$response = []; |
| 120 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 121 |
$response['tokenDetails'] = ZohoDeskHandler::_refreshAccessToken($queryParams); |
| 122 |
} |
| 123 |
|
| 124 |
$organizationsMetaApiEndpoint = "https://desk.zoho.{$queryParams->dataCenter}/api/v1/organizations"; |
| 125 |
|
| 126 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 127 |
$organizationsMetaResponse = HttpHelper::get($organizationsMetaApiEndpoint, null, $authorizationHeader); |
| 128 |
|
| 129 |
if (!is_wp_error($organizationsMetaResponse)) { |
| 130 |
$allOrganizations = []; |
| 131 |
$organizations = $organizationsMetaResponse->data; |
| 132 |
|
| 133 |
if (count($organizations) > 0) { |
| 134 |
foreach ($organizations as $organization) { |
| 135 |
$allOrganizations[$organization->companyName] = (object) [ |
| 136 |
'orgId' => $organization->id, |
| 137 |
'portalName' => $organization->companyName |
| 138 |
]; |
| 139 |
} |
| 140 |
} |
| 141 |
uksort($allOrganizations, 'strnatcasecmp'); |
| 142 |
$response['organizations'] = $allOrganizations; |
| 143 |
} else { |
| 144 |
wp_send_json_error( |
| 145 |
empty($organizationsMetaResponse->data) ? 'Unknown' : $organizationsMetaResponse->error, |
| 146 |
400 |
| 147 |
); |
| 148 |
} |
| 149 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 150 |
ZohoDeskHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 151 |
} |
| 152 |
wp_send_json_success($response, 200); |
| 153 |
} else { |
| 154 |
wp_send_json_error( |
| 155 |
__( |
| 156 |
'Token expired', |
| 157 |
'bit-form' |
| 158 |
), |
| 159 |
401 |
| 160 |
); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Process ajax request for refresh crm modules |
| 166 |
* |
| 167 |
* @return JSON crm module data |
| 168 |
*/ |
| 169 |
public static function refreshDepartmentsAjaxHelper() |
| 170 |
{ |
| 171 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 172 |
$authorizationHeader = null; |
| 173 |
$inputJSON = file_get_contents('php://input'); |
| 174 |
$queryParams = json_decode($inputJSON); |
| 175 |
if ( |
| 176 |
empty($queryParams->tokenDetails) |
| 177 |
|| empty($queryParams->dataCenter) |
| 178 |
|| empty($queryParams->clientId) |
| 179 |
|| empty($queryParams->clientSecret) |
| 180 |
|| empty($queryParams->orgId) |
| 181 |
) { |
| 182 |
wp_send_json_error( |
| 183 |
__( |
| 184 |
'Requested parameter is empty', |
| 185 |
'bit-form' |
| 186 |
), |
| 187 |
400 |
| 188 |
); |
| 189 |
} |
| 190 |
$response = []; |
| 191 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 192 |
$response['tokenDetails'] = ZohoDeskHandler::_refreshAccessToken($queryParams); |
| 193 |
} |
| 194 |
|
| 195 |
$departmentsMetaApiEndpoint = "https://desk.zoho.{$queryParams->dataCenter}/api/v1/departments?isEnabled=true&limit=100"; |
| 196 |
|
| 197 |
$authorizationHeader['orgId'] = "{$queryParams->orgId}"; |
| 198 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 199 |
$departmentsMetaResponse = HttpHelper::get($departmentsMetaApiEndpoint, null, $authorizationHeader); |
| 200 |
|
| 201 |
if (!is_wp_error($departmentsMetaResponse)) { |
| 202 |
$allDepartments = []; |
| 203 |
$departments = $departmentsMetaResponse->data; |
| 204 |
|
| 205 |
if (count($departments) > 0) { |
| 206 |
foreach ($departments as $department) { |
| 207 |
$allDepartments[$department->name] = (object) [ |
| 208 |
'departmentId' => $department->id, |
| 209 |
'departmentName' => $department->name |
| 210 |
]; |
| 211 |
} |
| 212 |
} |
| 213 |
uksort($allDepartments, 'strnatcasecmp'); |
| 214 |
$response['departments'] = $allDepartments; |
| 215 |
} else { |
| 216 |
wp_send_json_error( |
| 217 |
empty($departmentsMetaResponse->data) ? 'Unknown' : $departmentsMetaResponse->error, |
| 218 |
400 |
| 219 |
); |
| 220 |
} |
| 221 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 222 |
ZohoDeskHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 223 |
} |
| 224 |
wp_send_json_success($response, 200); |
| 225 |
} else { |
| 226 |
wp_send_json_error( |
| 227 |
__( |
| 228 |
'Token expired', |
| 229 |
'bit-form' |
| 230 |
), |
| 231 |
401 |
| 232 |
); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Process ajax request for refesh crm layouts |
| 238 |
* |
| 239 |
* @return JSON crm layout data |
| 240 |
*/ |
| 241 |
public static function refreshFieldsAjaxHelper() |
| 242 |
{ |
| 243 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 244 |
$authorizationHeader = null; |
| 245 |
$inputJSON = file_get_contents('php://input'); |
| 246 |
$queryParams = json_decode($inputJSON); |
| 247 |
if ( |
| 248 |
empty($queryParams->tokenDetails) |
| 249 |
|| empty($queryParams->dataCenter) |
| 250 |
|| empty($queryParams->clientId) |
| 251 |
|| empty($queryParams->clientSecret) |
| 252 |
|| empty($queryParams->orgId) |
| 253 |
) { |
| 254 |
wp_send_json_error( |
| 255 |
__( |
| 256 |
'Requested parameter is empty', |
| 257 |
'bit-form' |
| 258 |
), |
| 259 |
400 |
| 260 |
); |
| 261 |
} |
| 262 |
$response = []; |
| 263 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 264 |
$response['tokenDetails'] = ZohoDeskHandler::_refreshAccessToken($queryParams); |
| 265 |
} |
| 266 |
|
| 267 |
$fieldsMetaApiEndpoint = "https://desk.zoho.{$queryParams->dataCenter}/api/v1/organizationFields?module=tickets"; |
| 268 |
|
| 269 |
$authorizationHeader['orgId'] = "{$queryParams->orgId}"; |
| 270 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 271 |
$fieldsMetaResponse = HttpHelper::get($fieldsMetaApiEndpoint, null, $authorizationHeader); |
| 272 |
|
| 273 |
if (!is_wp_error($fieldsMetaResponse)) { |
| 274 |
$fields = $fieldsMetaResponse->data; |
| 275 |
|
| 276 |
if (count($fields) > 0) { |
| 277 |
$response['fields']['Contact Name - Last Name'] = (object) [ |
| 278 |
'apiName' => 'lastName', |
| 279 |
'displayLabel' => 'Contact Name - Last Name', |
| 280 |
'isCustomField' => false, |
| 281 |
'required' => true |
| 282 |
]; |
| 283 |
$response['fields']['Contact Name - First Name'] = (object) [ |
| 284 |
'apiName' => 'firstName', |
| 285 |
'displayLabel' => 'Contact Name - First Name', |
| 286 |
'isCustomField' => false, |
| 287 |
'required' => false |
| 288 |
]; |
| 289 |
$response['required'][] = 'lastName'; |
| 290 |
foreach ($fields as $field) { |
| 291 |
if ('contactId' === $field->apiName || 'assigneeId' === $field->apiName) { |
| 292 |
continue; |
| 293 |
} |
| 294 |
$response['fields'][$field->displayLabel] = (object) [ |
| 295 |
'apiName' => $field->apiName, |
| 296 |
'displayLabel' => $field->displayLabel, |
| 297 |
'isCustomField' => $field->isCustomField, |
| 298 |
'required' => $field->isMandatory |
| 299 |
]; |
| 300 |
|
| 301 |
if ($field->isMandatory) { |
| 302 |
$response['required'][] = $field->apiName; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
uksort($response['fields'], 'strnatcasecmp'); |
| 307 |
usort($response['required'], 'strnatcasecmp'); |
| 308 |
} else { |
| 309 |
wp_send_json_error( |
| 310 |
'error' === $fieldsMetaResponse->status ? $fieldsMetaResponse->message : 'Unknown', |
| 311 |
400 |
| 312 |
); |
| 313 |
} |
| 314 |
if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { |
| 315 |
$response['queryModule'] = $queryParams->module; |
| 316 |
ZohoDeskHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); |
| 317 |
} |
| 318 |
wp_send_json_success($response, 200); |
| 319 |
} else { |
| 320 |
wp_send_json_error( |
| 321 |
__( |
| 322 |
'Token expired', |
| 323 |
'bit-form' |
| 324 |
), |
| 325 |
401 |
| 326 |
); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Process ajax request for refresh crm modules |
| 332 |
* |
| 333 |
* @return JSON crm module data |
| 334 |
*/ |
| 335 |
public static function refreshTicketOwnersAjaxHelper() |
| 336 |
{ |
| 337 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 338 |
$authorizationHeader = null; |
| 339 |
$inputJSON = file_get_contents('php://input'); |
| 340 |
$queryParams = json_decode($inputJSON); |
| 341 |
if ( |
| 342 |
empty($queryParams->tokenDetails) |
| 343 |
|| empty($queryParams->dataCenter) |
| 344 |
|| empty($queryParams->clientId) |
| 345 |
|| empty($queryParams->clientSecret) |
| 346 |
|| empty($queryParams->orgId) |
| 347 |
) { |
| 348 |
wp_send_json_error( |
| 349 |
__( |
| 350 |
'Requested parameter is empty', |
| 351 |
'bit-form' |
| 352 |
), |
| 353 |
400 |
| 354 |
); |
| 355 |
} |
| 356 |
$response = []; |
| 357 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 358 |
$response['tokenDetails'] = ZohoDeskHandler::_refreshAccessToken($queryParams); |
| 359 |
} |
| 360 |
|
| 361 |
$ownersMetaApiEndpoint = "https://desk.zoho.{$queryParams->dataCenter}/api/v1/agents?status=ACTIVE&limit=100"; |
| 362 |
|
| 363 |
$authorizationHeader['orgId'] = "{$queryParams->orgId}"; |
| 364 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 365 |
$ownersMetaResponse = HttpHelper::get($ownersMetaApiEndpoint, null, $authorizationHeader); |
| 366 |
|
| 367 |
if (!is_wp_error($ownersMetaResponse)) { |
| 368 |
$owners = $ownersMetaResponse->data; |
| 369 |
|
| 370 |
if (count($owners) > 0) { |
| 371 |
foreach ($owners as $owner) { |
| 372 |
$response['owners'][] = (object) [ |
| 373 |
'ownerId' => $owner->id, |
| 374 |
'ownerName' => $owner->name |
| 375 |
]; |
| 376 |
} |
| 377 |
} |
| 378 |
} else { |
| 379 |
wp_send_json_error( |
| 380 |
empty($ownersMetaResponse->data) ? 'Unknown' : $ownersMetaResponse->error, |
| 381 |
400 |
| 382 |
); |
| 383 |
} |
| 384 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 385 |
ZohoDeskHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 386 |
} |
| 387 |
wp_send_json_success($response, 200); |
| 388 |
} else { |
| 389 |
wp_send_json_error( |
| 390 |
__( |
| 391 |
'Token expired', |
| 392 |
'bit-form' |
| 393 |
), |
| 394 |
401 |
| 395 |
); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Process ajax request for refresh crm modules |
| 401 |
* |
| 402 |
* @return JSON crm module data |
| 403 |
*/ |
| 404 |
public static function refreshProductsAjaxHelper() |
| 405 |
{ |
| 406 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 407 |
$authorizationHeader = null; |
| 408 |
$inputJSON = file_get_contents('php://input'); |
| 409 |
$queryParams = json_decode($inputJSON); |
| 410 |
if ( |
| 411 |
empty($queryParams->tokenDetails) |
| 412 |
|| empty($queryParams->dataCenter) |
| 413 |
|| empty($queryParams->clientId) |
| 414 |
|| empty($queryParams->clientSecret) |
| 415 |
|| empty($queryParams->orgId) |
| 416 |
|| empty($queryParams->departmentId) |
| 417 |
) { |
| 418 |
wp_send_json_error( |
| 419 |
__( |
| 420 |
'Requested parameter is empty', |
| 421 |
'bit-form' |
| 422 |
), |
| 423 |
400 |
| 424 |
); |
| 425 |
} |
| 426 |
|
| 427 |
$response = []; |
| 428 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 429 |
$response['tokenDetails'] = ZohoDeskHandler::_refreshAccessToken($queryParams); |
| 430 |
} |
| 431 |
|
| 432 |
$productsMetaApiEndpoint = "https://desk.zoho.{$queryParams->dataCenter}/api/v1/products?departmentId={$queryParams->departmentId}&limit=100"; |
| 433 |
|
| 434 |
$authorizationHeader['orgId'] = "{$queryParams->orgId}"; |
| 435 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 436 |
$productsMetaResponse = HttpHelper::get($productsMetaApiEndpoint, null, $authorizationHeader); |
| 437 |
|
| 438 |
if (!is_wp_error($productsMetaResponse)) { |
| 439 |
$products = $productsMetaResponse->data; |
| 440 |
|
| 441 |
if (count($products) > 0) { |
| 442 |
foreach ($products as $product) { |
| 443 |
$response['products'][] = (object) [ |
| 444 |
'productId' => $product->id, |
| 445 |
'productName' => $product->productName |
| 446 |
]; |
| 447 |
} |
| 448 |
} |
| 449 |
} else { |
| 450 |
wp_send_json_error( |
| 451 |
empty($productsMetaResponse->data) ? 'Unknown' : $productsMetaResponse->error, |
| 452 |
400 |
| 453 |
); |
| 454 |
} |
| 455 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 456 |
ZohoDeskHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 457 |
} |
| 458 |
wp_send_json_success($response, 200); |
| 459 |
} else { |
| 460 |
wp_send_json_error( |
| 461 |
__( |
| 462 |
'Token expired', |
| 463 |
'bit-form' |
| 464 |
), |
| 465 |
401 |
| 466 |
); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Helps to refresh zoho crm access_token |
| 472 |
* |
| 473 |
* @param Array $apiData Contains required data for refresh access token |
| 474 |
* @return JSON $tokenDetails API token details |
| 475 |
*/ |
| 476 |
protected static function _refreshAccessToken($apiData) |
| 477 |
{ |
| 478 |
if ( |
| 479 |
empty($apiData->dataCenter) |
| 480 |
|| empty($apiData->clientId) |
| 481 |
|| empty($apiData->clientSecret) |
| 482 |
|| empty($apiData->tokenDetails) |
| 483 |
) { |
| 484 |
return false; |
| 485 |
} |
| 486 |
$tokenDetails = $apiData->tokenDetails; |
| 487 |
|
| 488 |
$dataCenter = $apiData->dataCenter; |
| 489 |
$apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; |
| 490 |
$requestParams = [ |
| 491 |
'grant_type' => 'refresh_token', |
| 492 |
'client_id' => $apiData->clientId, |
| 493 |
'client_secret' => $apiData->clientSecret, |
| 494 |
'refresh_token' => $tokenDetails->refresh_token, |
| 495 |
]; |
| 496 |
|
| 497 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 498 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 499 |
return false; |
| 500 |
} |
| 501 |
$tokenDetails->generates_on = \time(); |
| 502 |
$tokenDetails->access_token = $apiResponse->access_token; |
| 503 |
return $tokenDetails; |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Save updated access_token to avoid unnecessary token generation |
| 508 |
* |
| 509 |
* @param Integer $formID ID of Integration related form |
| 510 |
* @param Integer $integrationID ID of Zoho crm Integration |
| 511 |
* @param Obeject $tokenDetails refreshed token info |
| 512 |
* |
| 513 |
* @return null |
| 514 |
*/ |
| 515 |
protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) |
| 516 |
{ |
| 517 |
if (empty($formID) || empty($integrationID)) { |
| 518 |
return; |
| 519 |
} |
| 520 |
|
| 521 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 522 |
$zdeskDetails = $integrationHandler->getAIntegration($integrationID); |
| 523 |
|
| 524 |
if (is_wp_error($zdeskDetails)) { |
| 525 |
return; |
| 526 |
} |
| 527 |
$newDetails = json_decode($zdeskDetails[0]->integration_details); |
| 528 |
|
| 529 |
$newDetails->tokenDetails = $tokenDetails; |
| 530 |
if (!empty($others['organizations'])) { |
| 531 |
$newDetails->default->organizations = $others['organizations']; |
| 532 |
} |
| 533 |
|
| 534 |
$integrationHandler->updateIntegration($integrationID, $zdeskDetails[0]->integration_name, 'Zoho Desk', \json_encode($newDetails), 'form'); |
| 535 |
} |
| 536 |
|
| 537 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 538 |
{ |
| 539 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 540 |
|
| 541 |
$tokenDetails = $integrationDetails->tokenDetails; |
| 542 |
$orgId = $integrationDetails->orgId; |
| 543 |
$department = $integrationDetails->department; |
| 544 |
$dataCenter = $integrationDetails->dataCenter; |
| 545 |
$fieldMap = $integrationDetails->field_map; |
| 546 |
$required = $integrationDetails->default->fields->{$orgId}->required; |
| 547 |
$actions = $integrationDetails->actions; |
| 548 |
if ( |
| 549 |
empty($tokenDetails) |
| 550 |
|| empty($orgId) |
| 551 |
|| empty($department) |
| 552 |
|| empty($fieldMap) |
| 553 |
) { |
| 554 |
return new WP_Error('REQ_FIELD_EMPTY', __('list are required for zoho desk api', 'bit-form')); |
| 555 |
} |
| 556 |
|
| 557 |
$requiredParams = null; |
| 558 |
if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 559 |
$requiredParams['clientId'] = $integrationDetails->clientId; |
| 560 |
$requiredParams['clientSecret'] = $integrationDetails->clientSecret; |
| 561 |
$requiredParams['dataCenter'] = $integrationDetails->dataCenter; |
| 562 |
$requiredParams['tokenDetails'] = $tokenDetails; |
| 563 |
$newTokenDetails = ZohoDeskHandler::_refreshAccessToken((object)$requiredParams); |
| 564 |
if ($newTokenDetails) { |
| 565 |
ZohoDeskHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); |
| 566 |
$tokenDetails = $newTokenDetails; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
// $actions = $integrationDetails->actions; |
| 571 |
$recordApiHelper = new RecordApiHelper($tokenDetails, $orgId, $this->_integrationID, $logID); |
| 572 |
|
| 573 |
$zdeskApiResponse = $recordApiHelper->executeRecordApi( |
| 574 |
$department, |
| 575 |
$dataCenter, |
| 576 |
$fieldValues, |
| 577 |
$fieldMap, |
| 578 |
$required, |
| 579 |
$actions, |
| 580 |
$entryID, |
| 581 |
$this->_formID |
| 582 |
); |
| 583 |
|
| 584 |
if (is_wp_error($zdeskApiResponse)) { |
| 585 |
return $zdeskApiResponse; |
| 586 |
} |
| 587 |
return $zdeskApiResponse; |
| 588 |
} |
| 589 |
} |
| 590 |
|