PHPackages                             zain4picker/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. zain4picker/zatca

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

zain4picker/zatca
=================

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

07[1 PRs](https://github.com/zain4picker/ZATCA/pulls)PHP

Since Aug 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/zain4picker/ZATCA)[ Packagist](https://packagist.org/packages/zain4picker/zatca)[ RSS](/packages/zain4picker-zatca/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

 [ ![Logo](https://camo.githubusercontent.com/52680b26bb205f5757d4ac0b2adc6a939ad41ef3f9489d0f429627f4f4606000/68747470733a2f2f7069636b65722e73642f7a61696e347069636b65722d636f7665722e737667) ](https://picker.sd)ZATCA (Fatoora) QR-Code Implementation
======================================

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

 An unofficial package maintained by [Picker](https://picker.sd) to help developers to implement ZATCA (Fatoora) QR code easily which required for e-invoicing

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

[](#requirements)

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

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

[](#installation)

You can install the package via composer:

```
composer require zain4picker/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 Picker\ZATCA\GenerateCSR;
use Picker\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 Picker\ZATCA\Helpers\Certificate;
use Picker\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

```
