@@ -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}
0 commit comments