feat: add mpre endpoints

This commit is contained in:
2026-05-11 19:04:39 +03:00
parent 42fcfbff34
commit 489c9cb2da
18 changed files with 691 additions and 75 deletions

View File

@@ -1,14 +1,14 @@
from __future__ import annotations
from datetime import datetime, timezone
from decimal import Decimal, ROUND_UP
from decimal import Decimal
from ulid import ULID
from src.application.abstractions import IUnitOfWork
from src.application.contracts import ICache,ILogger
from src.application.contracts import ILogger
from src.application.contracts import IItPayService
from src.application.domain.entities.order import OrderEntity
from src.application.domain.enums import OrderStatus
from src.application.domain.exceptions import ApplicationException,ServiceUnavailableException
from src.infrastructure.config import settings
from src.application.domain.exceptions import ApplicationException
from src.application.services import PaymentQuoteService
from src.infrastructure.database.decorators import transactional
from src.presentation.schemas.order import CreateOrder
@@ -20,12 +20,12 @@ class CreateOrderCommand:
*,
unit_of_work: IUnitOfWork,
logger: ILogger,
remote_cache: ICache,
payment_quote_service: PaymentQuoteService,
itpay_service: IItPayService,
) -> None:
self._unit_of_work = unit_of_work
self._logger = logger
self._remote_cache = remote_cache
self._payment_quote_service = payment_quote_service
self._itpay_service = itpay_service
@@ -33,22 +33,11 @@ class CreateOrderCommand:
async def __call__(self, payment_data: CreateOrder, user_id: str) -> OrderEntity:
client_payment_id = str(ULID())
rate_raw = await self._remote_cache.hget('tradex:rub:rate','value')
gas_raw = await self._remote_cache.hget('gwei:eth:last','normal_rub')
self._logger.info(f'Actual market values: rate={rate_raw}, gas={gas_raw}')
if rate_raw is None:
self._logger.error('Exchange rate unavailable')
raise ServiceUnavailableException(message='Exchange rate unavailable')
if gas_raw is None:
self._logger.error('Exchange gas unavailable')
raise ServiceUnavailableException(message='Exchange gas unavailable')
actual_gas_fee = Decimal(gas_raw).quantize(Decimal('0.00'), rounding=ROUND_UP)
actual_usdt_exchange_rate = Decimal(rate_raw).quantize(Decimal('0.00'), rounding=ROUND_UP)
actual_service_fee = (payment_data.usdt_amount * actual_usdt_exchange_rate * settings.PAYMENT_SERVICE_FEE_RATE).quantize(Decimal('0.01'))
actual_total_price = (payment_data.usdt_amount * actual_usdt_exchange_rate + actual_service_fee + actual_gas_fee).quantize(Decimal('0.01'))
quote = await self._payment_quote_service.get_quote(payment_data.usdt_amount)
actual_gas_fee = quote.gas_fee
actual_usdt_exchange_rate = quote.usdt_exchange_rate
actual_service_fee = quote.service_fee
actual_total_price = quote.total_price
if actual_total_price > payment_data.total_price * Decimal('1.01'):
self._logger.error('Price has changed, please refresh and try again')
raise ApplicationException(status_code=409, message='Price has changed, please refresh and try again')