Sample code for 30+ languages & platforms
DataFlex

Sign Bolivia Invoice XML

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature (XmlDSig) for a Bolivia invoice (factura).

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXml
    Handle hoGen
    Variant vCert
    Handle hoCert
    Variant vSbXml
    Handle hoSbXml
    Handle hoVerifier
    Integer iNumSigs
    Integer iVerifyIdx
    Boolean iVerified
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Move True To iSuccess

    // Create the following XML to be signed:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <facturaElectronicaCompraVenta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="facturaElectronicaCompraVenta.xsd">
    //     <cabecera>
    //         <nitEmisor>99999999</nitEmisor>
    //         <razonSocialEmisor>Abc Xyz</razonSocialEmisor>
    //         <municipio>Cochabamba - Cochabamba</municipio>
    //         <telefono>4444444</telefono>
    //         <numeroFactura>417</numeroFactura>
    //         <cuf>ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA</cuf>
    //         <cufd>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</cufd>
    //         <codigoSucursal>0</codigoSucursal>
    //         <direccion>aaa aaa aaa</direccion>
    //         <codigoPuntoVenta>1</codigoPuntoVenta>
    //         <fechaEmision>2023-04-13T22:43:22.417</fechaEmision>
    //         <nombreRazonSocial>xyz</nombreRazonSocial>
    //         <codigoTipoDocumentoIdentidad>1</codigoTipoDocumentoIdentidad>
    //         <numeroDocumento>1234567</numeroDocumento>
    //            <complemento xsi:nil="true"/>
    //         <codigoCliente>1234567</codigoCliente>
    //         <codigoMetodoPago>1</codigoMetodoPago>
    //            <numeroTarjeta xsi:nil="true"/>
    //         <montoTotal>22.00</montoTotal>
    //         <montoTotalSujetoIva>22.00</montoTotalSujetoIva>
    //         <codigoMoneda>1</codigoMoneda>
    //         <tipoCambio>1.00</tipoCambio>
    //         <montoTotalMoneda>22.00</montoTotalMoneda>
    //            <montoGiftCard xsi:nil="true"/>
    //         <descuentoAdicional>0.00</descuentoAdicional>
    //            <codigoExcepcion>1</codigoExcepcion>
    //            <cafc xsi:nil="true"/>
    //         <leyenda>Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.</leyenda>
    //         <usuario>nromero</usuario>
    //         <codigoDocumentoSector>1</codigoDocumentoSector>
    //     </cabecera>
    //     <detalle>
    //         <actividadEconomica>620100</actividadEconomica>
    //         <codigoProductoSin>123456</codigoProductoSin>
    //         <codigoProducto>1</codigoProducto>
    //         <descripcion>Economicos</descripcion>
    //         <cantidad>1.00</cantidad>
    //         <unidadMedida>58</unidadMedida>
    //         <precioUnitario>22.00</precioUnitario>
    //         <montoDescuento>0.00</montoDescuento>
    //         <subTotal>22.00</subTotal>
    //            <numeroSerie xsi:nil="true"/>
    //            <numeroImei xsi:nil="true"/>
    //     </detalle>
    // </facturaElectronicaCompraVenta>

    // Use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "facturaElectronicaCompraVenta"
    Get ComAddAttribute Of hoXml "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance" To iSuccess
    Get ComAddAttribute Of hoXml "xsi:noNamespaceSchemaLocation" "facturaElectronicaCompraVenta.xsd" To iSuccess
    Send ComUpdateChildContent To hoXml "cabecera|nitEmisor" "99999999"
    Send ComUpdateChildContent To hoXml "cabecera|razonSocialEmisor" "Abc Xyz"
    Send ComUpdateChildContent To hoXml "cabecera|municipio" "Cochabamba - Cochabamba"
    Send ComUpdateChildContent To hoXml "cabecera|telefono" "4444444"
    Send ComUpdateChildContent To hoXml "cabecera|numeroFactura" "417"
    Send ComUpdateChildContent To hoXml "cabecera|cuf" "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA"
    Send ComUpdateChildContent To hoXml "cabecera|cufd" "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    Send ComUpdateChildContent To hoXml "cabecera|codigoSucursal" "0"
    Send ComUpdateChildContent To hoXml "cabecera|direccion" "aaa aaa aaa"
    Send ComUpdateChildContent To hoXml "cabecera|codigoPuntoVenta" "1"
    Send ComUpdateChildContent To hoXml "cabecera|fechaEmision" "2023-04-13T22:43:22.417"
    Send ComUpdateChildContent To hoXml "cabecera|nombreRazonSocial" "xyz"
    Send ComUpdateChildContent To hoXml "cabecera|codigoTipoDocumentoIdentidad" "1"
    Send ComUpdateChildContent To hoXml "cabecera|numeroDocumento" "1234567"
    Get ComUpdateAttrAt Of hoXml "cabecera|complemento" True "xsi:nil" "true" To iSuccess
    Send ComUpdateChildContent To hoXml "cabecera|codigoCliente" "1234567"
    Send ComUpdateChildContent To hoXml "cabecera|codigoMetodoPago" "1"
    Get ComUpdateAttrAt Of hoXml "cabecera|numeroTarjeta" True "xsi:nil" "true" To iSuccess
    Send ComUpdateChildContent To hoXml "cabecera|montoTotal" "22.00"
    Send ComUpdateChildContent To hoXml "cabecera|montoTotalSujetoIva" "22.00"
    Send ComUpdateChildContent To hoXml "cabecera|codigoMoneda" "1"
    Send ComUpdateChildContent To hoXml "cabecera|tipoCambio" "1.00"
    Send ComUpdateChildContent To hoXml "cabecera|montoTotalMoneda" "22.00"
    Get ComUpdateAttrAt Of hoXml "cabecera|montoGiftCard" True "xsi:nil" "true" To iSuccess
    Send ComUpdateChildContent To hoXml "cabecera|descuentoAdicional" "0.00"
    Send ComUpdateChildContent To hoXml "cabecera|codigoExcepcion" "1"
    Get ComUpdateAttrAt Of hoXml "cabecera|cafc" True "xsi:nil" "true" To iSuccess
    Send ComUpdateChildContent To hoXml "cabecera|leyenda" "Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones."
    Send ComUpdateChildContent To hoXml "cabecera|usuario" "nromero"
    Send ComUpdateChildContent To hoXml "cabecera|codigoDocumentoSector" "1"
    Send ComUpdateChildContent To hoXml "detalle|actividadEconomica" "620100"
    Send ComUpdateChildContent To hoXml "detalle|codigoProductoSin" "123456"
    Send ComUpdateChildContent To hoXml "detalle|codigoProducto" "1"
    Send ComUpdateChildContent To hoXml "detalle|descripcion" "Economicos"
    Send ComUpdateChildContent To hoXml "detalle|cantidad" "1.00"
    Send ComUpdateChildContent To hoXml "detalle|unidadMedida" "58"
    Send ComUpdateChildContent To hoXml "detalle|precioUnitario" "22.00"
    Send ComUpdateChildContent To hoXml "detalle|montoDescuento" "0.00"
    Send ComUpdateChildContent To hoXml "detalle|subTotal" "22.00"
    Get ComUpdateAttrAt Of hoXml "detalle|numeroSerie" True "xsi:nil" "true" To iSuccess
    Get ComUpdateAttrAt Of hoXml "detalle|numeroImei" True "xsi:nil" "true" To iSuccess

    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
    If (Not(IsComObjectCreated(hoGen))) Begin
        Send CreateComObject of hoGen
    End

    Set ComSigLocation Of hoGen To "facturaElectronicaCompraVenta"
    Set ComSigLocationMod Of hoGen To 0
    Set ComSigNamespacePrefix Of hoGen To ""
    Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#"
    Set ComSignedInfoCanonAlg Of hoGen To "C14N"
    Set ComSignedInfoDigestMethod Of hoGen To "sha256"

    Get ComAddSameDocRef Of hoGen "" "sha256" "C14N_WithComments" "" "" To iSuccess

    // Provide your certificate + private key. (PFX password is test123)
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert to vCert
    Get ComSetX509Cert Of hoGen vCert True To iSuccess

    Set ComKeyInfoType Of hoGen To "X509Data"
    Set ComX509Type Of hoGen To "Certificate"

    Set ComBehaviors Of hoGen To "EnvelopedTransformFirst"

    // Load XML to be signed...
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Set ComEmitCompact Of hoXml To True
    Get pvComObject of hoSbXml to vSbXml
    Get ComGetXmlSb Of hoXml vSbXml To iSuccess

    // Sign the XML...
    Get pvComObject of hoSbXml to vSbXml
    Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // -----------------------------------------------

    // Save the signed XML to a file.
    Get ComWriteFile Of hoSbXml "qa_output/signedXml.xml" "utf-8" True To iSuccess

    Get ComGetAsString Of hoSbXml To sTemp1
    Showln sTemp1

    // ----------------------------------------
    // Verify the signatures we just produced...
    Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
    If (Not(IsComObjectCreated(hoVerifier))) Begin
        Send CreateComObject of hoVerifier
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumSignatures Of hoVerifier To iNumSigs
    Move 0 To iVerifyIdx
    While (iVerifyIdx < iNumSigs)
        Set ComSelector Of hoVerifier To iVerifyIdx
        Get ComVerifySignature Of hoVerifier True To iVerified
        If (iVerified <> True) Begin
            Get ComLastErrorText Of hoVerifier To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Move (iVerifyIdx + 1) To iVerifyIdx
    Loop

    Showln "All signatures were successfully verified."


End_Procedure