ahmedabdelnaby/zatca-tools

A helper to generate the QR code and signed it for ZATCA e-invoicing

dev-master 2025-02-17 08:46 UTC

This package is auto-updated.

Last update: 2025-04-17 09:06:59 UTC


README

๐Ÿงพ ZATCA (Fatoora) QR-Code & Invoice Signing

A PHP library to generate QR codes and sign invoices for ZATCA e-invoicing compliance in Saudi Arabia.
๐Ÿ› Report Bug ยท โœจ Request Feature

โœ… Requirements

  • ๐Ÿ˜ PHP >= 8.0
  • ๐Ÿ”ก mbstring extension
  • ๐Ÿ“œ ext-dom extension

๐Ÿ“ฆ Installation

Install the package via Composer:

composer require ahmedabdelnaby/zatca-tools

๐Ÿš€ Usage

This package supports both Phase 1 and Phase 2 of ZATCA compliance.

  • Phase 1: ๐Ÿ”ฒ QR code generation.
  • Phase 2: โœ๏ธ Invoice signing & ๐Ÿข System integration with ZATCA.

1๏ธโƒฃ Generating CSR (Certificate Signing Request)

Before signing invoices, you need to generate a CSR and register your merchant in ZATCA APIs.

use ZATCA\Tools\GenerateCSR;
use ZATCA\Tools\Models\CSRRequest;

$data = CSRRequest::make()
    ->setUID('string $OrganizationIdentifier')
    ->setSerialNumber('string $solutionName', 'string $version', 'string $serialNumber')
    ->setCommonName('string $commonName')
    ->setCountryName('SA')
    ->setOrganizationName('string $organizationName')
    ->setOrganizationalUnitName('string $organizationalUnitName')
    ->setRegisteredAddress('string $registeredAddress')
    ->setInvoiceType(true, true) // invoice types, default is true, true
    ->setCurrentZatcaEnv('sandbox') // Supports ['sandbox', 'simulation', 'core']
    ->setBusinessCategory('string $businessCategory');

$CSR = GenerateCSR::fromRequest($data)->initialize()->generate();

// Save ๐Ÿ”‘ private key
openssl_pkey_export_to_file($CSR->getPrivateKey(), 'private-key.pem');

// Save ๐Ÿ“ CSR content
file_put_contents('csr.pem', $CSR->getCsrContent());

๐Ÿ”น Now, submit the CSR to ZATCA APIs to receive your certificate.

2๏ธโƒฃ Signing Invoices & Generating QR Code

After receiving a certificate from ZATCA, you can sign invoices and generate QR codes.

use ZATCA\Tools\Helpers\Certificate;
use ZATCA\Tools\Models\InvoiceSign;

$xmlInvoice = 'xml invoice text';

$certificate = (new Certificate(
    'certificate plain text (base64 decoded)', // ๐Ÿ“œ Get from ZATCA
    'private key plain text' // ๐Ÿ”‘ Generated in CSR step
))->setSecretKey('๐Ÿ”’ secret key text'); // ๐Ÿ”‘ Get from ZATCA

$invoice = (new InvoiceSign($xmlInvoice, $certificate))->sign();

// Get invoice details ๐Ÿ“„
$invoice->getHash();      // ๐Ÿท๏ธ Invoice Hash
$invoice->getInvoice();   // ๐Ÿ“ Signed XML Invoice
$invoice->getQRCode();    // ๐Ÿ”ฒ QR Code in Base64 format

3๏ธโƒฃ Generating QR Code from Invoice Data

use ZATCA\Tools\GenerateQrCode;
use ZATCA\Tools\Helpers\UXML;
use ZATCA\Tools\Helpers\Certificate;

$xmlInvoice = 'xml invoice text';

$certificate = (new Certificate(
    'certificate plain text (base64 decoded)',
    'private key plain text'
))->setSecretKey('secret key text');

$tags = UXML::fromString($xmlInvoice)->toTagsArray($certificate);

$QRCodeAsBase64 = GenerateQrCode::fromArray($tags)->toBase64();

4๏ธโƒฃ Generating Basic QR Code (Phase 1)

use ZATCA\Tools\GenerateQrCode;
use ZATCA\Tools\Tags\InvoiceDate;
use ZATCA\Tools\Tags\InvoiceTaxAmount;
use ZATCA\Tools\Tags\InvoiceTotalAmount;
use ZATCA\Tools\Tags\Seller;
use ZATCA\Tools\Tags\TaxNumber;

$generatedString = GenerateQrCode::fromArray([
    new Seller('Company Name'),
    new TaxNumber('1234567891'),
    new InvoiceDate('2024-02-17T14:25:09Z'),
    new InvoiceTotalAmount('500.00'),
    new InvoiceTaxAmount('75.00')
])->toBase64();

5๏ธโƒฃ Display QR Code in HTML

$displayQRCodeAsBase64 = GenerateQrCode::fromArray([
    new Seller('Company Name'),
    new TaxNumber('1234567891'),
    new InvoiceDate('2024-02-17T14:25:09Z'),
    new InvoiceTotalAmount('500.00'),
    new InvoiceTaxAmount('75.00')
])->render();

Now, use it in an <img> tag:

<img src="<?php echo $displayQRCodeAsBase64; ?>" alt="Invoice QR Code" />

๐Ÿ”Ž Reading the QR Code

The QR code follows ZATCA's TLV format, making it unreadable by normal scanners.
To check your QR code, use this online scanner:
๐Ÿ”— ๐Ÿ“ธ Online QR Code Reader

๐Ÿงช Running Tests

Run the tests using:

composer test

๐Ÿ”’ Security

If you discover security vulnerabilities, please do not open an issue. Instead, email:
๐Ÿ“ง ahmedabdelnabyfahmy1@gmail.com