文件
yjyf/server/application/admin/controller/PayOrder.php
MiAn 19651c0899 feat: 重构商品定价系统,新增步梯搬运费配置与支付台账统计
- 新增商品服务定价配置表,支持多规格服务价格体系
- 新增步梯搬运费配置功能,可按楼层阶梯收费
- 重构支付订单台账,新增核心数据统计卡片
- 优化下单流程,支持定价配置校验与增项计算
- 整合财务配置,移除独立充值配置菜单
2026-11-18 08:52:12 +08:00

174 行
6.9 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\library\PaymentService;
use think\Db;
use think\exception\PDOException;
/**
* 微信支付退款台账
*
* 展示全站微信支付流水(fa_pay_order)的支付/退款状态、网关回执与异常诊断,
* 提供人工同步微信退款状态与重发原路退款入口(复用 PaymentService 防资损校验)。
*
* @icon fa fa-undo
*/
class PayOrder extends Backend
{
/**
* PayOrder模型对象(只读台账,禁止新增/编辑/删除)
* @var \app\admin\model\PayOrder
*/
protected $model = null;
protected $relationSearch = true;
protected $searchFields = 'out_trade_no,refund_no,transaction_id,order.order_no,user.nickname,user.mobile';
protected $noNeedRight = [];
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\PayOrder;
$this->view->assign('statusList', $this->model->getStatusList());
$this->view->assign('refundStatusList', $this->model->getRefundStatusList());
$this->view->assign('sceneList', $this->model->getSceneList());
}
/**
* 退款台账列表(微信支付/退款流水全量)
*/
public function index()
{
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
$stats = [
'total_count' => $this->model->count(),
'total_paid_amount' => (float)$this->model->whereIn('status', ['paid', 'refunded', 'refunding', 'refund_failed'])->sum('amount'),
'total_refund_amount' => (float)$this->model->sum('refund_amount'),
'refunding_count' => $this->model->where('status', 'refunding')->whereOr('refund_status', 'processing')->count(),
'abnormal_count' => $this->model->whereIn('status', ['pay_failed', 'refund_failed'])->whereOr('refund_status', 'abnormal')->count(),
];
$this->view->assign('stats', $stats);
return $this->view->fetch();
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = $this->model
->with(['order', 'user'])
->where($where)
->order($sort, $order)
->paginate($limit);
$result = ['total' => $list->total(), 'rows' => $list->items()];
return json($result);
}
/**
* 主动向微信查询该笔流水的最新支付/退款状态并落库
*/
public function sync($ids = null)
{
$id = (int)$ids;
$po = Db::name('pay_order')->where('id', $id)->find();
if (!$po) {
$this->error(__('No Results were found'));
}
if (!in_array($po['status'], ['paid', 'refunding', 'refunded', 'refund_failed'], true)) {
$this->error('该流水当前状态(待支付/已关闭)无需同步');
}
try {
$after = PaymentService::syncRefundPayOrder($id);
} catch (\Throwable $e) {
$this->error('同步微信状态失败:' . $e->getMessage());
return;
}
$this->success('微信状态同步完成:' . PaymentService::payOrderStatusLabel($after['status'])
. '(退款' . PaymentService::refundStatusLabel($after['refund_status']) . ',累计已退 ¥' . $after['refund_amount'] . ')');
}
/**
* 对该笔流水重发/补发微信原路退款(自动退该笔剩余可退金额)
*/
public function retryrefund($ids = null)
{
$id = (int)$ids;
$po = Db::name('pay_order')->where('id', $id)->find();
if (!$po) {
$this->error(__('No Results were found'));
}
$left = round((float)$po['amount'] - (float)$po['refund_amount'], 2);
if ($left <= 0) {
$this->error('该流水可退余额为 0,无需再次退款');
}
try {
$after = PaymentService::refundSinglePayOrder($id, '平台人工重发原路退款(退款台账)');
} catch (\Throwable $e) {
$this->error('重发退款失败:' . $e->getMessage());
return;
}
$statusLabel = PaymentService::payOrderStatusLabel($after['status'] ?? '');
$refundLabel = PaymentService::refundStatusLabel($after['refund_status'] ?? 'none');
$this->success('退款请求已提交:' . $statusLabel . '(' . $refundLabel . ',累计已退 ¥' . $after['refund_amount'] . ')');
}
/**
* 流水详情弹窗(微信网关回执、异常诊断、关联订单)
*/
public function detail($ids = null)
{
$row = Db::name('pay_order')->where('id', (int)$ids)->find();
if (!$row) {
$this->error(__('No Results were found'));
}
$order = null;
if (!empty($row['order_id'])) {
$order = Db::name('order')->where('id', (int)$row['order_id'])->field('id,order_no,service_name,pay_amount,pay_type,status,user_id')->find();
if ($order) {
$orderModel = new \app\admin\model\Order();
$orderStatusList = $orderModel->getStatusList();
$order['status_text'] = $orderStatusList[(int)$order['status']] ?? '未知状态';
}
}
$userId = !empty($row['user_id']) ? (int)$row['user_id'] : ($order ? (int)$order['user_id'] : 0);
$user = $userId ? Db::name('user')->where('id', $userId)->field('id,nickname,username,mobile,avatar')->find() : null;
$refundableAmount = round((float)$row['amount'] - (float)$row['refund_amount'], 2);
$sceneMap = [
'order' => '订单支付',
'quote' => '报价支付',
'diff' => '增项补差',
'batch' => '批次支付',
'recharge' => '用户充值'
];
$sceneText = $sceneMap[$row['scene']] ?? $row['scene'];
$this->view->assign('row', $row);
$this->view->assign('order', $order ?: []);
$this->view->assign('user', $user ?: []);
$this->view->assign('refundableAmount', $refundableAmount);
$this->view->assign('sceneText', $sceneText);
$this->view->assign('statusText', PaymentService::payOrderStatusLabel($row['status']));
$this->view->assign('refundStatusText', PaymentService::refundStatusLabel($row['refund_status']));
return $this->view->fetch();
}
/**
* 台账为审计数据,禁止后台手工新增/编辑/删除
*/
public function add()
{
$this->error('支付流水由交易系统自动生成,禁止手工新增');
}
public function edit($ids = null)
{
$this->error('支付流水不可手工编辑,如需退款请使用行内"同步/重发退款"操作');
}
public function del($ids = null)
{
$this->error('支付流水为资金审计数据,禁止删除');
}
}