fix(promo): Исправлен баг с применением двойной скидки при оплате через CryptoBot
This commit is contained in:
@@ -71,15 +71,24 @@ class CryptoPayService:
|
|||||||
logging.error("CryptoPayService not configured")
|
logging.error("CryptoPayService not configured")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Apply active discount if exists
|
# Check for active discount to save metadata (price already discounted from previous step)
|
||||||
|
original_amount = None
|
||||||
|
discount_amount = None
|
||||||
|
promo_code_id = None
|
||||||
|
|
||||||
if promo_code_service:
|
if promo_code_service:
|
||||||
# Import here to avoid circular import
|
from db.dal import active_discount_dal
|
||||||
from bot.handlers.user.subscription.payment_discount_helper import apply_discount_to_payment
|
active_discount = await active_discount_dal.get_active_discount(session, user_id)
|
||||||
final_amount, discount_amount, promo_code_id = await apply_discount_to_payment(
|
if active_discount:
|
||||||
session, user_id, amount, promo_code_service
|
# Price is already discounted, calculate original price backwards
|
||||||
)
|
discount_pct = active_discount.discount_percentage
|
||||||
else:
|
original_amount = amount / (1 - discount_pct / 100)
|
||||||
final_amount, discount_amount, promo_code_id = amount, None, None
|
discount_amount = original_amount - amount
|
||||||
|
promo_code_id = active_discount.promo_code_id
|
||||||
|
logging.info(
|
||||||
|
f"Recording {discount_pct}% discount for CryptoPay payment: "
|
||||||
|
f"original {original_amount:.2f} -> final {amount}"
|
||||||
|
)
|
||||||
|
|
||||||
# Create pending payment in DB and commit to persist
|
# Create pending payment in DB and commit to persist
|
||||||
try:
|
try:
|
||||||
@@ -87,8 +96,8 @@ class CryptoPayService:
|
|||||||
session,
|
session,
|
||||||
{
|
{
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"amount": final_amount,
|
"amount": amount,
|
||||||
"original_amount": amount if discount_amount else None,
|
"original_amount": original_amount,
|
||||||
"discount_applied": discount_amount,
|
"discount_applied": discount_amount,
|
||||||
"currency": self.settings.CRYPTOPAY_ASSET,
|
"currency": self.settings.CRYPTOPAY_ASSET,
|
||||||
"status": "pending_cryptopay",
|
"status": "pending_cryptopay",
|
||||||
@@ -115,7 +124,7 @@ class CryptoPayService:
|
|||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
invoice = await self.client.create_invoice(
|
invoice = await self.client.create_invoice(
|
||||||
amount=final_amount,
|
amount=amount,
|
||||||
currency_type=self.settings.CRYPTOPAY_CURRENCY_TYPE,
|
currency_type=self.settings.CRYPTOPAY_CURRENCY_TYPE,
|
||||||
fiat=self.settings.CRYPTOPAY_ASSET if self.settings.CRYPTOPAY_CURRENCY_TYPE == "fiat" else None,
|
fiat=self.settings.CRYPTOPAY_ASSET if self.settings.CRYPTOPAY_CURRENCY_TYPE == "fiat" else None,
|
||||||
asset=self.settings.CRYPTOPAY_ASSET if self.settings.CRYPTOPAY_CURRENCY_TYPE == "crypto" else None,
|
asset=self.settings.CRYPTOPAY_ASSET if self.settings.CRYPTOPAY_CURRENCY_TYPE == "crypto" else None,
|
||||||
|
|||||||
Reference in New Issue
Block a user