|
| 1 | +package io.github.project.openubl.quickstart.xbuilder.springboot; |
| 2 | + |
| 3 | +import io.github.project.openubl.xbuilder.content.catalogs.Catalog6; |
| 4 | +import io.github.project.openubl.xbuilder.content.models.common.Cliente; |
| 5 | +import io.github.project.openubl.xbuilder.content.models.common.Proveedor; |
| 6 | +import io.github.project.openubl.xbuilder.content.models.standard.general.DocumentoVentaDetalle; |
| 7 | +import io.github.project.openubl.xbuilder.content.models.standard.general.Invoice; |
| 8 | +import io.github.project.openubl.xbuilder.enricher.ContentEnricher; |
| 9 | +import io.github.project.openubl.xbuilder.enricher.config.DateProvider; |
| 10 | +import io.github.project.openubl.xbuilder.enricher.config.Defaults; |
| 11 | +import io.github.project.openubl.xbuilder.renderer.TemplateProducer; |
| 12 | +import io.github.project.openubl.xbuilder.signature.CertificateDetails; |
| 13 | +import io.github.project.openubl.xbuilder.signature.CertificateDetailsFactory; |
| 14 | +import io.github.project.openubl.xbuilder.signature.XMLSigner; |
| 15 | +import io.github.project.openubl.xbuilder.signature.XmlSignatureHelper; |
| 16 | +import io.quarkus.qute.Template; |
| 17 | +import org.springframework.web.bind.annotation.RequestBody; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 20 | +import org.springframework.web.bind.annotation.RestController; |
| 21 | +import org.w3c.dom.Document; |
| 22 | + |
| 23 | +import java.io.InputStream; |
| 24 | +import java.math.BigDecimal; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.security.PrivateKey; |
| 27 | +import java.security.cert.X509Certificate; |
| 28 | +import java.time.LocalDate; |
| 29 | + |
| 30 | +@RestController |
| 31 | +public class XBuilderController { |
| 32 | + |
| 33 | + Defaults defaults = Defaults.builder() |
| 34 | + .icbTasa(new BigDecimal("0.2")) |
| 35 | + .igvTasa(new BigDecimal("0.18")) |
| 36 | + .build(); |
| 37 | + |
| 38 | + DateProvider dateProvider = LocalDate::now; |
| 39 | + |
| 40 | + @RequestMapping( |
| 41 | + method = RequestMethod.POST, |
| 42 | + value = "/api/create-xml", |
| 43 | + produces = "text/plain" |
| 44 | + ) |
| 45 | + public String createXML(@RequestBody String clientName) throws Exception { |
| 46 | + Invoice invoice = createInvoice(clientName); |
| 47 | + |
| 48 | + ContentEnricher enricher = new ContentEnricher(defaults, dateProvider); |
| 49 | + enricher.enrich(invoice); |
| 50 | + |
| 51 | + Template template = TemplateProducer.getInstance().getInvoice(); |
| 52 | + String xml = template.data(invoice).render(); |
| 53 | + |
| 54 | + // Sign XML |
| 55 | + InputStream ksInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("LLAMA-PE-CERTIFICADO-DEMO-12345678912.pfx"); |
| 56 | + CertificateDetails certificate = CertificateDetailsFactory.create(ksInputStream, "password"); |
| 57 | + |
| 58 | + X509Certificate x509Certificate = certificate.getX509Certificate(); |
| 59 | + PrivateKey privateKey = certificate.getPrivateKey(); |
| 60 | + Document signedXML = XMLSigner.signXML(xml, "Project OpenUBL", x509Certificate, privateKey); |
| 61 | + |
| 62 | + // Return |
| 63 | + byte[] bytesFromDocument = XmlSignatureHelper.getBytesFromDocument(signedXML); |
| 64 | + return new String(bytesFromDocument, StandardCharsets.ISO_8859_1); |
| 65 | + } |
| 66 | + |
| 67 | + private Invoice createInvoice(String clientName) { |
| 68 | + return Invoice.builder() |
| 69 | + .serie("F001") |
| 70 | + .numero(1) |
| 71 | + .proveedor(Proveedor.builder() |
| 72 | + .ruc("12345678912") |
| 73 | + .razonSocial("Softgreen S.A.C.") |
| 74 | + .build() |
| 75 | + ) |
| 76 | + .cliente(Cliente.builder() |
| 77 | + .nombre(clientName) |
| 78 | + .numeroDocumentoIdentidad("12121212121") |
| 79 | + .tipoDocumentoIdentidad(Catalog6.RUC.toString()) |
| 80 | + .build() |
| 81 | + ) |
| 82 | + .detalle(DocumentoVentaDetalle.builder() |
| 83 | + .descripcion("Item1") |
| 84 | + .cantidad(new BigDecimal("10")) |
| 85 | + .precio(new BigDecimal("100")) |
| 86 | + .unidadMedida("KGM") |
| 87 | + .build() |
| 88 | + ) |
| 89 | + .detalle(DocumentoVentaDetalle.builder() |
| 90 | + .descripcion("Item2") |
| 91 | + .cantidad(new BigDecimal("10")) |
| 92 | + .precio(new BigDecimal("100")) |
| 93 | + .unidadMedida("KGM") |
| 94 | + .build() |
| 95 | + ) |
| 96 | + .build(); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments