xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.nuvole.hnnx.orderQueryTask;
 
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nuvole.common.domain.emnu.CommonResultEmnu;
import com.nuvole.common.domain.result.CommonResult;
import com.nuvole.hnnx.conf.NxPayConfig;
import com.nuvole.hnnx.hnnxPay.NXPayService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
 
import java.util.Date;
 
/**
 * @ClassName OrderQueryTask
 * @Author cy
 * @Date 2024/1/2
 * @Description
 * @Version 1.0
 **/
@Slf4j
//@Component
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OrderQueryTask implements Runnable {
//    @Autowired
    private ApplicationEventPublisher eventPublisher;
//    @Autowired
    private NXPayService nxPayService;
//    @Autowired
    private NxPayConfig nxPayConfig;
 
    @ApiModelProperty("订单ID")
    private String orderId;
 
    @ApiModelProperty("支付服务商")
    private Short payService;
 
    @ApiModelProperty("农信需要区分是否为快捷支付 1 普通支付 2快捷支付")
    private Short payType;
 
    @ApiModelProperty("订单尝试支付时间")
    private Date orderPayTime;
 
 
    @Override
    public void run() {
        Date nowTime = new Date();
        long lastDuration = nowTime.getTime() - orderPayTime.getTime();
        if (lastDuration > 10 * 60 * 1000) {
            log.error("10钟前的订单,取消任务:{}", orderId);
            OrderQueryUtil.cancelTask(orderId);
            return;
        }
        log.info("查询订单为{}, 订单初始支付时间为{}", orderId, orderPayTime);
        boolean isPaySuccess = false;
 
        String[] orderInfoArr = orderId.split("-");
        String orderChannel = orderInfoArr[0];
        String myOrderId = orderInfoArr[1];
        Short tryPayNum = Convert.toShort(orderInfoArr[3],null);
        String outTradeNo = null;
 
        if (payService == 7) {
            if (payType == 1) {
                JSONObject payConfig = new JSONObject();
                payConfig.put("branchId", nxPayConfig.getBranchId());
                CommonResult<JSONObject> tradeQueryResult = nxPayService.tradeQuery(payConfig, orderId);
                // TransStatus 0    交易成功 1    交易失败 2    撤销 3    部分退货 4    全部退货 5    处理中 9    交易超时
                if (!tradeQueryResult.getCode().equals(CommonResultEmnu.OK.getCode())) {
                    OrderQueryUtil.cancelTask(orderId);
                    return;
                }
                JSONObject tradeQueryResultJson = tradeQueryResult.getData();
                JSONObject payload = tradeQueryResultJson.getJSONObject("payload");
                Integer transStatus = payload.getInteger("TransStatus");
                if (transStatus != null) {
                    if (transStatus == 0) {
                        isPaySuccess = true;
                        outTradeNo = payload.getString("TransSeqNbr");
                    } else if (transStatus == 1 || transStatus == 9) {
                        log.error("交易失败 或 交易超时,取消任务.{}", orderId);
                        OrderQueryUtil.cancelTask(orderId);
                    }
                }
            } else if (payType == 2) {
                //orderStatus   0-待支付 1-成功 2-失败 3-处理中 4-订单取消
                CommonResult<JSONArray> tradeQueryResult = nxPayService.kjTradeQuery(orderId, null, null, 1, 1);
                if (!tradeQueryResult.getCode().equals(CommonResultEmnu.OK.getCode())) {
                    OrderQueryUtil.cancelTask(orderId);
                    return;
                }
                JSONArray tradeQueryResultJsonArr = tradeQueryResult.getData();
                if (tradeQueryResultJsonArr != null && tradeQueryResultJsonArr.size() > 0){
                    JSONObject tradeQueryResultJson = (JSONObject) tradeQueryResultJsonArr.get(0);
                    Integer transStatus = tradeQueryResultJson.getInteger("orderStatus");
                    if (transStatus != null) {
                        if (transStatus == 1) {
                            isPaySuccess = true;
                            outTradeNo = tradeQueryResultJson.getString("TransSeqNbr");
                        } else if (transStatus == 2 || transStatus == 4) {
                            log.error("交易失败 或 订单取消,取消任务.{}", orderId);
                            OrderQueryUtil.cancelTask(orderId);
                        }
                    }
                }
 
 
            } else {
                log.error("支付方式错误,取消任务.{}", orderId);
                OrderQueryUtil.cancelTask(orderId);
            }
        } else {
            log.error("目前除农信外,其他暂不查询");
            OrderQueryUtil.cancelTask(orderId);
        }
        // 判断支付状态,确定是否发送通知
        if (isPaySuccess) {
            eventPublisher.publishEvent(new OrderQueryEvent(this,
                    OrderQueryMessageEntity.builder().code("10000").orderChannel(orderChannel).orderId(myOrderId).outTradeNo(outTradeNo).payService(payService).tryPayNum(tryPayNum).build()));
            OrderQueryUtil.cancelTask(orderId);
        }
 
    }
 
    public OrderQueryTask(ApplicationEventPublisher eventPublisher, NXPayService nxPayService, NxPayConfig nxPayConfig, String orderId, Short payService, Short payType, Date orderPayTime) {
        this.eventPublisher = eventPublisher;
        this.nxPayService = nxPayService;
        this.nxPayConfig = nxPayConfig;
        this.orderId = orderId;
        this.payService = payService;
        this.payType = payType;
        this.orderPayTime = orderPayTime;
    }
 
    public String getOrderId() {
        return orderId;
    }
 
    public Short getPayService() {
        return payService;
    }
 
    public Short getPayType() {
        return payType;
    }
 
    public Date getOrderPayTime() {
        return orderPayTime;
    }
}