2 changed files with 163 additions and 2 deletions
-
48app/AdminAgent/Controllers/OrderController.php
-
117app/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]; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue