ApiLogic.lua 6.6 KB

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