From a1612a8bdca468c1a939c9b20f0b1356e3feff79 Mon Sep 17 00:00:00 2001 From: Carlos Feria <2582866+carlosthe19916@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:50:38 -0500 Subject: [PATCH] feat(xsender): read the CDR's document description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xhandler 6.0.0-alpha.2 adds `document_description` to the CDR: for an accepted guía de remisión, the URL its printed copy's QR code must encode. The binding reads it, and keeps working with older cores, where it's absent. Interim until the binding generates its answer types from the core's schema (xhandler-rust docs/adrs/BINDINGS.md). Co-Authored-By: Claude Opus 5.5 --- .../project/openubl/xsender/models/Sunat.java | 5 +++ .../openubl/xsender/sunat/SunatResponses.java | 11 ++++-- .../xsender/sunat/SunatResponsesTest.java | 36 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 xsender/core/src/test/java/io/github/project/openubl/xsender/sunat/SunatResponsesTest.java diff --git a/xsender/core/src/main/java/io/github/project/openubl/xsender/models/Sunat.java b/xsender/core/src/main/java/io/github/project/openubl/xsender/models/Sunat.java index c81792b6..5d7a68c0 100644 --- a/xsender/core/src/main/java/io/github/project/openubl/xsender/models/Sunat.java +++ b/xsender/core/src/main/java/io/github/project/openubl/xsender/models/Sunat.java @@ -8,4 +8,9 @@ public class Sunat { private String ticket; private byte[] cdr; + /** + * For an accepted guía de remisión, the URL its printed copy's QR code must + * encode; null for other documents (and with a core older than 6.0.0-alpha.2) + */ + private String documentDescription; } diff --git a/xsender/core/src/main/java/io/github/project/openubl/xsender/sunat/SunatResponses.java b/xsender/core/src/main/java/io/github/project/openubl/xsender/sunat/SunatResponses.java index b80ee491..bb9f8fce 100644 --- a/xsender/core/src/main/java/io/github/project/openubl/xsender/sunat/SunatResponses.java +++ b/xsender/core/src/main/java/io/github/project/openubl/xsender/sunat/SunatResponses.java @@ -25,7 +25,10 @@ static SunatResponse of(JsonNode answer) { return SunatResponse.builder() .status(Status.valueOf(cdr.get("status").asText())) .metadata(new Metadata(number(cdr.get("response_code")), cdr.get("description").asText(), notes(cdr), null)) - .sunat(Sunat.builder().cdr(Base64.getDecoder().decode(cdr.get("zip").asText())).build()) + .sunat(Sunat.builder() + .cdr(Base64.getDecoder().decode(cdr.get("zip").asText())) + .documentDescription(text(cdr.get("document_description"))) + .build()) .build(); } if (answer.has("ticket")) { @@ -64,7 +67,7 @@ static ConsultResponse consult(JsonNode answer) { JsonNode cdr = status.get("cdr_zip"); return new ConsultResponse( status.get("status_code").asText(), - status.get("status_message").isNull() ? null : status.get("status_message").asText(), + text(status.get("status_message")), cdr.isNull() ? null : Base64.getDecoder().decode(cdr.asText()), null ); @@ -75,6 +78,10 @@ private static Integer httpStatus(JsonNode fault) { return status == null || status.isNull() ? null : status.asInt(); } + private static String text(JsonNode value) { + return value == null || value.isNull() ? null : value.asText(); + } + private static Integer number(JsonNode code) { String text = code.asText(); return text.matches("\\d+") ? Integer.valueOf(text) : null; diff --git a/xsender/core/src/test/java/io/github/project/openubl/xsender/sunat/SunatResponsesTest.java b/xsender/core/src/test/java/io/github/project/openubl/xsender/sunat/SunatResponsesTest.java new file mode 100644 index 00000000..3a19b990 --- /dev/null +++ b/xsender/core/src/test/java/io/github/project/openubl/xsender/sunat/SunatResponsesTest.java @@ -0,0 +1,36 @@ +package io.github.project.openubl.xsender.sunat; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.project.openubl.xsender.models.SunatResponse; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class SunatResponsesTest { + + private static final String QR = "https://e-factura.sunat.gob.pe/v1/contribuyente/gre/comprobantes/descargaqr?hashqr=abc"; + + private static JsonNode cdr(String documentDescription) throws Exception { + String field = documentDescription == null ? "" : ", \"document_description\": \"" + documentDescription + "\""; + return new ObjectMapper().readTree(""" + {"cdr": {"zip": "", "response_code": "0", "description": "La Guia numero T001-1, ha sido aceptada", + "notes": [], "status": "ACEPTADO"%s}} + """.formatted(field)); + } + + @Test + void cdr_carriesTheDocumentDescription() throws Exception { + SunatResponse response = SunatResponses.of(cdr(QR)); + + assertEquals(QR, response.getSunat().getDocumentDescription()); + } + + @Test + void cdr_withoutDocumentDescription() throws Exception { + SunatResponse response = SunatResponses.of(cdr(null)); + + assertNull(response.getSunat().getDocumentDescription()); + } +}