Introduction
The CompraVenta service is the core module for managing electronic invoices in the SIAT system. It provides functionality for:
Building and submitting invoices (facturas)
Canceling previously issued invoices
Generating CUF (Código Único de Factura)
Signing, compressing, and hashing invoice XML
Service Methods
The CompraVenta service exposes two primary methods:
RecepcionFactura Submit a signed and compressed invoice to SIAT for processing
AnulacionFactura Cancel a previously accepted invoice
Builder Pattern
The SDK uses a fluent builder pattern for constructing invoices and requests. This ensures type safety and prevents direct manipulation of internal structures.
Invoice Builder Learn how to build invoices using Factura, Cabecera, and Detalle builders
Accessing the Service
The CompraVenta service is available through the main SIAT client:
package main
import (
" github.com/ron86i/go-siat "
" log "
)
func main () {
// Initialize SIAT client
client , err := siat . New ( "https://pilotosiatservicios.impuestos.gob.bo/v2" , nil )
if err != nil {
log . Fatal ( err )
}
// Access CompraVenta service
cvService := client . CompraVenta
// Use the service methods
// cvService.RecepcionFactura(...)
// cvService.AnulacionFactura(...)
}
Utility Functions
The models.CompraVenta namespace provides utility functions for invoice processing:
GenerarCUF
Generates the unique invoice code (CUF):
cuf , err := models . CompraVenta . GenerarCUF (
nit , // int64: NIT del emisor
fechaHora , // time.Time: fecha y hora de emisión
sucursal , // int: código de sucursal
modalidad , // int: código de modalidad
tipoEmision , // int: tipo de emisión
tipoFactura , // int: tipo de factura
tipoDocumentoSector , // int: tipo de documento sector
numeroFactura , // int: número de factura
puntoVenta , // int: código punto de venta
codigoControl , // string: código de control
)
SignXML
Signs an invoice XML document with a digital certificate:
signedXML , err := models . CompraVenta . SignXML (
xmlBytes , // []byte: XML serializado
"key.pem" , // string: ruta al archivo de clave privada
"cert.crt" , // string: ruta al archivo de certificado
)
Complete Workflow
A typical invoice submission follows this workflow:
Build the Invoice using NewFactura(), NewCabecera(), and NewDetalle() builders
Marshal to XML using xml.Marshal()
Sign the XML using models.CompraVenta.SignXML()
Compress with Gzip to reduce payload size
Calculate SHA256 hash of the compressed bytes
Encode to Base64 for transmission
Build RecepcionFacturaRequest with the encoded file and hash
Submit to SIAT using cvService.RecepcionFactura()
See the complete example for implementation details.
Response Structure
All CompraVenta service methods return a SOAP envelope response:
type EnvelopeResponse [ T ] struct {
Body struct {
Content T
}
}
The response content includes:
RespuestaServicioFacturacion.Transaccion
Indicates if the transaction was successful
RespuestaServicioFacturacion.CodigoRecepcion
Reception code assigned by SIAT (on success)
RespuestaServicioFacturacion.CodigoEstado
Status code of the operation
RespuestaServicioFacturacion.CodigoDescripcion
Human-readable description of the result
RespuestaServicioFacturacion.MensajesList
List of messages or errors from the service
Error Handling
Always check both the error return value and the response transaction status:
resp , err := cvService . RecepcionFactura ( ctx , cfg , request )
if err != nil {
log . Fatalf ( "HTTP/Network error: %v " , err )
}
if ! resp . Body . Content . RespuestaServicioFacturacion . Transaccion {
log . Printf ( "SIAT rejected invoice: %s " ,
resp . Body . Content . RespuestaServicioFacturacion . CodigoDescripcion )
for _ , msg := range resp . Body . Content . RespuestaServicioFacturacion . MensajesList {
log . Printf ( " [ %d ] %s " , msg . Codigo , msg . Descripcion )
}
}