Добавлен заказ накладной

This commit is contained in:
Раис Юсупалиев
2026-05-23 17:29:56 +03:00
parent 39cd5ddc7a
commit c494d50566
21 changed files with 1384 additions and 1126 deletions
+39 -40
View File
@@ -20,6 +20,9 @@ from app.adapters.delivery_providers.base import (
ProviderClientError,
ProviderRequestError,
)
from app.adapters.delivery_providers.cdek.order_mapper import (
CDEKOrderRegistrationResult,
)
from app.adapters.tbank.base import (
TBankPaymentAdapterError,
TBankPaymentNotificationTokenError,
@@ -109,7 +112,9 @@ class PaymentPriceValidationAdapterProtocol(Protocol):
class OrderRegistrationAdapterProtocol(Protocol):
async def register_order(self, request: InitPaymentRequest) -> str: ...
async def register_order(
self, request: InitPaymentRequest
) -> CDEKOrderRegistrationResult: ...
class OrderRepositoryProtocol(Protocol):
@@ -136,6 +141,8 @@ class OrderRepositoryProtocol(Protocol):
session: object,
order_uuid: str,
cdek_order_uuid: str,
cdek_waybill_uuid: str | None = None,
cdek_waybill_url: str | None = None,
) -> object | None: ...
@@ -259,7 +266,7 @@ class AggregatorService:
try:
payment_url = await self._payment_adapter.create_payment_link(
order_uuid=request.order_uuid,
amount_kopecks=request.price,
amount_kopecks=request.system_data.tariff.price,
)
except TBankPaymentRequestError as exc:
logger.exception(
@@ -294,6 +301,8 @@ class AggregatorService:
"Payment price validation adapter is not configured."
)
tariff_code = request.system_data.tariff.tariff_code
requested_price = request.system_data.tariff.price
try:
provider_price = await self._payment_price_validation_adapter.get_payment_price(
request
@@ -302,8 +311,8 @@ class AggregatorService:
logger.warning(
"init_payment_price_validation_request_rejected",
order_uuid=request.order_uuid,
tariff_code=request.tariff_code,
requested_price_kopecks=request.price,
tariff_code=tariff_code,
requested_price_kopecks=requested_price,
error=str(exc),
)
raise InvalidInitPaymentRequestError(
@@ -313,8 +322,8 @@ class AggregatorService:
logger.warning(
"init_payment_price_validation_unavailable",
order_uuid=request.order_uuid,
tariff_code=request.tariff_code,
requested_price_kopecks=request.price,
tariff_code=tariff_code,
requested_price_kopecks=requested_price,
error=str(exc),
)
raise InitPaymentUnavailableError(
@@ -324,8 +333,8 @@ class AggregatorService:
logger.exception(
"init_payment_price_validation_unexpected_error",
order_uuid=request.order_uuid,
tariff_code=request.tariff_code,
requested_price_kopecks=request.price,
tariff_code=tariff_code,
requested_price_kopecks=requested_price,
)
raise InitPaymentUnavailableError(
"Payment price validation is temporarily unavailable."
@@ -335,8 +344,8 @@ class AggregatorService:
logger.warning(
"init_payment_price_validation_tariff_not_found",
order_uuid=request.order_uuid,
tariff_code=request.tariff_code,
requested_price_kopecks=request.price,
tariff_code=tariff_code,
requested_price_kopecks=requested_price,
)
raise InvalidInitPaymentRequestError(
"CDEK did not return the requested tariff for payment validation."
@@ -347,15 +356,15 @@ class AggregatorService:
price_multiplier=self._provider_price_multiplier,
)
if not is_init_payment_price_valid(
request.price,
requested_price,
provider_price,
price_multiplier=self._provider_price_multiplier,
):
logger.warning(
"init_payment_price_mismatch",
order_uuid=request.order_uuid,
tariff_code=request.tariff_code,
requested_price_kopecks=request.price,
tariff_code=tariff_code,
requested_price_kopecks=requested_price,
expected_price_kopecks=expected_amount_kopecks,
provider_currency=getattr(provider_price, "currency", None),
provider_price=str(getattr(provider_price, "price", None)),
@@ -398,10 +407,12 @@ class AggregatorService:
if existing_cdek_order_uuid:
return "OK"
cdek_order_uuid = await self._register_cdek_order(order)
registration_result = await self._register_cdek_order(order)
await self._save_cdek_order_uuid(
order_uuid=notification.OrderId,
cdek_order_uuid=cdek_order_uuid,
cdek_order_uuid=registration_result.order_uuid,
cdek_waybill_uuid=registration_result.waybill_uuid,
cdek_waybill_url=registration_result.waybill_url,
)
return "OK"
@@ -456,7 +467,9 @@ class AggregatorService:
"TBank payment notification order update failed."
) from exc
async def _register_cdek_order(self, order: object) -> str:
async def _register_cdek_order(
self, order: object
) -> CDEKOrderRegistrationResult:
if self._order_registration_adapter is None:
raise TBankPaymentNotificationProcessingError(
"CDEK order registration adapter is not configured."
@@ -479,6 +492,8 @@ class AggregatorService:
*,
order_uuid: str,
cdek_order_uuid: str,
cdek_waybill_uuid: str | None,
cdek_waybill_url: str | None,
) -> None:
if self._order_repository is None:
raise TBankPaymentNotificationProcessingError(
@@ -491,6 +506,8 @@ class AggregatorService:
session,
order_uuid,
cdek_order_uuid,
cdek_waybill_uuid,
cdek_waybill_url,
)
if order is None:
logger.warning(
@@ -540,37 +557,19 @@ class AggregatorService:
request: InitPaymentRequest,
payment_url: str,
) -> OrderData:
payload = request.model_dump(mode="json")
return OrderData(
order_uuid=request.order_uuid,
payment_url=payment_url,
price=request.price,
delivery_type=request.type,
tariff_code=request.tariff_code,
sender=payload["sender"],
recipient=payload["recipient"],
from_location=payload["from_location"],
to_location=payload["to_location"],
packages=payload["packages"],
services=payload["services"],
comment=request.comment,
price=request.system_data.tariff.price,
tariff_code=request.system_data.tariff.tariff_code,
account_email=request.account_email,
payload=request.model_dump(mode="json", by_alias=True),
)
@staticmethod
def _to_init_payment_request_from_order(order: object) -> InitPaymentRequest:
return InitPaymentRequest(
order_uuid=getattr(order, "order_uuid"),
price=getattr(order, "price"),
type=getattr(order, "delivery_type"),
tariff_code=getattr(order, "tariff_code"),
comment=getattr(order, "comment"),
sender=getattr(order, "sender"),
recipient=getattr(order, "recipient"),
from_location=getattr(order, "from_location"),
to_location=getattr(order, "to_location"),
services=getattr(order, "services"),
packages=getattr(order, "packages"),
)
payload = getattr(order, "payload")
return InitPaymentRequest.model_validate(payload)
async def _get_provider_prices(
self,