Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

601
Visualizações
How to create and parse Tag, Length, Value (TLV) in PHP and encode it in Base64

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'
];
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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();
over 4 years ago · Santiago Trujillo Relatório

0

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"

over 4 years ago · Santiago Trujillo Relatório

0

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);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda