PayMgr.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PAY_ORDER_COMPLETE_ACK, self.OnPayOrdercompleteACK, self)
  65. end
  66. function PayMgr:UnRegisterNetEvents()
  67. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_INFO_GET_ACK)
  68. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_FOR_GOODS_NTF)
  69. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_INFO_NTF)
  70. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_INFO_ORDER_OK_LIST_GET_ACK)
  71. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PAY_ORDER_COMPLETE_ACK)
  72. end
  73. function PayMgr:OnGetPayInfoAck(data)
  74. LogError('[wboy] SC_PAY_INFO_GET_ACK ' .. Inspect(data))
  75. if not data then
  76. LogError('[wboy] message data is Error !!!')
  77. return
  78. end
  79. if not self.curPayData then
  80. LogError('[wboy] cur pay data is Error !!!')
  81. return
  82. end
  83. if self.curPayData.goods_id ~= data.goods_id
  84. or self.curPayData.goods_type ~= data.goods_type
  85. or self.curPayData.count ~= data.count
  86. then
  87. LogError('[wboy] message data and cur pay data is not same !!!')
  88. return
  89. end
  90. if ManagerContainer.NetManager:IsErrorData(data) then
  91. if data then
  92. if data.error == Enum.NetErrorCode.ERROR_PAY_PRODUCTION_MODE then
  93. -- 测试环境
  94. self:SdkPayResult(true)
  95. return
  96. elseif data.error == Enum.NetErrorCode.ERROR_PAY_GOOD_PRICE_FREE then
  97. -- 商品免费
  98. self:SdkPayResult(true)
  99. return
  100. end
  101. end
  102. self:InterruptCurPay()
  103. return
  104. end
  105. if self.payState ~= PayState.Ordering then return end
  106. self.payState = PayState.Ordered
  107. self.curPayData.orderId = data.cp_order_id
  108. self.payState = PayState.SDKPaying
  109. self:StartWaiting(3)
  110. self:SdkPayResult(false)
  111. -- 启动支付SDK
  112. local rmbCb = function ()
  113. LogError("============正常流程==============")
  114. --local strArr = string.split(data.goods_name,",")
  115. --local strArrCount = #strArr
  116. local cbUrl = data.goods_name
  117. local payType = -1
  118. -- if strArrCount >= 2 then
  119. -- cbUrl = strArr[1]
  120. -- payType = strArr[2] + 0
  121. -- end
  122. payType = ManagerContainer.OpenPayMgr:GetCurPayMode()
  123. --ManagerContainer.SDKEventReportMgr:SendEvent(26)
  124. ManagerContainer.SDKEventReportMgr:SendEvent(27)
  125. --ManagerContainer.OpenPayMgr:Pay(data.goods_id, data.goods_name, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), cbUrl,"ex")
  126. -- and ManagerContainer.OpenPayMgr.platform == "SDKYOUYI_IOS"
  127. if payType >=1 and UseOpenPay then
  128. ManagerContainer.OpenPayMgr:Pay(data.goods_id, data.goods_name, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), cbUrl,"ex")
  129. else
  130. ManagerContainer.LuaGameMgr:SdkPay(data.goods_id,cbUrl, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), nil)
  131. end
  132. end
  133. local dbCount = CommonUtil.GetOwnResCountByItemId(40001)
  134. if(data.amount >100) then
  135. dbCount = 0
  136. end
  137. local dbCb = function ()
  138. LogError("============代币流程==============")
  139. local iszg = dbCount >= math.floor(data.amount *100+0.0005)
  140. local cpid = data.cp_order_id
  141. if iszg then
  142. ManagerContainer.SDKEventReportMgr:SendEvent(27)
  143. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PAY_ORDER_COMPLETE_REQ, {cp_order_id = cpid})
  144. else
  145. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("dbPayError")
  146. end
  147. return iszg
  148. end
  149. if not UseDbPay or dbCount <= 0 then
  150. rmbCb()
  151. else
  152. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPayTips,
  153. {
  154. selectRmbCallback =rmbCb,
  155. selectDbCallback = dbCb,
  156. dbCount = dbCount,
  157. amount = data.amount,
  158. })
  159. end
  160. --ManagerContainer.OpenPayMgr:Pay(data.goods_id, data.goods_name, '感谢您的支付,祝你有个愉快的游戏体验', data.count, CommonUtil.GetValidPayPrice(data.amount), tostring(data.cp_order_id), data.goods_name,"ex")
  161. end
  162. -- ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_MD5_GET_REQ, {md5 = paaramsStr})
  163. function PayMgr:OnPayCompleteNtf(data)
  164. --LogError('[wboy] SC_PAY_FOR_GOODS_NTF ' .. Inspect(data))
  165. if ManagerContainer.NetManager:IsErrorData(data) then
  166. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_SERVER_COMPLETED, false)
  167. return
  168. end
  169. self:ServerPayComplete(data.goods_type, data.goods_id, data.goods_num, data.cp_order_id, data.item_list)
  170. end
  171. function PayMgr:OnPayInfoNtf(data)
  172. --LogError('[wboy] SC_PAY_INFO_NTF ' .. Inspect(data))
  173. if ManagerContainer.NetManager:IsErrorData(data) then
  174. return
  175. end
  176. local lastTotalRecharge = self.rechargeData.totalRecharge or 0
  177. local lastDayRecharge = self.rechargeData.dayRecharge or 0
  178. local totalRecharge = CommonUtil.GetValidPayPrice(data.total_recharge or 0)
  179. local dayRecharge = CommonUtil.GetValidPayPrice(data.day_recharge or 0)
  180. self.rechargeData.totalRecharge = totalRecharge
  181. self.rechargeData.dayRecharge = dayRecharge
  182. if totalRecharge ~= lastTotalRecharge then
  183. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_TOTAL_RECHARGE_CHANGED)
  184. if dayRecharge > lastDayRecharge then
  185. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_DAY_RECHARGE_CHANGED, false)
  186. else
  187. -- 当天儲值数量重置了
  188. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_DAY_RECHARGE_CHANGED, true)
  189. end
  190. else
  191. if dayRecharge ~= lastDayRecharge then
  192. -- 当天儲值数量重置了
  193. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_DAY_RECHARGE_CHANGED, true)
  194. end
  195. end
  196. end
  197. function PayMgr:OnStartUpPayCompleteAck(data)
  198. -- LogError('[wboy] SC_PAY_INFO_ORDER_OK_LIST_GET_ACK ' .. Inspect(data))
  199. if ManagerContainer.NetManager:IsErrorData(data) then
  200. return
  201. end
  202. self:CommonShowPayResult(data.reward_item_list)
  203. end
  204. function PayMgr:SendGetPayInfo(goodsType, goodsId, goodsNum)
  205. if not self:IsCanSend(1) then return false end
  206. self:ClearCurPayData()
  207. self.payState = PayState.Ordering
  208. local curPayData =
  209. {
  210. goods_type = goodsType,
  211. goods_id = goodsId,
  212. count = goodsNum,
  213. }
  214. self.curPayData = curPayData
  215. self:RecordCurTime(2)
  216. self:StartWaiting()
  217. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PAY_INFO_GET_REQ, curPayData)
  218. return true
  219. end
  220. function PayMgr:SendStartUpPayComplete()
  221. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PAY_INFO_ORDER_OK_LIST_GET_REQ)
  222. end
  223. function PayMgr:IsCanSend(key, cdTime)
  224. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  225. if not self.lastSendMsgTimeMap then
  226. self.lastSendMsgTimeMap = {}
  227. self.lastSendMsgTimeMap[key] = curTime
  228. return true
  229. end
  230. local lastTime = self.lastSendMsgTimeMap[key]
  231. if lastTime then
  232. local cd = cdTime or REQ_PAY_CD
  233. if (curTime - lastTime) < cd then
  234. return false
  235. end
  236. end
  237. self.lastSendMsgTimeMap[key] = curTime
  238. return true
  239. end
  240. function PayMgr:RecordCurTime(key)
  241. if not self.lastSendMsgTimeMap then
  242. self.lastSendMsgTimeMap = {}
  243. end
  244. self.lastSendMsgTimeMap[key] = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  245. end
  246. function PayMgr:ClearCurPayData()
  247. self.curPayData = nil
  248. self.payState = PayState.Idle
  249. end
  250. function PayMgr:SdkPayResult(success)
  251. self.payState = PayState.SDKPayComplete
  252. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_CLIENT_COMPLETED, success)
  253. if not self.curPayData then
  254. -- 能到这里,说明已经收到了服务返回的支付结果,则不再做后续处理
  255. self:EndWaiting()
  256. self:ClearCurPayData()
  257. return
  258. end
  259. if success then
  260. self:StartWaiting()
  261. else
  262. self:EndWaiting()
  263. self:ClearCurPayData()
  264. end
  265. end
  266. function PayMgr:InterruptCurPay()
  267. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_CLIENT_COMPLETED, false)
  268. self:EndWaiting()
  269. self:ClearCurPayData()
  270. end
  271. function PayMgr:InitRechargeData(data)
  272. if not data then return nil end
  273. self.rechargeData.totalRecharge = CommonUtil.GetValidPayPrice(data.total_recharge or 0)
  274. self.rechargeData.dayRecharge = CommonUtil.GetValidPayPrice(data.day_recharge or 0)
  275. end
  276. --- 支付
  277. ---@param goodsType Enum.PayGoodsType 支付类型
  278. ---@param goodsId integer 支付商品Id
  279. ---@param goodsNum integer 支付数量
  280. ---@return Enum.InitPayErrorCode
  281. function PayMgr:Pay(goodsType, goodsId, goodsNum)
  282. if self.payState == PayState.Ordering or self.payState == PayState.Ordered then
  283. -- 处理超时,则丢弃上一次的支付
  284. if not self:IsCanSend(2, EXCPETION_WAIT_TIMESTAMP) then
  285. return Enum.InitPayErrorCode.Paying
  286. end
  287. elseif self.payState == PayState.SDKPaying then
  288. -- 处理超时,则丢弃上一次的支付
  289. if not self:IsCanSend(2, EXCPETION_WAIT_TIMESTAMP) then
  290. return Enum.InitPayErrorCode.Paying
  291. end
  292. end
  293. local success = self:SendGetPayInfo(goodsType, goodsId, goodsNum)
  294. if not success then
  295. return Enum.InitPayErrorCode.OperateFreq
  296. end
  297. return Enum.InitPayErrorCode.Success
  298. end
  299. function PayMgr:GetDayRecharge()
  300. return self.rechargeData and self.rechargeData.dayRecharge or 0
  301. end
  302. function PayMgr:GetTotalRecharge()
  303. return self.rechargeData and self.rechargeData.totalRecharge or 0
  304. end
  305. function PayMgr:CommonShowPayResult(itemList)
  306. if ManagerContainer.LuaUIMgr:IsBatting() then
  307. return
  308. end
  309. if not itemList then return end
  310. local itemLength = #itemList
  311. if itemLength > 0 then
  312. local addItemMap = {}
  313. local cfgId, addNum, item
  314. for i = 1, itemLength do
  315. item = itemList[i]
  316. cfgId = item.key
  317. addNum = item.value
  318. if addItemMap[cfgId] then
  319. addItemMap[cfgId] = addItemMap[cfgId] + addNum
  320. else
  321. addItemMap[cfgId] = addNum
  322. end
  323. end
  324. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_EQUIP_AND_ITEM_ADD, addItemMap)
  325. end
  326. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RANK_ACTIVITY_REWARD_SUCCESS_NTF)
  327. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.LIMIT_RECHARGE_PAY_SUCCESS_NTF)
  328. end
  329. function PayMgr:StartWaiting(time)
  330. if not self.waitTimer then
  331. self.waitTimer = Timer.New(slot(self.EndWaiting, self), time or EXCPETION_WAIT_TIME, 1, true)
  332. else
  333. self.waitTimer.time = time or EXCPETION_WAIT_TIME
  334. self.waitTimer.duration = time or EXCPETION_WAIT_TIME
  335. end
  336. if not self.waitTimer.running then
  337. self.waitTimer:Start()
  338. end
  339. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPayWaiting)
  340. end
  341. function PayMgr:EndWaiting()
  342. if self.waitTimer and self.waitTimer.running then
  343. self.waitTimer:Stop()
  344. end
  345. ManagerContainer.LuaUIMgr:ClosePage(Enum.UIPageName.UIPayWaiting)
  346. end
  347. ----------------------------- 以下接口是为每个支付方式单独提供支付传参API,这样外部就不用管传参不一样了 ------------------------
  348. --- 由服务器返回的支付结果,不一定为当前购买的结果(有延迟的数据)
  349. function PayMgr:ServerPayComplete(goodsType, goodsId, goodsNum, orderId, itemList)
  350. -- if goodsType == PayGoodsType.EPayType_Discount then
  351. -- elseif goodsType == PayGoodsType.EPayType_MonthCard then
  352. -- elseif goodsType == PayGoodsType.EPayType_NormalBag then
  353. -- elseif goodsType == PayGoodsType.EPayType_LimitBag then
  354. -- end
  355. self:CommonShowPayResult(itemList)
  356. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PAY_SERVER_COMPLETED, true)
  357. if self.curPayData then
  358. if self.curPayData.goods_id ~= goodsId
  359. or self.curPayData.goods_type ~= goodsType
  360. or self.curPayData.count ~= goodsNum
  361. then
  362. return
  363. end
  364. if (self.curPayData.orderId and self.curPayData.orderId ~= 0)
  365. and (orderId and orderId ~= 0)
  366. and self.curPayData.orderId ~= orderId
  367. then
  368. return
  369. end
  370. end
  371. self.payState = PayState.ServerPayComplete
  372. self:EndWaiting()
  373. self:ClearCurPayData()
  374. end
  375. function PayMgr:GetInitPayErrorCodeLangKey(errorCode)
  376. if errorCode == Enum.InitPayErrorCode.OperateFreq then
  377. return 'PayError_Frequency'
  378. elseif errorCode == Enum.InitPayErrorCode.Paying then
  379. return 'PayError_Paying'
  380. elseif errorCode == Enum.InitPayErrorCode.ValidPay then
  381. return 'PayError_ValidPay'
  382. end
  383. return nil
  384. end
  385. function PayMgr:RuneShopPay(runeShopType, runeShopSubType, goodsId)
  386. if runeShopType == Enum.RuneShopType.MonthCard then
  387. return self:Pay(PayGoodsType.EPayType_MonthCard, goodsId, 1)
  388. elseif runeShopType == Enum.RuneShopType.Gifts then
  389. if runeShopSubType == Enum.RuneShopSubType.Daily then
  390. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  391. elseif runeShopSubType == Enum.RuneShopSubType.Week then
  392. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  393. elseif runeShopSubType == Enum.RuneShopSubType.Month then
  394. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  395. elseif runeShopSubType == Enum.RuneShopSubType.Gold then
  396. return self:Pay(PayGoodsType.EPayType_NormalBag, goodsId, 1)
  397. end
  398. elseif runeShopType == Enum.RuneShopType.LimitTime then
  399. return self:Pay(PayGoodsType.EPayType_LimitBag, goodsId, 1)
  400. elseif runeShopType == Enum.RuneShopType.GuildWar then
  401. return self:Pay(PayGoodsType.EPayType_GuildWar, goodsId, 1)
  402. elseif runeShopType == Enum.RuneShopType.IdolShop then
  403. return self:Pay(PayGoodsType.EPayType_IdolShop, goodsId, 1)
  404. elseif runeShopType == Enum.RuneShopType.SpecialPrivilege then
  405. return self:Pay(PayGoodsType.EPayType_SpecialPrivilege, goodsId, 1)
  406. elseif runeShopType == Enum.RuneShopType.PassCheck then
  407. return self:Pay(PayGoodsType.EPayType_PassCheck, goodsId, 1)
  408. end
  409. return Enum.InitPayErrorCode.ValidPay
  410. end
  411. --- 超值礼包
  412. function PayMgr:LimitedGiftPay(goodsId)
  413. return self:Pay(PayGoodsType.EPayType_Discount, goodsId, 1)
  414. end
  415. --冲榜
  416. function PayMgr:RankActivityPay(type, goodsId)
  417. if type == Enum.RankActivitiesType.ClimbingTower then
  418. return self:Pay(PayGoodsType.EPayType_RushTower, goodsId, 1)
  419. elseif type == Enum.RankActivitiesType.Dojo then
  420. return self:Pay(PayGoodsType.EPayType_RushArena, goodsId, 1)
  421. elseif type == Enum.RankActivitiesType.MapProgress then
  422. return self:Pay(PayGoodsType.EPayType_RushMap, goodsId, 1)
  423. elseif type == Enum.RankActivitiesType.Pet then
  424. return self:Pay(PayGoodsType.EPayType_RushPet, goodsId, 1)
  425. elseif type == Enum.RankActivitiesType.Skill then
  426. return self:Pay(PayGoodsType.EPayType_RushSkill, goodsId, 1)
  427. end
  428. end
  429. --悬赏战令
  430. function PayMgr:AirShipPay(goodsId)
  431. return self:Pay(PayGoodsType.EPayType_AirShipCash ,goodsId ,1 )
  432. end
  433. function PayMgr:BTHundredRechargePay(goodsId)
  434. return self:Pay(PayGoodsType.EPayType_BTRecharge100, goodsId ,1 )
  435. end
  436. function PayMgr:OnPayOrdercompleteACK(data)
  437. --SC_PAY_ORDER_COMPLETE_ACK
  438. if data.error>0 then
  439. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("dbPayError")
  440. end
  441. end
  442. return PayMgr