确认订单(orderConfirm接口)

准备

该接口的返回类BookOrderResult封装了返回的结果

1
2
3
4
5
6
public class BookOrderResult extends AbstractResult {
private String id;
private String skuId;
private String goodsId;
private List<Item> itemList;
}
1
2
3
4
5
6
7
8
9
10
public class Item {
//商品skuId
private String skuId;
//商品goodsId
private String goodsId;
//响应码
private int recCode;
//备注
private String recMsg;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 订单的用户信息
public class UserInfo {
//第三方账号
private String accountId;
//收货人姓名
private String name;
//手机号码
private String mobile;
//省份名称 如 浙江省
private String provinceName;
//城市名称 如 杭州市
private String cityName;
//县(区)名称 如 桐庐县 或者 滨江区
private String districtName;
//街道地址
private String address;
//实名信息
private String identityId;
//身份证正面
private String identityPicFront;
//身份证反面
private String identityPicBack;
//订单实付金额
private BigDecimal realPrice;
//邮费
private BigDecimal expFee;
//支付方式
private String payMethod;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 订单商品信息
public class OrderItem {
//唯一标识
private String recordId;
//商品goodsId
private String goodsId;
//商品skuId
private String skuId;
//购买数量
private Integer buyAmount;
//价格
private BigDecimal channelSalePrice;
//商品名称
private String productName;
//金额小计
private BigDecimal subtotalAmount;

//第三方订单
private String orderId;
//供应商
private String supplyChannel;
//收货人姓名
private String recName;
//手机号码
private String mobile;
//省份名称 如 浙江省
private String provinceName;
//城市名称 如 杭州市
private String cityName;
//县(区)名称 如 桐庐县 或者 滨江区
private String districtName;
//街道地址
private String address;
//商品规格
private String attributeName;
//运费
private BigDecimal freight;
}

步骤

参数:供应渠道supplyChannel

  • 验证参数
  • 判断供应商,如果是严选或者考拉的供应渠道,那么执行各自对应的订单确认操作流程,否则报错supplyChannel参数错误

考拉订单确认

参数:用户信息userInfo

  • 将用户信息userInfo的JSON格式转为对象UserInfo userInfo = JSON.parseObject(userInfoString, UserInfo.class);
  • 将订单商品的信息orderItemList的JSON格式转为对象List<OrderItem> orderItemList = JSON.parseArray(map.get("orderItemList"), OrderItem.class);
  • 判断orderItemList的size,大于1报错,考拉商品只允许单商品下单
  • 判断用户余额(当金钱需要进行计算的时候,使用BigDecimal类),如果订单总金额大于余额,setRecMsg为余额不足请充值
  • 将OrderItem值和userInfo封装到考拉对应的参数中去(OrderItem-->OrderConfirm.Arg.OrderItemUserInfo--> OrderConfirm.Arg.UserInfo

List<OrderConfirm.Arg.OrderItem> orderItems = orderItemList.stream().map(orderItem -> TransitionUtil.getOrderConfirmArgOrderItem(orderItem)).collect(toList());

  • 调用考拉的确认订单接口OrderConfirm.Result orderConfirmresult = orderConfirm.request(orderConfirmArgUserInfo, orderItems);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 考拉的接口名称
private static final String URL_APPEND = "orderConfirm";
public Result request(Arg.UserInfo userInfo, List<Arg.OrderItem> items) {
Arg arg = new Arg();
arg.setSource(kaola.getChannelId());
arg.setV(kaola.getVersion());
arg.setApp_key(kaola.getAppkey());
arg.setSign_method(kaola.getSignMethod());

arg.setUserInfo(userInfo);
arg.setOrderItemList(items);
arg.sign(kaola);
String url = kaola.getUrl() + URL_APPEND;
System.out.println(url);
// Api请求接口
ResResult resResult = PostReq.request(arg.toMap(), url, "utf-8");
// 是否需要将响应信息转为内部类形式
if (true == parse && resResult.getRecCode() == SUCCESS) {
Result result = JSON.parseObject(resResult.getRecMsg(), Result.class);
return result;
}else{
Result result=new Result();
result.setRecCode(resResult.getRecCode());
result.setRecMeg(resResult.getRecMeg());
result.setRecMsg(resResult.getRecMsg());
return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Api请求接口
public interface PostReq {

static ResResult request(Map<String, String> args, String url, String charset) {
ResResult resResult = new ResResult() {
};
try {
HttpPost httpRequst = new HttpPost(url);
System.err.println(url);
List params = new ArrayList();
args.entrySet().stream().forEach(e -> params.add(new BasicNameValuePair(e.getKey(), e.getValue())));
System.err.println(JSON.toJSONString(params));
httpRequst.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpclient.execute(httpRequst);
BufferedReader buffReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), charset));
StringBuffer strBuff = new StringBuffer();
String temp = null;
while ((temp = buffReader.readLine()) != null) {
strBuff.append(temp);
}
resResult.setRecCode(httpResponse.getStatusLine().getStatusCode());
resResult.setRecMsg(strBuff.toString());
System.err.println("请求返回结果" + strBuff.toString());
} catch (Throwable t) {
resResult.setRecCode(9999);
resResult.setRecMsg("exception occured !");
}
return resResult;
}
}
  • 判断状态设置状态码
赏个🍗吧
0%