| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -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(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                if(!empty($orderMain)){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    // 添加退款时间
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    $orderMain->refund_time = time(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    $orderMain->save(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    // 退款返还优惠券
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                $this->couponService->orderRefundCoupon($message['out_trade_no']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    $this->couponService->orderRefundCoupons($orderMain->global_order_id); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                                     | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    // 删除特价商品缓存
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    // 添加用户的流水
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                if(!empty($orderMain)){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    $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())); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} |