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
V3.3.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 All 139 releases
bit-form / includes / Core / Integration / ZohoCampaigns / ZohoCampaignsHandler.php

ZohoCampaignsHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Integration/ZohoCampaigns/ZohoCampaignsHandler.php

355 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoCampaigns Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoCampaigns;
9
10 use BitCode\BitForm\Core\Integration\IntegrationHandler;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13 use BitCode\BitForm\Core\Util\IpTool;
14
15 /**
16 * Provide functionality for ZohoCrm integration
17 */
18 class ZohoCampaignsHandler {
19 private $_formID;
20 private $_integrationID;
21
22 private $_logResponse;
23
24 public function __construct($integrationID, $fromID) {
25 $this->_formID = $fromID;
26 $this->_integrationID = $integrationID;
27 $this->_logResponse = new UtilApiResponse();
28 }
29
30 /**
31 * Helps to register ajax function's with wp
32 *
33 * @return null
34 */
35 public static function registerAjax() {
36 add_action('wp_ajax_bitforms_zcampaigns_generate_token', [__CLASS__, 'generateTokens']);
37 add_action('wp_ajax_bitforms_zcampaigns_refresh_lists', [__CLASS__, 'refreshListsAjaxHelper']);
38 add_action('wp_ajax_bitforms_zcampaigns_refresh_contact_fields', [__CLASS__, 'refreshContactFieldsAjaxHelper']);
39 }
40
41 /**
42 * Process ajax request for generate_token
43 *
44 * @return JSON zoho crm api response and status
45 */
46 public static function generateTokens() {
47 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
48 $inputJSON = file_get_contents('php://input');
49 $requestsParams = json_decode($inputJSON);
50 if (
51 empty($requestsParams->{'accounts-server'})
52 || empty($requestsParams->dataCenter)
53 || empty($requestsParams->clientId)
54 || empty($requestsParams->clientSecret)
55 || empty($requestsParams->redirectURI)
56 || empty($requestsParams->code)
57 ) {
58 wp_send_json_error(
59 __(
60 'Requested parameter is empty',
61 'bit-form'
62 ),
63 400
64 );
65 }
66
67 $apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token';
68 $requestParams = [
69 'grant_type' => 'authorization_code',
70 'client_id' => $requestsParams->clientId,
71 'client_secret' => $requestsParams->clientSecret,
72 'redirect_uri' => \urldecode($requestsParams->redirectURI),
73 'code' => $requestsParams->code
74 ];
75 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
76
77 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
78 wp_send_json_error(
79 empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
80 400
81 );
82 }
83 $apiResponse->generates_on = \time();
84 wp_send_json_success($apiResponse, 200);
85 } else {
86 wp_send_json_error(
87 __(
88 'Token expired',
89 'bit-form'
90 ),
91 401
92 );
93 }
94 }
95
96 /**
97 * Process ajax request for refresh crm modules
98 *
99 * @return JSON crm module data
100 */
101 public static function refreshListsAjaxHelper() {
102 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
103 $authorizationHeader = null;
104 $inputJSON = file_get_contents('php://input');
105 $queryParams = json_decode($inputJSON);
106 if (
107 empty($queryParams->tokenDetails)
108 || empty($queryParams->dataCenter)
109 || empty($queryParams->clientId)
110 || empty($queryParams->clientSecret)
111 ) {
112 wp_send_json_error(
113 __(
114 'Requested parameter is empty',
115 'bit-form'
116 ),
117 400
118 );
119 }
120 $response = [];
121 if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) {
122 $response['tokenDetails'] = ZohoCampaignsHandler::_refreshAccessToken($queryParams);
123 }
124
125 $listsMetaApiEndpoint = "https://campaigns.zoho.{$queryParams->dataCenter}/api/v1.1/getmailinglists?resfmt=JSON&range=100";
126
127 $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}";
128 $listsMetaResponse = HttpHelper::get($listsMetaApiEndpoint, null, $authorizationHeader);
129
130 $allLists = [];
131 if (!is_wp_error($listsMetaResponse)) {
132 $lists = $listsMetaResponse->list_of_details;
133
134 if (count($lists) > 0) {
135 foreach ($lists as $list) {
136 $allLists[$list->listname] = (object) [
137 'listkey' => $list->listkey,
138 'listname' => $list->listname
139 ];
140 }
141 }
142 uksort($allLists, 'strnatcasecmp');
143 $response['lists'] = $allLists;
144 } else {
145 wp_send_json_error(
146 empty($listsMetaResponse->data) ? 'Unknown' : $listsMetaResponse->error,
147 400
148 );
149 }
150 if (!empty($response['tokenDetails']) && !empty($queryParams->id)) {
151 ZohoCampaignsHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['lists']);
152 }
153 wp_send_json_success($response, 200);
154 } else {
155 wp_send_json_error(
156 __(
157 'Token expired',
158 'bit-form'
159 ),
160 401
161 );
162 }
163 }
164
165 /**
166 * Process ajax request for refesh crm layouts
167 *
168 * @return JSON crm layout data
169 */
170 public static function refreshContactFieldsAjaxHelper() {
171 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
172 $authorizationHeader = null;
173 $inputJSON = file_get_contents('php://input');
174 $queryParams = json_decode($inputJSON);
175 if (
176 empty($queryParams->list)
177 || empty($queryParams->tokenDetails)
178 || empty($queryParams->dataCenter)
179 || empty($queryParams->clientId)
180 || empty($queryParams->clientSecret)
181 ) {
182 wp_send_json_error(
183 __(
184 'Requested parameter is empty',
185 'bit-form'
186 ),
187 400
188 );
189 }
190 $response = [];
191 if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) {
192 $response['tokenDetails'] = ZohoCampaignsHandler::_refreshAccessToken($queryParams);
193 }
194
195 $contactFieldsMetaApiEndpoint = "https://campaigns.zoho.{$queryParams->dataCenter}/api/v1.1/contact/allfields?type=json";
196
197 $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}";
198 $contactFieldsMetaResponse = HttpHelper::get($contactFieldsMetaApiEndpoint, null, $authorizationHeader);
199
200 if (!is_wp_error($contactFieldsMetaResponse)) {
201 $allFields = [];
202 $fields = $contactFieldsMetaResponse->response->fieldnames->fieldname;
203
204 if (count($fields) > 0) {
205 foreach ($fields as $field) {
206 $allFields[] = $field->DISPLAY_NAME;
207 }
208 }
209
210 usort($allFields, 'strnatcasecmp');
211 $response['fields'] = $allFields;
212
213 $response['required'] = ['Contact Email'];
214 } else {
215 wp_send_json_error(
216 'error' === $contactFieldsMetaResponse->status ? $contactFieldsMetaResponse->message : 'Unknown',
217 400
218 );
219 }
220 if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) {
221 $response['queryModule'] = $queryParams->module;
222 ZohoCampaignsHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response);
223 }
224 wp_send_json_success($response, 200);
225 } else {
226 wp_send_json_error(
227 __(
228 'Token expired',
229 'bit-form'
230 ),
231 401
232 );
233 }
234 }
235
236 /**
237 * Helps to refresh zoho crm access_token
238 *
239 * @param Array $apiData Contains required data for refresh access token
240 * @return JSON $tokenDetails API token details
241 */
242 protected static function _refreshAccessToken($apiData) {
243 if (
244 empty($apiData->dataCenter)
245 || empty($apiData->clientId)
246 || empty($apiData->clientSecret)
247 || empty($apiData->tokenDetails)
248 ) {
249 return false;
250 }
251 $tokenDetails = $apiData->tokenDetails;
252
253 $dataCenter = $apiData->dataCenter;
254 $apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token";
255 $requestParams = [
256 'grant_type' => 'refresh_token',
257 'client_id' => $apiData->clientId,
258 'client_secret' => $apiData->clientSecret,
259 'refresh_token' => $tokenDetails->refresh_token,
260 ];
261
262 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
263 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
264 return false;
265 }
266 $tokenDetails->generates_on = \time();
267 $tokenDetails->access_token = $apiResponse->access_token;
268 return $tokenDetails;
269 }
270
271 /**
272 * Save updated access_token to avoid unnecessary token generation
273 *
274 * @param Integer $fromID ID of Integration related form
275 * @param Integer $integrationID ID of Zoho crm Integration
276 * @param Obeject $tokenDetails refreshed token info
277 *
278 * @return null
279 */
280 protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) {
281 if (empty($formID) || empty($integrationID)) {
282 return;
283 }
284
285 $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail());
286 $zcampaignsDetails = $integrationHandler->getAIntegration($integrationID);
287
288 if (is_wp_error($zcampaignsDetails)) {
289 return;
290 }
291 $newDetails = json_decode($zcampaignsDetails[0]->integration_details);
292
293 $newDetails->tokenDetails = $tokenDetails;
294 if (!empty($others['lists'])) {
295 $newDetails->default->lists = $others['lists'];
296 }
297 if (!empty($others['fieds'])) {
298 $newDetails->default->fields = $others['fields'];
299 }
300 if (!empty($others['required'])) {
301 $newDetails->default->required = $others['required'];
302 }
303
304 $integrationHandler->updateIntegration($integrationID, $zcampaignsDetails[0]->integration_name, 'Zoho Campaigns', \json_encode($newDetails), 'form');
305 }
306
307 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) {
308 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
309
310 $tokenDetails = $integrationDetails->tokenDetails;
311 $list = $integrationDetails->list;
312 $dataCenter = $integrationDetails->dataCenter;
313 $fieldMap = $integrationDetails->field_map;
314 $required = $integrationDetails->default->fields->{$list}->required;
315 if (
316 empty($tokenDetails)
317 || empty($list)
318 || empty($fieldMap)
319 ) {
320 $error = new WP_Error('REQ_FIELD_EMPTY', __('list are required for zoho campaigns api', 'bit-form'));
321 $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error);
322 return $error;
323 }
324
325 $requiredParams = null;
326 if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) {
327 $requiredParams['clientId'] = $integrationDetails->clientId;
328 $requiredParams['clientSecret'] = $integrationDetails->clientSecret;
329 $requiredParams['dataCenter'] = $integrationDetails->dataCenter;
330 $requiredParams['tokenDetails'] = $tokenDetails;
331 $newTokenDetails = ZohoCampaignsHandler::_refreshAccessToken((object)$requiredParams);
332 if ($newTokenDetails) {
333 ZohoCampaignsHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails);
334 $tokenDetails = $newTokenDetails;
335 }
336 }
337
338 // $actions = $integrationDetails->actions;
339 $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID);
340
341 $zcampaignsApiResponse = $recordApiHelper->executeRecordApi(
342 $list,
343 $dataCenter,
344 $fieldValues,
345 $fieldMap,
346 $required
347 );
348
349 if (is_wp_error($zcampaignsApiResponse)) {
350 return $zcampaignsApiResponse;
351 }
352 return $zcampaignsApiResponse;
353 }
354 }
355