apis
1 year ago
purchase-orders
1 year ago
zoho-crm
1 year ago
zoho-inventory
1 year ago
class-api-handler-zoho.php
1 year ago
class-auth-zoho.php
1 year ago
class-common.php
1 year ago
class-plugin.php
1 year ago
class-wc-api.php
1 year ago
class-webhook-modify.php
1 year ago
class-api-handler-zoho.php
394 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * All Execute Call Class related functions. |
| 5 | * |
| 6 | * @package Inventory |
| 7 | */ |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | class CMBIRD_API_Handler_Zoho { |
| 13 | /** |
| 14 | * @var array|array[] |
| 15 | */ |
| 16 | private array $config; |
| 17 | |
| 18 | private $filesystem; |
| 19 | |
| 20 | public function __construct() { |
| 21 | $this->config = array( |
| 22 | 'ExecutecallZI' => array( |
| 23 | 'OID' => get_option( 'cmbird_zoho_inventory_oid' ), |
| 24 | 'ATOKEN' => get_option( 'cmbird_zoho_inventory_access_token' ), |
| 25 | 'RTOKEN' => get_option( 'cmbird_zoho_inventory_refresh_token' ), |
| 26 | 'EXPIRESTIME' => get_option( 'cmbird_zoho_inventory_timestamp' ), |
| 27 | ), |
| 28 | 'ExecutecallZCRM' => array( |
| 29 | 'ATOKEN' => get_option( 'cmbird_zoho_crm_access_token' ), |
| 30 | 'RTOKEN' => get_option( 'cmbird_zoho_crm_refresh_token' ), |
| 31 | 'EXPIRESTIME' => get_option( 'cmbird_zoho_crm_timestamp' ), |
| 32 | ), |
| 33 | ); |
| 34 | if ( null === $this->filesystem ) { |
| 35 | global $wp_filesystem; |
| 36 | |
| 37 | if ( empty( $wp_filesystem ) ) { |
| 38 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 39 | WP_Filesystem(); |
| 40 | } |
| 41 | |
| 42 | $this->filesystem = $wp_filesystem; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Get Call Zoho |
| 47 | public function execute_curl_call_get( $url ) { |
| 48 | // Sleep for .5 sec for each api calls |
| 49 | usleep( 500000 ); |
| 50 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 51 | |
| 52 | // if $url contains 'inventory' use zoho_access_token, refresh token and timestamp |
| 53 | if ( strpos( $url, 'inventory' ) !== false ) { |
| 54 | $app_name = 'zoho_inventory'; |
| 55 | $zoho_access_token = $this->config['ExecutecallZI']['ATOKEN']; |
| 56 | $zoho_refresh_token = $this->config['ExecutecallZI']['RTOKEN']; |
| 57 | $zoho_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME']; |
| 58 | $authorization_header = 'Bearer'; |
| 59 | } else { |
| 60 | $app_name = 'zoho_crm'; |
| 61 | $zoho_access_token = $this->config['ExecutecallZCRM']['ATOKEN']; |
| 62 | $zoho_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN']; |
| 63 | $zoho_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME']; |
| 64 | $authorization_header = 'Zoho-oauthtoken'; |
| 65 | } |
| 66 | $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 67 | if ( $zoho_timestamp < $current_time ) { |
| 68 | |
| 69 | $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_refresh_token, $app_name ); |
| 70 | if ( empty( $respo_at_js ) || ! array_key_exists( 'access_token', $respo_at_js ) ) { |
| 71 | return new WP_Error( 403, 'Access denied!' ); |
| 72 | } |
| 73 | $zoho_access_token = $respo_at_js['access_token']; |
| 74 | if ( 'zoho_inventory' === $app_name ) { |
| 75 | update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] ); |
| 76 | update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 77 | } else { |
| 78 | update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] ); |
| 79 | update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $args = array( |
| 84 | 'headers' => array( |
| 85 | 'Authorization' => "$authorization_header $zoho_access_token", |
| 86 | ), |
| 87 | ); |
| 88 | |
| 89 | $response = wp_remote_get( $url, $args ); |
| 90 | |
| 91 | // get the status code of the response |
| 92 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 93 | // if code is 429, update the option "zoho_rate_limit_exceeded" to true |
| 94 | if ( 429 === $status_code ) { |
| 95 | update_option( 'cmbird_zoho_rate_limit_exceeded', true ); |
| 96 | } else { |
| 97 | update_option( 'cmbird_zoho_rate_limit_exceeded', false ); |
| 98 | } |
| 99 | |
| 100 | // Check if the request was successful |
| 101 | if ( ! is_wp_error( $response ) ) { |
| 102 | // If successful, get the body of the response |
| 103 | $body = wp_remote_retrieve_body( $response ); |
| 104 | |
| 105 | // Decode JSON response |
| 106 | return json_decode( $body ); |
| 107 | } else { |
| 108 | // If there was an error, handle it |
| 109 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 110 | return "Error: $error_message"; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Post Call Zoho |
| 115 | |
| 116 | public function execute_curl_call_post( $url, $data ) { |
| 117 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 118 | |
| 119 | // if $url contains 'inventory' use zoho__access_token, refresh token and timestamp |
| 120 | if ( strpos( $url, 'inventory' ) !== false ) { |
| 121 | $app_name = 'zoho_inventory'; |
| 122 | $zoho_access_token = $this->config['ExecutecallZI']['ATOKEN']; |
| 123 | $zoho_refresh_token = $this->config['ExecutecallZI']['RTOKEN']; |
| 124 | $zoho_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME']; |
| 125 | $authorization_header = 'Bearer'; |
| 126 | } else { |
| 127 | $app_name = 'zoho_crm'; |
| 128 | $zoho_access_token = $this->config['ExecutecallZCRM']['ATOKEN']; |
| 129 | $zoho_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN']; |
| 130 | $zoho_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME']; |
| 131 | $authorization_header = 'Zoho-oauthtoken'; |
| 132 | } |
| 133 | |
| 134 | $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 135 | |
| 136 | if ( $zoho_timestamp < $current_time ) { |
| 137 | |
| 138 | $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_refresh_token, $app_name ); |
| 139 | |
| 140 | $zoho_access_token = $respo_at_js['access_token']; |
| 141 | if ( 'zoho_inventory' === $app_name ) { |
| 142 | update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] ); |
| 143 | update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 144 | } else { |
| 145 | update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] ); |
| 146 | update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $args = array( |
| 151 | 'body' => $data, |
| 152 | 'headers' => array( |
| 153 | 'Authorization' => "$authorization_header $zoho_access_token", |
| 154 | ), |
| 155 | ); |
| 156 | $response = wp_remote_post( $url, $args ); |
| 157 | |
| 158 | // get the status code of the response |
| 159 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 160 | // if code is 429, update the option "zoho_rate_limit_exceeded" to true |
| 161 | if ( 429 === $status_code ) { |
| 162 | update_option( 'cmbird_zoho_rate_limit_exceeded', true ); |
| 163 | } else { |
| 164 | update_option( 'cmbird_zoho_rate_limit_exceeded', false ); |
| 165 | } |
| 166 | |
| 167 | // Check if the request was successful |
| 168 | if ( ! is_wp_error( $response ) ) { |
| 169 | // If successful, get the body of the response |
| 170 | $body = wp_remote_retrieve_body( $response ); |
| 171 | // Decode JSON response |
| 172 | return json_decode( $body ); |
| 173 | } else { |
| 174 | // If there was an error, handle it |
| 175 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 176 | return "Error: $error_message"; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Put Call Zoho |
| 181 | |
| 182 | public function execute_curl_call_put( $url, $data ) { |
| 183 | |
| 184 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 185 | |
| 186 | // if $url contains 'inventory' use zoho_inventory_access_token, refresh token and timestamp |
| 187 | if ( strpos( $url, 'inventory' ) !== false ) { |
| 188 | $app_name = 'zoho_inventory'; |
| 189 | $zoho_inventory_access_token = $this->config['ExecutecallZI']['ATOKEN']; |
| 190 | $zoho_inventory_refresh_token = $this->config['ExecutecallZI']['RTOKEN']; |
| 191 | $zoho_inventory_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME']; |
| 192 | $authorization_header = 'Bearer'; |
| 193 | } else { |
| 194 | $app_name = 'zoho_crm'; |
| 195 | $zoho_inventory_access_token = $this->config['ExecutecallZCRM']['ATOKEN']; |
| 196 | $zoho_inventory_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN']; |
| 197 | $zoho_inventory_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME']; |
| 198 | $authorization_header = 'Zoho-oauthtoken'; |
| 199 | } |
| 200 | |
| 201 | $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 202 | |
| 203 | if ( $zoho_inventory_timestamp < $current_time ) { |
| 204 | |
| 205 | $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_inventory_refresh_token, $app_name ); |
| 206 | $zoho_inventory_access_token = $respo_at_js['access_token']; |
| 207 | if ( 'zoho_inventory' === $app_name ) { |
| 208 | update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] ); |
| 209 | update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 210 | } else { |
| 211 | update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] ); |
| 212 | update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | $args = array( |
| 217 | 'body' => $data, |
| 218 | 'headers' => array( |
| 219 | 'Authorization' => "$authorization_header $zoho_inventory_access_token", |
| 220 | ), |
| 221 | 'method' => 'PUT', |
| 222 | ); |
| 223 | $response = wp_remote_request( $url, $args ); |
| 224 | |
| 225 | // get the status code of the response |
| 226 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 227 | // if code is 429, update the option "zoho_rate_limit_exceeded" to true |
| 228 | if ( 429 === $status_code ) { |
| 229 | update_option( 'cmbird_zoho_rate_limit_exceeded', true ); |
| 230 | } else { |
| 231 | update_option( 'cmbird_zoho_rate_limit_exceeded', false ); |
| 232 | } |
| 233 | |
| 234 | // Check if the request was successful |
| 235 | if ( ! is_wp_error( $response ) ) { |
| 236 | // If successful, get the body of the response |
| 237 | $body = wp_remote_retrieve_body( $response ); |
| 238 | // Decode JSON response |
| 239 | return json_decode( $body ); |
| 240 | } else { |
| 241 | // If there was an error, handle it |
| 242 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 243 | return "Error: $error_message"; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Delete Call to Zoho |
| 249 | * @param string $url |
| 250 | * @return mixed |
| 251 | */ |
| 252 | public function execute_curl_call_delete( $url ) { |
| 253 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 254 | |
| 255 | // if $url contains 'inventory' use zoho_inventory_access_token, refresh token and timestamp |
| 256 | if ( strpos( $url, 'inventory' ) !== false ) { |
| 257 | $app_name = 'zoho_inventory'; |
| 258 | $zoho_inventory_access_token = $this->config['ExecutecallZI']['ATOKEN']; |
| 259 | $zoho_inventory_refresh_token = $this->config['ExecutecallZI']['RTOKEN']; |
| 260 | $zoho_inventory_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME']; |
| 261 | $authorization_header = 'Bearer'; |
| 262 | } else { |
| 263 | $app_name = 'zoho_crm'; |
| 264 | $zoho_inventory_access_token = $this->config['ExecutecallZCRM']['ATOKEN']; |
| 265 | $zoho_inventory_refresh_token = $this->config['ExecutecallZCRM']['RTOKEN']; |
| 266 | $zoho_inventory_timestamp = $this->config['ExecutecallZCRM']['EXPIRESTIME']; |
| 267 | $authorization_header = 'Zoho-oauthtoken'; |
| 268 | } |
| 269 | |
| 270 | $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 271 | |
| 272 | if ( $zoho_inventory_timestamp < $current_time ) { |
| 273 | |
| 274 | $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_inventory_refresh_token, $app_name ); |
| 275 | $zoho_inventory_access_token = $respo_at_js['access_token']; |
| 276 | if ( 'zoho_inventory' === $app_name ) { |
| 277 | update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] ); |
| 278 | update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 279 | } else { |
| 280 | update_option( 'cmbird_zoho_crm_access_token', $respo_at_js['access_token'] ); |
| 281 | update_option( 'cmbird_zoho_crm_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | $args = array( |
| 286 | 'headers' => array( |
| 287 | 'Authorization' => "$authorization_header $zoho_inventory_access_token", |
| 288 | ), |
| 289 | 'method' => 'DELETE', |
| 290 | ); |
| 291 | $response = wp_remote_request( $url, $args ); |
| 292 | |
| 293 | // get the status code of the response |
| 294 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 295 | // if code is 429, update the option "zoho_rate_limit_exceeded" to true |
| 296 | if ( 429 === $status_code ) { |
| 297 | update_option( 'cmbird_zoho_rate_limit_exceeded', true ); |
| 298 | } else { |
| 299 | update_option( 'cmbird_zoho_rate_limit_exceeded', false ); |
| 300 | } |
| 301 | |
| 302 | // Check if the request was successful |
| 303 | if ( ! is_wp_error( $response ) ) { |
| 304 | // If successful, get the body of the response |
| 305 | $body = wp_remote_retrieve_body( $response ); |
| 306 | // Decode JSON response |
| 307 | return json_decode( $body ); |
| 308 | } else { |
| 309 | // If there was an error, handle it |
| 310 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 311 | return "Error: $error_message"; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * |
| 317 | * Get Call Zoho Image |
| 318 | * @param mixed $url - URL of the image. |
| 319 | * @return string |
| 320 | */ |
| 321 | public function execute_curl_call_image_get( $url, $image_name ) { |
| 322 | // $fd = fopen( __DIR__ . '/execute_curl_call_image_get.txt', 'w' ); |
| 323 | |
| 324 | global $wp_filesystem; |
| 325 | |
| 326 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 327 | $zoho_inventory_access_token = $this->config['ExecutecallZI']['ATOKEN']; |
| 328 | $zoho_inventory_refresh_token = $this->config['ExecutecallZI']['RTOKEN']; |
| 329 | $zoho_inventory_timestamp = $this->config['ExecutecallZI']['EXPIRESTIME']; |
| 330 | |
| 331 | $current_time = strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 332 | |
| 333 | if ( $zoho_inventory_timestamp < $current_time ) { |
| 334 | |
| 335 | $respo_at_js = $handlefunction->get_zoho_refresh_token( $zoho_inventory_refresh_token, 'zoho_inventory' ); |
| 336 | |
| 337 | $zoho_inventory_access_token = $respo_at_js['access_token']; |
| 338 | update_option( 'cmbird_zoho_inventory_access_token', $respo_at_js['access_token'] ); |
| 339 | update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $respo_at_js['expires_in'] ); |
| 340 | |
| 341 | } |
| 342 | $args = array( |
| 343 | 'headers' => array( |
| 344 | 'Authorization' => 'Bearer ' . $zoho_inventory_access_token, |
| 345 | ), |
| 346 | ); |
| 347 | $response = wp_remote_get( $url, $args ); |
| 348 | // fwrite( $fd, PHP_EOL . 'Response : ' . print_r( $response, true ) ); |
| 349 | // fclose( $fd ); |
| 350 | |
| 351 | // Check if the request was successful |
| 352 | if ( ! is_wp_error( $response ) ) { |
| 353 | // If successful, get the body of the response |
| 354 | $body = wp_remote_retrieve_body( $response ); |
| 355 | |
| 356 | // Set up the upload directory |
| 357 | $upload = wp_upload_dir(); |
| 358 | $absolute_upload_path = $upload['basedir'] . '/zoho_image/'; |
| 359 | $url_upload_path = $upload['baseurl'] . '/zoho_image/'; |
| 360 | |
| 361 | // Generate a unique image name |
| 362 | $img = $image_name; |
| 363 | $upload_dir = $absolute_upload_path . '/' . $img; |
| 364 | |
| 365 | // remove the file if it exists |
| 366 | if ( file_exists( $upload_dir ) ) { |
| 367 | wp_delete_file( $upload_dir ); |
| 368 | } |
| 369 | |
| 370 | // Create the directory if it doesn't exist |
| 371 | if ( ! is_dir( $absolute_upload_path ) ) { |
| 372 | wp_mkdir_p( $absolute_upload_path ); |
| 373 | } |
| 374 | // Save the image file |
| 375 | try { |
| 376 | $wp_filesystem->put_contents( $upload_dir, $body ); |
| 377 | } catch (Exception $e) { |
| 378 | wp_delete_file( $upload_dir ); |
| 379 | // If there was an error, handle it |
| 380 | $error_message = $e->getMessage(); |
| 381 | echo esc_html( "Error image import: $error_message" ); |
| 382 | return ''; |
| 383 | } |
| 384 | // Use trailingslashit to make sure the URL ends with a single slash |
| 385 | return trailingslashit( $url_upload_path ) . $img; |
| 386 | } else { |
| 387 | // If there was an error, handle it |
| 388 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 389 | echo esc_html( "Error image import: $error_message" ); |
| 390 | return ''; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 |