request->post(); $mtOrderId = $post['order_id'] ?? 0; $thirdSn = $post['day_seq'] ?? 0; //4秒内不允许第三方重复通知 $cacheKey = 'mt_confirm_order_' . $mtOrderId; $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($has)) { Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}"); return Json::encode(['data' => 'ok']); } Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']); // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回 if (empty($post)) { return Json::encode(['data' => 'ok']); } Yii::info('confirmOrderData: ' . json_encode($post)); //noticeUtil::push("美团请求通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { //noticeUtil::push("111", '15280215347'); $shopId = $post['app_poi_code'] ?? ''; $shop = ShopClass::getById($shopId, true); if (empty($shop)) { noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } //noticeUtil::push("222", '15280215347'); $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); if (empty($shopExt)) { noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } //noticeUtil::push("333", '15280215347'); $mainId = $shop->mainId ?? 0; $sjId = $shop->sjId ?? 0; $customId = $shop->defaultCustomId ?? 0; if (empty($customId)) { noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } //noticeUtil::push("444", '15280215347'); $custom = CustomClass::getById($customId); if (empty($custom)) { noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } //noticeUtil::push("555", '15280215347'); foreach ($post as $k => $val) { $post[$k] = urldecode($val); } MtOrderClass::add($post); //noticeUtil::push("666", '15280215347'); $detail = $post['detail'] ?? ''; if (empty($detail)) { noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $mtGoodsData = json_decode($detail, true); if (empty($mtGoodsData)) { noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } //noticeUtil::push("777", '15280215347'); Yii::info('商品信息:' . json_encode($mtGoodsData)); //noticeUtil::push("717171", '15280215347'); $productList = []; $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true); if (empty($mtCategory)) { $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true); } //noticeUtil::push("727272", '15280215347'); $catId = $mtCategory->id ?? 0; if (empty($catId)) { noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } //noticeUtil::push("888", '15280215347'); $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : ''; $remark = $caution; $bookMobile = $post['order_phone_number'] ?? ''; $receiveMobile = $post['recipient_phone'] ?? ''; $receiveUserName = $post['recipient_name'] ?? ''; $fullAddress = $post['recipient_address'] ?? ''; $orderCover = ''; $goodsNum = 0; foreach ($mtGoodsData as $mtKey => $mtGoods) { $mtGoodsSpu = $mtGoods['app_spu_code'] ?? ''; if (empty($mtGoodsSpu)) { if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) { $mtGoodsSpu = $mtGoods['app_food_code']; } } $goodsPrice = $mtGoods['price'] ?? 0; $num = $mtGoods['quantity'] ?? 0; $goodsNum = bcadd($num, $goodsNum); $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true); if (empty($currentGoods)) { noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); return Json::encode(['data' => 'ok']); } $id = $currentGoods->id ?? 0; $unitType = 0; $property = dict::getDict('property', 'goods'); $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]]; $productList = array_merge($goods, $productList); } //noticeUtil::push("999", '15280215347'); $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? ''; if (empty($poi_receive_detail_yuan)) { noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); util::stop(); } $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true); if (is_array($poi_receive_detail_yuan_data) == false) { noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); util::stop(); } $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0; if ($onlinePayment == 0) { noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347'); util::stop(); } $modifyPrice = $onlinePayment; //技术服务费 $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0; $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? ''; //履约服务费 $performanceServiceFee = 0; if (!empty($reconciliationExtras)) { $extArr = json_decode($reconciliationExtras, true); $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0; } //运费 $sendCost = $post['shipping_fee'] ?? 0; //打包费 $labourCost = $post['package_bag_money_yuan'] ?? 0; //服务费和手续费 $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2); $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0; $reachPeriod = date("H:i", $estimate_arrival_time); $reachDate = date("Y-m-d", $estimate_arrival_time); $reachTime = $estimate_arrival_time; //noticeUtil::push("1212", '15280215347'); $orderData = [ 'product' => $productList, 'fromType' => dict::getDict('fromType', 'mt'), 'labourCost' => $labourCost, 'sendCost' => $sendCost, 'shopId' => $shopId, 'mainId' => $mainId, 'sjId' => $sjId, 'modifyPrice' => $modifyPrice, 'serviceFee' => $serviceFee, 'thirdSn' => $thirdSn, 'cover' => $orderCover, 'reachTime' => $reachTime, 'reachDate' => $reachDate, 'reachPeriod' => $reachPeriod, 'remark' => $remark, 'goodsNum' => $goodsNum, 'bookMobile' => $bookMobile, 'receiveMobile' => $receiveMobile, 'receiveUserName' => $receiveUserName, 'fullAddress' => $fullAddress, 'thirdOrderId' => $mtOrderId, ]; $hasPay = dict::getDict('hasPay', 'payed'); //noticeUtil::push("1313", '15280215347'); $order = OrderService::createFinishOrder($orderData, $custom, $hasPay); $transaction->commit(); //noticeUtil::push("101010", '15280215347'); //新制作单提示 $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); ShopExtClass::newWorkRemind($shopExt, $order); return Json::encode(['data' => 'ok']); } catch (\Exception $e) { $transaction->rollBack(); $msg = $e->getMessage(); noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347'); return Json::encode(['data' => 'ok']); } } //取消订单 public function actionCancelOrder() { $post = Yii::$app->request->post(); $mtOrderId = $post['order_id'] ?? 0; $shopId = $post['app_poi_code'] ?? ''; if (empty($post)) { return Json::encode(['data' => 'ok']); } Yii::info('cancelOrderData: ' . json_encode($post)); //4秒内不允许第三方重复通知 $cacheKey = 'mt_cancel_order_' . $mtOrderId; $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($has)) { Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}"); return Json::encode(['data' => 'ok']); } Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']); $mt = dict::getDict('fromType', 'mt'); $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true); if (empty($order)) { //没有接单时取消订单 return Json::encode(['data' => 'ok']); } //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。 $orderId = $order->id ?? 0; $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true); $hasCompleteWork = false; if (!empty($workList)) { foreach ($workList as $work) { if ($work->status == 1) { $hasCompleteWork = true; } } } //取消订单和制作单 if ($hasCompleteWork == false) { $order->status = 5; $order->save(); if (!empty($workList)) { foreach ($workList as $work) { $work->status = 2; $work->save(); } } } //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!! //制作单取消通知 $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); ShopExtClass::orderCancelRemind($shopExt, $order); return Json::encode(['data' => 'ok']); } //推送已支付订单 public function actionNewOrder() { return Json::encode(['data' => 'ok']); } //部分退款 public function actionPartRefund() { return Json::encode(['data' => 'ok']); } //全部退款 public function actionAllRefund() { return Json::encode(['data' => 'ok']); } public function demo() { $config = dict::getDict('mtConfig'); $app = new Application($config); $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1]; $orderDetail = $app->order->getOrderDetail($params); print_r(json_decode($orderDetail, true)); // 商品类、活动类接口支持异步队列 $app->goods->async()->create([]); } protected function filterEmoji($str) { $str = preg_replace_callback('/./u', function (array $match) { return strlen($match[0]) >= 4 ? '' : $match[0]; }, $str); return $str; } }