pay.lua 3.3 KB

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