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; }