c1_lua/lua/app/common/pay_manager.lua
2023-05-23 17:04:04 +08:00

187 lines
6.8 KiB
Lua

local PayManager = class("PayManager", BaseModule)
local BLESSING_GIFT_ID = 30001
PayManager.PURCHARSE_TYPE = {
ACT_GIFT = 1,
ACT_GOLD_PIG = 2,
CHAPTER_GIFT = 3,
GROW_UP_GIFT = 4,
MALL_TREASURE = 5,
}
PayManager.PURCHARSE_ACT_TYPE = {
BOUNTY = 7,
}
PayManager.PURCHARSE_TYPE_CONFIG = {
[PayManager.PURCHARSE_TYPE.ACT_GIFT] = "act_gift",
[PayManager.PURCHARSE_TYPE.ACT_GOLD_PIG] = "act_gold_pig",
[PayManager.PURCHARSE_TYPE.MALL_TREASURE] = "mall_treasure",
}
PayManager.BI_ITEM_GET_TYPE = {
[PayManager.PURCHARSE_TYPE.ACT_GIFT] = {
[PayManager.PURCHARSE_ACT_TYPE.BOUNTY] = BIReport.ITEM_GET_TYPE.BOUNTY,
},
[PayManager.PURCHARSE_TYPE.ACT_GOLD_PIG] = BIReport.ITEM_GET_TYPE.GOLD_PIG,
[PayManager.PURCHARSE_TYPE.MALL_TREASURE] = BIReport.ITEM_GET_TYPE.MALL_TREASURE,
}
PayManager.BI_GIFT_TYPE = {
[PayManager.PURCHARSE_TYPE.ACT_GIFT] = {
[PayManager.PURCHARSE_ACT_TYPE.BOUNTY] = BIReport.GIFT_TYPE.BOUNTY,
},
[PayManager.PURCHARSE_TYPE.ACT_GOLD_PIG] = BIReport.GIFT_TYPE.GOLD_PIG,
[PayManager.PURCHARSE_TYPE.MALL_TREASURE] = BIReport.GIFT_TYPE.MALL_TREASURE,
}
function PayManager:getItemGetType(purchaseType, id)
local cfgName = PayManager.PURCHARSE_TYPE_CONFIG[purchaseType]
if not cfgName then
return
end
local cfg = ConfigManager:getConfig(cfgName)
local typeMap = PayManager.BI_ITEM_GET_TYPE[purchaseType]
if not cfg or not cfg[id] or not typeMap then
return
end
local subType = cfg[id].type
if subType then
return typeMap[cfg[id].type]
else
if type(typeMap) ~= "table" then
return typeMap
end
end
end
function PayManager:getGiftType(purchaseType, id)
local cfgName = PayManager.PURCHARSE_TYPE_CONFIG[purchaseType]
if not cfgName then
return
end
local cfg = ConfigManager:getConfig(cfgName)
local typeMap = PayManager.BI_GIFT_TYPE[purchaseType]
if not cfg or not cfg[id] or not typeMap then
return
end
local subType = cfg[id].type
if subType then
return typeMap[cfg[id].type]
else
if type(typeMap) ~= "table" then
return typeMap
end
end
end
function PayManager:purchasePackage(id, purchaseType)
local cfgName = PayManager.PURCHARSE_TYPE_CONFIG[purchaseType]
if not cfgName then
return
end
local cfg = ConfigManager:getConfig(cfgName)
if not cfg or not cfg[id] then
return
end
local rechargeId = cfg[id].recharge_id
local giftType = PayManager:getGiftType(purchaseType, id)
local productId
if rechargeId then
local rechargeCfg = ConfigManager:getConfig("recharge")[rechargeId]
if rechargeCfg == nil then
return
end
productId = rechargeCfg.payId
BIReport:postPayClick(giftType, id, rechargeId)
end
self:checkAndPay(productId, id, purchaseType, rechargeId)
end
function PayManager:requestRewards(purchaseToken, orderId, originOrderId, giftType, id, rechargeId)
self:sendMsgToServer(purchaseToken, orderId, function(binder, msgData)
if msgData.status == 0 then
if msgData.rewards and table.nums(msgData.rewards) > 0 then -- 奖励改到邮件领取
GFunc.showRewardBox(msgData.rewards)
end
BIReport:postPayGet(giftType, id, rechargeId, orderId, originOrderId, 1, msgData.rewards or {})
local rechargeCfg = ConfigManager:getConfig("recharge")[rechargeId]
if rechargeCfg then
BIReport:postPurchase(rechargeCfg.price, rechargeCfg.payId, originOrderId, orderId)
end
table.foreach(msgData.gift, function(i, gift)
local rechargeId = DataManager.ShopData:getShopItemCfg(gift).recharge_id
DataManager.PlayerData:addPayment(rechargeId)
DataManager.ShopData:updateGiftInfo(gift)
end)
-- 支付验证成功后消耗此订单
if purchaseToken then
SDKManager:consumePurchase(purchaseToken)
end
elseif msgData.status == 1010 then -- 验证异常,但是需要消耗订单
if purchaseToken then
SDKManager:consumePurchase(purchaseToken)
end
Logger.logError("重复验证")
else
Logger.logError("支付验证失败:%s", msgData.status)
local params = {
content = I18N:getGlobalText(I18N.GlobalConst.PAY_FAILED_DESC_1),
boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
}
GFunc.showMessageBox(params)
end
end)
end
function PayManager:checkAndPay(productId, id, purchaseType, rechargeId)
-- 检查是否可以支付
SDKManager:checkPay(productId, function(code)
if code == 0 then
self:sendMessage(ProtoMsgType.FromMsgEnum.ActPayReq, {id = id, act_type = purchaseType}, {}, function(binder, msgData)
if msgData.status == 0 then
if msgData.uuid and msgData.uuid ~= GConst.EMPTY_STRING then
local giftType = PayManager:getGiftType(purchaseType, id)
BIReport:postPayTurn(giftType, id, rechargeId)
SDKManager:pay(productId, msgData.uuid, rechargeId, giftType, function(purchaseToken, orderId, originOrderId)
if purchaseToken and orderId then
self:requestRewards(purchaseToken, orderId, originOrderId, giftType, id, rechargeId)
end
end)
else -- 没有支付信息,直接发奖
if table.nums(msgData.rewards) > 0 then
GFunc.showRewardBox(msgData.rewards)
end
local giftData = {}
giftData.act_type = msgData.act_type
giftData.id = msgData.id
giftData.buy_count = DataManager.ShopData:getGiftBoughtNum(msgData.act_type, msgData.id) + 1
giftData.latest_buy_at = Time:getServerTime() * 1000 -- 服务器都是毫秒
DataManager.ShopData:updateGiftInfo(giftData)
end
else
Logger.logError("预支付失败")
end
end)
end
end)
end
function PayManager:sendMsgToServer(purchaseToken, orderId, callback)
local args = {
uuid = {orderId},
channel = SDKManager:getSDKPayType(),
pay_token = purchaseToken
}
if EDITOR_MODE then
args.channel = SDKManager.PAY_TYPE.DEBUG
end
self:sendMessage(ProtoMsgType.FromMsgEnum.ActPaidResultReq, args, {}, callback)
end
return PayManager