优化小程序支付流程,新增订单插入逻辑,确保支付成功后更新订单状态并处理佣金分配。同时,重构阅读页面,增强权限管理和阅读追踪功能,提升用户体验。
This commit is contained in:
@@ -125,17 +125,78 @@ export async function POST(request: Request) {
|
||||
const { productType, productId, userId } = attach
|
||||
|
||||
// 1. 更新订单状态为已支付
|
||||
let orderExists = false
|
||||
try {
|
||||
await query(`
|
||||
UPDATE orders
|
||||
SET status = 'paid',
|
||||
transaction_id = ?,
|
||||
pay_time = CURRENT_TIMESTAMP
|
||||
WHERE order_sn = ? AND status = 'pending'
|
||||
`, [transactionId, orderSn])
|
||||
console.log('[PayNotify] 订单状态已更新:', orderSn)
|
||||
// 先查询订单是否存在
|
||||
const orderRows = await query(`
|
||||
SELECT id, user_id, product_type, product_id, status
|
||||
FROM orders
|
||||
WHERE order_sn = ?
|
||||
`, [orderSn]) as any[]
|
||||
|
||||
if (orderRows.length === 0) {
|
||||
console.warn('[PayNotify] ⚠️ 订单不存在,尝试补记:', orderSn)
|
||||
|
||||
// 订单不存在时,补记订单(可能是创建订单时失败了)
|
||||
try {
|
||||
await query(`
|
||||
INSERT INTO orders (
|
||||
id, order_sn, user_id, open_id,
|
||||
product_type, product_id, amount, description,
|
||||
status, transaction_id, pay_time, referrer_id, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'paid', ?, CURRENT_TIMESTAMP, NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
`, [
|
||||
orderSn, orderSn, userId || openId, openId,
|
||||
productType || 'unknown', productId || '', totalAmount,
|
||||
'支付回调补记订单', transactionId
|
||||
])
|
||||
console.log('[PayNotify] ✅ 订单补记成功:', orderSn)
|
||||
orderExists = true
|
||||
} catch (insertErr: any) {
|
||||
if (insertErr?.message?.includes('referrer_id') || insertErr?.code === 'ER_BAD_FIELD_ERROR') {
|
||||
try {
|
||||
await query(`
|
||||
INSERT INTO orders (
|
||||
id, order_sn, user_id, open_id,
|
||||
product_type, product_id, amount, description,
|
||||
status, transaction_id, pay_time, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'paid', ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
`, [
|
||||
orderSn, orderSn, userId || openId, openId,
|
||||
productType || 'unknown', productId || '', totalAmount,
|
||||
'支付回调补记订单', transactionId
|
||||
])
|
||||
console.log('[PayNotify] ✅ 订单补记成功(无 referrer_id):', orderSn)
|
||||
orderExists = true
|
||||
} catch (e2) {
|
||||
console.error('[PayNotify] ❌ 补记订单失败:', e2)
|
||||
}
|
||||
} else {
|
||||
console.error('[PayNotify] ❌ 补记订单失败:', insertErr)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const order = orderRows[0]
|
||||
orderExists = true
|
||||
|
||||
if (order.status === 'paid') {
|
||||
console.log('[PayNotify] ℹ️ 订单已支付,跳过更新:', orderSn)
|
||||
} else {
|
||||
// 更新订单状态
|
||||
await query(`
|
||||
UPDATE orders
|
||||
SET status = 'paid',
|
||||
transaction_id = ?,
|
||||
pay_time = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE order_sn = ?
|
||||
`, [transactionId, orderSn])
|
||||
|
||||
console.log('[PayNotify] ✅ 订单状态已更新为已支付:', orderSn)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[PayNotify] 更新订单状态失败:', e)
|
||||
console.error('[PayNotify] ❌ 处理订单失败:', e)
|
||||
}
|
||||
|
||||
// 2. 获取用户信息
|
||||
@@ -151,30 +212,83 @@ export async function POST(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 更新用户购买记录
|
||||
if (buyerUserId) {
|
||||
// 3. 更新用户购买记录(✅ 检查是否已有其他相同产品的已支付订单)
|
||||
if (buyerUserId && productType) {
|
||||
try {
|
||||
if (productType === 'fullbook') {
|
||||
// 全书购买
|
||||
// 全书购买:无论如何都解锁
|
||||
await query('UPDATE users SET has_full_book = TRUE WHERE id = ?', [buyerUserId])
|
||||
console.log('[PayNotify] 用户已购全书:', buyerUserId)
|
||||
console.log('[PayNotify] ✅ 用户已购全书:', buyerUserId)
|
||||
|
||||
} else if (productType === 'section' && productId) {
|
||||
// 单章购买
|
||||
await query(`
|
||||
UPDATE users
|
||||
SET purchased_sections = JSON_ARRAY_APPEND(
|
||||
COALESCE(purchased_sections, '[]'),
|
||||
'$', ?
|
||||
)
|
||||
WHERE id = ? AND NOT JSON_CONTAINS(COALESCE(purchased_sections, '[]'), ?)
|
||||
`, [productId, buyerUserId, JSON.stringify(productId)])
|
||||
console.log('[PayNotify] 用户已购章节:', buyerUserId, productId)
|
||||
// 单章购买:检查是否已有该章节的其他已支付订单
|
||||
const existingPaidOrders = await query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM orders
|
||||
WHERE user_id = ?
|
||||
AND product_type = 'section'
|
||||
AND product_id = ?
|
||||
AND status = 'paid'
|
||||
AND order_sn != ?
|
||||
`, [buyerUserId, productId, orderSn]) as any[]
|
||||
|
||||
const hasOtherPaidOrder = existingPaidOrders[0].count > 0
|
||||
|
||||
if (hasOtherPaidOrder) {
|
||||
console.log('[PayNotify] ℹ️ 用户已有该章节的其他已支付订单,无需重复解锁:', {
|
||||
userId: buyerUserId,
|
||||
productId
|
||||
})
|
||||
} else {
|
||||
// 第一次支付该章节,解锁权限
|
||||
await query(`
|
||||
UPDATE users
|
||||
SET purchased_sections = JSON_ARRAY_APPEND(
|
||||
COALESCE(purchased_sections, '[]'),
|
||||
'$', ?
|
||||
)
|
||||
WHERE id = ? AND NOT JSON_CONTAINS(COALESCE(purchased_sections, '[]'), ?)
|
||||
`, [productId, buyerUserId, JSON.stringify(productId)])
|
||||
console.log('[PayNotify] ✅ 用户首次购买章节,已解锁:', buyerUserId, productId)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[PayNotify] 更新用户购买记录失败:', e)
|
||||
console.error('[PayNotify] ❌ 更新用户购买记录失败:', e)
|
||||
}
|
||||
|
||||
// 4. 处理分销佣金(90%给推广者)
|
||||
// 4. 清理相同产品的无效订单(未支付的订单)
|
||||
if (productType && (productType === 'fullbook' || productId)) {
|
||||
try {
|
||||
const deleteResult = await query(`
|
||||
DELETE FROM orders
|
||||
WHERE user_id = ?
|
||||
AND product_type = ?
|
||||
AND product_id = ?
|
||||
AND status = 'created'
|
||||
AND order_sn != ?
|
||||
`, [
|
||||
buyerUserId,
|
||||
productType,
|
||||
productId || 'fullbook',
|
||||
orderSn // 保留当前已支付的订单
|
||||
])
|
||||
|
||||
const deletedCount = (deleteResult as any).affectedRows || 0
|
||||
if (deletedCount > 0) {
|
||||
console.log('[PayNotify] ✅ 已清理无效订单:', {
|
||||
userId: buyerUserId,
|
||||
productType,
|
||||
productId: productId || 'fullbook',
|
||||
deletedCount
|
||||
})
|
||||
}
|
||||
} catch (deleteErr) {
|
||||
console.error('[PayNotify] ❌ 清理无效订单失败:', deleteErr)
|
||||
// 清理失败不影响主流程
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 处理分销佣金(90%给推广者)
|
||||
await processReferralCommission(buyerUserId, totalAmount, orderSn)
|
||||
}
|
||||
|
||||
@@ -267,3 +381,58 @@ async function processReferralCommission(buyerUserId: string, amount: number, or
|
||||
// 分佣失败不影响主流程
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理无效订单
|
||||
* 当一个订单支付成功后,删除该用户相同产品的其他未支付订单
|
||||
*/
|
||||
async function cleanupUnpaidOrders(
|
||||
userId: string,
|
||||
productType: string | undefined,
|
||||
productId: string | undefined,
|
||||
paidOrderSn: string
|
||||
) {
|
||||
try {
|
||||
if (!userId || !productType) {
|
||||
return
|
||||
}
|
||||
|
||||
// 查询相同产品的其他未支付订单
|
||||
const unpaidOrders = await query(`
|
||||
SELECT id, order_sn, status, created_at
|
||||
FROM orders
|
||||
WHERE user_id = ?
|
||||
AND product_type = ?
|
||||
AND product_id = ?
|
||||
AND status IN ('created', 'pending')
|
||||
AND order_sn != ?
|
||||
`, [userId, productType, productId || 'fullbook', paidOrderSn]) as any[]
|
||||
|
||||
if (unpaidOrders.length === 0) {
|
||||
console.log('[PayNotify] ℹ️ 没有需要清理的无效订单')
|
||||
return
|
||||
}
|
||||
|
||||
// 删除这些无效订单
|
||||
await query(`
|
||||
DELETE FROM orders
|
||||
WHERE user_id = ?
|
||||
AND product_type = ?
|
||||
AND product_id = ?
|
||||
AND status IN ('created', 'pending')
|
||||
AND order_sn != ?
|
||||
`, [userId, productType, productId || 'fullbook', paidOrderSn])
|
||||
|
||||
console.log('[PayNotify] ✅ 已清理无效订单:', {
|
||||
userId,
|
||||
productType,
|
||||
productId,
|
||||
deletedCount: unpaidOrders.length,
|
||||
deletedOrders: unpaidOrders.map(o => o.order_sn)
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('[PayNotify] ❌ 清理无效订单失败:', error)
|
||||
// 清理失败不影响主流程
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user