优化提现流程,新增用户确认模式以支持待用户确认的转账,更新相关API和数据库结构以确保数据一致性。同时,调整小程序界面以展示待确认收款信息,提升用户体验。
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { requireAdminResponse } from '@/lib/admin-auth'
|
||||
import { createTransfer } from '@/lib/wechat-transfer'
|
||||
import { createTransferUserConfirm } from '@/lib/wechat-transfer'
|
||||
|
||||
// ========== GET:查询提现记录(带用户信息、收款账号=微信号)==========
|
||||
export async function GET(request: Request) {
|
||||
@@ -43,8 +43,9 @@ export async function GET(request: Request) {
|
||||
user_name: user?.nickname || '未知用户',
|
||||
userAvatar: user?.avatar,
|
||||
amount: Number(w.amount),
|
||||
status: w.status === 'success' ? 'completed' :
|
||||
w.status === 'failed' ? 'rejected' :
|
||||
status: w.status === 'success' ? 'completed' :
|
||||
w.status === 'failed' ? 'rejected' :
|
||||
w.status === 'pending_confirm' ? 'pending_confirm' :
|
||||
w.status,
|
||||
created_at: w.created_at,
|
||||
method: 'wechat',
|
||||
@@ -101,7 +102,7 @@ export async function PUT(request: Request) {
|
||||
const openid = withdrawal.wechat_openid
|
||||
|
||||
if (action === 'approve') {
|
||||
console.log(STEP, '3. 发起微信打款')
|
||||
console.log(STEP, '3. 发起转账(用户确认模式)')
|
||||
if (!openid) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
@@ -109,53 +110,85 @@ export async function PUT(request: Request) {
|
||||
}, { status: 400 })
|
||||
}
|
||||
const amountFen = Math.round(amount * 100)
|
||||
const transferResult = await createTransfer({
|
||||
const transferResult = await createTransferUserConfirm({
|
||||
openid,
|
||||
amountFen,
|
||||
outDetailNo: id,
|
||||
outBillNo: id,
|
||||
transferRemark: '提现',
|
||||
})
|
||||
if (!transferResult.success) {
|
||||
console.error(STEP, '微信打款失败:', transferResult.errorCode, transferResult.errorMessage)
|
||||
console.error(STEP, '发起转账失败:', transferResult.errorCode, transferResult.errorMessage)
|
||||
await prisma.withdrawals.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'failed',
|
||||
processed_at: new Date(),
|
||||
error_message: transferResult.errorMessage || transferResult.errorCode || '打款失败'
|
||||
error_message: transferResult.errorMessage || transferResult.errorCode || '发起失败'
|
||||
}
|
||||
})
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '微信打款失败: ' + (transferResult.errorMessage || transferResult.errorCode || '未知错误')
|
||||
error: '发起转账失败: ' + (transferResult.errorMessage || transferResult.errorCode || '未知错误')
|
||||
}, { status: 500 })
|
||||
}
|
||||
const batchNo = transferResult.outBatchNo || `B${Date.now()}`
|
||||
console.log(STEP, '4. 打款成功,更新数据库')
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.withdrawals.update({
|
||||
const state = transferResult.state || ''
|
||||
const hasPackage = !!(transferResult.packageInfo && (state === 'WAIT_USER_CONFIRM' || state === 'TRANSFERING' || state === 'ACCEPTED' || state === 'PROCESSING'))
|
||||
if (hasPackage && transferResult.packageInfo) {
|
||||
console.log(STEP, '4. 待用户确认收款,保存 package_info')
|
||||
await prisma.withdrawals.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'success',
|
||||
processed_at: new Date(),
|
||||
transaction_id: transferResult.batchId || batchNo
|
||||
status: 'pending_confirm',
|
||||
transfer_bill_no: transferResult.transferBillNo || null,
|
||||
package_info: transferResult.packageInfo,
|
||||
transaction_id: transferResult.transferBillNo || undefined,
|
||||
}
|
||||
})
|
||||
const user = await tx.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { withdrawn_earnings: true }
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '已发起转账,请通知用户在小程序「分销中心」或「我的」中点击「确认收款」完成到账'
|
||||
})
|
||||
await tx.users.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
withdrawn_earnings: Number(user?.withdrawn_earnings || 0) + amount
|
||||
}
|
||||
}
|
||||
if (state === 'SUCCESS') {
|
||||
console.log(STEP, '4. 微信直接返回成功,更新数据库')
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.withdrawals.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'success',
|
||||
processed_at: new Date(),
|
||||
transaction_id: transferResult.transferBillNo || undefined,
|
||||
transfer_bill_no: transferResult.transferBillNo || undefined,
|
||||
}
|
||||
})
|
||||
const user = await tx.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { withdrawn_earnings: true }
|
||||
})
|
||||
await tx.users.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
withdrawn_earnings: Number(user?.withdrawn_earnings || 0) + amount
|
||||
}
|
||||
})
|
||||
})
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '已批准并打款成功,款项将到账用户微信零钱'
|
||||
})
|
||||
}
|
||||
await prisma.withdrawals.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'pending_confirm',
|
||||
transfer_bill_no: transferResult.transferBillNo || null,
|
||||
package_info: transferResult.packageInfo || null,
|
||||
transaction_id: transferResult.transferBillNo || undefined,
|
||||
}
|
||||
})
|
||||
console.log(STEP, '5. 批准完成,钱已打到用户微信零钱')
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '已批准并打款成功,款项将到账用户微信零钱'
|
||||
message: '已发起转账,请通知用户在小程序内「确认收款」完成到账'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user