Repository navigation
SQUARE-367: Skip line item taxes whose rate is missing from the order - #602
iamdharmesh wants to merge 2 commits into
Conversation
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
Test the buildWordPress 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:
Note: the build is updated when a new commit is pushed to this PR. |
✅ WordPress Plugin Check Report
📊 ReportAll checks passed! No errors or warnings found. 🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check |
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
ff9e98b to
8a45e6a
Compare
There was a problem hiding this comment.
🟢 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
All Submissions:
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.Orders::get_order_tax_for_rate_id(), looks up the Square tax for a rate ID and returnsnullwhen the order has none. All three rate lookups inOrders.phpnow use it: product, fee and shipping line items, the shipping service charge, andapply_taxes().reconcile_order_total()step then adjusts the Square order, so the customer is still charged the WooCommerce order total.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.The helper does not cast rate IDs to
int, unlike the old shipping lookup. PHP already treats"5"and5as the same array key. An(int)cast turns a string rate ID such as"AVATAX-CA-STATE-TAX"into0, 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:
Ordersis notfinal. This PR adds twoprotectedmethods:get_order_tax_for_rate_id()andlog_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.
4111 1111 1111 1111.trunk: the renewal fails. WooCommerce > Status > Logs showsUncaught Error: Call to a member function getUid() on nullfromOrders.php.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.Changelog entry