| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoBigin Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoBigin; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 12 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 13 |
use BitCode\BitForm\Core\Util\IpTool; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
/** |
| 17 |
* Provide functionality for ZohoCrm integration |
| 18 |
*/ |
| 19 |
class ZohoBiginHandler |
| 20 |
{ |
| 21 |
private $_formID; |
| 22 |
private $_integrationID; |
| 23 |
private $_logResponse; |
| 24 |
|
| 25 |
public function __construct($integrationID, $fromID) |
| 26 |
{ |
| 27 |
$this->_formID = $fromID; |
| 28 |
$this->_integrationID = $integrationID; |
| 29 |
$this->_logResponse = new UtilApiResponse(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Helps to register ajax function's with wp |
| 34 |
* |
| 35 |
* @return null |
| 36 |
*/ |
| 37 |
public static function registerAjax() |
| 38 |
{ |
| 39 |
add_action('wp_ajax_bitforms_zbigin_generate_token', [__CLASS__, 'generateTokens']); |
| 40 |
add_action('wp_ajax_bitforms_zbigin_refresh_modules', [__CLASS__, 'refreshModulesAjaxHelper']); |
| 41 |
add_action('wp_ajax_bitforms_zbigin_refresh_notetypes', [__CLASS__, 'refreshNoteTypesAjaxHelper']); |
| 42 |
add_action('wp_ajax_bitforms_zbigin_refresh_related_lists', [__CLASS__, 'refreshRelatedModulesAjaxHelper']); |
| 43 |
add_action('wp_ajax_bitforms_zbigin_refresh_fields', [__CLASS__, 'getFields']); |
| 44 |
add_action('wp_ajax_bitforms_zbigin_refresh_tags', [__CLASS__, 'getTagList']); |
| 45 |
add_action('wp_ajax_bitforms_zbigin_refresh_users', [__CLASS__, 'getUsers']); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Process ajax request for generate_token |
| 50 |
* |
| 51 |
* @return JSON zoho bigin api response and status |
| 52 |
*/ |
| 53 |
public static function generateTokens() |
| 54 |
{ |
| 55 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 56 |
$inputJSON = file_get_contents('php://input'); |
| 57 |
$requestsParams = json_decode($inputJSON); |
| 58 |
if ( |
| 59 |
empty($requestsParams->{'accounts-server'}) |
| 60 |
|| empty($requestsParams->dataCenter) |
| 61 |
|| empty($requestsParams->clientId) |
| 62 |
|| empty($requestsParams->clientSecret) |
| 63 |
|| empty($requestsParams->redirectURI) |
| 64 |
|| empty($requestsParams->code) |
| 65 |
) { |
| 66 |
wp_send_json_error( |
| 67 |
__( |
| 68 |
'Requested parameter is empty', |
| 69 |
'bit-form' |
| 70 |
), |
| 71 |
400 |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
$apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; |
| 76 |
$requestParams = [ |
| 77 |
'grant_type' => 'authorization_code', |
| 78 |
'client_id' => $requestsParams->clientId, |
| 79 |
'client_secret' => $requestsParams->clientSecret, |
| 80 |
'redirect_uri' => \urldecode($requestsParams->redirectURI), |
| 81 |
'code' => $requestsParams->code |
| 82 |
]; |
| 83 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 84 |
|
| 85 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 86 |
wp_send_json_error( |
| 87 |
empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 88 |
400 |
| 89 |
); |
| 90 |
} |
| 91 |
$apiResponse->generates_on = \time(); |
| 92 |
wp_send_json_success($apiResponse, 200); |
| 93 |
} else { |
| 94 |
wp_send_json_error( |
| 95 |
__( |
| 96 |
'Token expired', |
| 97 |
'bit-form' |
| 98 |
), |
| 99 |
401 |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Process ajax request for refresh bigin modules |
| 106 |
* |
| 107 |
* @return JSON bigin module data |
| 108 |
*/ |
| 109 |
public static function refreshModulesAjaxHelper() |
| 110 |
{ |
| 111 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 112 |
$authorizationHeader = null; |
| 113 |
$inputJSON = file_get_contents('php://input'); |
| 114 |
$queryParams = json_decode($inputJSON); |
| 115 |
if ( |
| 116 |
empty($queryParams->tokenDetails) |
| 117 |
|| empty($queryParams->dataCenter) |
| 118 |
|| empty($queryParams->clientId) |
| 119 |
|| empty($queryParams->clientSecret) |
| 120 |
) { |
| 121 |
wp_send_json_error( |
| 122 |
__( |
| 123 |
'Requested parameter is empty', |
| 124 |
'bit-form' |
| 125 |
), |
| 126 |
400 |
| 127 |
); |
| 128 |
} |
| 129 |
$response = []; |
| 130 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 131 |
$response['tokenDetails'] = ZohoBiginHandler::_refreshAccessToken($queryParams); |
| 132 |
} |
| 133 |
$modulesMetaApiEndpoint = "https://www.zohoapis.{$queryParams->dataCenter}/bigin/v1/settings/modules"; |
| 134 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 135 |
$modulesMetaResponse = HttpHelper::get($modulesMetaApiEndpoint, null, $authorizationHeader); |
| 136 |
// wp_send_json_success($modulesMetaResponse, 200); |
| 137 |
if (!is_wp_error($modulesMetaResponse) && (empty($modulesMetaResponse->status) || (!empty($modulesMetaResponse->status) && 'error' !== $modulesMetaResponse->status))) { |
| 138 |
$retriveModuleData = $modulesMetaResponse->modules; |
| 139 |
$allModules = []; |
| 140 |
foreach ($retriveModuleData as $module) { |
| 141 |
if (!in_array($module->api_name, ['Activities', 'Social', 'Associated_Products', 'Notes', 'Attachments'])) { |
| 142 |
$allModules[$module->plural_label] = (object) [ |
| 143 |
'api_name' => $module->api_name, |
| 144 |
'plural_label' => $module->plural_label |
| 145 |
]; |
| 146 |
} |
| 147 |
} |
| 148 |
uksort($allModules, 'strnatcasecmp'); |
| 149 |
$response['modules'] = $allModules; |
| 150 |
} else { |
| 151 |
wp_send_json_error( |
| 152 |
empty($modulesMetaResponse->error) ? 'Unknown' : $modulesMetaResponse->error, |
| 153 |
400 |
| 154 |
); |
| 155 |
} |
| 156 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 157 |
ZohoBiginHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['modules']); |
| 158 |
} |
| 159 |
wp_send_json_success($response, 200); |
| 160 |
} else { |
| 161 |
wp_send_json_error( |
| 162 |
__( |
| 163 |
'Token expired', |
| 164 |
'bit-form' |
| 165 |
), |
| 166 |
401 |
| 167 |
); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Process ajax request for refresh bigin modules |
| 173 |
* |
| 174 |
* @return JSON bigin module data |
| 175 |
*/ |
| 176 |
public static function refreshRelatedModulesAjaxHelper() |
| 177 |
{ |
| 178 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 179 |
$inputJSON = file_get_contents('php://input'); |
| 180 |
$queryParams = json_decode($inputJSON); |
| 181 |
if ( |
| 182 |
empty($queryParams->tokenDetails) |
| 183 |
|| empty($queryParams->dataCenter) |
| 184 |
|| empty($queryParams->clientId) |
| 185 |
|| empty($queryParams->clientSecret) |
| 186 |
|| empty($queryParams->module) |
| 187 |
) { |
| 188 |
wp_send_json_error( |
| 189 |
__( |
| 190 |
'Requested parameter is empty', |
| 191 |
'bit-form' |
| 192 |
), |
| 193 |
400 |
| 194 |
); |
| 195 |
} |
| 196 |
$response = []; |
| 197 |
$relatedModules = []; |
| 198 |
|
| 199 |
$allModules = [ |
| 200 |
'Tasks' => (object) [ |
| 201 |
'api_name' => 'Tasks', |
| 202 |
'plural_label' => 'Tasks' |
| 203 |
], |
| 204 |
'Events' => (object) [ |
| 205 |
'api_name' => 'Events', |
| 206 |
'plural_label' => 'Events' |
| 207 |
], |
| 208 |
'Calls' => (object) [ |
| 209 |
'api_name' => 'Calls', |
| 210 |
'plural_label' => 'Calls' |
| 211 |
], |
| 212 |
]; |
| 213 |
// $modulesMetaApiEndpoint = "https://www.zohoapis.{$queryParams->dataCenter}/bigin/v1/settings/related_lists"; |
| 214 |
// $authorizationHeader["Authorization"] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 215 |
// $requiredParams['module'] = $queryParams->module; |
| 216 |
// $modulesMetaResponse = HttpHelper::get($modulesMetaApiEndpoint, $queryParams, $authorizationHeader); |
| 217 |
// wp_send_json_success($modulesMetaResponse, 200); |
| 218 |
foreach ($allModules as $module) { |
| 219 |
if ($module->api_name !== $queryParams->module) { |
| 220 |
$relatedModules[$module->plural_label] = (object) [ |
| 221 |
'api_name' => $module->api_name, |
| 222 |
'plural_label' => $module->plural_label |
| 223 |
]; |
| 224 |
} |
| 225 |
} |
| 226 |
uksort($relatedModules, 'strnatcasecmp'); |
| 227 |
$response['related_modules'] = $relatedModules; |
| 228 |
|
| 229 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 230 |
ZohoBiginHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['related_modules']); |
| 231 |
} |
| 232 |
wp_send_json_success($response, 200); |
| 233 |
} else { |
| 234 |
wp_send_json_error( |
| 235 |
__( |
| 236 |
'Token expired', |
| 237 |
'bit-form' |
| 238 |
), |
| 239 |
401 |
| 240 |
); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Process ajax request for refesh bigin layouts |
| 246 |
* |
| 247 |
* @return JSON bigin layout data |
| 248 |
*/ |
| 249 |
public static function getFields() |
| 250 |
{ |
| 251 |
$authorizationHeader = null; |
| 252 |
$requiredParams = null; |
| 253 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 254 |
$inputJSON = file_get_contents('php://input'); |
| 255 |
$queryParams = json_decode($inputJSON); |
| 256 |
if ( |
| 257 |
empty($queryParams->module) |
| 258 |
|| empty($queryParams->tokenDetails) |
| 259 |
|| empty($queryParams->dataCenter) |
| 260 |
|| empty($queryParams->clientId) |
| 261 |
|| empty($queryParams->clientSecret) |
| 262 |
) { |
| 263 |
wp_send_json_error( |
| 264 |
__( |
| 265 |
'Requested parameter is empty', |
| 266 |
'bit-form' |
| 267 |
), |
| 268 |
400 |
| 269 |
); |
| 270 |
} |
| 271 |
$response = []; |
| 272 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 273 |
$response['tokenDetails'] = ZohoBiginHandler::_refreshAccessToken($queryParams); |
| 274 |
} |
| 275 |
$fieldsMetaApiEndpoint = "https://www.zohoapis.{$queryParams->dataCenter}/bigin/v1/settings/fields"; |
| 276 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 277 |
$requiredParams['module'] = $queryParams->module; |
| 278 |
$fieldsMetaResponse = HttpHelper::get($fieldsMetaApiEndpoint, $requiredParams, $authorizationHeader); |
| 279 |
|
| 280 |
if (!is_wp_error($fieldsMetaResponse) && (empty($fieldsMetaResponse->status) || (!empty($fieldsMetaResponse->status) && 'error' !== $fieldsMetaResponse->status))) { |
| 281 |
$retriveFieldsData = $fieldsMetaResponse->fields; |
| 282 |
$fields = []; |
| 283 |
$fileUploadFields = []; |
| 284 |
$requiredFields = []; |
| 285 |
$requiredFileUploadFiles = []; |
| 286 |
foreach ($retriveFieldsData as $field) { |
| 287 |
$fields[$field->api_name] = (object) [ |
| 288 |
'api_name' => $field->api_name, |
| 289 |
'display_label' => $field->display_label, |
| 290 |
'data_type' => $field->data_type, |
| 291 |
'length' => $field->length, |
| 292 |
'required' => $field->system_mandatory |
| 293 |
]; |
| 294 |
if ($field->system_mandatory) { |
| 295 |
$requiredFields[] = $field->api_name; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
$fields['Pipeline'] = (object) [ |
| 300 |
'api_name' => 'Pipeline', |
| 301 |
'display_label' => 'Pipeline', |
| 302 |
'data_type' => 'text', |
| 303 |
'length' => 120, |
| 304 |
'required' => true |
| 305 |
]; |
| 306 |
$requiredFields[] = 'Pipeline'; |
| 307 |
|
| 308 |
uksort($fields, 'strnatcasecmp'); |
| 309 |
uksort($fileUploadFields, 'strnatcasecmp'); |
| 310 |
usort($requiredFields, 'strnatcasecmp'); |
| 311 |
usort($requiredFileUploadFiles, 'strnatcasecmp'); |
| 312 |
|
| 313 |
$fieldDetails = (object) [ |
| 314 |
'fields' => $fields, |
| 315 |
'fileUploadFields' => $fileUploadFields, |
| 316 |
'required' => $requiredFields, |
| 317 |
'requiredFileUploadFields' => $requiredFileUploadFiles |
| 318 |
]; |
| 319 |
$response['fieldDetails'] = $fieldDetails; |
| 320 |
} else { |
| 321 |
wp_send_json_error( |
| 322 |
'error' === $fieldsMetaResponse->status ? $fieldsMetaResponse->message : 'Unknown', |
| 323 |
400 |
| 324 |
); |
| 325 |
} |
| 326 |
if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { |
| 327 |
$response['queryModule'] = $queryParams->module; |
| 328 |
ZohoBiginHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); |
| 329 |
} |
| 330 |
wp_send_json_success($response, 200); |
| 331 |
} else { |
| 332 |
wp_send_json_error( |
| 333 |
__( |
| 334 |
'Token expired', |
| 335 |
'bit-form' |
| 336 |
), |
| 337 |
401 |
| 338 |
); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
public function getTagList() |
| 343 |
{ |
| 344 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 345 |
$authorizationHeader = null; |
| 346 |
$inputJSON = file_get_contents('php://input'); |
| 347 |
$queryParams = json_decode($inputJSON); |
| 348 |
if ( |
| 349 |
empty($queryParams->tokenDetails) |
| 350 |
|| empty($queryParams->dataCenter) |
| 351 |
|| empty($queryParams->clientId) |
| 352 |
|| empty($queryParams->clientSecret) |
| 353 |
|| empty($queryParams->module) |
| 354 |
) { |
| 355 |
wp_send_json_error( |
| 356 |
__( |
| 357 |
'Requested parameter is empty', |
| 358 |
'bit-form' |
| 359 |
), |
| 360 |
400 |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
$response = []; |
| 365 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 366 |
$response['tokenDetails'] = ZohoBiginHandler::_refreshAccessToken($queryParams); |
| 367 |
} |
| 368 |
|
| 369 |
$tagsMetaApiEndpoint = "http://www.zohoapis.{$queryParams->dataCenter}/bigin/v1/settings/tags?module={$queryParams->module}"; |
| 370 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 371 |
$tagsMetaResponse = HttpHelper::get($tagsMetaApiEndpoint, null, $authorizationHeader); |
| 372 |
|
| 373 |
if (!is_wp_error($tagsMetaResponse)) { |
| 374 |
$tags = $tagsMetaResponse->tags; |
| 375 |
|
| 376 |
if (count($tags) > 0) { |
| 377 |
$allTags = []; |
| 378 |
foreach ($tags as $tag) { |
| 379 |
$allTags[$tag->name] = (object) [ |
| 380 |
'tagId' => $tag->id, |
| 381 |
'tagName' => $tag->name |
| 382 |
]; |
| 383 |
} |
| 384 |
uksort($allTags, 'strnatcasecmp'); |
| 385 |
$response['tags'] = $allTags; |
| 386 |
} |
| 387 |
} else { |
| 388 |
wp_send_json_error( |
| 389 |
empty($tagsMetaResponse->data) ? 'Unknown' : $tagsMetaResponse->error, |
| 390 |
400 |
| 391 |
); |
| 392 |
} |
| 393 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 394 |
ZohoBiginHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 395 |
} |
| 396 |
wp_send_json_success($response, 200); |
| 397 |
} else { |
| 398 |
wp_send_json_error( |
| 399 |
__( |
| 400 |
'Token expired', |
| 401 |
'bit-form' |
| 402 |
), |
| 403 |
401 |
| 404 |
); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
public function getUsers() |
| 409 |
{ |
| 410 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 411 |
$authorizationHeader = null; |
| 412 |
$inputJSON = file_get_contents('php://input'); |
| 413 |
$queryParams = json_decode($inputJSON); |
| 414 |
if ( |
| 415 |
empty($queryParams->tokenDetails) |
| 416 |
|| empty($queryParams->dataCenter) |
| 417 |
|| empty($queryParams->clientId) |
| 418 |
|| empty($queryParams->clientSecret) |
| 419 |
) { |
| 420 |
wp_send_json_error( |
| 421 |
__( |
| 422 |
'Requested parameter is empty', |
| 423 |
'bit-form' |
| 424 |
), |
| 425 |
400 |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
$response = []; |
| 430 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 431 |
$response['tokenDetails'] = ZohoBiginHandler::_refreshAccessToken($queryParams); |
| 432 |
} |
| 433 |
|
| 434 |
$usersMetaApiEndpoint = "https://www.zohoapis.{$queryParams->dataCenter}/bigin/v1/users"; |
| 435 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 436 |
$usersMetaResponse = HttpHelper::get($usersMetaApiEndpoint, null, $authorizationHeader); |
| 437 |
|
| 438 |
// wp_send_json_success($usersMetaResponse, 200); |
| 439 |
|
| 440 |
if (!is_wp_error($usersMetaResponse)) { |
| 441 |
$users = $usersMetaResponse->users; |
| 442 |
|
| 443 |
if (count($users) > 0) { |
| 444 |
$allUsers = []; |
| 445 |
foreach ($users as $user) { |
| 446 |
$allUsers[$user->full_name] = (object) [ |
| 447 |
'userId' => $user->id, |
| 448 |
'userName' => $user->full_name |
| 449 |
]; |
| 450 |
} |
| 451 |
uksort($allUsers, 'strnatcasecmp'); |
| 452 |
$response['users'] = $allUsers; |
| 453 |
} |
| 454 |
} else { |
| 455 |
wp_send_json_error( |
| 456 |
empty($usersMetaResponse->data) ? 'Unknown' : $usersMetaResponse->error, |
| 457 |
400 |
| 458 |
); |
| 459 |
} |
| 460 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 461 |
ZohoBiginHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 462 |
} |
| 463 |
wp_send_json_success($response, 200); |
| 464 |
} else { |
| 465 |
wp_send_json_error( |
| 466 |
__( |
| 467 |
'Token expired', |
| 468 |
'bit-form' |
| 469 |
), |
| 470 |
401 |
| 471 |
); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Helps to refresh zoho bigin access_token |
| 477 |
* |
| 478 |
* @param object $apiData Contains required data for refresh access token |
| 479 |
* @return string|boolean $tokenDetails API token details |
| 480 |
*/ |
| 481 |
protected static function _refreshAccessToken($apiData) |
| 482 |
{ |
| 483 |
if ( |
| 484 |
empty($apiData->dataCenter) |
| 485 |
|| empty($apiData->clientId) |
| 486 |
|| empty($apiData->clientSecret) |
| 487 |
|| empty($apiData->tokenDetails) |
| 488 |
) { |
| 489 |
return false; |
| 490 |
} |
| 491 |
$tokenDetails = $apiData->tokenDetails; |
| 492 |
|
| 493 |
$dataCenter = $apiData->dataCenter; |
| 494 |
$apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; |
| 495 |
$requestParams = [ |
| 496 |
'grant_type' => 'refresh_token', |
| 497 |
'client_id' => $apiData->clientId, |
| 498 |
'client_secret' => $apiData->clientSecret, |
| 499 |
'refresh_token' => $tokenDetails->refresh_token, |
| 500 |
]; |
| 501 |
|
| 502 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 503 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
$tokenDetails->generates_on = \time(); |
| 507 |
$tokenDetails->access_token = $apiResponse->access_token; |
| 508 |
return $tokenDetails; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Save updated access_token to avoid unnecessary token generation |
| 513 |
* |
| 514 |
* @param integer $fromID ID of Integration related form |
| 515 |
* @param integer $integrationID ID of Zoho bigin Integration |
| 516 |
* @param object $tokenDetails refreshed token info |
| 517 |
* |
| 518 |
* @return null |
| 519 |
*/ |
| 520 |
protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) |
| 521 |
{ |
| 522 |
if (empty($formID) || empty($integrationID)) { |
| 523 |
return; |
| 524 |
} |
| 525 |
|
| 526 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 527 |
$zbiginDetails = $integrationHandler->getAIntegration($integrationID); |
| 528 |
|
| 529 |
if (is_wp_error($zbiginDetails)) { |
| 530 |
return; |
| 531 |
} |
| 532 |
$newDetails = json_decode($zbiginDetails[0]->integration_details); |
| 533 |
|
| 534 |
$newDetails->tokenDetails = $tokenDetails; |
| 535 |
if (!empty($others['modules'])) { |
| 536 |
$newDetails->default->modules = $others['modules']; |
| 537 |
} |
| 538 |
if (!empty($others['related_modules'])) { |
| 539 |
$newDetails->default->relatedlist['modules'] = $others['related_modules']; |
| 540 |
} |
| 541 |
|
| 542 |
$integrationHandler->updateIntegration($integrationID, $zbiginDetails[0]->integration_name, 'Zoho Bigin', wp_json_encode($newDetails), 'form'); |
| 543 |
} |
| 544 |
|
| 545 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 546 |
{ |
| 547 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 548 |
$integID = $integrationData->id; |
| 549 |
$tokenDetails = $integrationDetails->tokenDetails; |
| 550 |
$module = $integrationDetails->module; |
| 551 |
$fieldMap = $integrationDetails->field_map; |
| 552 |
$actions = $integrationDetails->actions; |
| 553 |
$defaultDataConf = $integrationDetails->default; |
| 554 |
if ( |
| 555 |
empty($tokenDetails) |
| 556 |
|| empty($module) |
| 557 |
|| empty($fieldMap) |
| 558 |
) { |
| 559 |
$error = new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho bigin api', 'bit-form')); |
| 560 |
$this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error); |
| 561 |
return $error; |
| 562 |
} |
| 563 |
$requiredParams = null; |
| 564 |
if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 565 |
$requiredParams['clientId'] = $integrationDetails->clientId; |
| 566 |
$requiredParams['clientSecret'] = $integrationDetails->clientSecret; |
| 567 |
$requiredParams['dataCenter'] = $integrationDetails->dataCenter; |
| 568 |
$requiredParams['tokenDetails'] = $tokenDetails; |
| 569 |
$newTokenDetails = ZohoBiginHandler::_refreshAccessToken((object)$requiredParams); |
| 570 |
if ($newTokenDetails) { |
| 571 |
ZohoBiginHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); |
| 572 |
$tokenDetails = $newTokenDetails; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
$required = !empty($defaultDataConf->moduleData->{$module}->required) ? |
| 577 |
$defaultDataConf->moduleData->{$module}->required : []; |
| 578 |
|
| 579 |
$actions = $integrationDetails->actions; |
| 580 |
$fileMap = $integrationDetails->upload_field_map; |
| 581 |
$recordApiHelper = new RecordApiHelper($tokenDetails, $integID, $logID); |
| 582 |
$zBiginApiResponse = $recordApiHelper->executeRecordApi( |
| 583 |
$this->_formID, |
| 584 |
$entryID, |
| 585 |
$defaultDataConf, |
| 586 |
$module, |
| 587 |
$fieldValues, |
| 588 |
$fieldMap, |
| 589 |
$actions, |
| 590 |
$required, |
| 591 |
$fileMap |
| 592 |
); |
| 593 |
if (is_wp_error($zBiginApiResponse)) { |
| 594 |
return $zBiginApiResponse; |
| 595 |
} |
| 596 |
|
| 597 |
if ( |
| 598 |
count($integrationDetails->relatedlists) |
| 599 |
&& !empty($zBiginApiResponse->response->result->row->success->details->FL[0]) |
| 600 |
&& 'Id' === $zBiginApiResponse->response->result->row->success->details->FL[0]->val |
| 601 |
) { |
| 602 |
foreach ($integrationDetails->relatedlists as $relatedlist) { |
| 603 |
if (!empty($relatedlist->module)) { |
| 604 |
$recordID = $zBiginApiResponse->response->result->row->success->details->FL[0]->content; |
| 605 |
$relatedListModule = $relatedlist->module; |
| 606 |
$defaultDataConf->moduleData->{$relatedListModule}->fields->{'SEMODULE'} = (object) [ |
| 607 |
'length' => \strlen($relatedListModule), |
| 608 |
'required' => true, |
| 609 |
'data_type' => 'string', |
| 610 |
]; |
| 611 |
$fieldValues['SEMODULE'] = $relatedListModule; |
| 612 |
$relatedlist->field_map[] = (object) |
| 613 |
[ |
| 614 |
'formField' => 'SEMODULE', |
| 615 |
'zohoFormField' => 'SEMODULE' |
| 616 |
]; |
| 617 |
|
| 618 |
$defaultDataConf->moduleData->{$relatedListModule}->fields->{'SEID'} = (object) [ |
| 619 |
'length' => \strlen($recordID), |
| 620 |
'required' => true, |
| 621 |
'data_type' => 'string', |
| 622 |
]; |
| 623 |
$fieldValues['SEID'] = $recordID; |
| 624 |
$relatedlist->field_map[] = (object) |
| 625 |
[ |
| 626 |
'formField' => 'SEID', |
| 627 |
'zohoFormField' => 'SEID' |
| 628 |
]; |
| 629 |
|
| 630 |
$zBiginRelatedRecResp = $recordApiHelper->executeRecordApi( |
| 631 |
$this->_formID, |
| 632 |
$entryID, |
| 633 |
$defaultDataConf, |
| 634 |
$relatedListModule, |
| 635 |
$fieldValues, |
| 636 |
$relatedlist->field_map, |
| 637 |
$relatedlist->actions, |
| 638 |
!empty($defaultDataConf->moduleData->{$relatedListModule}->required) ? |
| 639 |
$defaultDataConf->moduleData->{$relatedListModule}->required : [], |
| 640 |
$relatedlist->upload_field_map |
| 641 |
); |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
return $zBiginApiResponse; |
| 646 |
} |
| 647 |
} |