Generating an invoice sounds easy.
You have a customer, a few line items, a price, maybe a sales tax rate, and a button that generates a PDF.
Done, right?
Well... Not quite.
InvoiceCast was launched in 2018 as a free online invoice generator. However, once an invoicing feature moves beyond a weekend project and starts being used by real businesses, things become considerably more interesting. You need immutable invoice numbers, rounding rules, currencies, PDF generation, email delivery, recurring invoices, audit history and — depending on where your users live — completely different tax models. Things become even more interesting once you add payments via a platform like Stripe.
In this article, I want to look at the engineering problems behind a modern invoicing system and the architecture I would use to build one today.
1. An invoice is a financial record, not just a database row
A common first implementation might look something like this:
invoices
---------
id
customer_id
amount
tax
total
status
created_at
That works until something changes.
What happens if the customer changes their address?
What if a product price changes?
What if the tax rate changes next year?
An existing invoice shouldn't suddenly change because related records were updated.
For important invoice information, storing a snapshot is usually safer:
invoice_number
customer_name
customer_address
customer_tax_number
subtotal
tax_total
total
currency
issued_at
due_at
Individual line items should also preserve their description, quantity, price and tax configuration at the time the invoice was created.
The database should represent what was invoiced at that moment, not what the customer's profile looks like today. This might be a legal requirement not just a UX feature.
2. Invoice numbers need their own logic
Using your primary database ID as an invoice number is tempting:
1001
1002
1003
But invoice numbering often has business and regulatory requirements.
Users may expect:
INV-2026-001
INV-2026-002
INV-2026-003
Or:
CONSULTING-1042
Invoice numbers may need to be unique per business rather than globally across the application.
If your SaaS supports multiple organizations, the uniqueness constraint might effectively become:
UNIQUE(business_id, invoice_number)
You also need to think carefully about race conditions.
Two workers creating an invoice at exactly the same time should never receive the same number.
Database transactions and locking are your friends here.
3. Taxes get complicated very quickly
One of the biggest mistakes is treating taxes as simply:
$tax = $subtotal * 0.15;
That might work for a prototype.
Real-world taxation can depend on:
- seller location;
- customer location;
- type of product or service;
- tax registration status;
- jurisdiction;
- exemptions;
- multiple simultaneous taxes;
- effective date of the tax rate.
Canada provides a particularly good example.
Depending on the province you may deal with GST, HST, PST or QST.
An invoice in Quebec, for example, can contain both GST and QST rather than a single combined tax.
This is why a tax engine is better modeled as a set of tax rules rather than one tax_rate column.
Something like:
TaxRule
jurisdiction
name
rate
valid_from
valid_until
calculation_method
A production application should also preserve the actual tax rule used on the invoice instead of recalculating an old invoice using today's rates.
This is one of the problems I have been dealing with while building InvoiceCast, an invoicing SaaS for freelancers, contractors and small businesses.
The Canadian version has a somewhat different emphasis because InvoiceCast Canada includes GST/HST/QST/PST formulas for Canadian provinces and territories.
Building those two use cases is a good demonstration of why seemingly simple geographical differences quickly influence your domain model.
4. Money should never be stored as a float
This deserves its own section.
Don't do this:
$total = 10.10 + 20.20;
Binary floating-point arithmetic can produce surprising results.
For financial applications, use either:
- fixed-precision decimal database fields; or
- integer minor units such as cents.
For example:
$10.25 = 1025 cents
Then:
$total = 1025 + 2500;
becomes completely deterministic.
If you're supporting several currencies, don't assume that every currency has exactly two decimal places either.
Money is one of those areas where boring code is good code.
5. PDF generation should happen asynchronously
Generating a PDF while the user waits for the HTTP request to finish works fine at first.
Eventually it becomes painful.
A better workflow looks something like:
Create invoice
↓
Commit transaction
↓
Dispatch InvoiceCreated event
↓
Queue PDF generation
↓
Store PDF
↓
Queue email
↓
Send invoice
In Laravel, for example, this maps nicely to events, listeners and queued jobs.
Redis can handle the queue and workers can process PDF generation independently.
This also makes retries much easier.
If your email provider is temporarily unavailable, you don't want the entire invoice creation request to fail.
6. Email status is part of the product
Sending an email isn't the same as delivering an email.
A useful invoicing system should distinguish between states such as:
queued
sent
delivered
opened
bounced<br>sofrt_bounced
failed
Most transactional email providers expose delivery events through webhooks. Your application can consume those events and update the invoice accordingly.
Suddenly a simple "Send invoice" button becomes a small event-driven system. But that's a good thing. The user can now answer a useful question:
Did my client actually receive the invoice?
7. Payments should be idempotent
Payment webhooks create another interesting engineering problem.
Providers such as Stripe may deliver the same event more than once.
Your webhook handler therefore needs to be idempotent.
Conceptually:
if (PaymentEvent::where('provider_id', $event->id)->exists()) {
return;
}
Then process the event inside a transaction.
You never want the same payment callback to mark something twice, generate duplicate records or accidentally trigger multiple workflows.
8. Recurring invoices aren't really invoices
A recurring invoice is better thought of as an invoice template plus a schedule.
For example:
RecurringInvoice
customer_id
frequency
next_run_at
template
enabled
A scheduler periodically looks for records where:
next_run_at <= NOW()
and generates actual invoices.
That separation is important.
The recurring definition can change while previously generated invoices remain immutable.
9. Don't forget the audit trail
Financial applications benefit enormously from an event history.
Instead of only knowing:
status = paid
you want to be able to reconstruct:
Invoice created
Invoice emailed
Email delivered
Invoice viewed
Payment initiated
Payment completed
Invoice marked paid
This helps users, support staff and developers.
It also makes debugging substantially easier.
10. The interesting part isn't generating the invoice
The actual invoice document might be the easiest part of the entire system.
The challenging parts are everything around it:
- financial precision;
- immutable historical data;
- concurrency;
- taxation;
- asynchronous processing;
- email delivery;
- payment events;
- recurring jobs;
- auditability.
That's true of many SaaS products.
The feature users perceive as one button often represents half a dozen systems working together behind the scenes.
And that is exactly what makes building SaaS interesting.