Skip to content

Commit ef86dc5

Browse files
authored
Add extra logging for CA taxes added to non-CA merchants (#2893)
* WIP incorrect Cali tax nexus check * Update conditional checks * Remove no longer need param * Add changelog entries * Add changelog entries * Add error handling and additional logging * Fix PHP unit test
1 parent a92f588 commit ef86dc5

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
= 3.2.1 - 2025-11-03 =
44
* Fix - Exclude shipping-related admin components when shipping functionality is disabled.
5+
* Tweak - Add logging check for incorrect California tax nexus in the TaxJar API response or cached response.
56

67
= 3.2.0 - 2025-10-14 =
78
* Fix - No tax calculated for multi-word state/counties.

classes/class-wc-connect-taxjar-integration.php

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ public function calculate_tax( $options = array() ) {
11421142
$body['line_items'] = $line_items;
11431143
}
11441144

1145-
$response = $this->smartcalcs_cache_request( wp_json_encode( $body ) );
1145+
$response = $this->smartcalcs_cache_request( wp_json_encode( $body ), $from_state );
11461146

11471147
// if no response, no need to keep going - bail early.
11481148
if ( ! isset( $response ) || ! $response ) {
@@ -1427,10 +1427,11 @@ public function validate_taxjar_request( $json ) {
14271427
* See: https://github.com/taxjar/taxjar-woocommerce-plugin/blob/4b481f5/includes/class-wc-taxjar-integration.php#L451
14281428
*
14291429
* @param $json
1430+
* @param $from_state
14301431
*
14311432
* @return mixed|WP_Error
14321433
*/
1433-
public function smartcalcs_cache_request( $json ) {
1434+
public function smartcalcs_cache_request( $json, $from_state ) {
14341435
$cache_key = 'tj_tax_' . hash( 'md5', $json );
14351436
$zip_state_cache_key = false;
14361437
$request = json_decode( $json );
@@ -1440,17 +1441,33 @@ public function smartcalcs_cache_request( $json ) {
14401441
$zip_state_cache_key = strtolower( 'tj_tax_' . $to_zip . '_' . $to_state );
14411442
$response = get_transient( $zip_state_cache_key );
14421443
}
1443-
$response = $response ? $response : get_transient( $cache_key );
1444+
$response = $response ? $response : get_transient( $cache_key );
1445+
if ( $response && 'CA' !== $from_state ) {
1446+
// If $from_state is not California, we need to check for incorrect California tax nexus.
1447+
try {
1448+
$this->check_for_incorrect_california_tax_nexus( $response['body'], true, $from_state );
1449+
} catch ( Exception $e ) {
1450+
$this->_log( 'Error checking for incorrect California tax nexus: ' . $e->getMessage() );
1451+
}
1452+
}
14441453
$response_code = wp_remote_retrieve_response_code( $response );
14451454
$save_error_codes = array( 404, 400 );
14461455

14471456
// Clear the taxjar notices before calculating taxes or using cached response.
14481457
$this->notifier->clear_notices( 'taxjar' );
14491458

14501459
if ( false === $response ) {
1451-
$response = $this->smartcalcs_request( $json );
1452-
$response_code = wp_remote_retrieve_response_code( $response );
1453-
$body = json_decode( wp_remote_retrieve_body( $response ) );
1460+
$response = $this->smartcalcs_request( $json );
1461+
$response_code = wp_remote_retrieve_response_code( $response );
1462+
$body = json_decode( wp_remote_retrieve_body( $response ) );
1463+
if ( 'CA' !== $from_state ) {
1464+
// If $from_state is not California, we need to check for incorrect California tax nexus.
1465+
try {
1466+
$this->check_for_incorrect_california_tax_nexus( $body, false, $from_state );
1467+
} catch ( Exception $e ) {
1468+
$this->_log( 'Error checking for incorrect California tax nexus: ' . $e->getMessage() );
1469+
}
1470+
}
14541471
$is_zip_to_state_mismatch = (
14551472
isset( $body->detail )
14561473
&& is_string( $body->detail )
@@ -1594,4 +1611,38 @@ public function load_taxjar_admin_new_order_assets() {
15941611
// Load Javascript for WooCommerce new order page
15951612
wp_enqueue_script( 'wc-taxjar-order', $this->wc_connect_base_url . 'woocommerce-services-new-order-taxjar-' . WC_Connect_Loader::get_wcs_version() . '.js', array( 'jquery' ), null, true );
15961613
}
1614+
1615+
/**
1616+
* Check for incorrect California tax nexus in the TaxJar API response or cached response.
1617+
*
1618+
* @param $response_body
1619+
* @param $cached
1620+
*
1621+
* @return void
1622+
*/
1623+
private function check_for_incorrect_california_tax_nexus( $response_body, $cached, $from_state ): void {
1624+
$log_suffix = 'in TaxJar API response.';
1625+
1626+
if ( $cached ) {
1627+
$response_body = json_decode( $response_body );
1628+
$log_suffix = 'in cached response.';
1629+
}
1630+
1631+
$to_state = $response_body->tax->jurisdictions->state;
1632+
$to_country = $response_body->tax->jurisdictions->country;
1633+
$has_nexus = $response_body->tax->has_nexus;
1634+
1635+
if ( 'CA' === $to_state && 'US' === $to_country && true === $has_nexus ) {
1636+
$this->_log(
1637+
sprintf(
1638+
'Incorrect California tax nexus detected %s (from_state: %s, to_state: %s, to_country: %s, has_nexus: %s).',
1639+
$log_suffix,
1640+
$from_state ?: 'unknown',
1641+
$to_state,
1642+
$to_country,
1643+
$has_nexus ? 'true' : 'false'
1644+
)
1645+
);
1646+
}
1647+
}
15971648
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ This plugin relies on the following external services:
7272

7373
= 3.2.1 - 2025-11-03 =
7474
* Fix - Exclude shipping-related admin components when shipping functionality is disabled.
75+
* Tweak - Add logging check for incorrect California tax nexus in the TaxJar API response or cached response.
7576

7677
= 3.2.0 - 2025-10-14 =
7778
* Fix - No tax calculated for multi-word state/counties.

tests/php/classes/test-class-wc-rest-connect-shipping-label-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,7 @@ private function set_origin_address( $address = array() ) {
541541
*/
542542
private function set_destination_address( WC_Order $order, $address = array() ) {
543543
$order->set_address( $address, 'shipping' );
544+
545+
$order->save(); // Required for WC 10.3+ HPOS compatibility
544546
}
545547
}

0 commit comments

Comments
 (0)