Files
gitlab-instance-0a899031_pa…/app/main.py
2025-12-17 06:36:07 -06:00

84 lines
2.4 KiB
Python
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.
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import hashlib
import time
import urllib.parse
import random
import string
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ==================== XorPay 配置 ====================
AID = "8220" # 你的 aid
SECRET = "afcacd99570945f88de62624aaa3578e" # 你的 app secret
CASHIER_URL = f"https://xorpay.com/api/cashier/{AID}" # 收银台接口
# ====================================================
# 官方推荐签名方式(严格无分隔符拼接)
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
return hashlib.md5(raw.encode('utf-8')).hexdigest().lower()
@app.post("/payh5")
async def payh5(item: dict):
print('payh5 item:', item)
total_fee_yuan = f"{item['total_fee'] / 100:.2f}"
random_suffix = ''.join(random.choices(string.hexdigits.lower(), k=8))
order_id = f"order_{int(time.time())}_{random_suffix}"
type_map = {
"book": "购买书籍",
"meetup": "活动报名",
"video": "视频解锁",
"vip": "VIP会员充值",
}
name = type_map.get(item.get('type'), "商品支付")
notify_url = "https://www.hackrobot.cn".rstrip("/")
# 临时不传 return_url避免 ngrok 干扰
# return_url = item.get('callback_url')
params = {
"name": name,
"pay_type": "jsapi",
"price": total_fee_yuan,
"order_id": order_id,
"notify_url": notify_url,
}
# if return_url:
# params["return_url"] = return_url
params["sign"] = xorpay_sign(
params["name"],
params["pay_type"],
params["price"],
params["order_id"],
params["notify_url"]
)
print("生成的参数:", params)
# 返回参数字典,前端用 form POST
return {
"status": "ok",
"xorpay_params": params,
"xorpay_url": f"https://xorpay.com/api/cashier/{AID}"
}
# 异步通知回调
@app.post("/xorpay_notify")
async def xorpay_notify(request: dict):
print("XorPay 异步通知:", request)
# TODO: 正式上线后加验签和订单处理
return {"status": "ok"}