修改前台动态获取支付是否开启
This commit is contained in:
@@ -21,6 +21,17 @@ from app.services.payment import (
|
||||
router = APIRouter(prefix="/payments", tags=["payments"])
|
||||
|
||||
|
||||
@router.get("/methods")
|
||||
async def get_payment_methods(db: AsyncSession = Depends(get_db)):
|
||||
"""Return which payment methods are enabled (from admin config)."""
|
||||
from app.services.payment import _get_payment_configs
|
||||
configs = await _get_payment_configs(db)
|
||||
return {
|
||||
"alipay": configs.get("payment_alipay_enabled", "").lower() == "true",
|
||||
"wechat": configs.get("payment_wechat_enabled", "").lower() == "true",
|
||||
}
|
||||
|
||||
|
||||
@router.post("/recharge", response_model=PaymentOrderOut)
|
||||
async def recharge(
|
||||
req: RechargeRequest,
|
||||
@@ -30,6 +41,14 @@ async def recharge(
|
||||
if req.method not in ("wechat", "alipay"):
|
||||
raise HTTPException(status_code=400, detail="不支持的支付方式")
|
||||
|
||||
# Check if the selected payment method is enabled in admin config
|
||||
from app.services.payment import _get_payment_configs, _is_mock_mode
|
||||
configs = await _get_payment_configs(db)
|
||||
if not _is_mock_mode(configs):
|
||||
enabled_key = f"payment_{req.method}_enabled"
|
||||
if configs.get(enabled_key, "").lower() != "true":
|
||||
raise HTTPException(status_code=400, detail="该支付方式未启用")
|
||||
|
||||
result = await db.execute(
|
||||
select(RechargePackage).where(
|
||||
RechargePackage.id == req.plan,
|
||||
@@ -40,15 +59,18 @@ async def recharge(
|
||||
pkg = result.scalar_one_or_none()
|
||||
if not pkg:
|
||||
raise HTTPException(status_code=400, detail="无效的套餐")
|
||||
order = await create_recharge_order(
|
||||
db,
|
||||
current_user.id,
|
||||
credits=pkg.credits,
|
||||
price=pkg.price,
|
||||
label=pkg.name,
|
||||
bonus_credits=pkg.bonus_credits,
|
||||
method=req.method,
|
||||
)
|
||||
try:
|
||||
order = await create_recharge_order(
|
||||
db,
|
||||
current_user.id,
|
||||
credits=pkg.credits,
|
||||
price=pkg.price,
|
||||
label=pkg.name,
|
||||
bonus_credits=pkg.bonus_credits,
|
||||
method=req.method,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return order
|
||||
|
||||
|
||||
|
||||
@@ -97,6 +97,22 @@ async def create_recharge_order(
|
||||
Returns the order; for Alipay the ``qr_url`` attribute will be populated
|
||||
with the scan-to-pay URL.
|
||||
"""
|
||||
# Read config from database first
|
||||
db_configs = await _get_payment_configs(db)
|
||||
mock_mode = _is_mock_mode(db_configs)
|
||||
|
||||
# In real mode, validate that the payment method is enabled and configured
|
||||
if not mock_mode:
|
||||
enabled_key = f"payment_{method}_enabled"
|
||||
if db_configs.get(enabled_key, "").lower() != "true":
|
||||
raise ValueError("该支付方式未启用,请联系管理员")
|
||||
if method == "alipay":
|
||||
if not db_configs.get("payment_alipay_app_id") or not db_configs.get("payment_alipay_private_key"):
|
||||
raise ValueError("支付宝支付未完成配置,请联系管理员")
|
||||
elif method == "wechat":
|
||||
if not db_configs.get("payment_wechat_mch_id") or not db_configs.get("payment_wechat_api_key"):
|
||||
raise ValueError("微信支付未完成配置,请联系管理员")
|
||||
|
||||
total_credits = credits + bonus_credits
|
||||
order = PaymentOrder(
|
||||
id=generate_id(),
|
||||
@@ -110,10 +126,6 @@ async def create_recharge_order(
|
||||
db.add(order)
|
||||
await db.flush()
|
||||
|
||||
# Read config from database
|
||||
db_configs = await _get_payment_configs(db)
|
||||
mock_mode = _is_mock_mode(db_configs)
|
||||
|
||||
if mock_mode:
|
||||
# Mock: immediately complete payment
|
||||
order.status = "paid"
|
||||
|
||||
Reference in New Issue
Block a user