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

68 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(data_get($message, 'attach'), true);
if (! isset($attach['pay_target'])) {
return true;
}
switch ($attach['pay_target']) {
// 支付订单
case 'order':
try {
DB::beginTransaction();
(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']),
]);
DB::commit();
} catch (BizException|ModelNotFoundException $e) {
DB::rollBack();
} catch (Throwable $e) {
DB::rollBack();
throw $e;
}
break;
}
return true;
});
}
}