diff --git a/app/services/pal24_service.py b/app/services/pal24_service.py index 26b7e799..fa9ef8e8 100644 --- a/app/services/pal24_service.py +++ b/app/services/pal24_service.py @@ -5,7 +5,8 @@ from __future__ import annotations import logging from datetime import datetime, timedelta from decimal import Decimal -from typing import Any, Dict, Optional +import json +from typing import Any, Dict, Optional, Union from app.config import settings from app.external.pal24_client import Pal24Client, Pal24APIError @@ -35,7 +36,7 @@ class Pal24Service: order_id: str, description: str, ttl_seconds: Optional[int] = None, - custom_payload: Optional[Dict[str, Any]] = None, + custom_payload: Optional[Union[Dict[str, Any], str]] = None, payer_email: Optional[str] = None, payment_method: Optional[str] = None, ) -> Dict[str, Any]: @@ -43,10 +44,22 @@ class Pal24Service: raise Pal24APIError("Pal24 service is not configured") amount_decimal = Pal24Client.normalize_amount(amount_kopeks) - extra_payload: Dict[str, Any] = { - "custom": custom_payload or {}, - "ttl": ttl_seconds, - } + 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 if payer_email: extra_payload["payer_email"] = payer_email diff --git a/app/services/payment/pal24.py b/app/services/payment/pal24.py index ab2da960..2eef6061 100644 --- a/app/services/payment/pal24.py +++ b/app/services/payment/pal24.py @@ -535,8 +535,8 @@ class Pal24PaymentMixin: """Преобразует нормализованный метод оплаты в значение для Pal24 API.""" api_mapping = { - "sbp": "fast_payment", - "card": "bank_card", + "sbp": "SBP", + "card": "BANK_CARD", } return api_mapping.get(normalized_payment_method) diff --git a/tests/services/test_pal24_service_adapter.py b/tests/services/test_pal24_service_adapter.py index f371039a..362d943c 100644 --- a/tests/services/test_pal24_service_adapter.py +++ b/tests/services/test_pal24_service_adapter.py @@ -4,6 +4,7 @@ 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 @@ -70,13 +71,15 @@ async def test_create_bill_success(monkeypatch: pytest.MonkeyPatch) -> None: ttl_seconds=600, custom_payload={"extra": "value"}, payer_email="user@example.com", - payment_method="card", + payment_method="BANK_CARD", ) assert result["bill_id"] == "BILL42" 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=(",", ":")) + assert client.calls[0]["payment_method"] == "BANK_CARD" @pytest.mark.anyio("asyncio") diff --git a/tests/services/test_payment_service_pal24.py b/tests/services/test_payment_service_pal24.py index e93bf929..fc23a3e0 100644 --- a/tests/services/test_payment_service_pal24.py +++ b/tests/services/test_payment_service_pal24.py @@ -104,7 +104,7 @@ async def test_create_pal24_payment_success(monkeypatch: pytest.MonkeyPatch) -> assert result["payment_method"] == "card" assert result["link_url"] == "https://pal24/sbp" assert result["card_url"] == "https://pal24/card" - assert stub.calls and stub.calls[0]["payment_method"] == "bank_card" + assert stub.calls and stub.calls[0]["payment_method"] == "BANK_CARD" assert stub.calls and stub.calls[0]["amount_kopeks"] == 50000 assert "links" in captured_args["metadata"] @@ -137,7 +137,7 @@ async def test_create_pal24_payment_default_method(monkeypatch: pytest.MonkeyPat ) assert result is not None - assert stub.calls and stub.calls[0]["payment_method"] == "fast_payment" + assert stub.calls and stub.calls[0]["payment_method"] == "SBP" assert result["payment_method"] == "sbp"