xin
2025-09-04 d70c240e09a9021824cc9e9f1bed8028a0c194cd
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
package com.oying.modules.hwc.service.impl;
 
import com.oying.modules.hwc.service.CallbackService;
import com.oying.modules.hwc.utils.SignUtil;
import com.oying.modules.hwc.utils.XmlUtils;
import com.oying.modules.security.config.SwiftPassProperties;
import com.oying.modules.sh.domain.vo.OrderResponse;
import com.oying.modules.sh.domain.vo.OrderReturnResponse;
import com.oying.modules.sh.service.OrderReturnService;
import com.oying.modules.sh.service.OrderService;
import com.oying.utils.enums.OrderStatusEnum;
import com.oying.utils.enums.PayStateEnum;
import com.oying.utils.enums.PayTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author xin
 * @description
 * @date 2025/7/8 18:47
 */
@SuppressWarnings({"unchecked", "all"})
@Slf4j
@Service
@RequiredArgsConstructor
public class CallbackServiceImpl implements CallbackService {
 
    private final OrderService orderService;
    private final OrderReturnService returnService;
    private final SwiftPassProperties properties;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void alipayCallback(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        try {
            String resString = XmlUtils.parseRequest(request);
            String respString = "error";
            if (!resString.isEmpty()) {
                map = XmlUtils.toMap(resString.getBytes(), "utf-8");
                String sign_type = map.get("sign_type");
                String reSign = map.get("sign");
                if (map.containsKey("sign")) {
                    OrderResponse order = orderService.getByOrderNum(map.get("out_trade_no"));
                    PayTypeEnum status = PayTypeEnum.find(order.getOrder().getPayType());
                    if (SignUtil.verifySign(reSign, sign_type, map, properties, status)) {
                        log.error("汇旺财支付验证签名错误!:{}", map.toString());
                    } else {
                        if ("0".equals(map.get("status"))) {
                            if ("0".equals(map.get("result_code"))) {
                                //业务处理
                                if (PayStateEnum.SUCCESS.getKey().equals(order.getOrder().getPayState())) {
                                    // 已处理
                                    respString = "success";
                                } else {
                                    PayStateEnum stateEnum = PayStateEnum.NOTPAY;
                                    if ("0".equals(map.get("pay_result"))) {
                                        stateEnum = PayStateEnum.SUCCESS;
                                        orderService.operationLog(order, OrderStatusEnum.TWO, null);
                                    }
                                    orderService.updatePayStatus(map.get("out_trade_no"), stateEnum, map.get("pay_info"), map.get("time_end"));
                                    // 处理成功
                                    respString = "success";
                                }
                            }
                        }
                    }
                }
            }
            response.getWriter().write(respString);
        } catch (DataIntegrityViolationException e) {
            log.error("汇旺财退款回调数据异常失败:{} {}", map.toString(), e.getMessage());
        } catch (DocumentException e) {
            log.error("汇旺财退款回调XML转码失败:{} {}", map.toString(), e.getMessage());
        } catch (IOException e) {
            log.error("汇旺财退款回调写入流失败:{} {}", map.toString(), e.getMessage());
        } catch (Exception e) {
            log.error("汇旺财退款回调失败:{} {}", map.toString(), e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void returnNotify(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        try {
            String resString = XmlUtils.parseRequest(request);
            String respString = "error";
            if (!resString.isEmpty()) {
                map = XmlUtils.toMap(resString.getBytes(), "utf-8");
                String sign_type = map.get("sign_type");
                String reSign = map.get("sign");
                if (map.containsKey("sign")) {
                    OrderReturnResponse order = returnService.getByReturnNum(map.get("out_refund_no"));
                    PayTypeEnum status = PayTypeEnum.find(order.getOrderReturn().getPayType());
                    if (SignUtil.verifySign(reSign, sign_type, map, properties, status)) {
                        log.error("汇旺财退款验证签名错误!:{}", map.toString());
                    } else {
                        if ("0".equals(map.get("status"))) {
                            if ("0".equals(map.get("result_code"))) {
                                // 业务处理
                                returnService.updatePayStatus(map.get("out_refund_no"), map.get("refund_status"),
                                        map.get("refund_time"));
                                // 业务处理
                                respString = "success";
                            }
                        }
                    }
                }
            }
            response.getWriter().write(respString);
        } catch (DataIntegrityViolationException e) {
            log.error("汇旺财退款回调数据异常失败:{} {}", map.toString(), e.getMessage());
        } catch (DocumentException e) {
            log.error("汇旺财退款回调XML转码失败:{} {}", map.toString(), e.getMessage());
        } catch (IOException e) {
            log.error("汇旺财退款回调写入流失败:{} {}", map.toString(), e.getMessage());
        } catch (Exception e) {
            log.error("汇旺财退款回调失败:{} {}", map.toString(), e.getMessage());
        }
    }
}