<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrey Stikheev</title>
    <description>The latest articles on DEV Community by Andrey Stikheev (@sand-fiz-co).</description>
    <link>https://dev.to/sand-fiz-co</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4267391%2F155e12c6-7227-4383-a5df-3870ffbf9db7.png</url>
      <title>DEV Community: Andrey Stikheev</title>
      <link>https://dev.to/sand-fiz-co</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sand-fiz-co"/>
    <language>en</language>
    <item>
      <title>You vibe-coded a checkout in Portugal. Who issues the invoice?</title>
      <dc:creator>Andrey Stikheev</dc:creator>
      <pubDate>Thu, 26 Nov 2026 17:46:34 +0000</pubDate>
      <link>https://dev.to/sand-fiz-co/you-vibe-coded-a-checkout-in-portugal-who-issues-the-invoice-21hp</link>
      <guid>https://dev.to/sand-fiz-co/you-vibe-coded-a-checkout-in-portugal-who-issues-the-invoice-21hp</guid>
      <description>&lt;p&gt;Your first customer pays for a booking and asks for an invoice with their tax number. Your checkout already knows what they bought and how much they paid. It would take one more prompt to turn that into a PDF.&lt;/p&gt;

&lt;p&gt;In Portugal, the interesting question is who gets to assign the invoice number. If your business uses software to issue invoices under Portuguese invoicing rules, that software needs prior certification from the tax authority (&lt;em&gt;Autoridade Tributária&lt;/em&gt;, or AT). Your checkout can send the sale to a program that already has it. &lt;a href="https://info.portaldasfinancas.gov.pt/pt/informacao_fiscal/legislacao/diplomas_legislativos/Documents/Decreto_Lei_28_2019.pdf" rel="noopener noreferrer"&gt;Decreto-Lei 28/2019, article 4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I work on FIZ, an AT-certified invoicing provider. I'll use our API to follow one booking, order &lt;code&gt;1042&lt;/code&gt;, from payment to invoice. There's one line in the example I'd pay particular attention to in code review. It's the one that tells the API &lt;em&gt;which version&lt;/em&gt; of the invoice you're issuing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What your checkout needs to know
&lt;/h2&gt;

&lt;p&gt;Say your company sells a service in Portugal and the customer pays at checkout. The invoice is due on the payment date. For this order we'll issue a &lt;em&gt;fatura-recibo&lt;/em&gt;, a single document that records both the sale and the payment. If the customer will pay later, you'd issue a &lt;em&gt;fatura&lt;/em&gt; first and record the payment when it arrives. &lt;a href="https://info.portaldasfinancas.gov.pt/pt/informacao_fiscal/codigos_tributarios/civa_rep/pages/iva36.aspx" rel="noopener noreferrer"&gt;Portuguese VAT Code, article 36&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your app knows the customer, their Portuguese tax number (&lt;em&gt;NIF&lt;/em&gt;) if required or requested, what they bought, and the applicable VAT. The invoicing software takes care of the registered series, the next number in that series, the signature and the QR code. It also assigns the &lt;em&gt;ATCUD&lt;/em&gt;, the code that identifies this particular document. Your order ID and the ATCUD are worth keeping together so support can find the invoice for a booking. &lt;a href="https://diariodarepublica.pt/dr/detalhe/portaria/195-2020-140210523" rel="noopener noreferrer"&gt;Portuguese rules for ATCUD and QR codes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you only issue a few invoices, entering them by hand in the Portal das Finanças may be enough. For an automated checkout, calling a certified provider gives you a much smaller integration to maintain. Here's what that looks like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give the agent one booking to work on
&lt;/h2&gt;

&lt;p&gt;The company owner can create a sandbox in &lt;strong&gt;Settings → Integrations&lt;/strong&gt; once the company is connected to the AT. It has its own customers, items and test keys. Load a sandbox key as &lt;code&gt;FIZ_API_KEY&lt;/code&gt; in the shell where you start your coding agent. Here's the task I'd give it. &lt;a href="https://api.fiz.co/docs/api.md#sandbox" rel="noopener noreferrer"&gt;Sandbox setup&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Build the invoice step for booking 1042, paid at checkout.
Read https://api.fiz.co/llms.txt and the quickstart.
Use FIZ_API_KEY from the environment. Keep the key out of logs and chat.
Check GET /company first. Continue only if environment is "sandbox".
Create a draft invoice-receipt for one booking and show me its PDF.
Stop there so I can check it before we issue it.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The company response gives you two things you'll need shortly. &lt;code&gt;cae&lt;/code&gt; lists the company's activity codes. Pick the one for the service you sold. When &lt;code&gt;readiness.ready&lt;/code&gt; is false, you can still make a draft. Check &lt;code&gt;readiness.reasons&lt;/code&gt; for what to fix before issuing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make a draft you can read
&lt;/h2&gt;

