feat(payment): 新增订单创建与支付一体化接口
- 添加了 PaymentWithOrderRequest 类用于订单创建和支付请求 - 在 PaymentService接口中新增了 createPaymentWithOrder 方法 - 在 PaymentServiceImpl 中实现了订单创建和支付的逻辑 - 更新了 ShopOrderController 中的订单创建逻辑 - 添加了新的 API 文档 unified_payment_with_order_api.md
This commit is contained in:
@@ -7,6 +7,7 @@ import com.gxwebsoft.payment.constants.PaymentConstants;
|
||||
import com.gxwebsoft.payment.dto.PaymentRequest;
|
||||
import com.gxwebsoft.payment.dto.PaymentResponse;
|
||||
import com.gxwebsoft.payment.dto.PaymentStatusUpdateRequest;
|
||||
import com.gxwebsoft.payment.dto.PaymentWithOrderRequest;
|
||||
import com.gxwebsoft.payment.enums.PaymentType;
|
||||
import com.gxwebsoft.payment.exception.PaymentException;
|
||||
import com.gxwebsoft.payment.service.PaymentService;
|
||||
@@ -70,6 +71,33 @@ public class PaymentController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "创建支付订单(包含订单信息)", description = "统一支付模块:创建订单并发起支付")
|
||||
@PostMapping("/create-with-order")
|
||||
public ApiResult<?> createPaymentWithOrder(@Valid @RequestBody PaymentWithOrderRequest request) {
|
||||
log.info("收到支付与订单创建请求: {}", request);
|
||||
final User loginUser = getLoginUser();
|
||||
|
||||
if(loginUser == null){
|
||||
return fail("请先登录");
|
||||
}
|
||||
|
||||
// 设置用户信息
|
||||
if(request.getTenantId() == null){
|
||||
request.setTenantId(loginUser.getTenantId());
|
||||
}
|
||||
|
||||
try {
|
||||
PaymentResponse response = paymentService.createPaymentWithOrder(request, loginUser);
|
||||
return this.<PaymentResponse>success("订单创建并发起支付成功", response);
|
||||
} catch (PaymentException e) {
|
||||
log.error("创建支付订单失败: {}", e.getMessage());
|
||||
return fail(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("创建支付订单系统错误: {}", e.getMessage(), e);
|
||||
return fail(PaymentConstants.ErrorMessage.SYSTEM_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "查询支付状态", description = "查询指定订单的支付状态")
|
||||
@GetMapping("/query")
|
||||
public ApiResult<?> queryPayment(
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.gxwebsoft.payment.dto;
|
||||
|
||||
import com.gxwebsoft.payment.enums.PaymentType;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 支付与订单创建请求DTO
|
||||
* 用于统一支付模块中的订单创建和支付
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-01-26
|
||||
*/
|
||||
@Data
|
||||
@Schema(name = "PaymentWithOrderRequest", description = "支付与订单创建请求")
|
||||
public class PaymentWithOrderRequest {
|
||||
|
||||
// ========== 支付相关字段 ==========
|
||||
|
||||
@Schema(description = "支付类型", required = true)
|
||||
@NotNull(message = "支付类型不能为空")
|
||||
private PaymentType paymentType;
|
||||
|
||||
@Schema(description = "支付金额", required = true)
|
||||
@NotNull(message = "支付金额不能为空")
|
||||
@DecimalMin(value = "0.01", message = "支付金额必须大于0")
|
||||
@Digits(integer = 10, fraction = 2, message = "支付金额格式不正确")
|
||||
private BigDecimal amount;
|
||||
|
||||
@Schema(description = "订单标题", required = true)
|
||||
@NotBlank(message = "订单标题不能为空")
|
||||
@Size(max = 60, message = "订单标题长度不能超过60个字符")
|
||||
private String subject;
|
||||
|
||||
@Schema(description = "订单描述")
|
||||
@Size(max = 500, message = "订单描述长度不能超过500个字符")
|
||||
private String description;
|
||||
|
||||
@Schema(description = "租户ID", required = true)
|
||||
@NotNull(message = "租户ID不能为空")
|
||||
@Positive(message = "租户ID必须为正数")
|
||||
private Integer tenantId;
|
||||
|
||||
// ========== 订单相关字段 ==========
|
||||
|
||||
@Schema(description = "订单信息", required = true)
|
||||
@Valid
|
||||
@NotNull(message = "订单信息不能为空")
|
||||
private OrderInfo orderInfo;
|
||||
|
||||
/**
|
||||
* 订单信息
|
||||
*/
|
||||
@Data
|
||||
@Schema(name = "OrderInfo", description = "订单信息")
|
||||
public static class OrderInfo {
|
||||
|
||||
@Schema(description = "订单类型,0商城订单 1预定订单/外卖 2会员卡")
|
||||
@NotNull(message = "订单类型不能为空")
|
||||
@Min(value = 0, message = "订单类型值无效")
|
||||
@Max(value = 2, message = "订单类型值无效")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "收货人姓名")
|
||||
@Size(max = 50, message = "收货人姓名长度不能超过50个字符")
|
||||
private String realName;
|
||||
|
||||
@Schema(description = "收货地址")
|
||||
@Size(max = 200, message = "收货地址长度不能超过200个字符")
|
||||
private String address;
|
||||
|
||||
@Schema(description = "关联收货地址ID")
|
||||
private Integer addressId;
|
||||
|
||||
@Schema(description = "快递/自提,0快递 1自提")
|
||||
private Integer deliveryType;
|
||||
|
||||
@Schema(description = "下单渠道,0小程序预定 1俱乐部训练场 3活动订场")
|
||||
private Integer channel;
|
||||
|
||||
@Schema(description = "商户ID")
|
||||
private Long merchantId;
|
||||
|
||||
@Schema(description = "商户名称")
|
||||
private String merchantName;
|
||||
|
||||
@Schema(description = "使用的优惠券ID")
|
||||
private Integer couponId;
|
||||
|
||||
@Schema(description = "备注")
|
||||
@Size(max = 500, message = "备注长度不能超过500字符")
|
||||
private String comments;
|
||||
|
||||
@Schema(description = "订单商品列表", required = true)
|
||||
@Valid
|
||||
@NotEmpty(message = "订单商品列表不能为空")
|
||||
private List<OrderGoodsItem> goodsItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单商品项
|
||||
*/
|
||||
@Data
|
||||
@Schema(name = "OrderGoodsItem", description = "订单商品项")
|
||||
public static class OrderGoodsItem {
|
||||
|
||||
@Schema(description = "商品ID", required = true)
|
||||
@NotNull(message = "商品ID不能为空")
|
||||
@Positive(message = "商品ID必须为正数")
|
||||
private Integer goodsId;
|
||||
|
||||
@Schema(description = "商品SKU ID")
|
||||
private Integer skuId;
|
||||
|
||||
@Schema(description = "商品数量", required = true)
|
||||
@NotNull(message = "商品数量不能为空")
|
||||
@Min(value = 1, message = "商品数量必须大于0")
|
||||
private Integer quantity;
|
||||
|
||||
@Schema(description = "规格信息,如:颜色:红色|尺寸:L")
|
||||
private String specInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取格式化的金额字符串
|
||||
*/
|
||||
public String getFormattedAmount() {
|
||||
if (amount == null) {
|
||||
return "0.00";
|
||||
}
|
||||
return String.format("%.2f", amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证订单商品总金额是否与支付金额一致
|
||||
*/
|
||||
public boolean isAmountConsistent() {
|
||||
if (amount == null || orderInfo == null || orderInfo.getGoodsItems() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 这里可以添加商品金额计算逻辑
|
||||
// 实际实现时需要查询数据库获取商品价格
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("PaymentWithOrderRequest{paymentType=%s, amount=%s, subject='%s', tenantId=%d, goodsCount=%d}",
|
||||
paymentType, getFormattedAmount(), subject, tenantId,
|
||||
orderInfo != null && orderInfo.getGoodsItems() != null ? orderInfo.getGoodsItems().size() : 0);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.gxwebsoft.payment.service;
|
||||
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.payment.dto.PaymentRequest;
|
||||
import com.gxwebsoft.payment.dto.PaymentResponse;
|
||||
import com.gxwebsoft.payment.dto.PaymentWithOrderRequest;
|
||||
import com.gxwebsoft.payment.enums.PaymentType;
|
||||
import com.gxwebsoft.payment.exception.PaymentException;
|
||||
|
||||
@@ -27,6 +29,17 @@ public interface PaymentService {
|
||||
*/
|
||||
PaymentResponse createPayment(PaymentRequest request) throws PaymentException;
|
||||
|
||||
/**
|
||||
* 创建支付订单(包含订单信息)
|
||||
* 统一支付模块:先创建订单,再发起支付
|
||||
*
|
||||
* @param request 支付与订单创建请求
|
||||
* @param loginUser 当前登录用户
|
||||
* @return 支付响应
|
||||
* @throws PaymentException 创建失败时抛出
|
||||
*/
|
||||
PaymentResponse createPaymentWithOrder(PaymentWithOrderRequest request, User loginUser) throws PaymentException;
|
||||
|
||||
/**
|
||||
* 查询支付状态
|
||||
*
|
||||
@@ -64,7 +77,7 @@ public interface PaymentService {
|
||||
* @throws PaymentException 退款申请失败时抛出
|
||||
*/
|
||||
PaymentResponse refund(String orderNo, String refundNo, PaymentType paymentType,
|
||||
BigDecimal totalAmount, BigDecimal refundAmount,
|
||||
BigDecimal totalAmount, BigDecimal refundAmount,
|
||||
String reason, Integer tenantId) throws PaymentException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package com.gxwebsoft.payment.service.impl;
|
||||
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.payment.constants.PaymentConstants;
|
||||
import com.gxwebsoft.payment.dto.PaymentRequest;
|
||||
import com.gxwebsoft.payment.dto.PaymentResponse;
|
||||
import com.gxwebsoft.payment.dto.PaymentWithOrderRequest;
|
||||
import com.gxwebsoft.payment.enums.PaymentType;
|
||||
import com.gxwebsoft.payment.exception.PaymentException;
|
||||
import com.gxwebsoft.payment.service.PaymentService;
|
||||
import com.gxwebsoft.payment.strategy.PaymentStrategy;
|
||||
import com.gxwebsoft.shop.dto.OrderCreateRequest;
|
||||
import com.gxwebsoft.shop.service.OrderBusinessService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -40,6 +44,12 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
@Resource
|
||||
private List<PaymentStrategy> paymentStrategies;
|
||||
|
||||
/**
|
||||
* 订单业务服务
|
||||
*/
|
||||
@Resource
|
||||
private OrderBusinessService orderBusinessService;
|
||||
|
||||
/**
|
||||
* 初始化策略映射
|
||||
*/
|
||||
@@ -98,6 +108,40 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentResponse createPaymentWithOrder(PaymentWithOrderRequest request, User loginUser) throws PaymentException {
|
||||
log.info("开始创建支付订单(包含订单信息), 支付类型: {}, 租户ID: {}, 用户ID: {}, 金额: {}",
|
||||
request.getPaymentType(), request.getTenantId(), loginUser.getUserId(), request.getFormattedAmount());
|
||||
|
||||
try {
|
||||
// 1. 参数验证
|
||||
validatePaymentWithOrderRequest(request, loginUser);
|
||||
|
||||
// 2. 转换为订单创建请求
|
||||
OrderCreateRequest orderRequest = convertToOrderCreateRequest(request, loginUser);
|
||||
|
||||
// 3. 创建订单(包含商品验证、库存扣减等完整业务逻辑)
|
||||
Map<String, String> wxOrderInfo = orderBusinessService.createOrder(orderRequest, loginUser);
|
||||
|
||||
// 4. 构建支付响应(复用现有的微信支付返回格式)
|
||||
PaymentResponse response = buildPaymentResponseFromWxOrder(wxOrderInfo, request, orderRequest.getOrderNo());
|
||||
|
||||
log.info("支付订单创建成功(包含订单信息), 支付类型: {}, 租户ID: {}, 订单号: {}, 金额: {}",
|
||||
request.getPaymentType(), request.getTenantId(), response.getOrderNo(), request.getFormattedAmount());
|
||||
|
||||
return response;
|
||||
|
||||
} catch (PaymentException e) {
|
||||
log.error("创建支付订单失败(包含订单信息), 支付类型: {}, 租户ID: {}, 错误: {}",
|
||||
request.getPaymentType(), request.getTenantId(), e.getMessage());
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("创建支付订单系统错误(包含订单信息), 支付类型: {}, 租户ID: {}, 系统错误: {}",
|
||||
request.getPaymentType(), request.getTenantId(), e.getMessage(), e);
|
||||
throw PaymentException.systemError("支付订单创建失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentResponse queryPayment(String orderNo, PaymentType paymentType, Integer tenantId) throws PaymentException {
|
||||
log.info("开始查询支付状态, 支付类型: {}, 租户ID: {}, 订单号: {}",
|
||||
@@ -473,4 +517,105 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
throw PaymentException.paramError("租户ID不能为空且必须大于0");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证支付与订单创建请求参数
|
||||
*/
|
||||
private void validatePaymentWithOrderRequest(PaymentWithOrderRequest request, User loginUser) throws PaymentException {
|
||||
if (request == null) {
|
||||
throw PaymentException.paramError("请求参数不能为空");
|
||||
}
|
||||
if (loginUser == null) {
|
||||
throw PaymentException.paramError("用户未登录");
|
||||
}
|
||||
if (request.getPaymentType() == null) {
|
||||
throw PaymentException.paramError("支付类型不能为空");
|
||||
}
|
||||
if (request.getAmount() == null || request.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
throw PaymentException.amountError("支付金额必须大于0");
|
||||
}
|
||||
if (!StringUtils.hasText(request.getSubject())) {
|
||||
throw PaymentException.paramError("订单标题不能为空");
|
||||
}
|
||||
if (request.getTenantId() == null || request.getTenantId() <= 0) {
|
||||
throw PaymentException.paramError("租户ID不能为空且必须大于0");
|
||||
}
|
||||
if (request.getOrderInfo() == null) {
|
||||
throw PaymentException.paramError("订单信息不能为空");
|
||||
}
|
||||
if (request.getOrderInfo().getGoodsItems() == null || request.getOrderInfo().getGoodsItems().isEmpty()) {
|
||||
throw PaymentException.paramError("订单商品列表不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为订单创建请求
|
||||
*/
|
||||
private OrderCreateRequest convertToOrderCreateRequest(PaymentWithOrderRequest request, User loginUser) {
|
||||
OrderCreateRequest orderRequest = new OrderCreateRequest();
|
||||
|
||||
// 设置基本信息
|
||||
orderRequest.setType(request.getOrderInfo().getType());
|
||||
orderRequest.setTitle(request.getSubject());
|
||||
orderRequest.setComments(request.getOrderInfo().getComments());
|
||||
orderRequest.setTenantId(request.getTenantId());
|
||||
|
||||
// 设置收货信息
|
||||
orderRequest.setRealName(request.getOrderInfo().getRealName());
|
||||
orderRequest.setAddress(request.getOrderInfo().getAddress());
|
||||
orderRequest.setAddressId(request.getOrderInfo().getAddressId());
|
||||
orderRequest.setDeliveryType(request.getOrderInfo().getDeliveryType());
|
||||
|
||||
// 设置商户信息
|
||||
orderRequest.setMerchantId(request.getOrderInfo().getMerchantId());
|
||||
orderRequest.setMerchantName(request.getOrderInfo().getMerchantName());
|
||||
|
||||
// 设置支付信息
|
||||
orderRequest.setPayType(request.getPaymentType().getCode());
|
||||
orderRequest.setTotalPrice(request.getAmount());
|
||||
orderRequest.setPayPrice(request.getAmount());
|
||||
|
||||
// 设置优惠券
|
||||
orderRequest.setCouponId(request.getOrderInfo().getCouponId());
|
||||
|
||||
// 转换商品列表
|
||||
List<OrderCreateRequest.OrderGoodsItem> goodsItems = request.getOrderInfo().getGoodsItems().stream()
|
||||
.map(this::convertToOrderGoodsItem)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
orderRequest.setGoodsItems(goodsItems);
|
||||
|
||||
return orderRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换商品项
|
||||
*/
|
||||
private OrderCreateRequest.OrderGoodsItem convertToOrderGoodsItem(PaymentWithOrderRequest.OrderGoodsItem item) {
|
||||
OrderCreateRequest.OrderGoodsItem orderItem = new OrderCreateRequest.OrderGoodsItem();
|
||||
orderItem.setGoodsId(item.getGoodsId());
|
||||
orderItem.setSkuId(item.getSkuId());
|
||||
orderItem.setQuantity(item.getQuantity());
|
||||
orderItem.setSpecInfo(item.getSpecInfo());
|
||||
return orderItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从微信订单信息构建支付响应
|
||||
*/
|
||||
private PaymentResponse buildPaymentResponseFromWxOrder(Map<String, String> wxOrderInfo,
|
||||
PaymentWithOrderRequest request,
|
||||
String orderNo) {
|
||||
PaymentResponse response = PaymentResponse.wechatNative(
|
||||
orderNo,
|
||||
wxOrderInfo.get("codeUrl"),
|
||||
request.getAmount(),
|
||||
request.getTenantId()
|
||||
);
|
||||
|
||||
// 设置额外信息
|
||||
response.setSuccess(true);
|
||||
|
||||
log.debug("构建支付响应成功, 订单号: {}, 二维码URL: {}", orderNo, wxOrderInfo.get("codeUrl"));
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,9 +131,30 @@ public class ShopOrderController extends BaseController {
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
shopOrder.setUserId(loginUser.getUserId());
|
||||
}
|
||||
if (shopOrderService.save(shopOrder)) {
|
||||
return success("添加成功");
|
||||
shopOrder.setOpenid(loginUser.getOpenid());
|
||||
shopOrder.setPayUserId(loginUser.getUserId());
|
||||
if (shopOrder.getOrderNo() == null) {
|
||||
shopOrder.setOrderNo(Long.toString(IdUtil.getSnowflakeNextId()));
|
||||
}
|
||||
if (shopOrder.getComments() == null) {
|
||||
shopOrder.setComments("暂无");
|
||||
}
|
||||
// 微信支付(商品金额不能为0)
|
||||
if (shopOrder.getTotalPrice().compareTo(BigDecimal.ZERO) == 0) {
|
||||
return fail("商品金额不能为0");
|
||||
}
|
||||
// 百色中学项目捐赠金额不能低于20元
|
||||
if (shopOrder.getTenantId().equals(10324) && shopOrder.getTotalPrice().compareTo(new BigDecimal("10")) < 0) {
|
||||
return fail("捐款金额最低不能少于10元,感谢您的爱心捐赠^_^");
|
||||
}
|
||||
// 测试支付
|
||||
if (loginUser.getPhone().equals("13737128880")) {
|
||||
shopOrder.setPrice(new BigDecimal("0.01"));
|
||||
shopOrder.setTotalPrice(new BigDecimal("0.01"));
|
||||
}
|
||||
if (shopOrderService.save(shopOrder)) {
|
||||
return success("下单成功", shopOrderService.createWxOrder(shopOrder));
|
||||
}
|
||||
}
|
||||
return fail("添加失败");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user