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

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

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