From 74c58c16709d80ab2eee37edac75593c744bfc17 Mon Sep 17 00:00:00 2001 From: liapples Date: Fri, 20 Aug 2021 17:59:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=80=E6=AC=BE=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E5=92=8C=E6=8B=92=E7=BB=9D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/OrderController.php | 48 ++++++- .../Extensions/Grid/AuditRefund.php | 117 ++++++++++++++++++ 2 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 app/AdminAgent/Extensions/Grid/AuditRefund.php diff --git a/app/AdminAgent/Controllers/OrderController.php b/app/AdminAgent/Controllers/OrderController.php index 23a6dda..afd335d 100644 --- a/app/AdminAgent/Controllers/OrderController.php +++ b/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('查看 ') + ->modal('查看退款详情', function (Grid\Displayers\Modal $modal) { + $modal->icon(''); + if (!$this->refund_info) { + return ''; + } + $html = ''; + $pictures = $this->refund_info['pictures'] ?? []; + if (count($pictures) > 0) { + $html = '
'; + foreach ($pictures as $value) { + $html .= '  '; + } + $html .= '
'; + } + $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(' '); + $column->append((new AuditRefund(null, 2))->setKey($this->id)); + } else if ($this->status == OrderStatus::REFUNDED) { + $column->append('已同意退款'); + } else if ($this->status == OrderStatus::REFUSED_REFUND) { + $column->append('已拒绝退款'); + } + }) + ->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('禁止删除'); }); diff --git a/app/AdminAgent/Extensions/Grid/AuditRefund.php b/app/AdminAgent/Extensions/Grid/AuditRefund.php new file mode 100644 index 0000000..e726827 --- /dev/null +++ b/app/AdminAgent/Extensions/Grid/AuditRefund.php @@ -0,0 +1,117 @@ +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 "formatHtmlAttributes()}>{$this->title}"; + } + + 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]; + } +}