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

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