| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoSign; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 12 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Record insert,upsert |
| 16 |
*/ |
| 17 |
class RecordApiHelper |
| 18 |
{ |
| 19 |
private $_defaultHeader; |
| 20 |
private $_tokenDetails; |
| 21 |
private $_logID; |
| 22 |
private $_integrationID; |
| 23 |
private $_logResponse; |
| 24 |
|
| 25 |
public function __construct($tokenDetails, $integId, $logID) |
| 26 |
{ |
| 27 |
$this->_tokenDetails = $tokenDetails; |
| 28 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 29 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 30 |
$this->_integrationID = $integId; |
| 31 |
$this->_logID = $logID; |
| 32 |
$this->_logResponse = new UtilApiResponse(); |
| 33 |
} |
| 34 |
|
| 35 |
public function insertRecord($dataCenter, $data) |
| 36 |
{ |
| 37 |
$insertRecordEndpoint = "https://sign.zoho.{$dataCenter}/api/accounts/{$this->_defaultHeader['accountId']}/messages"; |
| 38 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 39 |
} |
| 40 |
|
| 41 |
private function sendDocument($dataCenter, $template, $data) |
| 42 |
{ |
| 43 |
$sendDocumentEndpoint = "https://sign.zoho.{$dataCenter}/api/v1/templates/{$template}/createdocument?data={$data}"; |
| 44 |
return HttpHelper::post($sendDocumentEndpoint, null, $this->_defaultHeader); |
| 45 |
} |
| 46 |
|
| 47 |
public function executeRecordApi($dataCenter, $template, $templateActions, $notes, $fieldValues) |
| 48 |
{ |
| 49 |
$notes = FieldValueHandler::replaceFieldWithValue($notes, $fieldValues); |
| 50 |
|
| 51 |
foreach ($templateActions as $action) { |
| 52 |
if (!empty($action->in_person_email)) { |
| 53 |
$action->in_person_email = FieldValueHandler::replaceFieldWithValue($action->in_person_email, $fieldValues); |
| 54 |
} |
| 55 |
if (!empty($action->in_person_name)) { |
| 56 |
$action->in_person_name = FieldValueHandler::replaceFieldWithValue($action->in_person_name, $fieldValues); |
| 57 |
} |
| 58 |
if (!empty($action->recipient_email)) { |
| 59 |
$action->recipient_email = FieldValueHandler::replaceFieldWithValue($action->recipient_email, $fieldValues); |
| 60 |
} |
| 61 |
if (!empty($action->recipient_name)) { |
| 62 |
$action->recipient_name = FieldValueHandler::replaceFieldWithValue($action->recipient_name, $fieldValues); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$actions = wp_json_encode($templateActions); |
| 67 |
|
| 68 |
$data = '{"templates":{"notes":"' . $notes . '","field_data":{"field_text_data":{},"field_boolean_data":{},"field_date_data":{}},"actions":' . $actions . '}}'; |
| 69 |
|
| 70 |
$recordApiResponse = $this->sendDocument($dataCenter, $template, $data); |
| 71 |
if (isset($recordApiResponse->error_param) || 'failure' === $recordApiResponse->status || 'error' === $recordApiResponse->status) { |
| 72 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'send', 'type_name' => 'template'], 'error', $recordApiResponse); |
| 73 |
} else { |
| 74 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'send', 'type_name' => 'template'], 'success', $recordApiResponse); |
| 75 |
} |
| 76 |
|
| 77 |
return $recordApiResponse; |
| 78 |
} |
| 79 |
} |
| 80 |
|