本文主要为填坑,为了文章格式好看会贴一些官方文档,着急的小伙伴直接下拉到最后看结果。
一、申请资质及监管处罚
满足以下所有条件的小程序才可进行申请使用:
- 1,需要开通了微信支付的小程序
- 2,过去30 天有交易
- 3,类目(内测暂不需要)
频次限制
- 1,一个 appid 最多调用10w 次/日
- 2,一个用户最多调用 100次/日
监管及处罚
- 1)仅支持查询「引用查询组件的小程序」产生的购物订单的物流运单。
- 2)仅支持给「已订阅微信快递服务」且订单实际购买者下发消息。
若违反监管条理,进行相应处罚,包括但不限于降低调用频次、禁用插件能力等
二、插件引入说明
当服务商调用时请使用第三方平台接口调用令牌authorizer_access_token以调用接口。
前端引入就不多说了,主要写后端请求接口:https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=XXX)返回的 waybill_token。
三、传运单接口 trace_waybill
- 描述:商户使用此接口向微信提供某交易单号对应的运单号。微信后台会跟踪运单的状态变化
- 请求方法: POST application/json
- 请求地址:https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=XXX
1、请求参数
参数名称 | 类型 | 必选 | 备注 |
---|---|---|---|
openid | string | 是 | 用户openid |
sender_phone | string | 否 | 寄件人手机号 |
receiver_phone | string | 是 | 收件人手机号,部分运力需要用户手机号作为查单依据 |
delivery_id | string | 否 | 运力id(运单号所属运力公司id),该字段从 get_delivery_list 获取。 该参数用于提高运单号识别的准确度;特别是对非主流快递公司,建议传入该参数,确保查询正确率。 |
waybill_id | string | 是 | 运单号 |
goods_info | object | 是 | 商品信息 |
trans_id | string | 是 | 交易单号(微信支付生成的交易单号,一般以420开头) |
order_detail_path | string | 否 | 点击落地页商品卡片跳转路径(建议为订单详情页path),不传默认跳转小程序首页。 |
2、本次更新点:sender_phone,receiver_phone。
其中goods_info的内容如下:
参数名称 | 类型 | 必选 | 备注 |
---|---|---|---|
detail_list | array | 是 | 商品信息 |
其中goods_info.detail_list的每一项的内容如下:
参数名称 | 类型 | 必选 | 备注 |
---|---|---|---|
goods_name | string | 是 | 商品名称 |
goods_img_url | string | 是 | 商品图片url |
goods_desc | string | 否 | 商品详情描述,不传默认取“商品名称”值,最多40汉字 |
3、返回参数
参数名称 | 类型 | 必选 | 备注 |
---|---|---|---|
errcode | number | 是 | 返回码 |
errmsg | string | 是 | 错误信息 |
waybill_token | string | 是 | 查询id |
四、请求示例参数
{
"openid":"ovtZW4yB7DIj3CxOb6ii-nk4HhFo",
"waybill_id":"WXTESTEXPRESS0000014",
"sender_phone":"12345678901" ,
"receiver_phone":"123456566" ,
"delivery_id":"KYSY",
"goods_info":{
"detail_list":[
{
"goods_name":"测试名字",
"goods_img_url":"www.qq.com"
},
{
"goods_name":"测试名字2",
"goods_img_url":"www.qq.com"
}
]
}
}
五、返回参数
{
"errcode": 0,
"errmsg": "ok",
"waybill_token": "o_ARWHaxIxzWHmdui-AIw9KBr8qNnbmc08V0KhDyXE-IMLo6AcOqJkPsNLcLzfTb"
}
六、具体代码
public function getWaybill($openid,$sender_phone,$receiver_phone,$delivery_id,$waybill_id,$goods_list,$trans_id)
{
$access_token = ''; // 这里调用之前写的access_token的获取方法
$url = 'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=' . $access_token;
$w_data = [
'openid'=>$openid,
'sender_phone'=>$sender_phone,
'receiver_phone'=>$receiver_phone,
'delivery_id'=>$delivery_id,
'waybill_id'=>$waybill_id,
'goods_info'=>[
'detail_list'=>$goods_list
],
'trans_id'=>$trans_id,
];
$response = $this->httpPost($url, json_encode($w_data,JSON_UNESCAPED_UNICODE), [
'Authorization: Bearer ' . $access_token,
'Content-Type: application/json',
]);
$res = json_decode($response, true);
if(isset($res['waybill_token'])) {
return ['code'=>100,'data'=>$res['waybill_token'],'msg'=>'success'];
}else return ['code'=>-23,'data'=>'','msg'=>$res['errmsg']];
}
public function httpPost($url, $data, $headers = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不验证SSL证书
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
七、重点
相信各位小伙伴对接个API还是很简单的,下图红框内容是重点:
是的没错,重点就是请求的时候需要加上header……