How to create and parse Tag, Length, Value (TLV) in PHP and encode it in Base64

Viewed 6486

There is a new regulation from the Government asking all VAT registered companies to implement QR CODE in the new E-Invoice.

  • The QR code fields shall be encoded in Tag-Length-Value (TLV) format with the tag values specified in the “Tag” column of the adjacent table.

  • The TLV encoding shall be as follows:

    • Tag: the tag value as mentioned above stored in one byte.
    • Length: the length of the byte array resulted from the UTF8 encoding of the field value. The length shall be stored in one byte.
    • Value: the byte array resulting from the UTF8 encoding of the field value.

How do I create TLV From an Array of Information? Is there a library that I can use?

$arr = [
    1 => 'Company Name',
    2 => '1234567890',
    3 => '2021/10/11 17:20:00',
    4 => '1000',
    5 => '150'
];
4 Answers

Yes, the QR code required is not a normal QR code with a link. It should be TLV base64 encoded. It can be done very easily. the values need to be hexed and then combined which will contain ASCII control characters.

If you still don't get it, Luckily, You can use the following package by Salla to generate a QR code from the array.

https://github.com/SallaApp/ZATCA

Make sure to follow the Tag structure provided by the ZATCA (GAZT previously). The package's example has the correct array:

$generatedString = GenerateQrCode::fromArray([
    new Seller('Salla'), // seller name
    new TaxNumber('1234567891'), // seller tax number
    new InvoiceDate('2021-07-12T14:25:09Z'), // invoice date as Zulu ISO8601 @see https://en.wikipedia.org/wiki/ISO_8601
    new InvoiceTotalAmount('100.00'), // invoice total amount
    new InvoiceTaxAmount('15.00') // invoice tax amount
    // TODO :: Support others tags
])->toTLV();

For those who are still using php5 based on https://github.com/SallaApp/ZATCA

/*
 * QR Encoding Functions
 */

function __getLength($value) {
    return strlen($value);
}

function __toHex($value) {
    return pack("H*", sprintf("%02X", $value));
}

function __toString($__tag, $__value, $__length) {
    $value = (string) $__value;
    return __toHex($__tag) . __toHex($__length) . $value;
}

function __getTLV($dataToEncode) {
    $__TLVS = '';
    for ($i = 0; $i < count($dataToEncode); $i++) {
        $__tag = $dataToEncode[$i][0];
        $__value = $dataToEncode[$i][1];
        $__length = __getLength($__value);
        $__TLVS .= __toString($__tag, $__value, $__length);
    }

    return $__TLVS;
}

/*
 * QR Encoding Functions
 */


/*
 * QR Code
 */
$dataToEncode = [
    [1, 'SellerName'],
    [2, 'VATNumber'],
    [3, 'invoiceDatetime'],
    [4, 'AmtwithVAT'],
    [5, 'VATamt']
];

$__TLV = __getTLV($dataToEncode);
$__QR = base64_encode($__TLV);

echo  $__QR;


/*
 * QR Code
 */

1-- Install ZATCA SDK https://zatca.gov.sa/en/E-Invoicing/SystemsDevelopers/ComplianceEnablementToolbox/Pages/DownloadSDK.aspx

2 -- fatoorah validateqr -qr "$__QR"

function einv_generate_tlv_qr_code($array_tag=array()){
    $index=1;
    foreach($array_tag as $tag_val){
        $tlv_string.=pack("H*", sprintf("%02X",(string) "$index")).
                     pack("H*", sprintf("%02X",strlen((string) "$tag_val"))).
                     (string) "$tag_val";
        $index++;                              
    }      
    return base64_encode($tlv_string);
}

Just send you tags values in array to the function and it will return you the TLV content of QRcode

$data_tlv=einv_generate_tlv_qr_code(array($company_name,$vat_reference,$timestamp,$total,$vat);

I have been into this ZATCA TLV Base64 QR code P2C issue by my clients in SA. well, I made this function and verified its result.

function zatca_base64_tlv_encode($seller_name, $vat_registration_number, $invoice_datetimez, $invoice_amount, $invoice_tax_amount)
{
    $result = chr(1) . chr( strlen($seller_name) ) . $seller_name;
    $result.= chr(2) . chr( strlen($vat_registration_number) ) . $vat_registration_number;
    $result.= chr(3) . chr( strlen($invoice_datetimez) ) . $invoice_datetimez;
    $result.= chr(4) . chr( strlen($invoice_amount) ) . $invoice_amount;
    $result.= chr(5) . chr( strlen($invoice_tax_amount) ) . $invoice_tax_amount;
    return base64_encode($result);
}
Related