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

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