Commit 1d83eec2aee0263591206ce9571665f3b8e9c7b3

Authored by zhouhaihai
2 parents e4a17692 d705a315

Merge branch 'bugfix' into develop

* bugfix:
  充值 整理
@@ -12,6 +12,8 @@ R_RUNE = "role:%d:rune:%d" -- 符文详细信息 @@ -12,6 +12,8 @@ R_RUNE = "role:%d:rune:%d" -- 符文详细信息
12 R_EMAIL = "role:%d:emailIds" --邮件列表 12 R_EMAIL = "role:%d:emailIds" --邮件列表
13 R_EMAIL_ITEM = "email:%d:%d" --邮件 13 R_EMAIL_ITEM = "email:%d:%d" --邮件
14 R_STORE = "role:%d:store" -- 商店 14 R_STORE = "role:%d:store" -- 商店
  15 +R_ORDERS = "role:%d:orders" -- 订单
  16 +R_ORDER = "order:%d:%d"
15 17
16 18
17 -- rank 19 -- rank
src/actions/StoreAction.lua
@@ -3,34 +3,6 @@ local _M = {} @@ -3,34 +3,6 @@ local _M = {}
3 local serverId = tonumber(skynet.getenv("servId")) 3 local serverId = tonumber(skynet.getenv("servId"))
4 local md5 = require "md5" 4 local md5 = require "md5"
5 5
6 -local function makeOrder(role, rechargeId)  
7 - local roleId = role:getProperty("id")  
8 - local rechargeData = csvdb["shop_rechargeCsv"][rechargeId]  
9 - if not rechargeData then  
10 - skynet.error("recharge id not exist", rechargeId)  
11 - return ""  
12 - end  
13 - local limit = rechargeData.limit  
14 - local rechargeRecord = role:getProperty("payR") or {}  
15 - if limit ~= 0 and limit <= (rechargeRecord[rechargeId] or 0) then  
16 - skynet.error(string.format("recharge id:%d count over limit, user id:%d", rechargeId, roleId))  
17 - return ""  
18 - end  
19 -  
20 - local orderId = redisproxy:hincrby("autoincrement_set", "order", 1)  
21 - local partnerOrderId = string.format("%d_%d_%d", serverId, roleId, orderId)  
22 - local orderKey = string.format("order:%d:%d", roleId, orderId)  
23 - redisproxy:del(orderKey)  
24 - local order = require("models.Order").new({  
25 - key = orderKey,  
26 - order = partnerOrderId,  
27 - rechargeId = rechargeId,  
28 - })  
29 - order:create()  
30 - redisproxy:sadd(string.format("role:%d:orders", roleId), partnerOrderId)  
31 - return partnerOrderId  
32 -end  
33 -  
34 -- 入口在正式服关闭 -- mock 充值 6 -- 入口在正式服关闭 -- mock 充值
35 function _M.rechargeRpc(agent , data) 7 function _M.rechargeRpc(agent , data)
36 local role = agent.role 8 local role = agent.role
@@ -41,7 +13,7 @@ function _M.rechargeRpc(agent , data) @@ -41,7 +13,7 @@ function _M.rechargeRpc(agent , data)
41 local roleId = role:getProperty("id") 13 local roleId = role:getProperty("id")
42 14
43 --创建订单号 15 --创建订单号
44 - local partnerOrderId = makeOrder(role, id) 16 + local partnerOrderId = role:getPurchaseOrder(id)
45 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId })) 17 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId }))
46 18
47 19
@@ -90,7 +62,7 @@ function _M.googleRechargeRpc(agent, data) @@ -90,7 +62,7 @@ function _M.googleRechargeRpc(agent, data)
90 62
91 role.ignoreHeartbeat = true 63 role.ignoreHeartbeat = true
92 --创建订单号 64 --创建订单号
93 - local partnerOrderId = makeOrder(role, id) 65 + local partnerOrderId = role:getPurchaseOrder(id)
94 -- 签名 66 -- 签名
95 local secret_key = "b7657fa7ccd44c16a35e3f454ac7a075" 67 local secret_key = "b7657fa7ccd44c16a35e3f454ac7a075"
96 local need = { 68 local need = {
@@ -117,7 +89,7 @@ function _M.myCardRechargeRpc(agent, data) @@ -117,7 +89,7 @@ function _M.myCardRechargeRpc(agent, data)
117 89
118 role.ignoreHeartbeat = true 90 role.ignoreHeartbeat = true
119 --创建订单号 91 --创建订单号
120 - local partnerOrderId = makeOrder(role, id) 92 + local partnerOrderId = role:getPurchaseOrder(id)
121 -- 签名 93 -- 签名
122 local secret_key = "48759e07540f46d9af17ec82669b4272" 94 local secret_key = "48759e07540f46d9af17ec82669b4272"
123 local need = { 95 local need = {
@@ -143,7 +115,7 @@ function _M.iosRechargeRpc(agent, data) @@ -143,7 +115,7 @@ function _M.iosRechargeRpc(agent, data)
143 115
144 role.ignoreHeartbeat = true 116 role.ignoreHeartbeat = true
145 --创建订单号 117 --创建订单号
146 - local partnerOrderId = makeOrder(role, id) 118 + local partnerOrderId = role:getPurchaseOrder(id)
147 -- 签名 119 -- 签名
148 local secret_key = "9647d2efe1074c73b9ac19af4337a70e" 120 local secret_key = "9647d2efe1074c73b9ac19af4337a70e"
149 local need = { 121 local need = {
@@ -167,43 +139,15 @@ function _M.purchaseOrderResult(agent, data) @@ -167,43 +139,15 @@ function _M.purchaseOrderResult(agent, data)
167 139
168 role.ignoreHeartbeat = false 140 role.ignoreHeartbeat = false
169 141
170 - local partnerOrderStr = msg.order  
171 - local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")  
172 - local orderObject = require("models.Order").new({ key = string.format("order:%d:%d", roleId, orderId) })  
173 - if not orderObject:load() then  
174 - -- 订单不存在  
175 - skynet.error("cancelPurchaseRpc", string.format("order %s not exist", partnerOrderStr))  
176 - return true  
177 - end  
178 -  
179 - if msg.status == "success" then  
180 - orderObject:setProperty("transactionId", msg.platformOrder or "")  
181 - local rechargeId = orderObject:getProperty("rechargeId")  
182 - local dataSet = csvdb["shop_rechargeCsv"][rechargeId]  
183 -  
184 - role:log("setOrder", {  
185 - order_status = 100, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他"  
186 - item_id = rechargeId, -- 道具id  
187 - item_type = dataSet.type, -- 购买的道具类型,具体见"onItems"方法中道具类型枚举表  
188 - item_name = dataSet.title, -- 购买的道具名  
189 - item_number = 1, -- 购买的道具数量  
190 - item_level = 1, -- 购买的道具等级  
191 - order_cost = dataSet.rmb * 100, -- 此次消费的现金金额(单位:分),如 51800即未518元,对应客户端SDK传入的'total_fee'  
192 - order_currency = "CNY", -- 货币类型,默认为"CNY"(人民币),遵循ISO 4217规范  
193 - order_type = role:getProperty("rmbC") > 0 and 0 or 1, -- 订单类型,首充记录为1,否则为0  
194 - order_id = msg.platformOrder, -- 本条记录的订单号,对应客户端SDK返回的'bs_trade_no'  
195 - })  
196 -  
197 - return true  
198 - end 142 + local status = {
  143 + fail = true,
  144 + success = true
  145 + }
199 146
200 - if orderObject:getProperty("finishTime") > 0 then  
201 - return true 147 + local partnerOrderStr = msg.order
  148 + if partnerOrderStr then
  149 + role:updatePurchaseOrder(partnerOrderStr, msg.platformOrder, status[msg.status] and msg.status or "unknown")
202 end 150 end
203 -  
204 - orderObject:setProperty("status", msg.status)  
205 -  
206 - redisproxy:srem(string.format("role:%d:orders", roleId), partnerOrderStr)  
207 return true 151 return true
208 end 152 end
209 153
src/models/RolePlugin.lua
@@ -1534,6 +1534,102 @@ function RolePlugin.bind(Role) @@ -1534,6 +1534,102 @@ function RolePlugin.bind(Role)
1534 self:updateProperty({field = "redp", value = redp}) 1534 self:updateProperty({field = "redp", value = redp})
1535 end 1535 end
1536 1536
  1537 + -- 获取充值订单号
  1538 + function Role:getPurchaseOrder(rechargeId)
  1539 + local roleId = self:getProperty("id")
  1540 + local rechargeData = csvdb["shop_rechargeCsv"][rechargeId]
  1541 + if not rechargeData then
  1542 + skynet.error("recharge id not exist", rechargeId)
  1543 + return ""
  1544 + end
  1545 + local limit = rechargeData.limit
  1546 + local rechargeRecord = self:getProperty("payR") or {}
  1547 + if limit ~= 0 and limit <= (rechargeRecord[rechargeId] or 0) then
  1548 + return ""
  1549 + end
  1550 +
  1551 + local orderId = redisproxy:hget(string.format(R_ORDERS, roleId), rechargeId)
  1552 + if orderId then
  1553 + local orderObject = require("models.Order").new({ key = string.format(R_ORDER, roleId, orderId) })
  1554 + if orderObject:load() and orderObject:getProperty("rechargeId") == rechargeId and math.abs(skynet.timex() - orderObject:getProperty("createTime")) < 5 * 60 then
  1555 + return string.format("%d_%d_%d", serverId, roleId, orderId)
  1556 + end
  1557 + end
  1558 +
  1559 + orderId = redisproxy:hincrby("autoincrement_set", "order", 1)
  1560 + local partnerOrderId = string.format("%d_%d_%d", serverId, roleId, orderId)
  1561 + local orderKey = string.format(R_ORDER, roleId, orderId)
  1562 + redisproxy:del(orderKey) -- 删掉可能有了
  1563 + local order = require("models.Order").new({
  1564 + key = orderKey,
  1565 + order = partnerOrderId,
  1566 + rechargeId = rechargeId,
  1567 + })
  1568 + order:create()
  1569 + -- 正在进行中的订单 缓存
  1570 + redisproxy:hset(string.format(R_ORDERS, roleId), rechargeId, orderId)
  1571 + return partnerOrderId
  1572 + end
  1573 +
  1574 + -- 更新订单信息
  1575 + --[[
  1576 +
  1577 + status
  1578 +
  1579 + success
  1580 + fail
  1581 + finsh
  1582 + unknow
  1583 +
  1584 + --]]
  1585 + function Role:updatePurchaseOrder(partnerOrderStr, platformOrder, status)
  1586 + if not partnerOrderStr then return false end
  1587 + local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")
  1588 +
  1589 + local roleId = self:getProperty("id")
  1590 + local orderObject = require("models.Order").new({ key = string.format(R_ORDER, roleId, orderId) })
  1591 + if not orderObject:load() then
  1592 + return false
  1593 + end
  1594 +
  1595 + local rechargeId = orderObject:getProperty("rechargeId")
  1596 + local dataSet = csvdb["shop_rechargeCsv"][rechargeId]
  1597 +
  1598 + if orderObject:getProperty("finishTime") > 0 then
  1599 + return false, "finsh"
  1600 + end
  1601 +
  1602 + if msg.platformOrder then
  1603 + orderObject:setProperty("transactionId", msg.platformOrder)
  1604 + end
  1605 + orderObject:setProperty("status", msg.status)
  1606 +
  1607 + -- 开始下单
  1608 + if status == "success" then
  1609 + elseif status == "fail" then
  1610 + redisproxy:hdel(string.format(R_ORDERS, roleId), orderId)
  1611 + elseif status == "finsh" then
  1612 + orderObject:setProperty("finishTime", skynet.time())
  1613 + redisproxy:hdel(string.format(R_ORDERS, roleId), orderId)
  1614 + end
  1615 +
  1616 + if status ~= "unknow" then
  1617 + self:log("setOrder", {
  1618 + order_status = ({success = 100, finsh = 200, fail = 300})[status] or 1000, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他"
  1619 + item_id = rechargeId, -- 道具id
  1620 + item_type = dataSet.type, -- 购买的道具类型,具体见"onItems"方法中道具类型枚举表
  1621 + item_name = dataSet.title, -- 购买的道具名
  1622 + item_number = 1, -- 购买的道具数量
  1623 + item_level = 1, -- 购买的道具等级
  1624 + order_cost = dataSet.rmb * 100, -- 此次消费的现金金额(单位:分),如 51800即未518元,对应客户端SDK传入的'total_fee'
  1625 + order_currency = "CNY", -- 货币类型,默认为"CNY"(人民币),遵循ISO 4217规范
  1626 + order_type = self:getProperty("rmbC") > 0 and 0 or 1, -- 订单类型,首充记录为1,否则为0
  1627 + order_id = platformOrder, -- 本条记录的订单号,对应客户端SDK返回的'bs_trade_no'
  1628 + })
  1629 + end
  1630 +
  1631 + return true, rechargeId
  1632 + end
1537 1633
1538 -- 充值 -- 1634 -- 充值 --
1539 --[[ 1635 --[[
@@ -1548,63 +1644,37 @@ function RolePlugin.bind(Role) @@ -1548,63 +1644,37 @@ function RolePlugin.bind(Role)
1548 local roleId = self:getProperty("id") 1644 local roleId = self:getProperty("id")
1549 local partnerOrderStr = params.order 1645 local partnerOrderStr = params.order
1550 1646
1551 - local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")  
1552 - local orderObject = require("models.Order").new({ key = string.format("order:%d:%d", roleId, orderId) })  
1553 - if not orderObject:load() then  
1554 - -- 订单不存在  
1555 - skynet.error("ayncPurchaseRpc", string.format("order %s not exist", partnerOrderStr))  
1556 - return  
1557 - end  
1558 1647
1559 - if orderObject:getProperty("finishTime") > 0 then  
1560 - -- 订单已经处理  
1561 - SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ result = "handled" })) 1648 + local status, back = self:updatePurchaseOrder(partnerOrderStr, params.transactionId, "finsh")
  1649 + if not status then
  1650 + if back == "finsh" then
  1651 + -- 订单已经处理
  1652 + SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ result = "handled" }))
  1653 + end
1562 return 1654 return
1563 end 1655 end
1564 - local rechargeId = orderObject:getProperty("rechargeId") 1656 + local rechargeId = back
1565 local rechargeData = csvdb["shop_rechargeCsv"][rechargeId] 1657 local rechargeData = csvdb["shop_rechargeCsv"][rechargeId]
1566 if rechargeData.rmb ~= tonumber(params.amount) then 1658 if rechargeData.rmb ~= tonumber(params.amount) then
1567 - skynet.error(string.format("fake order: %s, roleId: %d, order: %s, rmb %s, get %s", 1659 + skynet.error(string.format("[recharge] fake order: %s, roleId: %d, order: %s, rmb %s, get %s",
1568 params.transactionId, roleId, partnerOrderStr, rechargeData.rmb, params.amount 1660 params.transactionId, roleId, partnerOrderStr, rechargeData.rmb, params.amount
1569 )) 1661 ))
1570 return 1662 return
1571 end 1663 end
1572 1664
1573 - local order_type = self:getProperty("rmbC") > 0 and 0 or 1  
1574 local status, reward = self:recharge({ 1665 local status, reward = self:recharge({
1575 id = rechargeId, 1666 id = rechargeId,
1576 transactionId = params.transactionId, 1667 transactionId = params.transactionId,
1577 pay_time = params.pay_time, 1668 pay_time = params.pay_time,
1578 order = partnerOrderStr, 1669 order = partnerOrderStr,
1579 }) 1670 })
1580 - orderObject:setProperty("finishTime", skynet.time())  
1581 - orderObject:setProperty("status", "finish")  
1582 -  
1583 - redisproxy:srem(string.format("role:%d:orders", roleId), partnerOrderStr)  
1584 1671
1585 if not status then 1672 if not status then
1586 - status = 200  
1587 - else  
1588 - status = 1000 + status  
1589 - end  
1590 - self:log("setOrder", {  
1591 - order_status = status, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他"  
1592 - item_id = rechargeId, -- 道具id  
1593 - item_type = rechargeData.type, -- 购买的道具类型,具体见"onItems"方法中道具类型枚举表  
1594 - item_name = rechargeData.title, -- 购买的道具名  
1595 - item_number = 1, -- 购买的道具数量  
1596 - item_level = 1, -- 购买的道具等级  
1597 - order_cost = rechargeData.rmb * 100, -- 此次消费的现金金额(单位:分),如 51800即未518元,对应客户端SDK传入的'total_fee'  
1598 - order_currency = "CNY", -- 货币类型,默认为"CNY"(人民币),遵循ISO 4217规范  
1599 - order_type = order_type, -- 订单类型,首充记录为1,否则为0  
1600 - order_id = params.transactionId, -- 本条记录的订单号,对应客户端SDK返回的'bs_trade_no'  
1601 - })  
1602 - if status ~= 200 then return end  
1603 -  
1604 - SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ order = partnerOrderStr,  
1605 - result = "success", reward = reward})) 1673 + SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ order = partnerOrderStr,
  1674 + result = "success", reward = reward}))
  1675 + end
