xiaoyong931011
2022-08-12 09b9dda6ff7cfb8e195dbf04be6ed7703a3785a3
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
153
154
155
156
157
158
159
package cc.mrbird.febs.pay.controller;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.enumerates.OrderDeliveryStateEnum;
import cc.mrbird.febs.common.enumerates.OrderStatusEnum;
import cc.mrbird.febs.common.properties.XcxProperties;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.common.utils.SpringContextHolder;
import cc.mrbird.febs.mall.entity.MallOrderInfo;
import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper;
import cc.mrbird.febs.pay.model.NotifyData;
import cc.mrbird.febs.pay.model.OrderStateDto;
import cc.mrbird.febs.pay.model.OrderStateMsgVo;
import cc.mrbird.febs.pay.model.WxTemplateData;
import cc.mrbird.febs.pay.service.IPayService;
import cc.mrbird.febs.pay.service.IXcxPayService;
import cc.mrbird.febs.pay.util.PayThreadPool;
import cc.mrbird.febs.pay.util.Signature;
import cc.mrbird.febs.pay.util.Util;
import cc.mrbird.febs.pay.util.WechatConfigure;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.thymeleaf.engine.TemplateData;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping(value = "/api/xcxPay")
public class XcxPayController {
 
    @Autowired
    MallOrderInfoMapper mallOrderInfoMapper;
    @Autowired
    RedisUtils redisUtils;
 
    @Autowired
    private IXcxPayService iXcxPayService;
 
    private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
    /**
     * 微信支付回调接口
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/wxpayCallback")
    public void payCallBack(HttpServletResponse response, HttpServletRequest request) throws IOException {
        log.info("微信支付回调start....");
 
        // 获取输入参数
        String inputLine;
        StringBuffer notityXml = new StringBuffer();
        String resXml = "";
        String orderId = "";
 
        FebsResponse threadResult = new FebsResponse();
        try {
            while ((inputLine = request.getReader().readLine()) != null) {
                notityXml.append(inputLine);
            }
            request.getReader().close();
            log.info("notityXml ---- :{} ", notityXml);
 
 
            // XMl转对象
            Object bb = Util.getObjectFromXML(notityXml.toString(), NotifyData.class);
            NotifyData data = new NotifyData();
            BeanUtils.copyProperties(bb,data);
            log.info("----return_code = {}", data.getReturn_code());
 
            // 返回状态码 SUCCESS/FAIL
            if (WechatConfigure.CODE_SUCCESS.equals(data.getReturn_code())) {
 
                orderId = data.getAttach();
                // 检验订单状态
                MallOrderInfo order = mallOrderInfoMapper.selectById(Long.valueOf(orderId));
 
                // 校验签名
//                String paySecret = WechatConfigure.WECHARPAY_SECRET;
                String paySecret = xcxProperties.getWecharpaySecret();
                if (Signature.checkIsSignValidFromResponseString(notityXml.toString(),paySecret)) {
                    // 校验业务结果
                    if (WechatConfigure.CODE_SUCCESS.equals(data.getResult_code())) {
                        // 返回SUCCESS报文
                        resXml = WechatConfigure.RESULT_XML_SUCCESS;
                        // 支付费用
                        Double total_fee = Double.parseDouble(data.getTotal_fee());
                        // 商户订单号
                        String payNum = data.getOut_trade_no();
 
                        log.info("支付回调关键信息---total_fee:{},payNum:{},orderId:{}", total_fee, payNum, orderId);
                        // 订单ID
                        BigDecimal payMoney = new BigDecimal(total_fee).divide(new BigDecimal(100), 2,
                                RoundingMode.HALF_UP);
 
 
                        if (order != null && OrderStatusEnum.WAIT_PAY.getValue() == order.getStatus()) {
                            log.debug("检查支付金额payMoney={},order.getPayMoney()={}", payMoney, order.getAmount());
                            order.setStatus(OrderStatusEnum.WAIT_SHIPPING.getValue());
                            order.setPayResult("1");
                            order.setPayTime(new Date());
                            order.setDeliveryState(OrderDeliveryStateEnum.DELIVERY_WAIT.getValue());
                            mallOrderInfoMapper.updateById(order);
                            threadResult.success().message("支付成功");
                        } else {
                            log.info("订单状态不为待付款,order status=", order.getStatus());
                        }
                    } else {
                        log.info("微信标识业务是失败");
                        threadResult.fail().message("查询支付信息失败,请联系客服或者刷新支付信息(错误码:001)");
//                        resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败");
                    }
                } else {
                    log.info("无效签名");
                    threadResult.fail().message("查询支付信息失败,请联系客服或者刷新支付信息(错误码:002)");
//                    resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败");
                }
            } else {
                log.info("通信标识失败");
                threadResult.fail().message("查询支付信息失败,请联系客服或者刷新支付信息(错误码:003)");
//                resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "通信标识失败");
            }
        } catch (Exception e) {
            log.error("支付回调签名错误", e);
            threadResult.fail().message("查询支付信息失败,请联系客服或者刷新支付信息(错误码:004)");
//            resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "支付回调签名错误");
        } finally {
            // 通知线程消息
            PayThreadPool.notifyThread(Integer.valueOf(orderId), threadResult);
            sendResultBack(response, resXml);
        }
        return;
 
    }
 
    private void sendResultBack(HttpServletResponse response, String resXml) throws IOException {
        log.info("返回微信数据={}", resXml);
        ServletOutputStream out = response.getOutputStream();
        out.write(resXml.getBytes());
        out.flush();
        out.close();
    }
 
}