Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXml
LOCAL oGen
LOCAL oCert
LOCAL oSbXml
LOCAL oVerifier
LOCAL nNumSigs
LOCAL nVerifyIdx
LOCAL nVerified

nSuccess := 0

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

nSuccess := 1

//  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

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

oGen := CreateObject("Chilkat.XmlDSigGen")

oGen:SigLocation := "facturaElectronicaCompraVenta"
oGen:SigLocationMod := 0
oGen:SigNamespacePrefix := ""
oGen:SigNamespaceUri := "http://www.w3.org/2000/09/xmldsig#"
oGen:SignedInfoCanonAlg := "C14N"
oGen:SignedInfoDigestMethod := "sha256"

oGen:AddSameDocRef("", "sha256", "C14N_WithComments", "", "")

//  Provide your certificate + private key. (PFX password is test123)
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile("qa_data/pfx/cert_test123.pfx", "test123")
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oXml:destroy()
    oGen:destroy()
    oCert:destroy()
    RETURN
ENDIF

oGen:SetX509Cert(oCert, 1)

oGen:KeyInfoType := "X509Data"
oGen:X509Type := "Certificate"

oGen:Behaviors := "EnvelopedTransformFirst"

//  Load XML to be signed...
oSbXml := CreateObject("Chilkat.StringBuilder")
oXml:EmitCompact := 1
oXml:GetXmlSb(oSbXml)

//  Sign the XML...
nSuccess := oGen:CreateXmlDSigSb(oSbXml)
IF (nSuccess != 1)
    ? oGen:LastErrorText
    oXml:destroy()
    oGen:destroy()
    oCert:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

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

//  Save the signed XML to a file.
nSuccess := oSbXml:WriteFile("qa_output/signedXml.xml", "utf-8", 1)

? oSbXml:GetAsString()

//  ----------------------------------------
//  Verify the signatures we just produced...
oVerifier := CreateObject("Chilkat.XmlDSig")
nSuccess := oVerifier:LoadSignatureSb(oSbXml)
IF (nSuccess != 1)
    ? oVerifier:LastErrorText
    oXml:destroy()
    oGen:destroy()
    oCert:destroy()
    oSbXml:destroy()
    oVerifier:destroy()
    RETURN
ENDIF

nNumSigs := oVerifier:NumSignatures
nVerifyIdx := 0
DO WHILE nVerifyIdx < nNumSigs
    oVerifier:Selector := nVerifyIdx
    nVerified := oVerifier:VerifySignature(1)
    IF (nVerified != 1)
        ? oVerifier:LastErrorText
        oXml:destroy()
        oGen:destroy()
        oCert:destroy()
        oSbXml:destroy()
        oVerifier:destroy()
        RETURN
    ENDIF

    nVerifyIdx := nVerifyIdx + 1
ENDDO
? "All signatures were successfully verified."

oXml:destroy()
oGen:destroy()
oCert:destroy()
oSbXml:destroy()
oVerifier:destroy()