PayMgr.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. local PayMgr = class('PayMgr')
  2. local REQ_PAY_CD = 1000
  3. local EXCPETION_WAIT_TIMESTAMP = 20000
  4. local EXCPETION_WAIT_TIME = 20
  5. local PayGoodsType = {
  6. EPayType_None = 0, -- 无效商品
  7. EPayType_Discount = 1, -- 超值礼包购买
  8. EPayType_MonthCard = 2, -- 月卡
  9. EPayType_NormalBag = 3, -- 普通礼包
  10. EPayType_LimitBag = 4, -- 限时礼包
  11. EPayType_RushTower = 5, -- 爬塔冲榜商店
  12. EPayType_RushArena = 6, --英灵殿冲榜商店
  13. EPayType_RushMap = 7, --推图冲榜商店
  14. EPayType_AirShipCash = 8, --高级战令
  15. EPayType_GuildWar = 9, -- 公会战
  16. EPayType_RushPet = 10, --宠物冲榜
  17. EPayType_RushSkill = 11, --技能冲榜
  18. EPayType_IdolShop = 12, --偶像季礼包
  19. EPayType_SpecialPrivilege = 101,
  20. EPayType_PassCheck = 102,
  21. EPayType_BTRecharge100 = 103,
  22. }
  23. local PayState = {
  24. Idle = 1, -- 空闲中
  25. Ordering = 2, -- 正在获得订单号
  26. Ordered = 3, -- 获得订单号完成(一般来说,会立即到 SDKPaying 状态)
  27. SDKPaying = 4, -- 启动SDK支付中
  28. SDKPayComplete = 5, -- SDK支付完成
  29. ServerPayComplete = 6, --Server返回的支付完成 (一般来说,会立即回到 Idle 状态)
  30. }
  31. function PayMgr:ctor()
  32. self.lastSendMsgTimeMap = nil
  33. self.payState = PayState.Idle
  34. self.curPayData = nil
  35. self.rechargeData = {}
  36. self:RegisterNetEvents()
  37. end
  38. function PayMgr:Clear()
  39. self.lastSendMsgTimeMap = nil
  40. self.curPayData = nil
  41. self.payState = PayState.Idle
  42. self.rechargeData = {}
  43. if self.waitTimer then
  44. self.waitTimer:Stop()
  45. self.waitTimer = nil
  46. end
  47. end
  48. function PayMgr:Destroy()
  49. self.lastSendMsgTimeMap = nil
  50. self.payState = nil
  51. self.curPayData = nil
  52. self.rechargeData = nil
  53. if self.waitTimer then
  54. self.waitTimer:Stop()
  55. self.waitTimer = nil
  56. end
  57. self:UnRegisterNetEvents()
  58. end
  59. function PayMgr:RegisterNetEvents()
  60. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PAY_INFO_GET_ACK, self.OnGetPayInfoAck, self)
  61. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PAY_FOR_GOODS_NTF, self.OnPayCompleteNtf, self)
  62. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PAY_INFO_NTF, self.OnPayInfoNtf, self)
  63. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PAY_INFO_ORDER_OK_LIST_GET_ACK, self.OnStartUpPayCompleteAck, self)
  64. end
  65. function PayMgr:UnRegisterNetEvents()
  66. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_INFO_GET_ACK)
  67. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_FOR_GOODS_NTF)
  68. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_INFO_NTF)
  69. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_INFO_ORDER_OK_LIST_GET_ACK)
  70. end
  71. function PayMgr:OnGetPayInfoAck(data)
  72. LogError('[wboy] SC_PAY_INFO_GET_ACK ' .. Inspect(data))
  73. if not data then
  74. LogError('[wboy] message data is Error !!!')
  75. return
  76. end
  77. if not self.curPayData then
  78. LogError('[wboy] cur pay data is Error !!!')
  79. return
  80. end
  81. if self.curPayData.goods_id ~= data.goods_id
  82. or self.curPayData.goods_type ~= data.goods_type
  83. or self.curPayData.count ~= data.count
  84. then
  85. LogError('[wboy] message data and cur pay data is not same !!!')
  86. return
  87. end
  88. if ManagerContainer.NetManager:IsErrorData(data) then
  89. if data then
  90. if data.error == Enum.NetErrorCode.ERROR_PAY_PRODUCTION_MODE then
  91. -- 测试环境
  92. self:SdkPayResult(true)
  93. return
  94. elseif data.error == Enum.NetErrorCode.ERROR_PAY_GOOD_PRICE_FREE then
  95. -- 商品免费
  96. self:SdkPayResult(true)
  97. return
  98. end
  99. end
  100. self:InterruptCurPay()
  101. return
  102. end
  103. if self.payState ~= PayState.Ordering then return end
  104. self.payState = PayState.Ordered
  105. self.curPayData.orderId = data.cp_order_id
  106. self.payState = PayState.SDKPaying
  107. self:StartWaiting(3)
  108. self:SdkPayResult(false)
  109. -- 启动支付SDK
  110. local strArr = string.split(data.goods_name,",")
  111. local strArrCount = #strArr
  112. local cbUrl = data.goods_name
  113. local payType = -1
  114. if strArrCount >= 2 then
  115. cbUrl = strArr[1]
  116. payType = strArr[2] + 0
  117. end
  118. --1.0.0.25
  119. local version = UnityEngine.Application.version
  120. if version == "1.0.0.24" and ManagerContainer.OpenPayMgr.platform == "SDKHwQuick" then
  121. payType = -1
  122. end
  123. --ManagerContainer.OpenPayMgr:Pay(data.goods_id, data.goods_name, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), cbUrl,"ex")
  124. -- and ManagerContainer.OpenPayMgr.platform == "SDKYOUYI_IOS"
  125. if payType >=1 then
  126. ManagerContainer.OpenPayMgr:Pay(data.goods_id, data.goods_name, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), cbUrl,"ex")
  127. else
  128. ManagerContainer.LuaGameMgr:SdkPay(data.goods_id,cbUrl, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), nil)
  129. end
  130. --ManagerContainer.OpenPayMgr:Pay(data.goods_id, data.goods_name, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), data.goods_name,"ex")
  131. end
  132. function PayMgr:OnPayCompleteNtf(data)
  133. --LogError('[wboy] SC_PAY_FOR_GOODS_NTF ' .. Inspect(data))
  134. if ManagerContainer.NetManager:IsErrorData(data) then
  135. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_SERVER_COMPLETED, false)
  136. return
  137. end
  138. self:ServerPayComplete(data.goods_type, data.goods_id, data.goods_num, data.cp_order_id, data.item_list)
  139. end
  140. function PayMgr:OnPayInfoNtf(data)
  141. --LogError('[wboy] SC_PAY_INFO_NTF ' .. Inspect(data))
  142. if ManagerContainer.NetManager:IsErrorData(data) then
  143. return
  144. end
  145. local lastTotalRecharge = self.rechargeData.totalRecharge or 0
  146. local lastDayRecharge = self.rechargeData.dayRecharge or 0
  147. local totalRecharge = CommonUtil.GetValidPayPrice(data.total_recharge or 0)
  148. local dayRecharge = CommonUtil.GetValidPayPrice(data.day_recharge or 0)
  149. self.rechargeData.totalRecharge = totalRecharge
  150. self.rechargeData.dayRecharge = dayRecharge
  151. if totalRecharge ~= lastTotalRecharge then
  152. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_TOTAL_RECHARGE_CHANGED)
  153. if dayRecharge > lastDayRecharge then
  154. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_DAY_RECHARGE_CHANGED, false)
  155. else
  156. -- 当天儲值数量重置了
  157. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_DAY_RECHARGE_CHANGED, true)
  158. end
  159. else
  160. if dayRecharge ~= lastDayRecharge then
  161. -- 当天儲值数量重置了
  162. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_DAY_RECHARGE_CHANGED, true)
  163. end
  164. end
  165. end
  166. function PayMgr:OnStartUpPayCompleteAck(data)
  167. -- LogError('[wboy] SC_PAY_INFO_ORDER_OK_LIST_GET_ACK ' .. Inspect(data))
  168. if ManagerContainer.NetManager:IsErrorData(data) then
  169. return
  170. end
  171. self:CommonShowPayResult(data.reward_item_list)
  172. end
  173. function PayMgr:SendGetPayInfo(goodsType, goodsId, goodsNum)
  174. if not self:IsCanSend(1) then return false end
  175. self:ClearCurPayData()
  176. self.payState = PayState.Ordering
  177. local curPayData =
  178. {
  179. goods_type = goodsType,
  180. goods_id = goodsId,
  181. count = goodsNum,
  182. }
  183. self.curPayData = curPayData
  184. self:RecordCurTime(2)
  185. self:StartWaiting()
  186. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PAY_INFO_GET_REQ, curPayData)
  187. return true
  188. end
  189. function PayMgr:SendStartUpPayComplete()
  190. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PAY_INFO_ORDER_OK_LIST_GET_REQ)
  191. end
  192. function PayMgr:IsCanSend(key, cdTime)
  193. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  194. if not self.lastSendMsgTimeMap then
  195. self.lastSendMsgTimeMap = {}
  196. self.lastSendMsgTimeMap[key] = curTime
  197. return true
  198. end
  199. local lastTime = self.lastSendMsgTimeMap[key]
  200. if lastTime then
  201. local cd = cdTime or REQ_PAY_CD
  202. if (curTime - lastTime) < cd then
  203. return false
  204. end
  205. end
  206. self.lastSendMsgTimeMap[key] = curTime
  207. return true
  208. end
  209. function PayMgr:RecordCurTime(key)
  210. if not self.lastSendMsgTimeMap then
  211. self.lastSendMsgTimeMap = {}
  212. end
  213. self.lastSendMsgTimeMap[key] = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  214. end
  215. function PayMgr:ClearCurPayData()
  216. self.curPayData = nil
  217. self.payState = PayState.Idle
  218. end
  219. function PayMgr:SdkPayResult(success)
  220. self.payState = PayState.SDKPayComplete
  221. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_CLIENT_COMPLETED, success)
  222. if not self.curPayData then
  223. -- 能到这里,说明已经收到了服务返回的支付结果,则不再做后续处理
  224. self:EndWaiting()
  225. self:ClearCurPayData()
  226. return
  227. end
  228. if success then
  229. self:StartWaiting()
  230. else
  231. self:EndWaiting()
  232. self:ClearCurPayData()
  233. end
  234. end
  235. function PayMgr:InterruptCurPay()
  236. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_CLIENT_COMPLETED, false)
  237. self:EndWaiting()
  238. self:ClearCurPayData()
  239. end
  240. function PayMgr:InitRechargeData(data)
  241. if not data then return nil end
  242. self.rechargeData.totalRecharge = CommonUtil.GetValidPayPrice(data.total_recharge or 0)
  243. self.rechargeData.dayRecharge = CommonUtil.GetValidPayPrice(data.day_recharge or 0)
  244. end
  245. --- 支付
  246. ---@param goodsType Enum.PayGoodsType 支付类型
  247. ---@param goodsId integer 支付商品Id
  248. ---@param goodsNum integer 支付数量
  249. ---@return Enum.InitPayErrorCode
  250. function PayMgr:Pay(goodsType, goodsId, goodsNum)
  251. if self.payState == PayState.Ordering or self.payState == PayState.Ordered then
  252. -- 处理超时,则丢弃上一次的支付
  253. if not self:IsCanSend(2, EXCPETION_WAIT_TIMESTAMP) then
  254. return Enum.InitPayErrorCode.Paying
  255. end
  256. elseif self.payState == PayState.SDKPaying then
  257. -- 处理超时,则丢弃上一次的支付
  258. if not self:IsCanSend(2, EXCPETION_WAIT_TIMESTAMP) then
  259. return Enum.InitPayErrorCode.Paying
  260. end
  261. end
  262. local success = self:SendGetPayInfo(goodsType, goodsId, goodsNum)
  263. if not success then
  264. return Enum.InitPayErrorCode.OperateFreq
  265. end
  266. return Enum.InitPayErrorCode.Success
  267. end
  268. function PayMgr:GetDayRecharge()
  269. return self.rechargeData and self.rechargeData.dayRecharge or 0
  270. end
  271. function PayMgr:GetTotalRecharge()
  272. return self.rechargeData and self.rechargeData.totalRecharge or 0
  273. end
  274. function PayMgr:CommonShowPayResult(itemList)
  275. if ManagerContainer.LuaUIMgr:IsBatting() then
  276. return
  277. end
  278. if not itemList then return end
  279. local itemLength = #itemList
  280. if itemLength > 0 then
  281. local addItemMap = {}
  282. local cfgId, addNum, item
  283. for i = 1, itemLength do
  284. item = itemList[i]
  285. cfgId = item.key
  286. addNum = item.value
  287. if addItemMap[cfgId] then
  288. addItemMap[cfgId] = addItemMap[cfgId] + addNum
  289. else
  290. addItemMap[cfgId] = addNum
  291. end
  292. end
  293. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_EQUIP_AND_ITEM_ADD, addItemMap)
  294. end
  295. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RANK_ACTIVITY_REWARD_SUCCESS_NTF)
  296. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.LIMIT_RECHARGE_PAY_SUCCESS_NTF)
  297. end
  298. function PayMgr:StartWaiting(time)
  299. if not self.waitTimer then
  300. self.waitTimer = Timer.New(slot(self.EndWaiting, self), time or EXCPETION_WAIT_TIME, 1, true)
  301. else
  302. self.waitTimer.time = time or EXCPETION_WAIT_TIME
  303. self.waitTimer.duration = time or EXCPETION_WAIT_TIME
  304. end
  305. if not self.waitTimer.running then
  306. self.waitTimer:Start()
  307. end
  308. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPayWaiting)
  309. end
  310. function PayMgr:EndWaiting()
  311. if self.waitTimer and self.waitTimer.running then
  312. self.waitTimer:Stop()
  313. end
  314. ManagerContainer.LuaUIMgr:ClosePage(Enum.UIPageName.UIPayWaiting)
  315. end
  316. ----------------------------- 以下接口是为每个支付方式单独提供支付传参API,这样外部就不用管传参不一样了 ------------------------
  317. --- 由服务器返回的支付结果,不一定为当前购买的结果(有延迟的数据)
  318. function PayMgr:ServerPayComplete(goodsType, goodsId, goodsNum, orderId, itemList)
  319. -- if goodsType == PayGoodsType.EPayType_Discount then
  320. -- elseif goodsType == PayGoodsType.EPayType_MonthCard then
  321. -- elseif goodsType == PayGoodsType.EPayType_NormalBag then
  322. -- elseif goodsType == PayGoodsType.EPayType_LimitBag then
  323. -- end
  324. self:CommonShowPayResult(itemList)
  325. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_SERVER_COMPLETED, true)
  326. if self.curPayData then
  327. if self.curPayData.goods_id ~= goodsId
  328. or self.curPayData.goods_type ~= goodsType
  329. or self.curPayData.count ~= goodsNum
  330. then
  331. return
  332. end
  333. if (self.curPayData.orderId and self.curPayData.orderId ~= 0)
  334. and (orderId and orderId ~= 0)
  335. and self.curPayData.orderId ~= orderId
  336. then
  337. return
  338. end
  339. end
  340. self.payState = PayState.ServerPayComplete
  341. self:EndWaiting()
  342. self:ClearCurPayData()
  343. end
  344. function PayMgr:GetInitPayErrorCodeLangKey(errorCode)
  345. if errorCode == Enum.InitPayErrorCode.OperateFreq then
  346. return 'PayError_Frequency'
  347. elseif errorCode == Enum.InitPayErrorCode.Paying then
  348. return 'PayError_Paying'
  349. elseif errorCode == Enum.InitPayErrorCode.ValidPay then
  350. return 'PayError_ValidPay'
  351. end
  352. return nil
  353. end
  354. function PayMgr:RuneShopPay(runeShopType, runeShopSubType, goodsId)
  355. if runeShopType == Enum.RuneShopType.MonthCard then
  356. return self:Pay(PayGoodsType.EPayType_MonthCard, goodsId, 1)
  357. elseif runeShopType == Enum.RuneShopType.Gifts then
  358. if runeShopSubType == Enum.RuneShopSubType.Daily then
  359. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  360. elseif runeShopSubType == Enum.RuneShopSubType.Week then
  361. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  362. elseif runeShopSubType == Enum.RuneShopSubType.Month then
  363. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  364. elseif runeShopSubType == Enum.RuneShopSubType.Gold then
  365. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  366. end
  367. elseif runeShopType == Enum.RuneShopType.LimitTime then
  368. return self:Pay(PayGoodsType.EPayType_LimitBag, goodsId, 1)
  369. elseif runeShopType == Enum.RuneShopType.GuildWar then
  370. return self:Pay(PayGoodsType.EPayType_GuildWar, goodsId, 1)
  371. elseif runeShopType == Enum.RuneShopType.IdolShop then
  372. return self:Pay(PayGoodsType.EPayType_IdolShop, goodsId, 1)
  373. elseif runeShopType == Enum.RuneShopType.SpecialPrivilege then
  374. return self:Pay(PayGoodsType.EPayType_SpecialPrivilege, goodsId, 1)
  375. elseif runeShopType == Enum.RuneShopType.PassCheck then
  376. return self:Pay(PayGoodsType.EPayType_PassCheck, goodsId, 1)
  377. end
  378. return Enum.InitPayErrorCode.ValidPay
  379. end
  380. --- 超值礼包
  381. function PayMgr:LimitedGiftPay(goodsId)
  382. return self:Pay(PayGoodsType.EPayType_Discount, goodsId, 1)
  383. end
  384. --冲榜
  385. function PayMgr:RankActivityPay(type, goodsId)
  386. if type == Enum.RankActivitiesType.ClimbingTower then
  387. return self:Pay(PayGoodsType.EPayType_RushTower, goodsId, 1)
  388. elseif type == Enum.RankActivitiesType.Dojo then
  389. return self:Pay(PayGoodsType.EPayType_RushArena, goodsId, 1)
  390. elseif type == Enum.RankActivitiesType.MapProgress then
  391. return self:Pay(PayGoodsType.EPayType_RushMap, goodsId, 1)
  392. elseif type == Enum.RankActivitiesType.Pet then
  393. return self:Pay(PayGoodsType.EPayType_RushPet, goodsId, 1)
  394. elseif type == Enum.RankActivitiesType.Skill then
  395. return self:Pay(PayGoodsType.EPayType_RushSkill, goodsId, 1)
  396. end
  397. end
  398. --悬赏战令
  399. function PayMgr:AirShipPay(goodsId)
  400. return self:Pay(PayGoodsType.EPayType_AirShipCash ,goodsId ,1 )
  401. end
  402. function PayMgr:BTHundredRechargePay(goodsId)
  403. return self:Pay(PayGoodsType.EPayType_BTRecharge100, goodsId ,1 )
  404. end
  405. return PayMgr