| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace hd\controllers;
- use bizHd\custom\classes\CustomClass;
- use bizHd\goods\classes\CategoryClass;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\meituan\classes\MtOrderClass;
- use bizHd\order\services\OrderService;
- use bizHd\shop\classes\ShopClass;
- use common\components\arrayUtil;
- use common\components\dict;
- use common\components\httpUtil;
- use common\components\noticeUtil;
- use common\components\util;
- use Yii;
- use Shishaoqi\MeituanFlashPurchase\Application;
- use yii\helpers\Json;
- class MtController extends PublicController
- {
- public $guestAccess = [];
- private $config = [
- 'app_id' => '119094',
- 'app_secret' => 'b7e86d768391180f805621b40ec31e33',
- 'request_url' => '', // 默认 `https://waimaiopen.meituan.com/api/v1/`
- ];
- //部分退款
- public function actionPartRefund()
- {
- $post = Yii::$app->request->post();
- if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
- return Json::encode(['data' => 'ok']);
- }
- $data = arrayUtil::humpUnderlineConversion($post, 1);
- Yii::warning('partRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
- }
- //全部退款
- public function actionAllRefund()
- {
- $post = Yii::$app->request->post();
- if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
- return Json::encode(['data' => 'ok']);
- }
- $data = arrayUtil::humpUnderlineConversion($post, 1);
- Yii::warning('allRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
- }
- //已确认订单通知
- public function actionConfirmOrder()
- {
- // $post = Yii::$app->request->post();
- // if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
- // return Json::encode(['data' => 'ok']);
- // }
- // Yii::info('confirmOrderData: ' . json_encode($post));
- $str = '{"total":"20.5","delivery_time":"0","utime":"1657691753","wm_poi_name":"t_ZrECxUoNHg","taxpayer_id":"","detail":"%5B%7B%22actual_price%22%3A10.0%2C%22app_food_code%22%3A%22mtcode_1536299752939962428%22%2C%22app_spu_code%22%3A%22mtcode_1536299752939962428%22%2C%22box_num%22%3A0.0%2C%22box_price%22%3A0.0%2C%22detail_extra%22%3A%7B%22cate%22%3A%22%E8%8A%B1%E5%8D%89%E5%9B%AD%E8%89%BA%22%7D%2C%22food_discount%22%3A1.0%2C%22food_name%22%3A%22%E5%AE%B6%E5%B1%85%E9%B2%9C%E8%8A%B1-%E7%B2%89%E8%89%B2%E6%BB%A1%E5%A4%A9%E6%98%9F%E7%AE%80%E5%8D%95%E5%8C%85%E8%A3%85%22%2C%22item_id%22%3A4963320624333%2C%22mt_sku_id%22%3A3308253830%2C%22mt_spu_id%22%3A13964107149%2C%22mt_tag_id%22%3A10982511689%2C%22original_price%22%3A10.0%2C%22price%22%3A10.0%2C%22quantity%22%3A1%2C%22sku_id%22%3A%22%22%2C%22unit%22%3A%22%E4%BB%BD%22%2C%22upc%22%3A%22%22%2C%22weight%22%3A100%2C%22weight_for_unit%22%3A%22100.00%22%2C%22weight_unit%22%3A%22%E5%85%8B%28g%29%22%7D%2C%7B%22actual_price%22%3A10.0%2C%22app_food_code%22%3A%22mtcode_1536168818408202328%22%2C%22app_spu_code%22%3A%22mtcode_1536168818408202328%22%2C%22box_num%22%3A0.0%2C%22box_price%22%3A0.0%2C%22detail_extra%22%3A%7B%22cate%22%3A%22%E8%8A%B1%E5%8D%89%E5%9B%AD%E8%89%BA%22%7D%2C%22food_discount%22%3A1.0%2C%22food_name%22%3A%22%E3%80%90%E7%BA%AF%E7%9C%9F%E7%88%B1%E6%81%8B%E3%80%91+19%E6%9E%9D%E9%83%81%E9%87%91%E9%A6%99%E9%B2%9C%E8%8A%B1%E7%A4%BC%E7%A4%BC%E5%93%81%22%2C%22item_id%22%3A4963320624397%2C%22mt_sku_id%22%3A3298864862%2C%22mt_spu_id%22%3A14024093059%2C%22mt_tag_id%22%3A10982511689%2C%22original_price%22%3A10.0%2C%22price%22%3A10.0%2C%22quantity%22%3A1%2C%22sku_id%22%3A%22%22%2C%22unit%22%3A%22%E4%BB%BD%22%2C%22upc%22%3A%22%22%2C%22weight%22%3A500%2C%22weight_for_unit%22%3A%22500.00%22%2C%22weight_unit%22%3A%22%E5%85%8B%28g%29%22%7D%5D","poi_receive_detail":"%7B%22actOrderChargeByMt%22%3A%5B%7B%22comment%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22moneyCent%22%3A0%7D%5D%2C%22actOrderChargeByPoi%22%3A%5B%7B%22comment%22%3A%22%E5%87%8F%E9%85%8D%E9%80%81%E8%B4%B920.0%E5%85%83%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22moneyCent%22%3A1200%7D%5D%2C%22foodShareFeeChargeByPoi%22%3A80%2C%22logisticsFee%22%3A1200%2C%22onlinePayment%22%3A2050%2C%22reconciliationExtras%22%3A%22%7B%5C%22performanceServiceFee%5C%22%3A5.0%2C%5C%22chargeMode%5C%22%3A2%7D%22%2C%22wmPoiReceiveCent%22%3A270%7D","caution":"","original_price":"32.5","recipient_name":"%E7%9F%B3%28%E5%85%88%E7%94%9F%29","order_id":"27052664056314941","timestamp":"1657691754","city_id":"999999","wm_poi_phone":"4009208801","backup_recipient_phone":"%5B%5D","pay_type":"2","wm_poi_id":"2705266","longitude":"95.369279","invMakeType":"0","estimate_arrival_time":"1657694297","avg_send_time":"2582.0","logistics_code":"1001","scan_deliver_flag":"false","order_tag_list":"%5B%5D","order_phone_number":"15280215347","poi_receive_detail_yuan":"%7B%22actOrderChargeByMt%22%3A%5B%7B%22comment%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22money%22%3A%220.00%22%7D%5D%2C%22actOrderChargeByPoi%22%3A%5B%7B%22comment%22%3A%22%E5%87%8F%E9%85%8D%E9%80%81%E8%B4%B920.0%E5%85%83%22%2C%22feeTypeDesc%22%3A%22%E6%B4%BB%E5%8A%A8%E6%AC%BE%22%2C%22feeTypeId%22%3A10019%2C%22money%22%3A%2212.00%22%7D%5D%2C%22foodShareFeeChargeByPoi%22%3A%220.80%22%2C%22logisticsFee%22%3A%2212.00%22%2C%22onlinePayment%22%3A%2220.50%22%2C%22poiReceive%22%3A%222.70%22%2C%22reconciliationExtras%22%3A%22%7B%5C%22performanceServiceFee%5C%22%3A%5C%225.00%5C%22%2C%5C%22chargeMode%5C%22%3A2%7D%22%7D","is_pre_order":"false","invoice_title":"","status":"4","app_poi_code":"36524","shipper_phone":"","is_third_shipping":"0","is_pre_sale_order":"false","ctime":"1657691715","shipping_fee":"12.0","has_invoiced":"0","extras":"%5B%7B%22mt_charge%22%3A0.0%2C%22poi_charge%22%3A12.0%2C%22reduce_fee%22%3A12.0%2C%22remark%22%3A%22%E5%87%8F%E9%85%8D%E9%80%81%E8%B4%B920.0%E5%85%83%22%2C%22type%22%3A25%7D%5D","package_bag_money_yuan":"0.50","incmp_modules":"%5B%5D","wm_poi_address":"%E5%8D%97%E6%9E%81%E6%B4%B204%E5%8F%B7%E7%AB%99","recipient_phone":"15280215347","wm_order_id_view":"27052664056314941","incmp_code":"0","app_id":"119094","latitude":"29.774499","recipient_address":"%E8%89%B2%E9%87%91%E6%8B%89","sig":"014b03d8f643d2ef5362d571a73603f6"}';
- $post = json_decode($str, true);
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- $orderId = $post['order_id'] ?? 0;
- try {
- $shopId = $post['app_poi_code'] ?? '';
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$orderId}", '15280215347');
- return Json::encode(['data' => 'ok']);
- }
- $mainId = $shop->mainId ?? 0;
- $sjId = $shop->sjId ?? 0;
- $customId = $shop->defaultCustomId ?? 0;
- if (empty($customId)) {
- noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$orderId}", '15280215347');
- return Json::encode(['data' => 'ok']);
- }
- $custom = CustomClass::getById($customId);
- if (empty($custom)) {
- noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$orderId}", '15280215347');
- return Json::encode(['data' => 'ok']);
- }
- foreach ($post as $k => $val) {
- $post[$k] = urldecode($val);
- }
- MtOrderClass::add($post);
- $detail = $post['detail'] ?? '';
- if (empty($detail)) {
- noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$orderId}", '15280215347');
- return Json::encode(['data' => 'ok']);
- }
- $mtGoodsData = json_decode($detail, true);
- if (empty($mtGoodsData)) {
- noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$orderId}", '15280215347');
- return Json::encode(['data' => 'ok']);
- }
- $app = new Application($this->config);
- $productList = [];
- $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
- if (empty($mtCategory)) {
- $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
- }
- $catId = $mtCategory->id ?? 0;
- if (empty($catId)) {
- noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$orderId}", '15280215347');
- return Json::encode(['data' => 'ok']);
- }
- foreach ($mtGoodsData as $mtKey => $mtGoods) {
- $mtGoodsSpu = $mtGoods['app_spu_code'];
- $params = ['app_poi_code' => $shopId, 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
- $mgGoodsDetail = $app->retail->getDetail($params);
- $mgGoodsDetail = json_decode($mgGoodsDetail, true);
- if (isset($mgGoodsDetail['error'])) {
- noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
- util::stop();
- }
- $imgString = $mgGoodsDetail['data']['picture'];
- //$imgString = 'https://awp-assets.meituan.net/set/online_b_open_developer_react/images/biz-retail-bb948ac2b3.png,https://awp-assets.meituan.net/set/online_b_open_developer_react/images/biz-medicine-b6d2fdf744.png';
- $goodsName = $mtGoods['food_name'] ?? '';
- $goodsPrice = $mtGoods['price'] ?? 0;
- $num = $mtGoods['quantity'] ?? 0;
- $hasGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
- if (empty($hasGoods)) {
- $imgArr = explode(',', $imgString);
- $shopImg = [];
- foreach ($imgArr as $imgUrl) {
- $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
- $shopImg[] = $shortUrl;
- }
- $cover = $shopImg[0];
- $goodsData = [
- 'spu' => $mtGoodsSpu, 'shopImg' => $shopImg,
- 'name' => $goodsName, 'sjId' => $sjId, 'mainId' => $mainId, 'shopId' => $shopId, 'catId' => $catId, 'cover' => $cover, 'unitGoodsPrice' => $goodsPrice, 'num' => $num,
- ];
- print_r($goodsData);
- $goods = GoodsClass::orderCreateGoods($goodsData);
- Yii::$app->redis->executeCommand('SET', ['mt_goods_spu_' . $mtGoodsSpu, md5($imgString)]);
- } else {
- $id = $hasGoods->id ?? 0;
- $unitType = 0;
- $property = dict::getDict('property', 'goods');
- $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
- $imgMd5 = Yii::$app->redis->executeCommand('GET', ['mt_goods_spu_' . $mtGoodsSpu]);
- if (empty($imgMd5) || $imgMd5 != md5($imgString)) {
- //图片需要更新
- $imgArr = explode(',', $imgString);
- $shopImg = [];
- foreach ($imgArr as $imgUrl) {
- $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
- $shopImg[] = $shortUrl;
- }
- $hasGoods->shopImg = $shopImg;
- }
- $hasGoods->name = $goodsName;
- $hasGoods->price = $goodsPrice;
- $hasGoods->save();
- }
- $productList = array_merge($goods, $productList);
- }
- $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
- if (empty($poi_receive_detail_yuan)) {
- noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '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:{$orderId}", '15280215347');
- util::stop();
- }
- $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
- if ($onlinePayment == 0) {
- noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '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);
- $orderData = [
- 'product' => $productList,
- 'fromType' => dict::getDict('fromType', 'mt'),
- 'labourCost' => $labourCost,
- 'sendCost' => $sendCost,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'modifyPrice' => $modifyPrice,
- 'serviceFee' => $serviceFee,
- ];
- $hasPay = dict::getDict('hasPay', 'payed');
- OrderService::createFinishOrder($orderData, $custom, $hasPay);
- $transaction->commit();
- return Json::encode(['data' => 'ok']);
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
- }
- }
- public function actionNewOrder()
- {
- $post = Yii::$app->request->post();
- if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
- return Json::encode(['data' => 'ok']);
- }
- return Json::encode(['data' => 'ok']);
- Yii::warning('newOrder2: ' . json_encode($post));
- // 美团传入的订单数据,中文是 URL_encode 的,执行下解密
- foreach ($post as $k => $val) {
- $post[$k] = urldecode($val);
- }
- $data = arrayUtil::humpUnderlineConversion($post, 1);
- Yii::warning('newOrder: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
- $orderId = $data['orderId'];
- $mtOrder = MtOrderClass::getByCondition(['orderId' => $orderId]);
- if (!empty($mtOrder)) {
- Yii::warning('订单号已存在:' . $orderId);
- return Json::encode(['data' => 'ok']);
- }
- // TODO 通过 app_poi_code 查询批发商的美团商家帐号,获取 access_token 与 refresh_token
- // TODO 做到上一步,要实现好批发商把他的美团商家账号授权给花掌柜
- $mtOrder = MtOrderClass::add($data);
- $picArr = [];
- $app = new Application($this->config);
- $detail = $data['detail'];
- $items = json_decode($detail, true);
- foreach ($items as $item) {
- $mtGoodsSpu = $item['app_spu_code'];
- // access_token 是商家授权后,获取到的
- $params = ['app_poi_code' => $data['appPoiCode'], 'app_spu_code' => $mtGoodsSpu, 'access_token' => 'token_pOgHs0-h06JUsIrMJ3ZKyQ'];
- $mgGoodsDetail = $app->retail->getDetail($params);
- $mgGoodsDetail = json_decode($mgGoodsDetail, true);
- if (isset($mgGoodsDetail['error'])) {
- Yii::error('get retail fail:' . json_encode($params));
- } else {
- $picArr[$mtGoodsSpu] = $mgGoodsDetail['data']['picture'];
- }
- }
- Yii::info('picture: ' . json_encode($picArr)); // 订单商品图片
- return Json::encode(['data' => 'ok']);
- }
- public function actionCancelOrder()
- {
- $post = Yii::$app->request->post();
- if (empty($post)) {
- return Json::encode(['data' => 'ok']);
- }
- Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
- return Json::encode(['data' => 'ok']);
- }
- public function demo()
- {
- $app = new Application($this->config);
- $params = ['order_id' => '27061900338318741', 'is_mt_logistics' => 1];
- $orderDetail = $app->order->getOrderDetail($params);
- print_r(json_decode($orderDetail, true));
- // 商品类、活动类接口支持异步队列
- $app->goods->async()->create([]);
- }
- }
|