api_test.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from gmssl import sm2
  2. import requests
  3. import time
  4. import json
  5. import hashlib
  6. import pprint
  7. import datetime
  8. pay_url = "http://127.0.0.1:8848/pay"
  9. search_url = "http://127.0.0.1:8848/search"
  10. balance_url = "http://127.0.0.1:8848/balance"
  11. # 测试用 秘钥组1 脚本加密用
  12. pk1 = "04865e672fa71ab6e37429fd731800792c1015cf4e936bd01ec0092b7da571fb63cb99cca8104b35243175d3f535784f6127af451bed43b5b929db6b864de3b207"
  13. sk1 = "b73190a4fa4f6786fa36a35c1c16e533943fb5951a40c6b9e2d06b5a19d5c19a"
  14. # 测试用 秘钥组2 脚本解密用
  15. pk2 = "04f87981e38e98c5d400daed3e3dbf0a77e622860923b37ab94622c435401604a2e23f7c2ddb656f0b7735ca1f36cc203e6ac8fe47d6bc15f3739608e81b424c04"
  16. sk2 = "6907ee4516db5e2f04eef3976551f2c8ead793648edf37b82035013527b3ee42"
  17. sinKey = "ZctLTt2tugU2ntZdVgMXssrGuENPH6V7b7ROYN88Msw="
  18. pub_head = {
  19. "appId": "CESHI",
  20. "method": "sd.tovc.recharge",
  21. "signType": "md5",
  22. "sign": "",
  23. "timestamp": "",
  24. "version": "v1.0",
  25. "requestId": str(int(time.time()*1000)),
  26. "bizContent": ""
  27. }
  28. def encrypt(data: str):
  29. en = sm2.CryptSM2(private_key="", public_key=pk1)
  30. r = en.encrypt(data.encode("utf-8"))
  31. # result = bytearray([0x4])
  32. result = bytearray()
  33. result.extend(r) # type:ignore
  34. return result.hex() # type:ignore
  35. def decrypt(data: str):
  36. en = sm2.CryptSM2(private_key=sk2, public_key="")
  37. r = en.decrypt(data.encode("utf-8"))
  38. return r.hex() # type:ignore
  39. def sign_head(head: dict):
  40. keys = list(head.keys())
  41. keys.sort()
  42. ks = []
  43. for k in keys:
  44. v = head[k].strip()
  45. if k != "sign" and v:
  46. ks.append(f"{k}={v}")
  47. ks.append(f"key={sinKey}")
  48. s = "&".join(ks)
  49. m5 = hashlib.md5(s.encode("utf-8"))
  50. sign = m5.hexdigest()
  51. head["sign"] = sign
  52. def do_post(url, data):
  53. data = json.dumps(data, ensure_ascii=False)
  54. head = pub_head.copy()
  55. now = datetime.datetime.now()
  56. head["timestamp"] = f"{now:%Y-%m-%d %H:%M:%S}"
  57. head["requestId"] = str(int(time.time()*1000))
  58. head["bizContent"] = encrypt(data)
  59. sign_head(head)
  60. pprint.pprint(data)
  61. r = requests.post(url, json=head)
  62. result = r.json()
  63. return result
  64. def test_pay():
  65. pay_body = {
  66. "gameUserId": "13856023633",
  67. "serverType": "tel",
  68. "productNo": "YDHF10",
  69. "spOrderId": str(int(time.time()*1000)),
  70. "source": "SD"
  71. }
  72. result = do_post(pay_url, pay_body)
  73. print(result)
  74. def test_search():
  75. search_body = {
  76. "spOrderId": "1679680499000"
  77. }
  78. result = do_post(search_url, search_body)
  79. print(result)
  80. def test_balance():
  81. data = {
  82. "source": "SD"
  83. }
  84. result = do_post(balance_url, data)
  85. print(result)
  86. # test_pay()
  87. # test_search()
  88. test_balance()
  89. s = '''{\"HEADER\":{\"VERSION\":\"V1.1\",\"TIMESTAMP\":\"20230325132234425\",\"SEQNO\":\"1679721754404\",\"APPID\":\"jtceshi\",\"SECERTKEY\":\"3FF6CDABE28AFE275197768302AACBB4\"},\"MSGBODY\":{\"CONTENT\":{\"SIGN\":\"1FE2F722E6B4C4CD2CF458067A3A0EF9\",\"TIMESTAMP\":\"1679721754000\"}}}'''
  90. # pprint.pprint(json.loads(s))
  91. appId = "jtceshi"
  92. appSecret = "5f1b8349873841e3afe9c6ec91c9847e"
  93. def test_account():
  94. now = datetime.datetime.now()
  95. h_time = now.strftime("%Y%m%d%H%M%S%f")
  96. seqno = '1679720124150'
  97. version = 'V1.1'
  98. h_hash = hashlib.md5(
  99. f"{h_time}{seqno}{appId}{appSecret}".encode("utf-8")).hexdigest()
  100. b_time = str(int(now.timestamp()*1000))
  101. b_hash = hashlib.md5(f"{appSecret}{b_time}".encode("utf-8")).hexdigest()
  102. data = {
  103. 'HEADER': {
  104. 'APPID': appId,
  105. 'SECERTKEY': h_hash,
  106. 'SEQNO': seqno,
  107. 'TIMESTAMP': h_time,
  108. 'VERSION': version
  109. },
  110. 'MSGBODY': {
  111. 'CONTENT': {
  112. 'SIGN': f"{b_hash}a",
  113. 'TIMESTAMP': b_time
  114. }
  115. }
  116. }
  117. data = {'HEADER': {'APPID': 'jtceshi',
  118. 'SECERTKEY': '3FF6CDABE28AFE275197768302AACBB4',
  119. 'SEQNO': '1679721754404',
  120. 'TIMESTAMP': '20230325132234425',
  121. 'VERSION': 'V1.1'},
  122. 'MSGBODY': {'CONTENT': {'SIGN': '1FE2F722E6B4C4CD2CF458067A3A0EF9',
  123. 'TIMESTAMP': '1679721754000'}}}
  124. pprint.pprint(data)
  125. url = "http://192.144.212.211:36000/gateway/flowservice/queryAccount.ws"
  126. r = requests.post(url, json=data)
  127. pprint.pprint(r.json())
  128. # sign="86D85A1C092D38AAFBB86784138A5117"
  129. # timestamp="1679721020000"
  130. # b_hash = hashlib.md5(f"{appSecret}{timestamp}".encode("utf-8")).hexdigest()
  131. # print(b_hash.upper() == sign)
  132. # test_account()
  133. def aaa():
  134. data = "{\"HEADER\":{\"VERSION\":\"V1.1\",\"TIMESTAMP\":\"20230325134408538\",\"SEQNO\":\"1679723048517\",\"APPID\":\"jtceshi\",\"SECERTKEY\":\"D1219C9DD84CEA29686EC17435234A6D\"},\"MSGBODY\":{\"CONTENT\":{\"SIGN\":\"88E0B2DEDBF62439FADD205D7A7F7E2B\",\"TIMESTAMP\":\"1679723048000\"}}}"
  135. url = "http://192.144.212.211:36000/gateway/flowservice/queryAccount.ws"
  136. r = requests.post(url, data=data, headers={"Content-Type": "application/json"})
  137. pprint.pprint(r.json())
  138. # aaa()