python 支付接口自动化如何做
✅ 本节目标
我们将实现一个:
🧪 基于 Flask 的本地 Mock 支付回调服务
📡 可接收并验证签名的回调请求
🧾 返回标准响应格式(如微信/支付宝)
🔄 支持不同场景(支付成功、失败、超时等)
📦 所需依赖
确保你已安装以下库:
pip install flask pytest respx requests
📁 推荐目录结构
-
payment_automation/ -
│ -
├── mock_server/ -
│ ├── app.py # 回调服务主程序 -
│ └── config.py # 回调配置(如签名密钥) -
│ -
├── testcases/ -
│ └── test_payment_callback.py # 测试用例 -
│ -
├── utils/ -
│ └── sign_utils.py # 签名工具类 -
│ -
├── config/ -
│ └── payment_config.py # 支付配置 -
│ -
└── requirements.txt
🔧 核心代码实现
1️⃣ 回调配置:mock_server/config.py
-
# mock_server/config.py -
CALLBACK_CONFIG = { -
"host": "127.0.0.1", -
"port": 5000, -
"public_key": "third_party_public_key", # 第三方公钥(用于验签) -
"success_response": {"return_code": "SUCCESS", "return_msg": "OK"}, -
"fail_response": {"return_code": "FAIL", "return_msg": "SIGNATURE_FAILED"} -
}
2️⃣ 签名验证工具类:utils/sign_utils.py
-
# utils/sign_utils.py -
import hashlib -
import hmac -
from urllib.parse import quote_plus -
def generate_sign(params: dict, secret_key: str) -> str: -
""" -
生成签名(按 key 排序后拼接) -
""" -
sorted_params = "&".join(f"{k}={quote_plus(str(v))}" for k, v in sorted(params.items())) -
sign = hmac.new(secret_key.encode(), digestmod=hashlib.sha256) -
sign.update(sorted_params.encode()) -
return sign.hexdigest() -
def verify_sign(received_sign: str, params: dict, secret_key: str) -> bool: -
""" -
验证签名是否一致 -
""" -
expected_sign = generate_sign(params, secret_key) -
return hmac.compare_digest(expected_sign, received_sign)
3️⃣ Mock 回调服务:mock_server/app.py
-
# mock_server/app.py -
from flask import Flask, request, jsonify -
from utils.sign_utils import verify_sign -
from mock_server.config import CALLBACK_CONFIG -
app = Flask(__name__) -
PUBLIC_KEY = CALLBACK_CONFIG["public_key"] -
SUCCESS_RESPONSE = CALLBACK_CONFIG["success_response"] -
FAIL_RESPONSE = CALLBACK_CONFIG["fail_response"] -
@app.route("/wechat/notify", methods=["POST"]) -
def wechat_notify(): -
data = request.json -
sign = data.pop("sign", None) -
if not sign or not verify_sign(sign, data, PUBLIC_KEY): -
return jsonify(FAIL_RESPONSE), 200 -
# 模拟业务处理逻辑(更新订单状态) -
print("收到微信支付回调数据:", data) -
return jsonify(SUCCESS_RESPONSE), 200 -
@app.route("/alipay/notify", methods=["POST"]) -
def alipay_notify(): -
data = request.form.to_dict() -
sign = data.pop("sign", None) -
if not sign or not verify_sign(sign, data, PUBLIC_KEY): -
return "failure" -
print("收到支付宝支付回调数据:", data) -
return "success" -
if __name__ == "__main__": -
app.run(host="0.0.0.0", port=5000)
启动方式:
-
cd mock_server -
python app.py
🧪 使用示例:启动 Mock 服务 + 发送模拟回调
你可以使用 ngrok 或 localtunnel 将本地服务暴露为公网 URL:
ngrok http 5000
将 https://xxx.ngrok.io/wechat/notify 设置为你的支付平台回调地址即可。
📌 示例测试用例:testcases/test_payment_callback.py
-
# testcases/test_payment_callback.py -
import requests -
import json -
import pytest -
from utils.sign_utils import generate_sign -
from mock_server.config import CALLBACK_CONFIG -
BASE_URL = "http://localhost:5000" -
SECRET_KEY = CALLBACK_CONFIG["public_key"] -
def send_wechat_notify(order_id: str, status: str = "SUCCESS"): -
data = { -
"transaction_id": "T1234567890", -
"out_trade_no": order_id, -
"return_code": status, -
"total_fee": 9990, # 单位分 -
"time_end": "20250405120000" -
} -
data["sign"] = generate_sign(data, SECRET_KEY) -
response = requests.post(f"{BASE_URL}/wechat/notify", json=data) -
return response -
def test_wechat_callback_success(): -
order_id = "ORDER123456" -
response = send_wechat_notify(order_id) -
assert response.status_code == 200 -
assert response.json()["return_code"] == "SUCCESS" -
def test_wechat_callback_invalid_sign(): -
data = { -
"transaction_id": "T1234567890", -
"out_trade_no": "ORDER789012", -
"return_code": "SUCCESS", -
"total_fee": 9990, -
"time_end": "20250405120000", -
"sign": "invalid-sign-here" -
} -
response = requests.post(f"{BASE_URL}/wechat/notify", json=data) -
assert response.status_code == 200 -
assert response.json()["return_code"] == "FAIL"
🧩 进阶功能建议
功能 描述
✅ 多种支付结果模拟 如 success / failed / timeout
✅ 支持动态返回值 通过 URL 参数控制返回内容
✅ 支持数据库记录日志 存储每次回调请求详情
✅ 异步回调重试机制 模拟支付平台多次通知
✅ 结合 pytest fixtures 自动启动/关闭 Mock 服务
📦 启动命令汇总
# 启动 Mock 回调服务
cd mock_server
python app.py
# 在另一个终端运行测试
cd ..
pytest testcases/test_payment_callback.py -v
✅ 总结
这个 Mock 支付回调模拟器 具备以下优点:
特性 说明
✅ 安全签名验证 支持 HMAC-SHA256 签名校验
✅ 多支付平台支持 微信、支付宝回调均可模拟
✅ 易于扩展 可添加更多支付渠道和异常场景
✅ 与测试框架集成 支持 pytest 自动化断言
✅ 支持公网访问 配合 ngrok 可供外部调用
感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取

更多推荐


所有评论(0)