Check VAT ID validity via VIES for any country, not just Österreich

VAT-ID validity and the exemption decision are separate questions.
Previously VIES was only ever called when the destination already
qualified for the cross-border exemption (Österreich), so a garbage
VAT ID on a domestic order (e.g. "ED123456789" — not even a real
country code) sailed through with no feedback at all, and a
Deutschland/Schweiz customer got no confirmation their real VAT ID
was valid either. Now VIES checks any format-valid VAT ID regardless
of destination (data quality, same reasoning as company-settings'
own check) — the exemption itself still only applies when the
destination is also Österreich, a validated German VAT ID never
zero-rates a domestic sale. The status message now always shows
("✓ USt-IdNr. bestätigt", plus the exemption note only when it
actually applies) instead of staying hidden for non-Österreich
orders.

Also added maxLength to PLZ (per-country digit count) and USt-IdNr.
(14) checkout fields — they had pattern validation but nothing
stopping the browser from accepting more characters than could ever
be valid.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Marco
2026-07-23 19:37:29 +00:00
parent ba4d7b443f
commit 4d2e78dd2a
2 changed files with 49 additions and 28 deletions
+19 -10
View File
@@ -209,22 +209,31 @@ export async function POST(request: Request) {
);
}
// Innergemeinschaftliche Lieferung (§4 Nr. 1b UStG) — only for the goods'
// actual destination (the shipping override's country when set, the
// billing country otherwise) being Österreich, the one EU-cross-border
// option this checkout offers, AND a VAT ID that VIES itself confirms is
// currently registered right now, at the moment of purchase — a merely
// format-valid id is never enough (see lib/vatExemption.ts's own
// comment). VIES being unreachable fails closed: normal VAT applies,
// never a guessed exemption.
// VAT-ID validity and the exemption decision are two separate questions.
// Validity (is this actually a currently-registered VAT ID at all) is
// checked via VIES for ANY country whenever one is given — worth
// recording regardless of destination, same "data quality" reasoning as
// company-settings.vatId's own VIES check on the backend; a merely
// format-valid id (e.g. "ED123456789" — "ED" isn't even a real country
// code) is never enough on its own. The exemption itself
// (innergemeinschaftliche Lieferung, §4 Nr. 1b UStG) additionally
// requires the goods' actual destination (the shipping override's
// country when set, the billing country otherwise) to be Österreich,
// the one EU-cross-border option this checkout offers — a validated
// *German* VAT ID never zero-rates a domestic sale, no matter how real
// it is. VIES being unreachable fails closed on the exemption: normal
// VAT applies, never a guessed exemption (vatIdValidatedAt just stays
// unset in that case too).
let vatExempt = false;
let vatIdValidatedAt: string | null = null;
const buyerDestinationCountry = destinationCountry(body.country, Boolean(body.hasDifferentShippingAddress), body.shippingCountry);
if (normalizedVatId && isExemptionEligibleCountry(buyerDestinationCountry)) {
if (normalizedVatId) {
const viesResult = await checkVatIdViaVies(normalizedVatId);
if (viesResult.ok && viesResult.valid) {
vatExempt = true;
vatIdValidatedAt = new Date().toISOString();
if (isExemptionEligibleCountry(buyerDestinationCountry)) {
vatExempt = true;
}
}
}