update pay function
This commit is contained in:
96
app.py
96
app.py
@@ -1427,6 +1427,10 @@ def calculate_subscription_price_simple(user_id, to_plan_name, to_cycle, promo_c
|
||||
remaining_value = current_price * (remaining_days / total_days)
|
||||
# 实付金额 = 新套餐价格 - 剩余价值
|
||||
final_price = max(0, price - remaining_value)
|
||||
|
||||
# 如果剩余价值 >= 新套餐价格,标记为免费升级
|
||||
if remaining_value >= price:
|
||||
final_price = 0
|
||||
elif current_plan == 'max' and to_plan_name == 'pro':
|
||||
# 降级:Max → Pro,到期后切换,全价购买
|
||||
is_downgrade = True
|
||||
@@ -1786,6 +1790,89 @@ def calculate_subscription_price():
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route('/api/subscription/free-upgrade', methods=['POST'])
|
||||
@login_required
|
||||
def free_upgrade_subscription():
|
||||
"""
|
||||
免费升级订阅(当剩余价值 >= 新套餐价格时)
|
||||
|
||||
Request Body:
|
||||
{
|
||||
"plan_name": "max",
|
||||
"billing_cycle": "yearly"
|
||||
}
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
plan_name = data.get('plan_name')
|
||||
billing_cycle = data.get('billing_cycle')
|
||||
|
||||
if not plan_name or not billing_cycle:
|
||||
return jsonify({'success': False, 'error': '参数不完整'}), 400
|
||||
|
||||
user_id = current_user.id
|
||||
|
||||
# 计算价格,验证是否可以免费升级
|
||||
price_result = calculate_subscription_price_simple(user_id, plan_name, billing_cycle, None)
|
||||
|
||||
if 'error' in price_result:
|
||||
return jsonify({'success': False, 'error': price_result['error']}), 400
|
||||
|
||||
# 检查是否为升级且实付金额为0
|
||||
if not price_result.get('is_upgrade') or price_result.get('final_amount', 1) > 0:
|
||||
return jsonify({'success': False, 'error': '当前情况不符合免费升级条件'}), 400
|
||||
|
||||
# 获取当前订阅
|
||||
subscription = UserSubscription.query.filter_by(user_id=user_id).first()
|
||||
if not subscription:
|
||||
return jsonify({'success': False, 'error': '未找到订阅记录'}), 404
|
||||
|
||||
# 计算新的到期时间(按剩余价值折算)
|
||||
remaining_value = price_result.get('remaining_value', 0)
|
||||
new_plan_price = price_result.get('new_plan_price', 0)
|
||||
|
||||
if new_plan_price > 0:
|
||||
# 计算可以兑换的新套餐天数
|
||||
value_ratio = remaining_value / new_plan_price
|
||||
|
||||
cycle_days_map = {
|
||||
'monthly': 30,
|
||||
'quarterly': 90,
|
||||
'semiannual': 180,
|
||||
'yearly': 365
|
||||
}
|
||||
new_cycle_days = cycle_days_map.get(billing_cycle, 365)
|
||||
|
||||
# 新的到期天数 = 周期天数 × 价值比例
|
||||
new_days = int(new_cycle_days * value_ratio)
|
||||
|
||||
# 更新订阅信息
|
||||
subscription.subscription_type = plan_name
|
||||
subscription.billing_cycle = billing_cycle
|
||||
subscription.start_date = datetime.utcnow()
|
||||
subscription.end_date = datetime.utcnow() + timedelta(days=new_days)
|
||||
subscription.subscription_status = 'active'
|
||||
subscription.updated_at = datetime.utcnow()
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'升级成功!您的{plan_name.upper()}版本将持续{new_days}天',
|
||||
'data': {
|
||||
'subscription_type': plan_name,
|
||||
'end_date': subscription.end_date.isoformat(),
|
||||
'days': new_days
|
||||
}
|
||||
})
|
||||
else:
|
||||
return jsonify({'success': False, 'error': '价格计算异常'}), 500
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({'success': False, 'error': f'升级失败: {str(e)}'}), 500
|
||||
|
||||
|
||||
@app.route('/api/payment/create-order', methods=['POST'])
|
||||
def create_payment_order():
|
||||
"""
|
||||
@@ -1819,6 +1906,15 @@ def create_payment_order():
|
||||
amount = price_result['final_amount']
|
||||
subscription_type = price_result.get('subscription_type', 'new') # new 或 renew
|
||||
|
||||
# 检查是否为免费升级(金额为0)
|
||||
if amount <= 0 and price_result.get('is_upgrade'):
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': '当前剩余价值可直接免费升级,请使用免费升级功能',
|
||||
'should_free_upgrade': True,
|
||||
'price_info': price_result
|
||||
}), 400
|
||||
|
||||
# 创建订单
|
||||
try:
|
||||
order = PaymentOrder(
|
||||
|
||||
Reference in New Issue
Block a user