fix: 回调读取CoinInfo.Quantity嵌套字段+补发到账
This commit is contained in:
@@ -53,13 +53,35 @@ module.exports = function registerWxMsgRoutes(app) {
|
|||||||
try {
|
try {
|
||||||
const { User, Order } = require('../models')
|
const { User, Order } = require('../models')
|
||||||
const outTradeNo = fields.OutTradeNo || ''
|
const outTradeNo = fields.OutTradeNo || ''
|
||||||
const coins = parseInt(fields.BuyQuantity || fields.buyQuantity || '0')
|
// 微信回调格式:CoinInfo.Quantity(嵌套)——兼容 JSON 和 XML 两种
|
||||||
|
const coinInfo = fields.CoinInfo || fields.coinInfo || {}
|
||||||
|
const coins = parseInt(
|
||||||
|
(typeof coinInfo === 'object' ? coinInfo.Quantity : coinInfo) ||
|
||||||
|
fields.BuyQuantity || fields.buyQuantity || '0'
|
||||||
|
)
|
||||||
|
console.log(`[wx-msg] 充值回调解析: order=${outTradeNo} coins=${coins} coinInfo=${JSON.stringify(coinInfo)}`)
|
||||||
if (outTradeNo && coins > 0) {
|
if (outTradeNo && coins > 0) {
|
||||||
|
// 先查订单,没有也直接给用户加币(Attach 里带了 uid)
|
||||||
const order = await Order.findOne({ orderNo: outTradeNo })
|
const order = await Order.findOne({ orderNo: outTradeNo })
|
||||||
if (order && order.status === 'pending') {
|
if (order && order.status === 'pending') {
|
||||||
await Order.updateOne({ _id: order._id }, { status: 'paid', paidAt: new Date() })
|
await Order.updateOne({ _id: order._id }, { status: 'paid', paidAt: new Date() })
|
||||||
await User.updateOne({ _id: order.userId }, { $inc: { vpayCoins: coins } })
|
await User.updateOne({ _id: order.userId }, { $inc: { vpayCoins: coins } })
|
||||||
console.log(`[wx-msg] 星尘充值到账: ${outTradeNo} +${coins}`)
|
console.log(`[wx-msg] ✓ 星尘充值到账(有订单): ${outTradeNo} +${coins} → user=${order.userId}`)
|
||||||
|
} else {
|
||||||
|
// 订单可能未落库或已处理——从 Attach 里拿 uid 直接加
|
||||||
|
try {
|
||||||
|
const attach = typeof coinInfo === 'object' ? coinInfo.Attach || '' : ''
|
||||||
|
const uid = attach ? JSON.parse(attach).uid : ''
|
||||||
|
if (uid) {
|
||||||
|
await User.updateOne({ _id: uid }, { $inc: { vpayCoins: coins } })
|
||||||
|
if (order) await Order.updateOne({ _id: order._id }, { status: 'paid', paidAt: new Date() })
|
||||||
|
console.log(`[wx-msg] ✓ 星尘充值到账(从Attach): ${outTradeNo} +${coins} → user=${uid}`)
|
||||||
|
} else if (order) {
|
||||||
|
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 (e2) { console.error('[wx-msg] Attach解析失败:', e2.message) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) { console.error('[wx-msg] 充值到账失败:', e.message) }
|
} catch (e) { console.error('[wx-msg] 充值到账失败:', e.message) }
|
||||||
|
|||||||
Reference in New Issue
Block a user