增加支付订单接口以及取消订单接口

This commit is contained in:
2025-08-23 00:18:11 +08:00
parent d3fa230dd7
commit 94577c2f34
6 changed files with 172 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sczx.order.common.Result;
import com.sczx.order.dto.*;
import com.sczx.order.service.OrderService;
import com.sczx.order.thirdpart.dto.UnifiedPaymentInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
@ -50,6 +51,19 @@ public class ClientOrderController {
return Result.ok(orderService.reRentalCarOrder(rentCarOrderReq));
}
@ApiOperation(value = "取消订单")
@PostMapping("/cancelOrder")
public Result<Boolean> cancelOrder(@Valid @RequestBody PayOrderReq payOrderReq){
orderService.cancelOrder(payOrderReq);
return Result.ok(true);
}
@ApiOperation(value = "支付订单")
@PostMapping("/payOrder")
public Result<UnifiedPaymentInfoDTO> payOrder(@Valid @RequestBody PayOrderReq payOrderReq){
return Result.ok(orderService.payOrder(payOrderReq));
}
@ApiOperation(value = "逾期处理")
@PostMapping("/overDueRentalCarOrder")
public Result<RentCarOrderResultDTO> overDueRentalCarOrder(@Valid @RequestBody ReRentCarReq rentCarOrderReq){

View File

@ -0,0 +1,15 @@
package com.sczx.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value = "PayOrderReq对象", description = "支付订单请求对象")
@Data
public class PayOrderReq {
@ApiModelProperty(value = "订单编号")
private String orderNo;
@ApiModelProperty(value = "用户微信或支付宝id")
private String userOpenId;
}

View File

