feat: 虚拟支付完整链路——vpay服务模块(双签名/充值下单/回调到账/退款) + 充值页 + 我的入口 + session_key存储
This commit is contained in:
@@ -47,10 +47,39 @@ module.exports = function registerWxMsgRoutes(app) {
|
||||
}
|
||||
const event = fields.Event || fields.event || ''
|
||||
console.log('[wx-msg] 收到推送 Event=' + event, JSON.stringify(fields).slice(0, 500))
|
||||
// TODO(vpay接入):按事件分发改写用户星尘余额/发货状态
|
||||
// xpay_coin_pay_notify 代币充值到账
|
||||
// xpay_goods_deliver_notify 道具发货
|
||||
// xpay_refund_notify 退款
|
||||
|
||||
// 代币充值到账:改写用户星尘余额 + 订单状态
|
||||
if (event === 'xpay_coin_pay_notify') {
|
||||
try {
|
||||
const { User, Order } = require('../models')
|
||||
const outTradeNo = fields.OutTradeNo || ''
|
||||
const coins = parseInt(fields.BuyQuantity || fields.buyQuantity || '0')
|
||||
if (outTradeNo && coins > 0) {
|
||||
const order = await Order.findOne({ orderNo: outTradeNo })
|
||||
if (order && order.status === 'pending') {
|
||||
await Order.updateOne({ _id: order._id }, { status: 'paid', paidAt: new Date() })
|
||||
await User.updateOne({ _id: order.userId }, { $inc: { vpayCoins: coins } })
|
||||
console.log(`[wx-msg] 星尘充值到账: ${outTradeNo} +${coins}`)
|
||||
}
|
||||
}
|
||||
} catch (e) { console.error('[wx-msg] 充值到账失败:', e.message) }
|
||||
}
|
||||
|
||||
// 退款:扣回星尘
|
||||
if (event === 'xpay_refund_notify') {
|
||||
try {
|
||||
const { User, Order } = require('../models')
|
||||
const outTradeNo = fields.OutTradeNo || ''
|
||||
const order = await Order.findOne({ orderNo: outTradeNo, status: 'paid' })
|
||||
if (order) {
|
||||
await Order.updateOne({ _id: order._id }, { status: 'refunded' })
|
||||
const coins = (order.metadata || {}).coins || 0
|
||||
if (coins > 0) await User.updateOne({ _id: order.userId }, { $inc: { vpayCoins: -coins } })
|
||||
console.log(`[wx-msg] 星尘退款: ${outTradeNo} -${coins}`)
|
||||
}
|
||||
} catch (e) { console.error('[wx-msg] 退款处理失败:', e.message) }
|
||||
}
|
||||
|
||||
return res.status(200).json({ ErrCode: 0, ErrMsg: '' })
|
||||
} catch (e) {
|
||||
console.error('[wx-msg] 处理失败:', e.message)
|
||||
|
||||
Reference in New Issue
Block a user