| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoSign Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoSign; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use BitCode\BitForm\Core\Util\IpTool; |
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
/** |
| 16 |
* Provide functionality for ZohoCrm integration |
| 17 |
*/ |
| 18 |
class ZohoSignHandler |
| 19 |
{ |
| 20 |
private $_formID; |
| 21 |
private $_integrationID; |
| 22 |
|
| 23 |
public function __construct($integrationID, $fromID) |
| 24 |
{ |
| 25 |
$this->_formID = $fromID; |
| 26 |
$this->_integrationID = $integrationID; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Helps to register ajax function's with wp |
| 31 |
* |
| 32 |
* @return null |
| 33 |
*/ |
| 34 |
public static function registerAjax() |
| 35 |
{ |
| 36 |
add_action('wp_ajax_bitforms_zsign_generate_token', [__CLASS__, 'generateTokens']); |
| 37 |
add_action('wp_ajax_bitforms_zsign_refresh_templates', [__CLASS__, 'refreshTemplatesAjaxHelper']); |
| 38 |
add_action('wp_ajax_bitforms_zsign_refresh_template_details', [__CLASS__, 'refreshTemplateDetailsAjaxHelper']); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Process ajax request for generate_token |
| 43 |
* |
| 44 |
* @return JSON zoho crm api response and status |
| 45 |
*/ |
| 46 |
public static function generateTokens() |
| 47 |
{ |
| 48 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 49 |
$inputJSON = file_get_contents('php://input'); |
| 50 |
$requestsParams = json_decode($inputJSON); |
| 51 |
if ( |
| 52 |
empty($requestsParams->{'accounts-server'}) |
| 53 |
|| empty($requestsParams->dataCenter) |
| 54 |
|| empty($requestsParams->clientId) |
| 55 |
|| empty($requestsParams->clientSecret) |
| 56 |
|| empty($requestsParams->redirectURI) |
| 57 |
|| empty($requestsParams->code) |
| 58 |
) { |
| 59 |
wp_send_json_error( |
| 60 |
__( |
| 61 |
'Requested parameter is empty', |
| 62 |
'bit-form' |
| 63 |
), |
| 64 |
400 |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
$apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; |
| 69 |
$requestParams = [ |
| 70 |
'grant_type' => 'authorization_code', |
| 71 |
'client_id' => $requestsParams->clientId, |
| 72 |
'client_secret' => $requestsParams->clientSecret, |
| 73 |
'redirect_uri' => \urldecode($requestsParams->redirectURI), |
| 74 |
'code' => $requestsParams->code |
| 75 |
]; |
| 76 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 77 |
|
| 78 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 79 |
wp_send_json_error( |
| 80 |
empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 81 |
400 |
| 82 |
); |
| 83 |
} |
| 84 |
$apiResponse->generates_on = \time(); |
| 85 |
wp_send_json_success($apiResponse, 200); |
| 86 |
} else { |
| 87 |
wp_send_json_error( |
| 88 |
__( |
| 89 |
'Token expired', |
| 90 |
'bit-form' |
| 91 |
), |
| 92 |
401 |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Helps to refresh zoho crm access_token |
| 99 |
* |
| 100 |
* @param Array $apiData Contains required data for refresh access token |
| 101 |
* @return JSON $tokenDetails API token details |
| 102 |
*/ |
| 103 |
protected static function _refreshAccessToken($apiData) |
| 104 |
{ |
| 105 |
if ( |
| 106 |
empty($apiData->dataCenter) |
| 107 |
|| empty($apiData->clientId) |
| 108 |
|| empty($apiData->clientSecret) |
| 109 |
|| empty($apiData->tokenDetails) |
| 110 |
) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
$tokenDetails = $apiData->tokenDetails; |
| 114 |
|
| 115 |
$dataCenter = $apiData->dataCenter; |
| 116 |
$apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; |
| 117 |
$requestParams = [ |
| 118 |
'grant_type' => 'refresh_token', |
| 119 |
'client_id' => $apiData->clientId, |
| 120 |
'client_secret' => $apiData->clientSecret, |
| 121 |
'refresh_token' => $tokenDetails->refresh_token, |
| 122 |
]; |
| 123 |
|
| 124 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 125 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
$tokenDetails->generates_on = \time(); |
| 129 |
$tokenDetails->access_token = $apiResponse->access_token; |
| 130 |
return $tokenDetails; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Save updated access_token to avoid unnecessary token generation |
| 135 |
* |
| 136 |
* @param Integer $fromID ID of Integration related form |
| 137 |
* @param Integer $integrationID ID of Zoho crm Integration |
| 138 |
* @param Obeject $tokenDetails refreshed token info |
| 139 |
* |
| 140 |
* @return null |
| 141 |
*/ |
| 142 |
protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) |
| 143 |
{ |
| 144 |
if (empty($formID) || empty($integrationID)) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 149 |
$zsignDetails = $integrationHandler->getAIntegration($integrationID); |
| 150 |
|
| 151 |
if (is_wp_error($zsignDetails)) { |
| 152 |
return; |
| 153 |
} |
| 154 |
$newDetails = json_decode($zsignDetails[0]->integration_details); |
| 155 |
|
| 156 |
$newDetails->tokenDetails = $tokenDetails; |
| 157 |
|
| 158 |
$integrationHandler->updateIntegration($integrationID, $zsignDetails[0]->integration_name, 'Zoho Sign', \json_encode($newDetails), 'form'); |
| 159 |
} |
| 160 |
|
| 161 |
public function refreshTemplatesAjaxHelper() |
| 162 |
{ |
| 163 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 164 |
$authorizationHeader = null; |
| 165 |
$inputJSON = file_get_contents('php://input'); |
| 166 |
$queryParams = json_decode($inputJSON); |
| 167 |
if ( |
| 168 |
empty($queryParams->tokenDetails) |
| 169 |
|| empty($queryParams->dataCenter) |
| 170 |
|| empty($queryParams->clientId) |
| 171 |
|| empty($queryParams->clientSecret) |
| 172 |
) { |
| 173 |
wp_send_json_error( |
| 174 |
__( |
| 175 |
'Requested parameter is empty', |
| 176 |
'bit-form' |
| 177 |
), |
| 178 |
400 |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
$response = []; |
| 183 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 184 |
$response['tokenDetails'] = ZohoSignHandler::_refreshAccessToken($queryParams); |
| 185 |
} |
| 186 |
$templatesMetaApiEndpoint = "https://sign.zoho.{$queryParams->dataCenter}/api/v1/templates"; |
| 187 |
|
| 188 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 189 |
$templatesMetaResponse = HttpHelper::get($templatesMetaApiEndpoint, null, $authorizationHeader); |
| 190 |
|
| 191 |
$allTemplates = []; |
| 192 |
if (!is_wp_error($templatesMetaResponse) && empty($templatesMetaResponse->response->error)) { |
| 193 |
$templates = $templatesMetaResponse->templates; |
| 194 |
foreach ($templates as $template) { |
| 195 |
$allTemplates[$template->template_name] = (object) [ |
| 196 |
'templateId' => $template->template_id, |
| 197 |
'templateName' => $template->template_name |
| 198 |
]; |
| 199 |
} |
| 200 |
uksort($allTemplates, 'strnatcasecmp'); |
| 201 |
$response['templates'] = $allTemplates; |
| 202 |
} else { |
| 203 |
wp_send_json_error( |
| 204 |
$templatesMetaResponse->response->error->message, |
| 205 |
400 |
| 206 |
); |
| 207 |
} |
| 208 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 209 |
ZohoSignHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['templates']); |
| 210 |
} |
| 211 |
wp_send_json_success($response, 200); |
| 212 |
} else { |
| 213 |
wp_send_json_error( |
| 214 |
__( |
| 215 |
'Token expired', |
| 216 |
'bit-form' |
| 217 |
), |
| 218 |
401 |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
public function refreshTemplateDetailsAjaxHelper() |
| 224 |
{ |
| 225 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 226 |
$authorizationHeader = null; |
| 227 |
$inputJSON = file_get_contents('php://input'); |
| 228 |
$queryParams = json_decode($inputJSON); |
| 229 |
if ( |
| 230 |
empty($queryParams->tokenDetails) |
| 231 |
|| empty($queryParams->dataCenter) |
| 232 |
|| empty($queryParams->clientId) |
| 233 |
|| empty($queryParams->clientSecret) |
| 234 |
|| empty($queryParams->template) |
| 235 |
) { |
| 236 |
wp_send_json_error( |
| 237 |
__( |
| 238 |
'Requested parameter is empty', |
| 239 |
'bit-form' |
| 240 |
), |
| 241 |
400 |
| 242 |
); |
| 243 |
} |
| 244 |
$response = []; |
| 245 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 246 |
$response['tokenDetails'] = ZohoSignHandler::_refreshAccessToken($queryParams); |
| 247 |
} |
| 248 |
|
| 249 |
$templateDetailsMetaApiEndpoint = "https://sign.zoho.{$queryParams->dataCenter}/api/v1/templates/{$queryParams->template}"; |
| 250 |
|
| 251 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 252 |
$templateDetailsMetaResponse = HttpHelper::get($templateDetailsMetaApiEndpoint, null, $authorizationHeader); |
| 253 |
|
| 254 |
// wp_send_json_success($templateDetailsMetaResponse, 200); |
| 255 |
|
| 256 |
$details = []; |
| 257 |
if (!is_wp_error($templateDetailsMetaResponse) && empty($templateDetailsMetaResponse->response->error)) { |
| 258 |
$templateDetails = $templateDetailsMetaResponse->templates; |
| 259 |
if (!$templateDetails->is_deleted) { |
| 260 |
$details['actions'] = $templateDetails->actions; |
| 261 |
$details['notes'] = $templateDetails->notes; |
| 262 |
} |
| 263 |
$response['templateDetails'] = $details; |
| 264 |
} else { |
| 265 |
wp_send_json_error( |
| 266 |
$templateDetailsMetaResponse->response->error->message, |
| 267 |
400 |
| 268 |
); |
| 269 |
} |
| 270 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 271 |
ZohoSignHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['templateDetails']); |
| 272 |
} |
| 273 |
wp_send_json_success($response, 200); |
| 274 |
} else { |
| 275 |
wp_send_json_error( |
| 276 |
__( |
| 277 |
'Token expired', |
| 278 |
'bit-form' |
| 279 |
), |
| 280 |
401 |
| 281 |
); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 286 |
{ |
| 287 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 288 |
$templateActions = $integrationDetails->templateActions; |
| 289 |
$notes = $integrationDetails->notes; |
| 290 |
$dataCenter = $integrationDetails->dataCenter; |
| 291 |
$template = $integrationDetails->template; |
| 292 |
$tokenDetails = $integrationDetails->tokenDetails; |
| 293 |
if (empty($tokenDetails)) { |
| 294 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho sign api', 'bit-form')); |
| 295 |
} |
| 296 |
|
| 297 |
$requiredParams = null; |
| 298 |
if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 299 |
$requiredParams['clientId'] = $integrationDetails->clientId; |
| 300 |
$requiredParams['clientSecret'] = $integrationDetails->clientSecret; |
| 301 |
$requiredParams['dataCenter'] = $integrationDetails->dataCenter; |
| 302 |
$requiredParams['tokenDetails'] = $tokenDetails; |
| 303 |
$newTokenDetails = ZohoSignHandler::_refreshAccessToken((object)$requiredParams); |
| 304 |
if ($newTokenDetails) { |
| 305 |
ZohoSignHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); |
| 306 |
$tokenDetails = $newTokenDetails; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// $actions = $integrationDetails->actions; |
| 311 |
$recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID); |
| 312 |
|
| 313 |
$zsignApiResponse = $recordApiHelper->executeRecordApi( |
| 314 |
$dataCenter, |
| 315 |
$template, |
| 316 |
$templateActions, |
| 317 |
$notes, |
| 318 |
$fieldValues |
| 319 |
); |
| 320 |
|
| 321 |
return $zsignApiResponse; |
| 322 |
} |
| 323 |
} |
| 324 |
|