PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 2.21.13, at includes/Core/Integration/Hubspot/HubspotHandler.php

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