apis
4 months ago
purchase-orders
5 months ago
zoho-crm
6 months ago
zoho-inventory
4 months ago
class-api-handler-zoho.php
6 months ago
class-auth-zoho.php
6 months ago
class-common.php
6 months ago
class-plugin.php
5 months ago
class-wc-api.php
5 months ago
index.php
1 year ago
class-auth-zoho.php
186 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | /** |
| 7 | * Handles Zoho authentication for CommerceBird. |
| 8 | * |
| 9 | * Provides methods for obtaining and refreshing Zoho access tokens. |
| 10 | * |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | class CMBIRD_Auth_Zoho { |
| 14 | |
| 15 | /** |
| 16 | * Configuration options for Zoho authentication. |
| 17 | * |
| 18 | * @var array|array[] |
| 19 | */ |
| 20 | private array $config; |
| 21 | |
| 22 | /** |
| 23 | * Constructor for the CMBIRD_Auth_Zoho class. |
| 24 | * |
| 25 | * Sets up the object properties from the WordPress options table. |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | $config = array( |
| 31 | |
| 32 | 'ServiceZI' => array( |
| 33 | 'OID' => get_option( 'cmbird_zoho_inventory_oid' ), |
| 34 | 'CLIENTSECRET' => get_option( 'cmbird_zoho_inventory_cs' ), |
| 35 | 'CLIENTID' => get_option( 'cmbird_zoho_inventory_cid' ), |
| 36 | 'REDIRECTURL' => get_option( 'cmbird_authorization_redirect_uri' ), |
| 37 | 'APIURL' => get_option( 'cmbird_zoho_inventory_url' ), |
| 38 | 'DOMAINNAME' => get_option( 'cmbird_zoho_inventory_domain' ), |
| 39 | 'SCOPE' => 'ZohoInventory.FullAccess.all', |
| 40 | // 'STATE' => wp_create_nonce('redirect_url'),. |
| 41 | 'AUTHURL' => 'ca' === get_option( 'cmbird_zoho_inventory_domain' ) ? 'https://accounts.zohocloud.ca/oauth/v2/token' : 'https://accounts.zoho.' . get_option( 'cmbird_zoho_inventory_domain' ) . '/oauth/v2/token', |
| 42 | |
| 43 | ), |
| 44 | 'ServiceZCRM' => array( |
| 45 | 'CLIENTSECRET' => get_option( 'cmbird_zoho_crm_cs' ), |
| 46 | 'CLIENTID' => get_option( 'cmbird_zoho_crm_cid' ), |
| 47 | 'REDIRECTURL' => get_option( 'cmbird_authorization_redirect_uri' ), |
| 48 | 'APIURL' => get_option( 'cmbird_zoho_crm_url' ), |
| 49 | 'DOMAINNAME' => get_option( 'cmbird_zoho_crm_domain' ), |
| 50 | 'SCOPE' => 'ZohoCRM.users.ALL,ZohoCRM.bulk.ALL,ZohoCRM.modules.ALL,ZohoCRM.settings.ALL,ZohoCRM.org.ALL,profile.userphoto.READ,ZohoFiles.files.CREATE', |
| 51 | // 'STATE' => wp_create_nonce('redirect_url'),. |
| 52 | 'AUTHURL' => 'ca' === get_option( 'cmbird_zoho_crm_domain' ) ? 'https://accounts.zohocloud.ca/oauth/v2/token' : 'https://accounts.zoho.' . get_option( 'cmbird_zoho_crm_domain' ) . '/oauth/v2/token', |
| 53 | ), |
| 54 | |
| 55 | ); |
| 56 | |
| 57 | $this->config = $config; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Retrieves the access token for the given Zoho application. |
| 62 | * |
| 63 | * @param string $code The authorization code from Zoho. |
| 64 | * @param string $app_name The name of the Zoho application (e.g. 'zoho_inventory', 'zoho_crm'). |
| 65 | * |
| 66 | * @return array|WP_Error The decoded JSON response from the Zoho API, or a WP_Error object on failure. |
| 67 | */ |
| 68 | public function get_zoho_access_token( $code, $app_name ) { |
| 69 | |
| 70 | $headers = array( 'Content-Type: application/x-www-form-urlencoded' ); |
| 71 | switch ( $app_name ) { |
| 72 | case 'zoho_inventory': |
| 73 | $params = array( |
| 74 | 'code' => $code, |
| 75 | 'client_id' => $this->config['ServiceZI']['CLIENTID'], |
| 76 | 'client_secret' => $this->config['ServiceZI']['CLIENTSECRET'], |
| 77 | 'redirect_uri' => $this->config['ServiceZI']['REDIRECTURL'], |
| 78 | 'scope' => $this->config['ServiceZI']['SCOPE'], |
| 79 | 'grant_type' => 'authorization_code', |
| 80 | ); |
| 81 | $url = $this->config['ServiceZI']['AUTHURL']; |
| 82 | break; |
| 83 | default: |
| 84 | $params = array( |
| 85 | 'code' => $code, |
| 86 | 'client_id' => $this->config['ServiceZCRM']['CLIENTID'], |
| 87 | 'client_secret' => $this->config['ServiceZCRM']['CLIENTSECRET'], |
| 88 | 'redirect_uri' => $this->config['ServiceZCRM']['REDIRECTURL'], |
| 89 | 'scope' => $this->config['ServiceZCRM']['SCOPE'], |
| 90 | 'grant_type' => 'authorization_code', |
| 91 | ); |
| 92 | $url = $this->config['ServiceZCRM']['AUTHURL']; |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | // Set up the request arguments. |
| 97 | $args = array( |
| 98 | 'headers' => $headers, |
| 99 | 'body' => $params, |
| 100 | 'method' => 'POST', |
| 101 | ); |
| 102 | |
| 103 | // Debug: Log the auth URL being used. |
| 104 | if ( function_exists( 'wc_get_logger' ) ) { |
| 105 | $logger = wc_get_logger(); |
| 106 | $logger->debug( 'CommerceBird Auth Debug - App: ' . $app_name . ', Auth URL: ' . $url, array( 'source' => 'commercebird' ) ); |
| 107 | } |
| 108 | |
| 109 | // Make the request using wp_remote_post(). |
| 110 | $response = wp_remote_post( $url, $args ); |
| 111 | |
| 112 | // Check if the request was successful. |
| 113 | if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 114 | // If successful, get the body of the response. |
| 115 | $body = wp_remote_retrieve_body( $response ); |
| 116 | |
| 117 | // Decode JSON response. |
| 118 | return json_decode( $body, true ); |
| 119 | } else { |
| 120 | // If there was an error, handle it. |
| 121 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 122 | if ( function_exists( 'wc_get_logger' ) ) { |
| 123 | $logger = wc_get_logger(); |
| 124 | $logger->error( 'CommerceBird Access Token Error - App: ' . $app_name . ', Error: ' . $error_message, array( 'source' => 'commercebird' ) ); |
| 125 | } |
| 126 | return 'Error: ' . $error_message; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Retrieves a new access token for the given app using the refresh token. |
| 133 | * |
| 134 | * @param string $refresh_token The refresh token to use for the request. |
| 135 | * @param string $app_name The name of the app to retrieve the access token for (e.g. 'zoho_inventory', 'zoho_crm'). |
| 136 | * |
| 137 | * @return object | WP_Error The JSON response from the request, or an error message if there was an error. |
| 138 | */ |
| 139 | public function get_zoho_refresh_token( $refresh_token, $app_name ) { |
| 140 | $headers = array( 'Content-Type: application/x-www-form-urlencoded' ); |
| 141 | |
| 142 | $client_id = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['CLIENTID'] : $this->config['ServiceZCRM']['CLIENTID']; |
| 143 | $client_sec = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['CLIENTSECRET'] : $this->config['ServiceZCRM']['CLIENTSECRET']; |
| 144 | $params = array( |
| 145 | 'refresh_token' => $refresh_token, |
| 146 | 'grant_type' => 'refresh_token', |
| 147 | 'client_id' => $client_id, |
| 148 | 'client_secret' => $client_sec, |
| 149 | ); |
| 150 | $url = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['AUTHURL'] : $this->config['ServiceZCRM']['AUTHURL']; |
| 151 | // Set up the request arguments. |
| 152 | $args = array( |
| 153 | 'headers' => $headers, |
| 154 | 'body' => $params, |
| 155 | 'method' => 'POST', |
| 156 | ); |
| 157 | |
| 158 | // Debug: Log the auth URL being used for refresh token. |
| 159 | if ( function_exists( 'wc_get_logger' ) ) { |
| 160 | $logger = wc_get_logger(); |
| 161 | $logger->debug( 'CommerceBird Refresh Token Debug - App: ' . $app_name . ', Auth URL: ' . $url, array( 'source' => 'commercebird' ) ); |
| 162 | } |
| 163 | |
| 164 | // Make the request using wp_remote_post(). |
| 165 | $response = wp_remote_post( $url, $args ); |
| 166 | // Check if the request was successful. |
| 167 | if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 168 | // If successful, get the body of the response. |
| 169 | $body = wp_remote_retrieve_body( $response ); |
| 170 | // Decode JSON response. |
| 171 | return json_decode( $body, true ); |
| 172 | } else { |
| 173 | // If there was an error, handle it. |
| 174 | $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.'; |
| 175 | if ( function_exists( 'wc_get_logger' ) ) { |
| 176 | $logger = wc_get_logger(); |
| 177 | $logger->error( 'CommerceBird Refresh Token Error - App: ' . $app_name . ', Error: ' . $error_message, array( 'source' => 'commercebird' ) ); |
| 178 | } |
| 179 | // return the WP Error object. |
| 180 | return new WP_Error( 'zoho_refresh_token_error', $error_message ); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $cmbird_handlefunction = new CMBIRD_Auth_Zoho(); |
| 186 |