1606 1676
1607 - return orderObject:getProperty("rechargeId") 1677 + return rechargeId
1608 end 1678 end
1609 1679
1610 1680
@@ -1612,7 +1682,7 @@ function RolePlugin.bind(Role) @@ -1612,7 +1682,7 @@ function RolePlugin.bind(Role)
1612 local id = tonumber(params.id) 1682 local id = tonumber(params.id)
1613 local rechargeData = csvdb["shop_rechargeCsv"][id] 1683 local rechargeData = csvdb["shop_rechargeCsv"][id]
1614 if not rechargeData then 1684 if not rechargeData then
1615 - skynet.error("recharge id not exist", id) 1685 + skynet.error("[recharge] recharge id not exist", id)
1616 return 1 1686 return 1
1617 end 1687 end
1618 1688
@@ -1637,7 +1707,7 @@ function RolePlugin.bind(Role) @@ -1637,7 +1707,7 @@ function RolePlugin.bind(Role)
1637 elseif rechargeData.shop == 3 then -- 礼包商店 1707 elseif rechargeData.shop == 3 then -- 礼包商店
1638 reward, _ = self:award(rechargeData.itemFirst, {isRecharge = true, log = {desc = "recharge", int1 = id}}) 1708 reward, _ = self:award(rechargeData.itemFirst, {isRecharge = true, log = {desc = "recharge", int1 = id}})
1639 else 1709 else
1640 - skynet.error("invalid recharge shop type " .. id) 1710 + skynet.error("[recharge] invalid recharge shop type " .. id)
1641 return 3 1711 return 3
1642 end 1712 end
1643 1713
src/models/Store.lua
@@ -226,7 +226,7 @@ end @@ -226,7 +226,7 @@ end
226 function Store:checkRechargeRecord(limit, id) 226 function Store:checkRechargeRecord(limit, id)
227 local rechargeRecord = self:getProperty("payR") or {} 227 local rechargeRecord = self:getProperty("payR") or {}
228 if limit ~= 0 and limit <= (rechargeRecord[id] or 0) then 228 if limit ~= 0 and limit <= (rechargeRecord[id] or 0) then
229 - skynet.error(string.format("recharge id:%d count over limit, user id:%d", id, self.owner:getProperty("id"))) 229 + skynet.error(string.format("[recharge] recharge id:%d count over limit, user id:%d", id, self.owner:getProperty("id")))
230 return false 230 return false
231 end 231 end
232 rechargeRecord[id] = (rechargeRecord[id] or 0) + 1 232 rechargeRecord[id] = (rechargeRecord[id] or 0) + 1