cancelUnpayInquiryOrder($order['order_id'], $cancel_reason, $cancel_remarks); } elseif ($order['order_type'] == 2) { // 药品订单 $OrderProductService = new OrderProductService(); $result = $OrderProductService->cancelUnpayProductOrder($order['order_id'], $cancel_reason, $cancel_remarks); } elseif ($order['order_type'] == 3) { // 检测订单 $DetectionService = new DetectionService(); $result = $DetectionService->cancelUnpayDetectionOrder($order['order_id'], $cancel_reason, $cancel_remarks); } } catch (\Throwable $e) { $result['status'] = 0; $result['message'] = $e->getMessage(); } return $result; } /** * 发起订单退款 * @param string $order_no * @param string $refund_reason * @return void */ public function orderRefund(string $order_no, string $refund_reason): void { // 检测参数 try { // 获取订单数据 $params = array(); $params['order_no'] = $order_no; $order = Order::getOne($params); if (empty($order)) { throw new BusinessException("未查询到对应订单数据"); } // 检测订单退款状态 if (in_array($order['refund_status'], [2, 3, 5])) { // 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) throw new BusinessException("订单退款状态错误"); } // 检测支付状态 if ($order['pay_status'] != 2) { // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) throw new BusinessException("订单支付状态错误"); } // 取消订单 $order_data = array(); $order_data['cancel_status'] = 1; $order_data['cancel_time'] = date("Y-m-d H:i:s", time()); $order_data['cancel_remarks'] = "医生未接诊"; $order_data['updated_at'] = date("Y-m-d H:i:s", time()); $params = array(); $params['order_id'] = $order['order_id']; Order::edit($params, $order_data); // 处理对应订单 if ($order['order_type'] == 1) { // 检测问诊订单可退款状态 $this->checkOrderInquiryStatusForRefund($order['order_no']); } elseif ($order['order_type'] == 2) { // 检测药品订单可退款状态 $this->checkOrderProductStatusForRefund($order['order_no']); } elseif ($order['order_type'] == 3) { // 检测检测订单可退款状态 $this->checkOrderDetectionStatusForRefund($order['order_no']); } else { throw new BusinessException("订单类型错误"); } }catch (\Throwable $e){ throw new BusinessException($e->getMessage()); } try { // 系统退款编号 $generator = $this->container->get(IdGeneratorInterface::class); $refund_no = $generator->generate(); // 检测订单金额 if ($order['payment_amount_total'] > 0) { // 发起退款 $WechatPay = new WechatPay(1, $order['order_type']); $options = array(); $options['transaction_id'] = $order['escrow_trade_no']; $options['out_refund_no'] = $refund_no; $options['reason'] = $refund_reason; $options['amount'] = [ 'refund' => (int)round($order['payment_amount_total'] * 100), 'total' => (int)round($order['payment_amount_total'] * 100), 'currency' => "CNY", ]; $refund_result = $WechatPay->refund($options); // 处理订单退款状态 // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) $success_time = ""; if ($refund_result['status'] == "SUCCESS") { // 退款成功 $refund_status = 3; $success_time = $refund_result['success_time']; } elseif ($refund_result['status'] == "CLOSED") { // 退款关闭 $refund_status = 5; } elseif ($refund_result['status'] == "PROCESSING") { // 退款处理中 $refund_status = 2; } elseif ($refund_result['status'] == "ABNORMAL") { // 退款异常,此情况不处理,进行短信通知 throw new BusinessException("订单退款状态异常"); } else { throw new BusinessException("订单退款状态错误"); } $refund_id = $refund_result['refund_id']; } else { // 药品订单不存在模拟退款 if ($order['order_type'] == 2){ throw new BusinessException("订单金额错误"); } // 模拟退款 $refund_status = 3; $generator = $this->container->get(IdGeneratorInterface::class); $refund_id = "模拟退款:" . $generator->generate(); $success_time = date("Y-m-d H:i:s", time()); // 模拟退款时手动退还优惠卷 if (!empty($order['coupon_amount_total']) && $order['coupon_amount_total'] > 0) { // 获取该订单全部优惠卷数据 $params = array(); $params['order_id'] = $order['order_id']; $order_coupons = OrderCoupon::getList($params); if (!empty($order_coupons)) { $userCouponService = new UserCouponService(); foreach ($order_coupons as $order_coupon) { // 退还优惠卷 $userCouponService->returnUserCoupon($order_coupon['user_coupon_id']); // 发送站内消息-优惠卷退还 $MessagePush = new MessagePush($order_coupon['user_id']); $MessagePush->patientRefundCoupon($order_coupon['coupon_name']); } } } } // 新增退款表 $data = array(); $data['order_id'] = $order['order_id']; $data['patient_id'] = $order['patient_id']; $data['order_no'] = $order['order_no']; $data['refund_no'] = $refund_no; $data['inquiry_refund_no'] = $refund_no; $data['refund_id'] = $refund_id; $data['refund_status'] = $refund_status; $data['refund_total'] = $order['payment_amount_total']; $data['refund_reason'] = $refund_reason; if ($refund_status == 3 && !empty($success_time)) { $data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间 } $order_refund = OrderRefund::addOrderRefund($data); if (empty($order_refund)) { throw new BusinessException("添加退款表失败"); } switch ($order['order_type']) { case 1: // 问诊订单 // 获取订单数据 $params = array(); $params['inquiry_no'] = $order_no; $order_inquiry = OrderInquiry::getOne($params); if (empty($order_inquiry)) { throw new BusinessException("订单数据为空"); } // 新增退款表 $data = array(); $data['patient_id'] = $order['patient_id']; $data['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; $data['inquiry_no'] = $order_inquiry['inquiry_no']; $data['inquiry_refund_no'] = $refund_no; $data['refund_id'] = $refund_id; $data['inquiry_refund_status'] = $refund_status; $data['refund_total'] = $order_inquiry['payment_amount_total']; $data['refund_reason'] = $refund_reason; if ($refund_status == 3 && !empty($success_time)) { $data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间 } $order_inquiry_refund = OrderInquiryRefund::addOrderInquiryRefund($data); if (empty($order_inquiry_refund)) { throw new BusinessException("添加退款表失败"); } // 修改问诊订单表状态 $data = array(); $data['inquiry_refund_status'] = $refund_status; $params = array(); $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; OrderInquiry::edit($params, $data); break; case 2: // 药品订单 // 获取药品订单数据 $params = array(); $params['order_product_no'] = $order['order_no']; $order_product = OrderProduct::getOne($params); if (empty($order_product)){ throw new BusinessException("订单数据为空"); } $data = array(); $data['patient_id'] = $order_product['patient_id']; $data['order_product_id'] = $order_product['order_product_id']; $data['order_product_no'] = $order_product['order_product_no']; $data['product_refund_no'] = $refund_no; $data['refund_id'] = $refund_id; $data['product_refund_status'] = $refund_status; $data['refund_total'] = $order_product['payment_amount_total']; $data['refund_reason'] = $refund_reason; if ($refund_status == 3 && !empty($success_time)) { $data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间 } $order_product_refund = OrderProductRefund::addOrderProductRefund($data); if (empty($order_product_refund)) { throw new BusinessException("添加退款表失败"); } // 修改药品订单表状态 $data = array(); $data['refund_status'] = $refund_status; $params = array(); $params['order_product_id'] = $order_product['order_product_id']; OrderProduct::edit($params,$data); break; case 3: // 检测订单 // 获取订单数据 $params = array(); $params['detection_no'] = $order_no; $order_detection = OrderDetection::getOne($params); if (empty($order_detection)) { throw new BusinessException("订单数据为空"); } // 新增退款表 $data = array(); $data['patient_id'] = $order['patient_id']; $data['order_detection_id'] = $order_detection['order_detection_id']; $data['detection_no'] = $order_detection['detection_no']; $data['detection_refund_no'] = $refund_no; $data['refund_id'] = $refund_id; $data['detection_refund_status'] = $refund_status; $data['refund_total'] = $order_detection['payment_amount_total']; $data['refund_reason'] = $refund_reason; if ($refund_status == 3 && !empty($success_time)) { $data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间 } $order_detection_refund = OrderDetectionRefund::add($data); if (empty($order_detection_refund)) { throw new BusinessException("添加退款表失败"); } // 修改问诊订单表状态 $data = array(); $data['detection_refund_status'] = $refund_status; $params = array(); $params['order_detection_id'] = $order_detection['order_detection_id']; OrderDetection::editOrderDetection($params, $data); break; default: throw new BusinessException("订单类型错误"); } } catch (\Throwable $e) { throw new BusinessException($e->getMessage()); } } /** * 处理订单退款回调结果 * @param string $order_no * @param string|int $refund_status * @param string $success_time * @return void */ public function orderRefundResult(string $order_no,string|int $refund_status,string $success_time): void { // 获取订单数据 $params = array(); $params['order_no'] = $order_no; $order = Order::getOne($params); if (empty($order)) { throw new BusinessException("未查询到对应订单数据"); } // 退款状态 if ($refund_status == "SUCCESS") { // 退款成功 $refund_status = 3; } elseif ($refund_status == "CLOSED") { // 退款关闭 $refund_status = 5; } elseif ($refund_status == "ABNORMAL") { // 退款异常 $refund_status = 6; } else{ // 错误,无退款状态 throw new BusinessException("退款状态错误"); } // 修改订单 $data = array(); $data['refund_status'] = $refund_status; $params = array(); $params['order_no'] = $order['order_no']; Order::edit($params,$data); // 修改订单退款表 $data = array(); $data['refund_status'] = $refund_status; $data['success_time'] = $success_time; $params = array(); $params['order_no'] = $order['order_no']; OrderRefund::edit($params, $data); // 处理对应订单 switch ($order['order_type']) { case 1: // 问诊订单 // 修改订单 $data = array(); $data['inquiry_refund_status'] = $refund_status; $params = array(); $params['inquiry_no'] = $order['order_no']; OrderInquiry::edit($params, $data); // 修改退款订单 $data = array(); $data['inquiry_refund_status'] = $refund_status; $data['success_time'] = $success_time; $params = array(); $params['inquiry_no'] = $order['order_no']; OrderInquiryRefund::edit($params, $data); break; case 2: // 药品订单 // 修改订单 $data = array(); $data['refund_status'] = $refund_status; $params = array(); $params['order_product_no'] = $order['order_no']; OrderProduct::edit($params, $data); // 修改退款订单 $data = array(); $data['product_refund_status'] = $refund_status; $data['success_time'] = $success_time; $params = array(); $params['order_product_no'] = $order['order_no']; OrderProductRefund::edit($params, $data); break; case 3: // 检测订单 // 修改订单 $data = array(); $data['detection_refund_status'] = $refund_status; $params = array(); $params['detection_no'] = $order['order_no']; OrderDetection::editOrderDetection($params, $data); // 修改退款订单 $data = array(); $data['detection_refund_status'] = $refund_status; $data['success_time'] = $success_time; $params = array(); $params['detection_no'] = $order['order_no']; OrderDetectionRefund::edit($params, $data); break; default: throw new BusinessException("订单类型错误"); } // 恢复优惠卷 if ($refund_status == 3) { if (!empty($order['coupon_amount_total']) && $order['coupon_amount_total'] > 0) { // 获取该订单全部优惠卷数据 $params = array(); $params['order_id'] = $order['order_id']; $order_coupons = OrderCoupon::getList($params); if (!empty($order_coupons)){ $userCouponService = new UserCouponService(); foreach ($order_coupons as $order_coupon){ // 退还优惠卷 $userCouponService->returnUserCoupon($order_coupon['user_coupon_id']); try { // 发送站内消息-优惠卷退还 $MessagePush = new MessagePush($order_coupon['user_id']); $MessagePush->patientRefundCoupon($order_coupon['coupon_name']); }catch (\Throwable $e){ // 此处不处理错误 Log::getInstance("CallBack-wxPayInquiryRefund")->error("推送消息失败:" . $e->getMessage()); } } } } } } /** * 检测问诊订单可退款状态 * @param string $order_no */ public function checkOrderInquiryStatusForRefund(string $order_no): void { // 获取订单数据 $params = array(); $params['inquiry_no'] = $order_no; $order_inquiry = OrderInquiry::getOne($params); if (empty($order_inquiry)) { throw new BusinessException("订单数据为空"); } // 检测问诊订单状态 if (!in_array($order_inquiry['inquiry_status'], [2, 3, 4, 5, 7])) { // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) throw new BusinessException("订单状态错误"); } // 检测订单退款状态 if (in_array($order_inquiry['inquiry_refund_status'], [2, 3, 5])) { // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) throw new BusinessException("订单退款状态错误"); } // 检测支付状态 if ($order_inquiry['inquiry_pay_status'] != 2) { // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) throw new BusinessException("订单支付状态错误"); } } /** * 检测药品订单可退款状态 * @param string $order_no */ public function checkOrderProductStatusForRefund(string $order_no): void { // 获取药品订单数据 $params = array(); $params['order_product_no'] = $order_no; $order_product = OrderProduct::getOne($params); if (empty($order_product)){ throw new BusinessException("订单数据为空"); } // 检测药品订单数据状态 if ($order_product['order_product_status'] == 1){ throw new BusinessException("订单未支付"); } if ($order_product['order_product_status'] == 4){ throw new BusinessException("订单已签收"); } // 检测商品订单退款状态 if ($order_product['refund_status'] == 2){ // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) throw new BusinessException("订单退款中"); } if ($order_product['refund_status'] == 3){ // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) throw new BusinessException("订单已退款成功"); } if ($order_product['refund_status'] == 5){ // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) throw new BusinessException("订单退款关闭"); } // 检测支付状态 if ($order_product['pay_status'] != 2){ // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) throw new BusinessException("订单支付状态错误,无法退款"); } } /** * 检测检测订单可退款状态 * @param string $order_no */ public function checkOrderDetectionStatusForRefund(string $order_no): void { // 获取订单数据 $params = array(); $params['detection_no'] = $order_no; $order_detection = OrderDetection::getOne($params); if (empty($order_detection)) { throw new BusinessException("订单数据为空"); } // 检测问诊订单状态 if (!in_array($order_detection['detection_status'], [2, 5])) { // 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消) throw new BusinessException("订单状态错误"); } // 检测订单退款状态 if (in_array($order_detection['detection_refund_status'], [2, 3, 5, 6])) { // 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) throw new BusinessException("订单退款状态错误"); } // 检测支付状态 if ($order_detection['detection_pay_status'] != 2) { // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) throw new BusinessException("订单支付状态错误"); } } }