PHPackages                             salla/zatca - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. salla/zatca

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

salla/zatca
===========

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

3.0.4(9mo ago)161438.7k↓50.3%72[10 issues](https://github.com/SallaApp/ZATCA/issues)[7 PRs](https://github.com/SallaApp/ZATCA/pulls)2MITPHPPHP &gt;=8.0CI passing

Since Oct 11Pushed 3w ago30 watchersCompare

[ Source](https://github.com/SallaApp/ZATCA)[ Packagist](https://packagist.org/packages/salla/zatca)[ Docs](https://github.com/salla/zatca)[ RSS](/packages/salla-zatca/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (5)Versions (17)Used By (2)

 [ ![Logo](https://camo.githubusercontent.com/faa3709d60362cefdb30f6efac66211055ce41f1418c7725139d27bd69f85432/68747470733a2f2f73616c6c612e6465762f77702d636f6e74656e742f75706c6f6164732f323032332f30332f312d4c696768742e706e67) ](https://salla.dev)ZATCA (Fatoora) QR-Code Implementation
======================================

[](#zatca-fatoora-qr-code-implementation)

 An unofficial package maintained by [Salla](https://salla.dev) to help developers to implement ZATCA (Fatoora) QR code easily which required for e-invoicing
 [**Explore our blogs »**](https://salla.dev/)

 [Report Bug](https://github.com/SallaApp/ZATCA/issues/new) · [Request Feature](https://github.com/SallaApp/ZATCA/discussions/new) · [&lt;/Salla Developers&gt;](https://t.me/salladev)

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- A mbstring extension
- An ext-dom extension

Installation
------------

[](#installation)

You can install the package via composer:

```
$ composer require salla/zatca
```

([back to top](#top))

Usage
-----

[](#usage)

This library supports both Phase 1 and Phase 2.

Phase 2, include mandates integration of a taxpayer's system with the ZATCA, along with the transmission of e-invoices and e-notes to the ZATCA.

### Generating CSR

[](#generating-csr)

You need to onboard the merchant via the ZATCA APIs to had the authority to signing the invoice on behalf of the merchant.

```
use Salla\ZATCA\GenerateCSR;
use Salla\ZATCA\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 , the default is true, true
    ->setCurrentZatcaEnv('string $currentEnv') //support all modes ['sandbox','simulation','core']
    ->setBusinessCategory('string $businessCategory');

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

// writing the private_key to file
openssl_pkey_export_to_file($CSR->getPrivateKey(), 'output file path name');

//writing the csr_content to file
file_put_contents('output file path name', $CSR->getCsrContent());
```

At this stage you need to share the CSR to the ZATCA via APIs to get the certification for the current merchant

### Signing Invoices &amp; Generate QA code

[](#signing-invoices--generate-qa-code)

```
use Salla\ZATCA\Helpers\Certificate;
use Salla\ZATCA\Models\InvoiceSign;

$xmlInvoice = 'xml invoice text';

$certificate = (new Certificate(
    'certificate plain text (base64 decoded)', // get from ZATCA when you exchange the CSR via APIs
    'private key plain text' // generated at stage one
))->setSecretKey('secret key text'); // get from ZATCA when you exchange the CSR via APIs

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

// invoice Hash: $invoice->getHash()
// invoice signed as XML: $invoice->getInvoice()
// Invoice QR code as base64: $invoice->getQRCode()
```

### Generating QR Code As Base64

[](#generating-qr-code-as-base64)

> it's better to use `InvoiceSign` class to sign the invoice and generate the QR code for it in the same process

```
