From 1c411c3a8ee59d6d7899884be9e8b76a899ad02d Mon Sep 17 00:00:00 2001 From: lanzu_qinsheng <334039090@qq.com> Date: Fri, 11 Sep 2020 14:28:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 37 +++++++------------ .../v3/Implementations/PaymentService.php | 8 ++++ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 1f49932..530d09e 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -78,32 +78,21 @@ class OrderService implements OrderServiceInterface */ public function onlineRefund($global_order_id, $user_id) { - - Db::beginTransaction(); try { - $result = $this->orderOnlineService->doRefund($global_order_id, $user_id); - - Db::commit(); - if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") { - return [ - "status" => 200, - "code" => 0, - "result" => [], - "message" => '处理成功' - ]; - } else { - return [ - "status" => 200, - "code" => -1, - "result" => [], - "message" => $result['err_code_des'] - ]; - } + $this->orderOnlineService->doRefund($global_order_id, $user_id); + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; } catch (\Exception $e) { - - Db::rollBack(); - $this->log->event(LogLabel::ORDER_REFUND_LOG, ['jsonrpc_order_service_exception_onlineRefund' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + return [ + "status" => 200, + "code" => $e->getCode(), + "result" => [], + "message" => $e->getMessage() + ]; } } diff --git a/app/Service/v3/Implementations/PaymentService.php b/app/Service/v3/Implementations/PaymentService.php index e979635..58b25b5 100644 --- a/app/Service/v3/Implementations/PaymentService.php +++ b/app/Service/v3/Implementations/PaymentService.php @@ -127,6 +127,14 @@ class PaymentService implements PaymentServiceInterface 'notify_url' => config('wechat.notify_url.refund'), ] ); + if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") { + return true; + } else { + throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL); + } + + + return $result; } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_PAYMENT_LOG, ['payment_do_exception_msg' => $e->getMessage()]); From 9c2ce15789da1411555c851b8418658eb18fc157 Mon Sep 17 00:00:00 2001 From: weigang Date: Fri, 11 Sep 2020 14:45:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8D=95=E7=AC=94?= =?UTF-8?q?=E7=9A=84=E7=9B=B4=E6=8E=A5=E9=80=80=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 265 +++++++++++++++++++++-------------- 1 file changed, 162 insertions(+), 103 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 5a654fa..c803555 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -69,7 +69,12 @@ class OrderService implements OrderServiceInterface Db::rollBack(); $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]); - throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL); + return [ + "status" => 200, + "code" => $e->getCode() ?? ErrorCode::ORDER_COMPLETE_FAIL, + "result" => [], + "message" => $e->getMessage() + ]; } } @@ -130,132 +135,186 @@ class OrderService implements OrderServiceInterface public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null) { - $params = [ - 'user_id' => $user_id, - 'note' => $note, - 'global_order_id' => $global_order_id, - 'order_child_id' => $order_child_id, - 'order_goods_id' => $order_goods_id, - ]; - - if (!$user_id || !$global_order_id || !$note) { - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对', - 'params' => json_encode($params) - ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } + Db::beginTransaction(); + try { - // 主订单 - $orderMain = OrderMain::query() - ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id]) - ->whereIn('state', OrderState::CAN_REFUND_DIRECT) - ->first(); + $params = [ + 'user_id' => $user_id, + 'note' => $note, + 'global_order_id' => $global_order_id, + 'order_child_id' => $order_child_id, + 'order_goods_id' => $order_goods_id, + ]; - if (is_null($orderMain)) { - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', - 'params' => json_encode($params) - ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } + if (!$user_id || !$global_order_id || !$note) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } - // 子订单 - $orderChild = null; - if ($order_child_id) { - $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first(); - } + // 主订单 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id]) + ->whereIn('state', OrderState::CAN_REFUND_DIRECT) + ->first(); - // 订单商品 - $orderGoods = null; - if ($order_goods_id) { - if (!$order_child_id || is_null($orderChild)) { + if (is_null($orderMain)) { $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]', + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', 'params' => json_encode($params) ]); throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); } - $orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first(); + // 子订单 + $orderChild = null; + if ($order_child_id) { + $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first(); + } - if (is_null($orderGoods)) { + // 订单商品 + $orderGoods = null; + if ($order_goods_id) { + if (!$order_child_id || is_null($orderChild)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + $orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first(); + + if (is_null($orderGoods)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + } + + var_dump('ordergoods', $orderGoods); + $totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费 + $preRefundAmount = 0; // 预退款金额 + $refundAmount = 0; // 实际退款金额 + $refundType = 'main'; // 退款类型, Main整单 Sub子单 Goods单品 + if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品 + $preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2); + $refundType = 'goods'; + } elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单 + $preRefundAmount = $orderChild->money; + $refundType = 'sub'; + } else { // 3. 再则如果存在主订单说明退主订单 + $preRefundAmount = $orderMain->total_money; + $refundType = 'main'; + } + + // 占订单金额的比例 + $rate = bcdiv($preRefundAmount, $totalAmount, 6); + // 计算优惠券所占金额 + $couponMoney = bcmul($orderMain->coupon_money, $rate, 6); + // 计算本次退款实际退款金额 + $refundAmount = bcsub($preRefundAmount, $couponMoney, 2); + + if ($refundAmount <= 0) { $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]', + 'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]', 'params' => json_encode($params) ]); throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); } - } - - $totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费 - $preRefundAmount = 0; // 预退款金额 - $refundAmount = 0; // 实际退款金额 - $refundType = 'Main'; // 退款类型, Main整单 Sub子单 Goods单品 - if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品 - $preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2); - $refundType = 'Goods'; - } elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单 - $preRefundAmount = $orderChild->money; - $refundType = 'Sub'; - } else { // 3. 再则如果存在主订单说明退主订单 - $preRefundAmount = $orderMain->total_money; - $refundType = 'Main'; - } + var_dump($orderMain->money, $preRefundAmount, $refundAmount); + + // 开始退款 + $config = config('wxpay'); + $app = Factory::payment($config); + $app['guzzle_handler'] = CoroutineHandler::class; + $result = $app->refund->byOutTradeNumber( + $orderMain->global_order_id, + $orderMain->global_order_id, + bcmul($orderMain->money, 100, 0), + bcmul($refundAmount, 100, 0), + [ + 'refund_desc' => '订单协商退款[' . $refundType . ']', + 'notify_url' => config('wechat.notify_url.refund_single'), + ] + ); + + if ($result['return_code'] == 'FAIL') { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'] . '[微信退款失败]', + 'params' => json_encode($result) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } - // 占订单金额的比例 - $rate = bcdiv($preRefundAmount, $totalAmount, 6); - // 计算优惠券所占金额 - $couponMoney = bcmul($orderMain->coupon_money, $rate, 6); - // 计算本次退款实际退款金额 - $refundAmount = bcsub($preRefundAmount, $couponMoney, 2); + if ($result['result_code'] == 'FAIL') { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'] . '[微信退款失败]' . $result['err_code'], + 'params' => json_encode($result) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } - if ($refundAmount <= 0) { + // 退款申请成功,查询退款状态,此处暂时无法传递和记录单品退的信息,所以直接查询退款状态处理,不做回调 + $refundResult = $app->refund->queryByRefundId($result['refund_id']); $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]', - 'params' => json_encode($params) + 'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]', + 'params' => json_encode($refundResult) ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } - // 开始退款 - $config = config('wxpay'); - $app = Factory::payment($config); - $app['guzzle_handler'] = CoroutineHandler::class; - $result = $app->refund->byOutTradeNumber( - $orderMain->global_order_id, - $orderMain->global_order_id, - bcmul($orderMain->money, 100, 0), - bcmul($refundAmount, 100, 0), - [ - 'refund_desc' => '订单协商退款['.$refundType.']', - 'notify_url' => config('wechat.notify_url.refund_single'), - ] - ); - - if ($result['return_code'] == 'FAIL') { - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'].'[微信退款失败]', - 'params' => json_encode($result) - ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } + // 退款成功 + if ( + !($refundResult['return_code'] == 'SUCCESS' + && isset($refundResult['result_code']) + && $refundResult['result_code'] == 'SUCCESS') + ) { + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } - if ($result['result_code'] == 'FAIL') { - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'].'[微信退款失败]'.$result['err_code'], - 'params' => json_encode($result) - ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } + $currentTime = time(); + // 处理订单状态 + $orderMain->state = OrderState::REFUNDED; + $orderMain->total_refund_note = $note; + $orderMain->refund_time = $currentTime; + $orderMain->save(); + + if ($refundType == 'sub') { + $orderChild->status = 3; + $orderChild->refund_note = $note; + $orderChild->refund_time = $currentTime; + $orderChild->save(); + } - // 退款申请成功,查询退款状态 - $refundResult = $app->refund->queryByRefundId($result['refund_id']); - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]', - 'params' => json_encode($refundResult) - ]); + if ($refundType == 'goods') { + $orderGoods->status = 3; + $orderGoods->refund_note = $note; + $orderGoods->refund_time = $currentTime; + $orderGoods->save(); + } + + Db::commit(); + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; + + } catch (\Exception $e) { + Db::rollBack(); + return [ + "status" => 200, + "code" => $e->getCode(), + "result" => [], + "message" => '[退款失败]'.$e->getMessage() + ]; + } } } \ No newline at end of file