Skip to Content
Technical Articles
Author's profile photo S M Firoz Ashraf

QR Code in Base64 encoding for KSA E-Invoicing – Directly from data in Table EDOSAINV

I have explained how QR code can be generated in Base64 encoding conforming to Saudi Arabia ZATCA standard in my previous blog.

In this blog I will explain how the same can be done directly from data stored in field QR_CODE of table EDOSAINV.

With the data stored in EDOSAINV-QR_CODE you don’t need to worry on how to build and convert data in TLV format.

There are just three steps to follow:

  1. Get the QR data stored in Hexadecimal value (RAWSTRING) from table EDOSINV
  2. Convert this value to String value.
  3. Convert the string value obtained from 2nd step to Base64

Note: You may convert to Base64 is just two steps as well. Convert the hexa from EDOSAINV-QR_CODE directly to Base64 using method encode_x_base64 of cl_http_utility instead of converting first to string and then to Base64. The second step mentioned above is just to “see” the value in string.

You can then use the base64 value obtained in the final step 3 to generate the QR code.

I have created a custom FM which takes invoice number as input and gives QR code values in  Base64 along with hex and string.

FUNCTION z_einvoice_qrcode_from_table.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(INVOICE_NO) TYPE  VBELN_VF
*"  EXPORTING
*"     REFERENCE(QRCODE_HEXSTR) TYPE  EDOC_SA_XSTRING
*"     REFERENCE(QRCODE_STRING) TYPE  STRING
*"     REFERENCE(QRCODE_BASE64) TYPE  STRING
*"  EXCEPTIONS
*"      NO_INVOICE
*"----------------------------------------------------------------------
* For this FM to give QR value result in base64 encoding, XML should
* have been successfully generated from transaction EDOC_COCKPIT against
* the invoice.
*"----------------------------------------------------------------------

  DATA: v_guid   TYPE edoc_guid.

  SELECT SINGLE edoc_guid FROM edocument INTO v_guid
      WHERE source_key = invoice_no.
  IF sy-subrc = 0.
    SELECT SINGLE qr_code FROM edosainv INTO qrcode_hexstr
    WHERE edoc_guid = v_guid.
    IF sy-subrc = 0.
      PERFORM convert_hex_to_str USING qrcode_hexstr CHANGING qrcode_string.
***************Encode String to Base64*********************
      CALL METHOD cl_http_utility=>if_http_utility~encode_base64
        EXPORTING
          unencoded = qrcode_string
        RECEIVING
          encoded   = qrcode_base64.
    ENDIF.
  ELSE.
    RAISE no_invoice.
  ENDIF.

ENDFUNCTION.

The subroutine convert_hex_to_str is

*&---------------------------------------------------------------------*
*&      Form  CONVERT_HEX_TO_STR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_HEX  text
*      <--P_STR  text
*----------------------------------------------------------------------*
FORM convert_hex_to_str  USING    p_hex
                         CHANGING p_str.
*  CALL FUNCTION 'HR_RU_CONVERT_HEX_TO_STRING'
*    EXPORTING
*      xstring = p_hex
*    IMPORTING
*      cstring = p_str.

  DATA: loc_conv TYPE REF TO cl_abap_conv_in_ce.

  CALL METHOD cl_abap_conv_in_ce=>create
    EXPORTING
      input       = p_hex
      encoding    = 'UTF-8'
      replacement = '?'
      ignore_cerr = abap_true
    RECEIVING
      conv        = loc_conv.
  TRY.
      CALL METHOD loc_conv->read
        IMPORTING
          data = p_str.
    CATCH cx_sy_conversion_codepage.    "Should ignore errors in code conversions
    CATCH cx_sy_codepage_converter_init."Should ignore errors in code conversions
    CATCH cx_parameter_invalid_type.
    CATCH cx_parameter_invalid_range.
  ENDTRY.

ENDFORM.

Once you run the FM with any invoice whose eInvoice has been generated then you will get the output of this FM as shown below.

Take the value of export parameter QRCODE_BASE64 and generate the QR code.

You may follow the second half from “Setting up the QR Code font” of my previous blog on how to display QR code on any SAPScript/SmartForms layouts.

Regards,

Firoz.

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Boon Chai Loh
      Boon Chai Loh

      Just to say your code work in my smartforms. Thanks a lot.

      I have an interesting question here where the business transaction currency is in USD. ZATCA QR code specification never talks about currency. With your coding, the hex to string, it show the value of USD currency for example. It seems SAP did not store it in SAR. Android app scan the QR code and always show value in SAR. I wonder what should be right or wrong here.

      Author's profile photo Alghoneim Saleh
      Alghoneim Saleh

      I am looking for SAP Business One and Crystal Report

      Author's profile photo Divakar Balugarighatikachalam
      Divakar Balugarighatikachalam

      I have a basic question. I cant see table EDOSAINV in my S4 system. Do we need to implement any SAP Notes for E-Invoicing for KSA ?

      Author's profile photo Firoz Ashraf
      Firoz Ashraf

      Yes, there are some SAP Notes that should be implemented for eInvoicing KSA. One among them which is related to table EDOSAINV is

      3095262 - SAP Document Compliance Saudi Arabia - eInvoice