PHP code example of hamada-emam-tech / einvoice-zatca

1. Go to this page and download the library: Download hamada-emam-tech/einvoice-zatca library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hamada-emam-tech / einvoice-zatca example snippets


use HamadaEmamTech\Einvoice\Signing\SoftwareKey;
use HamadaEmamTech\Einvoice\Zatca\Onboarding\{CsrDetails, Onboarding};
use HamadaEmamTech\Einvoice\Zatca\ZatcaFactory;

$key = SoftwareKey::generate('secp256k1');
file_put_contents('/secrets/egs.key', $key->exportPrivateKey($passphrase));

$onboarding = Onboarding::create(['environment' => 'sandbox']);   // or 'simulation', 'production'

$csr = $onboarding->csr(new CsrDetails(
    commonName: 'POS-1',                               // your own name for this unit
    serialNumber: '1-MyERP|2-4.2|3-' . $unitSerial,     // 1-solution | 2-model or version | 3-unit serial
    organizationIdentifier: '399999999900003',          // VAT number: 15 digits, starts and ends with 3
    organizationUnit: 'Riyadh Branch',                  // for a VAT group: the member's 10-digit TIN
    organizationName: 'Example Trading Co',
    invoiceTypes: '1100',                               // standard | simplified | 0 | 0
    location: 'RRRD2929',                               // short national address
    industry: 'Retail',
), $key, $key->publicKeyPem());

// The OTP comes from the Fatoora portal. On the developer portal it is always 123345.
$compliance = $onboarding->complianceCsid($csr, $otp);

$authority = (new ZatcaFactory())->create(['environment' => 'sandbox']);
$signer = $key->withCertificate($compliance->certificatePem());

$result = $onboarding->check($compliance,
    $authority->sign($authority->prepare($simplifiedInvoice), $signer),
    $authority->sign($authority->prepare($standardInvoice), $signer),
    // ... credit and debit notes too
);

$production = $onboarding->productionCsid($compliance);
file_put_contents('/secrets/csid.pem', $production->certificatePem());
// Store $production->binarySecurityToken and $production->secret() with your other secrets.

use HamadaEmamTech\Einvoice\Einvoice;

$einvoice = new Einvoice([
    'default' => 'zatca',
    'authorities' => [
        'zatca' => [
            'environment' => 'production',                 // sandbox | simulation | production, or set 'base_url'
            'certificate' => getenv('ZATCA_CSID_TOKEN'),   // the binarySecurityToken
            'secret'      => getenv('ZATCA_CSID_SECRET'),
            'key'         => fn () => SoftwareKey::fromFile('/secrets/egs.key', $passphrase, '/secrets/csid.pem'),
        ],
    ],
]);

use HamadaEmamTech\Einvoice\Model\{Address, Invoice, Line, Money, Party};

$seller = new Party('Example Trading Co', '399999999900003',
    new Address('SA', 'Riyadh', 'King Fahd Road', '1234', 'Al Olaya', '12345', additionalNumber: '5678'),
    identifiers: ['CRN' => '1010010000']);

$invoice = new Invoice(
    number:   'INV-1001',
    issuedAt: new DateTimeImmutable('now', new DateTimeZone('Asia/Riyadh')),
    seller:   $seller,
    buyer:    null,                                      // no buyer VAT number: simplified (B2C)
    lines:    [Line::taxed('Widget', '3', Money::of('10.00', 'SAR'), 'S', '15')],
    currency: 'SAR',
    extra:    ['zatca' => [
        'icv'           => 1001,                         // invoice counter of this EGS unit,