This website requires JavaScript.
0
Issues
Pull Requests
Milestones
Explore
0
Create…
New Repository
New Migration
New Organization
public
Signed in as
public
Profile
Starred
Subscriptions
Settings
Help
Sign Out
public
/
jinzuanwork
Unwatch
1
Star
0
Fork
0
Code
Issues
Pull Requests
Actions
Packages
Projects
Releases
Wiki
Activity
Settings
Files
jinzuanwork
/
backend
/
backend-site
/
tests
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- coding: utf-8 -*- """收款自动化测试(2026-10-07):精确反查、主动对账、买家自查、去掉金额猜单、付款配置键名、支付宝待开通。 用法:python3 tests/收款对账测试.py <server.py 所在目录> · 临时数据目录 + 进程内起一份 server;爱发电开放接口用假的(FAKE 列表),不连外网。 """ import base64, json, os, struct, sys, tempfile, threading, urllib.error, urllib.request, zlib SRC = os.path.abspath(sys.argv[1]) ROOT = tempfile.mkdtemp(prefix="ys-pay-") os.environ["YS_ROOT"] = ROOT sys.path.insert(0, SRC) import server as S # noqa: E402 RESULTS = [] def check(name, cond, detail=""): RESULTS.append((name, bool(cond))) print(("PASS " if cond else "FAIL ") + name + ((" —— " + str(detail)[:200]) if detail != "" else ""), flush=True) # ---------------------------------------------------------------- 假的爱发电开放接口 FAKE, CALLS = [], [] S.payplatform_creds = lambda: ("uid-test", "tok-test") def fake_api(path, params=None): params = params or {} CALLS.append(dict(params)) if params.get("out_trade_no"): ids = str(params["out_trade_no"]).split(",") return {"ec": 200, "data": {"list": [o for o in FAKE if o["out_trade_no"] in ids], "total_page": 1}} pg = int(params.get("page") or 1) lst = list(reversed(FAKE))[(pg - 1) * 50: pg * 50] return {"ec": 200, "data": {"list": lst, "total_page": max(1, (len(FAKE) + 49) // 50)}} S.payplatform_api = fake_api _n = [0] def afd(custom="", amount="6.00", remark="", status=2): _n[0] += 1 o = {"out_trade_no": "2026100712%010d" % _n[0], "custom_order_id": custom, "user_id": "buyer%d" % _n[0], "plan_id": "plan", "total_amount": amount, "show_amount": amount, "status": status, "remark": remark, "plan_title": "自选金额", "title": "", "sku_detail": [], "create_time": 0} FAKE.append(o) return o # ---------------------------------------------------------------- 起服务 srv = S.ThreadingHTTPServer(("127.0.0.1", 0), S.H) srv.daemon_threads = True threading.Thread(target=srv.serve_forever, daemon=True).start() BASE = "http://127.0.0.1:%d" % srv.server_address[1] def req(method, path, obj=None, headers=None): r = urllib.request.Request(BASE + path, data=json.dumps(obj).encode() if obj is not None else None, method=method) r.add_header("Content-Type", "application/json") for k, v in (headers or {}).items(): r.add_header(k, v) try: with urllib.request.urlopen(r, timeout=30) as resp: return resp.status, json.loads(resp.read()) except urllib.error.HTTPError as e: return e.code, json.loads(e.read() or b"{}") def png(seed=0): raw = b"".join(b"\x00" + b"".join(bytes(((x * 7 + y * 3 + seed) % 256, x % 256, y % 256, 255)) for x in range(64)) for y in range(64)) ch = lambda t, d: struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xffffffff) return (b"\x89PNG\r\n\x1a\n" + ch(b"IHDR", struct.pack(">IIBBBBB", 64, 64, 8, 6, 0, 0, 0)) + ch(b"IDAT", zlib.compress(raw)) + ch(b"IEND", b"")) _ip = [0] def prepay(templates=("stand",)): _ip[0] += 1 body = {"skin": "data:image/png;base64," + base64.b64encode(png(_ip[0])).decode(), "fileName": "t.png", "arm": "classic", "templates": list(templates), "res": 1000, "codes": [], "pay": "prepay", "terms": {"v": S.TERMS_VERSION, "agreed": True}} st, j = req("POST", "/api/submit", body, {"X-Real-IP": "203.0.113.%d" % _ip[0]}) assert j.get("ok"), j return j["order"], j["token"], S.read_meta(j["order"])["amount"] def state(oid): return (S.read_meta(oid) or {}).get("state") def hook(o): return req("POST", "/api/payplatform-hook", {"ec": 200, "em": "ok", "data": {"type": "order", "order": o}}) # ================================================================ 1. 付款配置键名 with open(os.path.join(ROOT, "payinfo.json"), "w", encoding="utf-8") as f: json.dump({"afdian": "https://example.com/fixed", "afdian_uid": "UID-REAL"}, f) st, j = req("GET", "/api/payinfo") check("付款配置写的是 afdian / afdian_uid → 接口两种键名都给到(页面能拼出专链)", j.get("afdian_uid") == "UID-REAL" and j.get("payplatform_uid") == "UID-REAL" and j.get("afdian") == "https://example.com/fixed", {k: j.get(k) for k in ("afdian", "afdian_uid", "payplatform_uid")}) check("支付宝扫码:只预留入口,状态是「待开通」", (j.get("alipay") or {}).get("on") is False and (j.get("alipay") or {}).get("note") == "待开通", j.get("alipay")) # ================================================================ 2. 回调:精确反查 + 激活 oid, tok, amt = prepay(("stand", "wheat")) o = afd(custom=oid, amount="%.2f" % amt) CALLS.clear() st, j = hook(o) check("回调带回订单号 → 先按 out_trade_no 精确反查 → 激活", state(oid) == "waiting" and CALLS and CALLS[0].get("out_trade_no") == o["out_trade_no"], (state(oid), CALLS[:2])) check("回调应答是 JSON 且 ec=200", st == 200 and j.get("ec") == 200, j) st, j = hook(o) check("同一笔回调再推一次 → 不重复处理,仍回 ec=200", j.get("ec") == 200 and state(oid) == "waiting", j) # ================================================================ 3. 主动对账:回调丢了也能激活 oid2, tok2, amt2 = prepay() afd(custom=oid2, amount="%.2f" % amt2) # 只在爱发电那边有,回调没推过来 before = state(oid2) got = S.payplatform_reconcile() check("回调丢了 → 定时对账发现并激活", before == "unpaid" and oid2 in got and state(oid2) == "waiting", (before, got)) led_n = len(S.STORE.ledger_all()) got2 = S.payplatform_reconcile() check("再对一次账 → 不会重复激活、账本不多记", got2 == [] and len(S.STORE.ledger_all()) == led_n, (got2, led_n)) # 留言里写了订单号(没走专链)也认 oid3, tok3, amt3 = prepay() afd(custom="", amount="%.2f" % amt3, remark="我的订单 %s 谢谢" % oid3) got = S.payplatform_reconcile() check("没走专链、但留言里写了订单号 → 对账也能认出来", oid3 in got and state(oid3) == "waiting", got) # ================================================================ 4. 金额不足不激活 oid4, tok4, amt4 = prepay(("stand", "wheat", "beach")) afd(custom=oid4, amount="0.10") S.payplatform_reconcile() led = S.STORE.ledger_all() rec = [v for v in led.values() if v.get("want_order") == oid4] check("实付 ¥0.10 < 应付 → 不激活,记一笔待人工核对", state(oid4) == "unpaid" and rec and rec[0].get("need_human"), rec[:1]) # ================================================================ 5. 去掉「金额唯一匹配」猜单 oid5, tok5, amt5 = prepay(("stand", "boom", "sakura", "airship")) o5 = afd(custom="", amount="%.2f" % amt5, remark="") hook(o5) check("没带订单号、留言也空 → 不再按「金额唯一」去猜,网页单保持待付款", state(oid5) == "unpaid", state(oid5)) # ================================================================ 6. 买家点「我付好了」→ 当场对账 oid6, tok6, amt6 = prepay() afd(custom=oid6, amount="%.2f" % amt6) st, j = req("POST", "/api/paid", {"oid": oid6, "token": tok6}) check("买家点「我付好了」→ 当场查到付款,直接开始排队", j.get("found") and state(oid6) == "waiting", j) oid7, tok7, amt7 = prepay() st, j = req("POST", "/api/paid", {"oid": oid7, "token": tok7}) check("还没付就点「我付好了」→ 照旧登记成「待确认」,不会被激活", state(oid7) == "paid" and not j.get("found"), (state(oid7), j)) afd(custom=oid7, amount="%.2f" % amt7) S.payplatform_reconcile() check("登记过「待确认」的单,钱到了 → 对账自动激活,不用人工", state(oid7) == "waiting", state(oid7)) # ================================================================ 7. 卡密单 / 不相关的单不受影响 code = S.gen_codes(1)[0] _ip[0] += 1 st, j = req("POST", "/api/submit", {"skin": "data:image/png;base64," + base64.b64encode(png(99)).decode(), "fileName": "c.png", "arm": "classic", "templates": ["stand"], "res": 1000, "codes": [code], "terms": {"v": S.TERMS_VERSION, "agreed": True}}, {"X-Real-IP": "203.0.113.%d" % _ip[0]}) check("回归:卡密下单照旧直接进队列", j.get("ok") and state(j["order"]) == "waiting", j) srv.shutdown() bad = [n for n, ok in RESULTS if not ok] print("\n%d 条,失败 %d 条" % (len(RESULTS), len(bad))) for n in bad: print(" ✗ " + n) sys.exit(1 if bad else 0)
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
main
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel