xuhy
2025-02-10 c9743d30317ed0aaa70a2f6f3649915051ea59b8
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
package com.taxi591.bankapi.service;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.taxi591.bankapi.dto.ChargeBillRequest;
import com.taxi591.bankapi.dto.ChargeBillResponse;
import com.taxi591.bankapi.dto.CovertPayBackResult;
import com.taxi591.bankapi.utils.Base64;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
@Service
@Slf4j
public class BankService {
 
    @Autowired
    SignatureAndVerification signatureAndVerification;
 
    static final String TIMESTAMP_PATTERN = "yyyyMMddHHmmssSSS";
 
    /**
     * 创建银行应答
     * @param request 银行请求对象
     * @param dealResult 是否处理成功
     * @return
     */
    public String createResponse(ChargeBillRequest request,Boolean dealResult){
        ChargeBillResponse response = new ChargeBillResponse(request);
        response.getMessage().getHead().setReturnCode(dealResult?"0000":"1111");
        response.getMessage().getHead().setReturnMessage(dealResult?"处理成功":"处理失败");
        response.getMessage().getInfo().setRefundFlag("false");
        response.getMessage().getHead().setTimeStamp(DateUtils.dateTimeNow(TIMESTAMP_PATTERN));
        if (!dealResult){
            if  (Integer.parseInt(request.getMessage().getInfo().getResendTimes())==3){
                response.getMessage().getInfo().setRefundFlag("true");
            }else{
                response.getMessage().getInfo().setRefundFlag("false");
            }
        }
        String respJSON = JSON.toJSONString(response);
        String sign = signatureAndVerification.signWhithsha1withrsa(respJSON);
        String respStr = null;
        try {
            respStr = sign + "||" + new String(org.apache.commons.codec.binary.Base64.encodeBase64(respJSON.getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
        }
        return respStr;
    }
 
    /**
     * 处理支付回调数据
     * @param httpRequest  http请求对象
     * @param consumer 处理函数
     * @return
     */
    public CovertPayBackResult covertPayCallBack(HttpServletRequest httpRequest, Function<ChargeBillRequest,Boolean> consumer) {
        CovertPayBackResult result = new CovertPayBackResult();
        String requestContent = "";
        try {
            // 接收报文
            requestContent = SignatureAndVerification.getRequestBody(httpRequest).trim();
            String sign = requestContent.substring(0,
                    requestContent.indexOf("||"));;
            String requestBody = requestContent.substring(sign
                    .length() + 2);;
            Pattern p=Pattern.compile("\"");
            Matcher m=p.matcher(requestBody);
            while(m.find()){
                requestBody=requestBody.replace(m.group(), "");
            }
            String request = new String(Base64.decodeFast(requestBody));
            log.info("-----ChargeBillController------------解析完成后的requestBody-------{}" + request);
            ChargeBillRequest chargeBillRequest = JSON.parseObject(request,
                    new TypeReference<ChargeBillRequest>() {
                    });
            if (chargeBillRequest==null){
                log.error("支付回调解析失败:{}",requestContent);
                throw new ServiceException("支付回调解析失败");
            }
            boolean isok = signatureAndVerification.read_cer_and_verify_sign(requestBody,sign);
            if (!isok){
                throw new ServiceException("支付回调验签失败");
            }
            Boolean dealBack = true;
            if (consumer!=null){
                dealBack = consumer.apply(chargeBillRequest);
            }
            result.setResult(chargeBillRequest);
            result.setBack(createResponse(chargeBillRequest,dealBack));
        }catch (ServiceException e){
            result.setBack(e.getMessage());
            throw e;
        }catch (Exception e){
            log.error("解析异常:{}",requestContent,e);
            throw new ServiceException("支付回调解析异常");
        }
        return result;
    }
 
 
    public void covertBillTo(){
 
 
    }
 
 
 
}