From 20680191459da4ce4901a9e013fba63aef7ab2a6 Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Tue, 1 Aug 2023 13:31:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A3=80=E6=B5=8B=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=9B=9E=E8=B0=83=EF=BC=8C=E6=94=AF=E4=BB=98=E9=80=80?= =?UTF-8?q?=E6=AC=BE=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CallBackController.php | 208 +++++++++++++++++++++++++- app/Model/OrderDetectionRefund.php | 84 +++++++++++ 2 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 app/Model/OrderDetectionRefund.php diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index 188ff6b..bdc4146 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -9,6 +9,8 @@ use App\Exception\BusinessException; use App\Model\BasicLogisticsCompany; use App\Model\Hospital; use App\Model\MessageIm; +use App\Model\OrderDetection; +use App\Model\OrderDetectionRefund; use App\Model\OrderInquiry; use App\Model\OrderInquiryCoupon; use App\Model\OrderInquiryRefund; @@ -247,7 +249,7 @@ class CallBackController extends AbstractController // 验证订单支付状态 if (in_array($order_inquiry['inquiry_pay_status'], [1, 3, 4, 5, 6, 7])) { // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) - Log::getInstance()->error("队列执行失败原因:订单未支付"); + Log::getInstance()->info("订单支付状态错误:当前为" . $order_inquiry['inquiry_pay_status']); return $server->serve(); } @@ -265,7 +267,7 @@ class CallBackController extends AbstractController if (empty($inquiry_refund_status)) { // 错误,无退款状态 - Log::getInstance()->error("队列执行失败原因:订单未支付"); + Log::getInstance()->info("订单支付状态错误:未接收到退款状态"); return $this->wxPayErrorReturn("退款状态错误"); } @@ -1102,4 +1104,206 @@ class CallBackController extends AbstractController ); } + /** + * 患者端检测支付回调 + * @return ResponseInterface + * @throws \Throwable + */ + public function wxPayDetectionSuccessCallBack(): ResponseInterface + { + try { + // 处理支付结果事件 + $WechatPay = new WechatPay(1, 3); + $app = $WechatPay->createApp(); + $server = $app->getServer(); + + $message = $server->getRequestMessage(); + if (empty($message)) { + return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "问诊微信支付回调数据为空"], JSON_UNESCAPED_UNICODE)))); + } + + // 验证推送消息签名 + $app->getValidator()->validate($app->getRequest()); + + Log::getInstance()->info("检测微信支付回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE)); + + if (empty($message['out_trade_no'])) { + Log::getInstance()->info("检测微信支付回调数据处理失败,缺少外部订单号"); + return $server->serve(); + } + + // 查询订单 + $params = array(); + $params['detection_no'] = $message['out_trade_no']; + $order_detection = OrderDetection::getOne($params); + if (empty($order_detection)) { + Log::getInstance()->info("检测微信支付回调数据处理失败,无订单数据"); + return $server->serve(); + } + + // 验证订单状态 + if ($order_detection['detection_status'] != 1) { + // 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消) + Log::getInstance()->info("检测微信支付回调数据处理失败,订单状态当前为" . $order_detection['detection_status']); + return $server->serve(); + } + + // 支付状态无需验证,如第一次支付失败,会修改支付状态,再次支付时,会出现验证不通过的情况 + + // 修改支付状态 + $data = array(); + if ($message['trade_state'] == "SUCCESS") { + // 支付成功 + $data['detection_pay_status'] = 2; + $data['pay_time'] = date('Y-m-d H:i:s', strtotime($message['success_time']));// 支付时间 + $data['detection_status'] = 2;// 2:待绑定 + if (empty($message['amount'])){ + Log::getInstance()->error("检测微信支付回调数据处理失败:无支付金额"); + return $this->wxPayErrorReturn("检测微信支付回调数据处理失败:无支付金额"); + } + + // 实际付款金额 + $data['payment_amount_total'] = $message['amount']['payer_total']; + } elseif ($message['trade_state'] == "CLOSED") { + // 已关闭 + $data['detection_pay_status'] = 6; + } elseif ($message['trade_state'] == "REVOKED") { + // 已撤销(付款码支付) + $data['detection_pay_status'] = 7; + } elseif ($message['trade_state'] == "USERPAYING") { + // 用户支付中(付款码支付) + $data['detection_pay_status'] = 3; + } elseif ($message['trade_state'] == "PAYERROR") { + // 支付失败(其他原因,如银行返回失败) + $data['detection_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_detection_id'] = $order_detection['order_detection_id']; + OrderDetection::editOrderDetection($params, $data); + } catch (\Exception $e) { + // 验证失败 + Log::getInstance()->error("问诊微信支付回调数据处理失败:" . $e->getMessage()); + return $this->wxPayErrorReturn($e->getMessage()); + } + + Log::getInstance()->info("检测微信支付回调处理成功"); + + return $server->serve(); + } + + /** + * 微信检测退款回调 + * @return ResponseInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws \Throwable + */ + public function wxPayDetectionRefundCallBack(): ResponseInterface + { + Db::beginTransaction(); + + try { + // 处理支付结果事件 + $WechatPay = new WechatPay(1, 1); + $app = $WechatPay->createApp(); + $server = $app->getServer(); + + $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)))); + } + + // 验证推送消息签名 + $app->getValidator()->validate($app->getRequest()); + + Log::getInstance()->info("微信退款回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE)); + + if (empty($message['out_trade_no'])) { + Log::getInstance()->info("微信退款回调数据错误"); + return $server->serve(); + } + + // 验证订单数据 + $params = array(); + $params['detection_no'] = $message['out_trade_no']; + $order_detection = OrderDetection::getOne($params); + if (empty($order_detection)) { + Log::getInstance()->info("非法订单"); + return $server->serve(); + } + + // 验证订单状态 + if ($order_detection['detection_status'] == 1) { + // 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消) + Log::getInstance()->info("订单状态错误:当前为" . $order_detection['detection_status']); + return $server->serve(); + } + + // 验证订单退款状态 + if ($order_detection['detection_refund_status'] == 3) { + // 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + Log::getInstance()->info("订单退款状态错误:当前为" . $order_detection['detection_refund_status']); + return $server->serve(); + } + + // 验证订单支付状态 + if (in_array($order_detection['detection_pay_status'], [1, 3, 4, 5, 6, 7])) { + // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) + Log::getInstance()->info("订单支付状态错误:当前为" . $order_detection['detection_pay_status']); + return $server->serve(); + } + + // 退款状态 + if ($message['refund_status'] == "SUCCESS") { + // 退款成功 + $detection_refund_status = 3; + } elseif ($message['refund_status'] == "CLOSED") { + // 退款关闭 + $detection_refund_status = 5; + } elseif ($message['refund_status'] == "ABNORMAL") { + // 退款异常 + $detection_refund_status = 6; + } + + if (empty($detection_refund_status)) { + // 错误,无退款状态 + Log::getInstance()->error("队列执行失败原因:订单未支付"); + return $this->wxPayErrorReturn("退款状态错误"); + } + + // 修改订单 + $data = array(); + $data['detection_refund_status'] = $detection_refund_status; + + $params = array(); + $params['order_detection_id'] = $order_detection['order_detection_id']; + OrderDetection::editOrderDetection($params, $data); + + // 修改退款订单 + $data = array(); + $data['detection_refund_status'] = $detection_refund_status; + $data['success_time'] = $message['success_time']; + + $params = array(); + $params['order_detection_id'] = $order_detection['order_detection_id']; + OrderDetectionRefund::edit($params, $data); + + Db::commit(); + } catch (\Exception $e) { + // 验证失败 + Db::rollBack(); + Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage()); + return $this->wxPayErrorReturn($e->getMessage()); + } + + Log::getInstance()->info("微信退款回调处理成功"); + + return $server->serve(); + } + } \ No newline at end of file diff --git a/app/Model/OrderDetectionRefund.php b/app/Model/OrderDetectionRefund.php new file mode 100644 index 0000000..4b5b5cb --- /dev/null +++ b/app/Model/OrderDetectionRefund.php @@ -0,0 +1,84 @@ +first($fields); + } + + /** + * 获取数据-多 + * @param array $params + * @param array $fields + * @return Collection|array + */ + public static function getList(array $params = [], array $fields = ['*']): Collection|array + { + return self::where($params)->get($fields); + } + + /** + * 新增-批量 + * @param array $data 新增数据 + * @return \Hyperf\Database\Model\Model|OrderDetectionRefund + */ + public static function add(array $data): \Hyperf\Database\Model\Model|OrderDetectionRefund + { + return self::create($data); + } + + /** + * 修改-批量 + * @param array $params + * @param array $data + * @return int + */ + public static function edit(array $params = [], array $data = []): int + { + return self::where($params)->update($data); + } +}