74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Callback\Http\Controllers;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Services\PayService;
|
|
use App\Services\WeChatPayService;
|
|
use EasyWeChat\Factory;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class YzkWeChatPayController extends Controller
|
|
{
|
|
/**
|
|
* 支付结果通知
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function paidNotify()
|
|
{
|
|
$weChatPayService = new WeChatPayService(Factory::payment(config('wechat.payment.yzk')));
|
|
|
|
return $weChatPayService->handlePaidNotify(function ($message, $fail) {
|
|
$this->log('paid notify', $message);
|
|
|
|
// 通信失败
|
|
if (data_get($message, 'return_code') !== 'SUCCESS') {
|
|
return $fail('通信失败');
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($message) {
|
|
$payService = new PayService();
|
|
|
|
if (data_get($message, 'result_code') === 'SUCCESS') {
|
|
return $payService->handleSuccessByPaySerialNumber($message['out_trade_no'], [
|
|
'out_trade_no' => $message['transaction_id'],
|
|
'pay_at' => Carbon::parse($message['time_end']),
|
|
]);
|
|
} elseif (data_get($message, 'result_code') === 'FAIL') {
|
|
return $payService->handleFailedByPaySerialNumber($message['out_trade_no'], [
|
|
'out_trade_no' => $message['transaction_id'] ?? null,
|
|
'failed_reason' => '['.$message['err_code'].']'.$message['err_code_des'],
|
|
]);
|
|
}
|
|
});
|
|
} catch (ModelNotFoundException | BizException $e) {
|
|
} catch (Throwable $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 微信回调日志
|
|
*
|
|
* @param string $message
|
|
* @param array $context
|
|
* @return void
|
|
*/
|
|
protected function log(string $message, array $context = [])
|
|
{
|
|
return Log::build([
|
|
'driver' => 'daily',
|
|
'path' => storage_path('logs/yzk-wxpay-notify.log'),
|
|
])->info($message, $context);
|
|
}
|
|
}
|