ApiLogic.lua 6.4 KB

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