249 lines
8.8 KiB
Lua
249 lines
8.8 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
|
||
|
||
--初始化
|
||
function this.Initialize()
|
||
Game.GlobalEvent:AddEvent(GameEvent.MoneyPay.OnPayResultSuccess, this.onSdkPayResult)
|
||
end
|
||
|
||
----- 请求支付
|
||
----- @param context
|
||
---- {
|
||
---- Id, -- required, 商品ID
|
||
---- BuyNum, -- optional, 购买个数(默认1)
|
||
---- }
|
||
----
|
||
----
|
||
|
||
this.EventTrigger = {}
|
||
function this.onSdkPayResult(id)
|
||
LogRed("收到sdk支付成功回调,商品id = " .. id)
|
||
if this.EventTrigger[id] then
|
||
this.EventTrigger[id](id)
|
||
this.EventTrigger[id] = nil
|
||
end
|
||
local rechargeConfig = ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, id)
|
||
if rechargeConfig then
|
||
local Price = tostring(rechargeConfig.Price)
|
||
CustomEventManager.SendCustomEvent("mhtPay", Price, "4")
|
||
end
|
||
end
|
||
|
||
-- 这里发过来的是sdk的orderID
|
||
function this.RechargeSuccessOrder(msg)
|
||
LogRed("充值成功,orderId = " .. tostring(msg.orderId))
|
||
-- 订单id特殊传值
|
||
CustomEventManager.SendCustomEvent("PayOrderId", tostring(msg.orderId), "0")
|
||
end
|
||
|
||
function this.Pay(id, func)
|
||
if not RECHARGEABLE then
|
||
return
|
||
end
|
||
LogRed("发起购买商品的请求,商品id = " .. id)
|
||
if not func then
|
||
func = function() end
|
||
end
|
||
|
||
-- 请求游戏服务器的orderId
|
||
NetManager.RequestPayOrderId(id, function(orderId)
|
||
-- if this.EventTrigger[id] then
|
||
-- LogError("重复发起购买商品的请求,商品id = "..id..", uid = "..PlayerManager.uid)
|
||
-- PopupTipPanel.ShowTip("您有一个相同的订单正在处理,请稍后再试")
|
||
-- return
|
||
-- end
|
||
LogRed("发起购买商品的请求,订单id = " .. orderId)
|
||
this.EventTrigger[id] = func
|
||
CheckRedPointStatus(RedPointType.GrowthPackage) --成长礼包的红点检测
|
||
--直购商品
|
||
local rechargeConfig = ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, id)
|
||
--现金点券购买
|
||
local xjdqNum = BagManager.GetItemCountById(131)
|
||
if xjdqNum >= rechargeConfig.Price and rechargeConfig.Price > 0 and rechargeConfig.OtherBuy1 and #rechargeConfig.OtherBuy1 > 0 and rechargeConfig.OtherBuy1[1][1] == 131 then
|
||
local sureFunc = function()
|
||
NetManager.MoneyBuyGiftRequest(id)
|
||
end
|
||
--[[
|
||
local cancelFunc = function()
|
||
if AppConst.isSDKLogin and rechargeConfig.Price > 0 then
|
||
PayManager.SdkPay({ Id = id, orderId = orderId })
|
||
else
|
||
NetManager.RequestBuyGiftGoods(id, function(msg)
|
||
this.onSdkPayResult(id)
|
||
end)
|
||
end
|
||
end
|
||
]]
|
||
local args = {}
|
||
args[1] = Language[12029]
|
||
args[2] = Language[12030]
|
||
args[3] = Language[10685]
|
||
args[4] = sureFunc
|
||
args[5] = nil
|
||
UIManager.OpenPanel(UIName.GMCommonConfirmPanel, args)
|
||
return
|
||
end
|
||
|
||
local yhqNum = BagManager.GetItemCountById(108)
|
||
if yhqNum >= rechargeConfig.Price and rechargeConfig.Price > 0 then
|
||
local sureFunc = function()
|
||
NetManager.SendYHQBuyGoodRequest(id)
|
||
end
|
||
--[[
|
||
local cancelFunc = function()
|
||
if AppConst.isSDKLogin and rechargeConfig.Price > 0 then
|
||
PayManager.SdkPay({ Id = id, orderId = orderId })
|
||
else
|
||
NetManager.RequestBuyGiftGoods(id, function(msg)
|
||
this.onSdkPayResult(id)
|
||
end)
|
||
end
|
||
end
|
||
]]
|
||
local args = {}
|
||
args[1] = Language[12032]
|
||
args[2] = Language[12033]
|
||
args[3] = Language[10685]
|
||
args[4] = sureFunc
|
||
args[5] = nil
|
||
UIManager.OpenPanel(UIName.GMCommonConfirmPanel, args)
|
||
return
|
||
end
|
||
if rechargeConfig.OtherBuy1 == nil then
|
||
if rechargeConfig.BuyType == 1 then --人民币
|
||
if AppConst.isSDKLogin and rechargeConfig.Price > 0 then
|
||
PayManager.SdkPay({ Id = id, orderId = orderId })
|
||
else
|
||
NetManager.RequestBuyGiftGoods(id, function(msg)
|
||
this.onSdkPayResult(id)
|
||
end)
|
||
end
|
||
elseif rechargeConfig.BuyType == 2 then --gm余额购买
|
||
NetManager.SendGmBuyGoodRequest(id)
|
||
elseif rechargeConfig.BuyType == 3 then --rmb/gm余额购买
|
||
local sureFunc = function()
|
||
NetManager.SendGmBuyGoodRequest(id)
|
||
end
|
||
local cancelFunc = function()
|
||
if AppConst.isSDKLogin and rechargeConfig.Price > 0 then
|
||
PayManager.SdkPay({ Id = id, orderId = orderId })
|
||
else
|
||
NetManager.RequestBuyGiftGoods(id, function(msg)
|
||
this.onSdkPayResult(id)
|
||
end)
|
||
end
|
||
end
|
||
local args = {}
|
||
args[1] = Language[12034]
|
||
args[2] = Language[12035]
|
||
args[3] = Language[12031]
|
||
args[4] = sureFunc
|
||
args[5] = cancelFunc
|
||
UIManager.OpenPanel(UIName.GMCommonConfirmPanel, args)
|
||
end
|
||
else
|
||
MsgPanel.ShowTwo(GetLanguageStrById("现金点券不足是否前往充值"), function()
|
||
|
||
end, function()
|
||
UIManager.OpenPanel(UIName.ActivityMainPanel, 36, 8)
|
||
end, nil, GetLanguageStrById("前往充值"))
|
||
end
|
||
end)
|
||
end
|
||
|
||
function this.SdkPay(context)
|
||
local rechargeConfig = ConfigManager.GetConfigData(ConfigName.RechargeCommodityConfig, context.Id)
|
||
local payData = table.clone(context)
|
||
payData.Name = rechargeConfig.Name
|
||
payData.Desc = rechargeConfig.Desc
|
||
payData.Price = rechargeConfig.Price
|
||
payData.Type = rechargeConfig.Type
|
||
|
||
-- 判断计费点
|
||
local field = PackageManager.GetRechargeField()
|
||
if field and rechargeConfig[field] and rechargeConfig[field] ~= "" then
|
||
payData.RechargeId = rechargeConfig[field]
|
||
else
|
||
payData.RechargeId = rechargeConfig.Id
|
||
end
|
||
LogError("paydata.RechargeId==" .. payData.RechargeId)
|
||
this.RequestPay(payData)
|
||
end
|
||
|
||
function this.RequestPay(context)
|
||
local price = MoneyUtil.GetMoney(context.Price)
|
||
|
||
SDKMgr.onPayCallback = function(payResp)
|
||
Log("onPayCallback::" .. payResp)
|
||
local str = string.split(payResp, "#")
|
||
local result = tonumber(str[1])
|
||
-- local orderId = str[2]
|
||
if result == SDK_RESULT.SUCCESS then
|
||
|
||
elseif result == SDK_RESULT.FAILED then
|
||
|
||
else
|
||
--PopupTipPanel.ShowTip("取消充值" .. result)
|
||
-- DataCenterManager.CommitPayStatus(
|
||
-- "IN_GAME_"..context.Type,
|
||
-- SDKMgr:GetPayOrderID(),
|
||
-- "INVALID",
|
||
-- tostring(context.Price)
|
||
-- )
|
||
end
|
||
end
|
||
local params = SDK.SDKPayArgs.New()
|
||
params.productId = context.RechargeId
|
||
params.productName = context.Name or ""
|
||
params.productDesc = context.Desc or ""
|
||
params.price = price -- 以分为单位
|
||
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.roleID = tostring(PlayerManager.uid) ..
|
||
"_" ..
|
||
tostring(context.Id) ..
|
||
"_" ..
|
||
PackageManager.GetCCCode() ..
|
||
"_" ..
|
||
AppConst
|
||
.Platform --.."_"..tostring(context.orderId or "0").."_"..tostring(LoginManager.ServerId)
|
||
params.roleName = PlayerManager.nickName
|
||
params.roleLevel = PlayerManager.level
|
||
params.vip = tostring(VipManager.GetVipLevel())
|
||
params.guildID = PlayerManager.familyId
|
||
-- 其他
|
||
params.payNotifyUrl = ""
|
||
params.extension = tostring(PlayerManager.uid) ..
|
||
"_" ..
|
||
tostring(context.Id) ..
|
||
"_" ..
|
||
PackageManager.GetCCCode() ..
|
||
"_" ..
|
||
AppConst
|
||
.Platform --.."_"..tostring(context.orderId or "0").."_"..tostring(LoginManager.ServerId)
|
||
SDKMgr:Pay(params)
|
||
|
||
--
|
||
-- ThinkingAnalyticsManager.Track("create_order", {
|
||
-- goods_id = context.Id,
|
||
-- goods_name = context.Name,
|
||
-- order_money_amount = price,
|
||
-- Bundle_id = AppConst.SdkPackageName,
|
||
-- })
|
||
end
|
||
|
||
return this
|