| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoCrm Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Hubspot; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provide functionality for Record insert,upsert |
| 15 |
*/ |
| 16 |
class HubspotRecordApiHelper |
| 17 |
{ |
| 18 |
public $_header = []; |
| 19 |
public $_apiBaseUrl = 'https://api.hubapi.com/crm/v3/objects'; |
| 20 |
|
| 21 |
private $_logResponse; |
| 22 |
private $logID; |
| 23 |
|
| 24 |
public function __construct($logID, $apiKey) |
| 25 |
{ |
| 26 |
$this->_logResponse = new ApiResponse(); |
| 27 |
$this->logID = $logID; |
| 28 |
|
| 29 |
$this->_header = [ |
| 30 |
'Content-Type' => 'application/json', |
| 31 |
'authorization' => "Bearer {$apiKey}", |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
public function insertContact($data) |
| 36 |
{ |
| 37 |
$organizedData = json_encode([ |
| 38 |
'properties' => $data, |
| 39 |
]); |
| 40 |
$contactsEndPoint = "{$this->_apiBaseUrl}/contacts"; |
| 41 |
$response = HttpHelper::post($contactsEndPoint, $organizedData, $this->_header); |
| 42 |
return $response; |
| 43 |
} |
| 44 |
|
| 45 |
public function insertDeal($finalData) |
| 46 |
{ |
| 47 |
$data = json_encode($finalData); |
| 48 |
$dealsEndPoint = "{$this->_apiBaseUrl}/deals"; |
| 49 |
$response = HttpHelper::post($dealsEndPoint, $data, $this->_header); |
| 50 |
return $response; |
| 51 |
} |
| 52 |
|
| 53 |
public function insertTicket($finalData) |
| 54 |
{ |
| 55 |
$organizedData = json_encode([ |
| 56 |
'properties' => $finalData, |
| 57 |
]); |
| 58 |
$ticketsEndPoint = "{$this->_apiBaseUrl}/tickets"; |
| 59 |
$response = HttpHelper::post($ticketsEndPoint, $organizedData, $this->_header); |
| 60 |
return $response; |
| 61 |
} |
| 62 |
|
| 63 |
public function generateReqDataFromField($data, $fieldMap, $integrationDetails) |
| 64 |
{ |
| 65 |
$dataFinal = []; |
| 66 |
|
| 67 |
$dataFinal = Common::formFldMapping($dataFinal, $fieldMap, $data); |
| 68 |
|
| 69 |
$action = $integrationDetails->actions; |
| 70 |
$dataFinal = Common::customFldMapping('lead', $action, $integrationDetails, $dataFinal); |
| 71 |
|
| 72 |
return $dataFinal; |
| 73 |
} |
| 74 |
|
| 75 |
public function formatDealField($data, $fieldMap, $integrationDetails) |
| 76 |
{ |
| 77 |
$dataFinal = []; |
| 78 |
$dataFinal = Common::formFldMapping($dataFinal, $fieldMap, $data, 'deal'); |
| 79 |
|
| 80 |
$pipeline = $integrationDetails->pipeline; |
| 81 |
$stage = $integrationDetails->stage; |
| 82 |
$dataFinal['pipeline'] = $pipeline; |
| 83 |
$dataFinal['dealstage'] = $stage; |
| 84 |
$action = $integrationDetails->actions; |
| 85 |
$dataForAssosciations = []; |
| 86 |
if (!empty($action->action)) { |
| 87 |
$dataFinal = Common::customFldMapping('deal', $action, $integrationDetails, $dataFinal); |
| 88 |
$dataForAssosciations = Common::customFldMapping('company', $action, $integrationDetails, $dataForAssosciations); |
| 89 |
} |
| 90 |
|
| 91 |
$finalData = []; |
| 92 |
$finalData['properties'] = $dataFinal; |
| 93 |
$finalData['associations'] = (object) $dataForAssosciations; |
| 94 |
return $finalData; |
| 95 |
} |
| 96 |
|
| 97 |
public function formatTicketField($data, $fieldMap, $integrationDetails) |
| 98 |
{ |
| 99 |
$dataFinal = []; |
| 100 |
$dataFinal = Common::formFldMapping($dataFinal, $fieldMap, $data); |
| 101 |
$pipeline = $integrationDetails->pipeline; |
| 102 |
$stage = $integrationDetails->stage; |
| 103 |
$dataFinal['hs_pipeline'] = $pipeline; |
| 104 |
$dataFinal['hs_pipeline_stage'] = $stage; |
| 105 |
|
| 106 |
$action = $integrationDetails->actions; |
| 107 |
|
| 108 |
if (property_exists($action, 'contact_owner')) { |
| 109 |
$owner = $integrationDetails->contact_owner; |
| 110 |
$dataFinal['hubspot_owner_id'] = $owner; |
| 111 |
} |
| 112 |
if (property_exists($action, 'priority')) { |
| 113 |
$priority = $integrationDetails->priority; |
| 114 |
$dataFinal['hs_ticket_priority'] = $priority ? strtoupper($priority) : 'HIGH'; |
| 115 |
} |
| 116 |
|
| 117 |
return $dataFinal; |
| 118 |
} |
| 119 |
|
| 120 |
public function executeRecordApi($integId, $integrationDetails, $fieldValues, $fieldMap) |
| 121 |
{ |
| 122 |
$actionName = $integrationDetails->actionName; |
| 123 |
$type = ''; |
| 124 |
$typeName = ''; |
| 125 |
if ('contact-create' === $actionName) { |
| 126 |
$finalData = $this->generateReqDataFromField($fieldValues, $fieldMap, $integrationDetails); |
| 127 |
$apiResponse = $this->insertContact($finalData); |
| 128 |
$type = 'contact'; |
| 129 |
$typeName = 'contact-add'; |
| 130 |
} elseif ('deal-create' === $actionName) { |
| 131 |
$finalData = $this->formatDealField($fieldValues, $fieldMap, $integrationDetails); |
| 132 |
$apiResponse = $this->insertDeal($finalData); |
| 133 |
$type = 'deal'; |
| 134 |
$typeName = 'deal-add'; |
| 135 |
} elseif ('ticket-create' === $actionName) { |
| 136 |
$finalData = $this->formatTicketField($fieldValues, $fieldMap, $integrationDetails); |
| 137 |
$apiResponse = $this->insertTicket($finalData); |
| 138 |
$type = 'ticket'; |
| 139 |
$typeName = 'ticket-add'; |
| 140 |
} |
| 141 |
|
| 142 |
if (!isset($apiResponse->properties)) { |
| 143 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => $type, 'type_name' => $typeName], 'errors', $apiResponse); |
| 144 |
} else { |
| 145 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => $type, 'type_name' => $typeName], 'success', wp_json_encode($apiResponse)); |
| 146 |
} |
| 147 |
return $apiResponse; |
| 148 |
} |
| 149 |
} |
| 150 |
|