request->getQueryParams(); $post = $this->request->getParsedBody(); $cookie = $this->request->getCookieParams(); $files = $this->request->getUploadedFiles(); $server = $this->request->getServerParams(); $xml = $this->request->getBody()->getContents(); $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml); // 通知回调,进行业务处理 $response = $app->handlePaidNotify(function ($message, $fail) use ($app) { var_dump('message',$message); Db::beginTransaction(); try { // 支付失败或者通知失败 if ( empty($message) || $message['return_code'] != 'SUCCESS' || !isset($message['result_code']) || $message['result_code'] != 'SUCCESS' ) { $this->log->event( LogLabel::PAY_NOTIFY_WXMINI, $message ); Db::rollBack(); $fail('Unknown error but FAIL'); } // 查询订单 $orderMain = OrderMain::query() ->where([ 'global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_ONLINE ]) ->first(); // 订单不存在 if (empty($orderMain)) { $this->log->event( LogLabel::PAY_NOTIFY_WXMINI, ['global_order_id_fail' => $message['out_trade_no']] ); Db::rollBack(); return true; } // 修改订单、子订单状态 $currentTime = time(); $orderMain->state = OrderMain::ORDER_STATE_UNTAKE; $orderMain->time_pay = $currentTime; $orderMain->pay_time = date('Y-m-d H:i:s', $currentTime); $orderMain->save(); $upOrder = Order::query() ->where(['order_main_id' => $orderMain->id]) ->update(['state' => OrderMain::ORDER_STATE_UNTAKE, 'pay_time' => $orderMain->pay_time]); // 更新商户销量 $upStoreScore = Store::query() ->whereIn('id', explode(',', $orderMain->store_ids)) ->update(['score' => Db::raw('score+1')]); // 更新商品库存和销量 $orders = Order::query()->select(['id', 'money', 'user_id', 'store_id', 'pay_time']) ->where(['order_main_id' => $orderMain->id]) ->get() ->toArray(); $orderGoods = OrderGoods::query()->select(['good_id AS id', 'number', 'combination_id']) ->whereIn('order_id', array_values(array_column($orders, 'id'))) ->get() ->toArray(); foreach ($orderGoods as $key => &$goodsItem) { $goods = Goods::find($goodsItem['id']); // 库存处理,有规格 if ($goodsItem['combination_id']) { $combination = SpecCombination::find($goodsItem['combination_id']); $combination->number = $combination->number - $goodsItem['number']; $combination->save(); } else { $goods->inventory = $goods->inventory - $goodsItem['number']; } $goods->sales = $goods->sales - $goodsItem['number']; $goods->save(); } // 月销流水 $statistics = []; foreach ($orders as $key => &$order) { $statistics[] = [ 'money' => $order['money'], 'user_id' => $order['user_id'], 'store_id' => $order['store_id'], 'market_id' => $orderMain->market_id, 'order_id' => $order['id'], 'createtime' => strtotime($order['pay_time']), ]; } if (is_array($statistics) && !empty($statistics)) { $inSalesStatistics = OrderSalesStatistic::query()->insert($statistics); } // 喇叭通知,兼容旧音响,MQTT+IOT $res = $this->mqttSpeakerService->speakToStore($orderMain->id); $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id); // 公众号模板消息 // $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->id); // 打印订单,自动打印 TODO 后续优化调用逻辑 $res = $this->feiePrintService->feiePrint($orderMain->global_order_id); Db::commit(); return true; } catch (Exception $e) { $this->log->event( LogLabel::PAY_NOTIFY_WXMINI, ['exception_fail' => $e->getMessage()] ); Db::rollBack(); $fail('Exception'); } }); var_dump('reponse',$response->getContent()); return $this->response ->withHeader('Content-Type', 'text/xml') ->withStatus(200) ->withBody(new SwooleStream($response->getContent())); } 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(); } }