api_test.py 5.1 KB

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