海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

135 lines
4.2 KiB

<?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) {
return $this->response()->error("退款订单不存在或已处理过了")->refresh();
}
$order->verify_code = ''; //清空核销码
$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' => 'wxb35ef055a4dd8ad4',
'mch_id' => '1606181693',
'key' => 'lfyyhyz8888888888888888888888888',
'notify_url' => route('wxpay_refund', $agent->id),
'cert_path' => env('WECHAT_CERT'),
'key_path' => env('WECHAT_KEY'),
]);
$app = Factory::payment($config);
// 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
foreach ($log as $k=>$v) {
$refund_no = $order->refund_info['refund_no'] . '-' . $v['id'];
$money = intval(0 - $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 {
return $this->response()->error("操作失败,失败原因:" . ($result['return_msg']??'未知'))->refresh();
}
}
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) {
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];
}
}