feat(promo): Enhance promo code functionality and discount consumption logic
- Integrated promo code service into subscription service to streamline discount consumption during payment processing. - Improved error handling and logging for promo code activation and usage increment. - Updated payment services to handle discount calculations more robustly, including fallback mechanisms for invalid discount scenarios. - Added new methods for managing promo code activations and usage in the database layer.
This commit is contained in:
@@ -190,9 +190,10 @@ async def process_promo_code_input(message: types.Message, state: FSMContext,
|
||||
# Both failed
|
||||
await session.rollback()
|
||||
logging.info(
|
||||
f"Promo code '{code_input}' application failed for user {user.id}. Reason: {result}"
|
||||
f"Promo code '{code_input}' application failed for user {user.id}. "
|
||||
f"Bonus reason: {result}. Discount reason: {result_discount}"
|
||||
)
|
||||
response_to_user_text = result # Original error message from bonus code attempt
|
||||
response_to_user_text = result_discount # Prefer the discount attempt error
|
||||
reply_markup = get_back_to_main_menu_markup(
|
||||
current_lang, i18n
|
||||
)
|
||||
|
||||
@@ -88,13 +88,35 @@ async def _initiate_yk_payment(
|
||||
if active_discount:
|
||||
# Price is already discounted, calculate original price backwards
|
||||
discount_pct = active_discount.discount_percentage
|
||||
original_price = price_rub / (1 - discount_pct / 100)
|
||||
discount_amount = original_price - price_rub
|
||||
active_promo_code_id = active_discount.promo_code_id
|
||||
logging.info(
|
||||
f"Recording {discount_pct}% discount for YooKassa payment: "
|
||||
f"original {original_price:.2f} -> final {price_rub}"
|
||||
)
|
||||
denominator = 1 - discount_pct / 100
|
||||
if denominator <= 0:
|
||||
price_source = (
|
||||
getattr(settings, "traffic_packages", {}) or {}
|
||||
if sale_mode == "traffic"
|
||||
else (settings.subscription_options or {})
|
||||
)
|
||||
fallback_original = price_source.get(months)
|
||||
if fallback_original is not None:
|
||||
original_price = fallback_original
|
||||
discount_amount = original_price - price_rub
|
||||
logging.info(
|
||||
f"Recording {discount_pct}% discount for YooKassa payment: "
|
||||
f"original {original_price:.2f} -> final {price_rub}"
|
||||
)
|
||||
else:
|
||||
logging.warning(
|
||||
"YooKassa discount %s%% has invalid denominator and no fallback price for months=%s.",
|
||||
discount_pct,
|
||||
months,
|
||||
)
|
||||
else:
|
||||
original_price = price_rub / denominator
|
||||
discount_amount = original_price - price_rub
|
||||
logging.info(
|
||||
f"Recording {discount_pct}% discount for YooKassa payment: "
|
||||
f"original {original_price:.2f} -> final {price_rub}"
|
||||
)
|
||||
|
||||
payment_description = (
|
||||
get_text("payment_description_traffic", traffic_gb=_format_value(months))
|
||||
|
||||
Reference in New Issue
Block a user