前言:相关权限一定要授权!!!
准备工作:
1、开发者账户
2、开通接口权限!!!这点一定要确认好了,大坑。
2、品牌账户
3、品牌账户创建门店
4、品牌账户创建商品下发到门店
5、门店连接POS机,
6、开发者配置回调地址
a、地址一:业务授权码回调地址
b、地址二:业务消息回调地址
业务步骤参考文档!!!
下面我们按照文档的步骤来分析讲解
前面三个去联系销售人员,要是不懂的,有够麻烦的
接口开发
1、签名规则 参考链接
//签名计算方法
function get_sign($sign_key, $data)
{
if ($data == null) {
return null;
}
ksort($data);
$result_str = "";
foreach ($data as $key => $val) {
if ($key != null && $key != "" && $key != "sign") {
$result_str = $result_str . $key . $val;
}
}
$result_str = $sign_key . $result_str;
$ret = bin2hex(sha1($result_str, true));
return $ret;
}
第二步:业务授权URL拼装
<?php
//美团手动授权文件
include './php_conn.php';
$timestamp = time();
$data = array("developerId" => DEVELOPERID_TUAN, "businessId" => BUSINESSID_TUAN, "timestamp" => $timestamp, "charset" => 'UTF-8', "state" => "code");
$sign = get_sign(SIGNKEY_TUAN, $data);
//业务授权url
$url = 'https://open-erp.meituan.com/general/auth?developerId=' . DEVELOPERID_TUAN . '×tamp=' . $timestamp . '&charset=UTF-8&businessId=18&sign=' . $sign . '&state=code';
header('Location:' . $url);
exit();
?>
拼装授权后会自动跳转到美团的授权界面,使用商户账户登录
登录后界面,注意一定要有授权!!!!
第三步:回调地址获取authorization_code后同步获取access_token
日志存储函数
/**
* 日志内容储存(默认进入temp文件夹)
* log_api 小程序接口日志(页面预览信息等等)
* log_pay 支付接口日志
* log_wxmp 微信接口日志
* web_cache 页面缓存
*/
function log_result($str, $wjj = 'other', $file = "")
{
$file = $file ? $file : "" . date("Ymd") . ".txt";
$save_path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "temp" . DIRECTORY_SEPARATOR . $wjj . DIRECTORY_SEPARATOR;
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$file_path = $save_path . $file;
$fp = fopen($file_path, "a+");
fwrite($fp, "【" . date("Y-m-d H:i:s") . "】\r\n" . $str . "\r\n\r\n\r\n");
fclose($fp);
}
//curl发送post数据
function curl_post_json($url, $data = array())
{
$curl = curl_init();
$data_string = array();
foreach ($data as $k => $v) {
$data_string[] = $k . '=' . $v;
}
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => implode('&', $data_string),
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
注意:
-
接口只支持https;
-
端口只支持80或者443;
-
授权码有效期仅有10分钟,超时则需要重新生成授权链接,由商家重新授权。
-
请在“控制台->基础设置->回调接口设置->统一回调地址->业务授权码回调地址”中进行设置
<?php
//业务码回调
$get_data = $_GET;
$logStr = "【美团授权回调" . date("Y-m-d H:i:s", time()) . "】\r\n反馈结果:" . json_encode($get_data, true) . "";
log_result($logStr, "log_meituan_code", date("Ymd") . "_code.txt");
if ($get_data["state"] == 'code' && $get_data["code"]) {
$url = 'https://api-open-cater.meituan.com/oauth/token';
$data_arr = array();
$data_arr["businessId"] = BUSINESSID_TUAN;
$data_arr["charset"] = 'UTF-8';
$data_arr["code"] = $get_data["code"];
$data_arr["developerId"] = (float)$get_data["developerId"];
$data_arr["grantType"] = 'authorization_code';
$data_arr["timestamp"] = (float)time();
$sign = get_sign(SIGNKEY_TUAN, $data_arr);
$data_arr["sign"] = $sign;
$res = curl_post_json($url, $data_arr);
$res_arr = json_decode($res, true);
$logStr = "【美团授权回调(access_token)" . date("Y-m-d H:i:s", time()) . "】\r\n反馈结果:" . $res . "";
log_result($logStr, "log_meituan_code", date("Ymd") . "_code.txt");
if ($res_arr["code"] == 0) {
$file = "access_token_meituan.txt";
$file_up = "access_token_meituan_up.txt";
$save_path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "temp" . DIRECTORY_SEPARATOR;
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$file_path = $save_path . $file;
$file_path_up = $save_path . $file_up;
$access_token_meituan = $res_arr["data"]["accessToken"];
$_str = $access_token_meituan . "_||_" . (time() + $res_arr["data"]["expireIn"]);
$access_token_meituan_up = $res_arr["data"]["refreshToken"];
$_str_up = $access_token_meituan_up . "_||_" . (time() + $res_arr["data"]["expireIn"]);
file_put_contents($file_path, $_str);
file_put_contents($file_path_up, $_str_up);//存入txt文档
}
}
第五步:获取access_token
注意:ISV/开发者请在访问令牌过期之前(授权后30天内),通过更新令牌接口主动请求开放平台,获取新的访问令牌
//获取access_token
function access_token_meituan()//获取访问令牌
{
global $access_token_meituan;
if (!$access_token_meituan) {
$file = "access_token_meituan.txt";
$save_path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "temp" . DIRECTORY_SEPARATOR;
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$file_path = $save_path . $file;
if (file_exists($file_path)) {
$fp = fopen($file_path, "r");
$fp_str = fread($fp, filesize($file_path));
fclose($fp);
if ($fp_str) {
$arr = explode("_||_", $fp_str);
$access_time = $arr[1];
if ($access_time && $access_time > time()) {
$access_token_meituan = $arr[0];
}
}
}
}
return $access_token_meituan;
}
function access_token_meituan_up()//获取更新令牌
{
global $access_token_meituan_up;
if (!$access_token_meituan_up) {
$file = "access_token_meituan_up.txt";
$save_path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "temp" . DIRECTORY_SEPARATOR;
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$file_path = $save_path . $file;
if (file_exists($file_path)) {
$fp = fopen($file_path, "r");
$fp_str = fread($fp, filesize($file_path));
fclose($fp);
if ($fp_str) {
$arr = explode("_||_", $fp_str);
$access_time = $arr[1];
if ($access_time && $access_time > time()) {
$access_token_meituan_up = $arr[0];
}
}
}
}
return $access_token_meituan_up;
}
第六步:更新access_token (可以放在服务器周期任务中,每20天访问一次)
<?php
//美团手动修改access_token文件
$url = 'https://api-open-cater.meituan.com/oauth/refresh';//更新签名
$data_arr = array();
$data_arr["timestamp"] = time();
$data_arr["scope"] = 'all';
$data_arr["refreshToken"] = access_token_meituan_up();
$data_arr["grantType"] = 'refresh_token';
$data_arr["developerId"] = DEVELOPERID_TUAN;
$data_arr["charset"] = 'UTF-8';
$data_arr["businessId"] = BUSINESSID_TUAN;
$sign = get_sign(SIGNKEY_TUAN, $data_arr);
$data_arr["sign"] = $sign;
$res = curl_post_json($url, $data_arr);
$res_arr = json_decode($res, true);
$logStr = "【美团更签名(access_token_up)" . date("Y-m-d H:i:s", time()) . "】\r\n反馈结果:" . $res . "";
log_result($logStr, "log_meituan_code", date("Ymd") . "_code.txt");
if ($res_arr["code"] == 0) {
$file = "access_token_meituan.txt";
$file_up = "access_token_meituan_up.txt";
$save_path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "temp" . DIRECTORY_SEPARATOR;
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$file_path = $save_path . $file;
$file_path_up = $save_path . $file_up;
$access_token_meituan = $res_arr["data"]["accessToken"];
$_str = $access_token_meituan . "_||_" . (time() + $res_arr["data"]["expireIn"]);
$access_token_meituan_up = $res_arr["data"]["refreshToken"];
$_str_up = $access_token_meituan_up . "_||_" . (time() + $res_arr["data"]["expireIn"]);
file_put_contents($file_path, $_str);
file_put_contents($file_path_up, $_str_up);
}
七: 推送美团订单接口示例
<?php /* * 美团公共请求接口 * $url 请求地址 * $array 请求内容 * $retu_arry 返回数 */ function MeiTuanYin($url, $bizarray = array()) { $array = array(); $array["appAuthToken"] = access_token_meituan(); $array["charset"] = 'UTF-8'; $array["timestamp"] = (float)time(); $array["version"] = 2; $array["developerId"] = DEVELOPERID_TUAN; $array["businessId"] = BUSINESSID_TUAN; $array["biz"] = json_encode($bizarray, JSON_UNESCAPED_UNICODE); $sign = get_sign(SIGNKEY_TUAN, $array); $array["sign"] = $sign; $res = curl_post_json($url, $array); // $res_arr = json_decode($res, true); return $res; } //推送订单 $Mtuanurl = 'https://api-open-cater.meituan.com/rms/trade/api/v1/poi/order/push'; $Mtuanbiz = array(); $Mtuanbiz["orgId"] = $rs_mendian["meituan_id"]; $Mtuanbiz["messageType"] = $messageType; $Mtuanbiz["order"] = array(); $res_M = MeiTuanYin($Mtuanurl, $Mtuanbiz); //登记日志 $logStr = "【美团订单推送数据-反馈结果-messageType=" . $messageType . "】:\r\n" . $res_M . "\r\n请求参数:" . json_encode($Mtuanbiz, JSON_UNESCAPED_UNICODE); log_result($logStr, "log_meituan", date("Ymd") . "_template.txt"); $res_M_arr = json_decode($res_M, true); ?>
注意点:
1、如果订单推送成功后,POS机点击接单后出现这个提示,是美团服务商的问题,联系解决。代码无问题
2、如果出现这个提示,则表示,开发者消息回调地址没有配置
3、如果配置消息回调地址后出现以下提示,表示回调消息中返回结果不对
八、回调消息接口配置和代码
<?php //美团收银订单状态修改回调 $post_data = file_get_contents("php://input"); if ($post_data) { echo '{"code": 0,"message": "success"}'; $post_data_arr = convertUrlQuery(urldecode($post_data)); $post_data_arr["message"] = json_decode($post_data_arr["message"], JSON_UNESCAPED_UNICODE); if ($post_data_arr["message"] && $post_data_arr["message"]["mtOrderId"]) { if ($rsor = _get_one_tj('shop_order', "meituan_or_danhao='" . $post_data_arr["message"]["mtOrderId"] . "'")) { if ($post_data_arr["msgType"] == 1810011) {//接单 } elseif ($post_data_arr["msgType"] == 1810013) {//拒绝接单 } elseif ($post_data_arr["msgType"] == 1810017) {//制作完成,已发货 } elseif ($post_data_arr["msgType"] == 1810019) {//以取餐,已发货 } elseif ($post_data_arr["msgType"] == 1810023) {//商家主动取消订单 } } } } else { echo '{"code": -1,"message": "系统错误"}'; } $logStr = "【美团订单二回调" . date("Y-m-d H:i:s", time()) . "】\r\n反馈结果:" . json_encode($post_data_arr, JSON_UNESCAPED_UNICODE) . ""; log_result($logStr, "log_meituan_callback", date("Ymd") . "_callback2.txt"); ?>
久:订单推送注意事项
如果第三方小程序中的订单状态发生改变后,需同步POS订单。还是使用推单接口!并且推送的参数和推单一样!对应的massagetype状态值不一样,同时在订单参数中,填写推送成功后返回的POS订单ID