功能: 接入鱼翅供应商直充渠道

This commit is contained in:
yml2213
2026-08-13 21:11:20 +08:00
parent 420ee3d95c
commit 902e669614
14 changed files with 586 additions and 15 deletions
+65 -2
View File
@@ -34,7 +34,7 @@ class FishFinRechargeClientTests(unittest.TestCase):
}, "POST")
self.assertEqual(sign, "4087a959f3488ecb13efe6ef58e3bc67")
def test_create_order_posts_json_with_signed_nested_arguments(self):
def test_create_order_posts_json_with_signed_optional_arguments(self):
response = Mock()
response.json.return_value = {"code": 200, "order_status": 0}
self.session.post.return_value = response
@@ -52,10 +52,27 @@ class FishFinRechargeClientTests(unittest.TestCase):
self.assertEqual(result["code"], 200)
kwargs = self.session.post.call_args.kwargs
self.assertEqual(self.session.post.call_args.args[0], "https://supplier.example/adapter-apiaccess/open/api/createOrderV2")
self.assertEqual(kwargs["json"]["customer_price"], "1.2")
self.assertEqual(kwargs["json"]["customer_price"], 1.2)
self.assertEqual(kwargs["json"]["sign"], self.client.sign(kwargs["json"], "POST"))
self.assertEqual(kwargs["timeout"], (8, 20))
def test_create_order_omits_empty_optional_fields_and_serializes_integer_price(self):
response = Mock()
response.json.return_value = {"code": 200}
self.session.post.return_value = response
self.client.create_order(
charge_account="10001", buy_num=1, customer_price="1.0",
customer_order_no="merchant-001", product_id="111570",
)
body = self.session.post.call_args.kwargs["json"]
self.assertEqual(body["customer_price"], 1)
self.assertNotIn("notify_url", body)
self.assertNotIn("recharge_arg", body)
self.assertNotIn("ext_arg", body)
self.assertEqual(body["sign"], self.client.sign(body, "POST"))
def test_query_order_requires_identifier_and_uses_get_params(self):
with self.assertRaisesRegex(ValueError, "订单标识"):
self.client.query_order()
@@ -74,6 +91,52 @@ class FishFinRechargeClientTests(unittest.TestCase):
with self.assertRaises(FishFinRechargeConfigError):
FishFinRechargeClient(FishFinRechargeConfig(base_url="", app_id="", app_secret=""))
def test_trace_excludes_replayable_signature_and_nested_account_data(self):
response = Mock()
response.status_code = 200
response.json.return_value = {"code": 200, "result": {"order_status": 0}}
self.session.post.return_value = response
events = []
client = FishFinRechargeClient(self.config, session=self.session, trace=events.append)
client.create_order(
charge_account="10001", buy_num=1, customer_price="0.993",
customer_order_no="merchant-001", product_id="111570",
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
)
self.assertEqual([event["stage"] for event in events], ["request", "response"])
self.assertNotIn("sign", events[0]["params"])
self.assertNotIn("recharge_arg", events[0]["params"])
self.assertEqual(events[0]["params"]["customer_price"], 0.993)
def test_debug_trace_includes_complete_request_and_response_bodies(self):
response = Mock()
response.status_code = 200
response.headers = {"Content-Type": "application/json", "X-Request-Id": "request-1"}
response.text = '{"code":1000,"message":"未传递支付金额","sign":"response-sign"}'
response.json.return_value = {"code": 1000, "message": "未传递支付金额", "sign": "response-sign"}
self.session.post.return_value = response
events = []
config = FishFinRechargeConfig(
base_url=self.config.base_url, app_id=self.config.app_id,
app_secret=self.config.app_secret, debug=True,
)
client = FishFinRechargeClient(config, session=self.session, trace=events.append)
client.create_order(
charge_account="10001", buy_num=1, customer_price="0.993",
customer_order_no="merchant-001", product_id="111570",
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
)
self.assertEqual(events[0]["json_body"]["sign"], client.sign(events[0]["json_body"], "POST"))
self.assertEqual(events[0]["json_body"]["customer_price"], 0.993)
self.assertNotIn("sign", events[0]["sign_params"])
self.assertIn(self.config.app_secret, events[0]["sign_source"])
self.assertEqual(events[1]["response_body"]["sign"], "response-sign")
self.assertEqual(events[1]["response_text"], response.text)
if __name__ == "__main__":
unittest.main()