Files
gitlab-instance-0a899031_pa…/app/main.py
2026-03-10 05:25:08 -05:00

85 lines
2.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
支付 API 主入口
支持多支付渠道XorPay、ZPAY通过 PAYMENT_PROVIDER 环境变量切换
路由模块化,按项目拆分:
- nomadvip: /nomadvip/* - 固定 ZPAY
- digital: /digital/* - 使用 PAYMENT_PROVIDER
- cnomadcna: /cnomadcna/* - 使用 PAYMENT_PROVIDER
- nomadlms: /nomadlms/* - 使用 PAYMENT_PROVIDER
- legacy: /payh5、/zpay_notify 等(向后兼容)
- common: /submit_meetup_application、/create_user
本地调试BASE_URL 需为 ZPAY 可访问地址(如 ngrok
线上部署https://api.hackrobot.cn
"""
import logging
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from .routers import (
nomadvip_router,
digital_router,
cnomadcna_router,
nomadlms_router,
legacy_router,
common_router,
)
app = FastAPI(
title="PayJS API",
description="nomadvip / digital / cnomadcna / nomadlms 支付接口",
version="2.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""捕获未处理异常,记录日志后返回 500HTTPException 交由 FastAPI 默认处理)"""
if isinstance(exc, HTTPException):
raise exc
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.StreamHandler()],
)
# 挂载路由
app.include_router(nomadvip_router)
app.include_router(digital_router)
app.include_router(cnomadcna_router)
app.include_router(nomadlms_router)
app.include_router(legacy_router)
app.include_router(common_router)
@app.get("/")
async def root():
"""健康检查"""
return {
"status": "ok",
"service": "payjsapi",
"routes": {
"nomadvip": "/nomadvip/payh5, /nomadvip/payh5/redirect, /nomadvip/zpay_order_status, /nomadvip/zpay_notify",
"digital": "/digital/payh5, /digital/payh5/redirect, /digital/zpay_order_status, /digital/zpay_notify, /digital/xorpay_notify",
"cnomadcna": "/cnomadcna/payh5, ...",
"nomadlms": "/nomadlms/payh5, ...",
"legacy": "/payh5, /payh5/redirect, /zpay_notify, /xorpay_notify, /zpay_order_status",
"common": "/submit_meetup_application, /create_user",
},
}