feat: 新增充值退款功能完整实现

- 新增充值退款数据库表结构及相关枚举字段
- 新增充值退款服务层核心逻辑(创建、审核、退款、回调处理)
- 新增充值退款后台管理页面及操作入口
- 新增充值退款前端页面及样式调整
- 新增充值退款相关测试用例
- 新增充值退款策略校验及网关处理逻辑
这个提交包含在:
2026-11-20 16:04:01 +08:00
父节点 6b83afb750
当前提交 f4f0c5513e
共有 29 个文件被更改,包括 487 次插入 和 651 次删除

查看文件

@@ -0,0 +1,77 @@
<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\library\RechargeRefundService;
use think\Db;
class RechargeRefund extends Backend
{
public function index()
{
$status = trim((string)$this->request->get('status', ''));
$query = Db::name('recharge_refund')->alias('r')
->join('pay_order p', 'p.id=r.pay_order_id', 'LEFT')
->join('user u', 'u.id=r.user_id', 'LEFT')
->field('r.*,p.out_trade_no,u.mobile,u.nickname');
if ($status !== '') $query->where('r.status', $status);
$list = $query->order('r.id desc')->paginate(20, false, ['query'=>$this->request->get()]);
$this->view->assign('list', $list);
return $this->view->fetch();
}
public function create($ids = null)
{
$pay = Db::name('pay_order')->where('id', (int)$ids)->where('scene', 'recharge')->find();
if (!$pay) $this->error('充值订单不存在');
if ($this->request->isPost()) {
$this->token();
try { RechargeRefundService::create((int)$ids, $this->request->post('amount', ''), $this->request->post('reason', ''), $this->auth->id); }
catch (\Throwable $e) { $this->error($e->getMessage()); }
$this->success('退款申请已提交,余额已占用,等待审核');
}
$this->view->assign('pay', $pay);
$this->view->assign('balance', Db::name('user')->where('id', (int)$pay['user_id'])->value('money'));
return $this->view->fetch();
}
public function review($ids = null)
{
$row = Db::name('recharge_refund')->where('id', (int)$ids)->find();
if (!$row) $this->error('退款申请不存在');
if ($this->request->isPost()) {
$this->token();
$action = $this->request->post('action', '');
try {
if ($action === 'reject') {
RechargeRefundService::reject((int)$ids, $this->auth->id, $this->request->post('note', ''));
$this->success('已驳回并释放余额');
}
if ($action !== 'approve') throw new \InvalidArgumentException('无效操作');
RechargeRefundService::begin((int)$ids, $this->auth->id, $this->request->post('note', ''));
} catch (\think\exception\HttpResponseException $e) { throw $e; }
catch (\Throwable $e) { $this->error($e->getMessage()); }
try { RechargeRefundService::send((int)$ids); }
catch (\Throwable $e) { $this->error('申请已审核,微信结果待核实,余额保持占用:' . $e->getMessage()); }
$this->success('退款请求已处理,请查看退款状态');
}
$this->view->assign('row', $row);
return $this->view->fetch();
}
public function resolve($ids = null)
{
$row = Db::name('recharge_refund')->where('id', (int)$ids)->find();
if (!$row) $this->error('退款申请不存在');
if ($this->request->isPost()) {
$this->token();
try {
if ($this->request->post('action', '') === 'retry') RechargeRefundService::send((int)$ids);
else RechargeRefundService::sync((int)$ids);
} catch (\Throwable $e) { $this->error('处理失败,余额保持占用:' . $e->getMessage()); }
$this->success('处理完成,请查看退款状态');
}
$this->view->assign('row', $row);
return $this->view->fetch();
}
}

查看文件

@@ -30,6 +30,8 @@ class WalletLog extends Model
'settle' => '佣金结算',
'withdraw' => '余额提现',
'income' => '奖励补贴',
'recharge_refund_hold' => '充值退款占用',
'recharge_refund_release' => '充值退款余额释放',
];
}
}

查看文件

@@ -649,7 +649,7 @@
<span>系统会自动核对微信已退金额,不会重复退款。</span>
</div>
<div class="po-footer-actions">
{if in_array($row.status, ['paid', 'refunding', 'refunded', 'refund_failed'])}
{if $row.scene != 'recharge' && in_array($row.status, ['paid', 'refunding', 'refunded', 'refund_failed'])}
<button type="button" class="btn btn-info js-action-sync" data-id="{$row.id}">
<i class="fa fa-refresh"></i> 同步微信状态
</button>

查看文件

@@ -187,7 +187,7 @@
</div>
<div class="po-guide-body">
<strong>操作指引:</strong>
{if $rechargeOnly}展示用户微信在线充值记录。充值退款功能正在完善,暂不可通过退款台账发起充值退款。{else /}展示全站微信支付与原路退款记录。充值流水暂不可通过此处发起退款。{/if}
{if $rechargeOnly}可对已支付的充值订单申请原路退款,提交后占用余额,审核通过后向微信退款。{else /}展示全站微信支付与原路退款记录。充值退款请使用充值订单中的专用入口。{/if}
</div>
</div>
@@ -201,6 +201,7 @@
<!-- 表格 -->
<table id="table" class="table table-striped table-bordered table-hover"
data-operate-detail="{:$auth->check('pay_order/detail')}"
data-buttons-rechargerefund="{:$auth->check('recharge_refund/create')}"
data-operate-sync="{:$auth->check('pay_order/sync')}"
data-operate-retryrefund="{:$auth->check('pay_order/retryrefund')}"
width="100%">

