ApiLogic.lua 6.5 KB

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