's'
This commit is contained in:
21
app/main.py
21
app/main.py
@@ -9,7 +9,6 @@ import requests
|
|||||||
from fastapi import Request, HTTPException
|
from fastapi import Request, HTTPException
|
||||||
import logging
|
import logging
|
||||||
from pocketbase import PocketBase
|
from pocketbase import PocketBase
|
||||||
from pocketbase.client import ListResult # 新增导入(可选,但明确类型)
|
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@@ -21,14 +20,12 @@ app.add_middleware(
|
|||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# ==================== PocketBase 配置 ====================
|
# ==================== PocketBase 配置 ====================
|
||||||
PB_URL = "https://pocketbase.hackrobot.cn"
|
PB_URL = "https://pocketbase.hackrobot.cn"
|
||||||
PB_ADMIN_EMAIL = "xiaoshuang.eric@gmail.com"
|
PB_ADMIN_EMAIL = "xiaoshuang.eric@gmail.com"
|
||||||
PB_ADMIN_PASSWORD = "Xiao4669805@"
|
PB_ADMIN_PASSWORD = "Xiao4669805@"
|
||||||
# ========================================================
|
# ========================================================
|
||||||
|
|
||||||
# 动态获取已登录的 PocketBase 客户端
|
|
||||||
def get_pb_client():
|
def get_pb_client():
|
||||||
client = PocketBase(PB_URL)
|
client = PocketBase(PB_URL)
|
||||||
try:
|
try:
|
||||||
@@ -39,12 +36,11 @@ def get_pb_client():
|
|||||||
raise e
|
raise e
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
# 配置日志
|
# 配置日志
|
||||||
logging.basicConfig(filename='xorpay_notify.log', level=logging.INFO,
|
logging.basicConfig(filename='xorpay_notify.log', level=logging.INFO,
|
||||||
format='%(asctime)s - %(message)s')
|
format='%(asctime)s - %(message)s')
|
||||||
|
|
||||||
# 全局已处理订单(保留原始)
|
# 全局已处理订单
|
||||||
processed_orders = set()
|
processed_orders = set()
|
||||||
|
|
||||||
# ==================== XorPay 配置 ====================
|
# ==================== XorPay 配置 ====================
|
||||||
@@ -52,16 +48,14 @@ AID = "8220"
|
|||||||
SECRET = "afcacd99570945f88de62624aaa3578e"
|
SECRET = "afcacd99570945f88de62624aaa3578e"
|
||||||
# ====================================================
|
# ====================================================
|
||||||
|
|
||||||
# memos 配置(保留原始)
|
# memos 配置
|
||||||
MEMOS_API = "https://qun.hackrobot.cn/api/v1/users"
|
MEMOS_API = "https://qun.hackrobot.cn/api/v1/users"
|
||||||
MEMOS_TOKEN = "eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoiaGFja3JvYm90IiwiaXNzIjoibWVtb3MiLCJzdWIiOiIxIiwiYXVkIjpbInVzZXIuYWNjZXNzLXRva2VuIl0sImlhdCI6MTc1NzQwNTE0OX0.Idn7kBlxE-CoSOXwWuZ1tHGIRKHAIeDyXSafGS5OHsg"
|
MEMOS_TOKEN = "eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoiaGFja3JvYm90IiwiaXNzIjoibWVtb3MiLCJzdWIiOiIxIiwiYXVkIjpbInVzZXIuYWNjZXNzLXRva2VuIl0sImlhdCI6MTc1NzQwNTE0OX0.Idn7kBlxE-CoSOXwWuZ1tHGIRKHAIeDyXSafGS5OHsg"
|
||||||
|
|
||||||
# 官方推荐签名方式
|
|
||||||
def xorpay_sign(name: str, pay_type: str, price: str, order_id: str, notify_url: str) -> str:
|
def xorpay_sign(name: str, pay_type: str, price: str, order_id: str, notify_url: str) -> str:
|
||||||
raw = name + pay_type + price + order_id + notify_url + SECRET
|
raw = name + pay_type + price + order_id + notify_url + SECRET
|
||||||
return hashlib.md5(raw.encode('utf-8')).hexdigest().lower()
|
return hashlib.md5(raw.encode('utf-8')).hexdigest().lower()
|
||||||
|
|
||||||
# 回调验签函数
|
|
||||||
def verify_xorpay_notify(data: dict) -> bool:
|
def verify_xorpay_notify(data: dict) -> bool:
|
||||||
received_sign = data.get("sign", "")
|
received_sign = data.get("sign", "")
|
||||||
aoid = data.get("aoid", "")
|
aoid = data.get("aoid", "")
|
||||||
@@ -112,7 +106,6 @@ def create_memos_user(user_id: str, password: str = "123456"):
|
|||||||
print(f"创建 memos 用户异常: {e}")
|
print(f"创建 memos 用户异常: {e}")
|
||||||
logging.error(f"创建 memos 用户异常 - username: {user_id}, error: {e}")
|
logging.error(f"创建 memos 用户异常 - username: {user_id}, error: {e}")
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
|
||||||
@app.post("/payh5")
|
@app.post("/payh5")
|
||||||
async def payh5(item: dict):
|
async def payh5(item: dict):
|
||||||
@@ -123,7 +116,7 @@ async def payh5(item: dict):
|
|||||||
pay_type = item.get('type', 'unknown').lower()
|
pay_type = item.get('type', 'unknown').lower()
|
||||||
|
|
||||||
random_suffix = ''.join(random.choices(string.hexdigits.lower(), k=8))
|
random_suffix = ''.join(random.choices(string.hexdigits.lower(), k=8))
|
||||||
timestamp = int(time.time())
|
timestamp = int(time.time()) # 修正:去掉多余字符
|
||||||
order_id = f"{user_id}_{pay_type}_order_{timestamp}_{random_suffix}"
|
order_id = f"{user_id}_{pay_type}_order_{timestamp}_{random_suffix}"
|
||||||
|
|
||||||
type_map = {
|
type_map = {
|
||||||
@@ -242,15 +235,14 @@ async def xorpay_notify(request: Request):
|
|||||||
|
|
||||||
valid_types = ["vip", "book", "meetup", "video"]
|
valid_types = ["vip", "book", "meetup", "video"]
|
||||||
if pay_type not in valid_types:
|
if pay_type not in valid_types:
|
||||||
print(f"无效的 pay_type: {pay_type},强制设为 vip 或拒绝写入")
|
print(f"无效的 pay_type: {pay_type},强制设为 vip")
|
||||||
pay_type = "vip"
|
pay_type = "vip"
|
||||||
|
|
||||||
amount_cents = int(float(data.get("pay_price", 0)) * 100)
|
amount_cents = int(float(data.get("pay_price", 0)) * 100)
|
||||||
|
|
||||||
if pay_type == "meetup":
|
if pay_type == "meetup":
|
||||||
solan = pb_client.collection("solan")
|
solan = pb_client.collection("solan")
|
||||||
# 修复bug:pocketbase-py SDK 用 get_list 而非 list
|
existing = solan.get_list(1, 1, {"filter": f'user_id="{user_id}"'})
|
||||||
existing: ListResult = solan.get_list(1, 1, {"filter": f'user_id="{user_id}"'})
|
|
||||||
if existing.items:
|
if existing.items:
|
||||||
record_id = existing.items[0].id
|
record_id = existing.items[0].id
|
||||||
solan.update(record_id, {
|
solan.update(record_id, {
|
||||||
@@ -280,11 +272,10 @@ async def xorpay_notify(request: Request):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"PocketBase 操作失败: {e}")
|
print(f"PocketBase 操作失败: {e}")
|
||||||
logging.error(f"PocketBase 操作失败 - order_id: {order_id}, error: {e}")
|
logging.error(f"PocketBase 操作失败 - order_id: {order_id}, error: {e}")
|
||||||
processed_orders.add(order_id)
|
processed_orders.add(order_id) # 防止重复通知
|
||||||
|
|
||||||
return "ok"
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
class CreateUserRequest(BaseModel):
|
class CreateUserRequest(BaseModel):
|
||||||
username: str = None
|
username: str = None
|
||||||
password: str = None
|
password: str = None
|
||||||
|
|||||||
Reference in New Issue
Block a user