Добавлена обработка уведомлений через ручку

This commit is contained in:
Раис Юсупалиев
2026-04-18 21:27:15 +03:00
parent 78e9ad4fa9
commit 6b66af5eb3
38 changed files with 1519 additions and 32 deletions
+46 -1
View File
@@ -1,6 +1,7 @@
"""Delivery API controller skeleton."""
from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import PlainTextResponse
from app.adapters.postgres.engine import create_postgres_engine, create_postgres_session_factory
from app.adapters.address_suggestions.dadata import DadataAddressSuggestionProvider
@@ -14,7 +15,11 @@ from app.config import Settings
from app.controllers.http_client import build_controller_http_client
from app.repositories.cache.redis_cache import PriceCache
from app.repositories.order import OrderRepository
from app.schemas.payment import InitPaymentRequest, InitPaymentResponse
from app.schemas.payment import (
InitPaymentRequest,
InitPaymentResponse,
TBankPaymentNotification,
)
from app.schemas.request import AddressSuggestRequest, DeliveryCalculationRequest
from app.schemas.response import AddressSuggestion, DeliveryPrice
from app.services.aggregator import (
@@ -24,7 +29,9 @@ from app.services.aggregator import (
InvalidAddressSuggestRequestError,
InvalidDeliveryRequestError,
InvalidInitPaymentRequestError,
InvalidTBankPaymentNotificationError,
InitPaymentUnavailableError,
TBankPaymentNotificationProcessingError,
UnsupportedAddressSuggestionCountryError,
)
@@ -68,6 +75,7 @@ def _build_aggregator_service(settings: Settings) -> AggregatorService:
cache=cache,
payment_adapter=payment_adapter,
order_repository=order_repository,
order_registration_adapter=cdek_provider,
address_suggestion_providers=(
dadata_provider,
yandex_geosuggest_provider,
@@ -191,3 +199,40 @@ async def init_payment(
"message": "Payment initialization is temporarily unavailable.",
},
) from exc
@router.post(
"/tbank/notifications",
response_class=PlainTextResponse,
)
async def handle_tbank_payment_notification(
notification: TBankPaymentNotification,
service: AggregatorService = Depends(get_aggregator_service),
) -> PlainTextResponse:
try:
response_body = await service.handle_tbank_payment_notification(notification)
except InvalidTBankPaymentNotificationError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"code": "invalid_tbank_payment_notification",
"message": "TBank payment notification token is invalid.",
},
) from exc
except TBankPaymentNotificationProcessingError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail={
"code": "tbank_payment_notification_processing_unavailable",
"message": "TBank payment notification processing is temporarily unavailable.",
},
) from exc
except AggregatorServiceError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail={
"code": "tbank_payment_notification_processing_unavailable",
"message": "TBank payment notification processing is temporarily unavailable.",
},
) from exc
return PlainTextResponse(response_body)