Cita:
Empezado por thinkows
Buenos días creía que era el único en desarrollar en anacrónico VB6 que alegría me has dado, yo también genero el xml y firmo con chilkat ... pero el envío a Guipuzkoa lo tengo muy verde, estoy probando con Advanced Rest Client de SoftEmule el ejemplo que ha puesto un compañero pero siempre me da error 500, podrías poner algun ejemplo de envio a Guipuzkoa ??? te estaría muy agradecido y si necesitas algo mi
aquí estoy ....
Saludos y gracias de antemano
|
Aqui tienes lo que tengo hasta el momento, está un poco guarrete y con cosas a piñón, ya que estoy de pruebas aún, pero como punto de partida te funcionará, por lo menos para Gipuzkoa, yo tengo que implemetar los tres envíos. Como ves usando Chilkat no es muy complejo, además hay ejemplos y utilidades https://tools.chilkat.io/xmlCreate.cshtml, puedes instalar una version trial de 30 dias.
Código:
Attribute VB_Name = "TicketBAI"
Option Explicit
Public Function GenerarXMLTbai() '(ejercicio As Integer, serie As String, factura As Long) As Boolean
Dim success As Long
Dim rsFactura As Recordset
Dim sSQL As String
success = 1
' Create the XML to be signed...
Dim xmlToSign As New ChilkatXml
xmlToSign.Tag = "T:TicketBai"
success = xmlToSign.AddAttribute("xmlns:T", "urn:ticketbai:emision")
xmlToSign.UpdateChildContent "Cabecera|IDVersionTBAI", "1.2"
xmlToSign.UpdateChildContent "Sujetos|Emisor|NIF", "B00000034"
xmlToSign.UpdateChildContent "Sujetos|Emisor|ApellidosNombreRazonSocial", "HOTEL ADIBIDEZ"
xmlToSign.UpdateChildContent "Sujetos|Destinatarios|IDDestinatario|NIF", "B26248146"
xmlToSign.UpdateChildContent "Sujetos|Destinatarios|IDDestinatario|ApellidosNombreRazonSocial", "EMPRESA LANTEGIA"
xmlToSign.UpdateChildContent "Factura|CabeceraFactura|SerieFactura", "B2022"
xmlToSign.UpdateChildContent "Factura|CabeceraFactura|NumFactura", "0100"
xmlToSign.UpdateChildContent "Factura|CabeceraFactura|FechaExpedicionFactura", "30-01-2021"
xmlToSign.UpdateChildContent "Factura|CabeceraFactura|HoraExpedicionFactura", "18:00:17"
xmlToSign.UpdateChildContent "Factura|DatosFactura|DescripcionFactura", "Servicios Hotel"
xmlToSign.UpdateChildContent "Factura|DatosFactura|DetallesFactura|IDDetalleFactura|DescripcionDetalle", ""
xmlToSign.UpdateChildContent "Factura|DatosFactura|DetallesFactura|IDDetalleFactura|Cantidad", "1"
xmlToSign.UpdateChildContent "Factura|DatosFactura|DetallesFactura|IDDetalleFactura|ImporteUnitario", "55"
xmlToSign.UpdateChildContent "Factura|DatosFactura|DetallesFactura|IDDetalleFactura|Descuento", "0"
xmlToSign.UpdateChildContent "Factura|DatosFactura|DetallesFactura|IDDetalleFactura|ImporteTotal", "66.55"
xmlToSign.UpdateChildContent "Factura|DatosFactura|ImporteTotalFactura", "66.55"
xmlToSign.UpdateChildContent "Factura|DatosFactura|Claves|IDClave|ClaveRegimenIvaOpTrascendencia", "01"
xmlToSign.UpdateChildContent "Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|TipoNoExenta", "S1"
xmlToSign.UpdateChildContent "Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|DesgloseIVA|DetalleIVA|BaseImponible", "55.00"
xmlToSign.UpdateChildContent "Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|DesgloseIVA|DetalleIVA|TipoImpositivo", "21.00"
xmlToSign.UpdateChildContent "Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|DesgloseIVA|DetalleIVA|CuotaImpuesto", "11.55"
xmlToSign.UpdateChildContent "HuellaTBAI|EncadenamientoFacturaAnterior|SerieFacturaAnterior", "B2022"
xmlToSign.UpdateChildContent "HuellaTBAI|EncadenamientoFacturaAnterior|NumFacturaAnterior", "0099"
xmlToSign.UpdateChildContent "HuellaTBAI|EncadenamientoFacturaAnterior|FechaExpedicionFacturaAnterior", "29-01-2022"
xmlToSign.UpdateChildContent "HuellaTBAI|EncadenamientoFacturaAnterior|SignatureValueFirmaFacturaAnterior", "BeMkKwXaFsxHQec65SKpVP7EU9o4nUXOx7SAftIToFsxH+2j2tXPXhpBUnS26dhdSpiMl2DlTuqRsFdZfWyYazaGHgSRQHZZAnFt"
xmlToSign.UpdateChildContent "HuellaTBAI|Software|LicenciaTBAI", "TBAXXXXX"
xmlToSign.UpdateChildContent "HuellaTBAI|Software|EntidadDesarrolladora|NIF", "XXXXXX"
xmlToSign.UpdateChildContent "HuellaTBAI|Software|Nombre", "XXXXXXX"
xmlToSign.UpdateChildContent "HuellaTBAI|Software|Version", App.Major & "." & App.Minor & "." & App.Revision
FirmarXMLTbai xmlToSign
End Function
Public Function FirmarXMLTbai(xmlToSign As ChilkatXml) As Boolean
Dim gen As New ChilkatXmlDSigGen
Dim success As Long
success = 1
gen.SigLocation = "T:TicketBai"
gen.SigLocationMod = 0
gen.SigId = "Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-Signature"
gen.SigNamespacePrefix = "ds"
gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
gen.SigValueId = "Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-SignatureValue"
gen.SignedInfoCanonAlg = "C14N"
gen.SignedInfoDigestMethod = "sha256"
' Set the KeyInfoId before adding references..
gen.KeyInfoId = "Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-KeyInfo"
' Create an Object to be added to the Signature.
Dim object1 As New ChilkatXml
object1.Tag = "xades:QualifyingProperties"
success = object1.AddAttribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#")
success = object1.AddAttribute("Id", "Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-QualifyingProperties")
success = object1.AddAttribute("Target", "#Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-Signature")
success = object1.AddAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#")
success = object1.UpdateAttrAt("xades:SignedProperties", 1, "Id", "Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-SignedProperties")
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT"
success = object1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod", 1, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha512")
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT"
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName", "TO BE GENERATED BY CHILKAT"
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber", "TO BE GENERATED BY CHILKAT"
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyId|xades:Identifier", "https://www.batuz.eus/fitxategiak/batuz/ticketbai/sinadura_elektronikoaren_zehaztapenak_especificaciones_de_la_firma_electronica_v1_0.pdf"
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyId|xades:Description", ""
success = object1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyHash|ds:DigestMethod", 1, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256")
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyHash|ds:DigestValue", "Quzn98x3PMbSHwbUzaj5f5KOpiH0u8bvmwbbbNkO9Es="
object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyQualifiers|xades:SigPolicyQualifier|xades:SPURI", "https://www.batuz.eus/fitxategiak/batuz/ticketbai/sinadura_elektronikoaren_zehaztapenak_especificaciones_de_la_firma_electronica_v1_0.pdf"
success = object1.UpdateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", 1, "ObjectReference", "#Reference-cf54e6a9-7bf2-4128-8cae-474a267a16a1")
object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", ""
success = object1.UpdateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", 1, "Qualifier", "OIDAsURN")
object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", "urn:oid:1.2.840.10003.5.109.10"
object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Description", ""
object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "text/xml"
object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", ""
success = gen.AddObject("", object1.GetXml(), "", "")
' -------- Reference 1 --------
success = gen.AddSameDocRef("", "sha512", "C14N", "", "http://www.w3.org/2000/09/xmldsig#Object")
success = gen.SetRefIdAttr("", "Reference-cf54e6a9-7bf2-4128-8cae-474a267a16a1")
' -------- Reference 2 --------
success = gen.AddObjectRef("Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-SignedProperties", "sha512", "", "", "http://uri.etsi.org/01903#SignedProperties")
' -------- Reference 3 --------
success = gen.AddSameDocRef("Signature-a53a6ab2-f904-4f7c-be64-603333f651bf-KeyInfo", "sha512", "", "", "")
' Provide a certificate + private key. (PFX password is test123)
Dim cert As New ChilkatCert
success = cert.LoadPfxFile("ruta.pfx", "1234")
If (success <> 1) Then
Debug.Print cert.LastErrorText
Exit Function
End If
success = gen.SetX509Cert(cert, 1)
gen.KeyInfoType = "X509Data+KeyValue"
gen.X509Type = "Certificate"
' Load XML to be signed...
Dim sbXml As New ChilkatStringBuilder
success = xmlToSign.GetXmlSb(sbXml)
gen.Behaviors = "IndentedSignature,TransformSignatureXPath"
' Sign the XML...
success = gen.CreateXmlDSigSb(sbXml)
If (success <> 1) Then
Debug.Print gen.LastErrorText
Exit Function
End If
' Save the signed XML to a file.
success = sbXml.WriteFile("c:\signedXml.xml", "utf-8", 0)
'Debug.Print sbXml.GetAsString()
VerificarFirmaXMLTbai sbXml
End Function
Public Function VerificarFirmaXMLTbai(sbXml As ChilkatStringBuilder) As Boolean
Dim verifier As New ChilkatXmlDSig
Dim success As Long
success = 1
success = verifier.LoadSignatureSb(sbXml)
If (success <> 1) Then
Debug.Print verifier.LastErrorText
Exit Function
End If
Dim numSigs As Long
numSigs = verifier.NumSignatures
Dim verifyIdx As Long
verifyIdx = 0
Do While verifyIdx < numSigs
verifier.selector = verifyIdx
Dim verified As Long
verified = verifier.VerifySignature(1)
If (verified <> 1) Then
Debug.Print verifier.LastErrorText
Exit Function
End If
verifyIdx = verifyIdx + 1
Loop
Debug.Print "All signatures were successfully verified."
End Function
Public Sub EnviarXMLTbai()
Dim Http As New ChilkatHttp
Dim success As Boolean
success = Http.SetSslClientCertPfx("ruta.pfx", "1234")
If (success = False) Then
Debug.Print Http.LastErrorText
Exit Sub
End If
' Get the XML we wish to send in the body of the request.
Dim sbXml As New ChilkatStringBuilder
success = sbXml.LoadFile("c:\signedXml.xml", "utf-8")
If (success = False) Then
Debug.Print ("Failed to load XML that is to be the HTTP request body")
Exit Sub
End If
Dim url As String
url = "https://tbai-prep.egoitza.gipuzkoa.eus/WAS/HACI/HTBRecepcionFacturasWEB/rest/recepcionFacturas/alta"
Dim bGzip As Boolean
bGzip = True
Dim resp As ChilkatHttpResponse
Set resp = Http.PostXml(url, sbXml.GetAsString, "utf-8")
If (Http.LastMethodSuccess = False) Then
Debug.Print (Http.LastErrorText)
Exit Sub
End If
Http.ClearHeaders
Debug.Print ("response status code: " & resp.StatusCode)
' Examine the response (it is already decompressed)
Debug.Print ("response body:")
Debug.Print (resp.BodyStr)
End Sub
|