From 3793706945c00f7c2b24f6a97db93bfe577e4bce Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 26 Aug 2020 16:38:19 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E9=80=80=E6=AC=BE--?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=AE=E9=A2=98-=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + app/Constants/LogLabel.php | 7 +++- app/Controller/NotifyPayRefundController.php | 44 +++++++++++++------- app/JsonRpc/OrderService.php | 34 ++++++++------- app/Service/CouponService.php | 4 +- app/Service/CouponServiceInterface.php | 2 +- app/Service/OrderService.php | 36 ++++++++++++++-- app/Service/WxRefundService.php | 33 ++++++++------- 8 files changed, 108 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 9454e2b..0e78326 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ vendor/ .phpunit* /watch .vscode/ +config/cert/apiclient_cert_2.pem +config/cert/apiclient_key_2.pem diff --git a/app/Constants/LogLabel.php b/app/Constants/LogLabel.php index 8807da4..4b998b7 100644 --- a/app/Constants/LogLabel.php +++ b/app/Constants/LogLabel.php @@ -57,6 +57,11 @@ class LogLabel extends AbstractConstants /** * @Message("Pay refund Log Label") */ - const PAY_NOTIFY_REFUND = 'notify_refund'; + const WX_NOTIFY_REFUND = 'wx_notify_refund'; + + /** + * @Message("Pay refund Log Label") + */ + const WX_PAY_REFUND = 'wx_pay_refund'; } diff --git a/app/Controller/NotifyPayRefundController.php b/app/Controller/NotifyPayRefundController.php index 24c12b1..dbc9c53 100644 --- a/app/Controller/NotifyPayRefundController.php +++ b/app/Controller/NotifyPayRefundController.php @@ -15,6 +15,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpMessage\Stream\SwooleStream; use Symfony\Component\HttpFoundation\Request; use App\Service\PurchaseLimitServiceInterface; +use Hyperf\DbConnection\Db; class NotifyPayRefundController extends BaseController { @@ -44,6 +45,10 @@ class NotifyPayRefundController extends BaseController */ public function wxPayRefund() { + $this->log->event( + LogLabel::WX_NOTIFY_REFUND, + '进入回调' + ); $config = config('wxpay'); $app = Factory::payment($config); $app['guzzle_handler'] = CoroutineHandler::class; @@ -59,7 +64,10 @@ class NotifyPayRefundController extends BaseController /* 通知回调,进行业务处理 */ $response = $app->handleRefundedNotify(function ($message, $fail) use ($app) { - + $this->log->event( + LogLabel::WX_NOTIFY_REFUND, + $message + ); try { /* --- 退款失败 --- */ if ( @@ -69,41 +77,47 @@ class NotifyPayRefundController extends BaseController ) { // 错误日志 $this->log->event( - LogLabel::PAY_NOTIFY_REFUND, + LogLabel::WX_NOTIFY_REFUND, $message ); $fail('Unknown error but FAIL'); + return false; } /* --- 退款成功 --- */ $orderMain = OrderMain::select('id','global_order_id','money','user_id') ->where('global_order_id',$message['out_trade_no']) ->where('state',OrderMain::ORDER_STATE_REFUNDED) + ->where(Db::raw('refund_time is null')) ->first(); - - // 退款返还优惠券 - $this->couponService->orderRefundCoupon($message['out_trade_no']); - - // 删除特价商品缓存 - $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id); - - // 添加用户的流水 + if(!empty($orderMain)){ + // 添加退款时间 + $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); } } catch (\Exception $e) { $this->log->event( - LogLabel::PAY_NOTIFY_REFUND, + LogLabel::WX_NOTIFY_REFUND, ['exception_fail' => $e->getMessage()] ); $fail('Exception'); } }); - // return $this->response - // ->withHeader('Content-Type', 'text/xml') - // ->withStatus(200) - // ->withBody(new SwooleStream($response->getContent())); + return $this->response + ->withHeader('Content-Type', 'text/xml') + ->withStatus(200) + ->withBody(new SwooleStream($response->getContent())); } } \ No newline at end of file diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 43abb2e..68f754f 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -69,24 +69,26 @@ class OrderService implements OrderServiceInterface * 退款成功 state = 9 */ public function onlineRefund($global_order_id){ - Db::beginTransaction(); + $result = [ + "status" => 200, + "code" => ErrorCode::ORDER_FAILURE, + "result" => [], + "message" => '' + ]; try{ - - return [ - "status" => 200, - "code" => 0, - "result" => $this->orderService->onlineRefund($global_order_id), - // 'result' => $global_order_id, - "message" => '退款成功' - ]; + $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){ - Db::rollBack(); - return [ - "status" => 200, - "code" => ErrorCode::ORDER_FAILURE, - "result" => [], - "message" => $e->getMessage() - ]; + $result['message'] = $e->getMessage(); } + return $result; } } \ No newline at end of file diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php index d6a2d9f..dd1a8c7 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -323,7 +323,7 @@ class CouponService implements CouponServiceInterface return $res; } - /* 删除-优惠券今日使用的缓存 + /* 删除-优惠券今日使用的缓存 * @param $userId * @param $couponId * @return bool @@ -345,7 +345,7 @@ class CouponService implements CouponServiceInterface * 先查询是否正常使用优惠券 * 修改状态,退还领取记录库存,删除ssdb缓存 */ - public function orderRefundCoupon($global_order_id) + public function orderRefundCoupons($global_order_id) { $time = time(); Db::beginTransaction(); diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index 2789efb..4dc0584 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -29,5 +29,5 @@ interface CouponServiceInterface public function refundOrderCoupons($order_id); public function clearTodayCouponUsed($userId, $couponId); - public function orderRefundCoupon($global_order_id); + public function orderRefundCoupons($global_order_id); } diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index 57aadf3..58a7435 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -759,6 +759,7 @@ class OrderService implements OrderServiceInterface } $orderMain->state = OrderMain::ORDER_STATE_REFUNDED; + if(!$orderMain->save()){ Db::rollBack(); return false; @@ -773,13 +774,40 @@ class OrderService implements OrderServiceInterface Db::rollBack(); return false; } - + if($orderMain->pay_type == OrderMain::ORDER_PAY_WX){ + $data = [ + 'global_order_id' => $global_order_id, + 'money' => $orderMain->money + ]; // 微信支付 微信退款 - if(!$this->wxRefundService->wxPayRefund($global_order_id)){ + $refundRes = $this->wxRefundService->wxPayRefund($data); + var_dump($refundRes); + if( + empty($refundRes) + || !isset($refundRes['result_code']) + || $refundRes['result_code'] != 'SUCCESS' + ){ Db::rollBack(); return false; }; + + /* --- 退款成功 --- */ + $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)){ @@ -794,10 +822,10 @@ class OrderService implements OrderServiceInterface // 添加用户流水 // $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money); } - + Db::commit(); return true; - } catch (Exception $e) { + } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款','exception' => $e->getMessage()]); Db::rollBack(); diff --git a/app/Service/WxRefundService.php b/app/Service/WxRefundService.php index bbd2dd6..aba5531 100644 --- a/app/Service/WxRefundService.php +++ b/app/Service/WxRefundService.php @@ -5,9 +5,10 @@ namespace App\Service; use App\Model\OrderMain; use EasyWeChat\Factory; use Hyperf\DbConnection\Db; -use App\Constants\ErrorCode; use App\Commons\Log; +use App\Constants\LogLabel; use Hyperf\Di\Annotation\Inject; +use Hyperf\Guzzle\CoroutineHandler; class WxRefundService implements WxRefundServiceInterface { @@ -26,29 +27,31 @@ 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(); + // $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; }; + $options = [ - 'refund_desc' => '', - 'notify_url' => config('site_host') . '/wechat/notify/wxpayrefund' + 'refund_desc' => '线上订单退款', + // '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('wx_pay_refund',$result); + $this->log->event(LogLabel::WX_PAY_REFUND,$result); return $result; } From 547b10793b1c741c27867f2e72171af871f439f9 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 26 Aug 2020 17:14:07 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E9=80=80=E6=AC=BE--?= =?UTF-8?q?=E4=BF=AE=E6=94=B93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/OrderService.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index 7931b32..db8777a 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -23,6 +23,7 @@ use App\Service\WxRefundServiceInterface; use App\Service\UserServiceInterface; use App\Model\Users; use App\Constants\SsdbKeysPrefix; +use App\Service\PurchaseLimitServiceInterface; class OrderService implements OrderServiceInterface { @@ -56,6 +57,12 @@ class OrderService implements OrderServiceInterface */ protected $purchaseLimitService; + /** + * @Inject + * @var FinancialRecordServiceInterface + */ + protected $financialService; + /** * @inheritDoc */ @@ -745,8 +752,6 @@ class OrderService implements OrderServiceInterface { Db::beginTransaction(); try { - - $time = time(); // 主订单状态更新 $orderMain = OrderMain::query() ->select('id','global_order_id','state','pay_type','user_id','money') @@ -755,14 +760,14 @@ class OrderService implements OrderServiceInterface if (empty($orderMain)) { Db::rollBack(); - return false; + return '查询不到订单'; } $orderMain->state = OrderMain::ORDER_STATE_REFUNDED; if(!$orderMain->save()){ Db::rollBack(); - return false; + return '更新主订单失败'; }; // 子订单状态更新 @@ -772,7 +777,7 @@ class OrderService implements OrderServiceInterface ->update(['state' => OrderMain::ORDER_STATE_REFUNDED]); if(empty($upChild)){ Db::rollBack(); - return false; + return '更新子订单失败'; } if($orderMain->pay_type == OrderMain::ORDER_PAY_WX){ @@ -789,7 +794,7 @@ class OrderService implements OrderServiceInterface || $refundRes['result_code'] != 'SUCCESS' ){ Db::rollBack(); - return false; + return $refundRes; }; /* --- 退款成功 --- */ 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 3/4] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E9=80=80=E6=AC=BE--?= =?UTF-8?q?=E4=BF=AE=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); From 079f2e52554336f550d70af8c7e6f3f310cfd696 Mon Sep 17 00:00:00 2001 From: weigang Date: Wed, 26 Aug 2020 17:54:53 +0800 Subject: [PATCH 4/4] fix --- app/Controller/NotifyController.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Controller/NotifyController.php b/app/Controller/NotifyController.php index 1ddfb11..1073dc2 100644 --- a/app/Controller/NotifyController.php +++ b/app/Controller/NotifyController.php @@ -115,7 +115,7 @@ class NotifyController extends BaseController $message ); Db::rollBack(); - $fail('Unknown error but FAIL'); + return $fail('Unknown error but FAIL'); } // 查询订单 @@ -127,7 +127,7 @@ class NotifyController extends BaseController ->first(); // 订单不存在 - if (empty($orderMain)) { + if (empty($orderMain) || $orderMain->state == OrderMain::ORDER_STATE_DELIVERY) { $this->log->event( LogLabel::PAY_NOTIFY_WXMINI, ['global_order_id_fail' => $message['out_trade_no']] @@ -222,7 +222,7 @@ class NotifyController extends BaseController ['exception_fail' => $e->getMessage()] ); Db::rollBack(); - $fail('Exception'); + return $fail('Exception'); } }); @@ -267,7 +267,7 @@ class NotifyController extends BaseController $message ); Db::rollBack(); - $fail('Unknown error but FAIL'); + return $fail('Unknown error but FAIL'); } // 查询订单 @@ -397,7 +397,7 @@ class NotifyController extends BaseController ['exception_fail' => $e->getMessage()] ); Db::rollBack(); - $fail('Exception'); + return $fail('Exception'); } });