Design an Invoice & Billing System

Medium45 min
1 / 30
understanding•10 min read

Problem Statement: Invoices Are Legal Documents, Not Receipt Emails

Frames the invoice and billing system as a money-correct, compliance-bound financial subsystem rather than a PDF printer attached to checkout.

Problem statement

Design an invoice and billing system for a large e-commerce platform. After every checkout the system must generate an itemized invoice containing line items, taxes, shipping fees, and discounts, render a durable PDF or digital format for customer and accounting reference, handle partial or split payments, and store records for the legally required retention period. For business customers it must integrate with AP/AR workflows so that purchases land in the buyer's accounting system and the seller's receivables ledger.

The brief deliberately calls out four capabilities: itemized invoice generation post-checkout, taxes plus shipping plus discounts, PDF or digital format, and partial or split payments. Each one must appear explicitly in the design.

Why this problem is harder than it looks

Most candidates treat an invoice as a formatted email attachment. In reality an invoice is a financial and often legal document. Five properties make the design distinctive:

  1. Money correctness. Totals must reconcile exactly: the sum of line amounts, discounts, taxes, and shipping equals the amount charged, the amount recorded in the ledger, and the amount collected from the payment gateway. A one-cent rounding mismatch between the cart, the invoice, and the ledger is a reconciliation break that finance teams investigate for hours.
  1. Immutability. Once issued, an invoice cannot be silently edited. Corrections happen through credit notes or replacement invoices, both of which reference the original. Many jurisdictions mandate sequential, gap-free invoice numbering, which turns numbering into a distributed systems problem.
  1. Tax jurisdiction complexity. A single order can span state, county, city, and special-district tax authorities. Sales tax in the United States alone has more than 13,000 jurisdictions, and the European Union runs VAT with country-specific rates, reverse-charge rules for B2B, and emerging real-time e-invoicing mandates. Tax must be computed against the rules in force at the time of supply and preserved as a snapshot on the invoice.
  1. Payment lifecycle separation. An invoice is a claim for money; a payment is the settlement of that claim. Partial payments, split payments across methods, refunds, chargebacks, and write-offs mean the invoice carries a balance that evolves over weeks while the document itself never changes.
  1. Compliance retention. Tax authorities in many countries require invoice records for 6 to 10 years. GDPR-style privacy regimes conflict with that retention duty, so the design must encode retention classes, not a single TTL.

The four planes of the system

A strong answer separates the architecture into four planes:

  • Transaction plane: the checkout event that triggers invoicing, the order data it carries, and the guarantee that every paid order produces exactly one invoice.
  • Financial plane: the double-entry ledger postings, accounts receivable balance, payment allocation, credit notes, and reconciliation with gateway settlement files.
  • Compliance plane: tax calculation and snapshotting, sequential numbering, e-invoicing mandates, audit trails, and retention policy.
  • Delivery plane: PDF rendering, storage, download links, email dispatch, and webhooks into the merchant's ERP or accounting stack.

The cloud can delay a PDF; it cannot delay correctness. The ledger and the invoice totals must be right at commit time, even if rendering and email are eventually consistent background work.

Key Highlights

  • •An invoice is an immutable legal-financial document; corrections flow through credit notes, never silent edits.
  • •Totals must reconcile across cart, invoice, ledger, and gateway: rounding rules are an architecture decision.
  • •Invoice lifecycle and payment lifecycle are separate state machines linked by balance allocation.
  • •Tax is computed against time-of-supply rules and frozen as a snapshot on the invoice.
  • •Retention is measured in years (6-10) and encoded as storage classes, not a single TTL.
Lead With the Legal Document Frame
State in the first two minutes that an invoice is an immutable legal-financial document, not an email attachment. It instantly separates you from candidates who draw a PDF printer and move on.
Do Not Merge Invoice and Payment State
A single enum mixing ISSUED, PAID, and RENDERED collapses two independent lifecycles. Model invoice state, payment state, and render state separately and join them through balance.

Section Rescue Kit

Buzzwords to use:

Time of SupplyCredit Note

Safe statements:

  • "I will separate the financial correctness path, which must be synchronous, from rendering and delivery, which can be eventually consistent."
  • "Before drawing services, let me define what an invoice actually is legally, because that decides immutability, numbering, and retention."
Design an Invoice & Billing System - System Design | WinJob | WinJob