------------------------------------------------ --作者: 丁华强 --日期: 2020-01-02 --文件: PaySystem.lua --模块: PaySystem --描述: 充值系统 ------------------------------------------------ local SDKCacheData = CS.Thousandto.CoreSDK.SDKCacheData local L_PayData = require("Logic.PaySystem.PayData") local L_ServerData = require("Logic.PaySystem.ServerData") local RuntimePlatform = CS.UnityEngine.RuntimePlatform local L_UnityUtils = require("Common.CustomLib.Utility.UnityUtils"); --充值数据保存的路径 local L_JSON_SAVE_PATH = string.format("%s/PayJson.txt",PathUtils.GetWritePath("Log")) local PaySystem = { --已充值的金额 RechargeNum = 0, --真实的充值金额 RechargeRealMoney = 0, --充值数据,Id, Data PayDataIdDict = nil, --充值数据,rechargeType, List PayDataTypeDict = nil, --功能剩余时间 SubTypeTimeDict = nil, --是否设置过红点了 HasSetRedPoint = false, --当前平台类型 CurPlatform = nil, --本次充值数据的MD5值,用于和服务器进行数据验证 Md5 = "", --渠道配置的参数表 SdkPlatCfg = nil, CacheMoneySign = nil, TempPayJson = nil, } function PaySystem:Initialize(clearLoginData) if clearLoginData then self.SubTypeTimeDict = Dictionary:New() self.PayDataIdDict = Dictionary:New() self.PayDataTypeDict = Dictionary:New() --充值列表下载成功,刷新下数据 self.CurPlatform = LogicAdaptor.GetRuntimePlatform() local _fgi = GameCenter.SDKSystem.LocalFGI self.SdkPlatCfg = DataConfig.DataSdkplatform[_fgi] end end function PaySystem:UnInitialize(clearLoginData) if clearLoginData then end end --下载充值列表的数据 function PaySystem:DownLoadPayList() if File.Exists(L_JSON_SAVE_PATH) then self.Md5 = string.upper(MD5Utils.MD5String(File.ReadAllText(L_JSON_SAVE_PATH))) end local _msg = ReqMsg.MSG_Recharge.ReqCheckRechargeMd5:New() _msg.md5 = self.Md5 _msg:Send() end --配置表rechargeType字段获取到的一组数据 function PaySystem:GetPayDataByRechargeType(rechargeType) local _rechargeType = tonumber(rechargeType) local _payDataList = nil if self.PayDataTypeDict:ContainsKey(_rechargeType) then _payDataList = self.PayDataTypeDict[_rechargeType] end return _payDataList end --配置表id字段 function PaySystem:GetPayDataById(cfgId) local _cfgId = tonumber(cfgId) local _payData = nil if self.PayDataIdDict:ContainsKey(_cfgId) then _payData = self.PayDataIdDict[_cfgId] end return _payData end --配置表rechargeType, subType字段 function PaySystem:GetPayDataByType(rechargeType, subType) --1. 根据rechargeType取出对应的List数据 local _rechargeType = tonumber(rechargeType) local _typeDataList = nil if self.PayDataTypeDict:ContainsKey(_rechargeType) then _typeDataList = self.PayDataTypeDict[_rechargeType] end --2. 根据subType取出对应的List数据 local _payDataList = List:New() if _typeDataList ~= nil and #_typeDataList > 0 then for i = 1, #_typeDataList do if _typeDataList[i].ServerCfgData.RechargeSubType == subType then _payDataList:Add(_typeDataList[i]) end end end _payDataList:Sort( function(x, y) return x.ServerCfgData.Money < y.ServerCfgData.Money end ) return _payDataList end --根据配置表的ID直接发起充值 function PaySystem:PayByCfgId(cfgId) if self.PayDataIdDict:Count() > 0 and self.PayDataIdDict:ContainsKey(cfgId) then if L_UnityUtils.UNITY_EDITOR() then local _payData = self.PayDataIdDict[cfgId] --编辑器模式下直接发送充值gm命令 local req ={} req.chattype = 0 req.recRoleId = 0 req.condition = string.format("&rechargeid %d", _payData.ServerCfgData.Id) req.chatchannel = 0 req.voiceLen = 0 GameCenter.Network.Send("MSG_Chat.ChatReqCS",req) else local _msg = ReqMsg.MSG_Recharge.ReqCheckGoodsIsCanbuy:New() _msg.id = cfgId _msg.moneyType = self.SdkPlatCfg.MoneyCode _msg:Send() Debug.LogError(string.format("当前充值的配置表ID %s",cfgId)) GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_LOCK_OPEN); end else Debug.LogError(string.format("当前 %s ID找不到,不能进行充值!",cfgId)) Utils.ShowPromptByEnum("C_PAY_ID_NOT_FIND", cfgId) end end --rechargeType 3:周卡 4:月卡 5:终身卡 function PaySystem:PayByCardType(rechargeType) --这里的数据都只有一个 local _payDataList = nil local _cardData = nil if self.PayDataTypeDict:ContainsKey(rechargeType) then _payDataList = self.PayDataTypeDict[rechargeType] if _payDataList ~= nil and _payDataList:Count() > 0 then _cardData = _payDataList[1] end end if _cardData ~= nil then --给服务器请求看能不能进行充值,服务器返回可以充值才能去调用SDK走扣款 self:PayByCfgId(_cardData.ServerCfgData.CfgId) else Debug.LogError("PayByCardType _cardData is nil!") end end --根据充值id获取货币数量 function PaySystem:GetMoneyCountById(cfgId) if self.PayDataIdDict:ContainsKey(cfgId) then local _data = self.PayDataIdDict[cfgId] if _data ~= nil then return _data.ServerCfgData.Money end end return 0 end --玩家当前充值的总值 function PaySystem:ResRechargeTotalValue(msg) --玩家当前账号的总充值元宝数量 GameCenter.PaySystem.RechargeNum = msg.goldTotal GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_REFESH_PAY_DATA) end --充值了多少元 function PaySystem:ResVipRechageMoney(rechargeMoney) self.RechargeRealMoney = rechargeMoney end --返回充值数据 function PaySystem:ResRechargeData(msg) if msg ~= nil and msg.items ~= nil then local _dataList = msg.items local _typeTimeList = msg.typeTime local _serverTime = GameCenter.HeartSystem.ServerTime if _typeTimeList ~= nil then for i = 1, #_typeTimeList do local _subType = _typeTimeList[i].subtype % 10000 local _remiantime = _typeTimeList[i].remiantime if not self.SubTypeTimeDict:ContainsKey(_subType) then self.SubTypeTimeDict:Add(_subType, _remiantime) else self.SubTypeTimeDict[_subType] = _remiantime end if _subType == PaySubType.Novice then GameCenter.MainFunctionSystem:SetFunctionVisible(FunctionStartIdCode.PayNewbie, _serverTime < _remiantime) elseif _subType == PaySubType.Day then GameCenter.MainFunctionSystem:SetFunctionVisible(FunctionStartIdCode.PayDay, _serverTime < _remiantime) elseif _subType == PaySubType.Week then GameCenter.MainFunctionSystem:SetFunctionVisible(FunctionStartIdCode.PayWeek, _serverTime < _remiantime) end end end for i = 1, #_dataList do --商品ID,这里是cfg.id local _cfgId = _dataList[i].id --充值次数 local _num = _dataList[i].count --更新次数 if self.PayDataIdDict:ContainsKey(_cfgId) then self.PayDataIdDict[_cfgId]:UpdateNum(_num) end local _keys = self.PayDataTypeDict:GetKeys() if _keys ~= nil then for k = 1, #_keys do local _rechargeType = _keys[k] local _typeDataList = self.PayDataTypeDict[_rechargeType] if _typeDataList ~= nil then for j = 1, #_typeDataList do if _typeDataList[j].ServerCfgData.CfgId == _cfgId then _typeDataList[j]:UpdateNum(_num) break end end end end end end GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_REFESH_PAY_DATA) end end --客户端请求商品是否能购买返回 function PaySystem:ResCheckGoodsResult(msg) GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE); --0不能购买,1可以购买 local _result = msg.result local _payId = msg.id local _orderInfo = msg.orderInfo Debug.LogError(string.format("ResCheckGoodsResult result = %s, id = %s, _orderInfo = %s", _result, _payId, _orderInfo)) if _result == 1 then if self.PayDataIdDict:Count() > 0 and self.PayDataIdDict:ContainsKey(_payId) then local _sData = self.PayDataIdDict[_payId].ServerCfgData --这里是向SDK发起支付请求,需要传入SDK认可的支付ID local _itemId = _sData.Id local _itemName = _sData.Desc local _price = _sData.SDKPrice local _rechargeType = _sData.RechargeType local _goodsDealType = _sData.GoodsDealType local _payNotifyUrl = GameCenter.ServerListSystem.LastEnterServer.PayCallURL local _orderData = Json.decode(_orderInfo) --1才是下单成功 if _orderData.state == 1 then local _order = _orderData.data --U8Server生成的唯一订单号,需要回传SDK local _orderNo = _order.order_no --渠道SDK支付的扩展数据,回传赋值给SDK local _extension = _order.extension local _buyNum = 1 Debug.LogError(string.format("当前调用SDK支付传递的参数 _itemId = %s, _itemName = %s, _price = %s, _goodsDealType = %s, _payNotifyUrl = %s, _rechargeType = %s, _extension = %s, _cfgId = %s, _orderNo = %s", _itemId, _itemName, _price, _goodsDealType, _payNotifyUrl, _rechargeType, _extension, _payId, _orderNo)) GameCenter.SDKSystem:Pay( tostring(_itemId), tostring(_itemName), _buyNum, tonumber(_price), tostring(_goodsDealType), tostring(_payNotifyUrl), tostring(_rechargeType), _extension, tonumber(_payId), tostring(_orderNo)) else --下单失败 Utils.ShowPromptByEnum("C_PAY_ORDER_CREATE_FAIL") end end else Utils.ShowPromptByEnum("Pay_CAN_NOT_BUY_TIPS", _payId) end end --服务器下发所有充值商品 function PaySystem:ResRechargeItems(msg) local _payJson = msg.rechargeItemJson if msg.md5 ~= nil or msg.md5 ~= "" then --判断下内存中是否存过充值的字符串了,并且MD5改变过了,没改变就不去动数据 if self.TempPayJson == nil or msg.md5 ~= self.MD5 then --数据为空的情况下读取本地的数据 if _payJson == nil or _payJson == "" then if File.Exists(L_JSON_SAVE_PATH) then _payJson = File.ReadAllText(L_JSON_SAVE_PATH) self.TempPayJson = _payJson self.MD5 = string.upper(MD5Utils.MD5String(_payJson)) if msg.md5 ~= self.MD5 then Debug.LogError("MD5不匹配,充值数据有异常.."..msg.md5) end else Debug.LogError("哦和..服务器没发充值数据..本地也没有充值数据..") end --充值数据不为空就在本地再保存下 else --这里rechargeItemJson不为空,那可能是首次下发和数据有改变 if not File.Exists(L_JSON_SAVE_PATH) then File.Create(L_JSON_SAVE_PATH):Dispose() else --如果存在文件,那么肯定就是数据有改变的情况 Debug.LogError("充值MD5有改变,最新数据...MD5 = "..msg.md5..", json = ".._payJson) end File.WriteAllText(L_JSON_SAVE_PATH, _payJson); end else _payJson = self.TempPayJson end else Debug.LogError("服务器充值的JSON数据异常") end if _payJson ~= nil and _payJson ~= "" then local _payDataTab = Json.decode(_payJson) local _payDataList = List:New(_payDataTab) for _key, _data in pairs(_payDataList) do local _cfgId = tonumber(_data.goods_system_cfg_id) --1. 根据配置表ID储存数据 local _sData = L_ServerData:New(_data) local _payData = self.PayDataIdDict[_cfgId] if _payData ~= nil then _payData:RefeshData(_sData) else _payData = L_PayData:New(_sData) self.PayDataIdDict:Add(_cfgId, _payData) end --2. 根据RechargeType来储存数据 local _rechargeType = _sData.RechargeType if not self.PayDataTypeDict:ContainsKey(_rechargeType) then local _dataList = List:New() _dataList:Add(_payData) self.PayDataTypeDict:Add(_rechargeType, _dataList) else local _dataList = self.PayDataTypeDict[_rechargeType] local _hasSameData = false local _curCfgId = _payData.ServerCfgData.CfgId for i = 1, #_dataList do if _curCfgId == _dataList[i].ServerCfgData.CfgId then self.PayDataTypeDict[_rechargeType][i] = _payData _hasSameData = true break end end if not _hasSameData then self.PayDataTypeDict[_rechargeType]:Add(_payData) end end end --处理下上线的时候需要处理的内容 self:HandleOnline() GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_REFESH_PAY_DATA) --刷新月卡周卡的数据 GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_WELFARE_CARD_REFRESH, nil) else Debug.LogError("服务器没下发充值的Json数据!") end local _sing = self:GeMoneySignByCfg() end --处理下上线的时候需要处理的内容 function PaySystem:HandleOnline() --上线的时候设置下所有充值功能的红点,点开界面之后关掉红点 if not self.HasSetRedPoint then GameCenter.MainFunctionSystem:SetAlertFlag(FunctionStartIdCode.Pay, true) GameCenter.MainFunctionSystem:SetFunctionEffect(FunctionStartIdCode.Pay, true) GameCenter.MainFunctionSystem:SetAlertFlag(FunctionStartIdCode.PayBase, true) GameCenter.MainFunctionSystem:SetAlertFlag(FunctionStartIdCode.PayNewbie, true) GameCenter.MainFunctionSystem:SetAlertFlag(FunctionStartIdCode.PayWeek, true) GameCenter.MainFunctionSystem:SetAlertFlag(FunctionStartIdCode.PayDay, true) self.HasSetRedPoint = true end local _localPlayer = GameCenter.GameSceneSystem:GetLocalPlayer() --这里加个判断是防止还没进游戏服就在请求数据,如果localplayer都有数据了那肯定是进了场景了 if _localPlayer ~= nil then --请求游戏服的充值数据 local _reqMsg = ReqMsg.MSG_Recharge.ReqRechargeData:New() _reqMsg:Send() end end --根据配置表获取当前货币符号 function PaySystem:GeMoneySignByCfg() if self.CacheMoneySign == nil then local _region = GameCenter.LoginSystem.Region if _region ~= nil and _region ~= "" and _region ~= 0 then --tw local _chanel = self.SdkPlatCfg.Chanel DataConfig.DataSdkplatform:ForeachCanBreak( function(_id, _cfg) if _cfg.Chanel == _chanel and _cfg.Region == _region then self.CacheMoneySign = _cfg.MoneySign return true end end ) else if self.SdkPlatCfg ~= nil then if self.SdkPlatCfg.Chanel == "TW" then self.CacheMoneySign = "$" else self.CacheMoneySign = self.SdkPlatCfg.MoneySign end end end end return self.CacheMoneySign end return PaySystem