Skip to content

Commit

Permalink
close alipay order after 5min
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteInternetHK committed Jan 4, 2021
1 parent 490715a commit 2e297ee
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -272,10 +267,10 @@ protected Map<String, Object> 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次
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +60,9 @@ public void checkOrderJob() throws AlipayApiException {
List<Order> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,7 +38,7 @@ public interface OrderService extends IService<Order> {

Result getOrder(HttpServletRequest request);

List<Order> getCheckedOrder();
List<Order> getCheckedOrder() throws AlipayApiException;

BigDecimal getMonthIncome();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +37,7 @@
* @Date: 2020-10-07 21:14
*/
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

@Autowired
Expand All @@ -40,6 +46,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
@Autowired
private UserService userService;

@Autowired
private Alipay alipay;

@Override
public Order getCurrentPlan(Integer userId) {
QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
Expand Down Expand Up @@ -205,13 +214,25 @@ public Result getOrder(HttpServletRequest request) {
}

@Override
public List<Order> getCheckedOrder() {
public List<Order> getCheckedOrder() throws AlipayApiException {
Date now = new Date();
// 关闭5分钟前的订单
// 查5分钟前的订单
QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
orderQueryWrapper.lt("create_time", DateUtil.offsetMinute(now, -5)).in("status", 0, 2);
List<Order> 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<Order> orderUpdateWrapper = new UpdateWrapper<>();
orderUpdateWrapper.set("status", 2).lt("create_time", DateUtil.offsetMinute(now, -5)).eq("status", 0);
this.update(orderUpdateWrapper);
QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
// 返回需要查询的订单
orderQueryWrapper = new QueryWrapper<>();
orderQueryWrapper
.eq("`status`", 0)
.or()
Expand Down

0 comments on commit 2e297ee

Please sign in to comment.