对接美团外卖开放平台

 

话不多说 直接上代码

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

1
<br><br>

 <?php

 class MTController extends commonController {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
######################################################### 必接接口 订单推送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 推送到这个配置接口地址<br>
     */
    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 ########################################################
 
 
 
 
}

 

posted @   现世中的素人  阅读(2514)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示