fix(优惠券中心):携程订单接口对接第一版完成对接
This commit is contained in:
+207
-31
@@ -2,6 +2,7 @@ package com.central.coupon.controller;
|
||||
|
||||
import com.central.common.model.Result;
|
||||
import com.central.coupon.entity.Coupon;
|
||||
import com.central.coupon.model.CtripCouponResponse;
|
||||
import com.central.coupon.service.ICouponService;
|
||||
import com.central.coupon.service.ICtripApiService;
|
||||
import com.central.coupon.service.impl.CouponServiceImpl;
|
||||
@@ -66,7 +67,7 @@ public class CtripCouponController {
|
||||
*/
|
||||
@Operation(summary = "携程创建优惠券接口")
|
||||
@PostMapping("/create")
|
||||
public Result<Boolean> createCouponFromCtrip(@RequestBody Map<String, Object> request,
|
||||
public CtripCouponResponse createCouponFromCtrip(@RequestBody Map<String, Object> request,
|
||||
jakarta.servlet.http.HttpServletRequest httpRequest) {
|
||||
log.info("收到携程创建优惠券请求,IP: {}", httpRequest.getRemoteAddr());
|
||||
|
||||
@@ -78,7 +79,7 @@ public class CtripCouponController {
|
||||
|
||||
if (header == null || bodyObj == null) {
|
||||
log.error("请求格式错误,缺少header或body");
|
||||
return Result.succeed(false);
|
||||
return buildErrorResponse("0002", "请求格式错误,缺少header或body");
|
||||
}
|
||||
|
||||
// 处理body,可能是字符串或对象
|
||||
@@ -105,7 +106,7 @@ public class CtripCouponController {
|
||||
log.info("body直接解析JSON成功");
|
||||
} catch (Exception e) {
|
||||
log.error("body既不是有效的加密数据也不是有效的JSON: {}", e.getMessage());
|
||||
return Result.succeed(false);
|
||||
return buildErrorResponse("0001", "报文解析失败");
|
||||
}
|
||||
}
|
||||
} else if (bodyObj instanceof Map) {
|
||||
@@ -115,7 +116,7 @@ public class CtripCouponController {
|
||||
log.info("body是对象类型");
|
||||
} else {
|
||||
log.error("body类型不支持: {}", bodyObj.getClass().getName());
|
||||
return Result.succeed(false);
|
||||
return buildErrorResponse("0001", "报文解析失败");
|
||||
}
|
||||
|
||||
// 验证签名
|
||||
@@ -128,6 +129,12 @@ public class CtripCouponController {
|
||||
if (!ctripDecryptUtil.verifySign(accountId, serviceName, requestTime, bodyJson, version, sign)) {
|
||||
log.error("签名验证失败,但继续处理业务逻辑");
|
||||
log.error("期望签名: {}, 计算签名: {}", sign, ctripDecryptUtil.generateSign(accountId, serviceName, requestTime, bodyJson, version));
|
||||
|
||||
// 调用详细调试方法
|
||||
Map<String, Object> debugInfo = ctripDecryptUtil.debugSignCalculation(
|
||||
accountId, serviceName, requestTime, bodyJson, version, sign);
|
||||
log.error("签名调试信息: {}", debugInfo);
|
||||
|
||||
// 临时跳过签名验证,继续处理业务逻辑
|
||||
// return Result.succeed(false);
|
||||
}
|
||||
@@ -165,24 +172,19 @@ public class CtripCouponController {
|
||||
// 参数验证
|
||||
if (otaOrderId == null || userPhone == null) {
|
||||
log.error("参数不完整 - otaOrderId: {}, userPhone: {}", otaOrderId, userPhone);
|
||||
return Result.succeed(false);
|
||||
return buildErrorResponse("1005", "信息缺失:订单ID或用户手机号");
|
||||
}
|
||||
|
||||
// 生成携程优惠券ID(使用订单ID作为唯一标识)
|
||||
String ctripCouponId = "CTRIP_" + otaOrderId;
|
||||
|
||||
// 幂等校验
|
||||
if (couponService.existsByUserPhoneAndCtripCouponId(userPhone, ctripCouponId)) {
|
||||
log.info("携程优惠券已存在,userPhone: {}, ctripCouponId: {}", userPhone, ctripCouponId);
|
||||
return Result.succeed(true);
|
||||
}
|
||||
// 生成供应商订单ID(使用时间戳和订单ID生成唯一标识)
|
||||
String supplierOrderId = "SUP_" + System.currentTimeMillis() + "_" + otaOrderId;
|
||||
|
||||
// 创建优惠券(使用默认值)
|
||||
String couponName = "携程订单优惠券";
|
||||
Integer couponType = 1; // 满减券
|
||||
String couponType = "DISCOUNT"; // 优惠券类型
|
||||
BigDecimal amount = new BigDecimal("50.00"); // 默认50元优惠券
|
||||
BigDecimal minAmount = new BigDecimal("100.00"); // 满100可用
|
||||
Integer discountRate = null;
|
||||
|
||||
// 设置有效期(从当前时间开始,30天有效期)
|
||||
Calendar cal = Calendar.getInstance();
|
||||
@@ -190,24 +192,30 @@ public class CtripCouponController {
|
||||
cal.add(Calendar.DAY_OF_MONTH, 30);
|
||||
Date validEndTime = cal.getTime();
|
||||
|
||||
// 幂等校验
|
||||
if (couponService.existsByUserPhoneAndCtripCouponId(userPhone, ctripCouponId)) {
|
||||
log.info("携程优惠券已存在,userPhone: {}, ctripCouponId: {}", userPhone, ctripCouponId);
|
||||
return buildSuccessResponse(otaOrderId, supplierOrderId, ctripCouponId, userPhone, couponName, couponType, amount, validStartTime, validEndTime);
|
||||
}
|
||||
|
||||
// 创建优惠券
|
||||
boolean success = couponService.createCouponFromCtrip(
|
||||
ctripCouponId, userPhone, couponName, couponType, amount,
|
||||
minAmount, discountRate, validStartTime, validEndTime
|
||||
validStartTime, validEndTime
|
||||
);
|
||||
|
||||
if (success) {
|
||||
log.info("携程优惠券创建成功,userPhone: {}, ctripCouponId: {}, otaOrderId: {}",
|
||||
userPhone, ctripCouponId, otaOrderId);
|
||||
return Result.succeed(true);
|
||||
log.info("携程优惠券创建成功,userPhone: {}, ctripCouponId: {}, otaOrderId: {}, supplierOrderId: {}",
|
||||
userPhone, ctripCouponId, otaOrderId, supplierOrderId);
|
||||
return buildSuccessResponse(otaOrderId, supplierOrderId, ctripCouponId, userPhone, couponName, couponType, amount, validStartTime, validEndTime);
|
||||
} else {
|
||||
log.error("携程优惠券创建失败,userPhone: {}, ctripCouponId: {}", userPhone, ctripCouponId);
|
||||
return Result.succeed(false);
|
||||
return buildErrorResponse("1100", "优惠券创建失败");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理携程创建优惠券请求异常", e);
|
||||
return Result.succeed(false);
|
||||
return buildErrorResponse("1100", "系统异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,6 +303,12 @@ public class CtripCouponController {
|
||||
if (!ctripDecryptUtil.verifySign(accountId, serviceName, requestTime, bodyJson, version, sign)) {
|
||||
log.error("签名验证失败,但继续处理业务逻辑");
|
||||
log.error("期望签名: {}, 计算签名: {}", sign, ctripDecryptUtil.generateSign(accountId, serviceName, requestTime, bodyJson, version));
|
||||
|
||||
// 调用详细调试方法
|
||||
Map<String, Object> debugInfo = ctripDecryptUtil.debugSignCalculation(
|
||||
accountId, serviceName, requestTime, bodyJson, version, sign);
|
||||
log.error("签名调试信息: {}", debugInfo);
|
||||
|
||||
// 临时跳过签名验证,继续处理业务逻辑
|
||||
// return Result.succeed(false);
|
||||
}
|
||||
@@ -421,6 +435,12 @@ public class CtripCouponController {
|
||||
if (!ctripDecryptUtil.verifySign(accountId, serviceName, requestTime, bodyJson, version, sign)) {
|
||||
log.error("签名验证失败,但继续处理业务逻辑");
|
||||
log.error("期望签名: {}, 计算签名: {}", sign, ctripDecryptUtil.generateSign(accountId, serviceName, requestTime, bodyJson, version));
|
||||
|
||||
// 调用详细调试方法
|
||||
Map<String, Object> debugInfo = ctripDecryptUtil.debugSignCalculation(
|
||||
accountId, serviceName, requestTime, bodyJson, version, sign);
|
||||
log.error("签名调试信息: {}", debugInfo);
|
||||
|
||||
// 临时跳过签名验证,继续处理业务逻辑
|
||||
// return Result.succeed(false);
|
||||
}
|
||||
@@ -526,16 +546,103 @@ public class CtripCouponController {
|
||||
/**
|
||||
* 获取状态文本
|
||||
*/
|
||||
private String getStatusText(Integer status) {
|
||||
private String getStatusText(String status) {
|
||||
if (status == null) {
|
||||
return "未知状态";
|
||||
}
|
||||
switch (status) {
|
||||
case 0: return "未使用";
|
||||
case 1: return "已使用";
|
||||
case 2: return "已过期";
|
||||
case 3: return "已作废";
|
||||
case "UNUSED": return "未使用";
|
||||
case "USED": return "已使用";
|
||||
case "EXPIRED": return "已过期";
|
||||
case "VOIDED": return "已作废";
|
||||
default: return "未知状态";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建成功响应
|
||||
*/
|
||||
private CtripCouponResponse buildSuccessResponse(String otaOrderId, String supplierOrderId, String ctripCouponId, String userPhone,
|
||||
String couponName, String couponType,
|
||||
BigDecimal amount, Date validStartTime, Date validEndTime) {
|
||||
// 构建body对象
|
||||
CtripCouponResponse.CtripCouponResponseBody body = new CtripCouponResponse.CtripCouponResponseBody();
|
||||
body.setOtaOrderId(otaOrderId);
|
||||
body.setSupplierOrderId(supplierOrderId);
|
||||
body.setCtripCouponId(ctripCouponId);
|
||||
body.setUserPhone(userPhone);
|
||||
body.setCouponName(couponName);
|
||||
body.setCouponType(couponType);
|
||||
body.setAmount(amount);
|
||||
body.setStatus("UNUSED");
|
||||
body.setValidStartTime(validStartTime);
|
||||
body.setValidEndTime(validEndTime);
|
||||
|
||||
// 将body对象转换为JSON字符串
|
||||
try {
|
||||
String bodyJson = objectMapper.writeValueAsString(body);
|
||||
log.info("构建响应body JSON: {}", bodyJson);
|
||||
|
||||
// 检查AES配置
|
||||
log.info("AES密钥长度: {}", ctripDecryptUtil.getAesKey() != null ? ctripDecryptUtil.getAesKey().length() : 0);
|
||||
log.info("AES IV长度: {}", ctripDecryptUtil.getAesIv() != null ? ctripDecryptUtil.getAesIv().length() : 0);
|
||||
|
||||
// 使用AES加密body
|
||||
String encryptedBody = ctripDecryptUtil.encryptAES(bodyJson);
|
||||
log.info("AES加密结果: {}", encryptedBody != null ? "成功" : "失败");
|
||||
|
||||
if (encryptedBody != null) {
|
||||
// 创建一个新的响应对象,body字段为加密字符串
|
||||
CtripCouponResponse encryptedResponse = new CtripCouponResponse();
|
||||
|
||||
// 设置header
|
||||
CtripCouponResponse.CtripResponseHeader header = new CtripCouponResponse.CtripResponseHeader();
|
||||
header.setResultCode("0000");
|
||||
header.setResultMessage("操作成功");
|
||||
encryptedResponse.setHeader(header);
|
||||
|
||||
// 设置加密的body
|
||||
encryptedResponse.setBody(encryptedBody);
|
||||
|
||||
log.info("AES加密body成功,加密后长度: {}", encryptedBody.length());
|
||||
log.info("加密后的body内容: {}", encryptedBody);
|
||||
|
||||
// 验证加密是否成功
|
||||
String decryptedTest = ctripDecryptUtil.decryptAES(encryptedBody);
|
||||
if (decryptedTest != null) {
|
||||
log.info("加密验证成功,解密后内容: {}", decryptedTest);
|
||||
log.info("数据匹配: {}", bodyJson.equals(decryptedTest));
|
||||
} else {
|
||||
log.error("加密验证失败,无法解密");
|
||||
}
|
||||
|
||||
return encryptedResponse;
|
||||
} else {
|
||||
log.error("AES加密body失败");
|
||||
// 如果加密失败,返回错误响应
|
||||
return buildErrorResponse("1100", "响应加密失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("构建响应body失败: {}", e.getMessage(), e);
|
||||
return buildErrorResponse("1100", "响应构建失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建错误响应
|
||||
*/
|
||||
private CtripCouponResponse buildErrorResponse(String resultCode, String resultMessage) {
|
||||
CtripCouponResponse response = new CtripCouponResponse();
|
||||
|
||||
// 设置header
|
||||
CtripCouponResponse.CtripResponseHeader header = new CtripCouponResponse.CtripResponseHeader();
|
||||
header.setResultCode(resultCode);
|
||||
header.setResultMessage(resultMessage);
|
||||
response.setHeader(header);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试接口 - 明文创建优惠券(仅用于开发测试)
|
||||
*/
|
||||
@@ -549,16 +656,11 @@ public class CtripCouponController {
|
||||
String ctripCouponId = (String) request.get("ctripCouponId");
|
||||
String userPhone = (String) request.get("userPhone");
|
||||
String couponName = (String) request.get("couponName");
|
||||
Integer couponType = (Integer) request.get("couponType");
|
||||
String couponType = (String) request.get("couponType");
|
||||
BigDecimal amount = null;
|
||||
if (request.get("amount") != null) {
|
||||
amount = new BigDecimal(request.get("amount").toString());
|
||||
}
|
||||
BigDecimal minAmount = null;
|
||||
if (request.get("minAmount") != null) {
|
||||
minAmount = new BigDecimal(request.get("minAmount").toString());
|
||||
}
|
||||
Integer discountRate = (Integer) request.get("discountRate");
|
||||
String validStartTimeStr = (String) request.get("validStartTime");
|
||||
String validEndTimeStr = (String) request.get("validEndTime");
|
||||
|
||||
@@ -588,7 +690,7 @@ public class CtripCouponController {
|
||||
// 创建优惠券
|
||||
boolean success = couponService.createCouponFromCtrip(
|
||||
ctripCouponId, userPhone, couponName, couponType, amount,
|
||||
minAmount, discountRate, validStartTime, validEndTime
|
||||
validStartTime, validEndTime
|
||||
);
|
||||
|
||||
if (success) {
|
||||
@@ -727,6 +829,31 @@ public class CtripCouponController {
|
||||
|
||||
try {
|
||||
Map<String, Object> result = ctripDecryptUtil.validateAESConfig();
|
||||
|
||||
// 添加加密解密测试
|
||||
String testData = "{\"test\":\"data\"}";
|
||||
String encrypted = ctripDecryptUtil.encryptAES(testData);
|
||||
String decrypted = ctripDecryptUtil.decryptAES(encrypted);
|
||||
|
||||
result.put("testData", testData);
|
||||
result.put("encrypted", encrypted);
|
||||
result.put("decrypted", decrypted);
|
||||
result.put("encryptionSuccess", encrypted != null);
|
||||
result.put("decryptionSuccess", decrypted != null);
|
||||
result.put("dataMatch", testData.equals(decrypted));
|
||||
|
||||
// 测试实际的优惠券响应数据
|
||||
String couponResponseJson = "{\"ctripCouponId\":\"CTRIP_test\",\"userPhone\":\"13800138000\",\"couponName\":\"测试优惠券\",\"couponType\":\"DISCOUNT\",\"amount\":50.00,\"status\":\"UNUSED\",\"validStartTime\":\"2025-08-07T07:00:21.303+00:00\",\"validEndTime\":\"2025-09-06T07:00:21.303+00:00\"}";
|
||||
String encryptedCoupon = ctripDecryptUtil.encryptAES(couponResponseJson);
|
||||
String decryptedCoupon = ctripDecryptUtil.decryptAES(encryptedCoupon);
|
||||
|
||||
result.put("couponResponseJson", couponResponseJson);
|
||||
result.put("encryptedCoupon", encryptedCoupon);
|
||||
result.put("decryptedCoupon", decryptedCoupon);
|
||||
result.put("couponEncryptionSuccess", encryptedCoupon != null);
|
||||
result.put("couponDecryptionSuccess", decryptedCoupon != null);
|
||||
result.put("couponDataMatch", couponResponseJson.equals(decryptedCoupon));
|
||||
|
||||
return Result.succeed(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -801,4 +928,53 @@ public class CtripCouponController {
|
||||
return Result.succeed(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试接口 - 签名调试
|
||||
*/
|
||||
@Operation(summary = "测试接口 - 签名调试")
|
||||
@PostMapping("/test/debug-sign")
|
||||
public Result<Map<String, Object>> debugSign(@RequestBody Map<String, Object> request) {
|
||||
log.info("收到签名调试请求");
|
||||
|
||||
try {
|
||||
// 解析请求格式
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> header = (Map<String, Object>) request.get("header");
|
||||
Object bodyObj = request.get("body");
|
||||
|
||||
if (header == null) {
|
||||
return Result.succeed(null);
|
||||
}
|
||||
|
||||
String accountId = (String) header.get("accountId");
|
||||
String serviceName = (String) header.get("serviceName");
|
||||
String requestTime = (String) header.get("requestTime");
|
||||
String version = (String) header.get("version");
|
||||
String sign = (String) header.get("sign");
|
||||
|
||||
String bodyJson = null;
|
||||
if (bodyObj instanceof String) {
|
||||
String bodyStr = (String) bodyObj;
|
||||
String decryptedBody = ctripDecryptUtil.decryptAES(bodyStr);
|
||||
if (decryptedBody != null) {
|
||||
bodyJson = decryptedBody;
|
||||
} else {
|
||||
bodyJson = bodyStr;
|
||||
}
|
||||
} else if (bodyObj instanceof Map) {
|
||||
bodyJson = objectMapper.writeValueAsString(bodyObj);
|
||||
}
|
||||
|
||||
// 调用调试方法
|
||||
Map<String, Object> debugInfo = ctripDecryptUtil.debugSignCalculation(
|
||||
accountId, serviceName, requestTime, bodyJson, version, sign);
|
||||
|
||||
return Result.succeed(debugInfo);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("签名调试失败", e);
|
||||
return Result.succeed(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,9 +30,9 @@ public class Coupon implements Serializable {
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 优惠券编号
|
||||
* 租户ID
|
||||
*/
|
||||
private String couponNo;
|
||||
private String tenantId;
|
||||
|
||||
/**
|
||||
* 优惠券名称
|
||||
@@ -40,29 +40,19 @@ public class Coupon implements Serializable {
|
||||
private String couponName;
|
||||
|
||||
/**
|
||||
* 优惠券类型:1-满减券,2-折扣券,3-免费券
|
||||
* 优惠券类型
|
||||
*/
|
||||
private Integer couponType;
|
||||
private String couponType;
|
||||
|
||||
/**
|
||||
* 优惠券面额
|
||||
* 优惠券金额
|
||||
*/
|
||||
private BigDecimal amount;
|
||||
|
||||
/**
|
||||
* 使用门槛金额
|
||||
* 货币类型
|
||||
*/
|
||||
private BigDecimal minAmount;
|
||||
|
||||
/**
|
||||
* 折扣率(0-100,折扣券时使用)
|
||||
*/
|
||||
private Integer discountRate;
|
||||
|
||||
/**
|
||||
* 优惠券状态:0-未使用,1-已使用,2-已过期,3-已作废
|
||||
*/
|
||||
private Integer status;
|
||||
private String amountCurrency;
|
||||
|
||||
/**
|
||||
* 用户手机号
|
||||
@@ -70,20 +60,20 @@ public class Coupon implements Serializable {
|
||||
private String userPhone;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 来源:1-携程,2-高飞自建
|
||||
*/
|
||||
private Integer source;
|
||||
|
||||
/**
|
||||
* 携程优惠券ID(来源为携程时使用)
|
||||
* 携程优惠券ID
|
||||
*/
|
||||
private String ctripCouponId;
|
||||
|
||||
/**
|
||||
* 携程订单ID
|
||||
*/
|
||||
private String ctripOrderId;
|
||||
|
||||
/**
|
||||
* 状态:UNUSED-未使用,USED-已使用,EXPIRED-已过期,VOIDED-已作废
|
||||
*/
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 有效期开始时间
|
||||
*/
|
||||
@@ -105,9 +95,14 @@ public class Coupon implements Serializable {
|
||||
private String usedOrderNo;
|
||||
|
||||
/**
|
||||
* 退券时间
|
||||
* 备注
|
||||
*/
|
||||
private Date cancelTime;
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
@@ -115,6 +110,11 @@ public class Coupon implements Serializable {
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@@ -122,22 +122,7 @@ public class Coupon implements Serializable {
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
* 是否删除
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 租户ID
|
||||
*/
|
||||
private String tenantId;
|
||||
private Integer deleted;
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.central.coupon.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 携程优惠券接口响应
|
||||
* 供应商在收到携程的优惠券请求时的标准响应格式
|
||||
*/
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class CtripCouponResponse {
|
||||
|
||||
/**
|
||||
* 返回头
|
||||
*/
|
||||
@JsonProperty("header")
|
||||
private CtripResponseHeader header;
|
||||
|
||||
/**
|
||||
* 返回体(传输时该字段需要AES加密)
|
||||
* 该节点在返回码为0000时必填,其他均为非必填
|
||||
* 可以是CtripCouponResponseBody对象或加密后的字符串
|
||||
*/
|
||||
@JsonProperty("body")
|
||||
private Object body;
|
||||
|
||||
/**
|
||||
* 携程返回头
|
||||
*/
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class CtripResponseHeader {
|
||||
@JsonProperty("resultCode")
|
||||
private String resultCode; // 处理结果返回码
|
||||
@JsonProperty("resultMessage")
|
||||
private String resultMessage; // 处理结果返回信息
|
||||
}
|
||||
|
||||
/**
|
||||
* 携程优惠券返回体
|
||||
*/
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class CtripCouponResponseBody {
|
||||
@JsonProperty("otaOrderId")
|
||||
private String otaOrderId; // 携程订单ID
|
||||
@JsonProperty("supplierOrderId")
|
||||
private String supplierOrderId; // 供应商订单ID
|
||||
@JsonProperty("ctripCouponId")
|
||||
private String ctripCouponId; // 携程优惠券ID
|
||||
@JsonProperty("userPhone")
|
||||
private String userPhone; // 用户手机号
|
||||
@JsonProperty("couponName")
|
||||
private String couponName; // 优惠券名称
|
||||
@JsonProperty("couponType")
|
||||
private String couponType; // 优惠券类型
|
||||
@JsonProperty("amount")
|
||||
private java.math.BigDecimal amount; // 优惠券金额
|
||||
@JsonProperty("status")
|
||||
private String status; // 优惠券状态
|
||||
@JsonProperty("validStartTime")
|
||||
private java.util.Date validStartTime; // 有效期开始时间
|
||||
@JsonProperty("validEndTime")
|
||||
private java.util.Date validEndTime; // 有效期结束时间
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -58,8 +58,7 @@ public interface ICouponService extends IService<Coupon> {
|
||||
* 根据携程优惠券ID创建优惠券(携程接口调用)
|
||||
*/
|
||||
boolean createCouponFromCtrip(String ctripCouponId, String userPhone, String couponName,
|
||||
Integer couponType, java.math.BigDecimal amount,
|
||||
java.math.BigDecimal minAmount, Integer discountRate,
|
||||
String couponType, java.math.BigDecimal amount,
|
||||
java.util.Date validStartTime,
|
||||
java.util.Date validEndTime);
|
||||
|
||||
|
||||
+28
-55
@@ -44,13 +44,10 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
coupon.setUserPhone((String) params.get("userPhone"));
|
||||
}
|
||||
if (params.get("status") != null) {
|
||||
coupon.setStatus(Integer.valueOf(params.get("status").toString()));
|
||||
}
|
||||
if (params.get("source") != null) {
|
||||
coupon.setSource(Integer.valueOf(params.get("source").toString()));
|
||||
coupon.setStatus(params.get("status").toString());
|
||||
}
|
||||
if (params.get("couponType") != null) {
|
||||
coupon.setCouponType(Integer.valueOf(params.get("couponType").toString()));
|
||||
coupon.setCouponType(params.get("couponType").toString());
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
@@ -74,11 +71,10 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean createCoupon(Coupon coupon) {
|
||||
if (coupon.getCouponNo() == null) {
|
||||
coupon.setCouponNo(generateCouponNo());
|
||||
}
|
||||
coupon.setCreateTime(new Date());
|
||||
coupon.setUpdateTime(new Date());
|
||||
coupon.setTenantId("000000");
|
||||
coupon.setDeleted(0);
|
||||
return save(coupon);
|
||||
}
|
||||
|
||||
@@ -114,7 +110,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
return false;
|
||||
}
|
||||
|
||||
if (coupon.getStatus() != 0) {
|
||||
if (!"UNUSED".equals(coupon.getStatus())) {
|
||||
log.error("优惠券状态不正确,couponId: {}, status: {}", couponId, coupon.getStatus());
|
||||
return false;
|
||||
}
|
||||
@@ -130,13 +126,12 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean createCouponFromCtrip(String ctripCouponId, String userPhone, String couponName,
|
||||
Integer couponType, java.math.BigDecimal amount,
|
||||
java.math.BigDecimal minAmount, Integer discountRate,
|
||||
String couponType, java.math.BigDecimal amount,
|
||||
java.util.Date validStartTime,
|
||||
java.util.Date validEndTime) {
|
||||
|
||||
log.info("开始创建携程优惠券 - ctripCouponId: {}, userPhone: {}, couponName: {}, amount: {}, minAmount: {}",
|
||||
ctripCouponId, userPhone, couponName, amount, minAmount);
|
||||
log.info("开始创建携程优惠券 - ctripCouponId: {}, userPhone: {}, couponName: {}, amount: {}",
|
||||
ctripCouponId, userPhone, couponName, amount);
|
||||
|
||||
// 检查是否已存在该携程优惠券
|
||||
Coupon existingCoupon = baseMapper.selectByCtripCouponId(ctripCouponId);
|
||||
@@ -148,15 +143,12 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
log.info("携程优惠券不存在,开始创建新优惠券");
|
||||
|
||||
Coupon coupon = new Coupon();
|
||||
coupon.setCouponNo(generateCouponNo());
|
||||
coupon.setCouponName(couponName);
|
||||
coupon.setCouponType(couponType);
|
||||
coupon.setAmount(amount);
|
||||
coupon.setMinAmount(minAmount);
|
||||
coupon.setDiscountRate(discountRate);
|
||||
coupon.setStatus(0); // 未使用
|
||||
coupon.setAmountCurrency("CNY");
|
||||
coupon.setStatus("UNUSED"); // 未使用
|
||||
coupon.setUserPhone(userPhone);
|
||||
coupon.setSource(1); // 来源:携程
|
||||
coupon.setCtripCouponId(ctripCouponId);
|
||||
coupon.setValidStartTime(validStartTime);
|
||||
coupon.setValidEndTime(validEndTime);
|
||||
@@ -164,14 +156,16 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
coupon.setUpdateTime(new Date());
|
||||
coupon.setCreateBy("ctrip");
|
||||
coupon.setUpdateBy("ctrip");
|
||||
coupon.setTenantId("000000");
|
||||
coupon.setDeleted(0);
|
||||
|
||||
log.info("优惠券对象创建完成,准备保存到数据库 - couponNo: {}", coupon.getCouponNo());
|
||||
log.info("优惠券对象创建完成,准备保存到数据库");
|
||||
|
||||
boolean saveResult = save(coupon);
|
||||
|
||||
if (saveResult) {
|
||||
log.info("携程优惠券保存成功 - 优惠券ID: {}, couponNo: {}, ctripCouponId: {}",
|
||||
coupon.getId(), coupon.getCouponNo(), ctripCouponId);
|
||||
log.info("携程优惠券保存成功 - 优惠券ID: {}, ctripCouponId: {}",
|
||||
coupon.getId(), ctripCouponId);
|
||||
} else {
|
||||
log.error("携程优惠券保存失败 - ctripCouponId: {}, userPhone: {}", ctripCouponId, userPhone);
|
||||
}
|
||||
@@ -199,7 +193,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
}
|
||||
|
||||
// 2. 验证优惠券状态
|
||||
if (coupon.getStatus() != 0) {
|
||||
if (!"UNUSED".equals(coupon.getStatus())) {
|
||||
log.error("携程优惠券状态不正确,ctripCouponId: {}, status: {}", ctripCouponId, coupon.getStatus());
|
||||
throw new CouponInvalidException("优惠券状态不正确");
|
||||
}
|
||||
@@ -273,8 +267,8 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
}
|
||||
|
||||
Coupon bestCoupon = sortedCoupons.get(0);
|
||||
log.info("选择最优优惠券,couponId: {}, amount: {}, discount: {}",
|
||||
bestCoupon.getId(), bestCoupon.getAmount(), bestCoupon.getDiscountRate());
|
||||
log.info("选择最优优惠券,couponId: {}, amount: {}",
|
||||
bestCoupon.getId(), bestCoupon.getAmount());
|
||||
|
||||
return bestCoupon;
|
||||
}
|
||||
@@ -286,14 +280,10 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
try {
|
||||
// 1. 验证订单金额是否满足使用门槛
|
||||
java.math.BigDecimal orderAmount = getOrderAmount(orderNo);
|
||||
if (coupon.getMinAmount() != null && orderAmount.compareTo(coupon.getMinAmount()) < 0) {
|
||||
log.warn("订单金额不满足优惠券使用门槛,orderAmount: {}, minAmount: {}",
|
||||
orderAmount, coupon.getMinAmount());
|
||||
return false;
|
||||
}
|
||||
log.info("验证优惠券使用条件,orderAmount: {}", orderAmount);
|
||||
|
||||
// 2. 验证优惠券是否已被使用
|
||||
if (coupon.getStatus() != 0) {
|
||||
if (!"UNUSED".equals(coupon.getStatus())) {
|
||||
log.warn("优惠券已被使用,status: {}", coupon.getStatus());
|
||||
return false;
|
||||
}
|
||||
@@ -321,17 +311,12 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
* 验证优惠券是否适用于当前订单
|
||||
*/
|
||||
private boolean validateCouponForOrder(Coupon coupon, java.math.BigDecimal orderAmount) {
|
||||
// 1. 验证订单金额是否满足使用门槛
|
||||
if (coupon.getMinAmount() != null && orderAmount.compareTo(coupon.getMinAmount()) < 0) {
|
||||
// 1. 验证优惠券状态
|
||||
if (!"UNUSED".equals(coupon.getStatus())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 验证优惠券状态
|
||||
if (coupon.getStatus() != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 验证有效期
|
||||
// 2. 验证有效期
|
||||
Date now = new Date();
|
||||
if (coupon.getValidStartTime().after(now) || coupon.getValidEndTime().before(now)) {
|
||||
return false;
|
||||
@@ -344,25 +329,13 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
* 比较优惠券优先级
|
||||
*/
|
||||
private int compareCouponPriority(Coupon c1, Coupon c2, java.math.BigDecimal orderAmount) {
|
||||
// 1. 优先选择满减券
|
||||
if (c1.getCouponType() == 1 && c2.getCouponType() != 1) {
|
||||
return -1;
|
||||
}
|
||||
if (c1.getCouponType() != 1 && c2.getCouponType() == 1) {
|
||||
return 1;
|
||||
// 1. 优先选择金额大的优惠券
|
||||
int amountCompare = c2.getAmount().compareTo(c1.getAmount());
|
||||
if (amountCompare != 0) {
|
||||
return amountCompare;
|
||||
}
|
||||
|
||||
// 2. 满减券按金额排序(金额大的优先)
|
||||
if (c1.getCouponType() == 1 && c2.getCouponType() == 1) {
|
||||
return c2.getAmount().compareTo(c1.getAmount());
|
||||
}
|
||||
|
||||
// 3. 折扣券按折扣率排序(折扣率高的优先)
|
||||
if (c1.getCouponType() == 2 && c2.getCouponType() == 2) {
|
||||
return c2.getDiscountRate().compareTo(c1.getDiscountRate());
|
||||
}
|
||||
|
||||
// 4. 按创建时间排序(先创建的优先)
|
||||
// 2. 按创建时间排序(先创建的优先)
|
||||
return c1.getCreateTime().compareTo(c2.getCreateTime());
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -508,7 +508,7 @@ public class CtripOrderServiceImpl implements ICtripOrderService {
|
||||
}
|
||||
|
||||
// 验证优惠券状态
|
||||
if (existingCoupon.getStatus() != 0) {
|
||||
if (!"UNUSED".equals(existingCoupon.getStatus())) {
|
||||
log.warn("优惠券状态不正确,code: {}, status: {}", coupon.getCode(), existingCoupon.getStatus());
|
||||
return false;
|
||||
}
|
||||
|
||||
+101
-10
@@ -11,6 +11,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* 携程解密工具类
|
||||
@@ -26,8 +27,8 @@ public class CtripDecryptUtil {
|
||||
@Value("${ctrip.aes.iv:your-aes-iv-here}")
|
||||
private String aesIv;
|
||||
|
||||
@Value("${ctrip.sign.key:your-sign-key-here}")
|
||||
private String md5Secret;
|
||||
@Value("${ctrip.account.secret:your-account-secret}")
|
||||
private String accountSecret;
|
||||
|
||||
/**
|
||||
* AES-128-CBC解密
|
||||
@@ -82,6 +83,12 @@ public class CtripDecryptUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 检查解密数据长度是否为16的倍数
|
||||
if (encryptedBytes.length % 16 != 0) {
|
||||
log.error("AES解密失败:加密数据长度不是16的倍数,长度: {}", encryptedBytes.length);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 创建密钥规范
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey.getBytes(StandardCharsets.UTF_8), "AES");
|
||||
IvParameterSpec ivParameterSpec = new IvParameterSpec(aesIv.getBytes(StandardCharsets.UTF_8));
|
||||
@@ -99,7 +106,14 @@ public class CtripDecryptUtil {
|
||||
return decryptedData;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("AES解密失败: {}", e.getMessage(), e);
|
||||
log.error("AES解密失败: {}", e.getMessage());
|
||||
if (e instanceof IllegalArgumentException) {
|
||||
log.error("数据格式错误,可能是非携程加密数据");
|
||||
} else if (e instanceof javax.crypto.IllegalBlockSizeException) {
|
||||
log.error("数据块大小错误,可能是加密数据损坏或格式不正确");
|
||||
} else if (e instanceof javax.crypto.BadPaddingException) {
|
||||
log.error("填充错误,可能是密钥或IV不正确");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -159,6 +173,12 @@ public class CtripDecryptUtil {
|
||||
* @return
|
||||
*/
|
||||
public byte[] decodeBytes(String str) {
|
||||
// 检查字符串长度是否为偶数
|
||||
if (str.length() % 2 != 0) {
|
||||
log.error("携程自定义解码失败:字符串长度不是偶数,长度: {}", str.length());
|
||||
throw new IllegalArgumentException("字符串长度必须是偶数");
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[str.length() / 2];
|
||||
for (int i = 0; i < str.length(); i += 2) {
|
||||
char c = str.charAt(i);
|
||||
@@ -185,7 +205,7 @@ public class CtripDecryptUtil {
|
||||
try {
|
||||
log.info("开始验证MD5签名,accountId: {}, serviceName: {}, requestTime: {}",
|
||||
accountId, serviceName, requestTime);
|
||||
log.info("签名密钥长度: {}", md5Secret != null ? md5Secret.length() : 0);
|
||||
log.info("账户密钥长度: {}", accountSecret != null ? accountSecret.length() : 0);
|
||||
|
||||
// 构建签名字符串
|
||||
String signString = buildSignString(accountId, serviceName, requestTime, body, version);
|
||||
@@ -198,7 +218,7 @@ public class CtripDecryptUtil {
|
||||
|
||||
log.info("MD5签名验证结果: {}, 期望签名: {}, 计算签名: {}", isValid, sign, calculatedSign);
|
||||
log.debug("签名字符串: {}", signString);
|
||||
log.debug("签名密钥: {}", md5Secret);
|
||||
log.debug("账户密钥: {}", accountSecret);
|
||||
return isValid;
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -247,7 +267,7 @@ public class CtripDecryptUtil {
|
||||
log.info("requestTime: {}", requestTime);
|
||||
log.info("body: {}", body);
|
||||
log.info("version: {}", version);
|
||||
log.info("signKey: {}", md5Secret);
|
||||
log.info("accountSecret: {}", accountSecret);
|
||||
|
||||
String signString = buildSignString(accountId, serviceName, requestTime, body, version);
|
||||
log.info("签名字符串: {}", signString);
|
||||
@@ -262,6 +282,62 @@ public class CtripDecryptUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 详细调试签名计算过程
|
||||
*
|
||||
* @param accountId 供应商帐号
|
||||
* @param serviceName 接口名称
|
||||
* @param requestTime 请求时间
|
||||
* @param body 请求体
|
||||
* @param version 版本号
|
||||
* @param expectedSign 期望的签名
|
||||
* @return 调试信息
|
||||
*/
|
||||
public Map<String, Object> debugSignCalculation(String accountId, String serviceName, String requestTime,
|
||||
String body, String version, String expectedSign) {
|
||||
Map<String, Object> debugInfo = new java.util.HashMap<>();
|
||||
|
||||
try {
|
||||
log.info("=== 详细调试签名计算 ===");
|
||||
|
||||
// 记录原始参数
|
||||
debugInfo.put("accountId", accountId);
|
||||
debugInfo.put("serviceName", serviceName);
|
||||
debugInfo.put("requestTime", requestTime);
|
||||
debugInfo.put("body", body);
|
||||
debugInfo.put("version", version);
|
||||
debugInfo.put("expectedSign", expectedSign);
|
||||
debugInfo.put("accountSecret", accountSecret);
|
||||
|
||||
// 构建签名字符串
|
||||
String signString = buildSignString(accountId, serviceName, requestTime, body, version);
|
||||
debugInfo.put("signString", signString);
|
||||
debugInfo.put("signStringLength", signString.length());
|
||||
|
||||
// 计算MD5
|
||||
String calculatedSign = calculateMD5(signString).toLowerCase();
|
||||
debugInfo.put("calculatedSign", calculatedSign);
|
||||
debugInfo.put("isMatch", calculatedSign.equalsIgnoreCase(expectedSign));
|
||||
|
||||
// 分析每个部分的长度
|
||||
debugInfo.put("accountIdLength", accountId != null ? accountId.length() : 0);
|
||||
debugInfo.put("serviceNameLength", serviceName != null ? serviceName.length() : 0);
|
||||
debugInfo.put("requestTimeLength", requestTime != null ? requestTime.length() : 0);
|
||||
debugInfo.put("bodyLength", body != null ? body.length() : 0);
|
||||
debugInfo.put("versionLength", version != null ? version.length() : 0);
|
||||
debugInfo.put("accountSecretLength", accountSecret != null ? accountSecret.length() : 0);
|
||||
|
||||
log.info("签名调试完成 - 匹配结果: {}", debugInfo.get("isMatch"));
|
||||
|
||||
return debugInfo;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("签名调试失败: {}", e.getMessage(), e);
|
||||
debugInfo.put("error", e.getMessage());
|
||||
return debugInfo;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证AES配置
|
||||
*
|
||||
@@ -369,8 +445,8 @@ public class CtripDecryptUtil {
|
||||
|
||||
/**
|
||||
* 构建签名字符串
|
||||
* 格式:accountId + serviceName + requestTime + body + version + signkey
|
||||
* 注意:这里使用的是signkey而不是md5Secret
|
||||
* 格式:accountId + serviceName + requestTime + body + version + accountSecret
|
||||
* 使用账户密钥作为签名密钥
|
||||
*/
|
||||
private String buildSignString(String accountId, String serviceName, String requestTime,
|
||||
String body, String version) {
|
||||
@@ -378,9 +454,24 @@ public class CtripDecryptUtil {
|
||||
sb.append(accountId != null ? accountId : "");
|
||||
sb.append(serviceName != null ? serviceName : "");
|
||||
sb.append(requestTime != null ? requestTime : "");
|
||||
sb.append(body != null ? body : "");
|
||||
|
||||
// 确保body是紧凑的JSON格式(无多余空格和换行)
|
||||
String compactBody = body;
|
||||
if (body != null) {
|
||||
try {
|
||||
// 重新格式化JSON,确保格式一致
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Object jsonObject = objectMapper.readValue(body, Object.class);
|
||||
compactBody = objectMapper.writeValueAsString(jsonObject);
|
||||
} catch (Exception e) {
|
||||
log.warn("JSON格式化失败,使用原始body: {}", e.getMessage());
|
||||
compactBody = body;
|
||||
}
|
||||
}
|
||||
sb.append(compactBody != null ? compactBody : "");
|
||||
sb.append(version != null ? version : "");
|
||||
sb.append(md5Secret != null ? md5Secret : "");
|
||||
// 使用账户密钥而不是单独的签名密钥
|
||||
sb.append(accountSecret != null ? accountSecret : "");
|
||||
|
||||
String signString = sb.toString();
|
||||
log.debug("构建签名字符串: {}", signString);
|
||||
|
||||
Reference in New Issue
Block a user