Prompt
I'm trying to build an integration that syncs invoices, payments, and ledger entries into accounting software. What architecture should I use?
Latest observation
For this kind of integration, a canonical-ledger + event-driven sync architecture is usually the safest and most scalable choice.
Recommended architecture
1) Use your own canonical data model
Don’t map your domain objects directly to each accounting system’s API shape.
Create internal entities like:
- Invoice
- Payment
- LedgerEntry / JournalEntry
- Customer / Contact
- Tax / LineItem / Account mapping
Your system becomes the source of truth for transformation and sync state.
2) Treat accounting software as a downstream projection
Instead of “pushing business objects” directly, think of the accounting system as a readable projection of your canonical ledger.
That means:
- Your app records the business event
- You translate it into accounting-ready entries
- You sync those entries to the target software
- You track what was successfully posted
This works well because accounting systems often have different rules for:
- invoice lifecycle
- payment application
- rounding
- tax treatment
- double-entry bookkeeping
- deletion/voiding behavior
3) Use an event-driven pipeline
A good flow is:
-
Source event occurs
- invoice created
- payment received
- refund issued
- invoice updated
- ledger adjustment posted
-
Write to internal DB
- store business object
- store an immutable event or change log
- create an outbox record for sync
-
Async workers process sync jobs
- validate
- transform to accounting payload
- resolve mappings
- call accounting API
- store external ID and sync status
-
Reconcile and retry
- retry transient failures
- surface permanent mapping errors
- periodically reconcile local vs remote state
This avoids blocking your core app on external API latency or outages.
Core components
A) Canonical ledger service
Responsible for:
- financial event normalization
- double-entry journal creation
- maintaining balances
- idempotency keys
- audit trail
This is the most important layer if you need reliable accounting.
B) Integration orchestration service
Responsible for:
- deciding what needs to sync
- handling retries
- managing webhook callbacks
- tracking external IDs
- per-tenant credentials and connection health
C) Mapper / transformer layer
Responsible for converting canonical objects into:
- QuickBooks / Xero / NetSuite / Sage payloads
- account codes
- tax codes
- customer/contact formats
- line item semantics
Keep this isolated per provider.
D) Sync state store
Track:
- internal object ID
- external object ID
- provider
- sync status
- last successful sync
- last error
- version/hash of synced payload
- retry count
This is essential for idempotency and reconciliation.
E) Webhook and reconciliation layer
Use both:
- webhooks for near-real-time updates from accounting software
- scheduled reconciliation jobs to detect drift, missed events, or manual edits
Webhooks alone are not enough.
Important design principles
1) Idempotency everywhere
Accounting APIs may be called multiple times due to retries.
Use:
- idempotency keys
- deterministic external references
- upsert behavior where supported
- sync journal entries with stable identifiers
2) Prefer append-only accounting events
Avoid editing historical posted entries whenever possible.
Instead:
- issue reversals
- create adjustment entries
- mark invoices void/cancelled if supported
This preserves auditability.
3) Separate business state from accounting state
An invoice can be:
- created in your product
- partially paid
- fully paid
- synced
- reconciled
- manually adjusted in accounting software
These are related but not the same state.
4) Make mappings explicit
Accounting integrations often fail because of ambiguous mappings:
- which account receives revenue?
- which tax code applies?
- how to map discounts?
- where to post fees?
- how to handle foreign currency?
Store tenant-specific configuration and validate it early.
Suggested high-level flow
App UI / API
↓
Core domain service
↓
Canonical DB + outbox/event log
↓
Sync worker / integration orchestrator
↓
Provider adapter (QuickBooks/Xero/etc.)
↓
Accounting software
↑
Webhook / polling / reconciliation
When to use direct sync vs ledger-first
Use ledger-first if:
- you care about auditability
- you support payments, refunds, taxes, multi-currency
- you need multiple accounting destinations
- you expect scale or complex edge cases
Direct sync can work if:
- the product is simple
- only one accounting provider matters
- invoices are the only object being synced
- correctness/audit requirements are low
For most serious finance integrations, ledger-first is the better foundation.
Data model recommendation
At minimum, store:
tenant_idsource_object_typesource_object_idproviderexternal_idexternal_versionsync_statussync_errorpayload_hashcreated_at,updated_atlast_synced_at
And keep:
- immutable event history
- mapping tables for accounts, taxes, contacts, products
Operational concerns
Build for:
- rate limits
- API pagination
- partial failures
- eventual consistency
- backfills
- tenant reauth / token refresh
- manual remapping and re-sync
Practical recommendation
If you’re starting from scratch, I’d build:
- Canonical finance model
- Outbox-based event pipeline
- Provider adapter interface
- Sync state and mapping tables
- Webhook + reconciliation jobs
- Double-entry ledger for internal correctness
If you want, I can also sketch:
- a reference architecture diagram
- a database schema
- or a provider adapter interface for QuickBooks/Xero/NetSuite.