rechargemod.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. --玩家充值处理
  2. local skynet = require "skynet"
  3. local logger = require "logger"
  4. local asset = require "model.asset"
  5. local schema = require "model.schema"
  6. local common_fun = require "model.common_fun"
  7. local reward = require "model.reward"
  8. local httpc = require "http.httpc"
  9. local cjson = require "cjson"
  10. local stringify = require "stringify"
  11. local log = require "model.log"
  12. local shengtian
  13. local logger_info = logger.info
  14. local function deal_num(num)
  15. local num1 = tonumber(string.format("%.02f", num))
  16. local num2 = tonumber(string.format("%.1f", num))
  17. local num3 = math.floor(num)
  18. if math.abs(num1 - num2) >= 0.01 then
  19. return num1
  20. end
  21. if math.abs(num2 - num3) >= 0.1 then
  22. return num2
  23. end
  24. return num3
  25. end
  26. local skynet_call = skynet.call
  27. local REQUEST = {}
  28. local CMD = {}
  29. local recharge = {}
  30. local rechargeser
  31. local _M = schema.new('rechargedata', {
  32. })
  33. function recharge.list_request_interests() return REQUEST end
  34. function recharge.list_command_interests() return CMD end
  35. function recharge.parse(character)
  36. local d = _M.load(character)
  37. end
  38. -- TODO: 充值成功推送给客户端
  39. local function recharge_succeed(character,orderid, info)
  40. logger.trace(" ######### orderid %s", orderid)
  41. local ok, info = pcall(function()
  42. character.send("recharge_succeed", {
  43. orderid = orderid,
  44. })
  45. -- log
  46. log.recharge(character, info.pay_price,info) -- 数据统计
  47. end)
  48. if not ok then
  49. logger.trace(" ## 通知客户端充值成功呢异常 %s", info)
  50. end
  51. end
  52. function recharge.monitor(character)
  53. --玩家充值时间
  54. character.monitor("recharge", function(_, cfid, orderid, moneynum, appname, giftid)
  55. logger_info("玩家充值了礼包:%d, cfid:%s, orderid:%s",giftid, cfid, orderid)
  56. local conf = asset.GiftConfig_proto[giftid]
  57. if not conf or conf.spend ~= cfid then
  58. logger.error("recharge err, orderid:%s, giftid:%d", orderid, giftid)
  59. end
  60. character.dispatch("recharge.money", cfid, orderid, moneynum, giftid, conf.type, conf.sort)
  61. skynet_call(rechargeser,"lua", "orderid_refresh", character.uid, orderid)
  62. -- 充值金额大于零, 修改玩家充值总金额数量 -- 玩家金额统计
  63. if moneynum > 0 then
  64. character.set_money(character.money + moneynum)
  65. end
  66. log.recharge(character, moneynum , {
  67. pay_platform = character.platform,
  68. pay_orderID = orderid,
  69. pay_id = cfid,
  70. pay_price = moneynum,
  71. pay_level = character.level,
  72. pay_time = os.date("%Y-%m-%d %H:%M:%S"),
  73. id = character.uid,
  74. })
  75. character.send("recharge_success", {
  76. id = orderid,
  77. })
  78. -- local MONEY_TYPE = assert(asset.variable_proto[1851].data1)
  79. -- local MONEY_TYPE = 1
  80. -- recharge_succeed(character,orderid,{ --PK3-1085
  81. -- pay_platform = character.platform,
  82. -- pay_orderID = orderid,
  83. -- pay_id = cfid,
  84. -- pay_type = MONEY_TYPE,
  85. -- pay_price = moneynum*copies, --多份购买
  86. -- pay_configtype = MONEY_TYPE,
  87. -- pay_configprice = moneynum*copies, --多份购买
  88. -- pay_level = character.level,
  89. -- pay_time = os.date("%Y-%m-%d %H:%M:%S",now),
  90. -- id = character.uid,
  91. -- })
  92. end)
  93. character.monitor("recharge.money",function(_, cfid, orderid, moneynum, giftid, modleid)
  94. -- 钻石商店自己发奖
  95. if modleid == MODULE_ID_DIA_SHOP then
  96. return
  97. end
  98. local conf = asset.GiftConfig_proto[giftid]
  99. local award_list
  100. if conf.award and conf.award > 0 then
  101. local errno = 0
  102. errno, award_list = common_fun.get_award(conf.award, "NewawardConfig_proto")
  103. if errno ~= 0 then
  104. logger.warn("计费点奖励异常, errno:%d, giftid:%d, awardid:%d", errno, giftid, conf.award)
  105. return
  106. end
  107. end
  108. award_list = award_list or {}
  109. for i = 1, #conf.library, 2 do
  110. local item_id = conf.library[i]
  111. local item_num = conf.library[i+1]
  112. if item_id and item_num then
  113. table.insert(award_list, {id = item_id, num = item_num})
  114. end
  115. end
  116. -- 发奖励
  117. local desc = {
  118. module="recharge",
  119. brief="充值礼包",
  120. context= "充值礼包:"..giftid,
  121. mailgen={subject=2, body=""},
  122. notify={
  123. flags="recharge_money"
  124. },
  125. detail=award_list,
  126. must_mail = true
  127. }
  128. reward(character, desc)
  129. end)
  130. end
  131. -- function recharge.create_order(character, args)
  132. -- -- 检查具体的档位 等等
  133. -- -- 暂时无需要, 不开发
  134. -- local cfid = args.cfid
  135. -- local act_id = args.act_id or 0
  136. -- local act_type = args.act_type or 0
  137. -- local goods_id = args.goods_id or 0
  138. -- local login_args = character.login_args
  139. -- local platform = login_args.platform
  140. -- local appname = login_args.appname
  141. -- local conf = util.recharge_conf(cfid)
  142. -- local errno = check_rechage_limit(character, conf[MONEY_STR])
  143. -- if errno ~= 0 then
  144. -- return 0, {reason = errno}
  145. -- end
  146. -- local EDITOR_MODEL = true
  147. -- if (character.channel == 'editor' and LAUNCH.private_gm) or LAUNCH.debug_model then
  148. -- skynet.fork(function()
  149. -- skynet.sleep(0)
  150. -- logger.trace("启动editor模式充值")
  151. -- local conf = util.recharge_conf(cfid)
  152. -- local temp = {
  153. -- orderid = "editor_orderid",
  154. -- cfid = cfid,
  155. -- act_id = act_id,
  156. -- act_type = act_type,
  157. -- goods_id = goods_id,
  158. -- moneynum = conf[MONEY_STR]
  159. -- }
  160. -- character.dispatch("recharge.money", temp)
  161. -- end)
  162. -- return 0, {reason = 0}
  163. -- local ok,herrno,info = pcall(httpc.post, RECHARGE_SERVICE, '/getorderid', {
  164. -- userid = character.account, -- 平台或渠道的账号id
  165. -- serverid = character.sid, -- 游戏服务器id
  166. -- uid = character.uid, -- 游戏内的账号id
  167. -- platform = platform, -- 平台id(Andorid、iOS)
  168. -- channel = character.channel, -- 渠道标识
  169. -- product_id = cfid, -- 计费点名称(用的计费点id)
  170. -- remarks = 'remarks', -- ,
  171. -- appname = appname or 'appname', -- 玩家使用的包
  172. -- act_id = act_id or 0, -- 活动唯一id
  173. -- act_type = act_type or 0, -- 活动类型
  174. -- goods_id = goods_id or 0, -- 商品对应配置id
  175. -- })
  176. -- -- logger.trace("########## ok:%s, info:%s, info2:%s", ok, herrno or 'nil', info or 'nil')
  177. -- if herrno ~= 200 then
  178. -- return 0, {reason = STD_ERR.RECHARGE_CREATE_HTTPE_ERR} -- http请求异常
  179. -- end
  180. -- -- info 解码
  181. -- local order = cjson.decode(info)
  182. -- -- logger.trace("充值系统 返回"..stringify(order))
  183. -- if order.erron ~= 0 then
  184. -- logger.trace("[充值系统] 订单创建失败 %s", stringify(order))
  185. -- return 0, {reason = STD_ERR.RECHARGE_CREATE_FAIL} -- 订单创建失败
  186. -- end
  187. -- -- skynet.fork(function( )
  188. -- -- skynet.sleep(300)
  189. -- -- -- MODULE.test_recharge(character, order.desc)
  190. -- -- end)
  191. -- return 0, {orderid = order.desc, goods_id = cfid}
  192. -- end
  193. function recharge.get_orderid(character, giftid)
  194. local conf = asset.GiftConfig_proto[giftid]
  195. if not conf then
  196. return 0
  197. end
  198. local cfid = conf.spend
  199. if not cfid then
  200. return 0
  201. end
  202. local orderid = "1"
  203. local recharge_conf = asset.RechargeConfig_proto[cfid]
  204. if not recharge_conf then
  205. return 0
  206. end
  207. local moneytype = asset.DataConfig_proto[19].data1
  208. local moneynum
  209. if moneytype == 1 then
  210. moneynum = recharge_conf.my
  211. elseif moneytype == 2 then
  212. moneynum = recharge_conf.rmb
  213. elseif moneytype == 3 then
  214. moneynum = recharge_conf.tw
  215. elseif moneytype == 4 then
  216. moneynum = recharge_conf.hk
  217. end
  218. if LAUNCH.test_recharge then
  219. character.dispatch("recharge.money", cfid, orderid, moneynum, giftid, conf.type, conf.sort)
  220. return
  221. end
  222. local login_args = character.login_args
  223. local platform = login_args.platform
  224. local appname = login_args.appname
  225. local ok,herrno,info = pcall(httpc.post, RECHARGE_SERVICE, '/getorderid', {
  226. userid = character.account, -- 平台或渠道的账号id
  227. serverid = character.sid, -- 游戏服务器id
  228. uid = character.uid, -- 游戏内的账号id
  229. platform = platform, -- 平台id(Andorid、iOS)
  230. channel = character.channel, -- 渠道标识
  231. product_id = giftid, -- 礼包id
  232. cfid = cfid, -- 计费点id
  233. remarks = 'remarks', -- ,
  234. appname = appname or 'appname', -- 玩家使用的包
  235. act_id = conf.type,
  236. })
  237. if herrno ~= 200 then
  238. logger.trace("[充值系统] 订单创建失败.errno %s", herrno)
  239. return 0
  240. end
  241. -- info 解码
  242. local order = cjson.decode(info)
  243. if order.erron ~= 0 then
  244. logger.trace("[充值系统] 订单创建失败 %s", stringify(order))
  245. return 0
  246. end
  247. local now = os.time()
  248. local ret = {
  249. id = order.desc,
  250. cfid = cfid,
  251. time = now
  252. }
  253. if character.channel == "shengtian" then
  254. shengtian = shengtian or require "service.loginserver.shengtian"
  255. local content = {
  256. fuse_uid = character.account,
  257. order_id = order.desc,
  258. product_id = conf.association,
  259. product_name = conf.name,
  260. price = deal_num(moneynum/MONEY_PARAM),
  261. role_id = character.uid,
  262. role_name = character.nickname,
  263. role_level = character.level,
  264. server_code = character.sid,
  265. server_name = character.sid,
  266. extension = character.login_args.extension,
  267. time = now
  268. }
  269. ret.sign = shengtian.recharge_sign(content)
  270. ret.cfid = giftid
  271. end
  272. logger.trace("[充值系统] 订单创建成功"..stringify(order))
  273. character.send("create_order_nty", ret)
  274. return 0
  275. end
  276. function recharge.launch(character)
  277. rechargeser = rechargeser or skynet.localname(".recharge")
  278. end
  279. function recharge.ready(character)
  280. end
  281. function recharge.saybye(character)
  282. end
  283. local function get_accesstoken(character)
  284. local host = "https://accounts.google.com"
  285. local url = "/o/oauth2/token"
  286. local grant_type = "refresh_token"
  287. local refresh_token = "1//0e3sYXF6c7hfMCgYIARAAGA4SNwF-L9IrVKAk6l2BMnTqOTqTxZ6_6Sy7y5LHfKUYqXQFvHuOu8WUuTq31l1kGTeLvylvo-hg8pc"
  288. local client_id = "1053688457546-djajb15ii7v0q6pjsh7361fpsh97353g.apps.googleusercontent.com"
  289. local client_secret = "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"
  290. local ok,herrno,info = pcall(httpc.post, host, url, {
  291. grant_type = grant_type,
  292. refresh_token = refresh_token,
  293. client_id = client_id,
  294. client_secret = client_secret,
  295. })
  296. logger.trace("get_accesstoken ok:%s, herrno:%s, info:%s", ok, herrno or 'nil', info or 'nil')
  297. if herrno ~= 200 then
  298. return nil
  299. end
  300. -- info 解码
  301. local order = cjson.decode(info)
  302. logger.trace("get_accesstoken 返回"..stringify(order))
  303. return order.access_token
  304. end
  305. function REQUEST.recharge_result(character, args)
  306. local access_token = get_accesstoken(character)
  307. if not access_token then
  308. return {errno = 1} -- 系统异常
  309. end
  310. local str = "/androidpublisher/v3/applications/%s/purchases/products/%s/tokens/%s?access_token=%s"
  311. local packageName = "com.blackart.b22"
  312. local productId = args.cfg_id
  313. local token = args.purchase_token
  314. local url = "https://androidpublisher.googleapis.com"
  315. local from = string.format(str, packageName, productId, token, access_token)
  316. local ok,herrno,inapp_purchase_data = pcall(httpc.get, url, from)
  317. logger.trace("########## ok:%s, herrno:%s, inapp_purchase_data:%s", ok, herrno or 'nil', inapp_purchase_data or 'nil')
  318. if herrno ~= 200 then
  319. return {errno = 2} -- 未找到订单
  320. end
  321. -- info 解码
  322. local order = cjson.decode(inapp_purchase_data)
  323. logger.trace("recharge_result 返回"..stringify(order))
  324. if order.purchaseState ~= 0 then
  325. return {errno = 3} -- 未购买
  326. end
  327. if not order.orderId then
  328. return {errno = 4} -- 未找到订单id
  329. end
  330. local ok,herrno,info = pcall(httpc.post, RECHARGE_SERVICE, '/googleplay', {
  331. orderid = args.order_id, -- 订单id
  332. userid = character.account, -- 平台或渠道的账号id
  333. serverid = character.sid, -- 游戏服务器id
  334. uid = character.uid, -- 游戏内的账号id
  335. channel = character.channel, -- 渠道标识
  336. cfid = args.cfg_id, -- 计费点名称(用的计费点id)
  337. channel_order_id = order.orderId -- 渠道订单id
  338. })
  339. logger.trace("googleplay ok:%s, herrno:%s, info:%s", ok, herrno or 'nil', info or 'nil')
  340. if herrno ~= 200 then
  341. return {errno = 2} -- 未找到订单
  342. end
  343. return {errno = 0}
  344. end
  345. return recharge