c1_lua/lua/app/common/pay_manager.lua
2023-04-26 18:12:49 +08:00

175 lines
6.3 KiB
Lua

local PayManager = class("PayManager", BaseModule)
local BLESSING_GIFT_ID = 30001
PayManager.PURCHARSE_TYPE = {
MALL_ACT = 1,
MALL_DAILY = 2,
MALL_TREASURE = 3,
}
PayManager.PURCHARSE_TYPE_CONFIG = {
[PayManager.PURCHARSE_TYPE.MALL_ACT] = "mall_act",
[PayManager.PURCHARSE_TYPE.MALL_DAILY] = "mall_daily",
[PayManager.PURCHARSE_TYPE.MALL_TREASURE] = "mall_treasure",
}
PayManager.BI_ITEM_GET_TYPE = {
}
PayManager.BI_GIFT_TYPE = {
}
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:setPaymentCount(rechargeId)
DataManager.ShopData:updateGiftInfo(gift)
if gift.mall_type == PayManager.PURCHARSE_TYPE.MALL_ACT and gift.id == BLESSING_GIFT_ID then
DataManager.BlessingData:onBuyGift()
end
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.MallPayReq, {id = id, mall_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.mall_type = msgData.mall_type
giftData.id = msgData.id
giftData.buy_count = DataManager.ShopData:getGiftBoughtNum(msgData.id, msgData.mall_type) + 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.MallPaidResultReq, args, {}, callback)
end
return PayManager