Commit 6f9bd590a985efb4ea2c5a9b0ce62472caf3ddb0

Authored by zhouhaihai
2 parents 697a8bc0 d705a315

Merge branch 'bugfix' into tr/ts

* 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 if true then return end 8 if true then return end
@@ -42,7 +14,7 @@ function _M.rechargeRpc(agent , data) @@ -42,7 +14,7 @@ function _M.rechargeRpc(agent , data)
42 local roleId = role:getProperty("id") 14 local roleId = role:getProperty("id")
43 15
44 --创建订单号 16 --创建订单号
45 - local partnerOrderId = makeOrder(role, id) 17 + local partnerOrderId = role:getPurchaseOrder(id)
46 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId })) 18 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId }))
47 19
48 20
@@ -91,7 +63,7 @@ function _M.googleRechargeRpc(agent, data) @@ -91,7 +63,7 @@ function _M.googleRechargeRpc(agent, data)
91 63
92 role.ignoreHeartbeat = true 64 role.ignoreHeartbeat = true
93 --创建订单号 65 --创建订单号
94 - local partnerOrderId = makeOrder(role, id) 66 + local partnerOrderId = role:getPurchaseOrder(id)
95 -- 签名 67 -- 签名
96 local secret_key = "b7657fa7ccd44c16a35e3f454ac7a075" 68 local secret_key = "b7657fa7ccd44c16a35e3f454ac7a075"
97 local need = { 69 local need = {
@@ -118,7 +90,7 @@ function _M.myCardRechargeRpc(agent, data) @@ -118,7 +90,7 @@ function _M.myCardRechargeRpc(agent, data)
118 90
119 role.ignoreHeartbeat = true 91 role.ignoreHeartbeat = true
120 --创建订单号 92 --创建订单号
121 - local partnerOrderId = makeOrder(role, id) 93 + local partnerOrderId = role:getPurchaseOrder(id)
122 -- 签名 94 -- 签名
123 local secret_key = "48759e07540f46d9af17ec82669b4272" 95 local secret_key = "48759e07540f46d9af17ec82669b4272"
124 local need = { 96 local need = {
@@ -144,7 +116,7 @@ function _M.iosRechargeRpc(agent, data) @@ -144,7 +116,7 @@ function _M.iosRechargeRpc(agent, data)
144 116
145 role.ignoreHeartbeat = true 117 role.ignoreHeartbeat = true
146 --创建订单号 118 --创建订单号
147 - local partnerOrderId = makeOrder(role, id) 119 + local partnerOrderId = role:getPurchaseOrder(id)
148 -- 签名 120 -- 签名
149 local secret_key = "9647d2efe1074c73b9ac19af4337a70e" 121 local secret_key = "9647d2efe1074c73b9ac19af4337a70e"
150 local need = { 122 local need = {
@@ -168,43 +140,15 @@ function _M.purchaseOrderResult(agent, data) @@ -168,43 +140,15 @@ function _M.purchaseOrderResult(agent, data)
168 140
169 role.ignoreHeartbeat = false 141 role.ignoreHeartbeat = false
170 142
171 - local partnerOrderStr = msg.order  
172 - local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")  
173 - local orderObject = require("models.Order").new({ key = string.format("order:%d:%d", roleId, orderId) })  
174 - if not orderObject:load() then  
175 - -- 订单不存在  
176 - skynet.error("cancelPurchaseRpc", string.format("order %s not exist", partnerOrderStr))  
177 - return true  
178 - end  
179 -  
180 - if msg.status == "success" then  
181 - orderObject:setProperty("transactionId", msg.platformOrder or "")  
182 - local rechargeId = orderObject:getProperty("rechargeId")  
183 - local dataSet = csvdb["shop_rechargeCsv"][rechargeId]  
184 -  
185 - role:log("setOrder", {  
186 - order_status = 100, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他"  
187 - item_id = rechargeId, -- 道具id  
188 - item_type = dataSet.type, -- 购买的道具类型,具体见"onItems"方法中道具类型枚举表  
189 - item_name = dataSet.title, -- 购买的道具名  
190 - item_number = 1, -- 购买的道具数量  
191 - item_level = 1, -- 购买的道具等级  
192 - order_cost = dataSet.rmb * 100, -- 此次消费的现金金额(单位:分),如 51800即未518元,对应客户端SDK传入的'total_fee'  
193 - order_currency = "CNY", -- 货币类型,默认为"CNY"(人民币),遵循ISO 4217规范  
194 - order_type = role:getProperty("rmbC") > 0 and 0 or 1, -- 订单类型,首充记录为1,否则为0  
195 - order_id = msg.platformOrder, -- 本条记录的订单号,对应客户端SDK返回的'bs_trade_no'  
196 - })  
197 -  
198 - return true  
199 - end 143 + local status = {
  144 + fail = true,
  145 + success = true
  146 + }
200 147
201 - if orderObject:getProperty("finishTime") > 0 then  
202 - return true 148 + local partnerOrderStr = msg.order
  149 + if partnerOrderStr then
  150 + role:updatePurchaseOrder(partnerOrderStr, msg.platformOrder, status[msg.status] and msg.status or "unknown")
203 end 151 end
204 -  
205 - orderObject:setProperty("status", msg.status)  
206 -  
207 - redisproxy:srem(string.format("role:%d:orders", roleId), partnerOrderStr)  
208 return true 152 return true
209 end 153 end
210 154
src/models/RolePlugin.lua
@@ -1497,6 +1497,102 @@ function RolePlugin.bind(Role) @@ -1497,6 +1497,102 @@ function RolePlugin.bind(Role)
1497 self:updateProperty({field = "redp", value = redp}) 1497 self:updateProperty({field = "redp", value = redp})
1498 end 1498 end
1499 1499
  1500 + -- 获取充值订单号
  1501 + function Role:getPurchaseOrder(rechargeId)
  1502 + local roleId = self:getProperty("id")
  1503 + local rechargeData = csvdb["shop_rechargeCsv"][rechargeId]
  1504 + if not rechargeData then
  1505 + skynet.error("recharge id not exist", rechargeId)
  1506 + return ""
  1507 + end
  1508 + local limit = rechargeData.limit
  1509 + local rechargeRecord = self:getProperty("payR") or {}
  1510 + if limit ~= 0 and limit <= (rechargeRecord[rechargeId] or 0) then
  1511 + return ""
  1512 + end
  1513 +
  1514 + local orderId = redisproxy:hget(string.format(R_ORDERS, roleId), rechargeId)
  1515 + if orderId then
  1516 + local orderObject = require("models.Order").new({ key = string.format(R_ORDER, roleId, orderId) })
  1517 + if orderObject:load() and orderObject:getProperty("rechargeId") == rechargeId and math.abs(skynet.timex() - orderObject:getProperty("createTime")) < 5 * 60 then
  1518 + return string.format("%d_%d_%d", serverId, roleId, orderId)
  1519 + end
  1520 + end
  1521 +
  1522 + orderId = redisproxy:hincrby("autoincrement_set", "order", 1)
  1523 + local partnerOrderId = string.format("%d_%d_%d", serverId, roleId, orderId)
  1524 + local orderKey = string.format(R_ORDER, roleId, orderId)
  1525 + redisproxy:del(orderKey) -- 删掉可能有了
  1526 + local order = require("models.Order").new({
  1527 + key = orderKey,
  1528 + order = partnerOrderId,
  1529 + rechargeId = rechargeId,
  1530 + })
  1531 + order:create()
  1532 + -- 正在进行中的订单 缓存
  1533 + redisproxy:hset(string.format(R_ORDERS, roleId), rechargeId, orderId)
  1534 + return partnerOrderId
  1535 + end
  1536 +
  1537 + -- 更新订单信息
  1538 + --[[
  1539 +
  1540 + status
  1541 +
  1542 + success
  1543 + fail
  1544 + finsh
  1545 + unknow
  1546 +
  1547 + --]]
  1548 + function Role:updatePurchaseOrder(partnerOrderStr, platformOrder, status)
  1549 + if not partnerOrderStr then return false end
  1550 + local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")
  1551 +
  1552 + local roleId = self:getProperty("id")
  1553 + local orderObject = require("models.Order").new({ key = string.format(R_ORDER, roleId, orderId) })
  1554 + if not orderObject:load() then
  1555 + return false
  1556 + end
  1557 +
  1558 + local rechargeId = orderObject:getProperty("rechargeId")
  1559 + local dataSet = csvdb["shop_rechargeCsv"][rechargeId]
  1560 +
  1561 + if orderObject:getProperty("finishTime") > 0 then
  1562 + return false, "finsh"
  1563 + end
  1564 +
  1565 + if msg.platformOrder then
  1566 + orderObject:setProperty("transactionId", msg.platformOrder)
  1567 + end
  1568 + orderObject:setProperty("status", msg.status)
  1569 +
  1570 + -- 开始下单
  1571 + if status == "success" then
  1572 + elseif status == "fail" then
  1573 + redisproxy:hdel(string.format(R_ORDERS, roleId), orderId)
  1574 + elseif status == "finsh" then
  1575 + orderObject:setProperty("finishTime", skynet.time())
  1576 + redisproxy:hdel(string.format(R_ORDERS, roleId), orderId)
  1577 + end
  1578 +
  1579 + if status ~= "unknow" then
  1580 + self:log("setOrder", {
  1581 + order_status = ({success = 100, finsh = 200, fail = 300})[status] or 1000, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他"
  1582 + item_id = rechargeId, -- 道具id
  1583 + item_type = dataSet.type, -- 购买的道具类型,具体见"onItems"方法中道具类型枚举表
  1584 + item_name = dataSet.title, -- 购买的道具名
  1585 + item_number = 1, -- 购买的道具数量
  1586 + item_level = 1, -- 购买的道具等级
  1587 + order_cost = dataSet.rmb * 100, -- 此次消费的现金金额(单位:分),如 51800即未518元,对应客户端SDK传入的'total_fee'
  1588 + order_currency = "CNY", -- 货币类型,默认为"CNY"(人民币),遵循ISO 4217规范
  1589 + order_type = self:getProperty("rmbC") > 0 and 0 or 1, -- 订单类型,首充记录为1,否则为0
  1590 + order_id = platformOrder, -- 本条记录的订单号,对应客户端SDK返回的'bs_trade_no'
  1591 + })
  1592 + end
  1593 +
  1594 + return true, rechargeId
  1595 + end
1500 1596
1501 -- 充值 -- 1597 -- 充值 --
1502 --[[ 1598 --[[
@@ -1511,63 +1607,37 @@ function RolePlugin.bind(Role) @@ -1511,63 +1607,37 @@ function RolePlugin.bind(Role)
1511 local roleId = self:getProperty("id") 1607 local roleId = self:getProperty("id")
1512 local partnerOrderStr = params.order 1608 local partnerOrderStr = params.order
1513 1609
1514 - local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")  
1515 - local orderObject = require("models.Order").new({ key = string.format("order:%d:%d", roleId, orderId) })  
1516 - if not orderObject:load() then  
1517 - -- 订单不存在  
1518 - skynet.error("ayncPurchaseRpc", string.format("order %s not exist", partnerOrderStr))  
1519 - return  
1520 - end  
1521 1610
1522 - if orderObject:getProperty("finishTime") > 0 then  
1523 - -- 订单已经处理  
1524 - SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ result = "handled" })) 1611 + local status, back = self:updatePurchaseOrder(partnerOrderStr, params.transactionId, "finsh")
  1612 + if not status then
  1613 + if back == "finsh" then
  1614 + -- 订单已经处理
  1615 + SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ result = "handled" }))
  1616 + end
1525 return 1617 return
1526 end 1618 end
1527 - local rechargeId = orderObject:getProperty("rechargeId") 1619 + local rechargeId = back
1528 local rechargeData = csvdb["shop_rechargeCsv"][rechargeId] 1620 local rechargeData = csvdb["shop_rechargeCsv"][rechargeId]
1529 if rechargeData.rmb ~= tonumber(params.amount) then 1621 if rechargeData.rmb ~= tonumber(params.amount) then
1530 - skynet.error(string.format("fake order: %s, roleId: %d, order: %s, rmb %s, get %s", 1622 + skynet.error(string.format("[recharge] fake order: %s, roleId: %d, order: %s, rmb %s, get %s",
1531 params.transactionId, roleId, partnerOrderStr, rechargeData.rmb, params.amount 1623 params.transactionId, roleId, partnerOrderStr, rechargeData.rmb, params.amount
1532 )) 1624 ))
1533 return 1625 return
1534 end 1626 end
1535 1627
1536 - local order_type = self:getProperty("rmbC") > 0 and 0 or 1  
1537 local status, reward = self:recharge({ 1628 local status, reward = self:recharge({
1538 id = rechargeId, 1629 id = rechargeId,
1539 transactionId = params.transactionId, 1630 transactionId = params.transactionId,
1540 pay_time = params.pay_time, 1631 pay_time = params.pay_time,
1541 order = partnerOrderStr, 1632 order = partnerOrderStr,
1542 }) 1633 })
1543 - orderObject:setProperty("finishTime", skynet.time())  
1544 - orderObject:setProperty("status", "finish")  
1545 -  
1546 - redisproxy:srem(string.format("role:%d:orders", roleId), partnerOrderStr)  
1547 1634
1548 if not status then 1635 if not status then
1549 - status = 200  
1550 - else  
1551 - status = 1000 + status  
1552 - end  
1553 - self:log("setOrder", {  
1554 - order_status = status, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他"  
1555 - item_id = rechargeId, -- 道具id  
1556 - item_type = rechargeData.type, -- 购买的道具类型,具体见"onItems"方法中道具类型枚举表  
1557 - item_name = rechargeData.title, -- 购买的道具名  
1558 - item_number = 1, -- 购买的道具数量  
1559 - item_level = 1, -- 购买的道具等级  
1560 - order_cost = rechargeData.rmb * 100, -- 此次消费的现金金额(单位:分),如 51800即未518元,对应客户端SDK传入的'total_fee'  
1561 - order_currency = "CNY", -- 货币类型,默认为"CNY"(人民币),遵循ISO 4217规范  
1562 - order_type = order_type, -- 订单类型,首充记录为1,否则为0  
1563 - order_id = params.transactionId, -- 本条记录的订单号,对应客户端SDK返回的'bs_trade_no'  
1564 - })  
1565 - if status ~= 200 then return end  
1566 -  
1567 - SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ order = partnerOrderStr,  
1568 - result = "success", reward = reward})) 1636 + SendPacket(actionCodes.Store_ayncPurchaseRpc, MsgPack.pack({ order = partnerOrderStr,
  1637 + result = "success", reward = reward}))
  1638 + end
1569 1639
1570 - return orderObject:getProperty("rechargeId") 1640 + return rechargeId
1571 end 1641 end
1572 1642
1573 1643
@@ -1575,7 +1645,7 @@ function RolePlugin.bind(Role) @@ -1575,7 +1645,7 @@ function RolePlugin.bind(Role)
1575 local id = tonumber(params.id) 1645 local id = tonumber(params.id)
1576 local rechargeData = csvdb["shop_rechargeCsv"][id] 1646 local rechargeData = csvdb["shop_rechargeCsv"][id]
1577 if not rechargeData then 1647 if not rechargeData then
1578 - skynet.error("recharge id not exist", id) 1648 + skynet.error("[recharge] recharge id not exist", id)
1579 return 1 1649 return 1
1580 end 1650 end
1581 1651
@@ -1600,7 +1670,7 @@ function RolePlugin.bind(Role) @@ -1600,7 +1670,7 @@ function RolePlugin.bind(Role)
1600 elseif rechargeData.shop == 3 then -- 礼包商店 1670 elseif rechargeData.shop == 3 then -- 礼包商店
1601 reward, _ = self:award(rechargeData.itemFirst, {isRecharge = true, log = {desc = "recharge", int1 = id}}) 1671 reward, _ = self:award(rechargeData.itemFirst, {isRecharge = true, log = {desc = "recharge", int1 = id}})
1602 else 1672 else
1603 - skynet.error("invalid recharge shop type " .. id) 1673 + skynet.error("[recharge] invalid recharge shop type " .. id)
1604 return 3 1674 return 3
1605 end 1675 end
1606 1676
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