84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Callback\Http\Controllers;
|
|
|
|
use App\Events\OrderPaid;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Order;
|
|
use App\Models\OrderRefundTask;
|
|
use App\Services\OrderService;
|
|
use App\Services\WeChatPayService;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class WeChatPayController extends Controller
|
|
{
|
|
/**
|
|
* 订单支付结果通知
|
|
*
|
|
* @param \App\Services\WeChatPayService $weChatPayService
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function orderPaidNotify(WeChatPayService $weChatPayService)
|
|
{
|
|
return $weChatPayService->handlePaidNotify(function ($message, $fail) {
|
|
// 通信失败
|
|
if (data_get($message, 'return_code') !== 'SUCCESS') {
|
|
return $fail('通信失败');
|
|
}
|
|
|
|
// 支付失败
|
|
if (data_get($message, 'result_code') !== 'SUCCESS') {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
$order = DB::transaction(function () use ($message) {
|
|
return (new OrderService())->paySuccess($message['out_trade_no'], [
|
|
'pay_sn' => $message['transaction_id'],
|
|
'pay_way' => Order::PAY_WAY_WXPAY,
|
|
'pay_at' => Carbon::parse($message['time_end']),
|
|
]);
|
|
});
|
|
|
|
OrderPaid::dispatchIf($order->isPaid(), $order);
|
|
} catch (ModelNotFoundException | BizException $e) {
|
|
} catch (Throwable $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 订单退款结果通知
|
|
*
|
|
* @param \App\Services\WeChatPayService $weChatPayService
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function orderRefundedNotify(WeChatPayService $weChatPayService)
|
|
{
|
|
return $weChatPayService->handleRefundedNotify(function ($message, $reqInfo, $fail) {
|
|
// 通信失败
|
|
if (data_get($message, 'return_code') !== 'SUCCESS') {
|
|
return $fail('通信失败');
|
|
}
|
|
|
|
// 退款失败
|
|
if ($reqInfo['refund_status'] !== 'SUCCESS') {
|
|
$orderRefundTask = OrderRefundTask::where('sn', $reqInfo['out_refund_no'])->first();
|
|
|
|
$orderRefundTask?->update([
|
|
'status' => OrderRefundTask::STATUS_FAILED,
|
|
'failed_reason' => $reqInfo['refund_status'] === 'CHANGE' ? '退款异常' : '退款关闭',
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
}
|