PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.2
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 / HubspotHandler.php

HubspotHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V3.0.2, at includes/Core/Integration/Hubspot/HubspotHandler.php

312 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\Hubspot;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 use BitCode\BitForm\Core\Integration\IntegrationHandler;
10 use BitCode\BitForm\Core\Util\HttpHelper;
11 use BitCode\BitForm\GlobalHelper;
12 use WP_Error;
13
14 final class HubspotHandler
15 {
16 private $_integrationID;
17 public static $apiBaseUri = 'https://apiv3.emailsys.net/v1';
18 protected $_defaultHeader;
19
20 private $formId;
21
22 public function __construct($integrationID, $fromID)
23 {
24 $this->_integrationID = $integrationID;
25 $this->formId = $fromID;
26 }
27
28 public static function registerAjax()
29 {
30 add_action('wp_ajax_bitforms_hubspot_authorize', [__CLASS__, 'hubspotAuthorize']);
31 add_action('wp_ajax_bitforms_hubspot_pipeline', [__CLASS__, 'getAllPipelines']);
32 add_action('wp_ajax_bitforms_hubspot_pipeline_tickets', [__CLASS__, 'getAllPipelinesTickets']);
33 add_action('wp_ajax_bitforms_hubspot_owners', [__CLASS__, 'getAllOwners']);
34 add_action('wp_ajax_bitforms_hubspot_contacts', [__CLASS__, 'getAllContacts']);
35 add_action('wp_ajax_bitforms_hubspot_company', [__CLASS__, 'getAllCompany']);
36 }
37
38 public static function hubspotAuthorize()
39 {
40 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
41 $authorizationHeader = null;
42
43 GlobalHelper::requirePostMethod();
44 try {
45 $requestsParams = GlobalHelper::formatRequestData();
46 } catch (\InvalidArgumentException $e) {
47 wp_send_json_error($e->getMessage(), 400);
48 }
49
50 if (empty($requestsParams->api_key)) {
51 wp_send_json_error(
52 __(
53 'Requested parameter is empty',
54 'bit-form'
55 ),
56 400
57 );
58 }
59 $apiKey = $requestsParams->api_key;
60 $apiEndpoint = 'https://api.hubapi.com/crm/v3/objects/contacts?limit=10&archived=false';
61 $authorizationHeader['Accept'] = 'application/json';
62 $authorizationHeader['authorization'] = "Bearer $apiKey";
63
64 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
65
66 if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) {
67 wp_send_json_error(
68 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
69 400
70 );
71 }
72 wp_send_json_success(true);
73 }
74 }
75
76 public static function getAllPipelines()
77 {
78 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
79 $authorizationHeader = null;
80
81 GlobalHelper::requirePostMethod();
82
83 try {
84 $requestsParams = GlobalHelper::formatRequestData();
85 } catch (\InvalidArgumentException $e) {
86 wp_send_json_error($e->getMessage(), 400);
87 }
88
89 if (empty($requestsParams->apiKey)) {
90 wp_send_json_error(
91 __(
92 'Requested parameter is empty',
93 'bit-form'
94 ),
95 400
96 );
97 }
98 $apiKey = $requestsParams->apiKey;
99 $type = $requestsParams->actionName;
100 $apiEndpoint = "https://api.hubapi.com/crm/v3/pipelines/$type";
101 $authorizationHeader['Accept'] = 'application/json';
102 $authorizationHeader['authorization'] = "Bearer {$apiKey}";
103
104 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
105
106 if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) {
107 wp_send_json_error(
108 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
109 400
110 );
111 }
112 $pipelines = $apiResponse->results;
113 $response = [];
114
115 foreach ($pipelines as $pipeline) {
116 $tempStage = [];
117 foreach ($pipeline->stages as $stage) {
118 $tempStage[] = (object) [
119 'stageId' => $stage->id,
120 'stageName' => $stage->label,
121 ];
122 }
123 $response[] = (object) [
124 'pipelineId' => $pipeline->id,
125 'pipelineName' => $pipeline->label,
126 'stages' => $tempStage,
127 ];
128 }
129 wp_send_json_success($response, 200);
130 }
131 }
132
133 public static function getAllOwners()
134 {
135 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
136 $authorizationHeader = null;
137
138 GlobalHelper::requirePostMethod();
139
140 try {
141 $requestsParams = GlobalHelper::formatRequestData();
142 } catch (\InvalidArgumentException $e) {
143 wp_send_json_error($e->getMessage(), 400);
144 }
145
146 if (empty($requestsParams->apiKey)) {
147 wp_send_json_error(
148 __(
149 'Requested parameter is empty',
150 'bit-form'
151 ),
152 400
153 );
154 }
155 $apiKey = $requestsParams->apiKey;
156 // $apiEndpoint = "https://api.hubapi.com/owners/v2/owners?hapikey={$apiKey}";
157 $apiEndpoint = 'https://api.hubapi.com/crm/v3/owners/?limit=100&archived=false';
158
159 $authorizationHeader['Accept'] = 'application/json';
160 $authorizationHeader['authorization'] = "Bearer {$apiKey}";
161
162 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
163 if (is_wp_error($apiResponse) || (isset($apiResponse->status) && 'error' === $apiResponse->status)) {
164 wp_send_json_error(
165 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
166 400
167 );
168 }
169 $response = [];
170 foreach ($apiResponse->results as $owner) {
171 $response[] = (object) [
172 'ownerId' => $owner->id,
173 'ownerName' => $owner->firstName . ' ' . $owner->lastName,
174 ];
175 }
176 wp_send_json_success($response, 200);
177 }
178 }
179
180 public static function getAllContacts()
181 {
182 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
183 $authorizationHeader = null;
184
185 GlobalHelper::requirePostMethod();
186
187 try {
188 $requestsParams = GlobalHelper::formatRequestData();
189 } catch (\InvalidArgumentException $e) {
190 wp_send_json_error($e->getMessage(), 400);
191 }
192
193 if (empty($requestsParams->apiKey)) {
194 wp_send_json_error(
195 __(
196 'Requested parameter is empty',
197 'bit-form'
198 ),
199 400
200 );
201 }
202 $apiKey = $requestsParams->apiKey;
203 $apiEndpoint = 'https://api.hubapi.com/crm/v3/objects/contacts';
204 $authorizationHeader['Accept'] = 'application/json';
205 $authorizationHeader['authorization'] = "Bearer {$apiKey}";
206
207 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
208 if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) {
209 wp_send_json_error(
210 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
211 400
212 );
213 }
214 $response = [];
215 $contacts = $apiResponse->results;
216
217 foreach ($contacts as $contact) {
218 $name = $contact->properties->firstname;
219 $response[] = (object) [
220 'contactId' => $contact->id,
221 'contactName' => $name,
222 ];
223 }
224 wp_send_json_success($response, 200);
225 }
226 }
227
228 public static function getAllCompany()
229 {
230 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
231 $authorizationHeader = null;
232
233 GlobalHelper::requirePostMethod();
234
235 try {
236 $requestsParams = GlobalHelper::formatRequestData();
237 } catch (\InvalidArgumentException $e) {
238 wp_send_json_error($e->getMessage(), 400);
239 }
240 if (empty($requestsParams->apiKey)) {
241 wp_send_json_error(
242 __(
243 'Requested parameter is empty',
244 'bit-form'
245 ),
246 400
247 );
248 }
249 $apiKey = $requestsParams->apiKey;
250
251 $apiEndpoint = 'https://api.hubapi.com/companies/v2/companies/';
252 $authorizationHeader['Accept'] = 'application/json';
253 $authorizationHeader['authorization'] = "Bearer {$apiKey}";
254
255 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
256 if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) {
257 wp_send_json_error(
258 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
259 400
260 );
261 }
262 $companies = $apiResponse->companies;
263 $response = [];
264
265 foreach ($companies as $company) {
266 if (property_exists($company->properties, 'name')) {
267 $name = $company->properties->name->value;
268 $response[] = (object) [
269 'companyId' => $company->companyId,
270 'companyName' => $name,
271 ];
272 }
273 }
274 wp_send_json_success($response, 200);
275 }
276 }
277
278 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
279 {
280 $integrationDetails = $integrationData->integration_details;
281 if (is_string($integrationDetails)) {
282 $integrationDetails = json_decode($integrationDetails);
283 }
284
285 $fieldMap = $integrationDetails->field_map;
286 $apiKey = $integrationDetails->api_key;
287 $integId = $integrationData->id;
288
289 if (
290 empty($apiKey)
291 || empty($fieldMap)
292 ) {
293 $error = new WP_Error('REQ_FIELD_EMPTY', __('api key field is required for hubspot api', 'bit-form'));
294 return $error;
295 }
296
297 // $actions = $integrationDetails->actions;
298 $recordApiHelper = new HubspotRecordApiHelper($logID, $apiKey, $entryID);
299 $hubspotResponse = $recordApiHelper->executeRecordApi(
300 $integId,
301 $integrationDetails,
302 $fieldValues,
303 $fieldMap,
304 $this->formId
305 );
306 if (is_wp_error($hubspotResponse)) {
307 return $hubspotResponse;
308 }
309 return $hubspotResponse;
310 }
311 }
312