@ -3,9 +3,18 @@ package com.sczx.order.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sczx.order.dto.*;
import com.sczx.order.po.OrderMainPO;
import com.sczx.order.thirdpart.dto.UnifiedPaymentInfoDTO;
public interface OrderService {
/**
* 根据订单号查询订单信息
* @param orderNo
* @return
*/
OrderMainPO queryOrderMainPoByOrderNo(String orderNo, String delFlag);
/**
* 提交租车订单
* @param rentCarOrderReq
@ -20,6 +29,19 @@ public interface OrderService {
*/
RentCarOrderResultDTO reRentalCarOrder(ReRentCarReq rentCarOrderReq);
/**
* 支付订单
* @param payOrderReq
* @return
*/
UnifiedPaymentInfoDTO payOrder(PayOrderReq payOrderReq);
/**
* 取消订单
* @param payOrderReq
*/
void cancelOrder(PayOrderReq payOrderReq);
/**
* 逾期处理
* @param rentCarOrderReq

View File

@ -67,6 +67,14 @@ public class OrderServiceImpl implements OrderService {
@Autowired
private PayInteg payInteg;
@Override
public OrderMainPO queryOrderMainPoByOrderNo(String orderNo, String delFlag) {
LambdaQueryWrapper<OrderMainPO> currentOrderWrapper = new LambdaQueryWrapper<>();
currentOrderWrapper.eq(OrderMainPO::getOrderNo, orderNo)
.eq(OrderMainPO::getDelFlag, delFlag).last(" limit 1");
return orderMainRepo.getOne(currentOrderWrapper);
}
@Transactional(rollbackFor = Exception.class)
@Override
public RentCarOrderResultDTO submitRentCarOrder(RentCarOrderReq rentCarOrderReq) {
@ -313,6 +321,82 @@ public class OrderServiceImpl implements OrderService {
}
@Override
public UnifiedPaymentInfoDTO payOrder(PayOrderReq payOrderReq) {
OrderMainPO orderMainPO = queryOrderMainPoByOrderNo(payOrderReq.getOrderNo(),"0");
if(!OrderStatusEnum.WAIT_PAY.getCode().equalsIgnoreCase(orderMainPO.getOrderStatus())&&OrderStatusEnum.RERENT_WAIT_PAY.getCode().equalsIgnoreCase(orderMainPO.getOrderStatus())){
throw new BizException("订单非待支付状态");
}
LambdaQueryWrapper<OrderSubPO> orderSubQueryWrapper = new LambdaQueryWrapper<>();
orderSubQueryWrapper.eq(OrderSubPO::getOrderId, orderMainPO.getOrderId());
orderSubQueryWrapper.eq(OrderSubPO::getPayStatus, PayStatusEnum.USERPAYING.getCode());
List<OrderSubPO> orderSubPOList = orderSubRepo.list(orderSubQueryWrapper);
if(orderSubPOList.isEmpty()){
throw new InnerException("待支付子订单不存在");
}
//获取门店信息
CompanyStoreDTO companyStoreDTO = storeInteg.getStoreById(Integer.valueOf(orderMainPO.getStoreId().toString()));
CarModelSimpleDTO carModelSimpleDTO = carInteg.getCarModelByModelId(orderMainPO.getCarModelId());
RentCarRuleDTO rentCarRuleDTO = carInteg.getRentCarRuleByCarRuleId(orderMainPO.getRentCarRuleId());
SimpleUserInfoDTO userInfoDTO = jwtUtil.getUserInfoFromToken();
OrderSubPO orderSubPO = orderSubPOList.get(0);
UnifiedPaymentInfoDTO unifiedPaymentInfoDTO = null;
//发起支付
if(StringUtils.equalsIgnoreCase(orderSubPO.getPaymentMethod(), PaymentTypeEnum.WX_PAY.getCode())){
PaymentRequest paymentRequest = new PaymentRequest();
paymentRequest.setCompanyId(Long.valueOf(companyStoreDTO.getOperatingCompanyId()));
paymentRequest.setOutTradeNo(orderSubPO.getPaymentId());
paymentRequest.setOpenId(userInfoDTO.getWechatOpenid());
paymentRequest.setAttach(companyStoreDTO.getOperatingCompanyId().toString());
paymentRequest.setSpbillCreateIp("127.0.0.1");
paymentRequest.setBody(rentCarRuleDTO.getRuleName());
// BigDecimal orderAmount = orderSubPOList.stream().map(OrderSubPO::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
// paymentRequest.setTotalFee(orderAmount.multiply(new BigDecimal(100)).intValue());
paymentRequest.setTotalFee(10);
unifiedPaymentInfoDTO = payInteg.unifiedOrder(paymentRequest);
// TODO 其他支付类型
}
return unifiedPaymentInfoDTO;
}
@Override
public void cancelOrder(PayOrderReq payOrderReq) {
log.info("开始取消订单");
OrderMainPO orderMainPO = queryOrderMainPoByOrderNo(payOrderReq.getOrderNo(), "0");
if(Objects.nonNull(orderMainPO)){
LambdaQueryWrapper<OrderSubPO> orderSubQueryWrapper = new LambdaQueryWrapper<>();
orderSubQueryWrapper.eq(OrderSubPO::getOrderId, orderMainPO.getOrderId());
orderSubQueryWrapper.eq(OrderSubPO::getPayStatus, PayStatusEnum.USERPAYING.getCode());
orderSubQueryWrapper.orderByDesc(OrderSubPO::getCreatedAt).last(" limit 1");
OrderSubPO orderSubPO = orderSubRepo.getOne(orderSubQueryWrapper);
boolean closePayOrder = false;
//获取门店信息
CompanyStoreDTO companyStoreDTO = storeInteg.getStoreById(Integer.valueOf(orderMainPO.getStoreId().toString()));
//关闭支付单
if(StringUtils.equalsIgnoreCase(orderSubPO.getPaymentMethod(), PaymentTypeEnum.WX_PAY.getCode())){
log.info("开始关闭支付单");
closePayOrder = payInteg.closeOrder(Long.valueOf(companyStoreDTO.getOperatingCompanyId()), orderSubPO.getPaymentId());
}
if(closePayOrder){
log.info("开始逻辑删除订单");
//逻辑删除订单
LambdaUpdateWrapper<OrderMainPO> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(OrderMainPO::getDelFlag, "2");
updateWrapper.eq(OrderMainPO::getOrderId, orderMainPO.getOrderId());
orderMainRepo.update(updateWrapper);
LambdaUpdateWrapper<OrderSubPO> updateSubWrapper = new LambdaUpdateWrapper<>();
updateSubWrapper.set(OrderSubPO::getDelFlag, "2");
updateSubWrapper.eq(OrderSubPO::getOrderId, orderMainPO.getOrderId());
orderSubRepo.update(updateSubWrapper);
}
}
}
@Transactional(rollbackFor = Exception.class)
@Override

View File

@ -44,4 +44,13 @@ public interface PayFacade {
*/
@GetMapping("/api/payment/refundQuery/{companyId}")
Map<String, String> refundQuery(@PathVariable Long companyId, @RequestParam String outTradeNo);
/**
* 关闭订单
* @param companyId
* @param outTradeNo
* @return
*/
@PostMapping("/api/payment/close/{companyId}/{outTradeNo}")
Map<String, String> closeOrder(@PathVariable Long companyId, @PathVariable String outTradeNo);
}

View File

@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Objects;
@Slf4j
@Component
@ -106,4 +107,31 @@ public class PayInteg {
throw new InnerException("查询退款失败");
}
}
/**
* 关闭支付单
* @param companyId
* @param outTradeNo
* @return
*/
public Boolean closeOrder(Long companyId, String outTradeNo){
try {
Map<String, String> result = payFacade.closeOrder(companyId, outTradeNo);
if(Objects.isNull(result)){
return true;
} else {
String returnCode = result.get("return_code");
if(StringUtils.equalsIgnoreCase(returnCode, "FAIL")){
throw new InnerException("关闭订单失败");
}else if(Objects.nonNull(result.get("code"))){
throw new InnerException("关闭订单失败");
}
}
} catch (Exception e){
log.error("关闭订单失败",e);
throw new InnerException("关闭订单失败");
}
return false;
}
}