's'
This commit is contained in:
@@ -51,14 +51,11 @@ async def global_exception_handler(request: Request, exc: Exception):
|
||||
logging.error(f"未处理异常: {request.url.path} - {exc}", exc_info=True)
|
||||
return JSONResponse(status_code=500, content={"detail": str(exc)})
|
||||
|
||||
# 日志
|
||||
# 日志(仅控制台,不写文件,避免 git 本地/线上不一致)
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(message)s",
|
||||
handlers=[
|
||||
logging.FileHandler("payment_notify.log"),
|
||||
logging.StreamHandler(),
|
||||
],
|
||||
handlers=[logging.StreamHandler()],
|
||||
)
|
||||
|
||||
# 挂载路由
|
||||
|
||||
Binary file not shown.
@@ -167,3 +167,24 @@ class ZPayProvider(PaymentProvider):
|
||||
|
||||
def success_response(self) -> str:
|
||||
return "success"
|
||||
|
||||
def query_order_status(self, out_trade_no: str, timeout: int = 15) -> Dict[str, Any]:
|
||||
"""查询 ZPAY 订单支付状态(供 nomadvip/digital/cnomadcna/nomadlms 共用)"""
|
||||
url = f"https://zpayz.cn/api.php?act=order&pid={self.pid}&key={self.key}&out_trade_no={out_trade_no}"
|
||||
try:
|
||||
resp = requests.get(url, timeout=timeout)
|
||||
result = resp.json() if resp.ok else {}
|
||||
except Exception as e:
|
||||
return {"paid": False, "error": str(e)}
|
||||
try:
|
||||
code = result.get("code", 0)
|
||||
code = int(code) if isinstance(code, (int, float)) or str(code).isdigit() else 0
|
||||
except (ValueError, TypeError):
|
||||
code = 0
|
||||
if code != 1:
|
||||
return {"paid": False, "msg": result.get("msg", "查询失败")}
|
||||
try:
|
||||
status = int(result.get("status", 0))
|
||||
except (ValueError, TypeError):
|
||||
status = 0
|
||||
return {"paid": status == 1, "status": status}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -7,10 +7,9 @@ import random
|
||||
import string
|
||||
import time
|
||||
|
||||
import requests
|
||||
from fastapi import APIRouter, Request, HTTPException
|
||||
|
||||
from ..config import BASE_URL, ZPAY_PID, ZPAY_KEY
|
||||
from ..config import BASE_URL
|
||||
from ..payment import get_payment_provider
|
||||
from ..payment.zpay import ZPayProvider
|
||||
from ..services import handle_payment_success, processed_orders
|
||||
@@ -142,27 +141,12 @@ def create_digital_router(prefix: str, project_name: str) -> APIRouter:
|
||||
async def zpay_order_status(out_trade_no: str):
|
||||
"""查询 ZPAY 订单支付状态"""
|
||||
provider = get_payment_provider()
|
||||
if provider.name != "zpay":
|
||||
if not isinstance(provider, ZPayProvider):
|
||||
raise HTTPException(status_code=400, detail="仅 zpay 渠道支持")
|
||||
url = f"https://zpayz.cn/api.php?act=order&pid={ZPAY_PID}&key={ZPAY_KEY}&out_trade_no={out_trade_no}"
|
||||
try:
|
||||
resp = requests.get(url, timeout=10)
|
||||
result = resp.json() if resp.ok else {}
|
||||
except Exception as e:
|
||||
logging.warning(f"ZPAY 订单查询失败: {e}")
|
||||
return {"paid": False, "error": str(e)}
|
||||
try:
|
||||
code = result.get("code", 0)
|
||||
code = int(code) if isinstance(code, (int, float)) or str(code).isdigit() else 0
|
||||
except (ValueError, TypeError):
|
||||
code = 0
|
||||
if code != 1:
|
||||
return {"paid": False, "msg": result.get("msg", "查询失败")}
|
||||
try:
|
||||
status = int(result.get("status", 0))
|
||||
except (ValueError, TypeError):
|
||||
status = 0
|
||||
return {"paid": status == 1, "status": status}
|
||||
result = provider.query_order_status(out_trade_no, timeout=15)
|
||||
if result.get("error"):
|
||||
logging.warning(f"{project_name} ZPAY 订单查询失败: {result.get('error')}")
|
||||
return result
|
||||
|
||||
@router.post("/xorpay_notify")
|
||||
async def xorpay_notify(request: Request):
|
||||
|
||||
@@ -7,8 +7,6 @@ import time
|
||||
from fastapi import APIRouter, Request, HTTPException
|
||||
from fastapi.responses import RedirectResponse, JSONResponse
|
||||
|
||||
import requests
|
||||
|
||||
from ..config import BASE_URL, ZPAY_PID, ZPAY_KEY
|
||||
from ..payment.zpay import ZPayProvider
|
||||
from ..services import handle_payment_success, processed_orders
|
||||
@@ -118,25 +116,10 @@ async def nomadvip_payh5_redirect(item: dict, request: Request):
|
||||
@router.get("/zpay_order_status")
|
||||
async def nomadvip_zpay_order_status(out_trade_no: str):
|
||||
"""nomadvip 专用:查询 ZPAY 订单支付状态"""
|
||||
url = f"https://zpayz.cn/api.php?act=order&pid={ZPAY_PID}&key={ZPAY_KEY}&out_trade_no={out_trade_no}"
|
||||
try:
|
||||
resp = requests.get(url, timeout=10)
|
||||
result = resp.json() if resp.ok else {}
|
||||
except Exception as e:
|
||||
logging.warning(f"nomadvip ZPAY 订单查询失败: {e}")
|
||||
return {"paid": False, "error": str(e)}
|
||||
try:
|
||||
code = result.get("code", 0)
|
||||
code = int(code) if isinstance(code, (int, float)) or str(code).isdigit() else 0
|
||||
except (ValueError, TypeError):
|
||||
code = 0
|
||||
if code != 1:
|
||||
return {"paid": False, "msg": result.get("msg", "查询失败")}
|
||||
try:
|
||||
status = int(result.get("status", 0))
|
||||
except (ValueError, TypeError):
|
||||
status = 0
|
||||
return {"paid": status == 1, "status": status}
|
||||
result = _nomadvip_zpay.query_order_status(out_trade_no, timeout=15)
|
||||
if result.get("error"):
|
||||
logging.warning(f"nomadvip ZPAY 订单查询失败: {result.get('error')}")
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/zpay_notify")
|
||||
|
||||
Reference in New Issue
Block a user