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 / ZohoMail / ZohoMailHandler.php

ZohoMailHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.9.1, at includes/Core/Integration/ZohoMail/ZohoMailHandler.php

205 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoMail Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoMail;
9
10 use BitCode\BitForm\Core\Integration\IntegrationHandler;
11 use BitCode\BitForm\Core\Util\HttpHelper;
12 use BitCode\BitForm\Core\Util\IpTool;
13 use WP_Error;
14
15 /**
16 * Provide functionality for ZohoCrm integration
17 */
18 class ZohoMailHandler
19 {
20 private $_formID;
21 private $_integrationID;
22
23 public function __construct($integrationID, $fromID)
24 {
25 $this->_formID = $fromID;
26 $this->_integrationID = $integrationID;
27 }
28
29 /**
30 * Helps to register ajax function's with wp
31 *
32 * @return null
33 */
34 public static function registerAjax()
35 {
36 add_action('wp_ajax_bitforms_zmail_generate_token', [__CLASS__, 'generateTokens']);
37 add_action('wp_ajax_bitforms_zmail_refresh_workspaces', [__CLASS__, 'refreshWorkspacesAjaxHelper']);
38 add_action('wp_ajax_bitforms_zmail_refresh_tables', [__CLASS__, 'refreshTablesAjaxHelper']);
39 add_action('wp_ajax_bitforms_zmail_refresh_table_headers', [__CLASS__, 'refreshTableHeadersAjaxHelper']);
40 }
41
42 /**
43 * Process ajax request for generate_token
44 *
45 * @return JSON zoho crm api response and status
46 */
47 public static function generateTokens()
48 {
49 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
50 $authorizationHeader = null;
51 $inputJSON = file_get_contents('php://input');
52 $requestsParams = json_decode($inputJSON);
53 if (
54 empty($requestsParams->{'accounts-server'})
55 || empty($requestsParams->dataCenter)
56 || empty($requestsParams->clientId)
57 || empty($requestsParams->clientSecret)
58 || empty($requestsParams->redirectURI)
59 || empty($requestsParams->code)
60 ) {
61 wp_send_json_error(
62 __(
63 'Requested parameter is empty',
64 'bit-form'
65 ),
66 400
67 );
68 }
69
70 $apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token';
71 $requestParams = [
72 'grant_type' => 'authorization_code',
73 'client_id' => $requestsParams->clientId,
74 'client_secret' => $requestsParams->clientSecret,
75 'redirect_uri' => \urldecode($requestsParams->redirectURI),
76 'code' => $requestsParams->code
77 ];
78 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
79
80 $accountIdEndpoint = "http://mail.zoho.{$requestsParams->dataCenter}/api/accounts";
81 $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$apiResponse->access_token}";
82 $accountResponse = HttpHelper::get($accountIdEndpoint, null, $authorizationHeader);
83
84 $apiResponse->accountId = $accountResponse->data[0]->accountId;
85 $apiResponse->accountEmail = $accountResponse->data[0]->primaryEmailAddress;
86
87 if (is_wp_error($apiResponse) || !empty($apiResponse->error) || is_wp_error($accountResponse) || !empty($accountResponse->errors)) {
88 wp_send_json_error(
89 empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
90 400
91 );
92 }
93 $apiResponse->generates_on = \time();
94 wp_send_json_success($apiResponse, 200);
95 } else {
96 wp_send_json_error(
97 __(
98 'Token expired',
99 'bit-form'
100 ),
101 401
102 );
103 }
104 }
105
106 /**
107 * Helps to refresh zoho crm access_token
108 *
109 * @param Array $apiData Contains required data for refresh access token
110 * @return JSON $tokenDetails API token details
111 */
112 protected static function _refreshAccessToken($apiData)
113 {
114 if (
115 empty($apiData->dataCenter)
116 || empty($apiData->clientId)
117 || empty($apiData->clientSecret)
118 || empty($apiData->tokenDetails)
119 ) {
120 return false;
121 }
122 $tokenDetails = $apiData->tokenDetails;
123
124 $dataCenter = $apiData->dataCenter;
125 $apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token";
126 $requestParams = [
127 'grant_type' => 'refresh_token',
128 'client_id' => $apiData->clientId,
129 'client_secret' => $apiData->clientSecret,
130 'refresh_token' => $tokenDetails->refresh_token,
131 ];
132
133 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams);
134 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
135 return false;
136 }
137 $tokenDetails->generates_on = \time();
138 $tokenDetails->access_token = $apiResponse->access_token;
139 return $tokenDetails;
140 }
141
142 /**
143 * Save updated access_token to avoid unnecessary token generation
144 *
145 * @param Integer $fromID ID of Integration related form
146 * @param Integer $integrationID ID of Zoho crm Integration
147 * @param Obeject $tokenDetails refreshed token info
148 *
149 * @return null
150 */
151 protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null)
152 {
153 if (empty($formID) || empty($integrationID)) {
154 return;
155 }
156
157 $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail());
158 $zmailDetails = $integrationHandler->getAIntegration($integrationID);
159
160 if (is_wp_error($zmailDetails)) {
161 return;
162 }
163 $newDetails = json_decode($zmailDetails[0]->integration_details);
164
165 $newDetails->tokenDetails = $tokenDetails;
166
167 $integrationHandler->updateIntegration($integrationID, $zmailDetails[0]->integration_name, 'Zoho Mail', \json_encode($newDetails), 'form');
168 }
169
170 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
171 {
172 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
173
174 $tokenDetails = $integrationDetails->tokenDetails;
175
176 if (empty($tokenDetails)) {
177 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho mail api', 'bit-form'));
178 }
179
180 $requiredParams = null;
181 if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) {
182 $requiredParams['clientId'] = $integrationDetails->clientId;
183 $requiredParams['clientSecret'] = $integrationDetails->clientSecret;
184 $requiredParams['dataCenter'] = $integrationDetails->dataCenter;
185 $requiredParams['tokenDetails'] = $tokenDetails;
186 $newTokenDetails = ZohoMailHandler::_refreshAccessToken((object)$requiredParams);
187 if ($newTokenDetails) {
188 ZohoMailHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails);
189 $tokenDetails = $newTokenDetails;
190 }
191 }
192 // $actions = $integrationDetails->actions;
193 $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID);
194
195 $zmailApiResponse = $recordApiHelper->executeRecordApi(
196 $integrationDetails,
197 $fieldValues,
198 $this->_formID,
199 $entryID
200 );
201
202 return $zmailApiResponse;
203 }
204 }
205