Commit d705a315d66ebbd40c6bf1f09b4822c3e726e240

Authored by zhouhaihai
1 parent 3a3a3ddf

充值 整理

src/RedisKeys.lua
... ... @@ -12,6 +12,8 @@ R_RUNE = "role:%d:rune:%d" -- 符文详细信息
12 12 R_EMAIL = "role:%d:emailIds" --邮件列表
13 13 R_EMAIL_ITEM = "email:%d:%d" --邮件
14 14 R_STORE = "role:%d:store" -- 商店
  15 +R_ORDERS = "role:%d:orders" -- 订单
  16 +R_ORDER = "order:%d:%d"
15 17  
16 18  
17 19 -- rank
... ...
src/actions/StoreAction.lua
... ... @@ -3,34 +3,6 @@ local _M = {}
3 3 local serverId = tonumber(skynet.getenv("servId"))
4 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 6 -- 入口在正式服关闭 -- mock 充值
35 7 function _M.rechargeRpc(agent , data)
36 8 local role = agent.role
... ... @@ -41,7 +13,7 @@ function _M.rechargeRpc(agent , data)
41 13 local roleId = role:getProperty("id")
42 14  
43 15 --创建订单号
44   - local partnerOrderId = makeOrder(role, id)
  16 + local partnerOrderId = role:getPurchaseOrder(id)
45 17 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId }))
46 18  
47 19  
... ... @@ -90,7 +62,7 @@ function _M.googleRechargeRpc(agent, data)
90 62  
91 63 role.ignoreHeartbeat = true
92 64 --创建订单号
93   - local partnerOrderId = makeOrder(role, id)
  65 + local partnerOrderId = role:getPurchaseOrder(id)
94 66 -- 签名
95 67 local secret_key = "b7657fa7ccd44c16a35e3f454ac7a075"
96 68 local need = {
... ... @@ -117,7 +89,7 @@ function _M.myCardRechargeRpc(agent, data)
117 89  
118 90 role.ignoreHeartbeat = true
119 91 --创建订单号
120   - local partnerOrderId = makeOrder(role, id)
  92 + local partnerOrderId = role:getPurchaseOrder(id)
121 93 -- 签名
122 94 local secret_key = "48759e07540f46d9af17ec82669b4272"
123 95 local need = {
... ... @@ -143,7 +115,7 @@ function _M.iosRechargeRpc(agent, data)
143 115  
144 116 role.ignoreHeartbeat = true
145 117 --创建订单号
146   - local partnerOrderId = makeOrder(role, id)
  118 + local partnerOrderId = role:getPurchaseOrder(id)
147 119 -- 签名
148 120 local secret_key = "9647d2efe1074c73b9ac19af4337a70e"
149 121 local need = {
... ... @@ -167,43 +139,15 @@ function _M.purchaseOrderResult(agent, data)
167 139  
168 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 150 end
203   -
204   - orderObject:setProperty("status", msg.status)
205   -
206   - redisproxy:srem(string.format("role:%d:orders", roleId), partnerOrderStr)
207 151 return true
208 152 end
209 153  
... ...
src/models/RolePlugin.lua
... ... @@ -1497,6 +1497,102 @@ function RolePlugin.bind(Role)
1497 1497 self:updateProperty({field = "redp", value = redp})
1498 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 1607 local roleId = self:getProperty("id")
1512 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 1617 return
1526 1618 end
1527   - local rechargeId = orderObject:getProperty("rechargeId")
  1619 + local rechargeId = back
1528 1620 local rechargeData = csvdb["shop_rechargeCsv"][rechargeId]
1529 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 1623 params.transactionId, roleId, partnerOrderStr, rechargeData.rmb, params.amount
1532 1624 ))
1533 1625 return
1534 1626 end
1535 1627  
1536   - local order_type = self:getProperty("rmbC") > 0 and 0 or 1
1537 1628 local status, reward = self:recharge({
1538 1629 id = rechargeId,
1539 1630 transactionId = params.transactionId,
1540 1631 pay_time = params.pay_time,
1541 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 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 1641 end
1572 1642  
1573 1643  
... ... @@ -1575,7 +1645,7 @@ function RolePlugin.bind(Role)
1575 1645 local id = tonumber(params.id)
1576 1646 local rechargeData = csvdb["shop_rechargeCsv"][id]
1577 1647 if not rechargeData then
1578   - skynet.error("recharge id not exist", id)
  1648 + skynet.error("[recharge] recharge id not exist", id)
1579 1649 return 1
1580 1650 end
1581 1651  
... ... @@ -1600,7 +1670,7 @@ function RolePlugin.bind(Role)
1600 1670 elseif rechargeData.shop == 3 then -- 礼包商店
1601 1671 reward, _ = self:award(rechargeData.itemFirst, {isRecharge = true, log = {desc = "recharge", int1 = id}})
1602 1672 else
1603   - skynet.error("invalid recharge shop type " .. id)
  1673 + skynet.error("[recharge] invalid recharge shop type " .. id)
1604 1674 return 3
1605 1675 end
1606 1676  
... ...
src/models/Store.lua
... ... @@ -226,7 +226,7 @@ end
226 226 function Store:checkRechargeRecord(limit, id)
227 227 local rechargeRecord = self:getProperty("payR") or {}
228 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 230 return false
231 231 end
232 232 rechargeRecord[id] = (rechargeRecord[id] or 0) + 1
... ...