zatca 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -2
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/einvoicing-sdk/Apps/fatoora +12 -0
- data/einvoicing-sdk/Apps/fatoora.bat +16 -0
- data/einvoicing-sdk/Apps/global.json +1 -0
- data/einvoicing-sdk/Apps/jq.exe +0 -0
- data/einvoicing-sdk/Apps/zatca-einvoicing-sdk-238-R3.2.7.jar +0 -0
- data/einvoicing-sdk/Configuration/config.json +11 -0
- data/einvoicing-sdk/Configuration/defaults.json +11 -0
- data/einvoicing-sdk/Configuration/jq.exe +0 -0
- data/einvoicing-sdk/Configuration/usage.txt +16 -0
- data/einvoicing-sdk/Data/Certificates/cert.pem +1 -0
- data/einvoicing-sdk/Data/Certificates/ec-secp256k1-priv-key.pem +1 -0
- data/einvoicing-sdk/Data/Input/csr-config-example-AR-VAT-Group.properties +9 -0
- data/einvoicing-sdk/Data/Input/csr-config-example-AR.properties +9 -0
- data/einvoicing-sdk/Data/Input/csr-config-example-EN-VAT-group.properties +9 -0
- data/einvoicing-sdk/Data/Input/csr-config-example-EN.properties +9 -0
- data/einvoicing-sdk/Data/Input/csr-config-template.properties +9 -0
- data/einvoicing-sdk/Data/PIH/pih.txt +1 -0
- data/einvoicing-sdk/Data/Rules/schematrons/20210819_ZATCA_E-invoice_Validation_Rules.xsl +2844 -0
- data/einvoicing-sdk/Data/Rules/schematrons/CEN-EN16931-UBL.xsl +1973 -0
- data/einvoicing-sdk/Data/Samples/PDF-A3/Simplified Debit Note.pdf +0 -0
- data/einvoicing-sdk/Data/Samples/PDF-A3/Standard Credit Note.pdf +0 -0
- data/einvoicing-sdk/Data/Samples/PDF-A3/Standard Debit Note.pdf +0 -0
- data/einvoicing-sdk/Data/Samples/PDF-A3/Standard Invoice.pdf +0 -0
- data/einvoicing-sdk/Data/Samples/PDF-A3/Tax_Invoice_USD.pdf +0 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Credit/Simplified_Credit_Note.xml +300 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Credit/Simplified_Credit_Note_Error.xml +225 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Debit/Simplified_Debit_Note.xml +211 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Debit/Simplified_Debit_Note_Error.xml +226 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Invoice/Additional_Simplified_Invoices/Out_Of_Scope_Simplified_Invoice.xml +207 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Invoice/Additional_Simplified_Invoices/Simplified_Invoice_USD.xml +246 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Invoice/Simplified_Invoice.xml +246 -0
- data/einvoicing-sdk/Data/Samples/Simplified/Invoice/Simplified_Invoice_Error.xml +228 -0
- data/einvoicing-sdk/Data/Samples/Standard/Credit/Standard_Credit_Note.xml +227 -0
- data/einvoicing-sdk/Data/Samples/Standard/Credit/Standard_Credit_Note_Error.xml +224 -0
- data/einvoicing-sdk/Data/Samples/Standard/Debit/Standard_Debit_Note.xml +227 -0
- data/einvoicing-sdk/Data/Samples/Standard/Debit/Standard_Debit_Note_Error.xml +226 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Additional_Standard_Invoices/Exempt_Standard_Invoice.xml +227 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Additional_Standard_Invoices/Standard_Invoice_USD.xml +227 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Additional_Standard_Invoices/Zero_Rate_Standard_Invoice.xml +227 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Sample Invoice[Advance Payements] - 1.xml +294 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Sample Invoice[Advance Payements] - 2.xml +432 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Standard_Invoice.xml +222 -0
- data/einvoicing-sdk/Data/Samples/Standard/Invoice/Standard_Invoice_Error.xml +228 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/CCTS_CCT_SchemaModule-2.1.xsd +731 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-CommonAggregateComponents-2.1.xsd +44365 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-CommonBasicComponents-2.1.xsd +5389 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-CommonExtensionComponents-2.1.xsd +223 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-CommonSignatureComponents-2.1.xsd +101 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-CoreComponentParameters-2.1.xsd +63 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-ExtensionContentDataType-2.1.xsd +89 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-QualifiedDataTypes-2.1.xsd +69 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-SignatureAggregateComponents-2.1.xsd +138 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-SignatureBasicComponents-2.1.xsd +78 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-UnqualifiedDataTypes-2.1.xsd +553 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-XAdESv132-2.1.xsd +476 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-XAdESv141-2.1.xsd +25 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/common/UBL-xmldsig-core-schema-2.1.xsd +330 -0
- data/einvoicing-sdk/Data/Schemas/xsds/UBL2.1/xsd/maindoc/UBL-Invoice-2.1.xsd +1002 -0
- data/einvoicing-sdk/Dockerfile +26 -0
- data/einvoicing-sdk/LICENSE.txt +56 -0
- data/einvoicing-sdk/Lib/.Net/DLL/BouncyCastle.Crypto.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/IKVM.OpenJDK.Core.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/IKVM.OpenJDK.Text.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/IKVM.OpenJDK.Util.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/IKVM.OpenJDK.XML.API.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/IKVM.Runtime.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/SDKNETFrameWorkLib.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/SDKNETFrameWorkLib.dll.config +43 -0
- data/einvoicing-sdk/Lib/.Net/DLL/SDKNETFrameWorkLib.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/System.Net.Http.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/saxon-he-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/saxon-he-api-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/DLL/saxon-he-api-10.8.xml +11759 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/.vs/SDKNETFrameworkTest/FileContentIndex/3f204b7a-faba-42d8-8288-8b6fe516555c.vsidx +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/.vs/SDKNETFrameworkTest/FileContentIndex/f95b6f91-c110-4b7a-bb49-3fc6bde85b13.vsidx +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/.vs/SDKNETFrameworkTest/FileContentIndex/read.lock +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/.vs/SDKNETFrameworkTest/v17/.suo +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/App.config +14 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/BouncyCastle.Crypto.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/CSRGeneratorOpenSSL.cs +58 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/IKVM.OpenJDK.Core.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/IKVM.OpenJDK.Text.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/IKVM.OpenJDK.Util.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/IKVM.OpenJDK.XML.API.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/IKVM.Runtime.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/Program.cs +194 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/Properties/AssemblyInfo.cs +36 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/Readme.txt +8 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameWorkLib.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameWorkLib.dll.config +43 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameWorkLib.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameworkTest.csproj +120 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameworkTest.exe +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameworkTest.exe.config +20 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameworkTest.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/SDKNETFrameworkTest.sln +25 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/BouncyCastle.Crypto.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/Data/certificate.txt +1 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/Data/pih.txt +1 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/Data/privateKey.txt +1 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/IKVM.OpenJDK.Core.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/IKVM.OpenJDK.Text.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/IKVM.OpenJDK.Util.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/IKVM.OpenJDK.XML.API.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/IKVM.Runtime.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/SDKNETFrameWorkLib.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/SDKNETFrameWorkLib.dll.config +43 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/SDKNETFrameWorkLib.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/SDKNETFrameworkTest.exe +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/SDKNETFrameworkTest.exe.config +20 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/SDKNETFrameworkTest.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/System.Net.Http.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/saxon-he-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/saxon-he-api-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/bin/Debug/saxon-he-api-10.8.xml +11759 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/.NETFramework,Version=v4.7.2.AssemblyAttributes.cs +4 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/DesignTimeResolveAssemblyReferences.cache +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.csproj.AssemblyReference.cache +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.csproj.CopyComplete +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.csproj.CoreCompileInputs.cache +1 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.csproj.FileListAbsolute.txt +22 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.csproj.SuggestedBindingRedirects.cache +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.exe +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/obj/Debug/SDKNETFrameworkTest.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/saxon-he-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/saxon-he-api-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/SDKNETFrameworkTest/saxon-he-api-10.8.xml +11759 -0
- data/einvoicing-sdk/Lib/.Net/Test/BouncyCastle.Crypto.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/IKVM.OpenJDK.Core.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/IKVM.OpenJDK.Text.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/IKVM.OpenJDK.Util.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/IKVM.OpenJDK.XML.API.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/IKVM.Runtime.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/SDKNETFrameWorkLib.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/SDKNETFrameWorkLib.dll.config +43 -0
- data/einvoicing-sdk/Lib/.Net/Test/SDKNETFrameWorkLib.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/SDKNETFrameworkTest.exe +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/SDKNETFrameworkTest.exe.config +20 -0
- data/einvoicing-sdk/Lib/.Net/Test/SDKNETFrameworkTest.pdb +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/saxon-he-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/saxon-he-api-10.8.dll +0 -0
- data/einvoicing-sdk/Lib/.Net/Test/saxon-he-api-10.8.xml +11759 -0
- data/einvoicing-sdk/README.md +15 -0
- data/einvoicing-sdk/Readme/readme.docx +0 -0
- data/einvoicing-sdk/Readme/readme.md +674 -0
- data/einvoicing-sdk/Readme/readme.pdf +0 -0
- data/einvoicing-sdk/Readme/~$readme.docx +0 -0
- data/einvoicing-sdk/docker-compose.yml +12 -0
- data/einvoicing-sdk/install.bat +74 -0
- data/einvoicing-sdk/install.sh +74 -0
- data/lib/zatca/client.rb +173 -0
- data/lib/zatca/hacks.rb +45 -0
- data/lib/zatca/hashing.rb +18 -0
- data/lib/zatca/qr_code_extractor.rb +31 -0
- data/lib/zatca/qr_code_generator.rb +9 -2
- data/lib/zatca/signing/certificate.rb +78 -0
- data/lib/zatca/signing/csr.rb +220 -0
- data/lib/zatca/signing/ecdsa.rb +59 -0
- data/lib/zatca/tag.rb +18 -8
- data/lib/zatca/tags.rb +5 -1
- data/lib/zatca/tags_schema.rb +5 -5
- data/lib/zatca/types.rb +7 -0
- data/lib/zatca/ubl/base_component.rb +142 -0
- data/lib/zatca/ubl/builder.rb +166 -0
- data/lib/zatca/ubl/common_aggregate_components/allowance_charge.rb +64 -0
- data/lib/zatca/ubl/common_aggregate_components/classified_tax_category.rb +25 -0
- data/lib/zatca/ubl/common_aggregate_components/delivery.rb +27 -0
- data/lib/zatca/ubl/common_aggregate_components/invoice_line.rb +63 -0
- data/lib/zatca/ubl/common_aggregate_components/item.rb +21 -0
- data/lib/zatca/ubl/common_aggregate_components/legal_monetary_total.rb +59 -0
- data/lib/zatca/ubl/common_aggregate_components/party.rb +28 -0
- data/lib/zatca/ubl/common_aggregate_components/party_identification.rb +25 -0
- data/lib/zatca/ubl/common_aggregate_components/party_legal_entity.rb +19 -0
- data/lib/zatca/ubl/common_aggregate_components/party_tax_scheme.rb +30 -0
- data/lib/zatca/ubl/common_aggregate_components/postal_address.rb +59 -0
- data/lib/zatca/ubl/common_aggregate_components/price.rb +20 -0
- data/lib/zatca/ubl/common_aggregate_components/tax_category.rb +56 -0
- data/lib/zatca/ubl/common_aggregate_components/tax_total.rb +58 -0
- data/lib/zatca/ubl/common_aggregate_components.rb +2 -0
- data/lib/zatca/ubl/invoice.rb +481 -0
- data/lib/zatca/ubl/invoice_subtype_builder.rb +50 -0
- data/lib/zatca/ubl/signing/cert.rb +48 -0
- data/lib/zatca/ubl/signing/invoice_signed_data_reference.rb +44 -0
- data/lib/zatca/ubl/signing/key_info.rb +25 -0
- data/lib/zatca/ubl/signing/object.rb +20 -0
- data/lib/zatca/ubl/signing/qualifying_properties.rb +27 -0
- data/lib/zatca/ubl/signing/signature.rb +50 -0
- data/lib/zatca/ubl/signing/signature_information.rb +19 -0
- data/lib/zatca/ubl/signing/signature_properties_reference.rb +26 -0
- data/lib/zatca/ubl/signing/signed_info.rb +21 -0
- data/lib/zatca/ubl/signing/signed_properties.rb +81 -0
- data/lib/zatca/ubl/signing/signed_signature_properties.rb +23 -0
- data/lib/zatca/ubl/signing/ubl_document_signatures.rb +25 -0
- data/lib/zatca/ubl/signing/ubl_extension.rb +22 -0
- data/lib/zatca/ubl/signing/ubl_extensions.rb +17 -0
- data/lib/zatca/ubl/signing.rb +2 -0
- data/lib/zatca/ubl.rb +2 -0
- data/lib/zatca/version.rb +1 -1
- data/lib/zatca.rb +27 -3
- data/zatca.gemspec +52 -0
- metadata +318 -10
- data/Gemfile.lock +0 -100
@@ -0,0 +1,220 @@
|
|
1
|
+
class ZATCA::Signing::CSR
|
2
|
+
attr_reader :key, :csr_options, :mode
|
3
|
+
|
4
|
+
# For security purposes, please provide your own private key.
|
5
|
+
# If you don't provide one, a new unsecure one will be generated for testing purposes.
|
6
|
+
def initialize(
|
7
|
+
csr_options:,
|
8
|
+
mode: :production,
|
9
|
+
private_key_path: nil,
|
10
|
+
private_key_password: nil
|
11
|
+
)
|
12
|
+
@csr_options = default_csr_options.merge(csr_options)
|
13
|
+
@mode = mode.to_sym
|
14
|
+
@private_key_path = private_key_path
|
15
|
+
@private_key_password = private_key_password
|
16
|
+
@generated_private_key_path = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns a hash with two keys
|
20
|
+
# - csr
|
21
|
+
# - csr_without_headers
|
22
|
+
def generate(csr_options: {})
|
23
|
+
set_key
|
24
|
+
write_csr_config
|
25
|
+
|
26
|
+
command = generate_openssl_csr_command
|
27
|
+
|
28
|
+
# Run the command and return the output
|
29
|
+
output = `#{command}`
|
30
|
+
|
31
|
+
cleanup_leftover_files
|
32
|
+
|
33
|
+
output
|
34
|
+
|
35
|
+
# TODO: Ruby version
|
36
|
+
# request = OpenSSL::X509::Request.new
|
37
|
+
# request.version = 0
|
38
|
+
# request.subject = OpenSSL::X509::Name.new([
|
39
|
+
# ["C", csr_options[:country], OpenSSL::ASN1::PRINTABLESTRING],
|
40
|
+
# ["O", csr_options[:organization], OpenSSL::ASN1::UTF8STRING],
|
41
|
+
# ["OU", csr_options[:organization_unit], OpenSSL::ASN1::UTF8STRING],
|
42
|
+
# ["CN", csr_options[:common_name], OpenSSL::ASN1::UTF8STRING]
|
43
|
+
# ])
|
44
|
+
|
45
|
+
# extensions = [
|
46
|
+
# OpenSSL::X509::ExtensionFactory.new.create_extension("subjectAltName")
|
47
|
+
# ]
|
48
|
+
|
49
|
+
# # add SAN extension to the CSR
|
50
|
+
# attribute_values = OpenSSL::ASN1::Set [OpenSSL::ASN1::Sequence(extensions)]
|
51
|
+
# [
|
52
|
+
# OpenSSL::X509::Attribute.new("SN", attribute_values),
|
53
|
+
# OpenSSL::X509::Attribute.new("UID", attribute_values),
|
54
|
+
# OpenSSL::X509::Attribute.new("title", attribute_values),
|
55
|
+
# OpenSSL::X509::Attribute.new("registeredAddress", attribute_values),
|
56
|
+
# OpenSSL::X509::Attribute.new("businessCategory", attribute_values)
|
57
|
+
# ]
|
58
|
+
|
59
|
+
# attribute_values.each do |attribute|
|
60
|
+
# request.add_attribute attribute
|
61
|
+
# end
|
62
|
+
|
63
|
+
# request.public_key = public_key
|
64
|
+
# csr = request.sign(key, OpenSSL::Digest.new("SHA256"))
|
65
|
+
# csr_pem = csr.to_pem
|
66
|
+
# csr_without_headers = csr_pem
|
67
|
+
# .to_s
|
68
|
+
# .gsub("-----BEGIN CERTIFICATE REQUEST-----", "")
|
69
|
+
# .gsub("-----END CERTIFICATE REQUEST-----", "")
|
70
|
+
# .delete("\n")
|
71
|
+
|
72
|
+
# {
|
73
|
+
# csr: csr_pem,
|
74
|
+
# csr_without_headers: csr_without_headers
|
75
|
+
# }
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def default_csr_options
|
81
|
+
{
|
82
|
+
common_name: "",
|
83
|
+
organization_identifier: "",
|
84
|
+
organization_name: "",
|
85
|
+
organization_unit: "",
|
86
|
+
country: "SA",
|
87
|
+
invoice_type: "1100",
|
88
|
+
address: "",
|
89
|
+
business_category: "",
|
90
|
+
egs_solution_name: "",
|
91
|
+
egs_model: "",
|
92
|
+
egs_serial_number: ""
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
def set_key
|
97
|
+
if private_key_provided? && @key.blank?
|
98
|
+
@key = OpenSSL::PKey::EC.new(
|
99
|
+
File.read(@private_key_path),
|
100
|
+
@private_key_password
|
101
|
+
)
|
102
|
+
else
|
103
|
+
generate_key
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def generated_key?
|
108
|
+
@key.present?
|
109
|
+
end
|
110
|
+
|
111
|
+
def private_key_provided?
|
112
|
+
@private_key_path.present?
|
113
|
+
end
|
114
|
+
|
115
|
+
def generate_key
|
116
|
+
return if private_key_provided?
|
117
|
+
|
118
|
+
temp_key = OpenSSL::PKey::EC.new("secp256k1").generate_key
|
119
|
+
@generated_private_key_path = "./#{SecureRandom.uuid}.pem"
|
120
|
+
@key = temp_key
|
121
|
+
|
122
|
+
File.write(@generated_private_key_path, @key.to_pem)
|
123
|
+
end
|
124
|
+
|
125
|
+
def delete_generated_key
|
126
|
+
File.delete(@generated_private_key_path) if @generated_private_key_path.present?
|
127
|
+
end
|
128
|
+
|
129
|
+
def cert_environment
|
130
|
+
case mode
|
131
|
+
when :production
|
132
|
+
"ZATCA-Code-Signing"
|
133
|
+
when :simulation
|
134
|
+
"PREZATCA-Code-Signing"
|
135
|
+
when :sandbox
|
136
|
+
"TSTZATCA-Code-Signing"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def generate_openssl_csr_command
|
141
|
+
"openssl req -new -sha256 -key #{@private_key_path || @generated_private_key_path} -config #{@csr_config_path}"
|
142
|
+
end
|
143
|
+
|
144
|
+
def write_csr_config
|
145
|
+
@csr_config_path = "./#{SecureRandom.uuid}.conf"
|
146
|
+
File.write(@csr_config_path, generate_csr_config)
|
147
|
+
end
|
148
|
+
|
149
|
+
def cleanup_leftover_files
|
150
|
+
delete_generated_key
|
151
|
+
delete_csr_config_file
|
152
|
+
end
|
153
|
+
|
154
|
+
def delete_csr_config_file
|
155
|
+
File.delete(@csr_config_path) if @csr_config_path.present?
|
156
|
+
end
|
157
|
+
|
158
|
+
def egs_serial_number
|
159
|
+
"1-#{csr_options[:egs_solution_name]}|2-#{csr_options[:egs_model]}|3-#{csr_options[:egs_serial_number]}"
|
160
|
+
end
|
161
|
+
|
162
|
+
# Adapted from:
|
163
|
+
# https://github.com/wes4m/zatca-xml-js/blob/main/src/zatca/templates/csr_template.ts
|
164
|
+
def generate_csr_config
|
165
|
+
<<~TEMPLATE
|
166
|
+
# ------------------------------------------------------------------
|
167
|
+
# Default section for "req" command csr_options
|
168
|
+
# ------------------------------------------------------------------
|
169
|
+
[req]
|
170
|
+
|
171
|
+
# Password for reading in existing private key file
|
172
|
+
# input_password = todo_private_key_password
|
173
|
+
|
174
|
+
# Prompt for DN field values and CSR attributes in ASCII
|
175
|
+
prompt = no
|
176
|
+
utf8 = no
|
177
|
+
|
178
|
+
# Section pointer for DN field csr_options
|
179
|
+
distinguished_name = my_req_dn_prompt
|
180
|
+
|
181
|
+
# Extensions
|
182
|
+
req_extensions = v3_req
|
183
|
+
|
184
|
+
[ v3_req ]
|
185
|
+
#basicConstraints=CA:FALSE
|
186
|
+
#keyUsage = digitalSignature, keyEncipherment
|
187
|
+
# Production or Testing Template (TSTZATCA-Code-Signing - ZATCA-Code-Signing)
|
188
|
+
1.3.6.1.4.1.311.20.2 = ASN1:PRINTABLESTRING:#{cert_environment}
|
189
|
+
subjectAltName=dirName:dir_sect
|
190
|
+
|
191
|
+
[ dir_sect ]
|
192
|
+
# EGS Serial number (1-SolutionName|2-ModelOrVersion|3-serialNumber)
|
193
|
+
SN = #{egs_serial_number}
|
194
|
+
# VAT Registration number of TaxPayer (Organization identifier [15 digits begins with 3 and ends with 3])
|
195
|
+
UID = #{csr_options[:organization_identifier]}
|
196
|
+
# Invoice type (TSCZ)(1 = supported, 0 not supported) (Tax, Simplified, future use, future use)
|
197
|
+
title = #{csr_options[:invoice_type]}
|
198
|
+
# Location (branch address or website)
|
199
|
+
registeredAddress = #{csr_options[:address]}
|
200
|
+
# Industry (industry sector name)
|
201
|
+
businessCategory = #{csr_options[:business_category]}
|
202
|
+
|
203
|
+
# ------------------------------------------------------------------
|
204
|
+
# Section for prompting DN field values to create "subject"
|
205
|
+
# ------------------------------------------------------------------
|
206
|
+
[my_req_dn_prompt]
|
207
|
+
# Common name (EGS TaxPayer PROVIDED ID [FREE TEXT])
|
208
|
+
commonName = #{csr_options[:common_name]}
|
209
|
+
|
210
|
+
# Organization Unit (Branch name)
|
211
|
+
organizationalUnitName = #{csr_options[:organization_unit]}
|
212
|
+
|
213
|
+
# Organization name (Tax payer name)
|
214
|
+
organizationName = #{csr_options[:organization_name]}
|
215
|
+
|
216
|
+
# ISO2 country code is required with US as default
|
217
|
+
countryName = #{csr_options[:country]}
|
218
|
+
TEMPLATE
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "starkbank-ecdsa"
|
2
|
+
|
3
|
+
class ZATCA::Signing::ECDSA
|
4
|
+
def self.sign(content:, private_key: nil, private_key_path: nil, decode_from_base64: false)
|
5
|
+
private_key = parse_private_key(key: private_key, key_path: private_key_path, decode_from_base64: decode_from_base64)
|
6
|
+
|
7
|
+
ecdsa_signature = EllipticCurve::Ecdsa.sign(content, private_key)
|
8
|
+
|
9
|
+
{
|
10
|
+
base64: ecdsa_signature.toBase64,
|
11
|
+
bytes: ecdsa_signature.toDer
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.add_header_blocks(key_content)
|
16
|
+
# If key is missing header blocks, add them, otherwise return it as is
|
17
|
+
header = "-----BEGIN EC PRIVATE KEY-----"
|
18
|
+
footer = "-----END EC PRIVATE KEY-----"
|
19
|
+
|
20
|
+
unless key_content.include?(header) && key_content.include?(footer)
|
21
|
+
key_content = "#{header}\n#{key_content}\n#{footer}"
|
22
|
+
end
|
23
|
+
|
24
|
+
key_content
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.read_private_key_from_pem(pem)
|
28
|
+
EllipticCurve::PrivateKey.fromPem(add_header_blocks(pem))
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a parsed private key
|
32
|
+
# If decode_from_base64 is set to true, the key will be decoded from base64
|
33
|
+
# before passing to OpenSSL to read it. # This is necessary because that's how
|
34
|
+
# ZATCA's sample key is provided.
|
35
|
+
def self.parse_private_key(key: nil, key_path: nil, decode_from_base64: false)
|
36
|
+
parsed_key = if key.is_a?(EllipticCurve::PrivateKey)
|
37
|
+
key
|
38
|
+
elsif key.is_a?(String)
|
39
|
+
key_content = if decode_from_base64
|
40
|
+
Base64.decode64(key)
|
41
|
+
else
|
42
|
+
key
|
43
|
+
end
|
44
|
+
|
45
|
+
read_private_key_from_pem(key_content)
|
46
|
+
elsif key_path.present?
|
47
|
+
key_content = File.read(key_path)
|
48
|
+
key_content = Base64.decode64(key_content) if decode_from_base64
|
49
|
+
|
50
|
+
read_private_key_from_pem(key_content)
|
51
|
+
end
|
52
|
+
|
53
|
+
if parsed_key.blank?
|
54
|
+
raise ArgumentError.new("private_key or private_key_path is required")
|
55
|
+
end
|
56
|
+
|
57
|
+
parsed_key
|
58
|
+
end
|
59
|
+
end
|
data/lib/zatca/tag.rb
CHANGED
@@ -5,9 +5,21 @@ module ZATCA
|
|
5
5
|
vat_registration_number: 2,
|
6
6
|
timestamp: 3,
|
7
7
|
invoice_total: 4,
|
8
|
-
vat_total: 5
|
8
|
+
vat_total: 5,
|
9
|
+
xml_invoice_hash: 6,
|
10
|
+
ecdsa_signature: 7,
|
11
|
+
ecdsa_public_key: 8,
|
12
|
+
ecdsa_stamp_signature: 9 # TODO: is this needed ?
|
9
13
|
}.freeze
|
10
14
|
|
15
|
+
PHASE_1_TAGS = [
|
16
|
+
:seller_name,
|
17
|
+
:vat_registration_number,
|
18
|
+
:timestamp,
|
19
|
+
:invoice_total,
|
20
|
+
:vat_total
|
21
|
+
].freeze
|
22
|
+
|
11
23
|
attr_accessor :id, :key, :value
|
12
24
|
|
13
25
|
def initialize(key:, value:)
|
@@ -20,14 +32,12 @@ module ZATCA
|
|
20
32
|
{id: @id, key: @key, value: @value}
|
21
33
|
end
|
22
34
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
# All of this should be in 8-bit ASCII.
|
27
|
-
tlv = @id.chr + @value.bytesize.chr + value
|
35
|
+
def should_be_utf8_encoded?
|
36
|
+
PHASE_1_TAGS.include?(key)
|
37
|
+
end
|
28
38
|
|
29
|
-
|
30
|
-
|
39
|
+
def to_tlv
|
40
|
+
tlv = @id.chr + @value.bytesize.chr + @value
|
31
41
|
tlv.force_encoding("ASCII-8BIT")
|
32
42
|
end
|
33
43
|
end
|
data/lib/zatca/tags.rb
CHANGED
@@ -33,8 +33,12 @@ module ZATCA
|
|
33
33
|
Base64.strict_encode64(to_tlv)
|
34
34
|
end
|
35
35
|
|
36
|
-
|
36
|
+
# This is helpful for debugging only, for ZATCA's requirements just call `to_base64`
|
37
|
+
def to_hex_tlv
|
38
|
+
to_tlv.unpack1("H*")
|
39
|
+
end
|
37
40
|
|
41
|
+
# This is helpful for debugging only, for ZATCA's requirements just call `to_base64`
|
38
42
|
def to_tlv
|
39
43
|
@tags.map(&:to_tlv).join("")
|
40
44
|
end
|
data/lib/zatca/tags_schema.rb
CHANGED
@@ -13,10 +13,10 @@ module ZATCA
|
|
13
13
|
required(:invoice_total).filled(:string)
|
14
14
|
required(:vat_total).filled(:string)
|
15
15
|
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
# Data types required for Phase 2 by 1 January 2023
|
17
|
+
optional(:xml_invoice_hash).filled(:string)
|
18
|
+
optional(:ecdsa_signature).filled(:string)
|
19
|
+
optional(:ecdsa_public_key).filled(:string)
|
20
|
+
optional(:ecdsa_stamp_signature).filled(:string)
|
21
21
|
end
|
22
22
|
end
|
data/lib/zatca/types.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
require "dry-initializer"
|
2
|
+
require_relative "../types"
|
3
|
+
|
4
|
+
class ZATCA::UBL::BaseComponent
|
5
|
+
extend Dry::Initializer
|
6
|
+
attr_accessor :elements, :attributes, :name, :value, :index
|
7
|
+
|
8
|
+
# def self.guard_dig(obj)
|
9
|
+
# unless obj.respond_to?(:dig)
|
10
|
+
# raise TypeError, "#{obj.class.name} does not have #dig method"
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
|
14
|
+
ArrayOfBaseComponentOrNil = ZATCA::Types::Array.of(ZATCA::Types.Instance(ZATCA::UBL::BaseComponent))
|
15
|
+
.default { [] }
|
16
|
+
.constructor { |value| value.blank? ? [] : value.compact }
|
17
|
+
|
18
|
+
option :elements,
|
19
|
+
type: ArrayOfBaseComponentOrNil,
|
20
|
+
default: proc { [] },
|
21
|
+
optional: true
|
22
|
+
|
23
|
+
option :attributes, type: ZATCA::Types::Strict::Hash, default: proc { {} }, optional: true
|
24
|
+
option :value, type: ZATCA::Types::Coercible::String, default: proc { "" }, optional: true
|
25
|
+
option :name, type: ZATCA::Types::Strict::String, default: proc { "" }, optional: true
|
26
|
+
option :index, type: ZATCA::Types::Coercible::Integer.optional, default: proc {}, optional: true
|
27
|
+
|
28
|
+
# def initialize(elements: [], attributes: {}, value: "", name: "", index: nil)
|
29
|
+
# @elements = elements
|
30
|
+
# @attributes = attributes
|
31
|
+
# @value = value
|
32
|
+
# @name = name
|
33
|
+
|
34
|
+
# # HACK: Add a nil index property to be used for cases where we need
|
35
|
+
# # sequential IDs, this list can be populated after the array is built
|
36
|
+
# @index = index
|
37
|
+
# end
|
38
|
+
|
39
|
+
# There are cases where we end up constructing elements with no content
|
40
|
+
# and we don't want to include them in the final XML.
|
41
|
+
#
|
42
|
+
# This method helps us to return nil if the element has no attributes,
|
43
|
+
# elements or value.
|
44
|
+
#
|
45
|
+
# which is then caught in the `build_xml` method (using `elements.compact`)
|
46
|
+
# and ignored.
|
47
|
+
def self.build(elements: [], attributes: {}, value: "", name: "", index: nil)
|
48
|
+
return nil if elements.blank? && attributes.blank? && value.blank?
|
49
|
+
|
50
|
+
new(elements: elements, attributes: attributes, value: value, name: name, index: index)
|
51
|
+
end
|
52
|
+
|
53
|
+
def [](name)
|
54
|
+
elements.find { |element| element.name == name }
|
55
|
+
end
|
56
|
+
|
57
|
+
def dig(key, *args)
|
58
|
+
value = self[key]
|
59
|
+
return value if args.length == 0 || value.nil?
|
60
|
+
# DigRb.guard_dig(value)
|
61
|
+
value.dig(*args)
|
62
|
+
end
|
63
|
+
|
64
|
+
# TODO: Under Active Development
|
65
|
+
def find_nested_element_by_path(path)
|
66
|
+
path_parts = path.split("/")
|
67
|
+
nested_element = self
|
68
|
+
|
69
|
+
path_parts.each_with_index do |path_part, index|
|
70
|
+
# byebug
|
71
|
+
# next_path_part = path_parts[index + 1]
|
72
|
+
# found_element = nested_element[path_part]
|
73
|
+
# found_next_path_part = found_element[next_path_part]
|
74
|
+
|
75
|
+
# element_with_next_path_part = found_element.find do |child_element|
|
76
|
+
# child_element.name == next_path_part
|
77
|
+
# end
|
78
|
+
# byebug
|
79
|
+
# nested_element.elements.each do |element|
|
80
|
+
# byebug
|
81
|
+
# next_element = element[path_part]
|
82
|
+
|
83
|
+
# if next_element && next_element[next_path_part]
|
84
|
+
# nested_element = next_element[next_path_part]
|
85
|
+
# break
|
86
|
+
# elsif next_element.present?
|
87
|
+
# nested_element = next_element
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
|
91
|
+
# nested_element = found_element if found_element.present?
|
92
|
+
end
|
93
|
+
|
94
|
+
nested_element
|
95
|
+
end
|
96
|
+
|
97
|
+
def schema
|
98
|
+
self.class.schema
|
99
|
+
end
|
100
|
+
|
101
|
+
def to_h
|
102
|
+
{
|
103
|
+
name => {
|
104
|
+
attributes: attributes,
|
105
|
+
**elements.map(&:to_h),
|
106
|
+
value: value
|
107
|
+
}
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
def to_xml
|
112
|
+
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
113
|
+
xml.root { build_xml(xml) }
|
114
|
+
end
|
115
|
+
|
116
|
+
builder.to_xml
|
117
|
+
end
|
118
|
+
|
119
|
+
def build_xml(xml)
|
120
|
+
xml.send(name, attributes) do
|
121
|
+
if elements.length > 0
|
122
|
+
elements.compact.each { |element| element.build_xml(xml) }
|
123
|
+
elsif value.present?
|
124
|
+
xml.text(value)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def generate_xml(
|
130
|
+
canonicalized: false,
|
131
|
+
spaces: 2,
|
132
|
+
apply_invoice_hacks: false,
|
133
|
+
remove_root_xml_tag: false
|
134
|
+
)
|
135
|
+
ZATCA::UBL::Builder.new(element: self).build(
|
136
|
+
canonicalized: canonicalized,
|
137
|
+
spaces: spaces,
|
138
|
+
apply_invoice_hacks: apply_invoice_hacks,
|
139
|
+
remove_root_xml_tag: remove_root_xml_tag
|
140
|
+
)
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
class ZATCA::UBL::Builder
|
2
|
+
extend Dry::Initializer
|
3
|
+
|
4
|
+
option :element, type: ZATCA::Types.Instance(ZATCA::UBL::BaseComponent)
|
5
|
+
|
6
|
+
def build(
|
7
|
+
canonicalized: false,
|
8
|
+
spaces: 4,
|
9
|
+
apply_invoice_hacks: false,
|
10
|
+
remove_root_xml_tag: false
|
11
|
+
)
|
12
|
+
@remove_root_xml_tag = remove_root_xml_tag
|
13
|
+
|
14
|
+
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
15
|
+
element.build_xml(xml)
|
16
|
+
end
|
17
|
+
|
18
|
+
xml = if canonicalized
|
19
|
+
canonicalized_xml(builder: builder)
|
20
|
+
else
|
21
|
+
uncanonicalized_xml(builder: builder, spaces: spaces)
|
22
|
+
end
|
23
|
+
|
24
|
+
xml = apply_hacks_to_invoice(element, xml) if apply_invoice_hacks
|
25
|
+
|
26
|
+
xml
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# ZATCA sadly requires very specific and unconventional indentation in the XML
|
32
|
+
# when it is pretty (uncanonicalized), the only way we can accomplish this is
|
33
|
+
# to find and replace blocks manually.
|
34
|
+
def apply_hacks_to_invoice(element, xml)
|
35
|
+
return xml unless element.is_a?(ZATCA::UBL::Invoice)
|
36
|
+
|
37
|
+
apply_qualifying_properties_hacks(element, xml)
|
38
|
+
end
|
39
|
+
|
40
|
+
def apply_qualifying_properties_hacks(invoice, xml)
|
41
|
+
return xml if invoice.qualifying_properties.blank?
|
42
|
+
|
43
|
+
regex = ZATCA::Hacks.qualifying_properties_regex
|
44
|
+
|
45
|
+
xml.gsub(regex, invoice.qualifying_properties)
|
46
|
+
end
|
47
|
+
|
48
|
+
# This function does not produce canonicalization matching C14N 1.1, it applies
|
49
|
+
# C14N 1.1 then manually adds back the whitespace in the format that ZATCA
|
50
|
+
# expects.
|
51
|
+
def canonicalized_xml(builder:)
|
52
|
+
builder.doc.canonicalize(Nokogiri::XML::XML_C14N_1_1)
|
53
|
+
|
54
|
+
# TODO: In case ZATCA ever asks us to use their whitespace format again.
|
55
|
+
# In some meetings they say we have to use it, in some meetings they say
|
56
|
+
# we don't. The simpler approach is that we don't use it.
|
57
|
+
#
|
58
|
+
# ZATCA's docs specifically state we must use C14N 1.1 canonicalization.
|
59
|
+
# xml = uncanonicalized_xml(builder: builder, spaces: 4)
|
60
|
+
# xml_doc = Nokogiri::XML(xml)
|
61
|
+
|
62
|
+
# canonical_xml = xml_doc.canonicalize(Nokogiri::XML::XML_C14N_1_1)
|
63
|
+
|
64
|
+
# canonical_xml
|
65
|
+
end
|
66
|
+
|
67
|
+
def uncanonicalized_xml(builder:, spaces: 4)
|
68
|
+
builder.to_xml(indent: spaces.to_i)
|
69
|
+
|
70
|
+
# xml = builder.to_xml(indent: spaces.to_i)
|
71
|
+
# xml = match_xml_string_to_zatca_whitespaces(xml)
|
72
|
+
# xml
|
73
|
+
end
|
74
|
+
|
75
|
+
def match_xml_string_to_zatca_whitespaces(xml)
|
76
|
+
# ZATCA has elements that are not spaced by multiples of 4, and random new
|
77
|
+
# lines with trailing whitespaces, so we need to manually adjust our
|
78
|
+
# indentation to match ZATCA's.
|
79
|
+
zatca_weird_whitespaces.each do |whitespace_hash|
|
80
|
+
xml.gsub!(whitespace_hash[:our_version], whitespace_hash[:zatca_version])
|
81
|
+
end
|
82
|
+
|
83
|
+
# Canonicalization already removes the root XML tag for us, but since we had
|
84
|
+
# to create a new uncanonicalized document for ZATCA's invoice hacks, we
|
85
|
+
# have to remove it manually.
|
86
|
+
if @remove_root_xml_tag
|
87
|
+
xml.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "")
|
88
|
+
end
|
89
|
+
|
90
|
+
# ZATCA Removes the final newline character, so we do the same
|
91
|
+
xml.chomp
|
92
|
+
|
93
|
+
# This part is not clear, ZATCA shared documents with us that use CRLF
|
94
|
+
# but the samples in the SDK use LF, so we're not sure which one is correct.
|
95
|
+
# ZATCA wants CRLF (\r\n) in their canonicalized form instead of just LF (\n)
|
96
|
+
xml.gsub!("\n", "\r\n")
|
97
|
+
|
98
|
+
xml
|
99
|
+
end
|
100
|
+
|
101
|
+
# Not sure if this is needed, in some meetings ZATCA says you have to match
|
102
|
+
# their whitspace exactly and in some meetings they say you don't.
|
103
|
+
# HACK: This is really hacky, using regexes or XPaths would be better, but
|
104
|
+
# that wasn't easy to build and maintain, so we're using this if/until we run
|
105
|
+
# into issues.
|
106
|
+
#
|
107
|
+
# We may eventually go to an approach where we just have hardcoded XML in each
|
108
|
+
# element's file and we just add in the values instead of generating our own
|
109
|
+
# XML if this gets too hard to maintain.
|
110
|
+
def zatca_weird_whitespaces
|
111
|
+
@_zatca_weird_whitespaces ||= [
|
112
|
+
{
|
113
|
+
our_version: "<cbc:ProfileID>",
|
114
|
+
zatca_version: "\n <cbc:ProfileID>"
|
115
|
+
},
|
116
|
+
{
|
117
|
+
our_version: "<cac:AccountingSupplierParty>",
|
118
|
+
zatca_version: "\n \n <cac:AccountingSupplierParty>"
|
119
|
+
},
|
120
|
+
{
|
121
|
+
our_version: " <cbc:CompanyID>",
|
122
|
+
zatca_version: " <cbc:CompanyID>"
|
123
|
+
},
|
124
|
+
{
|
125
|
+
our_version: " <cac:TaxCategory>",
|
126
|
+
zatca_version: " <cac:TaxCategory>"
|
127
|
+
},
|
128
|
+
{
|
129
|
+
our_version: " </cac:TaxCategory>",
|
130
|
+
zatca_version: " </cac:TaxCategory>"
|
131
|
+
},
|
132
|
+
{
|
133
|
+
our_version: ' <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5305">S</cbc:ID>',
|
134
|
+
zatca_version: ' <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5305">S</cbc:ID>'
|
135
|
+
},
|
136
|
+
{
|
137
|
+
our_version: "<cac:TaxScheme>\n <cbc:ID schemeAgencyID=\"6\" schemeID=\"UN/ECE 5153\">VAT</cbc:ID>",
|
138
|
+
zatca_version: "<cac:TaxScheme>\n <cbc:ID schemeAgencyID=\"6\" schemeID=\"UN/ECE 5153\">VAT</cbc:ID>"
|
139
|
+
},
|
140
|
+
{
|
141
|
+
our_version: "<cac:TaxTotal>\n <cbc:TaxAmount currencyID=\"SAR\">",
|
142
|
+
zatca_version: "<cac:TaxTotal>\n <cbc:TaxAmount currencyID=\"SAR\">"
|
143
|
+
},
|
144
|
+
{
|
145
|
+
our_version: " <cbc:RoundingAmount currencyID=\"SAR\">",
|
146
|
+
zatca_version: " <cbc:RoundingAmount currencyID=\"SAR\">"
|
147
|
+
},
|
148
|
+
{
|
149
|
+
our_version: " <cbc:ChargeIndicator>",
|
150
|
+
zatca_version: " <cbc:ChargeIndicator>"
|
151
|
+
},
|
152
|
+
{
|
153
|
+
our_version: " <cbc:AllowanceChargeReason>",
|
154
|
+
zatca_version: " <cbc:AllowanceChargeReason>"
|
155
|
+
},
|
156
|
+
{
|
157
|
+
our_version: " <cbc:Amount currencyID=\"SAR\">",
|
158
|
+
zatca_version: " <cbc:Amount currencyID=\"SAR\">"
|
159
|
+
},
|
160
|
+
{
|
161
|
+
our_version: "<cbc:ID schemeAgencyID=\"6\" schemeID=\"UN/ECE 5305\">S</cbc:ID>\n <cbc:Percent>",
|
162
|
+
zatca_version: "<cbc:ID schemeAgencyID=\"6\" schemeID=\"UN/ECE 5305\">S</cbc:ID>\n <cbc:Percent>"
|
163
|
+
}
|
164
|
+
]
|
165
|
+
end
|
166
|
+
end
|