From c6682e1fabeb6430abb7c7f94671b077ff146af7 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Thu, 3 Sep 2026 00:40:01 +0545 Subject: [PATCH] feat(models): add shared BaseResponse and PaymentResult --- nepali_payment/models/base.py | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 nepali_payment/models/base.py diff --git a/nepali_payment/models/base.py b/nepali_payment/models/base.py new file mode 100644 index 0000000..6f0fdb2 --- /dev/null +++ b/nepali_payment/models/base.py @@ -0,0 +1,47 @@ +from dataclasses import dataclass +from typing import Any + +from nepali_payment.exceptions import PaymentError + + +@dataclass +class BaseResponse: + """The common fields every response carries. + + Each gateway response keeps its own specific fields, but they all share + these basics: a status, a message, some data, and whether it worked. + """ + + status: int = 200 + message: str = "" + data: Any | None = None + success: bool = True + error_code: int | None = None + + +@dataclass +class PaymentResult(BaseResponse): + """The standard answer every service returns. + + Holds whether the payment worked (``success``), a message, and the raw data + from the provider (``data``). It can be used like a ``bool`` (true = worked) + and can raise an error for you if it failed. + """ + + def __bool__(self) -> bool: + """Return True when the payment operation succeeded.""" + return bool(self.success) + + def raise_for_status(self) -> "PaymentResult": + """Raise an error if the payment failed, otherwise return ``self``. + + Handy after a call: this throws a :class:`PaymentError` when + ``success`` is False, so you can stop and handle the problem. + + Raises: + PaymentError: When ``success`` is False. + + """ + if not self.success: + raise PaymentError(self.message or "Payment failed") + return self