Converting Stripe Invoices to German XRechnung / EN 16931 XML: Fixing the 1-Cent Rounding Bug and Missing BR-DE Fields
If your B2B SaaS bills German customers through Stripe, you have probably started hearing from their accounting teams about XRechnung or ZUGFeRD ahead of Germany's January 1, 2027 e-invoicing milestone. Quick legal cont
If your B2B SaaS bills German customers through Stripe, you have probably started hearing from their accounting teams about XRechnung or ZUGFeRD ahead of Germany's January 1, 2027 e-invoicing milestone.
Quick legal context so the timeline is clear:
- Since Jan 1, 2025, all German businesses must be able to receive structured EN 16931 e-invoices.
- Starting Jan 1, 2027, German companies with more than β¬800,000 in prior-year revenue must issue structured domestic B2B e-invoices (all remaining German B2B firms follow on Jan 1, 2028).
- Even if your SaaS company sits outside Germany, German AP teams automating DATEV and sevdesk increasingly push back on plain Stripe PDFs and ask for EN 16931 XML instead.
When I built a Stripe Invoice JSON to EN 16931 CII XML exporter and ran the output against the official German KoSIT test suite, four non-obvious bugs broke validation immediately. Here is what failed and how to fix each one in code.
1. Tax-inclusive line.amount gets double-taxed if you treat it as net
In European B2B SaaS pricing, tax-inclusive (Bruttopreise) prices are everywhere. In Stripe's Invoice JSON, when a tax rate has inclusive: true, line.amount already includes VAT.
If your converter assumes line.amount is net and multiplies by 1.19 on top, you silently double-tax the XML. Worse, an invoice can have invoice.default_tax_rates set to inclusive: true while a specific line overrides line.tax_rates with inclusive: false (or a reduced 7% rate).
Always resolve tax rates per line first, falling back to invoice.default_tax_rates only when line.tax_rates is empty, and run every calculation in pure BigInt integer cents (never IEEE-754 floats):
// Pure BigInt HALF_UP division for exact cent arithmetic
function divRoundHalfUp(num, den) {
const neg = (num < 0n) !== (den < 0n);
const a = num < 0n ? -num : num;
const b = den < 0n ? -den : den;
const q = a / b;
const r = a % b;
const rounded = r * 2n >= b ? q + 1n : q;
return neg ? -rounded : rounded;
}
// Back-calculate net cents from a tax-inclusive Stripe line amount
// Example: β¬19.99 (1999 cents) incl. 19% VAT -> 1680 cents net (β¬16.80) + 319 cents VAT (β¬3.19)
function netCentsFromInclusive(grossCents, vatRateBps /* e.g. 1900n for 19.00% */) {
const num = grossCents * 10000n;
const den = 10000n + vatRateBps;
return divRoundHalfUp(num, den);
}
Check β¬19.99 at 19%:
-
netCentsFromInclusive(1999n, 1900n)=1680n(β¬16.80) -
VAT=1999n - 1680n=319n(β¬3.19) -
16.80 + 3.19 = 19.99exact to the cent.
2. The 1-cent multi-unit rounding trap (BR-CO-15 vs BR-CO-16)
This one catches almost every homegrown exporter. Suppose a customer buys 3 seats at β¬10.00 gross (19% VAT included), so Stripe charges β¬30.00:
- Per line net:
β¬10.00 / 1.19 = β¬8.40336...-> rounds toβ¬8.40. - Sum of 3 line nets (
BT-106/BT-109):3 Γ β¬8.40 = β¬25.20. - Under EN 16931, document VAT (
BT-110) is computed on the summed net per tax rate:19% Γ β¬25.20 = β¬4.788-> rounds toβ¬4.79. - Now add them up:
BT-109 (β¬25.20) + BT-110 (β¬4.79) = β¬29.99, while Stripe actually chargedβ¬30.00.
If you naively write BT-112 = 30.00 into the XML, KoSIT hard-rejects the invoice with BR-CO-15 because BT-112 must strictly equal BT-109 + BT-110 with zero tolerance.
How to handle it cleanly:
-
BT-114(RoundingAmount) does not bypassBR-CO-15. It feeds intoBT-115(DuePayableAmount, checked byBR-CO-16). - So you set
BT-112 = BT-109 + BT-110(29.99) soBR-CO-15passes, then setBT-114 = +0.01soBT-115 = BT-112 - BT-113 + BT-114 = 30.00matches what Stripe actually charged. - Alternatively, if your lines are distinct items, you can allocate the 1-cent remainder on the largest line net (
25.21 + 4.79 = 30.00), as long as each line still satisfieslineNet = qty * unitPrice - allowance + charge.
Also watch the UN/CEFACT CII D16B XSD element sequence inside SpecifiedTradeSettlementHeaderMonetarySummation:
-
ChargeTotalAmountmust appear beforeAllowanceTotalAmount -
RoundingAmount(BT-114) must sit betweenTaxTotalAmount(BT-110) andGrandTotalAmount(BT-112) or right beforeDuePayableAmountdepending on the exact D16B schema order (in UN/CEFACT CII D16BTradeSettlementHeaderMonetarySummationType, the strict order is:LineTotalAmount->ChargeTotalAmount->AllowanceTotalAmount->TaxBasisTotalAmount->TaxTotalAmount->RoundingAmount->GrandTotalAmount->TotalPrepaidAmount->DuePayableAmount).
Putting RoundingAmount after GrandTotalAmount fails XSD validation before Schematron even runs.
3. Mandatory German BR-DE-* fields missing from default Stripe JSON
Even when your math is 100% green under core EU EN 16931, Germany's XRechnung 3.0 CIUS rejects standard Stripe exports for missing metadata:
-
BR-DE-6(BT-42): Missing seller contact telephone number. (Note:BR-DE-2fires when the entireBG-6seller contact group is missing; if name and email exist but only the phone number is empty, KoSIT throwsBR-DE-6.) -
BR-DE-1(BG-16): Missing payment instructions / explicit UNTDID 4461 payment means code (such as58for SEPA credit transfer or48/54for card). -
BR-DE-15(BT-10): Missing buyer reference (BuyerReference/Leitweg-IDfor German public-sector buyers). A practical tip from enterprise Stripe setups: store this onsubscription.metadata(withcustomer.metadataas fallback), since larger German orgs often use different PO numbers or cost centers per subscription.
4. Don't mix up Germany, France, and Belgium
One compliance trap worth calling out if you sell across Europe:
- Germany (domestic B2B): Decentralized model. Emailing a valid XRechnung or ZUGFeRD (2.0.1+, EN 16931 profile) file is permitted.
- Belgium (domestic B2B): Since Jan 1, 2026, domestic B2B invoicing runs over the Peppol network via certified Access Points.
- France (domestic B2B): Phased rollout since Sep 1, 2026 requires certified private platforms (PDP / Plateformes Agréées).
And remember that generating or validating XML is purely technical formatting: it does not replace a Steuerberater or Germany's GoBD archiving rules (Β§ 147 AO).
Testing your own XML or Stripe JSON
If you run Java in CI, the official open-source KoSIT Validator and Mustangproject are great self-hosted options.
If you want a quick browser check without setting up a JVM pipeline, I built FakturaLab as a lightweight, strictly in-memory (EU Frankfurt RAM-only, zero invoice storage) EN 16931 / XRechnung 3.0 / ZUGFeRD 2.3 validator and Stripe Invoice JSON -> CII XML converter, regression-tested against all 72 official German KoSIT business-case fixtures. The free tier includes 5 validations/day, plus bilingual EN/DE fix guides for the 25 most common error codes.
If you're hitting a weird BR-DE-* or BR-CO-* code in your Stripe export, drop the code in the comments and I'm happy to help trace the XML field.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.