提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
API需要添加到APP中,创建APP在上一篇文章
提示:这里可以添加本文要记录的大概内容:
Parcel DE Returns (Post & Parcel Germany)
生产API地址:https://api-eu.dhl.com/parcel/de/shipping/returns/v1/
沙箱API地址:https://api-sandbox.dhl.com/parcel/de/shipping/returns/v1/
要使用两个接口:/orders,/locations
提示:以下是本篇文章正文内容,下面案例可供参考
一、需要在DHL卖家账号后台配置好receiverId
调用创建退货标签接口需要拿到DHL卖家账号配置信息。每个国家对应的receiverId
DHL卖家登录地址:https://geschaeftskunden.dhl.de/content/vls/gw/vlsweb/ShipmentOverview
进入settings页面查看配置的信息。
往下滑动可以看到。
可以看到这里有receiverId。
二、调用/locations接口获取配置信息
获取对应国家的receiverId
该接口为GET请求。
请求参数都为选填:
这里介绍使用countryCode查询。需使用国家三字简码。
String url = SAND_BOX_LOCATION_URL + "?countryCode=" + isoCode3;
String token = redisUtils.get(DHL_TOKEN_KEY);
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
.url(url)
.method("GET", null)
.addHeader("Authorization", String.format("Bearer %s",token))
.build();
Response response = client.newCall(request).execute();
if(response.code() == 401){
System.out.println("授权信息错误或token失效!");
}
String string = response.body().string();
响应体:一个对象数组,receiverId就是我们需要的字段。
[{
"receiverId": "deu",
"shipperCountry": "bel",
"ekp": "2174247313",
"billingNumber": "22222222220701",
"companyName": "DHL Paket GmbH",
"numberRange": "99999XXXXXXC",
"receiverAddress": {
"name1": "string",
"name2": "string",
"name3": "string",
"addressStreet": "string",
"addressHouse": "string",
"city": "Berlin",
"email": "max@mustermann.de",
"phone": "+49 123456789",
"postalCode": "12345",
"state": "NRW"
}]
二、调用/orders创建退货标签
接口为Post请求。接口参数查看原API文档,这里只获取b64的标签。https://developer.dhl.com/api-reference/dhl-parcel-de-returns-post-parcel-germany#get-started-section/
请求参数:
@Data
public class DHLPostReturnsRequest implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String receiverId;
private Shipper shipper = new Shipper();
/**
* 自定义单号
*/
private String customerReference;
/**
* 自定义单号
*/
private String shipmentReference;
private ItemWeight itemWeight;
private ItemValue itemValue;
private CustomsDetails customsDetails;
@Data
public static class Shipper implements Serializable{
/**
* 姓名
*/
private String name1;
/**
* 姓名2
*/
private String name2;
/**
* 姓名3
*/
private String name3;
/**
* 地址
*/
private String addressStreet;
/**
* 门牌号
*/
private String addressHouse;
/**
* 邮编
*/
private String postalCode;
/**
* 城市
*/
private String city;
/**
* 省/洲
*/
private String state;
/**
* 邮箱
*/
private String email;
/**
* 电话
*/
private String phone;
private ItemWeight itemWeight;
private ItemValue ItemValue;
}
@Data
public static class ItemWeight implements Serializable{
/**
* 重量单位
*/
private String uom;
/**
* 重量
*/
private Double value;
}
@Data
public static class ItemValue implements Serializable{
/**
* 币种
*/
private String currency;
/**
* 货值
*/
private BigDecimal value;
}
@Data
public static class Item implements Serializable{
/**
* 描述
*/
private String itemDescription;
/**
* 数量
*/
private Integer packagedQuantity;
private ItemWeight itemWeight = new ItemWeight();
private ItemValue ItemValue = new ItemValue();
}
@Data
public static class CustomsDetails implements Serializable{
private List<Item> Items = new ArrayList<>();
}
响应数据
@Data
public class DHLPostReturnsResponse implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ResultVo sstatus;
private String shipmentNo;
private String internationalShipmentNo;
private Lable label;
private String routingCode;
@Data
public static class ResultVo implements Serializable{
private String title;
private String status;
private String detail;
}
@Data
public static class Lable implements Serializable{
private String b64;
}
}
接口使用:
@Override
public DHLPostReturnsResponse returnPostOrder(DHLPostReturnsRequest dhlPostReturnsRequest) throws Exception {
String url = SAND_BOX_POST_DHL_URL;
String token = redisUtils.get(DHL_TOKEN_KEY);
OkHttpClient client = new OkHttpClient().newBuilder().build();
okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString (dhlPostReturnsRequest));
Request request = new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Authorization", String.format("Bearer %s",token))
.build();
Response response = client.newCall(request).execute();
return JSONObject.parseObject(response.body().string(), DHLPostReturnsResponse.class);
}
最后将响应中的b64,base64加密的编码进行解码,
byte[] buffer = Base64.getDecoder().decode(dhlPostReturnsResponse.getLabel().getB64());
转换成字节流,再转换成文件,就是最终获取的退货标签了。
总结
具体如何操作看自身业务功能。