Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading