PluginProbe ʕ •ᴥ•ʔ
TaxCloud for WooCommerce / 8.4.11
TaxCloud for WooCommerce v8.4.11
8.4.11 8.4.10 8.4.9 trunk 6.0.11 6.0.12 6.0.13 6.0.14 6.1.0 6.1.1 6.1.2 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.3.0 6.3.1 6.3.10 6.3.11 6.3.12 6.3.13 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 7.0.0 7.0.1 7.0.10 7.0.11 7.0.12 7.0.13 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 8.0.0 8.0.1 8.0.10 8.0.11 8.0.12 8.0.13 8.0.14 8.0.15 8.0.16 8.0.17 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.0.9 8.1.0 8.1.1 8.2.0 8.2.1 8.2.2 8.2.3 8.2.4 8.3.0 8.3.1 8.3.2 8.3.3 8.3.4 8.3.5 8.3.6 8.3.7 8.3.8 8.4.0 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.4.6 8.4.7 8.4.8
simple-sales-tax / includes / class-sst-taxcloud-v3-api.php
simple-sales-tax / includes Last commit date
abstracts 3 months ago admin 3 months ago frontend 1 month ago integrations 1 month ago v3 3 months ago vendor 6 days ago views 8 months ago class-simplesalestax.php 6 days ago class-sst-addresses.php 1 month ago class-sst-ajax.php 3 months ago class-sst-assets.php 6 months ago class-sst-blocks-integration.php 1 month ago class-sst-blocks.php 1 year ago class-sst-certificates.php 6 days ago class-sst-install.php 6 months ago class-sst-logger.php 5 months ago class-sst-marketplaces.php 6 months ago class-sst-order-controller.php 3 months ago class-sst-order.php 1 month ago class-sst-origin-address.php 8 months ago class-sst-product.php 3 months ago class-sst-rate-limit.php 5 months ago class-sst-settings.php 3 months ago class-sst-shipping.php 3 years ago class-sst-taxcloud-v3-api.php 3 months ago class-sst-taxcloud-v3.php 3 months ago class-sst-tic.php 2 years ago class-sst-updater.php 3 years ago sst-compatibility-functions.php 4 months ago sst-functions.php 6 days ago sst-message-functions.php 3 years ago sst-update-functions.php 11 months ago
class-sst-taxcloud-v3-api.php
187 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * TaxCloud v3 API Client.
9 *
10 * Handles authentication and settings retrieval from TaxCloud v3 API.
11 *
12 * @author Simple Sales Tax
13 * @package SST
14 * @since 8.4.0
15 */
16 class SST_TaxCloud_V3_API {
17
18 /**
19 * API Base URLs.
20 */
21 const STAGING_AUTH_URL = 'https://staging-taxcloudapi.azurewebsites.net/api/v3/auth/token';
22 const PROD_AUTH_URL = 'https://taxcloudapi-appservice-core-prod.azurewebsites.net/api/v3/auth/token';
23 const STAGING_MGMT_URL = 'https://api.v3.taxcloud.net/mgmt';
24 const PROD_MGMT_URL = 'https://api.v3.taxcloud.com/mgmt';
25
26 /**
27 * Get the appropriate Auth URL based on environment.
28 *
29 * @return string
30 */
31 private static function get_auth_url() {
32 // For now, we'll default to PROD unless a constant is defined for staging.
33 // In the future, this could be a setting.
34 if ( defined( 'SST_TAXCLOUD_STAGING' ) && SST_TAXCLOUD_STAGING ) {
35 return self::STAGING_AUTH_URL;
36 }
37 return self::PROD_AUTH_URL;
38 }
39
40 /**
41 * Get the appropriate Management URL based on environment.
42 *
43 * @return string
44 */
45 private static function get_mgmt_url() {
46 if ( defined( 'SST_TAXCLOUD_STAGING' ) && SST_TAXCLOUD_STAGING ) {
47 return self::STAGING_MGMT_URL;
48 }
49 return self::PROD_MGMT_URL;
50 }
51
52 /**
53 * Exchange v1 credentials for v3 Bearer token.
54 *
55 * @param string $api_login_id TaxCloud API Login ID.
56 * @param string $api_key TaxCloud API Key.
57 * @return string|WP_Error Access token on success, WP_Error on failure.
58 */
59 public static function get_auth_token( $api_login_id, $api_key ) {
60 $url = self::get_auth_url();
61
62 $response = wp_remote_post( $url, array(
63 'headers' => array(
64 'Content-Type' => 'application/json',
65 ),
66 'body' => json_encode( array(
67 'apiLoginID' => $api_login_id,
68 'apiKey' => $api_key,
69 ) ),
70 'timeout' => 30,
71 ) );
72
73 if ( is_wp_error( $response ) ) {
74 return $response;
75 }
76
77 $code = wp_remote_retrieve_response_code( $response );
78 $body = wp_remote_retrieve_body( $response );
79 $data = json_decode( $body, true );
80
81 if ( $code >= 400 ) {
82 return new WP_Error( 'sst_v3_auth_error', 'Failed to authenticate with TaxCloud v3 API: ' . ( isset( $data['message'] ) ? $data['message'] : $body ) );
83 }
84
85 if ( empty( $data['access_token'] ) ) {
86 return new WP_Error( 'sst_v3_auth_error', 'No access token received from TaxCloud v3 API.' );
87 }
88
89 if ( ! empty( $data['connection_id'] ) ) {
90 SST_Settings::set( 'tc_connection_id', $data['connection_id'] );
91 }
92
93 return $data['access_token'];
94 }
95
96 /**
97 * Get connection settings using Bearer token.
98 *
99 * @param string $api_key TaxCloud API Key (used as connection ID).
100 * @param string $access_token Bearer token.
101 * @return array|WP_Error Settings array on success, WP_Error on failure.
102 */
103 public static function get_connection_settings( $api_key, $access_token ) {
104 $url = self::get_mgmt_url() . '/connections/' . $api_key;
105
106 $response = wp_remote_get( $url, array(
107 'headers' => array(
108 'Authorization' => 'Bearer ' . $access_token,
109 'Content-Type' => 'application/json',
110 ),
111 'timeout' => 30,
112 ) );
113
114 if ( is_wp_error( $response ) ) {
115 return $response;
116 }
117
118 $code = wp_remote_retrieve_response_code( $response );
119 $body = wp_remote_retrieve_body( $response );
120
121 if ( $code === 404 ) {
122 // Connection settings don't exist yet, which is normal for new connections.
123 // Return empty settings.
124 return array();
125 }
126
127 if ( $code >= 400 ) {
128 return new WP_Error( 'sst_v3_settings_error', 'Failed to retrieve connection settings: ' . $body );
129 }
130
131 return json_decode( $body, true );
132 }
133
134 /**
135 * Get settings using v1 credentials.
136 *
137 * @param string $api_login_id TaxCloud API Login ID.
138 * @param string $api_key TaxCloud API Key.
139 * @return array|WP_Error Settings array on success, WP_Error on failure.
140 */
141 public static function get_settings_with_v1_creds( $api_login_id, $api_key ) {
142 $token = self::get_auth_token( $api_login_id, $api_key );
143
144 if ( is_wp_error( $token ) ) {
145 return $token;
146 }
147
148 return self::get_connection_settings( $api_key, $token );
149 }
150
151 /**
152 * Update data mover settings using v1 credentials.
153 *
154 * @param string $api_login_id TaxCloud API Login ID.
155 * @param string $api_key TaxCloud API Key.
156 * @return array|WP_Error Settings array on success, WP_Error on failure.
157 */
158 public static function update_data_mover_settings( $api_login_id = null, $api_key = null ) {
159 if ( !$api_login_id || !$api_key ) {
160 $api_login_id = SST_Settings::get( 'tc_id' );
161 $api_key = SST_Settings::get( 'tc_key' );
162 }
163
164 // Return if empty
165 if ( empty( $api_login_id ) || empty( $api_key ) ) {
166 SST_Logger::add( 'Failed to update data mover settings: API Login ID or API Key is empty' );
167 return;
168 }
169
170 // Add to cronjob to check daily
171 if ( ! wp_next_scheduled( 'sst_update_data_mover_settings' ) ) {
172 wp_schedule_event( time(), 'daily', 'sst_update_data_mover_settings' );
173 }
174
175 // Check v3 settings
176 $v3_settings = self::get_settings_with_v1_creds( $api_login_id, $api_key );
177
178 if ( ! is_wp_error( $v3_settings ) ) {
179 $data_mover = (bool) isset( $v3_settings['options']['data_mover']['flag'] ) && $v3_settings['options']['data_mover']['flag'];
180 SST_Settings::set( 'data_mover', $data_mover );
181 } else {
182 // Log error but don't fail verification if v3 fails (optional, depending on strictness)
183 SST_Logger::add( 'Failed to fetch v3 settings: ' . $v3_settings->get_error_message() );
184 }
185 }
186 }
187