Skip to content

SQUARE-367: Skip line item taxes whose rate is missing from the order - #602

Open
iamdharmesh wants to merge 2 commits into
trunkfrom
fix/SQUARE-367
Open

iamdharmesh wants to merge 2 commits into
trunkfrom
fix/SQUARE-367

Conversation

@iamdharmesh

@iamdharmesh iamdharmesh commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

All Submissions:

  • Does your code follow the WooCommerce Sniffs variant of WordPress coding standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Will this change require new documentation or changes to existing documentation?

Changes proposed in this Pull Request:

Tax plugins such as Avalara AvaTax and TaxJar can leave a line item tax that points to a tax rate the order has no tax item for. When the plugin builds the Square order, it looked up that rate without checking it exists. PHP then stopped with Uncaught Error: Call to a member function getUid() on null. Subscription renewals failed in the background.

  • A new helper, Orders::get_order_tax_for_rate_id(), looks up the Square tax for a rate ID and returns null when the order has none. All three rate lookups in Orders.php now use it: product, fee and shipping line items, the shipping service charge, and apply_taxes().
  • When a line item tax has no matching order tax, the plugin skips that tax and builds the rest of the Square order. The existing reconcile_order_total() step then adjusts the Square order, so the customer is still charged the WooCommerce order total.
  • A new Orders::log_missing_order_tax() writes the order ID, item ID, item name and rate ID to the Square log. Like the plugin's other logs, it writes only when Square Debug Mode logging is on.
  • New unit tests cover the skip, the log, a mix of found and missing rates, the shipping fallback, and the full create order request with an AvaTax-style string rate ID.

The helper does not cast rate IDs to int, unlike the old shipping lookup. PHP already treats "5" and 5 as the same array key. An (int) cast turns a string rate ID such as "AVATAX-CA-STATE-TAX" into 0, and that can match an unrelated tax.

Admin capture: capturing a held payment with a changed amount rebuilds the Square order through the same code, but it does not reconcile the total. Before this change, a missing rate there caused a fatal error. Now Square may reject the capture with an API error that the admin sees. This change does not address that older gap.

Backward compatibility: Orders is not final. This PR adds two protected methods: get_order_tax_for_rate_id() and log_missing_order_tax(). No existing method signature, hook, or persisted data changes. A third-party subclass that already defines a method with one of these names and a different signature would conflict. No subclass exists in this repository.

This also fixes the reports in SQUARE-78 (renewals with AvaTax), SQUARE-193 (block checkout) and SQUARE-152 (TaxJar with Google Pay).

Closes https://linear.app/a8c/issue/SQUARE-367/fatal-error-unguarded-tax-rate-lookup-in-ordersphp-breaks-renewals-and

Steps to test the changes in this Pull Request:

Setup: connect Square in sandbox mode and enable the Square credit card gateway. Activate WooCommerce Subscriptions. Turn on taxes in WooCommerce > Settings > General. Install Avalara AvaTax, connect a sandbox account and turn on tax calculation. In the Square settings, set Debug Mode to an option that saves errors to the log.

  1. Create a taxable simple subscription product.
  2. Check out with a billing address in California, US. Pay with the Square test card 4111 1111 1111 1111.
  3. Deactivate the Avalara AvaTax plugin.
  4. Open the subscription and run the Process renewal order action.
  5. On trunk: the renewal fails. WooCommerce > Status > Logs shows Uncaught Error: Call to a member function getUid() on null from Orders.php.
  6. On this branch: the renewal order is paid and moves to Processing. The Square log has a line like Order #123: item #45 ("Product") has tax for rate ID …, but the order has no tax item for that rate. Skipping this tax in the Square order.
  7. In the Square sandbox dashboard, confirm the renewal payment amount equals the WooCommerce renewal order total.
  8. Regression check: deactivate AvaTax, add a standard WooCommerce tax rate, and buy a taxable product. Confirm the Square order shows the tax line, the total matches the WooCommerce order total, and the log has no missing-rate line.

Changelog entry

Fix - Checkout and subscription renewals no longer fail with a fatal error when a tax plugin leaves a line item tax that has no matching order tax.

Tax plugins such as Avalara AvaTax and TaxJar can leave a line item
tax that points to a rate ID with no matching tax item on the order.
Building the Square order then failed with "Call to a member function
getUid() on null". This broke subscription renewals, block checkout,
and classic checkout.

Add get_order_tax_for_rate_id() and use it for every tax lookup by
rate ID. When the rate is missing, log the order, item, and rate ID,
and skip that tax. The existing order total adjustment keeps the
Square total equal to the WooCommerce total.

Add unit tests for the missing-rate cases.

Refs SQUARE-367
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Test the build

WordPress Playground - runs in your browser

ℹ️ Neither can connect to Square: signing in to Square has to send you back to the site, and a site that runs in your browser tab has no public address to come back to. Without a connection the store cannot take payments, sync products or receive webhooks, so the previews show what a merchant sees before connecting. Do not enter Square access tokens or other credentials in a preview. The links stop working 14 days after the last build.


Build info:

  • Latest commit: 8a45e6a
  • Build time: 2026-11-17 14:21:21 UTC

Note: the build is updated when a new commit is pushed to this PR.

@github-actions

Copy link
Copy Markdown

✅ WordPress Plugin Check Report

✅ Status: Passed

📊 Report

All checks passed! No errors or warnings found.


🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

@iamdharmesh iamdharmesh self-assigned this Nov 17, 2026
Classic checkout, block checkout, and subscription renewals all build
the Square order through set_create_order_data(). Add a test that runs
that entry point with an AvaTax-style string rate ID that the order has
no tax item for.

Refs SQUARE-367

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the missing-tax-rate handling changes.

No open issues 🎉

What changed in this PR

This PR prevents missing order tax rates from causing fatal errors while building Square orders, addressing checkout and subscription renewal failures involving tax plugins such as AvaTax and TaxJar.

Changes:

  • Adds a shared tax lookup helper and guards unresolved rates without casting string IDs to integers.
  • Skips unresolved line-item taxes and logs diagnostic details when Square debug logging is enabled.
  • Adds regression coverage for missing, resolved, mixed and string rate IDs, plus shipping fallback behavior.
File Description
tests/​php/​Unit/​Gateway/​Orders_Tax_Lookup_Test.php Tests guarded tax lookups, logging calls, shipping fallback and order request construction.
tests/​php/​Unit/​Gateway/​Fixtures/​Orders_Tax_Lookup_Test_Request.php Exposes protected request methods and records missing-rate diagnostics for tests.
includes/​Gateway/​API/​Requests/​Orders.php Centralizes tax lookup, guards missing rates and adds debug-gated diagnostics.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants