container->get(IdGeneratorInterface::class); $product_refund_no = $generator->generate(); // 检测订单金额 if ($order_product['payment_amount_total'] > 0){ // 发起退款 $WechatPay = new WechatPay(1, 2); $options = array(); $options['transaction_id'] = $order_product['escrow_trade_no']; $options['out_refund_no'] = (string)$product_refund_no; $options['reason'] = $refund_reason; $options['amount'] = [ 'refund' => (int)($order_product['payment_amount_total'] * 100), 'total' => (int)($order_product['payment_amount_total'] * 100), 'currency' => "CNY", ]; $result = $WechatPay->refund($options); // 处理订单退款状态 // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) $success_time = ""; if ($result['status'] == "SUCCESS") { // 退款成功 $refund_status = 3; $success_time = $result['success_time']; } elseif ($result['status'] == "CLOSED") { // 退款关闭 $refund_status = 5; } elseif ($result['status'] == "PROCESSING") { // 退款处理中 $refund_status = 2; } elseif ($result['status'] == "ABNORMAL") { // 退款异常,此情况不处理,进行短信通知 throw new BusinessException("订单退款状态异常"); } else { throw new BusinessException("订单退款状态错误"); } $refund_id = $result['refund_id']; }else{ // 模拟退款 $refund_status = 3; $refund_id = "模拟退款:" . $generator->generate(); $success_time = date("Y-m-d H:i:s", time()); } // 新增退款表 $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'] = $product_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); } }