Invoicing with web services: VAT, SOAP and GraphQL

Jennifer

Orders from a GraphQL API, EU VAT numbers checked with VIES over SOAP, reverse charge, exact amounts, a PDF per invoice, and what to do when a service is down.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 22

The pipeline

StepTool
Fetch open orders, page by pagegraphql module
Check the customer's VAT number@deckyard/vies over soap
Decide: domestic VAT, reverse charge, or holdyour code
Add up lines, VAT and total@deckyard/money
Write the invoicepdf module
Cache, retry, keep an audit trailflatdb, jsonl

Money basics: Numbers and data. PDF layout: Making PDFs.

2 / 22

SOAP without a WSDL

The soap module reads no WSDL: take the namespace, the operation and its parameters from the service's WSDL by hand.

def const NS as string init
    "urn:ec.europa.eu:taxud:vies:services:checkVat:types";
def op as xml.Value init soap.operation(NS, "checkVat",
    {"countryCode": "DE", "vatNumber": "100"}, true);
io.printf("%s\n", soap.envelope("1.1", [], [$op]));
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body><m:checkVat xmlns:m="urn:ec.europa.eu:taxud:...:types">
  <m:countryCode>DE</m:countryCode><m:vatNumber>100</m:vatNumber>
 </m:checkVat></soap:Body></soap:Envelope>
3 / 22

Calling VIES by hand

def c as soap.Client init soap.withTimeout(soap.connect(TEST), 10000);
for (def n in ["100", "200", "300"]) {
    $op = soap.operation(NS, "checkVat",
        {"countryCode": "DE", "vatNumber": $n}, true);
    def r as soap.Reply init soap.send($c, "", $op);
    if ($r.isFault) {
        io.printf("%s HTTP %d fault %s: %s\n", $n, $r.status,
            $r.fault.code, $r.fault.reason);
    } else { ... soap.values($r.envelope.body[0]) ... }
}
100 HTTP 200 checkVatResponse valid=true 2026-11-19+01:00
200 HTTP 200 checkVatResponse valid=false 2026-11-19+01:00
300 HTTP 200 fault Server: SERVICE_UNAVAILABLE
4 / 22

call: a fault is an error

def res as xml.Value init soap.call($c, "", $op);   # DE 100
io.printf("%s: %s\n", xml.localName($res), soap.values($res)["name"]);
try { soap.call($c, "", $op600); } catch (e) {
    if ($e.kind == soap.ERROR_KIND) {
        def se as soap.SoapError init $e;
        io.printf("%s\nstatus=%d isFault=%t code=%s\n", $se.message,
            $se.status, $se.isFault, $se.fault.code);
    }
}
checkVatResponse: John Doe
soap: fault Server: MS_MAX_CONCURRENT_REQ
status=200 isFault=true code=Server
5 / 22

What @deckyard/vies adds

ATU 123.456-78   -> AT U12345678 well-formed=true
de 123 456 789   -> DE 123456789 well-formed=true
BE 417.497.106   -> BE 0417497106 well-formed=true
vies | vies: "12345678" is not a VAT number of AT
6 / 22

check and checkApprox

def c as vies.Client init vies.sandboxClient();
for (def n in ["100", "200"]) {
    def r as vies.Result init vies.check($c, "DE", $n);
    io.printf("DE%s valid=%t name=[%s] date=%s\n", $r.number,
        $r.valid, $r.name, $r.requestDate);
}
def p as vies.ApproxResult init
    vies.checkApprox($c, "DE", "100", "AT", "U12345678");
io.printf("proof %s on %s\n", $p.requestIdentifier, $p.requestDate);
DE100 valid=true name=[John Doe] date=2026-11-19+01:00
DE200 valid=false name=[] date=2026-11-19+01:00
proof WAPI0000001 on 2026-11-19+01:00
7 / 22

When VIES says no, or not now

CodeMeaningtransient
INVALID_INPUTunknown country, empty numberno
VAT_BLOCKED, IP_BLOCKEDnumber or caller blockedno
SERVICE_UNAVAILABLEVIES itself failedyes
MS_UNAVAILABLEthe member state's register is downyes
TIMEOUT, *_MAX_CONCURRENT_REQtoo slow, too busyyes
(code "", status 503)the maintenance pageyes
vies: checkVat DE300: SERVICE_UNAVAILABLE code=SERVICE_UNAVAILABLE
  transient=true status=200
8 / 22

What a valid VAT ID enables

In general, within the EU, for a supplier in one member state:

Not legal advice: national rules, thresholds and exceptions apply.

9 / 22

The decision as code

func treatment(country as string, c as Check) {
    if ($country == SELLER_CC) {
        return Treatment{kind: "domestic", rate: VAT_RATE, ...};
    }
    if ($c.status == "valid") {
        return Treatment{kind: "reverse-charge", rate: "0", ...,
            note: "Reverse charge: VAT is due from the recipient."};
    }
    if ($c.status == "invalid") {
        return Treatment{kind: "no-valid-id", rate: VAT_RATE, ...};
    }
    return Treatment{kind: "held", rate: "", percent: "", note: ""};
}
10 / 22

GraphQL: the query is a raw string

def const ORDERS as string init
'query Orders($first: Int!, $after: String) {
  orders(status: OPEN, first: $first, after: $after) {
    nodes { number date customer { name country vatId }
            lines { description qty unitPrice currency } }
    pageInfo { hasNextPage endCursor }
  }
}';

A "..." string interpolates {...}: GraphQL in one does not even parse.

trap2.j: parse error at trap2.j:2:40: expected COLON after struct
  field name, got RBRACE ("}")
  def q as string init "{ viewer { login } }";
11 / 22

Every page, until there is no next

