对接美团外卖开放平台

时间:2024-01-26 14:20:53

 

话不多说 直接上代码

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




 <?php

 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 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 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 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 ######################################################## }