feat(huya): 开通支付码复用未支付旧单 + 二维码改用支付宝收银台直达地址

- 限购商品(精英宝典 buy_limit=1)挂着未支付旧单时,真正先失败的是购买权限
  校验 checkUserBuyAuth(真机 703「已超过该商品的购买上限,下单失败~」),
  createOrderV5 根本不会执行;现在 checkUserBuyAuth 与 createOrderV5 两处失败
  都会回退 find_reusable_order,拿同一商品的未支付旧单继续 payOrderSubmitV5。
- 新增支付宝收银台直达地址解析(resolve_pay_entry_url):跟随网关 302 后的
  excashier.alipay.com/standard/auth.htm?payOrderId=…(93 字符,原始网关地址
  1616 字符)写入结果 pay_cashier_url;解析失败回落本站短链
  /api/huya/pay/<task_id>?k=<hmac>,后端跑在 localhost 时客户也扫得开。
- 前端 QRCode 显式 color="#000" bgColor="#fff":antd v6 暗色主题默认
  color=token.colorText、bgColor=transparent,白码落在白底容器里根本看不见。
- 支付成功后列表「二维码」列不再展示二维码(qrDoneLabel → 已支付/已开通)。

测试:pytest 767 passed / 6 skipped;typecheck、eslint、ruff 通过。
This commit is contained in:
yml2213
2026-10-15 19:28:26 +08:00
parent f61284e808
commit 0b9a01ab3f
9 changed files with 850 additions and 44 deletions
+14 -1
View File
@@ -16,6 +16,7 @@ from core.huya.frame_decoder import decode_struct_bytes
from core.huya.http_client import HuyaHttpClient
from core.huya.shop_structs import ShopRawRsp
from web.backend.models import HuyaAccount
from web.backend.services.huya_pay_link import pay_qr_path
from web.backend.services.huya_runner import HuyaBatchRunner
UID = 1199668595203
@@ -332,6 +333,7 @@ def test_create_recharge_order_confirms_elite_open_after_payment(db, monkeypatch
supplier_name="和平精英官方小店",
promotion={},
promotion_info="本单无返利",
reused_order={},
)
shop_client = _StubClient(redeem=_redeem(REDEEM_OPENED_HEX), season=True)
@@ -380,13 +382,20 @@ def test_create_recharge_order_confirms_elite_open_after_payment(db, monkeypatch
"web.backend.services.huya_runner_recharge.run_open_flow",
lambda *args, **kwargs: outcome,
)
# 二维码内容优先用支付宝收银台直达地址(真机:网关 302 后的落点)
monkeypatch.setattr(
"web.backend.services.huya_runner_recharge.resolve_pay_entry_url",
lambda _pay_url, **_kwargs: (
"https://excashier.alipay.com/standard/auth.htm?payOrderId=x.25"
),
)
account = HuyaAccount(uid=str(UID), cookie=COOKIE)
db.add(account)
db.commit()
runner._execute_create_recharge_order(
db,
None,
SimpleNamespace(id=587),
account,
{"uid": UID, "cookie": COOKIE},
{"room_pid": "1199650619883", "pay_channel": "Zfb", "sid": "2203"},
@@ -399,6 +408,10 @@ def test_create_recharge_order_confirms_elite_open_after_payment(db, monkeypatch
assert result["elite_open_time"] == 1792053457
assert result["elite_expire_time"] == 1798732799
assert result["post_payment_points"] == 280
# 二维码内容给前端两条短地址(原始支付地址 1626 字符,扫码扫不动)
assert result["pay_qr_path"] == pay_qr_path(587)
# 优先用支付宝收银台直达地址(公网域名,localhost 下客户也扫得开)
assert result["pay_cashier_url"].startswith("https://excashier.alipay.com/")
assert account.elite_opened is True
assert account.elite_expire_time == 1798732799
assert account.points == 280
+181
View File
@@ -0,0 +1,181 @@
"""开通支付码二维码内容:1600+ 字符的支付地址 → 手机扫得动、打得开的地址。
生产实测(2026-10-15):`payOrderSubmitV5` 返回的支付宝网关地址有 **1626** 字符
(`https://openapi.alipay.com/gateway.do?...&sign=...`)。直接把它编成二维码是
version 29+(140+ 个模块),在 150px 的画面上每个模块只有约 1 像素 —— 手机扫不上。
现在的优先级:
1. **支付宝收银台直达地址**(`resolve_pay_entry_url`):网关地址 302 两次后落到
`https://excashier.alipay.com/standard/auth.htm?payOrderId=...`(约 74 字符),
公网域名,后端跑在 localhost 时客户也扫得开;
2. **本站短链** `/api/huya/pay/<task_id>?k=<hmac>`(约 38 字符):后端 302 跳到网关地址,
但要求本页域名手机能访问到。
"""
import pytest
from fastapi import HTTPException
from web.backend.models import HuyaAccount, HuyaTask, User
from web.backend.routers.huya.pay import redirect_huya_pay
from web.backend.services import huya_pay_link
from web.backend.services.huya_pay_link import (
PAY_PATH_PREFIX,
is_alipay_url,
pay_qr_path,
pay_token,
resolve_pay_entry_url,
verify_pay_token,
)
# 真机取到的支付宝网关地址形态(长度同量级)
ALIPAY_LONG_URL = (
"https://openapi.alipay.com/gateway.do"
"?alipay_sdk=alipay-sdk-java-4.40.272.ALL&app_id=2017123456789012"
"&method=alipay.trade.page.pay&sign=" + "a" * 1500
)
# 真机实测:上面的网关地址 302 两次后的落点(任务 191 原样取回)
CASHIER_URL = (
"https://excashier.alipay.com/standard/auth.htm"
"?payOrderId=6cb86f1d259642ac843522b85dab8a52.25"
)
def _create_pay_task(db, pay_url: str) -> HuyaTask:
user = User(username="pay-link-test", password_hash="x", role="support")
db.add(user)
db.commit()
account = HuyaAccount(uid="1199665170519", cookie="yyuid=1199665170519", tag="")
db.add(account)
db.commit()
task = HuyaTask(
batch_id="test-batch",
account_id=account.id,
task_type="create_recharge_order",
handbook_scope="elite",
status="running",
created_by=user.id,
result={"pay_url": pay_url} if pay_url else {},
)
db.add(task)
db.commit()
return task
def test_pay_token_round_trip_and_rejects_others():
token = pay_token(587)
assert len(token) == 16
assert verify_pay_token(587, token) is True
# 大小写不同就是另一个签名(hex 严格比对)
assert verify_pay_token(587, token.upper()) is False
assert verify_pay_token(587, "") is False
assert verify_pay_token(588, token) is False
def test_pay_qr_path_is_short_enough_to_scan():
path = pay_qr_path(587)
assert path.startswith(f"{PAY_PATH_PREFIX}/587?k=")
# 短链必须足够短:36 字符 → 二维码约 30 个模块(长链是 140+ 个模块)
assert len(path) <= 48
# 原始支付地址长度对比(就是它导致扫不上)
assert len(ALIPAY_LONG_URL) > 1000
def test_pay_redirect_route_returns_pay_url(db):
task = _create_pay_task(db, ALIPAY_LONG_URL)
response = redirect_huya_pay(task.id, pay_token(task.id), db)
assert response.status_code == 302
assert response.headers["location"] == ALIPAY_LONG_URL
def test_pay_redirect_route_rejects_bad_token(db):
task = _create_pay_task(db, ALIPAY_LONG_URL)
with pytest.raises(HTTPException) as excinfo:
redirect_huya_pay(task.id, "deadbeefdeadbeef", db)
assert excinfo.value.status_code == 404
def test_pay_redirect_route_rejects_task_without_pay_url(db):
task = _create_pay_task(db, "")
with pytest.raises(HTTPException) as excinfo:
redirect_huya_pay(task.id, pay_token(task.id), db)
assert excinfo.value.status_code == 404
class _FakeResponse:
"""假的 requests 响应:只需要 ``url`` 和 ``close()``。"""
def __init__(self, url: str):
self.url = url
self.closed = False
def close(self) -> None:
self.closed = True
def _patch_get(monkeypatch, *, final_url: str | None = None, error=None):
calls: list[dict] = []
def fake_get(url, **kwargs):
calls.append({"url": url, **kwargs})
if error is not None:
raise error
return _FakeResponse(final_url or "")
monkeypatch.setattr(huya_pay_link.requests, "get", fake_get)
return calls
def test_resolve_pay_entry_url_follows_redirect_to_cashier(monkeypatch):
calls = _patch_get(monkeypatch, final_url=CASHIER_URL)
assert resolve_pay_entry_url(ALIPAY_LONG_URL) == CASHIER_URL
assert calls[0]["url"] == ALIPAY_LONG_URL
# 必须跟随 302,否则拿到的是网关页自己
assert calls[0]["allow_redirects"] is True
assert "Mozilla" in calls[0]["headers"]["User-Agent"]
def test_resolve_pay_entry_url_rejects_non_alipay_target(monkeypatch):
"""落到非支付宝域名就当没解析出来(调用方回落到本站短链)。"""
_patch_get(monkeypatch, final_url="https://evil.example.com/cashier")
assert resolve_pay_entry_url(ALIPAY_LONG_URL) == ""
def test_resolve_pay_entry_url_swallows_errors(monkeypatch):
_patch_get(monkeypatch, error=RuntimeError("network down"))
assert resolve_pay_entry_url(ALIPAY_LONG_URL) == ""
def test_resolve_pay_entry_url_ignores_blank_or_relative():
assert resolve_pay_entry_url("") == ""
assert resolve_pay_entry_url("/api/huya/pay/191?k=x") == ""
def test_resolve_pay_entry_url_is_shorter_than_raw_url(monkeypatch):
_patch_get(monkeypatch, final_url=CASHIER_URL)
resolved = resolve_pay_entry_url(ALIPAY_LONG_URL)
# 收银台直达地址约 74 字符 → 二维码模块数远低于原始地址
assert len(resolved) < 120
assert len(resolved) < len(ALIPAY_LONG_URL)
def test_is_alipay_url_matches_only_alipay_domains():
assert is_alipay_url(CASHIER_URL) is True
assert is_alipay_url("https://openapi.alipay.com/gateway.do?x=1") is True
assert is_alipay_url("https://alipay.com/") is True
assert is_alipay_url("https://alipay.com.evil.com/") is False
assert is_alipay_url("https://excashier.alipay.com.evil.com/") is False
assert is_alipay_url("") is False
+310
View File
@@ -0,0 +1,310 @@
"""限购商品的旧订单复用:已有未支付订单时不该「已超过该商品的购买上限」直接失败。
生产反馈(2026-10-15):账号上挂着上一张没付掉的宝典订单时,先是**购买权限校验**
`checkUserBuyAuth` 返回 703
已超过该商品的购买上限,下单失败~(如有未支付订单,请及时完成支付)
(后端日志 18:34 / 18:48 / 19:00 三批都停在 checkUserBuyAuth,createOrderV5 根本没发),
下单接口 `createOrderV5` 也可能返回同一句。限购 1 件的商品,旧单没付掉就再也下不了
新单 —— 所以这两种失败都要复用那张旧单去发起支付(官方页面同样是复用旧单继续付)。
"""
from types import SimpleNamespace
from web.backend.services.huya_shop_flow import find_reusable_order, run_open_flow
BUY_LIMIT_MESSAGE = (
"已超过该商品的购买上限,下单失败~(如有未支付订单,请及时完成支付)"
)
SPU_ID = "hy-8328015"
UNPAID_ELITE_ORDER = {
"order_id": "11346649",
"item_name": "精英宝典",
"order_status": 10,
"pay_time": 0,
"goods_detail": {"spu_id": SPU_ID, "sku_id": 7756811},
}
PAID_ELITE_ORDER = {
"order_id": "11346648",
"item_name": "精英宝典",
"order_status": 60,
"pay_time": 1791895396000,
"goods_detail": {"spu_id": SPU_ID, "sku_id": 7756811},
}
OTHER_GOODS_ORDER = {
"order_id": "11346647",
"item_name": "精英币",
"order_status": 10,
"pay_time": 0,
"goods_detail": {"spu_id": "hy-8327356", "sku_id": 7759407},
}
#: 真机 2026-10-15:账号 66 的旧单被用户取消 → order_status=90、pay_time=0
CANCELLED_ELITE_ORDER = {
"order_id": "11357769",
"item_name": "精英宝典",
"order_status": 90,
"pay_time": 0,
"goods_detail": {"spu_id": SPU_ID, "sku_id": 7756811},
}
class _Order(SimpleNamespace):
def to_dict(self) -> dict:
return dict(self.__dict__)
class _OrderList(SimpleNamespace):
pass
class _Client:
"""商城链路假客户端:只造流程需要的那几个返回。"""
def __init__(
self,
*,
create_code: int,
create_message: str = "",
orders=None,
auth_code: int = 200,
auth_message: str = "",
):
self.create_code = create_code
self.create_message = create_message
self.orders = orders or []
self.auth_code = auth_code
self.auth_message = auth_message
self.pay_order_ids: list[int] = []
self.order_list_calls = 0
self.create_calls = 0
def get_goods_info(self, **kwargs):
return SimpleNamespace(
code=200,
message="",
to_dict=lambda: {
"sku_id": 7756811,
"name": "精英宝典",
"price": 3000,
},
)
def get_supplier_info(self, **kwargs):
resp = SimpleNamespace(code=200, message="", payload={})
resp.value = lambda *path: "和平精英官方小店"
return resp
def list_pay_channels(self, **kwargs):
return SimpleNamespace(code=200, message="", channels=["Zfb"])
def check_hy_protocol(self, **kwargs):
return SimpleNamespace(code=200, message="")
def play_mall_get_my_promotion(self, **kwargs):
return SimpleNamespace(code=200, message="", to_dict=lambda: {"code": 200})
def calc_order_promotion(self, **kwargs):
return SimpleNamespace(code=200, message="", to_dict=lambda: {"code": 200})
def accept_hy_protocol(self, **kwargs):
return SimpleNamespace(code=200, message="")
def check_user_buy_auth(self, **kwargs):
return SimpleNamespace(code=self.auth_code, message=self.auth_message)
def create_order(self, **kwargs):
self.create_calls += 1
if self.create_code == 200:
return SimpleNamespace(
code=200,
message="",
orderId=11346649,
to_dict=lambda: {"order_id": 11346649, "code": 200},
)
return SimpleNamespace(
code=self.create_code,
message=self.create_message,
orderId=0,
to_dict=lambda: {"code": self.create_code, "message": self.create_message},
)
def query_user_order_list(self, **kwargs):
self.order_list_calls += 1
return _OrderList(
totalCount=len(self.orders),
orders=[_Order(**item) for item in self.orders],
)
def pay_order_submit(self, **kwargs):
self.pay_order_ids.append(int(kwargs.get("order_id") or 0))
return SimpleNamespace(
code=200,
message="",
payUrl="https://openapi.alipay.com/gateway.do?probe=1",
appOrderId="app-1",
payOrderId="pay-1",
amount=3000,
to_dict=lambda: {
"code": 200,
"pay_url": "https://openapi.alipay.com/gateway.do?probe=1",
},
)
def _run(client: _Client):
return run_open_flow(
client,
uid=1199665170519,
cookie="yyuid=1199665170519",
pid=1199650619883,
spu_id=SPU_ID,
sku_id=7756811,
count=1,
pay_channel="Zfb",
)
def test_reuse_unpaid_order_when_create_hits_buy_limit():
client = _Client(
create_code=703, create_message=BUY_LIMIT_MESSAGE, orders=[UNPAID_ELITE_ORDER]
)
outcome = _run(client)
assert outcome.ok is True
assert outcome.order_id == 11346649 # 复用旧单
assert outcome.message == "已复用未支付订单 #11346649"
assert outcome.pay_url.startswith("https://openapi.alipay.com/gateway.do")
assert client.pay_order_ids == [11346649] # 支付发给的是旧单
assert client.create_calls == 1 # 先试过下单,失败后才复用
assert outcome.reused_order["order_id"] == "11346649"
def test_auth_buy_limit_reuses_unpaid_order_without_creating():
"""真机 2026-10-15:限购校验先报 703,这时复用旧单且不该再发下单请求。"""
client = _Client(
create_code=200,
orders=[UNPAID_ELITE_ORDER],
auth_code=703,
auth_message=BUY_LIMIT_MESSAGE,
)
outcome = _run(client)
assert outcome.ok is True
assert outcome.order_id == 11346649
assert outcome.message == "已复用未支付订单 #11346649"
assert client.pay_order_ids == [11346649]
assert client.create_calls == 0
assert outcome.reused_order["order_status"] == 10
def test_auth_buy_limit_without_unpaid_order_keeps_original_error():
"""没有可复用的旧单时保持原样:报限购错误、不下单、不起支付。"""
client = _Client(
create_code=200,
orders=[],
auth_code=703,
auth_message=BUY_LIMIT_MESSAGE,
)
outcome = _run(client)
assert outcome.ok is False
assert outcome.message == BUY_LIMIT_MESSAGE
assert client.pay_order_ids == []
assert client.create_calls == 0
def test_auth_buy_limit_ignores_paid_and_cancelled_orders():
client = _Client(
create_code=200,
orders=[CANCELLED_ELITE_ORDER, PAID_ELITE_ORDER],
auth_code=703,
auth_message=BUY_LIMIT_MESSAGE,
)
outcome = _run(client)
assert outcome.ok is False
assert outcome.message == BUY_LIMIT_MESSAGE
assert client.pay_order_ids == []
def test_auth_no_response_fails_without_touching_orders():
client = _Client(create_code=200, orders=[UNPAID_ELITE_ORDER], auth_code=200)
client.check_user_buy_auth = lambda **kwargs: None
outcome = _run(client)
assert outcome.ok is False
assert outcome.message == "购买权限校验接口无响应"
assert client.create_calls == 0
assert client.order_list_calls == 0
def test_paid_order_is_not_reused():
client = _Client(
create_code=703, create_message=BUY_LIMIT_MESSAGE, orders=[PAID_ELITE_ORDER]
)
outcome = _run(client)
assert outcome.ok is False
assert outcome.message == BUY_LIMIT_MESSAGE
assert client.pay_order_ids == []
def test_cancelled_order_is_not_reused():
"""已取消(90)的旧单不能复用(真机:用户取消后状态变 90)。"""
client = _Client(
create_code=703,
create_message=BUY_LIMIT_MESSAGE,
orders=[CANCELLED_ELITE_ORDER],
)
outcome = _run(client)
assert outcome.ok is False
assert client.pay_order_ids == []
def test_other_goods_order_is_not_reused():
client = _Client(
create_code=703, create_message=BUY_LIMIT_MESSAGE, orders=[OTHER_GOODS_ORDER]
)
outcome = _run(client)
assert outcome.ok is False
assert client.pay_order_ids == []
def test_create_success_does_not_look_up_old_orders():
client = _Client(create_code=200)
outcome = _run(client)
assert outcome.ok is True
assert outcome.message == "已生成支付二维码"
assert outcome.reused_order == {}
# 下单成功就不该多打一次订单列表
assert client.order_list_calls == 0
def test_find_reusable_order_skips_to_next_page():
class _PagedClient(_Client):
def query_user_order_list(self, **kwargs):
self.order_list_calls += 1
offset = int(kwargs.get("offset") or 0)
page = [OTHER_GOODS_ORDER] * 20 if offset == 0 else [UNPAID_ELITE_ORDER]
return _OrderList(totalCount=21, orders=[_Order(**item) for item in page])
client = _PagedClient(create_code=200)
reused = find_reusable_order(client, uid=1, cookie="c", spu_id=SPU_ID)
assert reused["order_id"] == "11346649"
assert client.order_list_calls == 2