ApiLogic.lua 6.6 KB

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