All articles

Published on ·2 min read·By Manassé N'Dri

Margokit: compliant invoices by design

Gap-free numbering assigned at issuance, frozen invoices corrected with credit notes, per-country tax rules, Factur-X and UBL: how Margokit's code enforces legal obligations.

  • Margokit
  • Next.js
  • MongoDB
  • Factur-X
  • SaaS
Margokit: compliant invoices by design

Context

Margokit is built for online sellers and freelancers: a free profit calculator, and Margokit Pro to create quotes, invoices, deposit invoices and credit notes. The product is live at margokit.com, built with Next.js 16, TypeScript and MongoDB.

An invoice is not just a PDF: it is a legal document. Here is how the code enforces those rules instead of relying on the user to be careful.

1. Continuous numbering, assigned at issuance

A draft has no number. The number is assigned when the document is issued, from an atomic counter scoped to each user, document type and period, inside a transaction. The sequence stays chronological and gap-free, as France and most countries require.

The format is customizable with tokens ({YYYY}, {MM}, {SEQ:4}…), and the counter period follows from it: one sequence per month, per year, or a continuous one.

2. An issued invoice never changes again

At issuance, Margokit freezes a copy of the seller's and the client's details. If the user later edits their profile or the client record, invoices already issued stay exactly the same.

Only a draft can be edited: the database update only goes through if the document is still a draft. A mistake on an issued invoice is fixed with a credit note, and a credit note for the full amount cancels the original invoice. Every issuance is written to the audit log.

3. Checking compliance before issuing

Before a number is assigned, a compliance check verifies the mandatory mentions. A blocking error prevents issuance and states exactly what is missing. A document is never half-issued.

4. Each country's rules in a registry

Tax identifiers, suggested tax rates, small-business VAT exemption, France's €40 late-payment recovery fee between businesses, the e-invoicing network: all of it lives in a per-country rules registry, with dedicated validators (French SIREN and SIRET, French and Belgian VAT, Belgian enterprise number, Moroccan ICE…). Adding a country means adding a tested entry, not scattering conditions through the code.

5. Exact amounts in every currency

Amounts are stored as integer minor units, with each currency's own number of decimals. No rounding error builds up on a multi-line invoice.

6. Ready for e-invoicing

From the same data model, Margokit generates a Factur-X invoice (a PDF with embedded XML) and a UBL file, the two European e-invoicing formats. Transmission to a platform goes through a provider interface: only a test provider exists today, disabled in production. Plugging in a certified platform will mean implementing that interface.

7. Never take a webhook at its word

Subscriptions are sold through Gumroad, whose notifications are not signed. The webhook is therefore protected by a secret URL compared in constant time, processed once per event, and the license is re-checked against Gumroad's API. Billing goes through a provider interface, so the payment solution can change without touching the rest.

Takeaways

  • Legal obligations can be coded: number at issuance, frozen document, corrections through credit notes.
  • What varies by country belongs in configuration, not in conditions scattered through the code.
  • External integrations sit behind interfaces, and their notifications get verified.