0002: Save documents before calling AvaTax, and never guess tax
Status: Accepted ยท Date: 2026-09-29
Contextโ
Business continuity: when Avalara is down, work must carry on the way it does today. A guessed tax amount is worse than none, and a retried call must not create a second AvaTax transaction.
Decisionโ
The document is stored first. Its provider document code is derived from the caller's natural key (entity,
source system, source document id, direction and kind: TaxDocument.ProviderCodeFor), not from the row id. The
AvaTax call uses CreateOrAdjust with that code. If AvaTax is unavailable, the document stays PendingTax,
TaxCalculationDeferred is published, and a job recalculates it. Estimates return 503 with fallback: "manual".
Revised 2026-10-13. The code used to come from the row id. On the REST path that was enough, because the early
save commits on its own. On the message path it was not: consumers run inside the outbox transaction, so the early
save only commits with the message. If that transaction rolled back after AvaTax was called (pod killed, serialization
failure), the redelivery created a new row with a new id, and AvaTax got a second document. Deriving the code from the
natural key makes every attempt address the same AvaTax document. Rows created before the change keep their stored code.
AvaTaxStubTests.A_redelivered_message_whose_first_attempt_rolled_back_adjusts_the_same_AvaTax_document guards it.
Consequencesโ
- No duplicate AvaTax transactions, even after a crash between the call and the save, on the REST path and the message path alike.
- Callers have to handle
202and the deferred event (the manual tax line). - Reconciliation flags anything still pending after 24 hours.