From 19852115e9b55647f8e6fa47842b4d8a340b5331 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 26 Aug 2020 17:48:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E9=80=80=E6=AC=BE--=E4=BF=AE?= =?UTF-8?q?=E6=94=B94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/NotifyPayRefundController.php | 7 +- app/JsonRpc/OrderService.php | 25 +++--- app/Service/OrderService.php | 82 +++++++++++--------- app/Service/PurchaseLimitService.php | 2 + app/Service/WxRefundService.php | 21 +++-- 5 files changed, 72 insertions(+), 65 deletions(-) diff --git a/app/Controller/NotifyPayRefundController.php b/app/Controller/NotifyPayRefundController.php index dbc9c53..dbeb6fa 100644 --- a/app/Controller/NotifyPayRefundController.php +++ b/app/Controller/NotifyPayRefundController.php @@ -1,12 +1,11 @@ [], "message" => '' ]; - try{ - $res = $this->orderService->onlineRefund($global_order_id); - if($res){ - $result['code'] = 0; - $result['result'] = $res; - $result['message'] = '退款成功'; - }else{ - $result['result'] = $res; - $result['message'] = '退款失败'; - }; - - } catch (\Exception $e){ - $result['message'] = $e->getMessage(); - } + + $res = $this->orderService->onlineRefund($global_order_id); + if($res['code'] > 0){ + $result['result'] = $res; + $result['message'] = '退款失败'; + }else{ + $result['code'] = 0; + $result['result'] = $res; + $result['message'] = '退款成功'; + }; + return $result; } } \ No newline at end of file diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index d32f54a..6db3f6c 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -761,14 +761,20 @@ class OrderService implements OrderServiceInterface if (empty($orderMain)) { Db::rollBack(); - return '查询不到订单'; + return [ + 'code' => 1, + 'msg' =>'查询不到订单' + ]; } $orderMain->state = OrderMain::ORDER_STATE_REFUNDED; if(!$orderMain->save()){ Db::rollBack(); - return '更新主订单失败'; + return [ + 'code' => 2, + 'msg' =>'更新主订单失败' + ]; }; // 子订单状态更新 @@ -778,64 +784,68 @@ class OrderService implements OrderServiceInterface ->update(['state' => OrderMain::ORDER_STATE_REFUNDED]); if(empty($upChild)){ Db::rollBack(); - return '更新子订单失败'; + return [ + 'code' => 3, + 'msg' =>'更新子订单失败' + ]; } if($orderMain->pay_type == OrderMain::ORDER_PAY_WX){ - $data = [ - 'global_order_id' => $global_order_id, - 'money' => $orderMain->money - ]; + // 微信支付 微信退款 - $refundRes = $this->wxRefundService->wxPayRefund($data); + $refundRes = $this->wxRefundService->wxPayRefund($orderMain->global_order_id); var_dump($refundRes); if( empty($refundRes) + || !$refundRes || !isset($refundRes['result_code']) || $refundRes['result_code'] != 'SUCCESS' ){ Db::rollBack(); return $refundRes; }; - - /* --- 退款成功 --- */ - $orderMain = $orderMain->fresh(); - if(empty($orderMain->refund_time)){ - // 添加退款时间 - $orderMain->refund_time = time(); - $orderMain->save(); - - // 退款返还优惠券 - $this->couponService->orderRefundCoupons($orderMain->global_order_id); - - // 删除特价商品缓存 - $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id); - - // 添加用户的流水 - $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money); - } }else if($orderMain->pay_type == OrderMain::ORDER_PAY_BALANCE){ + // 余额支付 退款到用户余额 - // if($this->userService->userWallet($orderMain->user_id,$orderMain->money,Users::WALLET_TYPE_INC)){ - // Db::rollBack(); - // return false; - // }; + if($this->userService->userWallet($orderMain->user_id,$orderMain->money,Users::WALLET_TYPE_INC)){ + Db::rollBack(); + return [ + 'code' => 4, + 'msg' =>'退款到用户余额失败' + ]; + }; + } - // 返还优惠券 - // $this->couponService->orderRefundCoupon($global_order_id); + /* --- 退款成功 --- */ + $orderMain = $orderMain->fresh(); + if(empty($orderMain->refund_time)){ + // 添加退款时间 + $orderMain->refund_time = time(); + $orderMain->save(); + + // 退款返还优惠券 + $this->couponService->orderRefundCoupons($orderMain->global_order_id); + // 删除特价商品缓存 - // $this->orderService->clearTodayGoodPurchase($orderMain->user_id,1); - // 添加用户流水 - // $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money); + $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->global_order_id); + + // 添加用户的流水 + $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money); } Db::commit(); - return true; + return [ + 'code' => 0, + 'msg' =>'退款成功' + ]; } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款','exception' => $e->getMessage()]); Db::rollBack(); - return false; + return [ + 'code' => 5, + 'msg' => $e->getMessage() + ]; } } diff --git a/app/Service/PurchaseLimitService.php b/app/Service/PurchaseLimitService.php index 8bbd222..3c10a5a 100644 --- a/app/Service/PurchaseLimitService.php +++ b/app/Service/PurchaseLimitService.php @@ -52,7 +52,9 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface $order_main = OrderMain::where('global_order_id',$global_order_id) ->select('id','user_id') ->first(); + $order_id = $order_main->id; + $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); $order = Order::query() ->where('order_main_id',$order_id) diff --git a/app/Service/WxRefundService.php b/app/Service/WxRefundService.php index aba5531..7fe5362 100644 --- a/app/Service/WxRefundService.php +++ b/app/Service/WxRefundService.php @@ -27,14 +27,13 @@ class WxRefundService implements WxRefundServiceInterface $app = Factory::payment($config); $app['guzzle_handler'] = CoroutineHandler::class; - $orderMain = $global_order_id; // 查询订单 - // $orderMain = OrderMain::query() - // ->select('id','global_order_id','order_num','money','state') - // ->where('global_order_id',$global_order_id) - // ->where('pay_type',OrderMain::ORDER_PAY_WX) - // ->where(Db::raw('refund_time is null')) - // ->first()->toArray(); + $orderMain = OrderMain::query() + ->select('id','global_order_id','order_num','money','state') + ->where('global_order_id',$global_order_id) + ->where('pay_type',OrderMain::ORDER_PAY_WX) + ->where(Db::raw('refund_time is null')) + ->first()->toArray(); if(empty($orderMain)){ return false; @@ -45,10 +44,10 @@ class WxRefundService implements WxRefundServiceInterface // 'notify_url' => config('site_host') . '/wechat/notify/wxpayrefund' ]; $result = $app->refund->byOutTradeNumber( - $orderMain['global_order_id'], - $orderMain['global_order_id'], - $orderMain['money'] * 100, - $orderMain['money'] * 100, + $orderMain->global_order_id, + $orderMain->global_order_id, + $orderMain->money * 100, + $orderMain->money * 100, $options ); $this->log->event(LogLabel::WX_PAY_REFUND,$result);