Java支付宝订单查询

电脑网站支付成功后可通过支付宝接口主动查询订单结果

前置条件

  1. 该方法基于 Java 支付宝扫码支付 (新) 实现

调用查询接口进行订单查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class AliQueryServiceImpl extends AliCoreServiceImpl {

    public Boolean getPayResult(Long orderId) {
        AlipayTradeQueryRequest queryRequest = new AlipayTradeQueryRequest();

        queryRequest.setBizModel(generateOrderInfo(orderId));

        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(queryRequest);
        } catch (AlipayApiException e) {
            logger.error("支付宝查询订单" + orderId + "失败!", e);
        }

        if (response == null) {
            logger.error("支付宝未获取订单" + orderId + "详情!");
            return false;
        }

        if (response.isSuccess()) {
            if (response.getTradeStatus().equals(TRADE_STATUS_SUCCESS) || response.getTradeStatus().equals(TRADE_STATUS_FINISHED)) {
                // 更新订单状态
                updateOrderInfo(response.getOutTradeNo(), OrderPay.aliPay.getCode());
                return true;
            }

            logger.error("支付宝订单" + orderId + "交易失败,交易状态:" + response.getTradeStatus());
            return false;
        } else {
            logger.error("支付宝订单" + orderId + "查询失败!");
            return false;
        }
    }

    private AlipayTradeQueryModel generateOrderInfo(Long orderId) {
        OrderDTO order = orderManageService.getOrder(orderId);

        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(order.getCode());

        return model;
    }

}