顺丰接口对接-订单创建与取消(java单元测试)

api文档

下单接口 :https://qiao.sf-express.com/Api/ApiDetails?level3=393&interName=%E4%B8%8B%E8%AE%A2%E5%8D%95%E6%8E%A5%E5%8F%A3-EXP_RECE_CREATE_ORDER

取消订单接口: https://qiao.sf-express.com/Api/ApiDetails?level3=339&interName=%E8%AE%A2%E5%8D%95%E7%A1%AE%E8%AE%A4%2F%E5%8F%96%E6%B6%88%E6%8E%A5%E5%8F%A3-EXP_RECE_UPDATE_ORDER

  • 每个接口需要测试成功三次才可上线
  • 下单接口上线需要上传电子面单,下线订单量不大时可以走免面单申请,不需要提交电子面单也可以上线接口

单元测试类

java 复制代码
package com.cqdh.group.applet;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;

/**
 * @Description 顺丰API测试
 * @Date 2023/10/17 16:21
 * @Author 余乐
 **/
public class SFApiTest {

    /**
     * 客户编码
     */
    private static final String partnerID = "ZYHNYK*****";
    /**
     * 测试环境url
     */
    private static final String url = "https://sfapi-sbox.sf-express.com/std/service";
    /**
     * 校验码
     */
    private static final String checkWord = "pUVFxVbrao35vtzY***********";


    /**
     * 创建物流订单 ,修改订单orderId即可反复下单
     *
     * @throws UnsupportedEncodingException
     */
    @Test
    public void createLogisticsOrder() throws UnsupportedEncodingException {
        // 请求参数
        String serviceCode = "EXP_RECE_CREATE_ORDER";
        String requestID = UUID.randomUUID().toString();
        String timespase = System.currentTimeMillis() + "";
        String msgData = "{\"expressTypeId\":1,\"orderId\":\"SC1714182733078591144\",\"payMethod\":1,\"totalWeight\":1,\"monthlyCard\":\"7551234567\",\"signBackRemark\":\"test--签\",\"contactInfoList\":[{\"country\":\"CN\",\"contactType\":1,\"tel\":\"13866666666\",\"address\":\"重庆市渝北区两港大道与婵衣路交叉路口往东约140米\",\"contact\":\"小红\"},{\"country\":\"CN\",\"contactType\":2,\"tel\":\"13866666666\",\"address\":\"遵义市汇川区红河路莲花山公园西南侧约150米\",\"contact\":\"张三\"}],\"isDocall\":1}";

        String msgDigest = getMsgDigest(msgData, timespase, checkWord);
        // 构建请求头
        HttpRequest request = HttpRequest.post(url);
        request.header("Content-Type", "application/x-www-form-urlencoded");
        // 构建请求体
        String formData = String.format("serviceCode=%s&partnerID=%s&requestID=%s&timestamp=%s&msgDigest=%s&msgData=%s",
                serviceCode, partnerID, requestID, timespase, msgDigest, msgData);
        // 发送请求并获取响应
        HttpResponse response = request.body(formData).execute();

        // 解析响应内容
        String result = response.body();
        System.out.println("响应内容:" + result);
    }

    /**
     * 取消物流订单 ,必须取消成功三次才可上线,通过接口创建成功的订单才可取消
     *
     * @throws UnsupportedEncodingException
     */
    @Test
    public void cannelLogisticsOrder() throws UnsupportedEncodingException {
        // 请求参数
        String serviceCode = "EXP_RECE_UPDATE_ORDER";
        String requestID = UUID.randomUUID().toString();
        String timespase = System.currentTimeMillis() + "";
        String msgData = "{\"dealType\":2,\"language\":\"zh-CN\",\"orderId\":\"SC1714182733078597644\",\"totalWeight\":1,\"waybillNoInfoList\":[]}";

        String msgDigest = getMsgDigest(msgData, timespase, checkWord);
        // 构建请求头
        HttpRequest request = HttpRequest.post(url);
        request.header("Content-Type", "application/x-www-form-urlencoded");

        // 构建请求体
        String formData = String.format("serviceCode=%s&partnerID=%s&requestID=%s&timestamp=%s&msgDigest=%s&msgData=%s",
                serviceCode, partnerID, requestID, timespase, msgDigest, msgData);
        // 发送请求并获取响应
        HttpResponse response = request.body(formData).execute();

        // 解析响应内容
        String result = response.body();
        System.out.println("响应内容:" + result);
    }


    public static String getMsgDigest(String msgData, String timeStamp, String md5Key) throws UnsupportedEncodingException {
        return (new Base64()).encodeAsString(md5Encrypt(URLEncoder.encode(msgData + timeStamp + md5Key, "UTF-8")));
    }

