diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index 86fcb4a..d323b2d 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -4,9 +4,11 @@ namespace App\Controller; use App\Constants\HttpEnumCode; use App\Exception\BusinessException; +use App\Model\OrderInquiry; use App\Services\BaseService; use App\Utils\Log; use Extend\Wechat\WechatPay; +use Hyperf\DbConnection\Db; use Hyperf\HttpMessage\Stream\SwooleFileStream; use Hyperf\HttpMessage\Stream\SwooleStream; use Psr\Http\Message\ResponseInterface; @@ -18,8 +20,10 @@ class CallBackController extends AbstractController * @return ResponseInterface * @throws \Throwable */ - public function patientWxPayCallBack(): ResponseInterface + public function patientWxPaySuccessCallBack(): ResponseInterface { + Db::beginTransaction(); + try { // 处理支付结果事件 $WechatPay = new WechatPay(1); @@ -28,41 +32,77 @@ class CallBackController extends AbstractController $message = $server->getRequestMessage(); if (empty($message)){ + Db::rollBack(); return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE)))); } - dump($message); + // 验证推送消息签名 + $app->getValidator()->validate($app->getRequest()); + Log::getInstance()->info("微信支付回调数据:" . json_encode($message,JSON_UNESCAPED_UNICODE)); - return $server->serve(); - }catch (\Exception $e) { - throw new BusinessException($e->getMessage()); - return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE)))); - } - } - - // 医生端微信支付回调 - public function doctorWxPayCallBack(): ResponseInterface - { - $request_params = $this->request->all(); - dump($request_params); - try { - // 处理支付结果事件 - $WechatPay = new WechatPay(2); - $app = $WechatPay->createApp(); - $server = $app->getServer(); - $message = $server->getRequestMessage(); - if (empty($message)){ + if (empty($message['out_trade_no'])){ + Db::rollBack(); + Log::getInstance()->info("微信支付回调数据错误"); return $server->serve(); -// return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE)))); } dump($message); + // 查询订单 + $params = array(); + $params['inquiry_no'] = $message['out_trade_no']; + $order_inquiry = OrderInquiry::getOne($params); + if (empty($order_inquiry)){ + Db::rollBack(); + Log::getInstance()->info("非法订单"); + return $server->serve(); + } + dump(1); + // 验证订单状态 + if ($order_inquiry['inquiry_status'] != 1){ + // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) + Db::rollBack(); + Log::getInstance()->info("订单状态错误:当前为" . $order_inquiry['inquiry_status']); + return $server->serve(); + } + dump(2); + // 支付状态无需验证,如第一次支付失败,会修改支付状态,再次支付时,会出现验证不通过的情况 + + // 修改支付状态 + $data = array(); + if ($message['trade_state'] == "SUCCESS"){ + // 支付成功 + $data['inquiry_pay_status'] = 2; + $data['pay_time'] = date('Y-m-d H:i:s',strtotime($message['success_time']));// 支付时间 + }elseif($message['trade_state'] == "CLOSED"){ + // 已关闭 + $data['inquiry_pay_status'] = 6; + }elseif($message['trade_state'] == "REVOKED"){ + // 已撤销(付款码支付) + $data['inquiry_pay_status'] = 7; + }elseif($message['trade_state'] == "USERPAYING"){ + // 用户支付中(付款码支付) + $data['inquiry_pay_status'] = 3; + }elseif($message['trade_state'] == "PAYERROR"){ + // 支付失败(其他原因,如银行返回失败) + $data['inquiry_pay_status'] = 4; + } + + $data['escrow_trade_no'] = $message['transaction_id']; + $data['updated_at'] = date('Y-m-d H:i:s',time()); + + $params = array(); + $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + OrderInquiry::edit($params,$data); + dump(3); + Db::commit(); return $server->serve(); }catch (\Exception $e) { - throw new BusinessException($e->getMessage()); -// return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE)))); + // 验证失败 + Db::rollBack(); + Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage()); + return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE)))); } } diff --git a/config/config.php b/config/config.php index f182fbe..0491ab9 100644 --- a/config/config.php +++ b/config/config.php @@ -37,12 +37,11 @@ return [ "doctor" => [ "app_id" => env('DOCTOR_WECHAT_APP_ID', 'wxc83296720404aa7b'), "secret" => env('DOCTOR_WECHAT_APP_SECRET', '817665d3763637fe66d56548f8484622'), - "notify_url" => env('DOCTOR_WECHAT_NOTIFY_URL', 'callback/wxpay/doctor'), ], "patient" => [ "app_id" => env('PATIENT_WECHAT_APP_ID', 'wx70a196902e0841b6'), "secret" => env('PATIENT_WECHAT_APP_SECRET', '2671d2f4285180ddec5a5a2b16ed50f2'), - "notify_url" => env('PATIENT_WECHAT_NOTIFY_URL', 'callback/wxpay/patient'), + "notify_url" => env('PATIENT_WECHAT_NOTIFY_URL', 'callback/wxpay/inquiry/success'), ], "pay" => [ "mch_id" => env('PATIENT_WECHAT_MCH_ID', '1636644248'), diff --git a/config/routes.php b/config/routes.php index 1ceb2b6..5174f08 100644 --- a/config/routes.php +++ b/config/routes.php @@ -374,11 +374,11 @@ Router::addGroup('/system', function () { Router::addGroup('/callback', function () { // 支付回调 Router::addGroup('/wxpay', function () { - // 患者端微信支付回调 - Router::post('/patient', [CallBackController::class, 'patientWxPayCallBack']); - - // 医生端微信支付回调 - Router::post('/doctor', [CallBackController::class, 'doctorWxPayCallBack']); + // 问诊 + Router::addGroup('/inquiry', function () { + // 支付成功回调 + Router::post('/success', [CallBackController::class, 'WxPaySuccessCallBack']); + }); }); // im回调 diff --git a/hospital-deploy.sh b/hospital-deploy.sh index 966aa98..7d39b21 100644 --- a/hospital-deploy.sh +++ b/hospital-deploy.sh @@ -36,13 +36,12 @@ JWT_ALGO=HS256 # [WECHAT] PATIENT_WECHAT_APP_ID=wx70a196902e0841b6 PATIENT_WECHAT_APP_SECRET=2671d2f4285180ddec5a5a2b16ed50f2 -PATIENT_WECHAT_NOTIFY_URL=callback/wxpay/patient +PATIENT_WECHAT_NOTIFY_URL=callback/wxpay/inquiry/success # [DOCTOR] # [WECHAT] DOCTOR_WECHAT_APP_ID=wxc83296720404aa7b DOCTOR_WECHAT_APP_SECRET=817665d3763637fe66d56548f8484622 -DOCTOR_WECHAT_NOTIFY_URL=callback/wxpay/doctor # [AMQP] AMQP_HOST=42.193.16.243