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) { throw new \Exception("退款订单不存在或已处理过了"); } /** * 【注意】线下付款退款暂不支持,若要开启,需要同时扣回供应商的余额,否则供应商可以白嫖 * 详见:App\AdminAgent\Extensions\Grid\ChangeOrderStatus */ if ($order->pay_type == PayType::OFFLINE) { throw new \Exception("线下支付退款功能暂未开放"); } //查看原来的支付信息,可能存在多条支付记录 $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('未查询到该笔订单的支付信息,退款失败'); } $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']); if (!isset($setting['payee_appid'], $setting['payee_mchid'], $setting['payee_mchkey'])) { throw new \Exception('获取系统配置失败'); } //向微信发起退款申请 $config = config('wechat.payment.default'); $config = array_merge($config, [ 'app_id' => $setting['payee_appid'], 'mch_id' => $setting['payee_mchid'], 'key' => $setting['payee_mchkey'], 'notify_url' => route('wxpay_refund', $agent->id), 'cert_path' => env('WECHAT_CERT'), 'key_path' => env('WECHAT_KEY'), ]); $app = Factory::payment($config); // 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数 $refund_money = 0; foreach ($log as $k=>$v) { $refund_money = $refund_money + $v->money; $refund_no = $order->refund_info['refund_no'] . '-' . $v['id']; $money = intval($v->money * 100); $result = $app->refund->byTransactionId($v->transaction_id, $refund_no, $money, $money); //存入UserMoneyLog if (isset($result['return_code'], $result['result_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { UserMoneyLog::query()->insert([ 'user_id' => $order->user_id, 'agent_id' => $order->agent_id, 'money' => $result['refund_fee'] / 100, 'order_id' => $order->id, 'type' => 2, 'desc' => DB::raw("LEFT('退款:{$order->title}', 250)"), 'transaction_id' => $result['transaction_id'], 'created_at' => now(), //模型没有updated_at,无法自动写入时间 ]); } else { throw new \Exception("操作失败,失败原因:" . ($result['return_msg']??'未知')); } } //保存到订单表 $order->verify_code = ''; //清空核销码 $order->status = OrderStatus::REFUNDED; $order->paid_money = DB::raw('`paid_money` - ' . $refund_money); $order->save(); DB::commit(); return $this->response()->success("操作成功,款项将原路退还")->refresh(); } catch (\Exception $e) { DB::rollBack(); return $this->response()->error($e->getMessage())->refresh(); } } //拒绝退款 private function refuse() { try { $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]); if (!$order) { return $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]; } }