&lt;p&gt;Create a test customer and a service item in the sandbox. Put the buyer's NIF on the customer and the price and VAT rate on the item. Save the JSON below as &lt;code&gt;booking-1042.json&lt;/code&gt;, filling in the activity code and the two IDs. The quickstart includes the customer and item requests. &lt;a href="https://api.fiz.co/docs/quickstart" rel="noopener noreferrer"&gt;FIZ quickstart&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"INVOICE_RECEIPT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"cae"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;activity code for this service&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"customerId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;sandbox customer id&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;sandbox item id&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"quantity"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl https://api.fiz.co/invoices \
  --header &lt;span class="s2"&gt;"x-api-key: $FIZ_API_KEY"&lt;/span&gt; \
  --header &lt;span class="s1"&gt;'Idempotency-Key: booking-1042-draft'&lt;/span&gt; \
  --json @booking-1042.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the returned &lt;code&gt;id&lt;/code&gt; as &lt;code&gt;INVOICE_ID&lt;/code&gt;. Read the draft with &lt;code&gt;GET /invoices/{id}&lt;/code&gt; and save its &lt;code&gt;updatedAt&lt;/code&gt;. Then open the download link from &lt;code&gt;GET /invoices/{id}/pdf&lt;/code&gt;. Does the NIF match the one the customer gave you? Does the total match what they paid? You can still fix a typo here. A draft has no invoice number and never reaches the AT.&lt;/p&gt;

&lt;h2&gt;
  
  
  Issue the version you reviewed
&lt;/h2&gt;

&lt;p&gt;Once the PDF looks right, save this as &lt;code&gt;booking-1042-issue.json&lt;/code&gt;. Paste in the exact timestamp you saved when you read the draft.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"expectedUpdatedAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;saved updatedAt&amp;gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="s2"&gt;"https://api.fiz.co/invoices/$INVOICE_ID/issue"&lt;/span&gt; \
  --header &lt;span class="s2"&gt;"x-api-key: $FIZ_API_KEY"&lt;/span&gt; \
  --header &lt;span class="s1"&gt;'Idempotency-Key: booking-1042-issue-1'&lt;/span&gt; \
  --json @booking-1042-issue.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the sandbox you'll get a test document. In production this call issues the customer's invoice. Save the document ID with the order and check the returned &lt;code&gt;number&lt;/code&gt; and &lt;code&gt;atcud&lt;/code&gt; before showing either to the customer.&lt;/p&gt;

&lt;p&gt;Reporting has its own deadline, normally the fifth of the following month. With AT reporting enabled, FIZ sends the data when the invoice is issued. If your company files monthly, it submits the SAF-T (PT) file itself. &lt;a href="https://info.portaldasfinancas.gov.pt/pt/informacao_fiscal/legislacao/diplomas_legislativos/Documents/Decreto-Lei%20n%20_198_2012_24_08.pdf" rel="noopener noreferrer"&gt;Decreto-Lei 198/2012, article 3&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Two tests I'd run before the first real booking
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. The invoice goes through and the response gets lost.&lt;/strong&gt; Have a mock accept the issue request, record success, then drop the connection. Restart your worker. Can it recover the draft ID, request body and issue key from the order? Assert that it sends all three unchanged and records a single invoice. This is why the issue key belongs with the order before the first call. Generating a UUID inside the retry loop gives every retry a fresh key.&lt;/p&gt;

&lt;p&gt;With FIZ, repeating a completed request with the same key returns its recorded result. If you get a &lt;code&gt;409&lt;/code&gt; because the first attempt is running, wait for &lt;code&gt;Retry-After&lt;/code&gt; and try again with that key. If the API says the outcome is unknown, read the invoice to see whether it was issued before deciding whether a fresh attempt is needed. Check the document first for retries older than 30 days, when the key may have expired. &lt;a href="https://api.fiz.co/docs/api.md#idempotency" rel="noopener noreferrer"&gt;FIZ retry behavior&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Someone changes the draft while you're looking at the PDF.&lt;/strong&gt; Make a second sandbox draft, save its &lt;code&gt;updatedAt&lt;/code&gt;, then change the quantity from one to two. Try to issue it with the saved timestamp. The API should refuse and the document should still be a draft. That's what &lt;code&gt;expectedUpdatedAt&lt;/code&gt; buys you. Fetching the latest timestamp just before issuing would quietly approve the extra booking.&lt;/p&gt;

&lt;p&gt;After issue, a quantity or price adjustment has a paper trail. A later discount calls for a credit note for the difference. A typo in the customer's NIF calls for annulling the document and issuing a corrected one with the original supply date. That's a good reason to give support an &lt;strong&gt;Edit draft&lt;/strong&gt; button and a separate way to correct an issued invoice. &lt;a href="https://info.portaldasfinancas.gov.pt/pt/informacao_fiscal/legislacao/instrucoes_administrativas/Documents/Ofcio-circulado-25120-2026.pdf" rel="noopener noreferrer"&gt;AT guidance on invoice corrections&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same two tests work if you use the TypeScript, Python or PHP SDK. For a booking you handle by hand, the MCP connector lets your assistant prepare an invoice and show you a preview before you confirm. &lt;a href="https://api.fiz.co/docs/libraries" rel="noopener noreferrer"&gt;SDKs&lt;/a&gt; · &lt;a href="https://api.fiz.co/docs/mcp" rel="noopener noreferrer"&gt;MCP connector&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For order &lt;code&gt;1042&lt;/code&gt;, my last check would be to open the invoice from the booking record after that simulated timeout. Can support find the right document, with the NIF the customer asked for and exactly one booking on it? That's the checkout I'd ship.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>startup</category>
      <category>api</category>
      <category>einvoicing</category>
    </item>
  </channel>
</rss>
