PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.5.0
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.5.0
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / class-auth-zoho.php
commercebird / includes / classes Last commit date
apis 9 months ago purchase-orders 9 months ago zoho-crm 9 months ago zoho-inventory 9 months ago class-api-handler-zoho.php 9 months ago class-auth-zoho.php 9 months ago class-common.php 9 months ago class-plugin.php 9 months ago class-wc-api.php 10 months ago index.php 1 year ago
class-auth-zoho.php
155 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
79 // Debug: Log the auth URL being used.
80 if ( function_exists( 'wc_get_logger' ) ) {
81 $logger = wc_get_logger();
82 $logger->debug( 'CommerceBird Auth Debug - App: ' . $app_name . ', Auth URL: ' . $url, array( 'source' => 'commercebird' ) );
83 }
84
85 // Make the request using wp_remote_post().
86 $response = wp_remote_post( $url, $args );
87
88 // Check if the request was successful.
89 if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
90 // If successful, get the body of the response.
91 $body = wp_remote_retrieve_body( $response );
92
93 // Decode JSON response.
94 return json_decode( $body, true );
95 } else {
96 // If there was an error, handle it.
97 $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.';
98 if ( function_exists( 'wc_get_logger' ) ) {
99 $logger = wc_get_logger();
100 $logger->error( 'CommerceBird Access Token Error - App: ' . $app_name . ', Error: ' . $error_message, array( 'source' => 'commercebird' ) );
101 }
102 return 'Error: ' . $error_message;
103 }
104 }
105
106
107 // get refresh token in ServiceZI.
108 public function get_zoho_refresh_token( $refresh_token, $app_name ) {
109 $headers = array( 'Content-Type: application/x-www-form-urlencoded' );
110
111 $client_id = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['CLIENTID'] : $this->config['ServiceZCRM']['CLIENTID'];
112 $client_sec = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['CLIENTSECRET'] : $this->config['ServiceZCRM']['CLIENTSECRET'];
113 $params = array(
114 'refresh_token' => $refresh_token,
115 'grant_type' => 'refresh_token',
116 'client_id' => $client_id,
117 'client_secret' => $client_sec,
118 );
119 $url = 'zoho_inventory' === $app_name ? $this->config['ServiceZI']['AUTHURL'] : $this->config['ServiceZCRM']['AUTHURL'];
120 // Set up the request arguments.
121 $args = array(
122 'headers' => $headers,
123 'body' => $params,
124 'method' => 'POST',
125 );
126
127 // Debug: Log the auth URL being used for refresh token.
128 if ( function_exists( 'wc_get_logger' ) ) {
129 $logger = wc_get_logger();
130 $logger->debug( 'CommerceBird Refresh Token Debug - App: ' . $app_name . ', Auth URL: ' . $url, array( 'source' => 'commercebird' ) );
131 }
132
133 // Make the request using wp_remote_post().
134 $response = wp_remote_post( $url, $args );
135 // Check if the request was successful.
136 if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
137 // If successful, get the body of the response.
138 $body = wp_remote_retrieve_body( $response );
139 // Decode JSON response.
140 return json_decode( $body, true );
141 } else {
142 // If there was an error, handle it.
143 $error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error.';
144 if ( function_exists( 'wc_get_logger' ) ) {
145 $logger = wc_get_logger();
146 $logger->error( 'CommerceBird Refresh Token Error - App: ' . $app_name . ', Error: ' . $error_message, array( 'source' => 'commercebird' ) );
147 }
148 echo 'Error: ' . esc_html( $error_message );
149 // echo the exception.
150 }
151 }
152 }
153
154 $handlefunction = new CMBIRD_Auth_Zoho();
155