diff --git a/app/Controller/NotifyController.php b/app/Controller/NotifyController.php index 177a84d..4794fce 100644 --- a/app/Controller/NotifyController.php +++ b/app/Controller/NotifyController.php @@ -3,20 +3,81 @@ namespace App\Controller; use App\Constants\LogLabel; +use App\Model\OrderMain; +use EasyWeChat\Factory; +use Hyperf\Guzzle\CoroutineHandler; class NotifyController extends BaseController { public function wxminiOnline() { - // 获取参数 - - // 查询订单 + $config = config('wxpay'); + $app = Factory::payment($config); + $app['guzzle_handler'] = CoroutineHandler::class; - // 开始处理 + // 通知回调,进行业务处理 + $response = $app->handlePaidNotify(function ($message, $fail) use ($app) { - $this->log->event( - LogLabel::PAY_NOTIFY_WXMINI, - json_encode($this->request->input()) - ); + $this->log->event( + LogLabel::PAY_NOTIFY_WXMINI, + $message + ); + + // 查询订单 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_ONLINE, 'state' => OrderMain::ORDER_STATE_UNPAY]) + ->where('time', '>=', date('Y-m-d H:i:s', (time()-900))) + ->first(); + + if (empty($orderMain)) { + // 去查一下微信订单 + $wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id); + + $this->log->event( + LogLabel::PAY_NOTIFY_WXMINI, + $wxOrder + ); + + // return true; + } + }); + + $response->send(); + } + + public function wxminiOffline() + { + $config = config('wxpay'); + $app = Factory::payment($config); + $app['guzzle_handler'] = CoroutineHandler::class; + + // 通知回调,进行业务处理 + $response = $app->handlePaidNotify(function ($message, $fail) use ($app) { + + $this->log->event( + LogLabel::PAY_NOTIFY_WXMINI, + $message + ); + + // 查询订单 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_OFFLINE, 'state' => OrderMain::ORDER_STATE_UNPAY]) + ->where('time', '>=', date('Y-m-d H:i:s', (time()-900))) + ->first(); + + if (empty($orderMain)) { + // 去查一下微信订单 + $wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id); + + $this->log->event( + LogLabel::PAY_NOTIFY_WXMINI, + $wxOrder + ); + + // return true; + } + }); + + $response->send(); } } \ No newline at end of file diff --git a/app/Controller/PaymentController.php b/app/Controller/PaymentController.php index efde0a2..d4189dd 100644 --- a/app/Controller/PaymentController.php +++ b/app/Controller/PaymentController.php @@ -4,7 +4,6 @@ namespace App\Controller; use App\Constants\ErrorCode; use App\Model\OrderMain; -use App\Model\Users; use App\Request\WxminiPayRequest; use EasyWeChat\Factory; @@ -13,7 +12,7 @@ use Hyperf\Guzzle\CoroutineHandler; class PaymentController extends BaseController { - public function wxminiPay(WxminiPayRequest $request){ + public function wxminiPayOnline(WxminiPayRequest $request){ $data = $request->validated(); @@ -21,7 +20,7 @@ class PaymentController extends BaseController $app = Factory::payment($config); $app['guzzle_handler'] = CoroutineHandler::class; - // 待支付的,类型一致的,未超时(15min,900sec)的订单 + // 待支付的,未超时(15min,900sec)的订单 $orderMain = OrderMain::query() ->where(['state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']]) ->where('time', '>=', date('Y-m-d H:i:s', (time()-900))) @@ -43,4 +42,34 @@ class PaymentController extends BaseController return $this->success($result); } + public function wxminiPayOffline(WxminiPayRequest $request){ + + $data = $request->validated(); + + $config = config('wxpay'); + $app = Factory::payment($config); + $app['guzzle_handler'] = CoroutineHandler::class; + + // 待支付的,未超时(15min,900sec)的订单 + $orderMain = OrderMain::query() + ->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']]) + ->where('time', '>=', date('Y-m-d H:i:s', (time()-900))) + ->first(); + + if (empty($orderMain)) { + return $this->result(ErrorCode::PAY_FAILURE, ['order_id' => $data['order_id']],'订单不存在或已失效'); + } + + $result = $app->order->unify([ + 'body' => '懒族生活 - 当面支付', + 'out_trade_no' => $orderMain->global_order_id, + 'total_fee' => bcmul(floatval($orderMain->money), 100), + 'notify_url' => config('site_host') . '/v1/notify/wxminiOffline', + 'trade_type' => 'JSAPI', + 'openid' => $data['openid'], + ]); + + return $this->success($result); + } + } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index 2393433..266122f 100644 --- a/config/routes.php +++ b/config/routes.php @@ -41,7 +41,9 @@ Router::addGroup('/v1/',function (){ Router::post('Order/addOnline', 'App\Controller\OrderController@addOnlineOrder'); //小程序支付相关 - Router::post('wxmini/pay', 'App\Controller\PaymentController@wxminiPay'); - Router::post('notify/wxmini', 'App\Controller\NotifyController@wxmini'); + Router::post('wxminipay/online', 'App\Controller\PaymentController@wxminiPayOnline'); + Router::post('wxminipay/offline', 'App\Controller\PaymentController@wxminiPayOffline'); + Router::post('notify/wxminionline', 'App\Controller\NotifyController@wxminiOnline'); + Router::post('notify/wxminioffline', 'App\Controller\NotifyController@wxminiOffline'); }); \ No newline at end of file