PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / -3.0.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v-3.0.1
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / Hubspot / HubspotRecordApiHelper.php

HubspotRecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder -3.0.1, at includes/Core/Integration/Hubspot/HubspotRecordApiHelper.php

160 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 private $entryId;
25
26 public function __construct($logID, $apiKey, $entryId)
27 {
28 $this->_logResponse = new ApiResponse();
29 $this->logID = $logID;
30
31 $this->_header = [
32 'Content-Type' => 'application/json',
33 'authorization' => "Bearer {$apiKey}",
34 ];
35
36 $this->entryId = $entryId;
37 }
38
39 public function insertContact($data)
40 {
41 $organizedData = wp_json_encode([
42 'properties' => $data,
43 ]);
44 $contactsEndPoint = "{$this->_apiBaseUrl}/contacts";
45 $response = HttpHelper::post($contactsEndPoint, $organizedData, $this->_header);
46 return $response;
47 }
48
49 public function insertDeal($finalData)
50 {
51 $data = wp_json_encode($finalData);
52 $dealsEndPoint = "{$this->_apiBaseUrl}/deals";
53 $response = HttpHelper::post($dealsEndPoint, $data, $this->_header);
54 return $response;
55 }
56
57 public function insertTicket($finalData)
58 {
59 $organizedData = wp_json_encode([
60 'properties' => $finalData,
61 ]);
62 $ticketsEndPoint = "{$this->_apiBaseUrl}/tickets";
63 $response = HttpHelper::post($ticketsEndPoint, $organizedData, $this->_header);
64 return $response;
65 }
66
67 public function generateReqDataFromField($data, $fieldMap, $integrationDetails)
68 {
69 $dataFinal = [];
70
71 $dataFinal = Common::formFldMapping($dataFinal, $fieldMap, $data);
72
73 $action = $integrationDetails->actions;
74 $dataFinal = Common::customFldMapping('lead', $action, $integrationDetails, $dataFinal);
75
76 return $dataFinal;
77 }
78
79 public function formatDealField($data, $fieldMap, $integrationDetails)
80 {
81 $dataFinal = [];
82 $dataFinal = Common::formFldMapping($dataFinal, $fieldMap, $data, 'deal');
83
84 $pipeline = $integrationDetails->pipeline;
85 $stage = $integrationDetails->stage;
86 $dataFinal['pipeline'] = $pipeline;
87 $dataFinal['dealstage'] = $stage;
88 $action = $integrationDetails->actions;
89 $dataForAssosciations = [];
90 if (!empty($action->action)) {
91 $dataFinal = Common::customFldMapping('deal', $action, $integrationDetails, $dataFinal);
92 $dataForAssosciations = Common::customFldMapping('company', $action, $integrationDetails, $dataForAssosciations);
93 }
94
95 $finalData = [];
96 $finalData['properties'] = $dataFinal;
97 $finalData['associations'] = (object) $dataForAssosciations;
98 return $finalData;
99 }
100
101 public function formatTicketField($data, $fieldMap, $integrationDetails)
102 {
103 $dataFinal = [];
104 $dataFinal = Common::formFldMapping($dataFinal, $fieldMap, $data);
105 $pipeline = $integrationDetails->pipeline;
106 $stage = $integrationDetails->stage;
107 $dataFinal['hs_pipeline'] = $pipeline;
108 $dataFinal['hs_pipeline_stage'] = $stage;
109
110 $action = $integrationDetails->actions;
111
112 if (property_exists($action, 'contact_owner')) {
113 $owner = $integrationDetails->contact_owner;
114 $dataFinal['hubspot_owner_id'] = $owner;
115 }
116 if (property_exists($action, 'priority')) {
117 $priority = $integrationDetails->priority;
118 $dataFinal['hs_ticket_priority'] = $priority ? strtoupper($priority) : 'HIGH';
119 }
120
121 return $dataFinal;
122 }
123
124 public function executeRecordApi($integId, $integrationDetails, $fieldValues, $fieldMap, $formId)
125 {
126 $actionName = $integrationDetails->actionName;
127 $type = '';
128 $typeName = '';
129 if ('contact-create' === $actionName) {
130 $finalData = $this->generateReqDataFromField($fieldValues, $fieldMap, $integrationDetails);
131 $apiResponse = $this->insertContact($finalData);
132 $type = 'contact';
133 $typeName = 'contact-add';
134 } elseif ('deal-create' === $actionName) {
135 $finalData = $this->formatDealField($fieldValues, $fieldMap, $integrationDetails);
136 $apiResponse = $this->insertDeal($finalData);
137 $type = 'deal';
138 $typeName = 'deal-add';
139 } elseif ('ticket-create' === $actionName) {
140 $finalData = $this->formatTicketField($fieldValues, $fieldMap, $integrationDetails);
141 $apiResponse = $this->insertTicket($finalData);
142 $type = 'ticket';
143 $typeName = 'ticket-add';
144 }
145
146 $entryDetails = [
147 'form_id' => $formId,
148 'entry_id' => $this->entryId,
149 'fieldValues' => $fieldValues,
150 ];
151
152 if (!isset($apiResponse->properties)) {
153 $this->_logResponse->apiResponse($this->logID, $integId, ['type' => $type, 'type_name' => $typeName], 'errors', $apiResponse, $entryDetails);
154 } else {
155 $this->_logResponse->apiResponse($this->logID, $integId, ['type' => $type, 'type_name' => $typeName], 'success', wp_json_encode($apiResponse), $entryDetails);
156 }
157 return $apiResponse;
158 }
159 }
160