ApiLogic.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.cnt = tonumber(oJsonInput.cnt)
  34. oJsonInput.money = tonumber(oJsonInput.money)
  35. ret.account = oJsonInput.account
  36. ret.order = oJsonInput.order
  37. ret.type = oJsonInput.type
  38. ret.id = oJsonInput.id
  39. ret.cnt = oJsonInput.cnt
  40. ret.money = oJsonInput.money
  41. ret.region = oJsonInput.region
  42. ret.err = ""
  43. if not (ret.account and ret.order and ret.type and ret.id and ret.cnt and ret.region) then
  44. ret.result = 6
  45. ret.err = "param empty"
  46. return
  47. end
  48. if DELIVER_CODE_HIS[oJsonInput.order] then
  49. ret.result = 4
  50. ret.err = "order double"
  51. return
  52. end
  53. DELIVER_CODE_HIS[oJsonInput.order] = os.time()
  54. local human = ObjHuman.onlineAccount[ret.account]
  55. local online = true
  56. if not human then
  57. local humanDb = RoleDBLogic.loadRole(ret.account)
  58. human = {}
  59. human.db = humanDb
  60. online = false
  61. if not humanDb then
  62. ret.result = 2
  63. ret.err = "account not exist"
  64. return
  65. end
  66. end
  67. if not BuyLogic.checkBuy(human,ret) then
  68. return
  69. end
  70. oJsonInput.cmd = "deliver"
  71. if online and human.db.middleFlag == nil then
  72. if Config.IS_DEBUG then
  73. BuyLogic.buy(human,ret.id)
  74. else
  75. local pcallRet, pcallErr = pcall(BuyLogic.buy, human,ret.id)
  76. if not pcallRet then
  77. Log.write(Log.LOGID_ERR_PCALL, "BuyLogic.buy err=" .. human.db.account, pcallErr)
  78. ret.result = -1
  79. ret.err = pcallErr
  80. return Broadcast.sendErr(human, "BuyLogic.buy "..Lang.PCALL_ERR)
  81. end
  82. end
  83. else
  84. human.db.buyOrder = human.db.buyOrder or {}
  85. human.db.buyOrder[#human.db.buyOrder + 1] = Util.copyTable(oJsonInput)
  86. end
  87. RoleDBLogic.saveRole(human.db)
  88. ret.result = 1
  89. 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)
  90. end
  91. --[[
  92. 1:充值成功
  93. 2:用户不存在
  94. 3:验证错误
  95. 4:订单号已存在
  96. 5:充值元宝数量有错
  97. 6:参数不全
  98. 7:请求过时
  99. 8:参数错误
  100. -1:未知错误
  101. --]]
  102. CHARGE_CODE_HIS = CHARGE_CODE_HIS or {}
  103. function charge(oJsonInput,ret)
  104. oJsonInput.gold = tonumber(oJsonInput.gold)
  105. oJsonInput.money = tonumber(oJsonInput.money)
  106. oJsonInput.buyID = tonumber(oJsonInput.buyID)
  107. ret.account = oJsonInput.account
  108. ret.order = oJsonInput.order
  109. ret.gold = oJsonInput.gold
  110. ret.money = oJsonInput.money
  111. ret.region = oJsonInput.region
  112. ret.appid = oJsonInput.appid or ""
  113. ret.err = ""
  114. if not (ret.account and ret.order and ret.gold and ret.money and ret.region) then
  115. ret.result = 6
  116. ret.err = "param empty"
  117. return
  118. end
  119. if CHARGE_CODE_HIS[oJsonInput.order] then
  120. ret.result = 4
  121. ret.err = "order double"
  122. return
  123. end
  124. local human = ObjHuman.onlineAccount[ret.account]
  125. local online = true
  126. if not human then
  127. local humanDb = RoleDBLogic.loadRole(oJsonInput.account)
  128. human = {}
  129. human.db = humanDb
  130. online = false
  131. if not humanDb then
  132. ret.result = 2
  133. ret.err = "account not exist"
  134. return
  135. end
  136. end
  137. local topupMoney = 0
  138. local buyConf = oJsonInput.buyID and BuyExcel[oJsonInput.buyID]
  139. if oJsonInput.buyID == 3 then
  140. if human.db.fundFlag ~= nil then
  141. ret.result = 5
  142. ret.err = "funds cannot be bought multiple times"
  143. return
  144. end
  145. end
  146. if not BuyLogic.isArgOK(human,oJsonInput.buyID,ret.region,ret.money) then
  147. ret.result = 5
  148. ret.err = "money not fix config"
  149. return
  150. end
  151. CHARGE_CODE_HIS[oJsonInput.order] = os.time()
  152. oJsonInput.cmd = "charge"
  153. local price = 0
  154. if ret.money ~= 0 then
  155. price = ret.money
  156. else
  157. price = buyConf["CN"][1]
  158. end
  159. oJsonInput.topupMoney = price
  160. human.db.buyHis = human.db.buyHis or {}
  161. human.db.buyHis[#human.db.buyHis + 1] = {buyID = oJsonInput.buyID or 0,money = ret.money,gold = ret.gold,upTime = os.time()}
  162. if online and human.db.middleFlag == nil then
  163. if Config.IS_DEBUG then
  164. BuyLogic.charge(human,oJsonInput)
  165. else
  166. local pcallRet, pcallErr = pcall(BuyLogic.charge, human,oJsonInput)
  167. if not pcallRet then
  168. Log.write(Log.LOGID_ERR_PCALL, "BuyLogic.charge err=" .. human.db.account, pcallErr)
  169. ret.result = -1
  170. ret.err = pcallErr
  171. return Broadcast.sendErr(human, "BuyLogic.charge "..Lang.PCALL_ERR)
  172. end
  173. end
  174. else
  175. human.db.buyOrder = human.db.buyOrder or {}
  176. human.db.buyOrder[#human.db.buyOrder + 1] = Util.copyTable(oJsonInput)
  177. end
  178. RoleDBLogic.saveRole(human.db)
  179. ret.result = 1
  180. if ret.order ~= 'transaction_id1' then
  181. 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)
  182. end
  183. local orderExt = {['orderID'] = oJsonInput.order, ['amount'] = oJsonInput.money}
  184. if oJsonInput.ext then
  185. local extData = Json.Decode(oJsonInput.ext)
  186. orderExt.OrderNO = extData.OrderNO
  187. orderExt.GoodsCode = extData.GoodsCode
  188. end
  189. orderExt.PayPurchaseType = buyConf.cmd == "topup" and 0 or 3
  190. orderExt.ItemCode = oJsonInput.buyID
  191. orderExt.ItemName = buyConf.name
  192. orderExt.Amount = 1
  193. orderExt.CurrencyCode = "KRW"
  194. orderExt.TotalPrice = oJsonInput.money
  195. orderExt.PayToolCode = 1
  196. orderExt.ReceiveMemberID = oJsonInput.account
  197. orderExt.Balance = human.db.zuanshi
  198. orderExt.PayResultType = 1
  199. orderExt.PayResultMessage = "success"
  200. ReportManager.charge(human, orderExt)
  201. end