-- 商城接口 local code = require "code" local util_user = require("utils.util_user") local lib_game_mysql = require("lib_game_mysql") local util_order = require("utils.util_order") local shopAdapt = require("adapt.shopAdapt") local dataBag = require("data.bag") local root = {} -- 购买商品 function root.shop_buy_goods(role, msg) local gid = msg.goodsId local conf = shopAdapt:goods_get_info(gid) if conf == nil or conf.price == nil then return code.PARAMTER_ERROR end local uid = role.uid local ok = dataBag:is_enough(uid, conf.price) if not ok then return code.RES.NOT_ENOUGH end local eventId = "shop-pay-" .. tostring(gid) dataBag:consume_items(uid, conf.price, eventId) return role.pay:dispatch_goods_items(role, gid) end -- 充值成功回调 function root.on_pay(role, msg) local uid = role.uid log.print("充值成功回调 %s, %s", uid, tostring(msg)) local order = msg.orderAction local logEvent = function(seasonId) local extraParams = table.concat( { order and order.orderNo or "", order and order.type or "", order and order.itemId or "", order and order.amount or "", order and order.status or "", seasonId or "" }, "," ) util_user:log_event(uid, "OrderError", extraParams) end local gid = msg.goodsId local conf = gid and shopAdapt:goods_get_info(gid) if is_empty(conf) then log.error("商城充值回调 gid =%s", gid) logEvent(1) return code.PARAMTER_ERROR end -- 订单先完成 util_order:update_order_status(order and order.id, 1) -- 统计 - 充值金额 local amount = math.floor(tonumber(msg.orderAction.amount) * 100) -- 增加物品 local errCode, res = role.pay:dispatch_goods_items(role, gid, amount) if code.is_not_ok(errCode) then return errCode end -- 推送客户端 util_order:on_shop_order(order) return code.OK, res end -- 1.5版本订单BUG,修复代码 local function is_order(str) if str == nil or #str == 0 then return false end for i = 1, #str do local cur_byte = string.byte(str, i) if cur_byte > 127 then return false end end return true end -- 查询订单结果 function root.shop_get_order(role, msg) if msg == nil or msg.orderId == nil then return code.PARAMTER_ERROR end local uid = role.uid local orderId = msg.orderId log.info("itf_get_order uid[%s] orderId[%s]", uid, orderId) if not is_order(orderId) then return code.SHOP.ALREADY_GET_ORDER end -- 订单是否已入库 local sql = string.format( "SELECT `id`, `gid`, `status`, `resMsg`, `pay`, `amount` FROM `mdl_order` WHERE orderNo='%s';", orderId ) local ret = lib_game_mysql:query(sql) if is_empty(ret) then return code.SHOP.NO_FOUND_ORDER end -- 修改订单状态 sql = string.format("UPDATE `mdl_order` SET `readStatus`=99 WHERE `orderNo`='%s';", orderId) lib_game_mysql:query(sql) local id = tonumber(ret[1].id) local status = tonumber(ret[1].status) local goodsId = tonumber(ret[1].gid) local resMsg = ret[1].resMsg local payName = ret[1].pay local amount = ret[1].amount local order = { status = status, goodsId = goodsId, orderId = orderId, errMsg = resMsg, payName = payName, amount = tostring(amount) } if status ~= 1 then -- 订单支付失败 return code.OK, {order = order} end local logEvent = function(seasonId) local extraParams = table.concat( { orderId, payName, goodsId, amount, status, seasonId or "" }, "," ) util_user:log_event(uid, "OrderError", extraParams) end local conf = goodsId and shopAdapt:goods_get_info(goodsId) if is_empty(conf) then log.error("商城充值回调 goodsId =%s", goodsId) logEvent(1) return code.PARAMTER_ERROR end -- TODO 订单回调是否重复 -- 订单先完成 util_order:update_order_status(id, 1) -- 增加物品 local errCode, res = role.pay:dispatch_goods_items(role, goodsId) if errCode ~= code.OK then return errCode end -- 推送客户端 util_order:on_shop_order(order) local ret = {orderInfo = util_order:pack_order_info(order)} return code.OK, ret end return root