334 lines
14 KiB
Lua
334 lines
14 KiB
Lua
--[[
|
||
* @ClassName PayManager
|
||
* @Description 支付管理系统
|
||
* @Date 2019/6/25 20:14
|
||
* @Author MagicianJoker, fengliudianshao@outlook.com
|
||
* @Copyright Copyright (c) 2019, MagicianJoker
|
||
--]]
|
||
PayManager = {}
|
||
local this = PayManager
|
||
local LoginRoot_PayUrl = VersionManager:GetVersionInfo("payUrl")
|
||
local RechargeCommodityConfig = ConfigManager.GetConfig(ConfigName.RechargeCommodityConfig)
|
||
--初始化
|
||
function this.Initialize()
|
||
Game.GlobalEvent:AddEvent(GameEvent.MoneyPay.OnPayResultSuccess, this.onSdkPayResult)
|
||
this.isCanPay = true
|
||
end
|
||
this.EventTrigger = {}
|
||
function this.onSdkPayResult(id)
|
||
LogRed("收到sdk支付成功回调,商品id = "..id)
|
||
if this.EventTrigger[id] then
|
||
this.EventTrigger[id](id)
|
||
this.EventTrigger[id] = nil
|
||
end
|
||
end
|
||
-- 请求支付
|
||
--@param context
|
||
--{
|
||
-- Id, -- required, 商品ID
|
||
-- BuyNum, -- optional, 购买个数(默认1)
|
||
--}
|
||
function this.Pay(context, func)
|
||
local rechargeConfig = ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, context.Id)
|
||
if rechargeConfig.Price > 0 then
|
||
|
||
if GetChannelConfig().IsItemPay then
|
||
if BagManager.GetItemCountById(920000) < RechargeCommodityConfig[context.Id].ItemPrice[2] then
|
||
UIManager.OpenPanel(UIName.GeneralPopup, GENERAL_POPUP_TYPE.Currency, GetLanguageStrById(50510), function ()
|
||
if AppConst.isSDKLogin then
|
||
SDKMgr:LoginPanel_Btn1()
|
||
end
|
||
end, GetLanguageStrById(50511))
|
||
else
|
||
NetManager.TestBuyGiftGoodsRequest2(context.Id, function (msg)
|
||
if func then
|
||
func()
|
||
end
|
||
end)
|
||
end
|
||
else
|
||
if rechargeConfig.OtherBuy and #rechargeConfig.OtherBuy>0 and BagManager.GetItemCountById(rechargeConfig.OtherBuy[1])>=rechargeConfig.OtherBuy[2] then
|
||
local sureFunc=function()
|
||
NetManager.VoucherPriceBuyGiftGoodsRequest(0,context.Id,function ()
|
||
LogError("支付成功")
|
||
if func then
|
||
func()
|
||
end
|
||
end)
|
||
end
|
||
local cancelFunc=function()
|
||
if rechargeConfig.IsDevalue == 1 then
|
||
UIManager.OpenPanel(UIName.DiscountPanel,
|
||
{configType = 1, config = rechargeConfig, openType = 1,},
|
||
function (isReal, discountID)
|
||
this.StartPay(isReal, discountID, context, func)
|
||
end)
|
||
elseif rechargeConfig.CashPointPay == 1 then
|
||
UIManager.OpenPanel(UIName.DiscountPanel,
|
||
{configType = 1, config = rechargeConfig, openType = 2,},
|
||
function (isReal, discountID)
|
||
this.StartPay(isReal, discountID, context, func)
|
||
end)
|
||
else
|
||
this.StartPay(true, 0, context, func)
|
||
end
|
||
end
|
||
local itemId=rechargeConfig.OtherBuy[1]
|
||
local itemNum=rechargeConfig.OtherBuy[2]
|
||
local args={}
|
||
args[1]=string.format(GetLanguageStrById(1013002),itemNum,BagManager.GetItemCountById(itemId))
|
||
args[2]=GetLanguageStrById(1013003)
|
||
args[3]=GetLanguageStrById(1013004)
|
||
args[4]=sureFunc
|
||
args[5]=cancelFunc
|
||
UIManager.OpenPanel(UIName.CommonConfirmTipPanel,args)
|
||
return
|
||
end
|
||
if rechargeConfig.IsDevalue == 1 then
|
||
UIManager.OpenPanel(UIName.DiscountPanel,
|
||
{configType = 1, config = rechargeConfig, openType = 1,},
|
||
function (isReal, discountID)
|
||
this.StartPay(isReal, discountID, context, func)
|
||
end)
|
||
elseif rechargeConfig.CashPointPay == 1 then
|
||
UIManager.OpenPanel(UIName.DiscountPanel,
|
||
{configType = 1, config = rechargeConfig, openType = 2,},
|
||
function (isReal, discountID)
|
||
this.StartPay(isReal, discountID, context, func)
|
||
end)
|
||
else
|
||
this.StartPay(true, 0, context, func)
|
||
end
|
||
end
|
||
else
|
||
this.StartPay(true, 0, context, func)
|
||
end
|
||
end
|
||
|
||
function this.StartPay(isReal, discountID, context, func)
|
||
if not GetChannelConfig().Recharge_SDK_open then
|
||
PopupTipPanel.ShowTipByLanguageId(10414)
|
||
return
|
||
end
|
||
if not this.isCanPay then
|
||
PopupTipPanel.ShowTipByLanguageId(91001609)
|
||
return
|
||
else
|
||
this.isCanPay = false
|
||
if not this.payCDTimer then
|
||
local cd = 1
|
||
this.payCDTimer = Timer.New(function ()
|
||
if cd <= 0 then
|
||
this.payCDTimer:Stop()
|
||
this.payCDTimer = nil
|
||
this.isCanPay = true
|
||
end
|
||
cd = cd - 1
|
||
end, 0.5, -1, true)
|
||
this.payCDTimer:Start()
|
||
end
|
||
end
|
||
|
||
if GetChannelConfig().IsRefund_tips then
|
||
if ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, context.Id).IsRefund == 1 then
|
||
UIManager.OpenPanel(UIName.GeneralPopup, GENERAL_POPUP_TYPE.Currency, GetLanguageStrById(50250), function (msg)
|
||
this.SelectPay(isReal, discountID, context, func)
|
||
end)
|
||
else
|
||
this.SelectPay(isReal, discountID, context, func)
|
||
end
|
||
else
|
||
this.SelectPay(isReal, discountID, context, func)
|
||
end
|
||
end
|
||
|
||
function this.SelectPay(isReal, discountID, context, func)
|
||
if isReal then
|
||
if not AppConst.isSDKLogin then
|
||
this.GMRealPay(discountID, context, func)
|
||
else
|
||
this.SDKPayFun(discountID, context, func)
|
||
end
|
||
else
|
||
this.GMPayFun(discountID, context, func)
|
||
end
|
||
end
|
||
|
||
--GM真实充值
|
||
function this.GMRealPay(discountID, context, func)
|
||
LogError("GMRealPay:"..context.Id)
|
||
NetManager.RequestBuyGiftGoods(discountID, context.Id, function(msg)
|
||
if func then
|
||
func()
|
||
end
|
||
end)
|
||
end
|
||
|
||
--福利币购买
|
||
function this.GMPayFun(discountID, context, func)
|
||
LogError("GMPay:"..context.Id)
|
||
NetManager.TestBuyGiftGoodsRequest(discountID, context.Id, function(msg)
|
||
if func then
|
||
func()
|
||
end
|
||
end)
|
||
end
|
||
|
||
function this.SDKPayFun(discountID, context, func)
|
||
LogError("SDKPay:"..context.Id)
|
||
if not func then
|
||
func = function() end
|
||
end
|
||
this.EventTrigger[context.Id] = func
|
||
local rechargeConfig = ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, context.Id)
|
||
local multiLanguage = ConfigManager.GetConfigData(ConfigName.MultiLanguage, GetCurLanguage())
|
||
local channelRechargeIdConfig = ConfigManager.GetConfigData(ConfigName.ChannelRechargeIdConfig, context.Id)
|
||
local price
|
||
if discountID > 0 then
|
||
for i = 1, #rechargeConfig.PriceDevalue do
|
||
if rechargeConfig.PriceDevalue[i][1] == discountID then
|
||
price = rechargeConfig.PriceDevalue[i][2]
|
||
end
|
||
end
|
||
else
|
||
price = rechargeConfig.Price
|
||
end
|
||
local exchangeRate = ConfigManager.GetConfigDataByKey(ConfigName.ExchangeRate, "Price_1", price)
|
||
price = exchangeRate[GetChannelConfig().Currency_type]
|
||
local payData = table.clone(context)
|
||
payData.Name = rechargeConfig.Name
|
||
payData.Desc = rechargeConfig.Desc
|
||
payData.Price = price
|
||
payData.CurrencyType = multiLanguage.CurrencyCode
|
||
payData.Type = rechargeConfig.Type
|
||
payData.ShowType = rechargeConfig.ShowType
|
||
local str = string.split(channelRechargeIdConfig[AppConst.ChannelType], "|")
|
||
local RechargeId
|
||
for i = 1, #str do
|
||
local arr = string.split(str[i], "#")
|
||
if tonumber(arr[1]) == discountID then
|
||
RechargeId = arr[2]
|
||
break
|
||
end
|
||
end
|
||
payData.RechargeId = RechargeId--channelRechargeIdConfig[AppConst.ChannelType]
|
||
payData.devaluateId = discountID
|
||
this.RequestPay(payData)
|
||
SubmitExtraData({ type = SDKSubMitType.TYPE_PURCHE })
|
||
end
|
||
|
||
function this.RequestPay(context)
|
||
SDKMgr.onPayCallback = function(payResp)
|
||
NetManager.RequestVipLevelUp(function()end)
|
||
local str = string.split(payResp, "#")
|
||
local result = tonumber(str[1])
|
||
local orderId = str[2]
|
||
if result == SDK_RESULT.SUCCESS then
|
||
DataCenterManager.CommitPayStatus(
|
||
"IN_GAME_"..context.Type,
|
||
orderId,
|
||
"VALID",
|
||
tostring(context.Price)
|
||
)
|
||
elseif result == SDK_RESULT.FAILED then
|
||
DataCenterManager.CommitPayStatus(
|
||
"IN_GAME_"..context.Type,
|
||
orderId,
|
||
"INVALID",
|
||
tostring(context.Price)
|
||
)
|
||
else
|
||
end
|
||
end
|
||
if context.Price == 0 then
|
||
NetManager.RequestBuyZeroGiftGoods(context.Id)
|
||
return
|
||
end
|
||
local sendStr1 = LoginRoot_PayUrl.."/pay/CreatOrder?"
|
||
.."openid="..AppConst.OpenId
|
||
.."&uid="..AppConst.UserId
|
||
.."®ion="..PlayerManager.serverInfo.server_id
|
||
.."&channel="..AppConst.SdkChannel
|
||
.."&pay_item="..context.Id
|
||
.."&price="..(context.Price*100)
|
||
.."&devalueId="..context.devaluateId
|
||
Log("sendStr1:"..sendStr1)
|
||
networkMgr:SendGetHttp(sendStr1,
|
||
function(msg)
|
||
local json = require 'cjson'
|
||
local data = json.decode(msg)
|
||
if data.errCode then
|
||
local errorCfg = ConfigManager.TryGetConfigData(ConfigName.ErrorCodeHint, data.errCode)
|
||
PopupTipPanel.ShowTip(GetLanguageStrById(errorCfg.Desc))
|
||
else
|
||
local params = SDK.SDKPayArgs.New()
|
||
params.rechargeId = ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, context.Id).RechargeId2
|
||
params.showType = context.ShowType
|
||
params.productId = context.Id
|
||
params.productName = GetLanguageStrById(context.Name) or ""
|
||
params.productDesc = GetLanguageStrById(context.Desc) or ""
|
||
params.price = tostring(context.Price)
|
||
params.currencyType = context.CurrencyType
|
||
-- 以分为单位
|
||
params.ratio = 1
|
||
params.buyNum = context.BuyNum or 1
|
||
params.coinNum = BagManager.GetItemCountById(16)
|
||
-- 服务器相关
|
||
-- params.zoneId = PlayerManager.serverInfo.server_id
|
||
params.serverID = PlayerManager.serverInfo.server_id
|
||
params.serverName = PlayerManager.serverInfo.name
|
||
params.accounted = ""
|
||
-- 角色相关
|
||
params.roleID = tostring(PlayerManager.uid)
|
||
params.roleName = PlayerManager.nickName
|
||
params.roleLevel = PlayerManager.level
|
||
params.vip = tostring(VipManager.GetVipLevel())
|
||
params.guildID = PlayerManager.familyId
|
||
-- 其他
|
||
if UnityEngine.Application.platform == UnityEngine.RuntimePlatform.IPhonePlayer then
|
||
params.payNotifyUrl = LoginRoot_PayUrl.."/pay/quickIosNotify"
|
||
else
|
||
params.payNotifyUrl = LoginRoot_PayUrl.."/pay/quickNotify"
|
||
end
|
||
|
||
params.orderID = data.orderId
|
||
params.extension = string.format('{"extension":"%s"}',string.format("%s_%s_%s_%s", PlayerManager.uid, context.Id, VersionManager:GetVersionInfo("subChannel"),data.orderId))--tostring(context.Id)
|
||
-- string.format("%s_%s_%s_%s_%s_%s",
|
||
-- AppConst.OpenId,context.Id,context.Price,PlayerManager.uid,
|
||
-- PlayerManager.serverInfo.server_id,PlayerManager.serverInfo.name)
|
||
local sendStr2 = LoginRoot_PayUrl.."/pay/sign?"
|
||
.."game_area="..PlayerManager.serverInfo.server_id
|
||
.."&game_guid="..AppConst.AppID or ""
|
||
.."&game_orderid="..data.orderId
|
||
.."&game_price="..tostring(string.format("%.2f", context.Price))
|
||
.."&subject="..params.productDesc
|
||
.."&channel="..AppConst.SdkChannel
|
||
Log("sendStr2:"..sendStr2)
|
||
networkMgr:SendGetHttp(sendStr2,function(msg_2)
|
||
local json2 = require 'cjson'
|
||
local data2 = json2.decode(msg_2)
|
||
if data2.errCode then
|
||
local errorCfg = ConfigManager.TryGetConfigData(ConfigName.ErrorCodeHint, data2.errCode)
|
||
PopupTipPanel.ShowTip(GetLanguageStrById(errorCfg.Desc))
|
||
else
|
||
--特殊参数 #拼接
|
||
params.zoneId = AppConst.AppID.."#"..(data2.game_sign or "")
|
||
ThinkingAnalyticsManager.Track("recharge", {
|
||
server_id = params.serverID,
|
||
area_id = 0,
|
||
vip_level = VipManager.GetVipLevel(),
|
||
level = params.roleLevel,
|
||
order_id = params.orderID,
|
||
pay_type = params.currencyType,
|
||
pay_amount = params.price,
|
||
pay_reason = params.productName,
|
||
pay_platform = UnityEngine.Application.platform
|
||
})
|
||
SDKMgr:Pay(params)
|
||
end
|
||
end, nil, nil, nil)
|
||
end
|
||
end, nil, nil, nil)
|
||
end
|
||
|
||
return this |