Browse Source

增加退款通过和拒绝处理

dev
李可松 5 years ago
parent
commit
74c58c1670
  1. 48
      app/AdminAgent/Controllers/OrderController.php
  2. 117
      app/AdminAgent/Extensions/Grid/AuditRefund.php

48
app/AdminAgent/Controllers/OrderController.php

@ -2,10 +2,10 @@
namespace App\AdminAgent\Controllers;
use App\AdminAgent\Extensions\Grid\AuditRefund;
use App\AdminAgent\Repositories\Order;
use App\Common\OrderStatus;
use App\Common\PayType;
use App\Models\Agent;
use App\Models\Supplier;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
@ -46,7 +46,46 @@ class OrderController extends AdminController
$this->product->supplier->name,
]]);
})->xl();
$grid->column('status')->select(OrderStatus::array());
//状态及退款处理
$grid->column('status')
->if(fn() => in_array($this->status, [OrderStatus::REFUNDING, OrderStatus::REFUNDED, OrderStatus::REFUSED_REFUND]))
->display('<a style="cursor: pointer;" class="btn btn-sm btn-info" href="javascript:;">查看</a>&nbsp;')
->modal('查看退款详情', function (Grid\Displayers\Modal $modal) {
$modal->icon('');
if (!$this->refund_info) {
return '';
}
$html = '';
$pictures = $this->refund_info['pictures'] ?? [];
if (count($pictures) > 0) {
$html = '<div class="box-body">';
foreach ($pictures as $value) {
$html .= '<img data-action="preview-img" src="' . $value . '" style="max-width:80px;max-height:80px" class="img"> &nbsp;';
}
$html .= '</div>';
}
$refund_info = [
'退款单号' => $this->refund_info['refund_no'] ?? '',
'退款说明' => $this->refund_info['desc'] ?? '',
'退款前状态' => $this->refund_info['old_status'] ? OrderStatus::array()[$this->refund_info['old_status']] : '',
'退款凭据' => $html,
];
return Table::make($refund_info);
})
->then(function (Grid\Column $column) {
if ($this->status == OrderStatus::REFUNDING) {
$column->append((new AuditRefund(null, 1))->setKey($this->id))->append('&nbsp;');
$column->append((new AuditRefund(null, 2))->setKey($this->id));
} else if ($this->status == OrderStatus::REFUNDED) {
$column->append('<a style="cursor: pointer;" class="btn btn-sm btn-success" href="javascript:;">已同意退款</a>');
} else if ($this->status == OrderStatus::REFUSED_REFUND) {
$column->append('<a style="cursor: pointer;" class="btn btn-sm btn-danger" href="javascript:;">已拒绝退款</a>');
}
})
->else()
->select(OrderStatus::array());
$grid->column('pay_type')->using(PayType::array());
$grid->column('price');
$grid->column('paid_money');
@ -139,6 +178,11 @@ class OrderController extends AdminController
//不允许编辑的字段
$form->ignore(['id', 'user_id', 'agent_id', 'agent_product_id', 'product_id', 'product_ids', 'order_no',
'pay_type', 'paid_money', 'created_at', 'updated_at', 'deleted_at']);
//退款不能直接编辑
if (in_array($form->status, [OrderStatus::REFUNDED, OrderStatus::REFUSED_REFUND])) {
return $form->response()->error('请通过订单列表的”通过“和”拒绝“按钮来审核退款');
}
})->deleting(function (Form $form) {
return $form->response()->error('禁止删除');
});

117
app/AdminAgent/Extensions/Grid/AuditRefund.php

@ -0,0 +1,117 @@
<?php
namespace App\AdminAgent\Extensions\Grid;
use App\Common\OrderStatus;
use App\Models\Order;
use App\Models\UserMoneyLog;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\RowAction;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 退款审核
* Class AuditRefund
* @package App\AdminAgent\Extensions\Grid
*/
class AuditRefund extends RowAction
{
private $action;
public function __construct($title = null, $action = 1)
{
parent::__construct($title);
$this->action = $action; //$action:1=通过;2=拒绝
$this->title = $action == 1 ? '通过' : '拒绝';
}
protected function html()
{
$class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
$this->appendHtmlAttribute('class', $class);
$this->defaultHtmlAttribute('href', 'javascript:;');
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
}
public function handle(Request $request)
{
return $request->action == 1 ? $this->pass() : $this->refuse();
}
//通过退款
private function pass()
{
$agent = Admin::user();
DB::beginTransaction();
try {
//修改订单状态
$order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => $agent->id, 'status' => OrderStatus::REFUNDING]);
if (!$order) {
$this->response()->error("退款订单不存在或已处理过了")->refresh();
}
$order->status = OrderStatus::REFUNDED;
$order->save();
//查看原来的支付信息,可能存在多条支付记录
$log = UserMoneyLog::query()
->where(['user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 1])
->where('transaction_id', '<>', '')
->get();
if ($log->isEmpty()) {
throw new \Exception('未查询到该笔订单的支付信息,退款失败');
}
//将微信发起退款申请
$config = config('wechat.payment.default');
$config = array_merge($config, [
'app_id' => $agent->appid,
'mch_id' => $agent->mchid,
'key' => $agent->mchkey,
'notify_url' => route('wxpay_refund', $agent->id),
]);
$app = Factory::payment($config);
// 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
foreach ($log as $k=>$v) {
$refund_no = $order->refund_info['refund_no'] . '-' . $v['id'];
$app->refund->byTransactionId($v->transaction_id, $refund_no, $v->money, $v->money, $config);
}
//退款回调之后再存入UserMoneyLog
DB::commit();
return $this->response()->success("操作成功,已向微信申请退款,款项将原路退还")->refresh();
} catch (\Exception $e) {
DB::rollBack();
return $this->response()->error($e->getMessage());
}
}
//拒绝退款
private function refuse()
{
try {
$order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]);
if (!$order) {
$this->response()->error("退款订单不存在或已处理过了")->refresh();
}
$order->status = OrderStatus::REFUSED_REFUND;
$order->save();
return $this->response()->success("操作成功,退款已拒绝")->refresh();
} catch (\Exception $e) {
return $this->response()->error($e->getMessage());
}
}
public function confirm()
{
return ['确定要'.$this->title.'该用户的退款吗?', ''];
}
public function parameters()
{
return ['action' => $this->action];
}
}
Loading…
Cancel
Save