From 1540d2665f6949f1d46438cf9d3a4ef2b1308246 Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 16 Oct 2025 16:08:03 +0300 Subject: [PATCH 1/3] Revert "Dev4" --- app/services/payment/pal24.py | 12 +------- tests/services/test_payment_service_pal24.py | 32 -------------------- 2 files changed, 1 insertion(+), 43 deletions(-) diff --git a/app/services/payment/pal24.py b/app/services/payment/pal24.py index 7b1d9a1e..2283aca2 100644 --- a/app/services/payment/pal24.py +++ b/app/services/payment/pal24.py @@ -64,7 +64,6 @@ class Pal24PaymentMixin: } normalized_payment_method = self._normalize_payment_method(payment_method) - pal24_payment_method = self._map_payment_method_for_api(normalized_payment_method) payment_module = import_module("app.services.payment_service") @@ -77,7 +76,7 @@ class Pal24PaymentMixin: ttl_seconds=ttl_seconds, custom_payload=custom_payload, payer_email=payer_email, - payment_method=pal24_payment_method, + payment_method=normalized_payment_method, ) except Pal24APIError as error: logger.error("Ошибка Pal24 API при создании счета: %s", error) @@ -529,12 +528,3 @@ class Pal24PaymentMixin: normalized = payment_method.strip().lower() return mapping.get(normalized, "sbp") - - @staticmethod - def _map_payment_method_for_api(normalized_method: str) -> Optional[str]: - mapping = { - "sbp": "fast_payment", - "card": "bank_card", - } - - return mapping.get(normalized_method) diff --git a/tests/services/test_payment_service_pal24.py b/tests/services/test_payment_service_pal24.py index b3e820f5..0e588b53 100644 --- a/tests/services/test_payment_service_pal24.py +++ b/tests/services/test_payment_service_pal24.py @@ -105,41 +105,9 @@ async def test_create_pal24_payment_success(monkeypatch: pytest.MonkeyPatch) -> assert result["link_url"] == "https://pal24/sbp" assert result["card_url"] == "https://pal24/card" assert stub.calls and stub.calls[0]["amount_kopeks"] == 50000 - assert stub.calls[0]["payment_method"] == "bank_card" assert "links" in captured_args["metadata"] -@pytest.mark.anyio("asyncio") -async def test_create_pal24_payment_default_method(monkeypatch: pytest.MonkeyPatch) -> None: - stub = StubPal24Service() - service = _make_service(stub) - db = DummySession() - - async def fake_create_pal24_payment(*args: Any, **kwargs: Any) -> DummyLocalPayment: - return DummyLocalPayment(payment_id=111) - - monkeypatch.setattr( - payment_service_module, - "create_pal24_payment", - fake_create_pal24_payment, - raising=False, - ) - monkeypatch.setattr(settings, "PAL24_MIN_AMOUNT_KOPEKS", 1000, raising=False) - monkeypatch.setattr(settings, "PAL24_MAX_AMOUNT_KOPEKS", 1_000_000, raising=False) - - result = await service.create_pal24_payment( - db=db, - user_id=42, - amount_kopeks=150000, - description="Пополнение", - language="ru", - ) - - assert result is not None - assert result["payment_method"] == "sbp" - assert stub.calls and stub.calls[0]["payment_method"] == "fast_payment" - - @pytest.mark.anyio("asyncio") async def test_create_pal24_payment_limits_and_configuration(monkeypatch: pytest.MonkeyPatch) -> None: stub = StubPal24Service() From 609a5575028d60f7dc23eedf6250ce69ad0c0321 Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 16 Oct 2025 16:25:53 +0300 Subject: [PATCH 2/3] Revert "Revert "Dev4"" --- app/services/payment/pal24.py | 12 +++++++- tests/services/test_payment_service_pal24.py | 32 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/services/payment/pal24.py b/app/services/payment/pal24.py index 2283aca2..7b1d9a1e 100644 --- a/app/services/payment/pal24.py +++ b/app/services/payment/pal24.py @@ -64,6 +64,7 @@ class Pal24PaymentMixin: } normalized_payment_method = self._normalize_payment_method(payment_method) + pal24_payment_method = self._map_payment_method_for_api(normalized_payment_method) payment_module = import_module("app.services.payment_service") @@ -76,7 +77,7 @@ class Pal24PaymentMixin: ttl_seconds=ttl_seconds, custom_payload=custom_payload, payer_email=payer_email, - payment_method=normalized_payment_method, + payment_method=pal24_payment_method, ) except Pal24APIError as error: logger.error("Ошибка Pal24 API при создании счета: %s", error) @@ -528,3 +529,12 @@ class Pal24PaymentMixin: normalized = payment_method.strip().lower() return mapping.get(normalized, "sbp") + + @staticmethod + def _map_payment_method_for_api(normalized_method: str) -> Optional[str]: + mapping = { + "sbp": "fast_payment", + "card": "bank_card", + } + + return mapping.get(normalized_method) diff --git a/tests/services/test_payment_service_pal24.py b/tests/services/test_payment_service_pal24.py index 0e588b53..b3e820f5 100644 --- a/tests/services/test_payment_service_pal24.py +++ b/tests/services/test_payment_service_pal24.py @@ -105,9 +105,41 @@ async def test_create_pal24_payment_success(monkeypatch: pytest.MonkeyPatch) -> assert result["link_url"] == "https://pal24/sbp" assert result["card_url"] == "https://pal24/card" assert stub.calls and stub.calls[0]["amount_kopeks"] == 50000 + assert stub.calls[0]["payment_method"] == "bank_card" assert "links" in captured_args["metadata"] +@pytest.mark.anyio("asyncio") +async def test_create_pal24_payment_default_method(monkeypatch: pytest.MonkeyPatch) -> None: + stub = StubPal24Service() + service = _make_service(stub) + db = DummySession() + + async def fake_create_pal24_payment(*args: Any, **kwargs: Any) -> DummyLocalPayment: + return DummyLocalPayment(payment_id=111) + + monkeypatch.setattr( + payment_service_module, + "create_pal24_payment", + fake_create_pal24_payment, + raising=False, + ) + monkeypatch.setattr(settings, "PAL24_MIN_AMOUNT_KOPEKS", 1000, raising=False) + monkeypatch.setattr(settings, "PAL24_MAX_AMOUNT_KOPEKS", 1_000_000, raising=False) + + result = await service.create_pal24_payment( + db=db, + user_id=42, + amount_kopeks=150000, + description="Пополнение", + language="ru", + ) + + assert result is not None + assert result["payment_method"] == "sbp" + assert stub.calls and stub.calls[0]["payment_method"] == "fast_payment" + + @pytest.mark.anyio("asyncio") async def test_create_pal24_payment_limits_and_configuration(monkeypatch: pytest.MonkeyPatch) -> None: stub = StubPal24Service() From 25c54c588b63c48e1babafa87fa622694cdd66b5 Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 16 Oct 2025 17:20:58 +0300 Subject: [PATCH 3/3] Revert "Dev4" --- app/services/pal24_service.py | 25 +++++--------------- tests/services/test_pal24_service_adapter.py | 2 -- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/app/services/pal24_service.py b/app/services/pal24_service.py index fa9ef8e8..26b7e799 100644 --- a/app/services/pal24_service.py +++ b/app/services/pal24_service.py @@ -5,8 +5,7 @@ from __future__ import annotations import logging from datetime import datetime, timedelta from decimal import Decimal -import json -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Optional from app.config import settings from app.external.pal24_client import Pal24Client, Pal24APIError @@ -36,7 +35,7 @@ class Pal24Service: order_id: str, description: str, ttl_seconds: Optional[int] = None, - custom_payload: Optional[Union[Dict[str, Any], str]] = None, + custom_payload: Optional[Dict[str, Any]] = None, payer_email: Optional[str] = None, payment_method: Optional[str] = None, ) -> Dict[str, Any]: @@ -44,22 +43,10 @@ class Pal24Service: raise Pal24APIError("Pal24 service is not configured") amount_decimal = Pal24Client.normalize_amount(amount_kopeks) - extra_payload: Dict[str, Any] = {"ttl": ttl_seconds} - - if custom_payload is not None: - if isinstance(custom_payload, str): - extra_payload["custom"] = custom_payload - else: - try: - extra_payload["custom"] = json.dumps( - custom_payload, - ensure_ascii=False, - separators=(",", ":"), - ) - except (TypeError, ValueError) as error: - raise Pal24APIError( - "Unable to serialize Pal24 custom payload to JSON" - ) from error + extra_payload: Dict[str, Any] = { + "custom": custom_payload or {}, + "ttl": ttl_seconds, + } if payer_email: extra_payload["payer_email"] = payer_email diff --git a/tests/services/test_pal24_service_adapter.py b/tests/services/test_pal24_service_adapter.py index d1fd744a..f371039a 100644 --- a/tests/services/test_pal24_service_adapter.py +++ b/tests/services/test_pal24_service_adapter.py @@ -4,7 +4,6 @@ from __future__ import annotations from datetime import datetime, timedelta from decimal import Decimal -import json from pathlib import Path from typing import Any, Dict, Optional import sys @@ -78,7 +77,6 @@ async def test_create_bill_success(monkeypatch: pytest.MonkeyPatch) -> None: assert client.calls and client.calls[0]["amount"] == Decimal("500.00") assert client.calls[0]["shop_id"] == "shop42" assert client.calls[0]["description"] == "Пополнение" - assert client.calls[0]["custom"] == json.dumps({"extra": "value"}, ensure_ascii=False, separators=(",", ":")) @pytest.mark.anyio("asyncio")