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:
@@ -94,13 +94,36 @@ class PlategaService:
|
||||
if active_discount:
|
||||
# Price is already discounted, calculate original price backwards
|
||||
discount_pct = active_discount.discount_percentage
|
||||
original_amount = amount / (1 - discount_pct / 100)
|
||||
discount_amount = original_amount - amount
|
||||
promo_code_id = active_discount.promo_code_id
|
||||
logging.info(
|
||||
f"Recording {discount_pct}% discount for Platega payment: "
|
||||
f"original {original_amount:.2f} -> final {amount}"
|
||||
)
|
||||
denominator = 1 - discount_pct / 100
|
||||
if denominator <= 0:
|
||||
traffic_mode = bool(getattr(self.settings, "traffic_sale_mode", False))
|
||||
price_source = (
|
||||
getattr(self.settings, "traffic_packages", {}) or {}
|
||||
if traffic_mode
|
||||
else (self.settings.subscription_options or {})
|
||||
)
|
||||
fallback_original = price_source.get(months)
|
||||
if fallback_original is not None:
|
||||
original_amount = fallback_original
|
||||
discount_amount = original_amount - amount
|
||||
logging.info(
|
||||
f"Recording {discount_pct}% discount for Platega payment: "
|
||||
f"original {original_amount:.2f} -> final {amount}"
|
||||
)
|
||||
else:
|
||||
logging.warning(
|
||||
"Platega discount %s%% has invalid denominator and no fallback price for months=%s.",
|
||||
discount_pct,
|
||||
months,
|
||||
)
|
||||
else:
|
||||
original_amount = amount / denominator
|
||||
discount_amount = original_amount - amount
|
||||
logging.info(
|
||||
f"Recording {discount_pct}% discount for Platega payment: "
|
||||
f"original {original_amount:.2f} -> final {amount}"
|
||||
)
|
||||
|
||||
# Update payment record with discount metadata
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user