核心链路: - purchase_realtime:算价→余额预检→计费写用量→实时复式记账→开权益,失败回滚 - _grant_subscription 续费修复:未到期从 end_date 顺延不丢剩余天数,已到期重算 - check_subscription_valid 到期门禁:挂到 execute_product,到期/未购买阻断使用 - get_my_subscriptions 我的权益列表(到期状态) - UI:purchase_confirm 确认页(余额+容量/月数输入)、purchase_realtime 支付、 my_subscriptions 权益列表+续费、usermenu.ui 挂菜单
43 lines
1.5 KiB
Plaintext
43 lines
1.5 KiB
Plaintext
# 实时支付接口:点击确认支付 → purchase_realtime(算价→余额预检→计费→实时记账→开权益)
|
||
from ahserver.serverenv import ServerEnv
|
||
result = {'widgettype': 'Message', 'options': {
|
||
'title': '支付失败', 'message': '未知错误', 'type': 'error', 'timeout': 5}}
|
||
|
||
try:
|
||
userid = await get_user()
|
||
if not userid:
|
||
raise Exception('请先登录')
|
||
|
||
product_id = params_kw.get('product_id', '')
|
||
if not product_id:
|
||
raise Exception('缺少产品ID')
|
||
|
||
env = ServerEnv()
|
||
r = await env.purchase_realtime(
|
||
product_id=product_id,
|
||
user_id=userid,
|
||
charge_mode=params_kw.get('charge_mode') or None,
|
||
quantity=int(params_kw.get('quantity', 1) or 1),
|
||
storage_gb=float(params_kw.get('storage_gb', 0) or 0),
|
||
valid_months=int(params_kw.get('valid_months', 1) or 1))
|
||
|
||
if not r.get('success'):
|
||
raise Exception(r.get('message', '支付失败'))
|
||
|
||
result = {
|
||
'widgettype': 'Message',
|
||
'options': {
|
||
'title': '支付成功',
|
||
'type': 'success',
|
||
'timeout': 6,
|
||
'message': ('%s 购买成功,实付 ¥%.2f,订单号 %s,权益有效期至 %s,已实时记账'
|
||
% (r.get('product_name', ''), r.get('amount', 0),
|
||
r.get('orderid', ''), r.get('end_date', '')))
|
||
}
|
||
}
|
||
except Exception as e:
|
||
debug(f'purchase_realtime.dspy error: {format_exc()}')
|
||
result['options']['message'] = str(e)
|
||
|
||
return json.dumps(result, ensure_ascii=False)
|