实现dashboard服务器详情页续费功能
This commit is contained in:
parent
1e9d669e8d
commit
33bd5dd87b
@ -287,14 +287,11 @@ export default {
|
|||||||
resources: {
|
resources: {
|
||||||
createRenewalOrder() {
|
createRenewalOrder() {
|
||||||
return {
|
return {
|
||||||
url: 'jcloud.api.billing.create_server_renewal_order',
|
url: 'jcloud.api.aliyun_server_light.create_server_renew_order',
|
||||||
validate() {
|
validate() {
|
||||||
if (!this.server) {
|
if (!this.server) {
|
||||||
throw new DashboardError('缺少服务器信息');
|
throw new DashboardError('缺少服务器信息');
|
||||||
}
|
}
|
||||||
if (!this.selectedPaymentMethod) {
|
|
||||||
throw new DashboardError('请选择支付方式');
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onSuccess(data) {
|
onSuccess(data) {
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
@ -316,10 +313,8 @@ export default {
|
|||||||
},
|
},
|
||||||
processBalancePayment() {
|
processBalancePayment() {
|
||||||
return {
|
return {
|
||||||
url: 'jcloud.api.billing.process_balance_payment_for_server_renew_order',
|
url: 'jcloud.api.billing.process_balance_payment_for_server_order',
|
||||||
params: {
|
params: {},
|
||||||
order_id: this.order?.order_id
|
|
||||||
},
|
|
||||||
validate() {
|
validate() {
|
||||||
if (!this.order || !this.order.order_id) {
|
if (!this.order || !this.order.order_id) {
|
||||||
throw new DashboardError('缺少订单信息');
|
throw new DashboardError('缺少订单信息');
|
||||||
@ -333,8 +328,15 @@ export default {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务器续费API
|
// 支付成功,等待支付完成回调处理续费
|
||||||
this.$resources.renewServer.submit();
|
this.$emit('success', {
|
||||||
|
order: this.order,
|
||||||
|
renewal_period: this.selectedPeriod,
|
||||||
|
message: response.message || '支付成功'
|
||||||
|
});
|
||||||
|
|
||||||
|
this.isProcessingPayment = false;
|
||||||
|
this.paymentSuccess = true;
|
||||||
},
|
},
|
||||||
onError(error) {
|
onError(error) {
|
||||||
this.error = error.message || '余额支付处理失败';
|
this.error = error.message || '余额支付处理失败';
|
||||||
@ -345,9 +347,7 @@ export default {
|
|||||||
processAlipayPayment() {
|
processAlipayPayment() {
|
||||||
return {
|
return {
|
||||||
url: 'jcloud.api.billing.process_alipay_order',
|
url: 'jcloud.api.billing.process_alipay_order',
|
||||||
params: {
|
params: {},
|
||||||
order_id: this.order?.order_id
|
|
||||||
},
|
|
||||||
validate() {
|
validate() {
|
||||||
if (!this.order || !this.order.order_id) {
|
if (!this.order || !this.order.order_id) {
|
||||||
throw new DashboardError('缺少订单信息');
|
throw new DashboardError('缺少订单信息');
|
||||||
@ -371,9 +371,7 @@ export default {
|
|||||||
processWechatPayment() {
|
processWechatPayment() {
|
||||||
return {
|
return {
|
||||||
url: 'jcloud.api.billing.process_wechatpay_order',
|
url: 'jcloud.api.billing.process_wechatpay_order',
|
||||||
params: {
|
params: {},
|
||||||
order_id: this.order?.order_id
|
|
||||||
},
|
|
||||||
validate() {
|
validate() {
|
||||||
if (!this.order || !this.order.order_id) {
|
if (!this.order || !this.order.order_id) {
|
||||||
throw new DashboardError('缺少订单信息');
|
throw new DashboardError('缺少订单信息');
|
||||||
@ -395,9 +393,7 @@ export default {
|
|||||||
checkPaymentStatus() {
|
checkPaymentStatus() {
|
||||||
return {
|
return {
|
||||||
url: 'jcloud.api.billing.check_order_payment_status',
|
url: 'jcloud.api.billing.check_order_payment_status',
|
||||||
params: {
|
params: {},
|
||||||
order_id: this.order?.order_id
|
|
||||||
},
|
|
||||||
validate() {
|
validate() {
|
||||||
if (!this.order || !this.order.order_id) {
|
if (!this.order || !this.order.order_id) {
|
||||||
throw new DashboardError('缺少订单信息');
|
throw new DashboardError('缺少订单信息');
|
||||||
@ -408,43 +404,15 @@ export default {
|
|||||||
// 支付成功,停止轮询
|
// 支付成功,停止轮询
|
||||||
this.stopPaymentCheck();
|
this.stopPaymentCheck();
|
||||||
|
|
||||||
// 调用服务器续费API
|
// 支付成功,等待支付完成回调处理续费
|
||||||
this.$resources.renewServer.submit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
},
|
|
||||||
renewServer() {
|
|
||||||
return {
|
|
||||||
url: 'jcloud.api.aliyun_server_light.renew_aliyun_instance',
|
|
||||||
params: {
|
|
||||||
instance_id: this.serverInfo?.instance_id,
|
|
||||||
period: this.selectedPeriod,
|
|
||||||
region_id: this.serverInfo?.region
|
|
||||||
},
|
|
||||||
validate() {
|
|
||||||
if (!this.serverInfo?.instance_id || !this.serverInfo?.region) {
|
|
||||||
throw new DashboardError('缺少服务器信息');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onSuccess(response) {
|
|
||||||
if (response.message?.success) {
|
|
||||||
this.$emit('success', {
|
this.$emit('success', {
|
||||||
order: this.order,
|
order: this.order,
|
||||||
renewal_period: this.selectedPeriod,
|
renewal_period: this.selectedPeriod,
|
||||||
message: response.message.message
|
message: '支付成功'
|
||||||
});
|
});
|
||||||
|
|
||||||
this.isProcessingPayment = false;
|
|
||||||
this.paymentSuccess = true;
|
this.paymentSuccess = true;
|
||||||
} else {
|
|
||||||
this.error = response.message?.message || '续费失败';
|
|
||||||
this.isProcessingPayment = false;
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
onError(error) {
|
|
||||||
this.error = error.message || '服务器续费失败';
|
|
||||||
this.isProcessingPayment = false;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -455,16 +423,10 @@ export default {
|
|||||||
this.show = false;
|
this.show = false;
|
||||||
},
|
},
|
||||||
createRenewalOrder() {
|
createRenewalOrder() {
|
||||||
if (!this.selectedPaymentMethod) {
|
|
||||||
this.error = '请选择支付方式';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.error = null;
|
this.error = null;
|
||||||
this.$resources.createRenewalOrder.submit({
|
this.$resources.createRenewalOrder.submit({
|
||||||
server: this.server,
|
server: this.server,
|
||||||
renewal_months: this.selectedPeriod,
|
renewal_months: this.selectedPeriod
|
||||||
payment_method: this.selectedPaymentMethod
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
processPayment() {
|
processPayment() {
|
||||||
@ -478,18 +440,26 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.selectedPaymentMethod === 'balance') {
|
if (this.selectedPaymentMethod === 'balance') {
|
||||||
this.$resources.processBalancePayment.submit();
|
this.$resources.processBalancePayment.submit({
|
||||||
|
order_id: this.order.order_id
|
||||||
|
});
|
||||||
} else if (this.selectedPaymentMethod === 'alipay') {
|
} else if (this.selectedPaymentMethod === 'alipay') {
|
||||||
this.$resources.processAlipayPayment.submit();
|
this.$resources.processAlipayPayment.submit({
|
||||||
|
order_id: this.order.order_id
|
||||||
|
});
|
||||||
} else if (this.selectedPaymentMethod === 'wechatpay') {
|
} else if (this.selectedPaymentMethod === 'wechatpay') {
|
||||||
this.$resources.processWechatPayment.submit();
|
this.$resources.processWechatPayment.submit({
|
||||||
|
order_id: this.order.order_id
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
startPaymentCheck() {
|
startPaymentCheck() {
|
||||||
this.isProcessingPayment = false;
|
this.isProcessingPayment = false;
|
||||||
|
|
||||||
this.checkInterval = setInterval(() => {
|
this.checkInterval = setInterval(() => {
|
||||||
this.$resources.checkPaymentStatus.submit();
|
this.$resources.checkPaymentStatus.submit({
|
||||||
|
order_id: this.order.order_id
|
||||||
|
});
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
// 15分钟后停止检查
|
// 15分钟后停止检查
|
||||||
|
|||||||
@ -809,6 +809,101 @@ def create_server_order(**kwargs):
|
|||||||
jingrow.log_error("创建服务器订单失败", str(e))
|
jingrow.log_error("创建服务器订单失败", str(e))
|
||||||
return {"success": False, "message": str(e)}
|
return {"success": False, "message": str(e)}
|
||||||
|
|
||||||
|
@jingrow.whitelist()
|
||||||
|
def create_server_renew_order(**kwargs):
|
||||||
|
"""创建服务器续费订单"""
|
||||||
|
try:
|
||||||
|
server = kwargs.get('server')
|
||||||
|
renewal_months = kwargs.get('renewal_months', 1)
|
||||||
|
|
||||||
|
if not server:
|
||||||
|
jingrow.throw("缺少服务器信息")
|
||||||
|
|
||||||
|
# 验证输入
|
||||||
|
server_pg = jingrow.get_pg("Jsite Server", server)
|
||||||
|
if not server_pg:
|
||||||
|
jingrow.throw("服务器不存在")
|
||||||
|
|
||||||
|
team = server_pg.team
|
||||||
|
|
||||||
|
# 验证当前用户权限
|
||||||
|
current_team = get_current_team(True)
|
||||||
|
if current_team.name != team:
|
||||||
|
jingrow.throw("您没有权限为此服务器创建续费订单")
|
||||||
|
|
||||||
|
# 计算续费金额
|
||||||
|
renewal_months = int(renewal_months)
|
||||||
|
plan_price = server_pg.plan_price or 0
|
||||||
|
total_amount = plan_price * renewal_months
|
||||||
|
|
||||||
|
# 生成唯一订单号
|
||||||
|
order_id = datetime.now().strftime('%Y%m%d%H%M%S%f')[:-3] + ''.join(random.choices('0123456789', k=6))
|
||||||
|
|
||||||
|
# 创建订单记录
|
||||||
|
order = jingrow.get_pg({
|
||||||
|
"pagetype": "Order",
|
||||||
|
"order_id": order_id,
|
||||||
|
"order_type": "服务器续费",
|
||||||
|
"team": team,
|
||||||
|
"status": "待支付",
|
||||||
|
"total_amount": total_amount,
|
||||||
|
"title": server_pg.instance_id,
|
||||||
|
"description": str(renewal_months) # 存储续费月数
|
||||||
|
})
|
||||||
|
|
||||||
|
order.insert(ignore_permissions=True)
|
||||||
|
jingrow.db.commit()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"order": order.as_dict()
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
jingrow.log_error("续费订单错误", f"创建续费订单失败: {str(e)}")
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": f"创建续费订单失败: {str(e)}"
|
||||||
|
}
|
||||||
|
|
||||||
|
def renew_server(order_name):
|
||||||
|
"""支付成功后异步续费服务器"""
|
||||||
|
try:
|
||||||
|
order = jingrow.get_pg("Order", order_name)
|
||||||
|
if not order:
|
||||||
|
raise Exception("订单不存在")
|
||||||
|
|
||||||
|
# 从订单中获取信息
|
||||||
|
instance_id = order.title # 实例ID
|
||||||
|
renewal_months = int(order.description) # 续费月数
|
||||||
|
|
||||||
|
# 查找服务器记录获取区域信息
|
||||||
|
server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id})
|
||||||
|
if not server:
|
||||||
|
raise Exception("找不到对应的服务器记录")
|
||||||
|
|
||||||
|
# 调用阿里云API续费
|
||||||
|
manager = _get_manager()
|
||||||
|
result = manager.renew_instance(instance_id, renewal_months, server.region)
|
||||||
|
|
||||||
|
if not result or not result.get('success'):
|
||||||
|
raise Exception(f"阿里云续费失败: {result.get('message', '未知错误')}")
|
||||||
|
|
||||||
|
# 更新服务器到期时间
|
||||||
|
server.end_date = jingrow.utils.add_months(server.end_date or jingrow.utils.nowdate(), renewal_months)
|
||||||
|
server.save(ignore_permissions=True)
|
||||||
|
|
||||||
|
# 更新订单状态
|
||||||
|
order.status = "交易成功"
|
||||||
|
order.save(ignore_permissions=True)
|
||||||
|
|
||||||
|
jingrow.db.commit()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
jingrow.log_error("服务器续费失败", f"订单 {order_name}: {str(e)}")
|
||||||
|
raise e
|
||||||
|
|
||||||
def create_aliyun_server(order_name):
|
def create_aliyun_server(order_name):
|
||||||
"""异步创建服务器"""
|
"""异步创建服务器"""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -1089,6 +1089,9 @@ def handle_order_payment_complete(order_id):
|
|||||||
process_balance_recharge(order)
|
process_balance_recharge(order)
|
||||||
elif order.order_type == "网站续费":
|
elif order.order_type == "网站续费":
|
||||||
process_site_renew(order_id)
|
process_site_renew(order_id)
|
||||||
|
elif order.order_type == "服务器续费":
|
||||||
|
# 异步续费服务器
|
||||||
|
jingrow.enqueue('jcloud.api.aliyun_server_light.renew_server', order_name=order.name)
|
||||||
elif order.order_type == "新建服务器":
|
elif order.order_type == "新建服务器":
|
||||||
# 异步创建服务器
|
# 异步创建服务器
|
||||||
jingrow.enqueue('jcloud.api.aliyun_server_light.create_aliyun_server', order_name=order.name)
|
jingrow.enqueue('jcloud.api.aliyun_server_light.create_aliyun_server', order_name=order.name)
|
||||||
@ -1621,9 +1624,9 @@ def process_balance_payment_for_server_order(order_id):
|
|||||||
order.save(ignore_permissions=True)
|
order.save(ignore_permissions=True)
|
||||||
jingrow.db.commit()
|
jingrow.db.commit()
|
||||||
|
|
||||||
# 异步执行服务器创建
|
# 支付成功后,调用订单完成处理函数
|
||||||
jingrow.enqueue('jcloud.api.aliyun_server_light.create_aliyun_server', order_name=order.name)
|
handle_order_payment_complete(order_id)
|
||||||
jingrow.log_error("异步执行创建服务器", f"订单ID: {order_id}")
|
|
||||||
return {
|
return {
|
||||||
"status": "Success",
|
"status": "Success",
|
||||||
"message": "支付成功",
|
"message": "支付成功",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user