后端建立通道
后端建立通道
:::tip
打开采集发票页面导入的数据通常是通过JS通道进行传输,当受到网络限制(只允许单向通讯)等等情况的时候,可以使用后端通道。
后端通道分为两种:
- 长轮询方式
- websocket方式
:::
长轮询方式
获取数据
请求
- 路径: /polling/fpzs/invoice/data/v2?access_token=access_token&key={key}
- 请求方式: GET
- 间隔:30秒(期间会HOLD住整个请求,30秒内若有数据会立即返回响应,若无数据,30秒后会返回没有数据的错误码及提示
参数名 | 是否必须 | 备注 |
---|---|---|
access_token | 是 | 授权信息,获取方式详见access_token |
key | 是 | MD5(linkKey),linkKey与打开选择发票url中传的需保持一致 |
返回
成功示例
{
"errcode": "0000",
"data": {
"deliveryTimes": 3,
"data": {
"data": {
"invoiceSerialNos": "",
"data": [
{发票结构化数据1},
{发票结构化数据2}
],
"billnumber": "",//单据编号
"invoicesAmount": 27040, //价税合计
"bxd_key": "",//单据ID
"invoicesTaxAmount": 0,//税额
"userid": ""//userKey里面的eid
},
"eventType": "push-invoice",
"type": "saveBill"
},
"createTime": 1719220813807,
"msgId": "O296UUXO0D",
"from": "2fdcb9f69ab33d5ed97faf90692a4636",
"key": "d0dade658084e06305483d277103bb44"
},
"description": "成功"
}
失败示例
{
"errcode": "1307",
"description": "没有数据"
}
确认回执
请求
- 路径: /polling/fpzs/portal/receipt?access_token=access_token&key={key}&msgId={msgId}
- 请求方式: GET
参数名 | 是否必须 | 备注 |
---|---|---|
access_token | 是 | 授权信息,获取方式详见access_token |
key | 是 | MD5(linkKey),linkKey与打开选择发票url中传的需保持一致 |
msgId | 是 | 获取数据中返回的msgId |
成功示例
{
"errcode": "0000",
"description": "成功"
}
示例代码
public static void main(String[] args) {
// 替换为你自己的accessToken
String accessToken = "YOUR_ACCESS_TOKEN";
//MD5(linkKey),linkKey与打开选择发票url中传的需保持一致
String key = "linkKey";
for(;;){
//发送请求
String response = HttpUtil.get(HOST + "/polling/fpzs/invoice/data/v2?access_token="+accessToken+"&key="+key);
JSONObject responseJson = JSONObject.parseObject(response);
//请求成功
if ("0000".equals(responseJson.getString("errcode"))) {
String confirmResult = HttpUtil.get(HOST + "/polling/fpzs/portal/receipt?access_token=" + accessToken + "&key=" + key + "&msgId=" + responseJson.getString("msgId"));
System.out.println("确认回执结果:"+confirmResult);
//处理业务
....
}
}
}
websocket方式
引入JAR包
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.19</version>
</dependency>
//测试环境: https://api-dev.piaozone.com/test
//正式环境: https://api.piaozone.com
private static final String HOST = "https://api-dev.piaozone.com/test";
//socket正式环境连接地址:https://link.piaozone.com
//socket测试环境连接地址:https://link-test.piaozone.com
private static final String websocketUrl = "https://link.piaozone.com";
public static void main(String[] args) {
// 替换为你自己的accessToken
String accessToken = "YOUR_ACCESS_TOKEN";
//MD5(linkKey),linkKey与打开选择发票url中传的需保持一致
String key = "linkKey";
IO.Options options = new IO.Options();
options.query = "userId="+key;
Socket socket = IO.socket(websocketUrl, options);
socket.on(key, response -> {
if (null != response) {
JSONObject responseJson = JSONObject.parseObject(response);
//请求成功
if ("0000".equals(responseJson.getString("errcode"))) {
String confirmResult = HttpUtil.get(HOST + "/polling/fpzs/portal/receipt?access_token=" + accessToken + "&key=" + key + "&msgId=" + responseJson.getString("msgId"));
System.out.println("确认回执结果:"+confirmResult);
//处理业务
....
}
}
});
}
最后修改时间: 5 个月前