# Buyer reference and Leitweg-ID

Every XRechnung needs a buyer reference. Without it, the KoSIT validator rejects the invoice with rule BR-DE-15. Stripe has no such field, so you have to supply it.

## What the buyer reference is

The buyer reference (business term BT-10) is an identifier the **buyer** specifies so they can route the invoice internally: a purchase order number, a cost centre, a department name or a contract number. The European standard EN 16931 makes it optional; XRechnung makes it mandatory.

## When it has to be a Leitweg-ID

If you bill a public authority or another public-sector buyer in Germany, the buyer reference is the **Leitweg-ID**. It routes the invoice in the receiving portals of the federal government, the states and municipalities and looks like this: `04011000-1234512345-06` (coarse address, fine address, check digits). The public buyer gives you the Leitweg-ID; it cannot be made up or derived. KoSIT's format specification describes its structure.

Businesses have no Leitweg-ID. When you set up the customer, ask which reference their accounts payable expects. If they have none, use a number they know themselves, such as their purchase order or contract number.

## How it gets from Stripe into the XRechnung

BelegDock reads the buyer reference from the **`buyer_reference` metadata of the Stripe invoice**. If it is missing there, BelegDock takes the metadata field of the same name on the **Stripe customer**, so you enter a fixed reference (such as a public authority's Leitweg-ID) only once. A reference on the invoice always wins. On the invoice you set it like this:

1. Open the draft invoice in the Stripe Dashboard and add the key `buyer_reference` with the reference as its value under "Metadata". Through the API, set `metadata[buyer_reference]` when creating or updating the draft.
2. Only then finalize the invoice.

For **subscription invoices**, the customer's metadata is usually enough. If the reference changes from invoice to invoice: Stripe creates the draft itself. Renewal invoices stay drafts for about an hour before Stripe finalizes them. During that time you can set the metadata, by hand or automatically on the `invoice.created` event. The first invoice of a subscription that charges automatically, however, is finalized at once. For that one Stripe describes workarounds, such as starting the subscription through a subscription schedule, which gives the first invoice a one-hour draft period too.

A **credit note** takes the buyer reference of the original invoice.

## When it is missing

BelegDock makes no reference up. If `buyer_reference` is missing on both the invoice and the customer, BelegDock stops the document with that reason and sends nothing. The Stripe invoice itself is not affected.

## Sources

- [Leitweg-ID format specification](https://xeinkauf.de/app/uploads/2022/11/Leitweg-ID-Formatspezifikation-v2-0-2-1.pdf) and [XRechnung](https://xeinkauf.de/xrechnung/) (KoSIT, German)
- [E-invoicing to the federal administration](https://www.e-rechnung-bund.de/) (German federal government)
- [Metadata](https://docs.stripe.com/metadata), [editing subscription invoices](https://docs.stripe.com/billing/invoices/subscription) and [automatic invoice advancement](https://docs.stripe.com/invoicing/integration/automatic-advancement-collection) (Stripe)

This page is a summary, not tax or legal advice.