查看文件

@@ -0,0 +1,8 @@
<form id="form" class="form-horizontal" role="form" data-toggle="validator" method="POST">
{:token()}
<div class="form-group"><label class="control-label col-sm-3">支付单号</label><div class="col-sm-8"><p class="form-control-static">{$pay.out_trade_no|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">充值 / 已退</label><div class="col-sm-8"><p class="form-control-static">¥{$pay.amount|htmlentities} / ¥{$pay.refund_amount|htmlentities},当前可用余额 ¥{$balance|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">退款金额</label><div class="col-sm-8"><input class="form-control" name="amount" data-rule="required" placeholder="最多两位小数"></div></div>
<div class="form-group"><label class="control-label col-sm-3">退款原因</label><div class="col-sm-8"><textarea class="form-control" name="reason" maxlength="255" data-rule="required"></textarea></div></div>
<div class="form-group"><div class="col-sm-offset-3 col-sm-8"><p>提交后将立即占用余额,审核通过后向微信发起退款。</p><button type="submit" class="btn btn-primary">提交申请</button></div></div>
</form>

查看文件

@@ -0,0 +1,15 @@
<div class="panel panel-default panel-intro">
<div class="panel-heading"><h4>充值退款申请</h4><p>退款处理中、状态未知及异常时,余额保持占用;请核实微信状态。</p></div>
<div class="panel-body">
<form class="form-inline" method="get"><label>状态 <select name="status" class="form-control"><option value="">全部</option><option value="pending">待审核</option><option value="submitting">提交中</option><option value="processing">退款处理中</option><option value="unknown">结果未知</option><option value="abnormal">异常待处理</option><option value="success">退款成功</option><option value="closed">微信关闭</option><option value="rejected">已驳回</option></select></label> <button class="btn btn-default">查询</button></form>
<div class="table-responsive"><table class="table table-striped table-bordered"><thead><tr><th>申请单号</th><th>充值支付单号</th><th>用户</th><th>金额</th><th>状态</th><th>申请原因 / 审核备注</th><th>操作人 / 审核人 ID</th><th>申请时间</th><th>操作</th></tr></thead><tbody>
{volist name="list" id="row"}
<tr><td>{$row.refund_no|htmlentities}</td><td>{$row.out_trade_no|htmlentities}</td><td>{$row.nickname|htmlentities} {$row.mobile|htmlentities}</td><td>¥{$row.amount|htmlentities}</td><td>{$row.status|htmlentities}</td><td>{$row.reason|htmlentities}<br>{$row.audit_note|htmlentities}<br>{$row.error|htmlentities}</td><td>{$row.operator_id|htmlentities} / {$row.reviewer_id|htmlentities}</td><td>{$row.createtime|date='Y-m-d H:i:s',###}</td><td>
{if $row.status == 'pending' && $auth->check('recharge_refund/review')}<a href="{:url('recharge_refund/review', ['ids'=>$row.id])}" class="btn btn-xs btn-primary btn-dialog" title="审核退款">审核</a>{/if}
{if in_array($row.status, ['submitting','unknown','processing','abnormal']) && $auth->check('recharge_refund/resolve')}<a href="{:url('recharge_refund/resolve', ['ids'=>$row.id])}" class="btn btn-xs btn-warning btn-dialog" title="处理退款">处理</a>{/if}
</td></tr>
{/volist}
</tbody></table></div>
{$list->render()}
</div>
</div>

查看文件

@@ -0,0 +1,7 @@
<form id="form" class="form-horizontal" role="form" data-toggle="validator" method="POST">
{:token()}
<div class="form-group"><label class="control-label col-sm-3">退款单号</label><div class="col-sm-8"><p class="form-control-static">{$row.refund_no|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">当前状态</label><div class="col-sm-8"><p class="form-control-static">{$row.status|htmlentities} {$row.error|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">处理方式</label><div class="col-sm-8"><select name="action" class="form-control"><option value="sync">向微信查询退款状态</option>{if in_array($row.status, ["unknown", "submitting"])}<option value="retry">核实后按原退款单号重试</option>{/if}</select></div></div>
<div class="form-group"><div class="col-sm-offset-3 col-sm-8"><button type="submit" class="btn btn-primary">确认</button></div></div>
</form>

查看文件

@@ -0,0 +1,9 @@
<form id="form" class="form-horizontal" role="form" data-toggle="validator" method="POST">
{:token()}
<div class="form-group"><label class="control-label col-sm-3">退款单号</label><div class="col-sm-8"><p class="form-control-static">{$row.refund_no|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">退款金额</label><div class="col-sm-8"><p class="form-control-static">¥{$row.amount|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">申请原因</label><div class="col-sm-8"><p class="form-control-static">{$row.reason|htmlentities}</p></div></div>
<div class="form-group"><label class="control-label col-sm-3">审核结果</label><div class="col-sm-8"><select name="action" class="form-control"><option value="approve">通过并发起微信退款</option><option value="reject">驳回并释放余额</option></select></div></div>
<div class="form-group"><label class="control-label col-sm-3">审核备注</label><div class="col-sm-8"><textarea class="form-control" name="note" maxlength="255" placeholder="驳回时必填"></textarea></div></div>
<div class="form-group"><div class="col-sm-offset-3 col-sm-8"><button type="submit" class="btn btn-primary">确认审核</button></div></div>
</form>