update pay function
This commit is contained in:
38
app.py
38
app.py
@@ -1372,14 +1372,42 @@ def calculate_subscription_price_simple(user_id, to_plan_name, to_cycle, promo_c
|
||||
if current_sub.end_date and current_sub.end_date > datetime.utcnow():
|
||||
# 获取当前套餐的原始价格
|
||||
current_plan_obj = SubscriptionPlan.query.filter_by(name=current_plan, is_active=True).first()
|
||||
if current_plan_obj and current_plan_obj.pricing_options:
|
||||
if current_plan_obj:
|
||||
current_price = None
|
||||
|
||||
# 优先从 pricing_options 获取价格
|
||||
if current_plan_obj.pricing_options:
|
||||
try:
|
||||
pricing_opts = json.loads(current_plan_obj.pricing_options)
|
||||
current_price = None
|
||||
|
||||
# 如果 current_cycle 为空或无效,根据剩余天数推断计费周期
|
||||
if not current_cycle or current_cycle.strip() == '':
|
||||
remaining_days_total = (current_sub.end_date - current_sub.start_date).days if current_sub.start_date else 365
|
||||
|
||||
# 根据总天数推断计费周期
|
||||
if remaining_days_total <= 35:
|
||||
inferred_cycle = 'monthly'
|
||||
elif remaining_days_total <= 100:
|
||||
inferred_cycle = 'quarterly'
|
||||
elif remaining_days_total <= 200:
|
||||
inferred_cycle = 'semiannual'
|
||||
else:
|
||||
inferred_cycle = 'yearly'
|
||||
else:
|
||||
inferred_cycle = current_cycle
|
||||
|
||||
for opt in pricing_opts:
|
||||
if opt.get('cycle_key') == current_cycle:
|
||||
if opt.get('cycle_key') == inferred_cycle:
|
||||
current_price = float(opt.get('price', 0))
|
||||
current_cycle = inferred_cycle # 更新周期信息
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
# 如果 pricing_options 中没找到,使用 yearly_price 作为默认
|
||||
if current_price is None or current_price <= 0:
|
||||
current_price = float(current_plan_obj.yearly_price) if current_plan_obj.yearly_price else 0
|
||||
current_cycle = 'yearly'
|
||||
|
||||
if current_price and current_price > 0:
|
||||
# 计算剩余天数
|
||||
@@ -1392,15 +1420,13 @@ def calculate_subscription_price_simple(user_id, to_plan_name, to_cycle, promo_c
|
||||
'semiannual': 180,
|
||||
'yearly': 365
|
||||
}
|
||||
total_days = cycle_days_map.get(current_cycle, 30)
|
||||
total_days = cycle_days_map.get(current_cycle, 365)
|
||||
|
||||
# 计算剩余价值
|
||||
if total_days > 0 and remaining_days > 0:
|
||||
remaining_value = current_price * (remaining_days / total_days)
|
||||
# 实付金额 = 新套餐价格 - 剩余价值
|
||||
final_price = max(0, price - remaining_value)
|
||||
except:
|
||||
pass
|
||||
elif current_plan == 'max' and to_plan_name == 'pro':
|
||||
# 降级:Max → Pro,到期后切换,全价购买
|
||||
is_downgrade = True
|
||||
|
||||
Reference in New Issue
Block a user