Sample code for 30+ languages & platforms
PureBasic

Create SOAP with multiple VeriFactu Digitally Signed Registration Records

See more Verifactu Examples

Creates a SOAP message containing a multiple digitally signed invoice registration records, formatted according to the specifications for Spain's Veri*Factu system.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    success.i = 0

    success = 1

    ; Begin with the following SOAP XML:

    ; <?xml version="1.0" encoding="utf-8"?>
    ; <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sum="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/tike/cont/ws/SuministroLR.xsd" xmlns:sum1="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/tike/cont/ws/SuministroInformacion.xsd" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
    ;     <soapenv:Header/>
    ;     <soapenv:Body>
    ;         <sum:RegFactuSistemaFacturacion>
    ;             <sum:Cabecera>
    ;                 <sum1:ObligadoEmision>
    ;                     <sum1:NombreRazon>XYZ STORE SL</sum1:NombreRazon>
    ;                     <sum1:NIF>B99999999</sum1:NIF>
    ;                 </sum1:ObligadoEmision>
    ;                 <sum1:RemisionRequerimiento>
    ;                     <sum1:RefRequerimiento>3333333333</sum1:RefRequerimiento>
    ;                     <sum1:FinRequerimiento>S</sum1:FinRequerimiento>
    ;                 </sum1:RemisionRequerimiento>
    ;             </sum:Cabecera>
    ; 
    ;         </sum:RegFactuSistemaFacturacion>
    ;     </soapenv:Body>
    ; </soapenv:Envelope>

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "soapenv:Envelope")
    CkXml::ckAddAttribute(xml,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
    CkXml::ckAddAttribute(xml,"xmlns:sum","https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/tike/cont/ws/SuministroLR.xsd")
    CkXml::ckAddAttribute(xml,"xmlns:sum1","https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/tike/cont/ws/SuministroInformacion.xsd")
    CkXml::ckAddAttribute(xml,"xmlns:xd","http://www.w3.org/2000/09/xmldsig#")
    CkXml::ckUpdateChildContent(xml,"soapenv:Header","")
    CkXml::ckUpdateChildContent(xml,"soapenv:Body|sum:RegFactuSistemaFacturacion|sum:Cabecera|sum1:ObligadoEmision|sum1:NombreRazon","XYZ STORE SL")
    CkXml::ckUpdateChildContent(xml,"soapenv:Body|sum:RegFactuSistemaFacturacion|sum:Cabecera|sum1:ObligadoEmision|sum1:NIF","B99999999")
    CkXml::ckUpdateChildContent(xml,"soapenv:Body|sum:RegFactuSistemaFacturacion|sum:Cabecera|sum1:RemisionRequerimiento|sum1:RefRequerimiento","3333333333")
    CkXml::ckUpdateChildContent(xml,"soapenv:Body|sum:RegFactuSistemaFacturacion|sum:Cabecera|sum1:RemisionRequerimiento|sum1:FinRequerimiento","S")

    ; Prior to this code, we created N signed SOAP messages, each containing a single signed record.
    ; Load each of these signed SOAP messages into a StringBuilder and insert after the "</sum:Cabecera>"
    ; Note: We must NOT use Chilkat.Xml for this.  We must do string operations using Chilkat StringBuilder to prevent whitespace or formatting modifications which would break the signatures.

    sbXml.i = CkStringBuilder::ckCreate()
    If sbXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckGetXmlSb(xml,sbXml)

    ; ---------------------------------------------------------------------------------------------
    ; Load the previously signed XML containing just one signed record.
    sbSignedSoap.i = CkStringBuilder::ckCreate()
    If sbSignedSoap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckLoadFile(sbSignedSoap,"c:/temp/qa_output/signedSoapXml1.xml","utf-8")
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sbSignedSoap)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbXml)
        CkStringBuilder::ckDispose(sbSignedSoap)
        ProcedureReturn
    EndIf

    ; Remove everything before "</sum:Cabecera>", including the "</sum:Cabecera>"
    CkStringBuilder::ckRemoveBefore(sbSignedSoap,"</sum:Cabecera>")

    ; Remove everything after the "</sum:RegFactuSistemaFacturacion>", including the "</sum:RegFactuSistemaFacturacion>"
    CkStringBuilder::ckRemoveAfterFinal(sbSignedSoap,"</sum:RegFactuSistemaFacturacion>")

    ; We now have the portion of the signed SOAP xml from <sum:RegistroFactura> to </sum:RegistroFactura>
    ; Insert it after the </sum:Cabecera>
    CkStringBuilder::ckPrepend(sbSignedSoap,"</sum:Cabecera>")
    CkStringBuilder::ckReplaceFirst(sbXml,"</sum:Cabecera>",CkStringBuilder::ckGetAsString(sbSignedSoap))

    ; ---------------------------------------------------------------------------------------------
    ; Add the next signed SOAP containing a single signed record.
    success = CkStringBuilder::ckLoadFile(sbSignedSoap,"c:/temp/qa_output/signedSoapXml2.xml","utf-8")
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sbSignedSoap)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbXml)
        CkStringBuilder::ckDispose(sbSignedSoap)
        ProcedureReturn
    EndIf

    ; Remove everything before "</sum:Cabecera>", including the "</sum:Cabecera>"
    CkStringBuilder::ckRemoveBefore(sbSignedSoap,"</sum:Cabecera>")

    ; Remove everything after the "</sum:RegFactuSistemaFacturacion>", including the "</sum:RegFactuSistemaFacturacion>"
    CkStringBuilder::ckRemoveAfterFinal(sbSignedSoap,"</sum:RegFactuSistemaFacturacion>")

    ; We now have the portion of the signed SOAP xml from <sum:RegistroFactura> to </sum:RegistroFactura>
    ; Insert it after the </sum:Cabecera>
    CkStringBuilder::ckPrepend(sbSignedSoap,"</sum:Cabecera>")
    CkStringBuilder::ckReplaceFirst(sbXml,"</sum:Cabecera>",CkStringBuilder::ckGetAsString(sbSignedSoap))

    ; ---------------------------------------------------------------------------------------------
    ; Continue adding more signed records if needed.

    ; Save the SOAP containing multiple signed records..
    CkStringBuilder::ckWriteFile(sbXml,"c:/temp/qa_output/soap_combined.xml","utf-8",0)


    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbXml)
    CkStringBuilder::ckDispose(sbSignedSoap)


    ProcedureReturn
EndProcedure