while (true) {        # vars starts as {"first": 3}
    def conn as json.Value init json.get(
        graphql.query($gql, ORDERS, $vars), "/data/orders");
    # ... append each entry of /nodes to $nodes
    if (not json.asBool($conn, "/pageInfo/hasNextPage")) {
        return $nodes;
    }
    $vars = json.set($vars, "/after",
        json.asString($conn, "/pageInfo/endCursor"));
}
Orders {"first":3}
Orders {"first":3,"after":"b3JkZXI6Mg=="}
Orders {"first":3,"after":"b3JkZXI6NQ=="}
12 / 22

Errors arrive with HTTP 200

try { graphql.query($gql, ORDER, $vars); } catch (e) {
    if ($e.kind == "graphql") {
        def ge as graphql.GraphqlError init $e;
        io.printf("%s\n  reason=%s status=%d codes=%a\n", $ge.message,
            $ge.reason, $ge.status, $ge.codes);
    }
}
def resp as json.Value init graphql.tryQuery($gql, ORDER, $vars);
if (lists.contains(graphql.errorCodes($resp), "FORBIDDEN")) { ... }
graphql: order 2026-0199 is not visible to this token [FORBIDDEN]
  reason=graphql status=200 codes=[FORBIDDEN]
data.order is null: true
graphql | graphql: HTTP 401: not authenticated [UNAUTHENTICATED]
13 / 22

The amounts

# for each line $l of the order:
def unit as money.Money init money.parse(
    json.asString($l, "/unitPrice"), json.asString($l, "/currency"));
$lines[] = money.multiply($unit,
    io.sprintf("%d", json.asInt($l, "/qty")), "half-even");
# then, for the order:
def net as money.Money init money.sum($lines, "EUR");
def vat as money.Money init money.multiply($net, $t.rate, "half-up");
2026-0101 DE reverse-charge net  1860.00 VAT    0.00 vies
2026-0102 AT domestic       net   810.00 VAT  162.00
2026-0103 FR no-valid-id    net   394.00 VAT   78.80 vies
2026-0104 NL held: VAT ID NL300 unverified (SERVICE_UNAVAILABLE)
14 / 22

On the invoice

$pg = pdf.text($pg, 50, 764, FONT, 9,
    SELLER_ADDR + ", VAT ID " + SELLER_CC + SELLER_VAT);
$pg = pdf.text($pg, 50, 700, FONT, 10, pdf.toWinAnsi(
    json.asString($order, "/customer/name"), "?"));
if ($c.vatId != "") {
    $pg = pdf.text($pg, 50, 686, FONT, 10, "VAT ID " + $c.vatId);
}
...
if ($t.note != "") {
    $pg = pdf.text($pg, 50, $y - 70, BOLD, 10, $t.note);
    $pg = pdf.text($pg, 50, $y - 84, FONT, 9, "VAT ID checked on "
        + $c.date + ", VIES consultation number " + $c.proof + ".");
}
15 / 22

The result

Invoice 2026-0101 with reverse charge

16 / 22

When VIES is down

def sb as vies.Client init
    vies.withRetries(vies.sandboxClient(), 2, 500);
attempt("service unavailable", $sb, "NL", "300");
attempt("no answer", vies.withTimeout(vies.withEndpoint($sb,
    "http://127.0.0.1:8098/slow"), 1000), "DE", "100");
service unavailable: vies code=SERVICE_UNAVAILABLE transient=true
  status=200, 1917 ms
maintenance page: vies code= transient=true status=503, 1509 ms
no answer: timeout, 1001 ms
nobody listening: net, 1 ms
17 / 22

Unverified is a result

try {
    def r as vies.ApproxResult init vies.checkApprox($vc,
        $id.country, $id.number, SELLER_CC, SELLER_VAT);
    # ... status "valid" or "invalid", proof = r.requestIdentifier
} catch (e) {
    if ($e.kind == vies.ERROR_KIND) {
        def ve as vies.ViesError init $e;
        if (not $ve.transient) { throw $e; }
        $c.detail = $ve.code;
    } elseif ($e.kind == "timeout" or $e.kind == "net") {
        $c.detail = $e.kind;          # "closed" too
    } else { throw $e; }
}
18 / 22

A cache that respects the rules

def db as flatdb.DB init flatdb.open(CACHE);
if (flatdb.has($db, $key)) {
    def hit as json.Value init flatdb.get($db, $key);
    if (json.asString($hit, "/date") == today()) { ... "cache" ... }
}
def c as Check init askVies($vc, $id);
audit($order, $c);
if ($c.status == "valid") {     # only a positive answer is cached
    $db = flatdb.set($db, $key, json.map({"date": $c.date,
        "proof": $c.proof}));
    $db = flatdb.save($db);
}
{"DE100":{"date":"2026-11-19","proof":"WAPI0000001"}}
19 / 22

The audit trail

jsonl.appendFile(AUDIT, [json.map({"at": time.iso(time.now()),
    "order": $order, "vatId": $c.vatId, "status": $c.status,
    "source": $c.source, "proof": $c.proof, "detail": $c.detail})]);
{"order":"2026-0101","vatId":"DE100","status":"valid","source":"vies"}
{"order":"2026-0103","vatId":"FR200","status":"invalid","source":"vies"}
{"order":"2026-0104","vatId":"NL300","status":"unverified","source":"vies"}
{"order":"2026-0105","vatId":"DE100","status":"valid","source":"cache"}
2026-0104 NL300: still vies: checkVat NL300: SERVICE_UNAVAILABLE
  (after 2 retries)
20 / 22

Security, and two 0.26.0 gaps

21 / 22

Read more

Based on Jennifer 0.27.0, November 2026.

22 / 22