apis
11 months ago
purchase-orders
1 year ago
zoho-crm
1 year ago
zoho-inventory
11 months ago
class-api-handler-zoho.php
11 months ago
class-auth-zoho.php
1 year ago
class-common.php
1 year ago
class-plugin.php
11 months ago
class-wc-api.php
1 year ago
index.php
1 year ago
class-auth-zoho.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | class CMBIRD_Auth_Zoho { |
| 7 | |
| 8 | /** |
| 9 | * @var array|array[] |
| 10 | */ |
| 11 | private array $config; |
| 12 | |
| 13 | public function __construct() { |
| 14 | $config = array( |
| 15 | |
| 16 | 'ServiceZI' => array( |
| 17 | 'OID' => get_option( 'cmbird_zoho_inventory_oid' ), |
| 18 | 'CLIENTSECRET' => get_option( 'cmbird_zoho_inventory_cs' ), |
| 19 | 'CLIENTID' => get_option( 'cmbird_zoho_inventory_cid' ), |
| 20 | 'REDIRECTURL' => get_option( 'cmbird_authorization_redirect_uri' ), |
| 21 | 'APIURL' => get_option( 'cmbird_zoho_inventory_url' ), |
| 22 | 'DOMAINNAME' => get_option( 'cmbird_zoho_inventory_domain' ), |
| 23 | 'SCOPE' => 'ZohoInventory.FullAccess.all', |
| 24 | //'STATE' => wp_create_nonce('redirect_url'), |
| 25 | 'AUTHURL' => 'https://accounts.zoho.' . get_option( 'cmbird_zoho_inventory_domain' ) . '/oauth/v2/token', |
| 26 | |
| 27 | ), |
| 28 | 'ServiceZCRM' => array( |
| 29 | 'CLIENTSECRET' => get_option( 'cmbird_zoho_crm_cs' ), |
| 30 | 'CLIENTID' => get_option( 'cmbird_zoho_crm_cid' ), |
| 31 | 'REDIRECTURL' => get_option( 'cmbird_authorization_redirect_uri' ), |
| 32 | 'APIURL' => get_option( 'cmbird_zoho_crm_url' ), |
| 33 | 'DOMAINNAME' => get_option( 'cmbird_zoho_crm_domain' ), |
| 34 | 'SCOPE' => 'ZohoCRM.users.ALL,ZohoCRM.bulk.ALL,ZohoCRM.modules.ALL,ZohoCRM.settings.ALL,ZohoCRM.org.ALL,profile.userphoto.READ,ZohoFiles.files.CREATE', |
| 35 | //'STATE' => wp_create_nonce('redirect_url'), |
| 36 | 'AUTHURL' => 'https://accounts.zoho.' . get_option( 'cmbird_zoho_crm_domain' ) . '/oauth/v2/token', |
| 37 | ), |
| 38 | |
| 39 | ); |
| 40 | |
| 41 | $this->config = $config; |
| 42 | } |
| 43 | |
| 44 | public function get_zoho_access_token( $code, $app_name ) { |
| 45 | |
| 46 | $headers = array( 'Content-Type: application/x-www-form-urlencoded' ); |
| 47 | switch ( $app_name ) { |
| 48 | case 'zoho_inventory': |
| 49 | $params = array( |
| 50 | 'code' => $code, |
| 51 | 'client_id' => $this->config['ServiceZI']['CLIENTID'], |
| 52 | 'client_secret' => $this->config['ServiceZI']['CLIENTSECRET'], |
| 53 | 'redirect_uri' => $this->config['ServiceZI']['REDIRECTURL'], |
| 54 | 'scope' => $this->config['ServiceZI']['SCOPE'], |
| 55 | 'grant_type' => 'authorization_code', |
| 56 | ); |
| 57 | $url = $this->config['ServiceZI']['AUTHURL']; |
| 58 | break; |
| 59 | default: |
| 60 | $params = array( |
| 61 | 'code' => $code, |
| 62 | 'client_id' => $this->config['ServiceZCRM']['CLIENTID'], |
| 63 | 'client_secret' => $this->config['ServiceZCRM']['CLIENTSECRET'], |
| 64 | 'redirect_uri' => $this->config['ServiceZCRM']['REDIRECTURL'], |
| 65 | 'scope' => $this->config['ServiceZCRM']['SCOPE'], |
| 66 | 'grant_type' => 'authorization_code', |
| 67 | ); |
| 68 | $url = $this->config['ServiceZCRM']['AUTHURL']; |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | // Set up the request arguments |
| 73 | $args = array( |
| 74 | 'headers' => $headers, |
| 75 | 'body' => $params, |
| 76 | 'method' => 'POST', |
| 77 | ); |
| 78 | // Make the request using wp_remote_post() |
| 79 | $response = wp_remote_post( $url, $args ); |
| 80 | |
| 81 | // Check if the request was successful |
| 82 | if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 83 | // If successful, get the body of the response |
| 84 | $body = wp_remote_retrieve_body( $response ); |
| 85 | |
| 86 | // Decode JSON response |
| 87 | return json_decode( $body, true ); |
| 88 | } else { |
| 89 | // If there was an error, handle it |
| 90 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 91 | return 'Error: ' . $error_message; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | //get refresh token in ServiceZI |
| 97 | public function get_zoho_refresh_token( $refresh_token, $app_name ) { |
| 98 | $headers = array( 'Content-Type: application/x-www-form-urlencoded' ); |
| 99 | |
| 100 | $client_id = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['CLIENTID'] : $this->config['ServiceZCRM']['CLIENTID']; |
| 101 | $client_sec = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['CLIENTSECRET'] : $this->config['ServiceZCRM']['CLIENTSECRET']; |
| 102 | $params = array( |
| 103 | 'refresh_token' => $refresh_token, |
| 104 | 'grant_type' => 'refresh_token', |
| 105 | 'client_id' => $client_id, |
| 106 | 'client_secret' => $client_sec, |
| 107 | ); |
| 108 | $url = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['AUTHURL'] : $this->config['ServiceZCRM']['AUTHURL']; |
| 109 | // Set up the request arguments |
| 110 | $args = array( |
| 111 | 'headers' => $headers, |
| 112 | 'body' => $params, |
| 113 | 'method' => 'POST', |
| 114 | ); |
| 115 | // Make the request using wp_remote_post() |
| 116 | $response = wp_remote_post( $url, $args ); |
| 117 | // Check if the request was successful |
| 118 | if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 119 | // If successful, get the body of the response |
| 120 | $body = wp_remote_retrieve_body( $response ); |
| 121 | // Decode JSON response |
| 122 | return json_decode( $body, true ); |
| 123 | } else { |
| 124 | // If there was an error, handle it |
| 125 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 126 | echo 'Error: ' . esc_html( $error_message ); |
| 127 | // echo the exception |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | $handlefunction = new CMBIRD_Auth_Zoho(); |
| 133 |