diff --git a/src/xhandler/xsender/models.py b/src/xhandler/xsender/models.py index 1939f0b..c40222b 100644 --- a/src/xhandler/xsender/models.py +++ b/src/xhandler/xsender/models.py @@ -26,6 +26,12 @@ class _Model(BaseModel): model_config = ConfigDict(extra="forbid", frozen=True) +class _Answer(BaseModel): + """An answer of the core: ignores the fields a newer core adds.""" + + model_config = ConfigDict(extra="ignore", frozen=True) + + class URLs(_Model): """The SUNAT services where a company sends its documents.""" @@ -128,19 +134,22 @@ def __str__(self) -> str: return self.value -class CDR(_Model): +class CDR(_Answer): """The receipt SUNAT gives back (Constancia de Recepcion).""" zip: Bytes response_code: str description: str notes: list[str] + document_description: str | None = None + """For an accepted guía de remisión, the URL its printed copy's QR code must + encode; None for other documents (and with a core older than 6.0.0-alpha.2).""" status: Status status_code: str | None = None """The ticket status, only when verifying a ticket.""" -class Fault(_Model): +class Fault(_Answer): """An error answered by SUNAT.""" code: str @@ -154,14 +163,14 @@ class Fault(_Model): status: Status -class NoCDR(_Model): +class NoCDR(_Answer): """A ticket status without CDR, e.g. "98" (in progress).""" status_code: str status: Status -class SendResult(_Model): +class SendResult(_Answer): """The answer to a sent file: a CDR, a ticket to verify later (summaries, voided documents, packs, guias) or a fault.""" @@ -173,7 +182,7 @@ class SendResult(_Model): """Where the ticket is verified.""" -class VerifyResult(_Model): +class VerifyResult(_Answer): """The answer to a ticket: a CDR, no CDR yet (e.g. in progress) or a fault.""" cdr: CDR | None = None @@ -181,7 +190,7 @@ class VerifyResult(_Model): fault: Fault | None = None -class ConsultStatus(_Model): +class ConsultStatus(_Answer): """e.g. "0001" (accepted) for get_status, or "0" for validate_cdp.""" status_code: str @@ -190,7 +199,7 @@ class ConsultStatus(_Model): """The CDR of get_status_cdr.""" -class ConsultResult(_Model): +class ConsultResult(_Answer): """The answer of the consult and validation services.""" status: ConsultStatus | None = None diff --git a/tests/test_xsender.py b/tests/test_xsender.py index b52d8c7..d4ad1a3 100644 --- a/tests/test_xsender.py +++ b/tests/test_xsender.py @@ -8,6 +8,7 @@ from xhandler import ErrorKind, XhandlerError from xhandler.xsender import ( BILL_CONSULT_SERVICE_URL, + CDR, ConsultDocument, Credentials, FileSender, @@ -65,6 +66,7 @@ def test_send_file_reads_the_cdr() -> None: assert result.cdr is not None assert result.cdr.status is Status.ACCEPTED assert result.cdr.zip + assert result.cdr.document_description is None assert result.send_target.url == URLs.beta().invoice assert result.verify_ticket_target is None request = sunat.requests[0] @@ -72,6 +74,24 @@ def test_send_file_reads_the_cdr() -> None: assert str(request.url) == URLs.beta().invoice +def test_cdrs_carry_the_document_description() -> None: + """A newer core may add fields: the answers keep reading""" + cdr = CDR.model_validate( + { + "zip": "", + "response_code": "0", + "description": "La Guia numero T001-1, ha sido aceptada", + "notes": [], + "document_description": "https://e-factura.sunat.gob.pe/v1/contribuyente/gre/comprobantes/descargaqr?hashqr=abc", + "status": "ACEPTADO", + "added_by_a_newer_core": True, + } + ) + assert cdr.document_description == ( + "https://e-factura.sunat.gob.pe/v1/contribuyente/gre/comprobantes/descargaqr?hashqr=abc" + ) + + def test_send_file_reads_faults() -> None: """SUNAT answers SOAP faults with HTTP 500""" sunat = Sunat(lambda _: answer("send_file_rejected"))