pay.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -- 支付
  2. local code = require "code"
  3. local skynet = require "skynet"
  4. local nodeMgr = require "nodeMgr"
  5. local timeUtil = require "utils.timeUtil"
  6. local serverLogUtil = require "utils.serverLogUtil"
  7. local util_order = require("utils.util_order")
  8. local sessionData = require("data.session")
  9. local userData = require("data.user")
  10. local root = {}
  11. -- 支付埋点日志
  12. local function logPay(uid, orderAction)
  13. skynet.fork(
  14. function()
  15. local playerInfo = {
  16. uid = uid,
  17. bandShareCode = userData:get_key_info(uid, "bandShareCode"),
  18. version = userData:get_key_info(uid, "version")
  19. }
  20. -- 生成支付日志
  21. serverLogUtil.logPay(playerInfo, orderAction)
  22. end
  23. )
  24. end
  25. -- 充值到账
  26. function root.recharge(msg)
  27. -- 检查回调参数
  28. local payCode, orderAction = msg.code, msg.orderAction
  29. if not orderAction then
  30. log.error("[支付回调]订单无效")
  31. return false
  32. end
  33. local uid, orderId = tonumber(orderAction.uid), orderAction.orderNo
  34. -- 检查订单有效性
  35. local orderStatus = tonumber(orderAction.status or 0)
  36. if orderStatus == 0 then
  37. return util_order:handle_invalid_order(orderAction)
  38. end
  39. -- 检查订单物品是否存在
  40. local itemId = tonumber(orderAction.itemId or 0)
  41. if itemId <= 0 then
  42. orderAction.status = 0
  43. orderAction.resMsg = "订单参数错误"
  44. log.error("订单参数错误1, itemId:%d, uid:%d", itemId, uid)
  45. return util_order:handle_invalid_order(orderAction)
  46. end
  47. -- 检查玩家的在线情况
  48. local nodeInfo = sessionData:user_get_cluster_info(uid, "game")
  49. -- 玩家在线的处理
  50. if nodeMgr.is_node_info_valid(nodeInfo) then
  51. -- 回调到逻辑服处理
  52. local pack = {
  53. orderAction = orderAction,
  54. goodsId = tonumber(orderAction.itemId)
  55. }
  56. nodeMgr.send_with_node_info(nodeInfo, "doCmd", uid, "shop", "on_pay", pack)
  57. else
  58. nodeMgr.send("global", ".paySrv", "syncOrder", uid, orderAction)
  59. end
  60. pcall(logPay, uid, orderAction)
  61. return true
  62. end
  63. -- 支付服订单回调
  64. function root.pay_notify(msg)
  65. if (not msg) or (not msg.orderAction) or (not msg.orderAction.orderNo) or (not msg.orderAction.uid) then
  66. log.error("[支付回调]订单错误, data:%s", tostring(msg or ""))
  67. return code.OK
  68. end
  69. local orderAction = msg.orderAction
  70. local orderType = string.find(orderAction.pay, "baidu")
  71. if not orderType then
  72. local isExist = util_order:is_order_exist(orderAction.orderNo)
  73. if isExist then
  74. log.error("[支付回调]订单重复:Order:%s", tostring(msg or ""))
  75. return code.OK
  76. end
  77. end
  78. if orderAction.createtime and #(orderAction.createtime) <= 0 then
  79. orderAction.createtime = timeUtil.toString(timeUtil.currentTime())
  80. end
  81. -- 订单入库
  82. util_order:add_order(orderAction)
  83. util_order:on_shop_order(orderAction)
  84. orderAction.id = util_order:get_order_id(orderAction.orderNo, orderAction.uid)
  85. log.info("payOrder orderAction[%s]", tostring(orderAction))
  86. -- 支付到账
  87. root.recharge(msg)
  88. return code.OK
  89. end
  90. return root