mrsool / zatca

An unofficial Ruby library for generating ZATCA e-invoices, QR Codes, and submitting e-invoices to ZATCA's servers.
MIT License
36 stars 12 forks source link

fix(tlv_conversion): Convert to Characters Rather than Hex #5

Closed obahareth closed 2 years ago

obahareth commented 2 years ago

Description

The original implementation was converting to hex, whereas all we needed to do was to convert to characters and ensure ASCII-8BIT encoding.

ZATCA expects ASCII, the original implementation was assuming they were expecting hexadecimal versions of numbers, but the desired values are in ASCII.

This change ensures that the whole thing is forced to be ASCII-8BIT.

It was unclear from ZATCA if the value should be left as Unicode or not, but this presents a challenge because we would have to concatenate an ASCII string with a UTF-8 string and that's not possible.

We use String.force_encoding to ASCII-8BIT to get around those errors and avoid String#encode because it will throw an exception because you can't convert UTF-8 to ASCII-8BIT because there is no point of reference for what each character maps to.

This approach generates the same output as ZATCA's SDK does.

Summary