| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoCampaigns Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoCampaigns; |
| 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 |
|
| 15 |
/** |
| 16 |
* Provide functionality for ZohoCrm integration |
| 17 |
*/ |
| 18 |
class ZohoCampaignsHandler |
| 19 |
{ |
| 20 |
private $_formID; |
| 21 |
private $_integrationID; |
| 22 |
|
| 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_zcampaigns_generate_token', [__CLASS__, 'generateTokens']); |
| 40 |
add_action('wp_ajax_bitforms_zcampaigns_refresh_lists', [__CLASS__, 'refreshListsAjaxHelper']); |
| 41 |
add_action('wp_ajax_bitforms_zcampaigns_refresh_contact_fields', [__CLASS__, 'refreshContactFieldsAjaxHelper']); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Process ajax request for generate_token |
| 46 |
* |
| 47 |
* @return string zoho crm api response and status |
| 48 |
*/ |
| 49 |
public static function generateTokens() |
| 50 |
{ |
| 51 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 52 |
$inputJSON = file_get_contents('php://input'); |
| 53 |
$requestsParams = json_decode($inputJSON); |
| 54 |
if ( |
| 55 |
empty($requestsParams->{'accounts-server'}) |
| 56 |
|| empty($requestsParams->dataCenter) |
| 57 |
|| empty($requestsParams->clientId) |
| 58 |
|| empty($requestsParams->clientSecret) |
| 59 |
|| empty($requestsParams->redirectURI) |
| 60 |
|| empty($requestsParams->code) |
| 61 |
) { |
| 62 |
wp_send_json_error( |
| 63 |
__( |
| 64 |
'Requested parameter is empty', |
| 65 |
'bit-form' |
| 66 |
), |
| 67 |
400 |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
$apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; |
| 72 |
$requestParams = [ |
| 73 |
'grant_type' => 'authorization_code', |
| 74 |
'client_id' => $requestsParams->clientId, |
| 75 |
'client_secret' => $requestsParams->clientSecret, |
| 76 |
'redirect_uri' => \urldecode($requestsParams->redirectURI), |
| 77 |
'code' => $requestsParams->code |
| 78 |
]; |
| 79 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 80 |
|
| 81 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 82 |
wp_send_json_error( |
| 83 |
empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 84 |
400 |
| 85 |
); |
| 86 |
} |
| 87 |
$apiResponse->generates_on = \time(); |
| 88 |
wp_send_json_success($apiResponse, 200); |
| 89 |
} else { |
| 90 |
wp_send_json_error( |
| 91 |
__( |
| 92 |
'Token expired', |
| 93 |
'bit-form' |
| 94 |
), |
| 95 |
401 |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Process ajax request for refresh crm modules |
| 102 |
* |
| 103 |
* @return string crm module data |
| 104 |
*/ |
| 105 |
public static function refreshListsAjaxHelper() |
| 106 |
{ |
| 107 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 108 |
$authorizationHeader = null; |
| 109 |
$inputJSON = file_get_contents('php://input'); |
| 110 |
$queryParams = json_decode($inputJSON); |
| 111 |
if ( |
| 112 |
empty($queryParams->tokenDetails) |
| 113 |
|| empty($queryParams->dataCenter) |
| 114 |
|| empty($queryParams->clientId) |
| 115 |
|| empty($queryParams->clientSecret) |
| 116 |
) { |
| 117 |
wp_send_json_error( |
| 118 |
__( |
| 119 |
'Requested parameter is empty', |
| 120 |
'bit-form' |
| 121 |
), |
| 122 |
400 |
| 123 |
); |
| 124 |
} |
| 125 |
$response = []; |
| 126 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 127 |
$response['tokenDetails'] = ZohoCampaignsHandler::_refreshAccessToken($queryParams); |
| 128 |
} |
| 129 |
|
| 130 |
$listsMetaApiEndpoint = "https://campaigns.zoho.{$queryParams->dataCenter}/api/v1.1/getmailinglists?resfmt=JSON&range=100"; |
| 131 |
|
| 132 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 133 |
$listsMetaResponse = HttpHelper::get($listsMetaApiEndpoint, null, $authorizationHeader); |
| 134 |
|
| 135 |
$allLists = []; |
| 136 |
if (!is_wp_error($listsMetaResponse)) { |
| 137 |
$lists = $listsMetaResponse->list_of_details; |
| 138 |
|
| 139 |
if (count($lists) > 0) { |
| 140 |
foreach ($lists as $list) { |
| 141 |
$allLists[$list->listname] = (object) [ |
| 142 |
'listkey' => $list->listkey, |
| 143 |
'listname' => $list->listname |
| 144 |
]; |
| 145 |
} |
| 146 |
} |
| 147 |
uksort($allLists, 'strnatcasecmp'); |
| 148 |
$response['lists'] = $allLists; |
| 149 |
} else { |
| 150 |
wp_send_json_error( |
| 151 |
empty($listsMetaResponse->data) ? 'Unknown' : $listsMetaResponse->error, |
| 152 |
400 |
| 153 |
); |
| 154 |
} |
| 155 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 156 |
ZohoCampaignsHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']); |
| 157 |
} |
| 158 |
wp_send_json_success($response, 200); |
| 159 |
} else { |
| 160 |
wp_send_json_error( |
| 161 |
__( |
| 162 |
'Token expired', |
| 163 |
'bit-form' |
| 164 |
), |
| 165 |
401 |
| 166 |
); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Process ajax request for refesh crm layouts |
| 172 |
* |
| 173 |
* @return string crm layout data |
| 174 |
*/ |
| 175 |
public static function refreshContactFieldsAjaxHelper() |
| 176 |
{ |
| 177 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 178 |
$authorizationHeader = null; |
| 179 |
$inputJSON = file_get_contents('php://input'); |
| 180 |
$queryParams = json_decode($inputJSON); |
| 181 |
if ( |
| 182 |
empty($queryParams->list) |
| 183 |
|| empty($queryParams->tokenDetails) |
| 184 |
|| empty($queryParams->dataCenter) |
| 185 |
|| empty($queryParams->clientId) |
| 186 |
|| empty($queryParams->clientSecret) |
| 187 |
) { |
| 188 |
wp_send_json_error( |
| 189 |
__( |
| 190 |
'Requested parameter is empty', |
| 191 |
'bit-form' |
| 192 |
), |
| 193 |
400 |
| 194 |
); |
| 195 |
} |
| 196 |
$response = []; |
| 197 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 198 |
$response['tokenDetails'] = ZohoCampaignsHandler::_refreshAccessToken($queryParams); |
| 199 |
} |
| 200 |
|
| 201 |
$contactFieldsMetaApiEndpoint = "https://campaigns.zoho.{$queryParams->dataCenter}/api/v1.1/contact/allfields?type=json"; |
| 202 |
|
| 203 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 204 |
$contactFieldsMetaResponse = HttpHelper::get($contactFieldsMetaApiEndpoint, null, $authorizationHeader); |
| 205 |
|
| 206 |
if (!is_wp_error($contactFieldsMetaResponse)) { |
| 207 |
$allFields = []; |
| 208 |
$fields = $contactFieldsMetaResponse->response->fieldnames->fieldname; |
| 209 |
|
| 210 |
if (count($fields) > 0) { |
| 211 |
foreach ($fields as $field) { |
| 212 |
$allFields[] = $field->DISPLAY_NAME; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
usort($allFields, 'strnatcasecmp'); |
| 217 |
$response['fields'] = $allFields; |
| 218 |
|
| 219 |
$response['required'] = ['Contact Email']; |
| 220 |
} else { |
| 221 |
wp_send_json_error( |
| 222 |
'error' === $contactFieldsMetaResponse->status ? $contactFieldsMetaResponse->message : 'Unknown', |
| 223 |
400 |
| 224 |
); |
| 225 |
} |
| 226 |
if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { |
| 227 |
$response['queryModule'] = $queryParams->module; |
| 228 |
ZohoCampaignsHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); |
| 229 |
} |
| 230 |
wp_send_json_success($response, 200); |
| 231 |
} else { |
| 232 |
wp_send_json_error( |
| 233 |
__( |
| 234 |
'Token expired', |
| 235 |
'bit-form' |
| 236 |
), |
| 237 |
401 |
| 238 |
); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Helps to refresh zoho crm access_token |
| 244 |
* |
| 245 |
* @param object $apiData Contains required data for refresh access token |
| 246 |
* @return string | boolean $tokenDetails API token details |
| 247 |
*/ |
| 248 |
protected static function _refreshAccessToken($apiData) |
| 249 |
{ |
| 250 |
if ( |
| 251 |
empty($apiData->dataCenter) |
| 252 |
|| empty($apiData->clientId) |
| 253 |
|| empty($apiData->clientSecret) |
| 254 |
|| empty($apiData->tokenDetails) |
| 255 |
) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
$tokenDetails = $apiData->tokenDetails; |
| 259 |
|
| 260 |
$dataCenter = $apiData->dataCenter; |
| 261 |
$apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; |
| 262 |
$requestParams = [ |
| 263 |
'grant_type' => 'refresh_token', |
| 264 |
'client_id' => $apiData->clientId, |
| 265 |
'client_secret' => $apiData->clientSecret, |
| 266 |
'refresh_token' => $tokenDetails->refresh_token, |
| 267 |
]; |
| 268 |
|
| 269 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 270 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
$tokenDetails->generates_on = \time(); |
| 274 |
$tokenDetails->access_token = $apiResponse->access_token; |
| 275 |
return $tokenDetails; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Save updated access_token to avoid unnecessary token generation |
| 280 |
* |
| 281 |
* @param integer $fromID ID of Integration related form |
| 282 |
* @param integer $integrationID ID of Zoho crm Integration |
| 283 |
* @param object $tokenDetails refreshed token info |
| 284 |
* |
| 285 |
* @return null |
| 286 |
*/ |
| 287 |
protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) |
| 288 |
{ |
| 289 |
if (empty($formID) || empty($integrationID)) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 294 |
$zcampaignsDetails = $integrationHandler->getAIntegration($integrationID); |
| 295 |
|
| 296 |
if (is_wp_error($zcampaignsDetails)) { |
| 297 |
return; |
| 298 |
} |
| 299 |
$newDetails = json_decode($zcampaignsDetails[0]->integration_details); |
| 300 |
|
| 301 |
$newDetails->tokenDetails = $tokenDetails; |
| 302 |
if (!empty($others['lists'])) { |
| 303 |
$newDetails->default->lists = $others['lists']; |
| 304 |
} |
| 305 |
if (!empty($others['fieds'])) { |
| 306 |
$newDetails->default->fields = $others['fields']; |
| 307 |
} |
| 308 |
if (!empty($others['required'])) { |
| 309 |
$newDetails->default->required = $others['required']; |
| 310 |
} |
| 311 |
|
| 312 |
$integrationHandler->updateIntegration($integrationID, $zcampaignsDetails[0]->integration_name, 'Zoho Campaigns', wp_json_encode($newDetails), 'form'); |
| 313 |
} |
| 314 |
|
| 315 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 316 |
{ |
| 317 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 318 |
|
| 319 |
$tokenDetails = $integrationDetails->tokenDetails; |
| 320 |
$list = $integrationDetails->list; |
| 321 |
$dataCenter = $integrationDetails->dataCenter; |
| 322 |
$fieldMap = $integrationDetails->field_map; |
| 323 |
$required = $integrationDetails->default->fields->{$list}->required; |
| 324 |
if ( |
| 325 |
empty($tokenDetails) |
| 326 |
|| empty($list) |
| 327 |
|| empty($fieldMap) |
| 328 |
) { |
| 329 |
$error = new WP_Error('REQ_FIELD_EMPTY', __('list are required for zoho campaigns api', 'bit-form')); |
| 330 |
$this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error); |
| 331 |
return $error; |
| 332 |
} |
| 333 |
|
| 334 |
$requiredParams = null; |
| 335 |
if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 336 |
$requiredParams['clientId'] = $integrationDetails->clientId; |
| 337 |
$requiredParams['clientSecret'] = $integrationDetails->clientSecret; |
| 338 |
$requiredParams['dataCenter'] = $integrationDetails->dataCenter; |
| 339 |
$requiredParams['tokenDetails'] = $tokenDetails; |
| 340 |
$newTokenDetails = ZohoCampaignsHandler::_refreshAccessToken((object)$requiredParams); |
| 341 |
if ($newTokenDetails) { |
| 342 |
ZohoCampaignsHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); |
| 343 |
$tokenDetails = $newTokenDetails; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
// $actions = $integrationDetails->actions; |
| 348 |
$recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID); |
| 349 |
|
| 350 |
$zcampaignsApiResponse = $recordApiHelper->executeRecordApi( |
| 351 |
$list, |
| 352 |
$dataCenter, |
| 353 |
$fieldValues, |
| 354 |
$fieldMap, |
| 355 |
$required |
| 356 |
); |
| 357 |
|
| 358 |
if (is_wp_error($zcampaignsApiResponse)) { |
| 359 |
return $zcampaignsApiResponse; |
| 360 |
} |
| 361 |
return $zcampaignsApiResponse; |
| 362 |
} |
| 363 |
} |
| 364 |
|