RtMallHttpClient.java
2.98 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.diligrp.cashier.mall.client;
import com.diligrp.cashier.mall.domain.rtmall.vo.OrderDetailResponseVO;
import com.diligrp.cashier.mall.exception.RtMartMallException;
import com.diligrp.cashier.mall.util.HttpClientUtils;
import com.diligrp.cashier.mall.util.RtMallSignMd5Utils;
import com.diligrp.cashier.mall.type.RtMarkErrorCode;
import com.diligrp.cashier.shared.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* 大润发HTTP客户端
* 调用大润发订单详情接口
*/
public class RtMallHttpClient {
private static final Logger log = LoggerFactory.getLogger(RtMallHttpClient.class);
private final String apiUrl;
private final String appKey;
private final String appSecret;
public RtMallHttpClient(String apiUrl, String appKey, String appSecret) {
this.apiUrl = apiUrl;
this.appKey = appKey;
this.appSecret = appSecret;
}
/**
* 查询订单详情
*
* @param orderId 订单号
* @param tradeId 支付流水号
* @param userCode 会员编号
* @return 订单详情
*/
public OrderDetailResponseVO getOrderDetail(String orderId, String tradeId, String userCode) {
log.info("调用大润发订单详情接口: order_id={}, trade_id={}, user_code={}", orderId, tradeId, userCode);
//构建请求参数
Map<String, Object> params = new HashMap<>();
params.put("app_key", appKey);
params.put("version", "v1");
params.put("timestamp", System.currentTimeMillis() / 1000);
params.put("method", "scanbuy.order.detail");
params.put("user_code", userCode);
params.put("order_id", orderId);
params.put("trade_id", tradeId);
//生成签名
String sign = RtMallSignMd5Utils.generateSign(params, appSecret);
params.put("sign", sign);
//发送HTTP请求
String responseBody = HttpClientUtils.postJson(apiUrl, params, null, "大润发订单详情");
//解析响应
OrderDetailResponseVO detailResponse = JsonUtils.fromJsonString(responseBody, OrderDetailResponseVO.class);
if (detailResponse == null) {
log.error("解析大润发响应失败");
throw new RtMartMallException(RtMarkErrorCode.E4004);
}
//检查响应状态
if (!"success".equals(detailResponse.getResult()) || !"E0000".equals(detailResponse.getCode())) {
log.error("调用大润发订单详情接口失败: code={}, msg={}",
detailResponse.getCode(), detailResponse.getMsg());
throw new RtMartMallException(detailResponse.getCode(), detailResponse.getMsg());
}
log.info("订单详情查询成功: order_id={}, item_count={}", orderId,
detailResponse.getData() != null && detailResponse.getData().getItemList() != null
? detailResponse.getData().getItemList().size() : 0);
return detailResponse;
}
}