优化斗鱼充值

This commit is contained in:
yml2213
2026-08-13 21:43:27 +08:00
parent 902e669614
commit 0bf69c1240
19 changed files with 450 additions and 266 deletions
+26 -19
View File
@@ -34,16 +34,15 @@ class FishFinRechargeClientTests(unittest.TestCase):
}, "POST")
self.assertEqual(sign, "4087a959f3488ecb13efe6ef58e3bc67")
def test_create_order_posts_json_with_signed_optional_arguments(self):
def test_create_order_posts_new_document_payload(self):
response = Mock()
response.json.return_value = {"code": 200, "order_status": 0}
self.session.post.return_value = response
result = self.client.create_order(
charge_account="10001",
buy_num=2,
customer_price="1.20",
customer_order_no="merchant-001",
pay_amount="1.20",
out_order_id="OUT202503010010208888",
product_id="product-1",
recharge_arg=[{"templateName": "斗鱼账号", "templateVal": "10001"}],
ext_arg={"skuid": 12},
@@ -52,7 +51,14 @@ 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"]["pay_amount"], 1.2)
self.assertEqual(kwargs["json"]["out_order_id"], "OUT202503010010208888")
self.assertEqual(kwargs["json"]["order_type"], 0)
self.assertEqual(kwargs["json"]["recharge_arg"], '[{"templateName":"斗鱼账号","templateVal":"10001"}]')
self.assertEqual(kwargs["json"]["ext_arg"], '{"skuid":12}')
self.assertNotIn("charge_account", kwargs["json"])
self.assertNotIn("customer_price", kwargs["json"])
self.assertNotIn("customer_order_no", kwargs["json"])
self.assertEqual(kwargs["json"]["sign"], self.client.sign(kwargs["json"], "POST"))
self.assertEqual(kwargs["timeout"], (8, 20))
@@ -62,29 +68,30 @@ class FishFinRechargeClientTests(unittest.TestCase):
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",
buy_num=1, pay_amount="1.0",
out_order_id="merchant-001", product_id="111570",
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
)
body = self.session.post.call_args.kwargs["json"]
self.assertEqual(body["customer_price"], 1)
self.assertEqual(body["pay_amount"], 1)
self.assertNotIn("notify_url", body)
self.assertNotIn("recharge_arg", body)
self.assertEqual(body["recharge_arg"], '[{"templateName":"斗鱼UID","templateVal":"10001"}]')
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()
with self.assertRaisesRegex(ValueError, "out_order_id"):
self.client.query_order("")
response = Mock()
response.json.return_value = {"code": 200, "order_status": 2}
self.session.get.return_value = response
self.client.query_order(customer_order_no="merchant-001")
self.client.query_order("OUT202503010010208888")
kwargs = self.session.get.call_args.kwargs
self.assertEqual(self.session.get.call_args.args[0], "https://supplier.example/adapter-apiaccess/open/api/queryOrderV2")
self.assertEqual(kwargs["params"]["customer_order_no"], "merchant-001")
self.assertEqual(kwargs["params"]["out_order_id"], "OUT202503010010208888")
self.assertEqual(kwargs["params"]["sign"], self.client.sign(kwargs["params"], "GET"))
def test_missing_configuration_is_rejected_before_request(self):
@@ -100,15 +107,15 @@ class FishFinRechargeClientTests(unittest.TestCase):
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",
buy_num=1, pay_amount="1",
out_order_id="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)
self.assertEqual(events[0]["params"]["pay_amount"], 1)
def test_debug_trace_includes_complete_request_and_response_bodies(self):
response = Mock()
@@ -125,13 +132,13 @@ class FishFinRechargeClientTests(unittest.TestCase):
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",
buy_num=1, pay_amount="1",
out_order_id="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.assertEqual(events[0]["json_body"]["pay_amount"], 1)
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")