Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

You vibe-coded a checkout in Portugal. Who issues the invoice?

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. In Portug

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.

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 (Autoridade TributΓ‘ria, or AT). Your checkout can send the sale to a program that already has it. Decreto-Lei 28/2019, article 4

I work on FIZ, an AT-certified invoicing provider. I'll use our API to follow one booking, order 1042, 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 which version of the invoice you're issuing.

What your checkout needs to know

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 fatura-recibo, a single document that records both the sale and the payment. If the customer will pay later, you'd issue a fatura first and record the payment when it arrives. Portuguese VAT Code, article 36

Your app knows the customer, their Portuguese tax number (NIF) 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 ATCUD, 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. Portuguese rules for ATCUD and QR codes

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.

Give the agent one booking to work on

The company owner can create a sandbox in Settings β†’ Integrations once the company is connected to the AT. It has its own customers, items and test keys. Load a sandbox key as FIZ_API_KEY in the shell where you start your coding agent. Here's the task I'd give it. Sandbox setup

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.

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

Make a draft you can read

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 booking-1042.json, filling in the activity code and the two IDs. The quickstart includes the customer and item requests. FIZ quickstart

{
  "type": "INVOICE_RECEIPT",
  "cae": "<activity code for this service>",
  "customerId": "<sandbox customer id>",
  "items": [{ "id": "<sandbox item id>", "quantity": 1 }]
}
curl https://api.fiz.co/invoices \
  --header "x-api-key: $FIZ_API_KEY" \
  --header 'Idempotency-Key: booking-1042-draft' \
  --json @booking-1042.json

Save the returned id as INVOICE_ID. Read the draft with GET /invoices/{id} and save its updatedAt. Then open the download link from GET /invoices/{id}/pdf. 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.

Issue the version you reviewed

Once the PDF looks right, save this as booking-1042-issue.json. Paste in the exact timestamp you saved when you read the draft.

{
  "expectedUpdatedAt": "<saved updatedAt>"
}
curl "https://api.fiz.co/invoices/$INVOICE_ID/issue" \
  --header "x-api-key: $FIZ_API_KEY" \
  --header 'Idempotency-Key: booking-1042-issue-1' \
  --json @booking-1042-issue.json

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 number and atcud before showing either to the customer.

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. Decreto-Lei 198/2012, article 3

Two tests I'd run before the first real booking

1. The invoice goes through and the response gets lost. 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.

With FIZ, repeating a completed request with the same key returns its recorded result. If you get a 409 because the first attempt is running, wait for Retry-After 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. FIZ retry behavior

2. Someone changes the draft while you're looking at the PDF. Make a second sandbox draft, save its updatedAt, 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 expectedUpdatedAt buys you. Fetching the latest timestamp just before issuing would quietly approve the extra booking.

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 Edit draft button and a separate way to correct an issued invoice. AT guidance on invoice corrections

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. SDKs Β· MCP connector

For order 1042, 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.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.