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

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

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