对接美团外卖开放平台

话不多说 直接上代码

<?php
# 再此之前 你需要在美团的外卖开放平台配置回调地址 及 推送地址。
# 注意 美团推送的时候会有get请求,如果你的项目访问地址必带有"?" 在配置美团的时候 需要修改你的地址,apache下 修改项目根目录中的“.htaccess”文件;
# 例:http://xxx.com.cn/?md=mt&cl=mt&at=mt 这是你项目访问地址,需要修改为 http://xxx.com.cn/md=mt/mt/mt.自己网上找下怎么样配置的

# 你也可以验证是否是美团推送的信息 这里我没做。根据个人情况
# 美团所有接口要求返的信息只有一个 ['data'=>'ok'] 转json

class MTController extends commonController { ######################################################### 必接接口 订单推送API START ######################################################## const SANDBOX_APP_ID = ''; const SANDBOX_APP_SECRET = ''; const SANDBOX_URL = 'https://waimaiopen.meituan.com/api/v1/'; const SANDBOX_STORE_ID = ''; const APP_ID = ''; const APP_SECRET = ''; const URL = ''; const STORE_ID = ''; /** * 推送已确认订单 这里你可以操作获取订单信息存储数据库等 ;该信息由美团直接POST 推送到这个配置接口地址 */ public function MTConfirmOrderAction() { $log = new Logging(); $log->lfile('MTConfirmOrder'); if($_POST && $_POST['sig'] && count((array)$_POST) > 0){ $url_detail = urldecode($_POST['detail']); $detail = json_decode($url_detail,true); $field['order_no'] = $_POST['order_id']; $field['shop_no'] = $_POST['app_poi_code']; $field['lat'] = $_POST['latitude']; $field['lng'] = $_POST['longitude']; $field['source'] = '美团'; $field['buyer'] = urldecode($_POST['recipient_name']); $field['receiver'] = urldecode($_POST['recipient_name']); $field['receiver_tel'] = $_POST['recipient_phone']; $field['receiver_address'] = urldecode($_POST['recipient_address']); $field['customre_memo'] = urldecode($_POST['caution']); $field['actual_pay'] = $_POST['total']; $field['order_time'] = $_POST['ctime']; $field['delivery_time'] = $_POST['delivery_time']; $field['consignment_sync'] = 'Y'; $field['status'] = 0; $field['shop_name'] = $_POST['wm_poi_name']; $field['ph_no'] = 'MT'.$_POST['order_id']; $field['pos_fee'] = $_POST['shipping_fee']; $field['createdt'] = 'now()'; $OID = $this->model->addItem($field, 'orders'); $field = []; foreach ($detail as $v){ $field['orders_id'] = $OID; $field['product_no'] = $v['app_food_code']; $field['product_name'] = $v['food_name']; $field['qty'] = $v['quantity']; $field['unit_price'] = $v['price']; $field['ph_no'] = 'MT'.$_POST['order_id']; $field['createdt'] = 'now()'; $this->model->addItem($field, 'orders_product'); } } $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 已完成订单推送回调 */ public function MTFinishStatusAction() { $log = new Logging(); $log->lfile('MTFinishStatus'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($this->params,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 订单配送状态回调 */ public function MTOrderStatusAction() { $log = new Logging(); $log->lfile('MTOrderStatus'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 门店状态变更回调 */ public function MTStoreStatusAction() { $log = new Logging(); $log->lfile('MTStoreStatus'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 隐私号降级通知推送 可不接 */ public function MTPushNotificationAction() { $log = new Logging(); $log->lfile('MTPushNotification'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 推送已支付订单 */ public function MTPushPaidOrdersAction() { $log = new Logging(); $log->lfile('MTPushPaidOrders'); $this->MTConfirmAction($_POST['order_id']); # 直接确认订单 // $this->MTCancelAction($_POST['order_id']); # 直接取消订单 $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 推送催单消息 */ public function MTReminderAction() { $log = new Logging(); $log->lfile('MTReminder'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } ######################################################### 订单推送API END ######################################################## ######################################################### 取消订单推送API START ######################################################## /** * 推送取消订单消息 推送用户或客服取消订单 */ public function MTPushCancelOrderAction() { $log = new Logging(); $log->lfile('MTPushCancelOrder'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($this->params,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } ######################################################### 取消订单推送API END ######################################################## ######################################################### 退款信息推送API START ######################################################## /** * 推送全额退款信息 */ public function MTFullRefundAction() { $log = new Logging(); $log->lfile('MTFullRefund'); $this->MTAgreeAction($this->params['order_id'],$this->params['reason']); # 确认退款 // $this->MTRejectAction($this->params['order_id'],$this->params['reason']); # 驳回退款 $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode( $this->params ,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 推送部分退款信息 无法测试 APP 提示 只能整单退款 */ public function MTRebatesAction() { $log = new Logging(); $log->lfile('MTRebates'); $this->MTAgreeAction($this->params['order_id'],$this->params['reason']); # 确认退款 // $this->MTRejectAction($this->params['order_id'],$this->params['reason']); # 驳回退款 $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($this->params,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } ######################################################### 退款信息推送API END ######################################################## ######################################################### 必接接口 订单类API START ######################################################## /** * 商家确认订单 */ public function MTConfirmAction($order_id) { $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/confirm'; $parameter['order_id'] = $order_id; $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $log = new Logging(); $log->lfile('MTConfirm'); $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter,JSON_UNESCAPED_UNICODE)); } /** * 推送订单结算信息 */ public function MTOrderCloseAction() { $log = new Logging(); $log->lfile('MTOrderClose'); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($_POST,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 驳回订单退款申请 */ public function MTRejectAction($order_id,$reason) { $log = new Logging(); $log->lfile('MTReject'); $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/refund/reject'; $parameter['order_id'] = $order_id; $parameter['reason'] = $reason; $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); $r = ['data'=>'ok']; $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter,JSON_UNESCAPED_UNICODE)); echo json_encode($r); } /** * 订单确认退款请求 */ public function MTAgreeAction($order_id,$reason) { $log = new Logging(); $log->lfile('MTAgree'); $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/refund/agree'; $parameter['order_id'] = $order_id; $parameter['reason'] = $reason; $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter,JSON_UNESCAPED_UNICODE)); } /** * 拉取用户真实手机号 */ public function MTBatchPullPhoneNumberAction() { $log = new Logging(); $log->lfile('MTBatchPullPhoneNumber'); $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $STORE_ID = (($webSite['debug']) ? self::SANDBOX_STORE_ID : self::STORE_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/batchPullPhoneNumber'; // $parameter['app_poi_code'] = ''; # 非必填 $parameter['offset'] = 0; # 分页偏移量 $parameter['limit'] = 100; # 每页请求数 $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $sign = explode('&',$sign); $sign = array_pop($sign); $parameter['sig'] = substr($sign,strpos($sign,'=')+1); $Result = $this->http_post($URL,$parameter); # 获取一个多维数组【结构:$Result['data'][多个]】 $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter)); } /** * 商家取消订单 */ public function MTCancelAction($order_id) { $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/cancel'; $parameter['order_id'] = $order_id; $parameter['timestamp'] = time(); $parameter['reason'] = '卖光了'; $parameter['reason_code'] = 1001; $parameter['app_id'] = $APP_ID; $log = new Logging(); $log->lfile('MTCancel'); $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE)); } /** * 拉取骑手真实手机号 */ public function MTGetRiderInfoPhoneNumberAction() { $log = new Logging(); $log->lfile('MTGetRiderInfoPhoneNumber'); $Result = file_get_contents("php://input"); $Data = json_decode($Result, true); $r = ['data'=>'ok']; echo '<pre>'; var_dump($Data); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($r)); echo json_encode($r); } /** * 订单配送中 改状态 */ public function MTAeliveringAction() { $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $log = new Logging(); $log->lfile('MTAelivering'); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/delivering'; $parameter['order_id'] = '27009292362162587'; $parameter['timestamp'] = time(); $parameter['courier_name'] = '是我'; $parameter['courier_phone'] = 17321295203; $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter)); } /** * 修改状态送达 */ public function MTArrivedAction() { $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $log = new Logging(); $log->lfile('MTArrived'); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'order/arrived'; $parameter['order_id'] = '27009292362162587'; $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter)); } ######################################################### 订单类API END ######################################################## ######################################################### 店铺相关 START ######################################################## /** * 创建修改店铺 */ public function MTSaveStoreAction(){ $log = new Logging(); $log->lfile('MTSaveStore'); $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'poi/save'; $parameter['app_poi_code'] = '123456'; # APP方门店ID,最长不超过128个字符 $parameter['name'] = '朱泾店修改测试'; # 是 门店名称 $parameter['address'] = '上海金山区万安街634号'; # 是 门店地址 $parameter['latitude'] = 30.8971; # 是 门店纬度 (美团使用的是高德坐标系,也就是火星坐标系,如果是百度坐标系需要转换)(门店坐标不需要乘以一百万) $parameter['longitude'] = 121.16630; # 是 门店经度 // $parameter['pic_url'] = ''; # 否 门店头图图片地址 // $parameter['pic_url_large'] = ''; # 否 logo $parameter['phone'] = '17321295203'; # 是 客服电话号码 $parameter['shipping_fee'] = 0.2; # 是 每个订单的配送费 $parameter['shipping_time'] = '7:00-22:00'; # 是 7:00-9:00,11:30-19:00 门店营业时间 (注意格式,且保证不同时间段之间不存在交集) // $parameter['promotion_info'] = ''; # 否 门店推广信息 $parameter['open_level'] = 1; # 是 门店的营业状态:1为可配送,3为休息中 $parameter['is_online'] = 1; # 是 门店上下线状态:1为上线,0为下线,门店必须在菜品、配送范围和门店信息都齐全后,才能提交上线 (注意:此字段不为1时门店不会提交审核) // $parameter['invoice_support'] = ''; # 否 门店是否支持发票 // $parameter['invoice_min_price'] = ''; # 否 门店支持开发票的最小订单价(invoice_suport为1时可用) // $parameter['invoice_description'] = ''; # 否 发票相关说明(invoice_support为1时可用) 发票特殊说明,如只有定额发票,不支持开抬头等 $parameter['third_tag_name'] = '快餐简餐'; # 是 APP方品类名称需要根据接口“list 获取门店品类列表”获取门店品类列表接口获得,如果一个门店包含多个品类须以英文逗号隔开,如中餐,西餐 # 目前最多支持上传两个门店品类:一个主营品类(上传的第一个品类为主营品类),一个辅营品类;部分门店品类只支持上传一个品类(例如:火锅,特色菜,地方菜,东南亚菜,日韩料理,生活超市) // $parameter['pre_book'] = ''; # 否 是否支持营业时间范围外预下单,1表支持,0表不支持 // $parameter['time_select'] = ''; # 否 是否支持营业时间范围内预下单,1表支持,0表不支持,此字段开启后不可关闭,新建门店将自动开启 // $parameter['app_brand_code'] = ''; # 否 第三方品牌code值(对接的三方需要提前将该值告诉美团技术同学) $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $sign = explode('&',$sign); $sign = array_pop($sign); $parameter['sig'] = substr($sign,strpos($sign,'=')+1); $Result = $this->http_post($URL,$parameter); var_dump($Result); $Data = json_decode($Result, true); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter)); } /** * 获取门店列表 */ public function MTPoiMgetAction(){ $log = new Logging(); $log->lfile('MTPoiMget'); $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'poi/mget'; $parameter['app_poi_codes'] = "123456"; # APP方门店id(半角逗号分隔) $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $Result = $this->getCurl($sign); $Data = json_decode($Result, true); var_dump($Data); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter)); } /** * 获取门店品类列表 */ public function MTPoiTagListAction(){ $log = new Logging(); $log->lfile('MTPoiTagList'); $Config = Config::getInstance(); $webSite = $Config->getWebSiteConfig(); $APP_ID = (($webSite['debug']) ? self::SANDBOX_APP_ID : self::APP_ID); $URL = (($webSite['debug']) ? self::SANDBOX_URL : self::URL).'poiTag/list'; $parameter['timestamp'] = time(); $parameter['app_id'] = $APP_ID; $sign = $this->_sign($parameter,$URL); $sign = explode('&',$sign); $sign = array_pop($sign); $parameter['sig'] = substr($sign,strpos($sign,'=')+1); $Result = $this->http_post($URL,$parameter); $Data = json_decode($Result, true); var_dump($Data); $log->lwrite('reception:' . json_encode($Data,JSON_UNESCAPED_UNICODE).'response:'.json_encode($parameter)); } ######################################################### 店铺相关 END ######################################################## /** * 签名生成 * @param $parameter 数组参数
   * @param $URL 地址 * @return string */ private function _sign($parameter,$URL){ # 签名自己去根据文档写 return $args; } /** * POST 请求 * @param $url * @param $post_data * @return bool|string */ private function http_post($url, $post_data) { $header_data[] = "application/x-www-form-urlencoded"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); $sResult = curl_exec($ch); $info = curl_getinfo($ch); if($sError=curl_error($ch)){ die($sError); } curl_close($ch); return $sResult; } # get请求 private function getCurl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close ($ch); return $result; } }

猜你喜欢

转载自www.cnblogs.com/G921123/p/12523782.html