setWebsite(__NAMESPACE__); } ################################################################################################################## ### 订单退款 ################################################################################################ ################################################################################################################## /** * 确认退款 * 注意: * 1、交易时间超过一年的订单无法提交退款 * 2、微信支付退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。申请退款总金额不能超过订单金额。 一笔退款失败后重新提交,请不要更换退款单号,请使用原商户退款单号 * * 退款有一定延时,用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。 * * @return String * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException */ public function ensureRefund() { $requestData = Request::all(); $md5_id = $requestData['mid'] ?? ''; $price = $requestData['price'] ?? ''; $order_type = $requestData['order_type'] ?? ''; if (empty($md5_id) || empty($price) || empty($order_type)) { return responseMessage(2001, '非法提交!'); } if ($order_type == 1) { // 电商 $orderAlias = $this->setAliasCommerceOrder; $aliasOrderProduct = $this->setAliasCommerceOrderProduct; } elseif ($order_type == 2) { // 预约 $orderAlias = $this->setAliasTostoreOrder; $aliasOrderProduct = $this->setAliasTostoreOrderProduct; } else { return responseMessage(2003, '该产品不支持!'); } // 获取订单信息 order_id $orderService = new MaterialService(new EavSetService($orderAlias)); $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']); if (empty($orderInfo)) { return responseMessage(2005, '该订单不存在,请重试!'); } if ($price > $orderInfo['order_amount_total']) { return responseMessage(2006, '修改的价格,必须小于或等于原价'); } $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId); if (empty($payConfig)) { return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!'); } //pay_type 1 微信小程序 2 微信公众号 3支付宝 if ($orderInfo['pay_channel'] == 1) { $app = $this->wxGateway($payConfig, $this->storeId); } else { $app = $this->wechatGateway($payConfig, $this->storeId); } $pay_info = json_decode($orderInfo['pay_info'], true); $result = $app->refund->byTransactionId($pay_info['transaction_id'], $refund_no, $orderInfo['order_amount_total'] * 100, $price * 100, [ 'refund_desc' => $refundInfo['refund_reason'] ]); if ($result['return_code'] == 'SUCCESS') { if ($result['result_code'] == 'SUCCESS') { // 更新退款状态和信息 $refundOrderService->update($refundInfo['id'], [ 'status' => 5, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中 'refund_true_price' => $price, 'refund_info' => json_encode($result) ]); // 更新退款产品的状态 $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct)); $refundInfo['product_id'] = intval($refundInfo['product_id']); if ($refundInfo['product_id']) { // 拼团的订单,该字段为空值 $orderProductService->update($refundInfo['product_id'], [ 'status' => 5 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中 ]); } // 消息通知 // dispatch(new SendMessage($this->storeId, 'REFUND_SUCCESS', ['orderNo' => $orderInfo['order_no']], $orderInfo['member_id'])); return responseMessage(1001, '退款操作成功!'); } else { return responseMessage(2011, '操作失败:' . $result['err_code'] . '-' . $result['err_code_des']); } } else { return responseMessage(2010, '操作失败:' . $result['return_msg']); } } /** * 通过微信接口,检测该订单的退款状态 */ public function checkRefundOrder() { $requestData = Request::all(); $refund_no = $requestData['refund_no'] ?? ''; $md5_id = $requestData['mid'] ?? ''; $order_type = $requestData['order_type'] ?? ''; if (empty($md5_id) || empty($refund_no) || empty($order_type)) { return $this->responseMessage(2001, '非法提交!'); } if ($order_type == 1) { // 电商 $refundAlias = $this->setAliasCommerceRefund; $orderAlias = $this->setAliasCommerceOrder; $aliasOrderProduct = $this->setAliasCommerceOrderProduct; } elseif ($order_type == 2) { // 预约 $refundAlias = $this->setAliasTostoreRefund; $orderAlias = $this->setAliasTostoreOrder; $aliasOrderProduct = $this->setAliasTostoreOrderProduct; } elseif ($order_type == 3) { // 餐饮 $refundAlias = $this->setAliasTakeoutRefund; $orderAlias = $this->setAliasTakeoutOrder; $aliasOrderProduct = $this->setAliasTakeoutOrderProduct; } else { return $this->responseMessage(2003, '该产品不支持!'); } // 获取原订单信息 order_id $orderService = new MaterialService(new EavSetService($orderAlias)); $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']); if (empty($orderInfo)) { return $this->responseMessage(2005, '该订单不存在,请重试!'); } if ($orderInfo['status'] == 3) { return $this->responseMessage(2014, '退款完成'); } $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId); if (empty($payConfig)) { return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!'); } //pay_type 1 微信小程序 2 微信公众号 3支付宝 if ($orderInfo['pay_channel'] == 1) { $app = $this->wxGateway($payConfig, $this->storeId); } else { $app = $this->wechatGateway($payConfig, $this->storeId); } $pay_info = json_decode($orderInfo['pay_info'], true); $result = $app->refund->queryByTransactionId($pay_info['transaction_id']); if ($result['return_code'] == 'SUCCESS') { if ($result['result_code'] == 'SUCCESS') { // 更新退款状态和信息 $refundOrderService->update($refundInfo['id'], [ 'status' => 3, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中 ]); //更新总订单的状态 订单状态 1未付款2已付款3已发货4已完成5交易关闭6退款申请中7卖家退款中8退款完成订单关闭9拒绝退款 $orderService->update($orderInfo['id'], ['order_status' => 8]); // 更新退款产品的状态 $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct)); $orderProductService->update($refundInfo['product_id'], [ 'status' => 3 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中 ]); return $this->responseMessage(1001, '已退款!'); } else { return $this->responseMessage(2011, ''); } } else { return $this->responseMessage(2010, ''); } } }