6
0
Fork 0
jiqu-library-server/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php

73 lines
2.0 KiB
PHP

<?php
namespace App\Endpoint\Callback\Http\Controllers;
use App\Exceptions\BizException;
use App\Models\Order;
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 paidNotify(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;
}
$attach = json_decode($message['attach'], true);
if (! isset($attach['pay_target'])) {
return true;
}
switch ($attach['pay_target']) {
case 'order':
$this->handlePaidNotifyForOrder($message);
break;
}
return true;
});
}
/**
* 处理订单支付结果通知
*
* @param array $message
* @return void
*/
protected function handlePaidNotifyForOrder(array $message): void
{
try {
DB::transaction(function () use ($message) {
(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']),
]);
});
} catch (ModelNotFoundException|BizException $e) {
} catch (Throwable $e) {
throw $e;
}
}
}