diff --git a/src/application/commands/payment_read_commands.py b/src/application/commands/payment_read_commands.py index 87528db..47a274c 100644 --- a/src/application/commands/payment_read_commands.py +++ b/src/application/commands/payment_read_commands.py @@ -22,7 +22,7 @@ class GetPaymentConfigCommand: async def __call__(self) -> PaymentQuote: - quote = await self._payment_quote_service.get_quote(Decimal('1.00')) + quote = await self._payment_quote_service.get_reference_quote(Decimal('1.00')) self._logger.info({'event':'payment_config_requested'}) return quote diff --git a/src/application/services/payment_quote_service.py b/src/application/services/payment_quote_service.py index 85e8299..631c151 100644 --- a/src/application/services/payment_quote_service.py +++ b/src/application/services/payment_quote_service.py @@ -6,7 +6,7 @@ from src.application.contracts import ICache, ILogger from src.application.domain.exceptions import OrderTotalOutOfRangeException, ServiceUnavailableException -_MIN_USDT_AMOUNT: Decimal = Decimal('1') +_MIN_TOTAL_RUB: Decimal = Decimal('5000') _MAX_TOTAL_RUB: Decimal = Decimal('600000') @@ -105,8 +105,8 @@ class PaymentQuoteService: ) - async def get_quote(self, usdt_amount: Decimal) -> PaymentQuote: - if usdt_amount < _MIN_USDT_AMOUNT: + async def get_reference_quote(self, usdt_amount: Decimal) -> PaymentQuote: + if usdt_amount <= Decimal('0'): raise OrderTotalOutOfRangeException( message='Order total is below minimum allowed amount', ) @@ -120,8 +120,17 @@ class PaymentQuoteService: return quote + async def get_quote(self, usdt_amount: Decimal) -> PaymentQuote: + quote = await self.get_reference_quote(usdt_amount) + if quote.total_price < _MIN_TOTAL_RUB: + raise OrderTotalOutOfRangeException( + message='Order total is below minimum allowed amount', + ) + return quote + + async def get_quote_from_total_rub(self, total_rub: Decimal) -> PaymentQuote: - if total_rub <= Decimal('0'): + if total_rub < _MIN_TOTAL_RUB: raise OrderTotalOutOfRangeException( message='Order total is below minimum allowed amount', ) @@ -133,17 +142,6 @@ class PaymentQuoteService: usdt_exchange_rate, gas_fee = await self._load_prices() - if total_rub <= gas_fee: - raise OrderTotalOutOfRangeException( - message='Order total is below minimum allowed amount', - ) - - min_quote = self._compose_quote(_MIN_USDT_AMOUNT, usdt_exchange_rate, gas_fee) - if min_quote is None or min_quote.total_price > total_rub: - raise OrderTotalOutOfRangeException( - message='Order total is below minimum allowed amount', - ) - u_budget = ((total_rub - gas_fee) / usdt_exchange_rate).quantize(Decimal('0.01'), rounding=ROUND_DOWN) u_cap = ((_MAX_TOTAL_RUB - gas_fee) / (usdt_exchange_rate * Decimal('1.04'))).quantize( Decimal('0.01'), @@ -151,12 +149,12 @@ class PaymentQuoteService: ) u_upper = min(u_budget, u_cap) n_hi = int((u_upper * 100).to_integral_value(rounding=ROUND_DOWN)) - if n_hi < 100: + if n_hi < 1: raise OrderTotalOutOfRangeException( message='Order total is below minimum allowed amount', ) - n_lo = 100 + n_lo = 1 best_cent: int | None = None while n_lo <= n_hi: mid = (n_lo + n_hi) // 2