fix(优惠券中心):携程订单接口对接第一版完成对接,新增加密请求体方法

This commit is contained in:
2025-08-14 20:17:29 +08:00
parent 31c62956e8
commit 993fcc2fa4
13 changed files with 172 additions and 15 deletions
@@ -1166,4 +1166,84 @@ public class CtripCouponController {
return Result.succeed(result);
}
}
/**
* 测试接口 - 生成携程核销优惠券请求的加密body
*/
@Operation(summary = "测试接口 - 生成携程核销优惠券请求的加密body")
@PostMapping("/test/generate-verify-request")
public Result<Map<String, Object>> generateVerifyRequest(@RequestBody Map<String, Object> request) {
log.info("收到生成携程核销优惠券请求体请求");
// 验证请求体不为空
if (request == null || request.isEmpty()) {
log.error("请求体为空");
return Result.succeed(Map.of("success", false, "error", "请求体不能为空"));
}
try {
// 解析请求参数
String ctripCouponId = (String) request.get("ctripCouponId");
String orderNo = (String) request.get("orderNo");
if (ctripCouponId == null || orderNo == null) {
log.error("参数不完整 - ctripCouponId: {}, orderNo: {}", ctripCouponId, orderNo);
return Result.succeed(Map.of("success", false, "error", "参数不完整"));
}
log.info("开始生成携程核销优惠券请求体,ctripCouponId: {}, orderNo: {}", ctripCouponId, orderNo);
// 构建核销请求的body - 按照携程核销优惠券接口规范
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("userPhone", "13800138000"); // 默认用户手机号
requestBody.put("orderNo", orderNo);
requestBody.put("orderAmount", 100.00); // 默认订单金额
// 构建完整的请求(包含header和body)
Map<String, Object> fullRequest = new HashMap<>();
// 构建header
Map<String, Object> header = new HashMap<>();
header.put("accountId", "c50a8f2a4a59c9e7");
header.put("serviceName", "VerifyCoupon");
header.put("requestTime", java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
header.put("version", "1.0");
// 生成签名
String bodyJson = objectMapper.writeValueAsString(requestBody);
String sign = ctripDecryptUtil.generateSign("c50a8f2a4a59c9e7", "VerifyCoupon",
header.get("requestTime").toString(), bodyJson, "1.0");
header.put("sign", sign);
// 使用AES加密body
String encryptedBody = ctripDecryptUtil.encryptAES(bodyJson);
fullRequest.put("header", header);
fullRequest.put("body", encryptedBody); // 加密后的body
// 生成JSON字符串
String requestJson = objectMapper.writeValueAsString(fullRequest);
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("url", "/coupon/ctrip/verify");
result.put("method", "POST");
result.put("headers", Map.of("Content-Type", "application/json"));
result.put("originalBody", requestBody);
result.put("encryptedBody", encryptedBody);
result.put("fullRequest", fullRequest);
result.put("requestJson", requestJson);
result.put("message", "携程核销优惠券请求体生成成功");
log.info("携程核销优惠券请求体生成成功: {}", result);
return Result.succeed(result);
} catch (Exception e) {
log.error("生成携程核销优惠券请求体失败", e);
Map<String, Object> result = new HashMap<>();
result.put("success", false);
result.put("error", e.getMessage());
return Result.succeed(result);
}
}
}
@@ -46,8 +46,8 @@ logging:
ctrip:
# AES加密配置
aes:
key: 087b36ad0dff7942 # 请替换为实际的AES密钥
iv: 750e15a31356a99d # 请替换为实际的AES IV
key: 087b36ad0dff7942 # 为实际的AES密钥
iv: 750e15a31356a99d # 为实际的AES IV
# 签名配置(使用账户密钥进行签名计算)
# API配置
api:
@@ -56,5 +56,5 @@ ctrip:
retry-times: 3 # 重试次数
# 账户配置
account:
id: c50a8f2a4a59c9e7 # 请替换为实际的账户ID
secret: ce6033ae31124ec5cdab3d2bc79f2622 # 请替换为实际的账户密钥
id: c50a8f2a4a59c9e7 # 为实际的账户ID
secret: ce6033ae31124ec5cdab3d2bc79f2622 # 实际的账户密钥