ApiLogic.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. local LuaMongo = _G.lua_mongo
  2. local Config = require("Config")
  3. local Log = require("common.Log")
  4. local Util = require("common.Util")
  5. local Log = require("common.Log")
  6. local ObjHuman = require("core.ObjHuman")
  7. local BuyLogic = require("topup.BuyLogic")
  8. local RoleDBLogic = require("role.RoleDBLogic")
  9. local ReportManager = require("platform.ReportManager")
  10. local Lang = require("common.Lang")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local BuyExcel = require("excel.buy").buy
  13. local Msg = require("core.Msg")
  14. --[[
  15. result:
  16. 1:充值成功
  17. 2:用户不存在
  18. 3:验证错误
  19. 4:订单号已存在
  20. 5:参数有错
  21. 6:参数不全
  22. 7:请求过时
  23. 8:其他原因
  24. --]]
  25. DAY_PAY_NONAGE_1 = 50
  26. DAY_PAY_NONAGE_2 = 100
  27. MONTH_PAY_NONAGE_1 = 200
  28. MONTH_PAY_NONAGE_2 = 400
  29. DELIVER_CODE_HIS = DELIVER_CODE_HIS or {}
  30. function deliver(oJsonInput,ret)
  31. --oJsonInput.type = tonumber(oJsonInput.type)
  32. oJsonInput.id = tonumber(oJsonInput.id)
  33. oJsonInput.buyID = oJsonInput.buyID or oJsonInput.id
  34. oJsonInput.cnt = tonumber(oJsonInput.cnt)
  35. oJsonInput.money = tonumber(oJsonInput.money)
  36. oJsonInput.price = oJsonInput.money
  37. ret.account = oJsonInput.account
  38. ret.serverTag = oJsonInput.serverTag
  39. ret.order = oJsonInput.order
  40. --ret.type = oJsonInput.type
  41. ret.id = oJsonInput.id
  42. ret.buyID = ret.buyID or ret.id
  43. ret.cnt = oJsonInput.cnt
  44. ret.money = oJsonInput.money
  45. ret.price = oJsonInput.price
  46. --ret.region = oJsonInput.region
  47. ret.channelID = oJsonInput.channelID
  48. ret.err = ""
  49. if not (ret.account and ret.order and ret.id and ret.cnt and ret.serverTag and oJsonInput.channelID) then -- and ret.type and ret.region
  50. ret.result = 6
  51. ret.err = "param empty"
  52. return ret
  53. end
  54. if DELIVER_CODE_HIS[oJsonInput.order] then
  55. ret.result = 4
  56. ret.err = "order double"
  57. return ret
  58. end
  59. DELIVER_CODE_HIS[oJsonInput.order] = os.time()
  60. -- local human = ObjHuman.onlineAccount[ret.account]
  61. local uTag = RoleDBLogic.Generateuuid(oJsonInput.channelID, ret.account, ret.serverTag)
  62. local human = ObjHuman.onlineNewUniqueTag[uTag]
  63. local online = true
  64. if not human then
  65. local humanDb = RoleDBLogic.loadRole(uTag)
  66. human = {}
  67. human.db = humanDb
  68. online = false
  69. if not humanDb then
  70. ret.result = 2
  71. ret.err = "account not exist"
  72. return ret
  73. end
  74. end
  75. if not BuyLogic.checkBuy(human,ret) then
  76. ret.result = 8
  77. ret.err = "check buy fail"
  78. return ret
  79. end
  80. oJsonInput.cmd = "deliver"
  81. if online and human.db.middleFlag == nil then
  82. if Config.IS_DEBUG then
  83. BuyLogic.buy(human,ret)
  84. else
  85. local pcallRet, pcallErr = pcall(BuyLogic.buy, human,ret)
  86. if not pcallRet then
  87. --Log.write(Log.LOGID_ERR_PCALL, "BuyLogic.buy err=" .. human.db.account, pcallErr)
  88. Log.write(Log.LOGID_ERR_PCALL, "BuyLogic.buy err=" .. human.db.newUniqueTag, pcallErr)
  89. ret.result = -1
  90. ret.err = pcallErr
  91. return Broadcast.sendErr(human, "BuyLogic.buy "..Lang.PCALL_ERR)
  92. end
  93. end
  94. else
  95. human.db.buyOrder = human.db.buyOrder or {}
  96. human.db.buyOrder[#human.db.buyOrder + 1] = Util.copyTable(oJsonInput)
  97. end
  98. RoleDBLogic.saveRole(human.db)
  99. ret.result = 1
  100. --Log.write(Log.LOGID_OSS_DELIVER, human.db._id, human.db.account, human.db.name, human.db.lv,oJsonInput.order,oJsonInput.money,oJsonInput.type,oJsonInput.id,oJsonInput.cnt,online and 1 or 0,oJsonInput.region)
  101. Log.write(Log.LOGID_OSS_DELIVER, human.db._id, human.db.newUniqueTag, human.db.name, human.db.lv,oJsonInput.order,oJsonInput.money,oJsonInput.type,oJsonInput.id,oJsonInput.cnt,online and 1 or 0,oJsonInput.region)
  102. return ret
  103. end
  104. --[[
  105. 1:充值成功
  106. 2:用户不存在
  107. 3:验证错误
  108. 4:订单号已存在
  109. 5:充值元宝数量有错
  110. 6:参数不全
  111. 7:请求过时
  112. 8:参数错误
  113. -1:未知错误
  114. --]]
  115. CHARGE_CODE_HIS = CHARGE_CODE_HIS or {}
  116. --如果后续用到该接口时,需要参照deliver修改用到account的地方
  117. function charge(oJsonInput,ret)
  118. oJsonInput.gold = tonumber(oJsonInput.gold)
  119. oJsonInput.money = tonumber(oJsonInput.money)
  120. oJsonInput.buyID = tonumber(oJsonInput.buyID)
  121. ret.account = oJsonInput.account
  122. ret.order = oJsonInput.order
  123. ret.gold = oJsonInput.gold
  124. ret.money = oJsonInput.money
  125. ret.region = oJsonInput.region
  126. ret.appid = oJsonInput.appid or ""
  127. ret.err = ""
  128. if not (ret.account and ret.order and ret.gold and ret.money and ret.region) then
  129. ret.result = 6
  130. ret.err = "param empty"
  131. return
  132. end
  133. if CHARGE_CODE_HIS[oJsonInput.order] then
  134. ret.result = 4
  135. ret.err = "order double"
  136. return
  137. end
  138. local human = ObjHuman.onlineAccount[ret.account]
  139. local online = true
  140. if not human then
  141. local humanDb = RoleDBLogic.loadRole(oJsonInput.account)
  142. human = {}
  143. human.db = humanDb
  144. online = false
  145. if not humanDb then
  146. ret.result = 2
  147. ret.err = "account not exist"
  148. return
  149. end
  150. end
  151. local topupMoney = 0
  152. local buyConf = oJsonInput.buyID and BuyExcel[oJsonInput.buyID]
  153. if oJsonInput.buyID == 3 then
  154. if human.db.fundFlag ~= nil then
  155. ret.result = 5
  156. ret.err = "funds cannot be bought multiple times"
  157. return
  158. end
  159. end
  160. if not BuyLogic.isArgOK(human,oJsonInput.buyID,ret.region,ret.money) then
  161. ret.result = 5
  162. ret.err = "money not fix config"
  163. return
  164. end
  165. CHARGE_CODE_HIS[oJsonInput.order] = os.time()
  166. oJsonInput.cmd = "charge"
  167. local price = 0
  168. if ret.money ~= 0 then
  169. price = ret.money
  170. else
  171. price = buyConf["CN"][1]
  172. end
  173. oJsonInput.topupMoney = price
  174. human.db.buyHis = human.db.buyHis or {}
  175. human.db.buyHis[#human.db.buyHis + 1] = {buyID = oJsonInput.buyID or 0,money = ret.money,gold = ret.gold,upTime = os.time()}
  176. if online and human.db.middleFlag == nil then
  177. if Config.IS_DEBUG then
  178. BuyLogic.charge(human,oJsonInput)
  179. else
  180. local pcallRet, pcallErr = pcall(BuyLogic.charge, human,oJsonInput)
  181. if not pcallRet then
  182. Log.write(Log.LOGID_ERR_PCALL, "BuyLogic.charge err=" .. human.db.account, pcallErr)
  183. ret.result = -1
  184. ret.err = pcallErr
  185. return Broadcast.sendErr(human, "BuyLogic.charge "..Lang.PCALL_ERR)
  186. end
  187. end
  188. else
  189. human.db.buyOrder = human.db.buyOrder or {}
  190. human.db.buyOrder[#human.db.buyOrder + 1] = Util.copyTable(oJsonInput)
  191. end
  192. RoleDBLogic.saveRole(human.db)
  193. ret.result = 1
  194. if ret.order ~= 'transaction_id1' then
  195. Log.write(Log.LOGID_OSS_PAY, human.db._id, human.db.account, human.db.name, human.db.lv,ret.order,oJsonInput.buyID or 0,oJsonInput.amt or ret.money,ret.gold,online and 1 or 0,ret.region)
  196. end
  197. local orderExt = {['orderID'] = oJsonInput.order, ['amount'] = oJsonInput.money}
  198. if oJsonInput.ext then
  199. local extData = Json.Decode(oJsonInput.ext)
  200. orderExt.OrderNO = extData.OrderNO
  201. orderExt.GoodsCode = extData.GoodsCode
  202. end
  203. orderExt.PayPurchaseType = buyConf.cmd == "topup" and 0 or 3
  204. orderExt.ItemCode = oJsonInput.buyID
  205. orderExt.ItemName = buyConf.name
  206. orderExt.Amount = 1
  207. orderExt.CurrencyCode = "KRW"
  208. orderExt.TotalPrice = oJsonInput.money
  209. orderExt.PayToolCode = 1
  210. orderExt.ReceiveMemberID = oJsonInput.account
  211. orderExt.Balance = human.db.zuanshi
  212. orderExt.PayResultType = 1
  213. orderExt.PayResultMessage = "success"
  214. ReportManager.charge(human, orderExt)
  215. end