    private static byte[] md5Encrypt(String encryptStr) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(encryptStr.getBytes("utf8"));
            return md5.digest();
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }
}

下单响应报文

java 复制代码
{
    "apiErrorMsg":"",
    "apiResponseID":"00018BB37AD8993FDF02951B4A6F363F",
    "apiResultCode":"A1000",
    "apiResultData":"{\"success\":true,\"errorCode\":\"S0000\",\"errorMsg\":null,\"msgData\":{\"orderId\":\"SC1714182733078591144\",\"originCode\":\"023\",\"destCode\":\"052\",\"filterResult\":2,\"remark\":\"\",\"url\":null,\"paymentLink\":null,\"isUpstairs\":null,\"isSpecialWarehouseService\":null,\"mappingMark\":null,\"agentMailno\":null,\"returnExtraInfoList\":null,\"waybillNoInfoList\":[{\"waybillType\":1,\"waybillNo\":\"SF7444473317974\",\"boxNo\":null,\"length\":null,\"width\":null,\"height\":null,\"weight\":null,\"volume\":null}],\"routeLabelInfo\":[{\"code\":\"1000\",\"routeLabelData\":{\"waybillNo\":\"SF7444473317974\",\"sourceTransferCode\":\"023W\",\"sourceCityCode\":\"023\",\"sourceDeptCode\":\"023\",\"sourceTeamCode\":\"\",\"destCityCode\":\"052\",\"destDeptCode\":\"052GC\",\"destDeptCodeMapping\":\"\",\"destTeamCode\":\"004\",\"destTeamCodeMapping\":\"\",\"destTransferCode\":\"052\",\"destRouteLabel\":\"052GC-004\",\"proName\":\"\",\"cargoTypeCode\":\"C201\",\"limitTypeCode\":\"T4\",\"expressTypeCode\":\"B1\",\"codingMapping\":\"C15\",\"codingMappingOut\":\"\",\"xbFlag\":\"0\",\"printFlag\":\"000000000\",\"twoDimensionCode\":\"MMM={'k1':'052','k2':'052GC','k3':'004','k4':'T4','k5':'SF7444473317974','k6':'','k7':'1d8ee46c'}\",\"proCode\":\"特快\",\"printIcon\":\"00000000\",\"abFlag\":\"\",\"destPortCode\":\"\",\"destCountry\":\"\",\"destPostCode\":\"\",\"goodsValueTotal\":\"\",\"currencySymbol\":\"\",\"cusBatch\":\"\",\"goodsNumber\":\"\",\"errMsg\":\"\",\"checkCode\":\"1d8ee46c\",\"proIcon\":\"\",\"fileIcon\":\"\",\"fbaIcon\":\"\",\"icsmIcon\":\"\",\"destGisDeptCode\":\"052GC\",\"newIcon\":null,\"sendAreaCode\":null,\"destinationStationCode\":null,\"sxLabelDestCode\":null,\"sxDestTransferCode\":null,\"sxCompany\":null,\"newAbFlag\":null,\"destAddrKeyWord\":\"\",\"rongType\":null,\"waybillIconList\":null},\"message\":\"SF7444473317974:\"}],\"contactInfoList\":null,\"sendStartTm\":null,\"customerRights\":null,\"expressTypeId\":null}}"
}

取消订单响应报文

java 复制代码
{
    "apiErrorMsg":"",
    "apiResponseID":"00018BB37FFC733FEB93CB6399A4D03F",
    "apiResultCode":"A1000",
    "apiResultData":"{\"success\":true,\"errorCode\":\"S0000\",\"errorMsg\":null,\"msgData\":{\"orderId\":\"SC1714182733078591144\",\"waybillNoInfoList\":[{\"waybillType\":1,\"waybillNo\":\"SF7444473317974\"}],\"resStatus\":2,\"extraInfoList\":null}}"
}
相关推荐
Viktor_Ye16 分钟前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
hummhumm18 分钟前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
一二小选手22 分钟前
【Maven】IDEA创建Maven项目 Maven配置
java·maven
J老熊28 分钟前
JavaFX:简介、使用场景、常见问题及对比其他框架分析
java·开发语言·后端·面试·系统架构·软件工程
猿java33 分钟前
什么是 Hystrix?它的工作原理是什么?
java·微服务·面试
AuroraI'ncoding34 分钟前
时间请求参数、响应
java·后端·spring
所待.3831 小时前
JavaEE之线程初阶(上)
java·java-ee
Winston Wood1 小时前
Java线程池详解
java·线程池·多线程·性能
手握风云-1 小时前
数据结构(Java版)第二期:包装类和泛型
java·开发语言·数据结构
喵叔哟1 小时前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构