fix(优惠券中心):携程所有测试用例全部测试通过,提取配置文件,标准化上线生产环境
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
package com.central.coupon.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 携程API配置
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "ctrip")
|
||||
public class CtripApiConfig {
|
||||
|
||||
/**
|
||||
* AES加密配置
|
||||
*/
|
||||
private AesConfig aes;
|
||||
|
||||
/**
|
||||
* API配置
|
||||
*/
|
||||
private ApiConfig api;
|
||||
|
||||
/**
|
||||
* 账户配置
|
||||
*/
|
||||
private AccountConfig account;
|
||||
|
||||
@Data
|
||||
public static class AesConfig {
|
||||
private String key;
|
||||
private String iv;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ApiConfig {
|
||||
private String testUrl;
|
||||
private String prodUrl;
|
||||
private String environment;
|
||||
private Integer timeout;
|
||||
private Integer retryTimes;
|
||||
|
||||
/**
|
||||
* 获取当前环境的API基础URL
|
||||
*/
|
||||
public String getBaseUrl() {
|
||||
return "prod".equals(environment) ? prodUrl : testUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单通知接口的完整URL
|
||||
*/
|
||||
public String getOrderNoticeUrl() {
|
||||
return getBaseUrl() + "/order/notice.do";
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class AccountConfig {
|
||||
private String id;
|
||||
private String secret;
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -1,5 +1,6 @@
|
||||
package com.central.coupon.service.impl;
|
||||
|
||||
import com.central.coupon.config.CtripApiConfig;
|
||||
import com.central.coupon.controller.CtripConsumedController.CtripConsumeRequest;
|
||||
import com.central.coupon.controller.CtripConsumedController.CtripConsumeBatchRequest;
|
||||
import com.central.coupon.entity.CtripConsumed;
|
||||
@@ -40,6 +41,9 @@ public class CtripConsumedServiceImpl implements CtripConsumedService {
|
||||
@Autowired
|
||||
private CtripDecryptUtil ctripDecryptUtil;
|
||||
|
||||
@Autowired
|
||||
private CtripApiConfig ctripApiConfig;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CtripConsumedNoticeResponse consumeOrder(CtripConsumeRequest request) {
|
||||
@@ -390,7 +394,7 @@ public class CtripConsumedServiceImpl implements CtripConsumedService {
|
||||
return;
|
||||
}
|
||||
log.info("=== 开始发送HTTP请求到携程OrderConsumedNotice接口 ===");
|
||||
log.info("携程API URL: https://ttdopen.ctrip.com/api/order/notice.do");
|
||||
log.info("携程API URL: {}", ctripApiConfig.getApi().getOrderNoticeUrl());
|
||||
log.info("=== 发送到携程的完整请求JSON ===");
|
||||
|
||||
// 11. 生成签名
|
||||
@@ -448,7 +452,7 @@ public class CtripConsumedServiceImpl implements CtripConsumedService {
|
||||
objectMapper.writeValueAsString(request), headers);
|
||||
|
||||
// 发送POST请求
|
||||
String url = "https://ttdopen.ctrip.com/api/order/notice.do";
|
||||
String url = ctripApiConfig.getApi().getOrderNoticeUrl();
|
||||
log.info("发送请求到URL: {}", url);
|
||||
|
||||
org.springframework.http.ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
|
||||
|
||||
+2
-202
@@ -91,20 +91,8 @@ public class CtripPayPreOrderServiceImpl implements CtripPayPreOrderService {
|
||||
// 记录已处理的sequenceId(幂等性保证)
|
||||
recordProcessedSequenceId(sequenceId, response);
|
||||
|
||||
// PayPreOrderConfirm 是供应商主动调用携程的接口
|
||||
// 在 PayPreOrder 成功后自动调用,用于确认支付结果并发送凭证
|
||||
// 注意:某些测试用例可能不需要发送 PayPreOrderConfirm
|
||||
log.info("PayPreOrder 处理完成,检查是否需要发送 PayPreOrderConfirm");
|
||||
|
||||
// 检查是否需要发送 PayPreOrderConfirm
|
||||
// 根据携程文档,某些测试用例不需要发送确认通知
|
||||
boolean shouldSendConfirm = shouldSendPayPreOrderConfirm(request);
|
||||
if (shouldSendConfirm) {
|
||||
log.info("需要发送 PayPreOrderConfirm,开始自动调用");
|
||||
autoCallPayPreOrderConfirm(request.getBody().getOtaOrderId(), request.getBody().getSupplierOrderId(), request.getBody().getSequenceId());
|
||||
} else {
|
||||
log.info("当前测试用例不需要发送 PayPreOrderConfirm,跳过自动调用");
|
||||
}
|
||||
// PayPreOrderConfirm 不再自动调用,由携程主动发起
|
||||
log.info("PayPreOrder 处理完成,等待携程主动调用 PayPreOrderConfirm");
|
||||
|
||||
return response;
|
||||
|
||||
@@ -755,144 +743,6 @@ public class CtripPayPreOrderServiceImpl implements CtripPayPreOrderService {
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动调用携程的PayPreOrderConfirm接口
|
||||
* 按照携程文档格式实现
|
||||
* 现在只打印请求体,不实际调用接口
|
||||
*/
|
||||
private void autoCallPayPreOrderConfirm(String otaOrderId, String supplierOrderId, String sequenceId) {
|
||||
try {
|
||||
log.info("=== 生成PayPreOrderConfirm请求体(不实际调用) ===");
|
||||
log.info("otaOrderId: {}, supplierOrderId: {}, sequenceId: {}", otaOrderId, supplierOrderId, sequenceId);
|
||||
|
||||
// 1. 构建请求头
|
||||
Map<String, Object> header = new HashMap<>();
|
||||
header.put("accountId", "c50a8f2a4a59c9e7"); // 需要配置
|
||||
header.put("serviceName", "PayPreOrderConfirm");
|
||||
header.put("requestTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
header.put("version", "1.0");
|
||||
|
||||
// 2. 从数据库查询订单信息
|
||||
CtripOrder order = ctripOrderMapper.selectByOtaOrderId(otaOrderId);
|
||||
if (order == null) {
|
||||
log.error("未找到订单信息,otaOrderId: {}", otaOrderId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 查询订单项信息
|
||||
List<CtripOrderItem> orderItems = ctripOrderItemMapper.selectByOrderId(order.getId());
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
log.error("未找到订单项信息,orderId: {}", order.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 构建请求体
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("sequenceId", sequenceId);
|
||||
body.put("otaOrderId", otaOrderId);
|
||||
body.put("supplierOrderId", supplierOrderId);
|
||||
body.put("confirmResultCode", "0000"); // 确认成功
|
||||
body.put("confirmResultMessage", "确认成功");
|
||||
body.put("voucherSender", 1); // 携程对客发送凭证
|
||||
body.put("orderExtendMessage", "订单支付成功,凭证已生成");
|
||||
|
||||
// 5. 构建凭证信息
|
||||
List<Map<String, Object>> vouchers = new ArrayList<>();
|
||||
List<Map<String, Object>> items = new ArrayList<>();
|
||||
|
||||
for (CtripOrderItem orderItem : orderItems) {
|
||||
// 构建凭证
|
||||
Map<String, Object> voucher = new HashMap<>();
|
||||
voucher.put("itemId", orderItem.getItemId());
|
||||
voucher.put("voucherId", orderItem.getVoucherId());
|
||||
voucher.put("voucherType", 1); // 普通凭证
|
||||
voucher.put("voucherCode", "CODE_" + orderItem.getItemId());
|
||||
voucher.put("voucherData", "");
|
||||
voucher.put("voucherSeatInfo", "");
|
||||
vouchers.add(voucher);
|
||||
|
||||
// 构建订单项
|
||||
Map<String, Object> item = new HashMap<>();
|
||||
item.put("itemId", orderItem.getItemId());
|
||||
item.put("isCredentialVouchers", "0");
|
||||
|
||||
// 构建乘客凭证关联
|
||||
List<Map<String, Object>> passengerVouchers = new ArrayList<>();
|
||||
Map<String, Object> passengerVoucher = new HashMap<>();
|
||||
passengerVoucher.put("passengerId", "PASS_" + orderItem.getItemId());
|
||||
passengerVoucher.put("voucherId", orderItem.getVoucherId());
|
||||
passengerVouchers.add(passengerVoucher);
|
||||
item.put("passengerVouchers", passengerVouchers);
|
||||
|
||||
// 构建库存信息
|
||||
List<Map<String, Object>> inventorys = new ArrayList<>();
|
||||
Map<String, Object> inventory = new HashMap<>();
|
||||
// 从订单项中获取使用日期,如果没有则使用默认值
|
||||
String useDate = orderItem.getUseStartDate() != null ?
|
||||
orderItem.getUseStartDate().toString() : "2025-08-25";
|
||||
inventory.put("useDate", useDate);
|
||||
inventory.put("quantity", orderItem.getQuantity() != null ? orderItem.getQuantity() : 1);
|
||||
inventorys.add(inventory);
|
||||
item.put("inventorys", inventorys);
|
||||
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
body.put("vouchers", vouchers);
|
||||
body.put("items", items);
|
||||
|
||||
// 6. 序列化body为JSON
|
||||
String bodyJson = objectMapper.writeValueAsString(body);
|
||||
log.info("=== 自动调用支付确认接口 - JSON格式请求体 ===");
|
||||
log.info("Complete Request JSON: {}", bodyJson);
|
||||
|
||||
// 7. 加密body
|
||||
String encryptedBody = ctripDecryptUtil.encryptAES(bodyJson);
|
||||
if (encryptedBody == null) {
|
||||
log.error("加密body失败");
|
||||
return;
|
||||
}
|
||||
log.info("=== 开始发送HTTP请求到携程PayPreOrderConfirm接口 ===");
|
||||
log.info("携程API URL: https://ttdopen.ctrip.com/api/order/notice.do");
|
||||
log.info("=== 发送到携程的完整请求JSON ===");
|
||||
|
||||
// 8. 生成签名
|
||||
String correctSign = ctripDecryptUtil.generateSign(
|
||||
header.get("accountId").toString(),
|
||||
header.get("serviceName").toString(),
|
||||
header.get("requestTime").toString(),
|
||||
encryptedBody,
|
||||
header.get("version").toString()
|
||||
);
|
||||
|
||||
if (correctSign == null) {
|
||||
log.error("生成签名失败");
|
||||
return;
|
||||
}
|
||||
|
||||
// 9. 设置签名
|
||||
header.put("sign", correctSign);
|
||||
|
||||
// 10. 构建最终请求
|
||||
Map<String, Object> finalRequest = new HashMap<>();
|
||||
finalRequest.put("header", header);
|
||||
finalRequest.put("body", encryptedBody);
|
||||
|
||||
// 11. 序列化为JSON并打印
|
||||
String finalJson = objectMapper.writeValueAsString(finalRequest);
|
||||
log.info("Request JSON: {}", finalJson);
|
||||
|
||||
log.info("=== 请求体生成完成,请使用Postman手动调用 ===");
|
||||
log.info("URL: https://ttdopen.ctrip.com/api/order/notice.do");
|
||||
log.info("Method: POST");
|
||||
log.info("Content-Type: application/json");
|
||||
log.info("Body: {}", finalJson);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("生成PayPreOrderConfirm请求体异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP请求到携程的PayPreOrderConfirm接口
|
||||
*/
|
||||
@@ -1216,54 +1066,4 @@ public class CtripPayPreOrderServiceImpl implements CtripPayPreOrderService {
|
||||
throw new RuntimeException("验证itemId更新失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否需要发送PayPreOrderConfirm
|
||||
* 暂时不自动调用,只打印请求参数供手动测试
|
||||
*/
|
||||
private boolean shouldSendPayPreOrderConfirm(CtripPayPreOrderRequest request) {
|
||||
try {
|
||||
String sequenceId = request.getBody().getSequenceId();
|
||||
String otaOrderId = request.getBody().getOtaOrderId();
|
||||
Integer confirmType = request.getBody().getConfirmType();
|
||||
|
||||
log.info("=== PayPreOrderConfirm 手动测试参数 ===");
|
||||
log.info("otaOrderId: {}", otaOrderId);
|
||||
log.info("sequenceId: {}", sequenceId);
|
||||
log.info("confirmType: {}", confirmType);
|
||||
log.info("supplierOrderId: {}", request.getBody().getSupplierOrderId());
|
||||
|
||||
// 打印完整的请求参数,供Postman手动测试
|
||||
log.info("=== 完整的PayPreOrderConfirm请求参数 ===");
|
||||
log.info("请使用以下参数在Postman中手动调用PayPreOrderConfirm接口:");
|
||||
log.info("URL: https://ttdopen.ctrip.com/api/order/notice.do");
|
||||
log.info("Method: POST");
|
||||
log.info("Content-Type: application/json");
|
||||
log.info("=== 请求体参数 ===");
|
||||
log.info("accountId: c50a8f2a4a59c9e7");
|
||||
log.info("serviceName: PayPreOrderConfirm");
|
||||
log.info("requestTime: 当前时间 (yyyy-MM-dd HH:mm:ss格式)");
|
||||
log.info("version: 1.0");
|
||||
log.info("sequenceId: {}", sequenceId);
|
||||
log.info("otaOrderId: {}", otaOrderId);
|
||||
log.info("supplierOrderId: {}", request.getBody().getSupplierOrderId());
|
||||
log.info("confirmResultCode: 0000");
|
||||
log.info("confirmResultMessage: 确认成功");
|
||||
log.info("voucherSender: 1");
|
||||
log.info("orderExtendMessage: 订单支付成功,凭证已生成");
|
||||
log.info("=== 注意 ===");
|
||||
log.info("1. body需要AES加密");
|
||||
log.info("2. 需要生成MD5签名");
|
||||
log.info("3. 签名参数: accountId + serviceName + requestTime + body + version + accountSecret");
|
||||
log.info("=== 手动测试完成 ===");
|
||||
|
||||
// 返回true来触发请求体生成
|
||||
log.info("生成PayPreOrderConfirm请求体供手动测试");
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("判断是否需要发送PayPreOrderConfirm异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,9 @@ spring:
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# 环境配置:test(测试环境) / prod(生产环境)
|
||||
profiles:
|
||||
active: test
|
||||
|
||||
mybatis-plus:
|
||||
mapper-locations: classpath:/mapper/*Mapper.xml
|
||||
@@ -51,7 +54,12 @@ ctrip:
|
||||
# 签名配置(使用账户密钥进行签名计算)
|
||||
# API配置
|
||||
api:
|
||||
base-url: https://ttdopen.ctrip.com/api # 携程API基础URL
|
||||
# 测试环境URL
|
||||
test-url: https://ttdopen.ctrip.com/api
|
||||
# 生产环境URL
|
||||
prod-url: https://ttdentry.ctrip.com/ttd-connect-orderentryapi/supplier
|
||||
# 当前环境(test/prod)
|
||||
environment: test
|
||||
timeout: 30000 # 请求超时时间(毫秒)
|
||||
retry-times: 3 # 重试次数
|
||||
# 账户配置
|
||||
|
||||
Reference in New Issue
Block a user