发票哈希 API 正文与 XML 的(计算的)哈希不匹配

问题描述 投票:0回答:1

两个月前我最后一次使用 Zatca 时,它运行正常。我使用 XML 进行了测试,完成了整个过程,一切似乎都正常。但从昨天开始,我尝试使用相同的代码和相同的 XML 进行登录,这向我抛出了这个错误。而且根本不工作

{“validationResults”:{“infoMessages”:[],“warningMessages”:[],“errorMessages”:[{“type”:“错误”,“code”:“无效发票哈希”,“类别":"INVOICE_HASHING_ERRORS","message":"发票哈希 API 正文与 XML 的(计算的)哈希不匹配","status":"ERROR"},{"type":"ERROR","code" :"XSD_ZATCA_INVALID","category":"XSD 验证","message":"架构验证失败;XML 不符合符合 ZATCA 规范的 UBL 2.1 标准","status":"ERROR"}],"status ":"错误"},"reportingStatus":null,"clearanceStatus":"NOT_CLEARED","qrSellertStatus":null,"qrBuyertStatus":null}

这是我用于模拟合规性的 XML

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI>
<ext:ExtensionContent>
<sig:UBLDocumentSignatures xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">
<sac:SignatureInformation>
<cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID>
<sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoice</sbc:ReferencedSignatureID>
<ds:Signature Id="signature" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2006/12/xml-c14n11"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256"/>
<ds:Reference Id="invoiceSignedData" URI="">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
<ds:XPath>not(//ancestor-or-self::ext:UBLExtensions)</ds:XPath>
</ds:Transform>
<ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
<ds:XPath>not(//ancestor-or-self::cac:Signature)</ds:XPath>
</ds:Transform>
<ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
<ds:XPath>not(//ancestor-or-self::cac:AdditionalDocumentReference[cbc:ID='QR'])</ds:XPath>
</ds:Transform>
<ds:Transform Algorithm="http://www.w3.org/2006/12/xml-c14n11"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>7R4920paLHdfDZgCqSB+zL4IGNGHZ+xRLXWWkYnKISg=</ds:DigestValue>
</ds:Reference>
<ds:Reference Type="http://www.w3.org/2000/09/xmldsig#SignatureProperties" URI="#xadesSignedProperties">
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>YWY3MTQzZjQ5YjYxYmY2MzhlOTI1MzY3ZWYyNTdmODA3YjQwM2E5YmRhNzQyM2NhNjE2Mzc1MDlmZTMyZDE4MA==</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>rJS0dVKxI/U8M8PlR7zjRO2QMoMeTdbyxqNNMoXQPGrwM1JN6KPgBIZW66iGsojSYX2DdejE+iBwkc6pTmUw1A==</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>MIICNTCCAdugAwIBAgIGAY0307nzMAoGCCqGSM49BAMCMBUxEzARBgNVBAMMCmVJbnZvaWNpbmcwHhcNMjQwMTIzMTkzNzE0WhcNMjkwMTIyMjEwMDAwWjB4MQswCQYDVQQGEwJTQTEVMBMGA1UECwwMUml5YWQgQnJhbmNoMRgwFgYDVQRhDA8zMTA5OTEyNTYyMDAwMDMxGDAWBgNVBAoMD1plZG5hIEluZm8gVGVjaDEeMBwGA1UEAwwVUFJFWkFUQ0EtQ29kZS1TaWduaW5nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE/ELFYPr0rGw1+7rQorrpRPBMPWKwGTwgMGyZfNKye/nCzLpygOxNR5rQCymcxKR+gOdelh6Yd68XrodR6iwxwqOBtjCBszAMBgNVHRMBAf8EAjAAMIGiBgNVHREEgZowgZekgZQwgZExPDA6BgNVBAQMMzEtWmVkbmF8Mi1WMXwzLTFmMjliOTY0LWY4MzAtNDNiOC05MjdkLWVhMTkwZTA5MzM3NDEfMB0GCgmSJomT8ixkAQEMDzMxMDk5MTI1NjIwMDAwMzENMAsGA1UEDAwEMTAwMDEOMAwGA1UEGgwFUml5YWQxETAPBgNVBA8MCHNvZnR3YXJlMAoGCCqGSM49BAMCA0gAMEUCIQDY0xZd9W4PoK3JfRD+8v4fUKxQBOzKh9XRaiQvbY2oKgIgff52WZZt+plYw6Jk9gLa3Tfl4gne/B57Z4BE+haVXFI=</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
<ds:Object>
<xades:QualifyingProperties Target="signature" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
<xades:SignedProperties Id="xadesSignedProperties">
<xades:SignedSignatureProperties>
<xades:SigningTime>2024-01-21T02:32:02</xades:SigningTime>
<xades:SigningCertificate>
<xades:Cert>
<xades:CertDigest>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>NWFhZmZmNDdjZjFiNjJlNTM0ZDIzMmMzMzQ0OGMxZTNmZGZjZGJiNzI5MzE1MzIwNGUzYTg2NWIwNTliZmU5Mg==</ds:DigestValue>
</xades:CertDigest>
<xades:IssuerSerial>
<ds:X509IssuerName>CN=eInvoicing</ds:X509IssuerName>
<ds:X509SerialNumber>1706038639091</ds:X509SerialNumber>
</xades:IssuerSerial>
</xades:Cert>
</xades:SigningCertificate>
</xades:SignedSignatureProperties>
</xades:SignedProperties>
</xades:QualifyingProperties>
</ds:Object>
</ds:Signature>
</sac:SignatureInformation>
</sig:UBLDocumentSignatures>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions><cbc:ProfileID>reporting:1.0</cbc:ProfileID>
<cbc:ID>INV104</cbc:ID>
<cbc:UUID>cae7dfce-0dd7-488b-97f2-2504f79d3713</cbc:UUID>
<cbc:IssueDate>2024-01-21</cbc:IssueDate>
<cbc:IssueTime>02:32:02</cbc:IssueTime>
<cbc:InvoiceTypeCode name="0100000">388</cbc:InvoiceTypeCode>
<cbc:Note languageID="ar">ABC</cbc:Note>
<cbc:DocumentCurrencyCode>SAR</cbc:DocumentCurrencyCode>
<cbc:TaxCurrencyCode>SAR</cbc:TaxCurrencyCode>
<cac:AdditionalDocumentReference>
<cbc:ID>ICV</cbc:ID>
<cbc:UUID>1</cbc:UUID>
</cac:AdditionalDocumentReference>
<cac:AdditionalDocumentReference>
<cbc:ID>PIH</cbc:ID>
<cac:Attachment>
<cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==</cbc:EmbeddedDocumentBinaryObject>
</cac:Attachment>
</cac:AdditionalDocumentReference>
<cac:AdditionalDocumentReference>
<cbc:ID>QR</cbc:ID>
<cac:Attachment>
<cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">AQxBbHRhZiBNaWF6ZWUCDzMxMDk5MTI1NjIwMDAwMwMTMjAyNC0wMS0yMVQwMjozMjowMgQGMjMwMC4wBQUzMDAuMAYsN1I0OTIwcGFMSGRmRFpnQ3FTQit6TDRJR05HSForeFJMWFdXa1luS0lTZz0HWHJKUzBkVkt4SS9VOE04UGxSN3pqUk8yUU1vTWVUZGJ5eHFOTk1vWFFQR3J3TTFKTjZLUGdCSVpXNjZpR3NvalNZWDJEZGVqRStpQndrYzZwVG1VdzFBPT0IODA2MBAGByqGSM49AgEGBSuBBAAKAyIAAvxCxWD69KxsNfu60KK66UTwTD1isBk8IDBsmXzSsnv5CUcwRQIhANjTFl31bg+grcl9EP7y/h9QrFAE7MqH1dFqJC9tjagqAiB9/nZZlm36mVjDomT2AtrdN+XiCd78HntngET6FpVcUg==</cbc:EmbeddedDocumentBinaryObject>
</cac:Attachment>
</cac:AdditionalDocumentReference><cac:Signature>
<cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID>
<cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod>
</cac:Signature><cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeID="CRN">310991252200003</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:StreetName>noakhali</cbc:StreetName>
<cbc:AdditionalStreetName>noakhali</cbc:AdditionalStreetName>
<cbc:BuildingNumber>1234</cbc:BuildingNumber>
<cbc:PlotIdentification>12344</cbc:PlotIdentification>
<cbc:CitySubdivisionName>dhaka</cbc:CitySubdivisionName>
<cbc:CityName>dhaka</cbc:CityName>
<cbc:PostalZone>12234</cbc:PostalZone>
<cbc:CountrySubentity>noakhali</cbc:CountrySubentity>
<cac:Country>
<cbc:IdentificationCode>SA</cbc:IdentificationCode>
</cac:Country>
</cac:PostalAddress>
<cac:PartyTaxScheme>
<cbc:CompanyID>310991236200003</cbc:CompanyID>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:PartyTaxScheme>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Altaf Miazee</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cac:PostalAddress>
<cbc:StreetName>dhaka</cbc:StreetName>
<cbc:AdditionalStreetName>Dhaka</cbc:AdditionalStreetName>
<cbc:BuildingNumber>1234</cbc:BuildingNumber>
<cbc:PlotIdentification>1234</cbc:PlotIdentification>
<cbc:CitySubdivisionName>dhaka</cbc:CitySubdivisionName>
<cbc:CityName>riyadh</cbc:CityName>
<cbc:PostalZone>12235</cbc:PostalZone>
<cbc:CountrySubentity>Dhaka</cbc:CountrySubentity>
<cac:Country>
<cbc:IdentificationCode>SA</cbc:IdentificationCode>
</cac:Country>
</cac:PostalAddress>
<cac:PartyTaxScheme>
<cbc:CompanyID>309876543098333</cbc:CompanyID>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:PartyTaxScheme>
<cac:PartyLegalEntity>
<cbc:RegistrationName>James</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:Delivery>
<cbc:ActualDeliveryDate>2024-01-26</cbc:ActualDeliveryDate>
</cac:Delivery>
<cac:PaymentMeans>
<cbc:PaymentMeansCode>10</cbc:PaymentMeansCode>
</cac:PaymentMeans>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>true</cbc:ChargeIndicator>
<cbc:AllowanceChargeReasonCode>CG</cbc:AllowanceChargeReasonCode>
<cbc:AllowanceChargeReason>Cleaning</cbc:AllowanceChargeReason>
<cbc:Amount currencyID="SAR">0.0</cbc:Amount>
<cac:TaxCategory>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5305">S</cbc:ID>
<cbc:Percent>15.0</cbc:Percent>
<cac:TaxScheme>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">VAT</cbc:ID>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:AllowanceCharge>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:AllowanceChargeReasonCode>95</cbc:AllowanceChargeReasonCode>
<cbc:AllowanceChargeReason>Discount</cbc:AllowanceChargeReason>
<cbc:Amount currencyID="SAR">0.0</cbc:Amount>
<cac:TaxCategory>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5305">S</cbc:ID> 
<cbc:Percent>15.0</cbc:Percent>
<cac:TaxScheme>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">VAT</cbc:ID>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:AllowanceCharge>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="SAR">300.0</cbc:TaxAmount>
</cac:TaxTotal>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="SAR">300.0</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="SAR">2000.0</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="SAR">300.0</cbc:TaxAmount>
<cac:TaxCategory>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5305">S</cbc:ID>
<cbc:Percent>15.0</cbc:Percent>
<cac:TaxScheme>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">VAT</cbc:ID>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="SAR">2000.0</cbc:LineExtensionAmount>
<cbc:TaxExclusiveAmount currencyID="SAR">2000.0</cbc:TaxExclusiveAmount>
<cbc:TaxInclusiveAmount currencyID="SAR">2300.0</cbc:TaxInclusiveAmount>
<cbc:AllowanceTotalAmount currencyID="SAR">0.0</cbc:AllowanceTotalAmount>
<cbc:ChargeTotalAmount currencyID="SAR">0.0</cbc:ChargeTotalAmount>
<cbc:PrepaidAmount currencyID="SAR">0.0</cbc:PrepaidAmount>
<cbc:PayableAmount currencyID="SAR">2300.0</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>
<cbc:InvoicedQuantity unitCode="PCE">2</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="SAR">2000.0</cbc:LineExtensionAmount>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="SAR">300.0</cbc:TaxAmount>
<cbc:RoundingAmount currencyID="SAR">2300.0</cbc:RoundingAmount>
</cac:TaxTotal>
<cac:Item>
<cbc:Name>new product</cbc:Name>
<cac:SellersItemIdentification>
<cbc:ID>1</cbc:ID>
</cac:SellersItemIdentification>
<cac:ClassifiedTaxCategory>
<cbc:ID>S</cbc:ID>
<cbc:Percent>15.0</cbc:Percent>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="SAR">1000.0</cbc:PriceAmount>
<cbc:BaseQuantity unitCode="PCE">1</cbc:BaseQuantity>
</cac:Price>
</cac:InvoiceLine>
</Invoice>

这是我用 python 进行的哈希计算

 et = ET.parse('Final_invoice_as_XML.xml')
            # # find the UUID element
            uuid_element = et.find('.//cbc:UUID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
            hash_value = et.find('.//ds:DigestValue', namespaces={'ds': 'http://www.w3.org/2000/09/xmldsig#'})

            # # extract the text value of the UUID element
            uuid = uuid_element.text
            hash_value = hash_value.text
            final_XML_invoice = str(final_XML_invoice)
            final_XML_invoice = bytes(final_XML_invoice, 'utf-8')
            # Store the encoded invoice data in base64 format
            encoded_invoice = base64.b64encode(final_XML_invoice).decode()
python xml xml-parsing
1个回答
0
投票

也请始终分享您的导入。您应该必须学习调试代码:

import xml.etree.ElementTree as ET
import base64

et = ET.parse('final_XML_invoice.xml')
# # find the UUID element
uuid_element = et.find('.//cbc:UUID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
hash_value = et.find('.//ds:DigestValue', namespaces={'ds': 'http://www.w3.org/2000/09/xmldsig#'})

# # extract the text value of the UUID element
uuid = uuid_element.text
print(uuid)
hash_value = hash_value.text
final_XML_invoice = "final_XML_invoice"
final_XML_invoice = bytes(final_XML_invoice, 'utf-8')
# Store the encoded invoice data in base64 format
encoded_invoice = base64.b64encode(final_XML_invoice).decode()
print(encoded_invoice)

输出: cae7dfce-0dd7-488b-97f2-2504f79d3713 ZmluYWxfWE1MX2ludm9pY2U=

© www.soinside.com 2019 - 2024. All rights reserved.