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 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 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; } }