feat: 新增充值退款功能完整实现
- 新增充值退款数据库表结构及相关枚举字段 - 新增充值退款服务层核心逻辑(创建、审核、退款、回调处理) - 新增充值退款后台管理页面及操作入口 - 新增充值退款前端页面及样式调整 - 新增充值退款相关测试用例 - 新增充值退款策略校验及网关处理逻辑
这个提交包含在:
@@ -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>
|
||||
@@ -448,6 +448,8 @@ class User extends Api
|
||||
'income' => '奖励补贴入账',
|
||||
'settle' => '工单收益结算',
|
||||
'withdraw' => '余额提现出账',
|
||||
'recharge_refund_hold' => '充值退款占用',
|
||||
'recharge_refund_release' => '充值退款余额释放',
|
||||
];
|
||||
return $map[$type] ?? '资金变动';
|
||||
}
|
||||
|
||||
@@ -60,6 +60,11 @@ class Wxpay extends Api
|
||||
throw new \RuntimeException('退款回调缺少商户退款单号与商户支付单号');
|
||||
}
|
||||
|
||||
if (preg_match('/^RR[0-9]{14}[A-F0-9]{16}$/', $outRefundNo)) {
|
||||
\app\common\library\RechargeRefundService::applyResult($outRefundNo, $data);
|
||||
return $this->wxResponse(true);
|
||||
}
|
||||
|
||||
$q = Db::name('pay_order');
|
||||
if ($outRefundNo !== '') {
|
||||
$q->where('refund_no', $outRefundNo);
|
||||
|
||||
@@ -1006,6 +1006,9 @@ class PaymentService
|
||||
if (!$po) {
|
||||
throw new \InvalidArgumentException('支付交易单不存在');
|
||||
}
|
||||
if ($po['scene'] === 'recharge') {
|
||||
throw new \InvalidArgumentException('充值退款请使用专用退款审核页面同步');
|
||||
}
|
||||
$refundNo = trim((string)$po['refund_no']);
|
||||
if ($refundNo === '') {
|
||||
throw new \InvalidArgumentException('该支付单尚未生成退款单号,无需同步退款状态');
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library;
|
||||
|
||||
/** Amount checks shared by recharge refund submission and approval. */
|
||||
final class RechargeRefundPolicy
|
||||
{
|
||||
private static function cents($value)
|
||||
{
|
||||
$value = (string)$value;
|
||||
if (!preg_match('/\A(?:0|[1-9][0-9]{0,7})(?:\.[0-9]{1,2})?\z/', $value)) {
|
||||
throw new \InvalidArgumentException('金额格式不正确');
|
||||
}
|
||||
$parts = explode('.', $value, 2);
|
||||
return ((int)$parts[0] * 100) + (int)str_pad($parts[1] ?? '', 2, '0');
|
||||
}
|
||||
|
||||
public static function validateAmount($amount, $paid, $refunded, $balance)
|
||||
{
|
||||
$amount = self::cents($amount);
|
||||
$paid = self::cents($paid);
|
||||
$refunded = self::cents($refunded);
|
||||
$balance = self::cents($balance);
|
||||
if ($amount <= 0 || $refunded > $paid || $amount > $paid - $refunded || $amount > $balance) {
|
||||
throw new \InvalidArgumentException('退款金额超过充值单可退金额或账户可用余额');
|
||||
}
|
||||
return $amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
namespace app\common\library;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/** Recharge refunds reserve wallet funds before any external request. */
|
||||
final class RechargeRefundService
|
||||
{
|
||||
public static function create($payId, $amount, $reason, $operatorId)
|
||||
{
|
||||
$reason = trim((string)$reason);
|
||||
if ($reason === '' || mb_strlen($reason) > 255 || (int)$operatorId <= 0) {
|
||||
throw new \InvalidArgumentException('请填写退款原因和操作人');
|
||||
}
|
||||
$initial = Db::name('pay_order')->where('id', (int)$payId)->find();
|
||||
if (!$initial || $initial['scene'] !== 'recharge') throw new \InvalidArgumentException('充值订单不存在');
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = Db::name('user')->where('id', (int)$initial['user_id'])->lock(true)->find();
|
||||
$pay = Db::name('pay_order')->where('id', (int)$payId)->lock(true)->find();
|
||||
if (!$user || !$pay || $pay['scene'] !== 'recharge' || (int)$pay['user_id'] !== (int)$user['id'] || $pay['status'] !== 'paid') {
|
||||
throw new \InvalidArgumentException('充值订单当前状态不允许退款');
|
||||
}
|
||||
if (Db::name('recharge_refund')->where('pay_order_id', (int)$payId)->whereIn('status', ['pending','submitting','processing','unknown','abnormal'])->find()) {
|
||||
throw new \InvalidArgumentException('该充值订单已有未完成的退款申请');
|
||||
}
|
||||
$fen = RechargeRefundPolicy::validateAmount($amount, $pay['amount'], $pay['refund_amount'], $user['money']);
|
||||
$newBalance = number_format(((int)round((float)$user['money'] * 100) - $fen) / 100, 2, '.', '');
|
||||
$now = time();
|
||||
$refundNo = 'RR' . date('YmdHis', $now) . strtoupper(bin2hex(random_bytes(8)));
|
||||
$id = Db::name('recharge_refund')->insertGetId(['pay_order_id'=>(int)$payId,'user_id'=>(int)$user['id'],'amount'=>number_format($fen / 100, 2, '.', ''),'refund_no'=>$refundNo,'reason'=>$reason,'operator_id'=>(int)$operatorId,'status'=>'pending','createtime'=>$now,'updatetime'=>$now]);
|
||||
Db::name('user')->where('id', (int)$user['id'])->update(['money'=>$newBalance,'updatetime'=>$now]);
|
||||
Db::name('wallet_log')->insert(['user_type'=>'user','user_id'=>(int)$user['id'],'type'=>'recharge_refund_hold','amount'=>number_format(-$fen / 100, 2, '.', ''),'balance'=>$newBalance,'order_id'=>0,'remark'=>'充值退款占用余额(退款单 '.$refundNo.')','createtime'=>$now]);
|
||||
Db::commit();
|
||||
return Db::name('recharge_refund')->where('id', $id)->find();
|
||||
} catch (\Throwable $e) { Db::rollback(); throw $e; }
|
||||
}
|
||||
|
||||
public static function begin($refundId, $reviewerId, $note)
|
||||
{
|
||||
if ((int)$reviewerId <= 0 || mb_strlen(trim((string)$note)) > 255) throw new \InvalidArgumentException('审核信息不正确');
|
||||
$changed = Db::name('recharge_refund')->where('id', (int)$refundId)->where('status', 'pending')->update([
|
||||
'status'=>'submitting', 'reviewer_id'=>(int)$reviewerId, 'audit_note'=>trim((string)$note), 'updatetime'=>time()
|
||||
]);
|
||||
if (!$changed) throw new \InvalidArgumentException('退款申请不在待审核状态');
|
||||
return Db::name('recharge_refund')->where('id', (int)$refundId)->find();
|
||||
}
|
||||
|
||||
public static function applyResult($refundNo, array $data)
|
||||
{
|
||||
if (empty($data['out_refund_no']) || !hash_equals((string)$refundNo, (string)$data['out_refund_no'])) throw new \InvalidArgumentException('退款单号不匹配');
|
||||
$initial = Db::name('recharge_refund')->where('refund_no', $refundNo)->find();
|
||||
if (!$initial) throw new \InvalidArgumentException('充值退款申请不存在');
|
||||
$status = strtoupper((string)($data['refund_status'] ?? $data['status'] ?? ''));
|
||||
if (!in_array($status, ['SUCCESS','PROCESSING','CLOSED','ABNORMAL'], true)) throw new \InvalidArgumentException('微信退款状态无效');
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = Db::name('user')->where('id', (int)$initial['user_id'])->lock(true)->find();
|
||||
$pay = Db::name('pay_order')->where('id', (int)$initial['pay_order_id'])->lock(true)->find();
|
||||
$refund = Db::name('recharge_refund')->where('id', (int)$initial['id'])->lock(true)->find();
|
||||
if (!$user || !$pay || !$refund || $pay['scene'] !== 'recharge') throw new \RuntimeException('退款关联数据不存在');
|
||||
if (in_array($refund['status'], ['success','closed'], true)) {
|
||||
if ($status !== 'PROCESSING' && strtolower($status) !== $refund['status']) throw new \RuntimeException('退款终态不一致,请人工核查');
|
||||
if ($status === 'SUCCESS' && ((string)($data['refund_id'] ?? '') !== (string)$refund['wx_refund_id'] || (int)($data['amount']['refund'] ?? 0) !== (int)round((float)$refund['amount'] * 100) || (int)($data['amount']['total'] ?? 0) !== (int)round((float)$pay['amount'] * 100))) throw new \InvalidArgumentException('重复退款通知与原记录不一致');
|
||||
Db::commit();
|
||||
return $refund;
|
||||
}
|
||||
if (!in_array($refund['status'], ['submitting','unknown','processing','abnormal'], true)) throw new \RuntimeException('退款尚未提交微信');
|
||||
$now = time();
|
||||
$update = ['status'=>strtolower($status), 'wx_refund_id'=>(string)($data['refund_id'] ?? ''), 'updatetime'=>$now];
|
||||
if ($status === 'SUCCESS') {
|
||||
$fen = (int)round((float)$refund['amount'] * 100);
|
||||
if (empty($data['refund_id']) || (int)($data['amount']['refund'] ?? 0) !== $fen || (int)($data['amount']['total'] ?? 0) !== (int)round((float)$pay['amount'] * 100)) throw new \RuntimeException('微信退款金额或单号不匹配');
|
||||
$total = (int)round((float)$pay['refund_amount'] * 100) + $fen;
|
||||
if ($total > (int)round((float)$pay['amount'] * 100)) throw new \RuntimeException('累计退款超过充值金额');
|
||||
Db::name('pay_order')->where('id', (int)$pay['id'])->update(['refund_amount'=>number_format($total / 100, 2, '.', ''),'status'=>$total === (int)round((float)$pay['amount'] * 100) ? 'refunded' : 'paid','refund_status'=>'success','refund_no'=>$refundNo,'refund_id'=>$data['refund_id'],'refund_time'=>$now,'updatetime'=>$now]);
|
||||
} elseif ($status === 'CLOSED') {
|
||||
$balance = number_format(((int)round((float)$user['money'] * 100) + (int)round((float)$refund['amount'] * 100)) / 100, 2, '.', '');
|
||||
Db::name('user')->where('id', (int)$user['id'])->update(['money'=>$balance,'updatetime'=>$now]);
|
||||
Db::name('wallet_log')->insert(['user_type'=>'user','user_id'=>(int)$user['id'],'type'=>'recharge_refund_release','amount'=>$refund['amount'],'balance'=>$balance,'order_id'=>0,'remark'=>'充值退款关闭释放余额(退款单 '.$refundNo.')','createtime'=>$now]);
|
||||
}
|
||||
Db::name('recharge_refund')->where('id', (int)$refund['id'])->update($update);
|
||||
Db::commit();
|
||||
return Db::name('recharge_refund')->where('id', (int)$refund['id'])->find();
|
||||
} catch (\Throwable $e) { Db::rollback(); throw $e; }
|
||||
}
|
||||
|
||||
public static function sync($id)
|
||||
{
|
||||
$refund = Db::name('recharge_refund')->where('id', (int)$id)->find();
|
||||
if (!$refund || !in_array($refund['status'], ['submitting','unknown','processing','abnormal'], true)) throw new \InvalidArgumentException('当前状态不支持查询');
|
||||
$wx = new WxPayV3Service();
|
||||
return self::applyResult($refund['refund_no'], $wx->queryRefundByOutRefundNo($refund['refund_no']));
|
||||
}
|
||||
|
||||
public static function send($id)
|
||||
{
|
||||
$lock = 'yjyf_recharge_refund_' . (int)$id;
|
||||
$rows = Db::query('SELECT GET_LOCK(?, 2) AS acquired', [$lock]);
|
||||
if (empty($rows) || (int)$rows[0]['acquired'] !== 1) throw new \RuntimeException('退款正在处理中');
|
||||
try {
|
||||
$refund = Db::name('recharge_refund')->where('id', (int)$id)->find();
|
||||
if (!$refund || !in_array($refund['status'], ['submitting','unknown'], true)) throw new \InvalidArgumentException('当前状态不支持提交');
|
||||
$pay = Db::name('pay_order')->where('id', (int)$refund['pay_order_id'])->find();
|
||||
if (!$pay || $pay['scene'] !== 'recharge') throw new \RuntimeException('充值订单不存在');
|
||||
$wx = new WxPayV3Service();
|
||||
if ($refund['status'] === 'unknown') {
|
||||
try {
|
||||
return self::applyResult($refund['refund_no'], $wx->queryRefundByOutRefundNo($refund['refund_no']));
|
||||
} catch (\Throwable $e) {
|
||||
$failure = WxPayV3Service::extractFailInfo($e);
|
||||
if (($failure['code'] ?? '') !== 'RESOURCE_NOT_EXISTS') throw $e;
|
||||
}
|
||||
}
|
||||
try {
|
||||
$data = $wx->refund(['out_trade_no'=>$pay['out_trade_no'],'out_refund_no'=>$refund['refund_no'],'refund_amount'=>$refund['amount'],'total_amount'=>$pay['amount'],'reason'=>$refund['reason']]);
|
||||
} catch (\Throwable $e) {
|
||||
Db::name('recharge_refund')->where('id', (int)$id)->whereIn('status', ['submitting','unknown'])->update(['status'=>'unknown','error'=>mb_substr($e->getMessage(),0,500),'updatetime'=>time()]);
|
||||
throw $e;
|
||||
}
|
||||
return self::applyResult($refund['refund_no'], $data);
|
||||
} finally { Db::query('SELECT RELEASE_LOCK(?)', [$lock]); }
|
||||
}
|
||||
|
||||
public static function reject($refundId, $reviewerId, $note)
|
||||
{
|
||||
$note = trim((string)$note);
|
||||
if ($note === '' || mb_strlen($note) > 255 || (int)$reviewerId <= 0) throw new \InvalidArgumentException('请填写驳回原因');
|
||||
$initial = Db::name('recharge_refund')->where('id', (int)$refundId)->find();
|
||||
if (!$initial) throw new \InvalidArgumentException('退款申请不存在');
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = Db::name('user')->where('id', (int)$initial['user_id'])->lock(true)->find();
|
||||
$refund = Db::name('recharge_refund')->where('id', (int)$refundId)->lock(true)->find();
|
||||
if (!$user || !$refund || $refund['status'] !== 'pending') throw new \InvalidArgumentException('当前状态不允许驳回');
|
||||
$balance = number_format(((int)round((float)$user['money'] * 100) + (int)round((float)$refund['amount'] * 100)) / 100, 2, '.', '');
|
||||
$now = time();
|
||||
Db::name('user')->where('id', (int)$user['id'])->update(['money'=>$balance,'updatetime'=>$now]);
|
||||
Db::name('wallet_log')->insert(['user_type'=>'user','user_id'=>(int)$user['id'],'type'=>'recharge_refund_release','amount'=>$refund['amount'],'balance'=>$balance,'order_id'=>0,'remark'=>'充值退款驳回释放余额(退款单 '.$refund['refund_no'].')','createtime'=>$now]);
|
||||
Db::name('recharge_refund')->where('id', (int)$refundId)->update(['status'=>'rejected','reviewer_id'=>(int)$reviewerId,'audit_note'=>$note,'updatetime'=>$now]);
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) { Db::rollback(); throw $e; }
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户