<?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: Mohammad Sayyaf</title>
    <description>The latest articles on DEV Community by Mohammad Sayyaf (@mohammad_sayyaf_7b36a4a60).</description>
    <link>https://dev.to/mohammad_sayyaf_7b36a4a60</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%2F4257875%2F27c5a781-a57d-41fa-88a7-a8cf2b827ea7.png</url>
      <title>DEV Community: Mohammad Sayyaf</title>
      <link>https://dev.to/mohammad_sayyaf_7b36a4a60</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohammad_sayyaf_7b36a4a60"/>
    <language>en</language>
    <item>
      <title>ZATCA Phase 2 E-Invoicing: A Developer's Guide to XML, Hashing, Signing and Clearance</title>
      <dc:creator>Mohammad Sayyaf</dc:creator>
      <pubDate>Sat, 21 Nov 2026 19:43:44 +0000</pubDate>
      <link>https://dev.to/mohammad_sayyaf_7b36a4a60/zatca-phase-2-e-invoicing-a-developers-guide-to-xml-hashing-signing-and-clearance-6le</link>
      <guid>https://dev.to/mohammad_sayyaf_7b36a4a60/zatca-phase-2-e-invoicing-a-developers-guide-to-xml-hashing-signing-and-clearance-6le</guid>
      <description>&lt;p&gt;Saudi Arabia's e-invoicing programme (FATOORA) has moved well beyond "generate a PDF with a QR code". In Phase 2, every invoicing system has to produce structured XML, sign it, chain it to the previous invoice and talk to ZATCA's APIs in real time or within 24 hours.&lt;/p&gt;

&lt;p&gt;I've worked on several of these integrations across POS, ERP and custom billing systems. This post is the practical overview I wish I'd had at the start: the moving parts, the order to build them in, and the bugs that cost the most time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The big picture
&lt;/h2&gt;

&lt;p&gt;Phase 2 has two flows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Standard tax invoices (B2B)&lt;/strong&gt; go through &lt;strong&gt;clearance&lt;/strong&gt;. You send the signed XML to ZATCA, it validates and stamps it, and only then do you share it with the buyer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified tax invoices (B2C)&lt;/strong&gt; go through &lt;strong&gt;reporting&lt;/strong&gt;. You issue the invoice to the customer immediately, then report it to ZATCA within 24 hours.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Credit notes and debit notes follow the same flow as the invoice type they relate to.&lt;/p&gt;

&lt;h2&gt;
  
  
  The building blocks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. UBL 2.1 XML
&lt;/h3&gt;

&lt;p&gt;Each invoice is a UBL 2.1 XML document with ZATCA-specific fields: seller and buyer details, VAT breakdowns, invoice type codes, an Invoice Counter Value (ICV) and the Previous Invoice Hash (PIH).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; build a single function that maps your internal invoice model to XML, and unit-test it against ZATCA's sample invoices before touching anything else.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Invoice hash and the hash chain
&lt;/h3&gt;

&lt;p&gt;Each invoice is hashed (SHA-256) over a canonicalised version of the XML, with the signature-related parts excluded. That hash becomes the PIH of the next invoice from the same device or system.&lt;/p&gt;

&lt;p&gt;This chain is why you can't "just regenerate" an invoice later. If invoices are created out of order, or a failed invoice is dropped silently, the chain breaks.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Cryptographic stamp
&lt;/h3&gt;

&lt;p&gt;Invoices are signed with an ECDSA key pair, using the certificate issued to that device or system. The signature and related values are embedded in the XML.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. QR code
&lt;/h3&gt;

&lt;p&gt;Simplified invoices carry a TLV-encoded, Base64 QR code. Phase 2 adds fields beyond the Phase 1 basics (seller name, VAT number, timestamp, totals), including the invoice hash, the signature and the public key.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Onboarding and CSIDs
&lt;/h3&gt;

&lt;p&gt;Each invoicing unit is onboarded roughly like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate a key pair and a CSR containing the required fields.&lt;/li&gt;
&lt;li&gt;Request a &lt;strong&gt;compliance CSID&lt;/strong&gt; using an OTP from the FATOORA portal.&lt;/li&gt;
&lt;li&gt;Pass the &lt;strong&gt;compliance checks&lt;/strong&gt; by submitting sample invoices of each type you'll issue.&lt;/li&gt;
&lt;li&gt;Exchange it for a &lt;strong&gt;production CSID&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Certificates expire, so renewal needs an owner and a reminder. Otherwise invoicing stops on the day the certificate does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Order I'd build it in
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;XML generation, with tests against the official samples&lt;/li&gt;
&lt;li&gt;Canonicalisation and hashing&lt;/li&gt;
&lt;li&gt;Signing and the QR code&lt;/li&gt;
&lt;li&gt;A sandbox client for the compliance APIs&lt;/li&gt;
&lt;li&gt;Onboarding (CSR to production CSID)&lt;/li&gt;
&lt;li&gt;A queue for clearance and reporting, with retries&lt;/li&gt;
&lt;li&gt;Monitoring and alerting for rejected or stuck invoices&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The bugs that cost the most time
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Canonicalisation mismatches.&lt;/strong&gt; A single whitespace or namespace difference changes the hash. Always hash exactly what you send.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rounding.&lt;/strong&gt; Line-level versus invoice-level VAT rounding has to match ZATCA's validation rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Credit notes.&lt;/strong&gt; They must reference the original invoice correctly, and they're where "it worked in testing" usually breaks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Offline branches.&lt;/strong&gt; POS terminals lose internet. Queue invoices locally, keep the hash chain intact and report when you're back online.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency.&lt;/strong&gt; Two invoices created at the same moment on the same unit can corrupt the ICV/PIH sequence. Serialise per invoicing unit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dirty master data.&lt;/strong&gt; Missing buyer VAT numbers or addresses cause clearance rejections. Clean the customer master early.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Build vs buy
&lt;/h2&gt;

&lt;p&gt;If your team knows your billing code well and has time to learn the specs, building in-house is very doable. Otherwise, reusing tested components saves a lot of time. At &lt;a href="https://vocxa.com/" rel="noopener noreferrer"&gt;Vocxa&lt;/a&gt; we package this as ready-to-deploy &lt;a href="https://vocxa.com/" rel="noopener noreferrer"&gt;ZATCA e-invoicing integration&lt;/a&gt; code for Saudi businesses.&lt;/p&gt;

&lt;p&gt;If you're also choosing an ERP, POS or accounting tool that needs to support ZATCA, comparing options on a &lt;a href="https://stackcredo.com/" rel="noopener noreferrer"&gt;software comparison platform&lt;/a&gt; with verified reviews is a good way to check which vendors actually support Phase 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Phase 2 isn't conceptually hard, but it's unforgiving about details: canonical XML, a strict hash chain, certificates that expire and APIs that reject anything slightly off. Get the foundations right, test the awkward cases (returns, offline mode, concurrency) and monitor everything.&lt;/p&gt;

&lt;p&gt;Happy to answer questions in the comments if you're working on a FATOORA integration.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>api</category>
      <category>tutorial</category>
      <category>security</category>
    </item>
  </channel>
</rss>
