--玩家充值处理 local skynet = require "skynet" local logger = require "logger" local asset = require "model.asset" local schema = require "model.schema" local common_fun = require "model.common_fun" local reward = require "model.reward" local httpc = require "http.httpc" local cjson = require "cjson" local stringify = require "stringify" local log = require "model.log" local shengtian local logger_info = logger.info local function deal_num(num) local num1 = tonumber(string.format("%.02f", num)) local num2 = tonumber(string.format("%.1f", num)) local num3 = math.floor(num) if math.abs(num1 - num2) >= 0.01 then return num1 end if math.abs(num2 - num3) >= 0.1 then return num2 end return num3 end local skynet_call = skynet.call local REQUEST = {} local CMD = {} local recharge = {} local rechargeser local _M = schema.new('rechargedata', { }) function recharge.list_request_interests() return REQUEST end function recharge.list_command_interests() return CMD end function recharge.parse(character) local d = _M.load(character) end -- TODO: 充值成功推送给客户端 local function recharge_succeed(character,orderid, info) logger.trace(" ######### orderid %s", orderid) local ok, info = pcall(function() character.send("recharge_succeed", { orderid = orderid, }) -- log log.recharge(character, info.pay_price,info) -- 数据统计 end) if not ok then logger.trace(" ## 通知客户端充值成功呢异常 %s", info) end end function recharge.monitor(character) --玩家充值时间 character.monitor("recharge", function(_, cfid, orderid, moneynum, appname, giftid) logger_info("玩家充值了礼包:%d, cfid:%s, orderid:%s",giftid, cfid, orderid) local conf = asset.GiftConfig_proto[giftid] if not conf or conf.spend ~= cfid then logger.error("recharge err, orderid:%s, giftid:%d", orderid, giftid) end character.dispatch("recharge.money", cfid, orderid, moneynum, giftid, conf.type, conf.sort) skynet_call(rechargeser,"lua", "orderid_refresh", character.uid, orderid) -- 充值金额大于零, 修改玩家充值总金额数量 -- 玩家金额统计 if moneynum > 0 then character.set_money(character.money + moneynum) end log.recharge(character, moneynum , { pay_platform = character.platform, pay_orderID = orderid, pay_id = cfid, pay_price = moneynum, pay_level = character.level, pay_time = os.date("%Y-%m-%d %H:%M:%S"), id = character.uid, }) character.send("recharge_success", { id = orderid, }) -- local MONEY_TYPE = assert(asset.variable_proto[1851].data1) -- local MONEY_TYPE = 1 -- recharge_succeed(character,orderid,{ --PK3-1085 -- pay_platform = character.platform, -- pay_orderID = orderid, -- pay_id = cfid, -- pay_type = MONEY_TYPE, -- pay_price = moneynum*copies, --多份购买 -- pay_configtype = MONEY_TYPE, -- pay_configprice = moneynum*copies, --多份购买 -- pay_level = character.level, -- pay_time = os.date("%Y-%m-%d %H:%M:%S",now), -- id = character.uid, -- }) end) character.monitor("recharge.money",function(_, cfid, orderid, moneynum, giftid, modleid) -- 钻石商店自己发奖 if modleid == MODULE_ID_DIA_SHOP then return end local conf = asset.GiftConfig_proto[giftid] local award_list if conf.award and conf.award > 0 then local errno = 0 errno, award_list = common_fun.get_award(conf.award, "NewawardConfig_proto") if errno ~= 0 then logger.warn("计费点奖励异常, errno:%d, giftid:%d, awardid:%d", errno, giftid, conf.award) return end end award_list = award_list or {} for i = 1, #conf.library, 2 do local item_id = conf.library[i] local item_num = conf.library[i+1] if item_id and item_num then table.insert(award_list, {id = item_id, num = item_num}) end end -- 发奖励 local desc = { module="recharge", brief="充值礼包", context= "充值礼包:"..giftid, mailgen={subject=2, body=""}, notify={ flags="recharge_money" }, detail=award_list, must_mail = true } reward(character, desc) end) end -- function recharge.create_order(character, args) -- -- 检查具体的档位 等等 -- -- 暂时无需要, 不开发 -- local cfid = args.cfid -- local act_id = args.act_id or 0 -- local act_type = args.act_type or 0 -- local goods_id = args.goods_id or 0 -- local login_args = character.login_args -- local platform = login_args.platform -- local appname = login_args.appname -- local conf = util.recharge_conf(cfid) -- local errno = check_rechage_limit(character, conf[MONEY_STR]) -- if errno ~= 0 then -- return 0, {reason = errno} -- end -- local EDITOR_MODEL = true -- if (character.channel == 'editor' and LAUNCH.private_gm) or LAUNCH.debug_model then -- skynet.fork(function() -- skynet.sleep(0) -- logger.trace("启动editor模式充值") -- local conf = util.recharge_conf(cfid) -- local temp = { -- orderid = "editor_orderid", -- cfid = cfid, -- act_id = act_id, -- act_type = act_type, -- goods_id = goods_id, -- moneynum = conf[MONEY_STR] -- } -- character.dispatch("recharge.money", temp) -- end) -- return 0, {reason = 0} -- local ok,herrno,info = pcall(httpc.post, RECHARGE_SERVICE, '/getorderid', { -- userid = character.account, -- 平台或渠道的账号id -- serverid = character.sid, -- 游戏服务器id -- uid = character.uid, -- 游戏内的账号id -- platform = platform, -- 平台id(Andorid、iOS) -- channel = character.channel, -- 渠道标识 -- product_id = cfid, -- 计费点名称(用的计费点id) -- remarks = 'remarks', -- , -- appname = appname or 'appname', -- 玩家使用的包 -- act_id = act_id or 0, -- 活动唯一id -- act_type = act_type or 0, -- 活动类型 -- goods_id = goods_id or 0, -- 商品对应配置id -- }) -- -- logger.trace("########## ok:%s, info:%s, info2:%s", ok, herrno or 'nil', info or 'nil') -- if herrno ~= 200 then -- return 0, {reason = STD_ERR.RECHARGE_CREATE_HTTPE_ERR} -- http请求异常 -- end -- -- info 解码 -- local order = cjson.decode(info) -- -- logger.trace("充值系统 返回"..stringify(order)) -- if order.erron ~= 0 then -- logger.trace("[充值系统] 订单创建失败 %s", stringify(order)) -- return 0, {reason = STD_ERR.RECHARGE_CREATE_FAIL} -- 订单创建失败 -- end -- -- skynet.fork(function( ) -- -- skynet.sleep(300) -- -- -- MODULE.test_recharge(character, order.desc) -- -- end) -- return 0, {orderid = order.desc, goods_id = cfid} -- end function recharge.get_orderid(character, giftid) local conf = asset.GiftConfig_proto[giftid] if not conf then return 0 end local cfid = conf.spend if not cfid then return 0 end local orderid = "1" local recharge_conf = asset.RechargeConfig_proto[cfid] if not recharge_conf then return 0 end local moneytype = asset.DataConfig_proto[19].data1 local moneynum if moneytype == 1 then moneynum = recharge_conf.my elseif moneytype == 2 then moneynum = recharge_conf.rmb elseif moneytype == 3 then moneynum = recharge_conf.tw elseif moneytype == 4 then moneynum = recharge_conf.hk end if LAUNCH.test_recharge then character.dispatch("recharge.money", cfid, orderid, moneynum, giftid, conf.type, conf.sort) return end local login_args = character.login_args local platform = login_args.platform local appname = login_args.appname local ok,herrno,info = pcall(httpc.post, RECHARGE_SERVICE, '/getorderid', { userid = character.account, -- 平台或渠道的账号id serverid = character.sid, -- 游戏服务器id uid = character.uid, -- 游戏内的账号id platform = platform, -- 平台id(Andorid、iOS) channel = character.channel, -- 渠道标识 product_id = giftid, -- 礼包id cfid = cfid, -- 计费点id remarks = 'remarks', -- , appname = appname or 'appname', -- 玩家使用的包 act_id = conf.type, }) if herrno ~= 200 then logger.trace("[充值系统] 订单创建失败.errno %s", herrno) return 0 end -- info 解码 local order = cjson.decode(info) if order.erron ~= 0 then logger.trace("[充值系统] 订单创建失败 %s", stringify(order)) return 0 end local now = os.time() local ret = { id = order.desc, cfid = cfid, time = now } if character.channel == "shengtian" then shengtian = shengtian or require "service.loginserver.shengtian" local content = { fuse_uid = character.account, order_id = order.desc, product_id = conf.association, product_name = conf.name, price = deal_num(moneynum/MONEY_PARAM), role_id = character.uid, role_name = character.nickname, role_level = character.level, server_code = character.sid, server_name = character.sid, extension = character.login_args.extension, time = now } ret.sign = shengtian.recharge_sign(content) ret.cfid = giftid end logger.trace("[充值系统] 订单创建成功"..stringify(order)) character.send("create_order_nty", ret) return 0 end function recharge.launch(character) rechargeser = rechargeser or skynet.localname(".recharge") end function recharge.ready(character) end function recharge.saybye(character) end local function get_accesstoken(character) local host = "https://accounts.google.com" local url = "/o/oauth2/token" local grant_type = "refresh_token" local refresh_token = "1//0e3sYXF6c7hfMCgYIARAAGA4SNwF-L9IrVKAk6l2BMnTqOTqTxZ6_6Sy7y5LHfKUYqXQFvHuOu8WUuTq31l1kGTeLvylvo-hg8pc" local client_id = "1053688457546-djajb15ii7v0q6pjsh7361fpsh97353g.apps.googleusercontent.com" local client_secret = "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" local ok,herrno,info = pcall(httpc.post, host, url, { grant_type = grant_type, refresh_token = refresh_token, client_id = client_id, client_secret = client_secret, }) logger.trace("get_accesstoken ok:%s, herrno:%s, info:%s", ok, herrno or 'nil', info or 'nil') if herrno ~= 200 then return nil end -- info 解码 local order = cjson.decode(info) logger.trace("get_accesstoken 返回"..stringify(order)) return order.access_token end function REQUEST.recharge_result(character, args) local access_token = get_accesstoken(character) if not access_token then return {errno = 1} -- 系统异常 end local str = "/androidpublisher/v3/applications/%s/purchases/products/%s/tokens/%s?access_token=%s" local packageName = "com.blackart.b22" local productId = args.cfg_id local token = args.purchase_token local url = "https://androidpublisher.googleapis.com" local from = string.format(str, packageName, productId, token, access_token) local ok,herrno,inapp_purchase_data = pcall(httpc.get, url, from) logger.trace("########## ok:%s, herrno:%s, inapp_purchase_data:%s", ok, herrno or 'nil', inapp_purchase_data or 'nil') if herrno ~= 200 then return {errno = 2} -- 未找到订单 end -- info 解码 local order = cjson.decode(inapp_purchase_data) logger.trace("recharge_result 返回"..stringify(order)) if order.purchaseState ~= 0 then return {errno = 3} -- 未购买 end if not order.orderId then return {errno = 4} -- 未找到订单id end local ok,herrno,info = pcall(httpc.post, RECHARGE_SERVICE, '/googleplay', { orderid = args.order_id, -- 订单id userid = character.account, -- 平台或渠道的账号id serverid = character.sid, -- 游戏服务器id uid = character.uid, -- 游戏内的账号id channel = character.channel, -- 渠道标识 cfid = args.cfg_id, -- 计费点名称(用的计费点id) channel_order_id = order.orderId -- 渠道订单id }) logger.trace("googleplay ok:%s, herrno:%s, info:%s", ok, herrno or 'nil', info or 'nil') if herrno ~= 200 then return {errno = 2} -- 未找到订单 end return {errno = 0} end return recharge