QQApi.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. local Msg = require("core.Msg")
  2. local ObjHuman = require("core.ObjHuman")
  3. local Define = require("platform.Define")
  4. local Broadcast = require("broadcast.Broadcast")
  5. local Lang = require("common.Lang")
  6. Json = Json or require("common.Json")
  7. local BuyLogic = require("topup.BuyLogic")
  8. local ApiLogic = require("platform.ApiLogic")
  9. local BuyExcel = require("excel.buy").buy
  10. OP_KT = "kt"
  11. OP_BUY = "buy"
  12. local TOKEN_DATA = TOKEN_DATA or {}
  13. TOKEN_DATA[OP_KT] = TOKEN_DATA[OP_KT] or {}
  14. TOKEN_DATA[OP_BUY] = TOKEN_DATA[OP_BUY] or {}
  15. local TIME_OUT = 2 * 60 * 60
  16. local s2aParam = {}
  17. local function addToken(op,token,account)
  18. TOKEN_DATA[op][token] = {account,os.time()+TIME_OUT}
  19. end
  20. local function rmToken(op,token)
  21. TOKEN_DATA[op][token] = nil
  22. end
  23. function getKTToken(human,type,actType)
  24. local pf_info = human.pf_info
  25. if (pf_info == nil) then
  26. return
  27. end
  28. for k in pairs(s2aParam) do
  29. s2aParam[k] = nil
  30. end
  31. -- 构造参数
  32. local callback_general_param = {}
  33. callback_general_param.account = human.db.account
  34. callback_general_param.name = human.db.name
  35. callback_general_param.type = type
  36. s2aParam.callback_general_param = callback_general_param
  37. local api_param = Define.MakeTencentCommonParam(pf_info)
  38. api_param.tokentype = 1
  39. if actType == 1 then
  40. api_param.discountid = Define.YELLOW_DISCOUNT_ID
  41. elseif actType == 2 then
  42. api_param.discountid = Define.BLUE_DISCOUNT_ID
  43. else
  44. return
  45. end
  46. s2aParam.callback_general_param.discountid = api_param.discountid
  47. api_param.ts = os.time()
  48. api_param.version = "v3"
  49. api_param.zoneid = pf_info.zoneid
  50. s2aParam.api_param = api_param
  51. s2aParam.api_url = "/v3/pay/get_token"
  52. s2aParam.api_cbMethod = "OnTokenReturnForOpenVip"
  53. _G.thread_http.send(Define.ADMIN_QQAPI_URL,Json.Encode(s2aParam))
  54. end
  55. --开通黄钻蓝钻的token
  56. function OnTokenReturnForKT(result_json)
  57. local respond = {}
  58. respond.ret = 0
  59. respond.msg = "fail"
  60. local tencent_ret = result_json.tencent_ret
  61. local general_param = result_json.callback_general_param
  62. local token = tencent_ret.token
  63. if not token then
  64. respond.msg = "fail:token should not be null"
  65. return Json.Encode(respond)
  66. end
  67. local account = general_param.account
  68. if (account == nil) then
  69. respond.msg = "fail:Callback param's name should not be null"
  70. return Json.Encode(respond)
  71. end
  72. local human = ObjHuman.onlineAccount[account]
  73. if (human == nil) then
  74. respond.msg = "fail:Human offlined"
  75. return Json.Encode(respond)
  76. end
  77. if (tencent_ret.ret ~= 0) then
  78. if tencent_ret.ret == 1002 then
  79. Broadcast.sendErr(human, Lang.QQAPI_ERR_TIMEOUT)
  80. end
  81. respond.msg = "fail:OPENKEY EXPIRE"
  82. return Json.Encode(respond)
  83. end
  84. local msgRet = Msg.gc.GC_KT_TOKEN
  85. msgRet.token = tencent_ret.token
  86. msgRet.discountid = general_param.discountid
  87. msgRet.type = general_param.type
  88. Msg.send(msgRet, human.fd)
  89. addToken(OP_KT,token,account)
  90. respond.ret = 1
  91. respond.msg = "success"
  92. return Json.Encode(respond)
  93. end
  94. function onDeliverReturnForKT(result_json)
  95. local respond = {}
  96. respond.ret = 0
  97. local token = result_json.token
  98. if token == nil then
  99. respond.msg = "token is nil"
  100. return Json.Encode(respond)
  101. end
  102. local account = TOKEN_DATA[OP_KT][token][1]
  103. if account == nil or account == "" then
  104. respond.msg = "account is nil"
  105. return Json.Encode(respond)
  106. end
  107. local human = ObjHuman.onlineAccount[account]
  108. if (human == nil) then
  109. respond.msg = "fail:Human offlined"
  110. return Json.Encode(respond)
  111. end
  112. local itemInfo = Split(result_json.payitem, "*")
  113. local item_id
  114. local price
  115. local item_cnt
  116. local item_name
  117. if itemInfo then
  118. item_id = tonumber(itemInfo[1])
  119. price = tonumber(itemInfo[2])
  120. item_cnt = tonumber(itemInfo[3])
  121. end
  122. local discountid = result_json.discountid
  123. if discountid == Define.BLUE_DISCOUNT_ID then
  124. local grid = Grid.create(item_id,item_cnt)
  125. BagLogic.addItemByGrid(human,grid,"kt_blue")
  126. end
  127. rmToken(OP_KT,token)
  128. --加道具 todo
  129. respond.ret = 1
  130. respond.msg = "OK"
  131. return Json.Encode(respond)
  132. end
  133. function getBuyToken(human, buyID, appmode)
  134. local buyConfig = BuyExcel[buyID]
  135. if not buyConfig then
  136. return
  137. end
  138. local name = buyConfig.name
  139. local desc = buyConfig.desc == "" and name or buyConfig.desc
  140. local price = buyConfig[human.region]
  141. local cnt = 1
  142. local icon = buyConfig.icon
  143. local pf_info = human.pf_info
  144. if (pf_info == nil) then
  145. assert(nil)
  146. return
  147. end
  148. for k in pairs(s2aParam) do
  149. s2aParam[k] = nil
  150. end
  151. -- 构造参数
  152. local callback_general_param = {}
  153. callback_general_param.account = human.db.account
  154. callback_general_param.name = human.db.name
  155. s2aParam.callback_general_param = callback_general_param
  156. local api_param = Define.MakeTencentCommonParam(pf_info)
  157. api_param.ts = os.time()
  158. api_param.payitem = string.format("%s*%d*%d", human.db.account .. "." .. buyID, price * 10, cnt)
  159. api_param.goodsmeta = string.format("%s*%s", name, desc)
  160. api_param.goodsurl = Define.QQ_GOOD_URL --.. icon .. ".png"
  161. api_param.zoneid = pf_info.zoneid
  162. api_param.appmode = appmode or 2
  163. s2aParam.api_url = "/v3/pay/buy_goods"
  164. s2aParam.api_cbMethod = "OnTokenReturnForBuy"
  165. --end
  166. s2aParam.api_param = api_param
  167. _G.thread_http.send(Define.ADMIN_QQAPI_URL,Json.Encode(s2aParam))
  168. end
  169. function OnTokenReturnForBuy(result_json)
  170. local respond = {}
  171. respond.ret = 0
  172. local tencent_ret = result_json.tencent_ret
  173. local general_param = result_json.callback_general_param
  174. local account = general_param.account
  175. if (account == nil) then
  176. respond.msg = "fail:Callback param's account should not be null"
  177. return Json.Encode(respond)
  178. end
  179. local human = ObjHuman.onlineAccount[account]
  180. if (human == nil) then
  181. respond.msg = "fail:Human offlined"
  182. return Json.Encode(respond)
  183. end
  184. if (tencent_ret.ret ~= 0) then
  185. if tencent_ret.ret == 1002 then
  186. Broadcast.sendErr(human, Lang.QQAPI_ERR_TIMEOUT)
  187. end
  188. respond.msg = "fail:OPENKEY expire"
  189. return Json.Encode(respond)
  190. end
  191. local token = tencent_ret.token
  192. local url_params = tencent_ret.url_params
  193. local msgRet = Msg.gc.GC_BUY_TOKEN
  194. msgRet.token = token
  195. msgRet.url = tencent_ret.url_params
  196. Msg.send(msgRet, human.fd)
  197. addToken(OP_BUY,token,account)
  198. respond.ret = 1
  199. respond.msg = "ok"
  200. return Json.Encode(respond)
  201. end
  202. -- 腾讯发货回调返回码
  203. local kTencentDeliverRetSuccess = 0
  204. local kTencentDeliverRetTokenNotExists = 3
  205. local kTencentDeliverRetParamError = 4
  206. local kTencentDeliverRetUserOffline = 5
  207. local kTencentDeliverRetHandlerFailed = 6
  208. BUY_CONFIRM_LIST = BUY_CONFIRM_LIST or {}
  209. function confirmDeliver(key,human,result_json)
  210. s2aParam = {}
  211. local now = os.time()
  212. local callback_general_param = {}
  213. s2aParam.callback_general_param = callback_general_param
  214. s2aParam.api_url = "/v3/pay/confirm_delivery"
  215. s2aParam.api_cbMethod = ""
  216. local confirm_param = {}
  217. local pf_info = human.pf_info
  218. confirm_param["openid"] = pf_info.openid
  219. confirm_param["openkey"] = pf_info.openkey
  220. confirm_param["pfkey"] = pf_info.pfkey
  221. confirm_param["pf"] = pf_info.pf
  222. confirm_param["billno"] = result_json.billno
  223. confirm_param["payitem"] = result_json.payitem
  224. confirm_param["zoneid"] = result_json.zoneid
  225. confirm_param["amt"] = result_json.amt
  226. confirm_param["payamt_coins"] = result_json.payamt_coins
  227. confirm_param["token_id"] = result_json.token
  228. confirm_param["provide_errno"] = 0
  229. confirm_param["userip"] = human.db.ip
  230. confirm_param["worldid"] = pf_info.serverid
  231. confirm_param["pubacct_payamt_coins"] = result_json.pubacct_payamt_coins
  232. confirm_param["ts"] = os.time()
  233. s2aParam.api_param = confirm_param
  234. s2aParam.userip = confirm_param.userip
  235. s2aParam.worldid = confirm_param.worldid
  236. _G.thread_http.send(Define.ADMIN_QQAPI_URL,Json.Encode(s2aParam))
  237. BUY_CONFIRM_LIST[key] = nil
  238. end
  239. function onDeliverReturnForBuy(result_json)
  240. local respond = {}
  241. respond.result = 0
  242. respond.account = ""
  243. local token = result_json.token
  244. if token == nil then
  245. respond.result = kTencentDeliverRetTokenNotExists
  246. respond.msg = "token is nil"
  247. return respond
  248. end
  249. if not TOKEN_DATA[OP_BUY][token] then
  250. respond.result = 7
  251. respond.msg = "token is invaild"
  252. return respond
  253. end
  254. local account = TOKEN_DATA[OP_BUY][token][1]
  255. if account == nil or account == "" then
  256. account = result_json.accountName
  257. if account == nil or account == "" then
  258. respond.result = kTencentDeliverRetParamError
  259. respond.msg = "account is nil"
  260. return respond
  261. end
  262. end
  263. respond.account = account
  264. local human = ObjHuman.onlineAccount[account]
  265. if (human == nil) then
  266. respond.result = kTencentDeliverRetUserOffline
  267. respond.msg = "fail:Human offlined"
  268. return respond
  269. end
  270. local payitem = result_json.items or result_json.payitem
  271. local itemInfo = Define.Split(payitem, "*")
  272. local buyID
  273. local price
  274. local cnt
  275. local name
  276. if itemInfo then
  277. buyID = tonumber(itemInfo[1])
  278. price = tonumber(itemInfo[2])/10
  279. cnt = tonumber(itemInfo[3])
  280. end
  281. if not BuyLogic.isArgOK(human,buyID,human.region,price) then
  282. respond.result = kTencentDeliverRetParamError
  283. respond.msg = "fail:param err"
  284. return respond
  285. end
  286. local input = {}
  287. input.account = account
  288. input.order = "zone_"..token
  289. input.region = "CN"
  290. input.gold = 0
  291. input.money = price
  292. input.buyID = buyID
  293. if result_json.amt then
  294. local amt = tonumber(result_json.amt)
  295. if amt > 0 then
  296. input.amt = amt / 100
  297. end
  298. end
  299. local ret = {}
  300. ApiLogic.charge(input,ret)
  301. --加道具 end
  302. if ret.result ~= 1 then
  303. respond.result = kTencentDeliverRetHandlerFailed
  304. respond.msg = "ApiLogic.charge err:"..ret.result.."-"..ret.err
  305. return respond
  306. end
  307. rmToken(OP_BUY,token)
  308. BUY_CONFIRM_LIST[result_json.billno] = {human,result_json}
  309. respond.result = kTencentDeliverRetSuccess
  310. respond.msg = "OK"
  311. return respond
  312. end
  313. --[[
  314. --实名注册
  315. function realNameRegister(human, idnumber, name)
  316. local pf_info = human.pf_info
  317. if (pf_info == nil) then
  318. assert(nil)
  319. return
  320. end
  321. for k in pairs(s2aParam) do
  322. s2aParam[k] = nil
  323. end
  324. -- 构造参数
  325. local callback_general_param = {}
  326. callback_general_param.account = human.db.account
  327. s2aParam.callback_general_param = callback_general_param
  328. local api_param = {}
  329. api_param.openid = pf_info.openid
  330. api_param.openkey = pf_info.openkey
  331. api_param.idnumber = idnumber
  332. api_param.name = name
  333. s2aParam.api_url = "/v3/user/gameapi_real_name_register"
  334. s2aParam.api_cbMethod = "OnRealNameRegister"
  335. s2aParam.api_param = api_param
  336. _G.thread_http.send(Define.ADMIN_QQAPI_REAL_NAME_REGISTER,Json.Encode(s2aParam))
  337. end
  338. function OnRealNameRegister(result_json)
  339. local respond = {}
  340. respond.ret = 0
  341. respond.msg = "fail"
  342. local general_param = result_json.callback_general_param
  343. local account = general_param.account
  344. if (account == nil) then
  345. respond.msg = "fail:Callback param's name should not be null"
  346. return Json.Encode(respond)
  347. end
  348. local human = ObjHuman.onlineAccount[account]
  349. if (human == nil) then
  350. respond.msg = "fail:Human offlined"
  351. return Json.Encode(respond)
  352. end
  353. local msgRet = Msg.gc.GC_REAL_NAME_REGISTER
  354. msgRet.ret = 0
  355. msgRet.msg = "success"
  356. local tencent_ret = result_json.tencent_ret
  357. if tencent_ret.ret ~= 0 or tencent_ret.reg_result ~= 0 then
  358. msgRet.ret = tencent_ret.ret
  359. msgRet.msg = tencent_ret.msg
  360. end
  361. Msg.send(msgRet, human.fd)
  362. respond.ret = 1
  363. respond.msg = "success"
  364. return Json.Encode(respond)
  365. end--]]
  366. function onTimer()
  367. for k,v in pairs(BUY_CONFIRM_LIST) do
  368. confirmDeliver(k,v[1],v[2])
  369. return
  370. end
  371. end