diff --git a/src/main/java/project/daihao18/panel/common/payment/alipay/Alipay.java b/src/main/java/project/daihao18/panel/common/payment/alipay/Alipay.java index 2e350d6..d6104d2 100644 --- a/src/main/java/project/daihao18/panel/common/payment/alipay/Alipay.java +++ b/src/main/java/project/daihao18/panel/common/payment/alipay/Alipay.java @@ -4,15 +4,10 @@ import cn.hutool.json.JSONUtil; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; -import com.alipay.api.domain.AlipayTradePagePayModel; -import com.alipay.api.domain.AlipayTradePrecreateModel; -import com.alipay.api.domain.AlipayTradeQueryModel; -import com.alipay.api.domain.AlipayTradeWapPayModel; +import com.alipay.api.domain.*; import com.alipay.api.internal.util.AlipayLogger; -import com.alipay.api.request.AlipayTradePagePayRequest; -import com.alipay.api.request.AlipayTradePrecreateRequest; -import com.alipay.api.request.AlipayTradeQueryRequest; -import com.alipay.api.request.AlipayTradeWapPayRequest; +import com.alipay.api.request.*; +import com.alipay.api.response.AlipayTradeCloseResponse; import com.alipay.api.response.AlipayTradeQueryResponse; import com.ijpay.alipay.AliPayApiConfig; import com.ijpay.alipay.AliPayApiConfigKit; @@ -272,10 +267,10 @@ protected Map createByF2F(CommonOrder order, String subject, Boo * @return * @throws AlipayApiException */ - public AlipayTradeQueryResponse query(Order order) throws AlipayApiException { + public AlipayTradeQueryResponse query(CommonOrder order) throws AlipayApiException { AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); AlipayTradeQueryModel model = new AlipayTradeQueryModel(); - model.setOutTradeNo(order.getOrderId() + "_0"); + model.setOutTradeNo(order.getId() + "_0"); request.setBizModel(model); // 设置alipayClient // 查2次 @@ -288,7 +283,7 @@ public AlipayTradeQueryResponse query(Order order) throws AlipayApiException { if (!"40004".equals(execute.getCode())) { return execute; } else { - model.setOutTradeNo(order.getOrderId() + "_1"); + model.setOutTradeNo(order.getId() + "_1"); request.setBizModel(model); if (isCertMode) { execute = alipayClient.certificateExecute(request); @@ -301,4 +296,22 @@ public AlipayTradeQueryResponse query(Order order) throws AlipayApiException { } return null; } + + /** + * 关闭订单 + * @param order + * @return + */ + public AlipayTradeCloseResponse close(CommonOrder order) throws AlipayApiException { + AlipayTradeCloseRequest request = new AlipayTradeCloseRequest(); + AlipayTradeCloseModel model = new AlipayTradeCloseModel(); + model.setOutTradeNo(order.getId()); + request.setBizModel(model); + // 设置alipayClient + if (isCertMode) { + return alipayClient.certificateExecute(request); + } else { + return alipayClient.execute(request); + } + } } \ No newline at end of file diff --git a/src/main/java/project/daihao18/panel/common/schedule/tasks/CheckOrderJobTaskService.java b/src/main/java/project/daihao18/panel/common/schedule/tasks/CheckOrderJobTaskService.java index f69feb4..d5e199e 100644 --- a/src/main/java/project/daihao18/panel/common/schedule/tasks/CheckOrderJobTaskService.java +++ b/src/main/java/project/daihao18/panel/common/schedule/tasks/CheckOrderJobTaskService.java @@ -11,6 +11,7 @@ import project.daihao18.panel.common.enums.PayStatusEnum; import project.daihao18.panel.common.payment.alipay.Alipay; import project.daihao18.panel.common.utils.NotifyLockUtil; +import project.daihao18.panel.entity.CommonOrder; import project.daihao18.panel.entity.Funds; import project.daihao18.panel.entity.Order; import project.daihao18.panel.entity.User; @@ -59,7 +60,9 @@ public void checkOrderJob() throws AlipayApiException { List orders = orderService.getCheckedOrder(); for (Order order : orders) { // log.info("{}", order); - AlipayTradeQueryResponse response = alipay.query(order); + CommonOrder commonOrder = new CommonOrder(); + commonOrder.setId(order.getOrderId()); + AlipayTradeQueryResponse response = alipay.query(commonOrder); if (ObjectUtil.isEmpty(response)) { continue; } diff --git a/src/main/java/project/daihao18/panel/service/OrderService.java b/src/main/java/project/daihao18/panel/service/OrderService.java index 1c09669..9289a50 100644 --- a/src/main/java/project/daihao18/panel/service/OrderService.java +++ b/src/main/java/project/daihao18/panel/service/OrderService.java @@ -1,5 +1,6 @@ package project.daihao18.panel.service; +import com.alipay.api.AlipayApiException; import com.baomidou.mybatisplus.extension.service.IService; import project.daihao18.panel.common.response.Result; import project.daihao18.panel.entity.Order; @@ -37,7 +38,7 @@ public interface OrderService extends IService { Result getOrder(HttpServletRequest request); - List getCheckedOrder(); + List getCheckedOrder() throws AlipayApiException; BigDecimal getMonthIncome(); diff --git a/src/main/java/project/daihao18/panel/serviceImpl/OrderServiceImpl.java b/src/main/java/project/daihao18/panel/serviceImpl/OrderServiceImpl.java index 6ff78d5..0174406 100644 --- a/src/main/java/project/daihao18/panel/serviceImpl/OrderServiceImpl.java +++ b/src/main/java/project/daihao18/panel/serviceImpl/OrderServiceImpl.java @@ -3,16 +3,21 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.json.JSONUtil; +import com.alipay.api.AlipayApiException; +import com.alipay.api.response.AlipayTradeCloseResponse; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import project.daihao18.panel.common.enums.PayStatusEnum; +import project.daihao18.panel.common.payment.alipay.Alipay; import project.daihao18.panel.common.response.Result; +import project.daihao18.panel.entity.CommonOrder; import project.daihao18.panel.entity.Order; import project.daihao18.panel.entity.User; import project.daihao18.panel.mapper.OrderMapper; @@ -32,6 +37,7 @@ * @Date: 2020-10-07 21:14 */ @Service +@Slf4j public class OrderServiceImpl extends ServiceImpl implements OrderService { @Autowired @@ -40,6 +46,9 @@ public class OrderServiceImpl extends ServiceImpl implements @Autowired private UserService userService; + @Autowired + private Alipay alipay; + @Override public Order getCurrentPlan(Integer userId) { QueryWrapper orderQueryWrapper = new QueryWrapper<>(); @@ -205,13 +214,25 @@ public Result getOrder(HttpServletRequest request) { } @Override - public List getCheckedOrder() { + public List getCheckedOrder() throws AlipayApiException { Date now = new Date(); - // 关闭5分钟前的订单 + // 查5分钟前的订单 + QueryWrapper orderQueryWrapper = new QueryWrapper<>(); + orderQueryWrapper.lt("create_time", DateUtil.offsetMinute(now, -5)).in("status", 0, 2); + List orders = this.list(orderQueryWrapper); + for (Order order : orders) { + // 关闭支付宝订单 + CommonOrder commonOrder = new CommonOrder(); + commonOrder.setId(order.getOrderId()); + AlipayTradeCloseResponse close = alipay.close(commonOrder); + log.debug("closeResponse: {}", close.toString()); + } + // 关闭本地订单 UpdateWrapper orderUpdateWrapper = new UpdateWrapper<>(); orderUpdateWrapper.set("status", 2).lt("create_time", DateUtil.offsetMinute(now, -5)).eq("status", 0); this.update(orderUpdateWrapper); - QueryWrapper orderQueryWrapper = new QueryWrapper<>(); + // 返回需要查询的订单 + orderQueryWrapper = new QueryWrapper<>(); orderQueryWrapper .eq("`status`", 0) .or()