教育训练处考试制证系统后端
heheng
2025-01-23 f8b251573040d2762507909a02f274a6cf4d751c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.gkhy.exam.pay.utils;
 
import com.alibaba.fastjson2.JSONObject;
import com.gkhy.exam.pay.entity.PayReqData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
 
/**
 * 缴费相关接口
 */
@Component
@Slf4j
public class PayUtils {
 
//    private final static String appid = "ED76A5F1703540BE977D34780B371FEB";
 
    @Value("${finance.orderUrl}")
    private String orderUrl;
 
    @Value("${finance.payNotifyUrl}")
    private String payNotifyUrl;
 
    @Value("${finance.payQueryUrl}")
    private String payQueryUrl;
 
    @Value("${finance.apiId}")
    private String appId;
 
    public ResultVo sendApiPost(PayReqData payReqData) throws IOException {
 
        //正式
        String proUrl = "http://finpt.xjcz.gov.cn/fs-service/fs-pay/invoice.do";
        //测试
        String testUrl = "http://finpt.xjcz.gov.cn/fs-service-test/fs-pay/invoice.do";
 
        Map<String, String> param = new HashMap<>();
 
        HttpPost httpPost = new HttpPost(orderUrl);
 
        //请求参数转为json格式base64编码
        String reqData = Base64.getEncoder().encodeToString(JSONObject.toJSONString(payReqData).getBytes());
        String mac = appId + "||" + reqData;
        mac = DigestUtils.md5Hex(mac.getBytes());
        param.put("appid", appId);
        param.put("reqdata", reqData);
        param.put("mac", mac);
 
        httpPost.setEntity(assemblyFormEntity(param, "utf-8"));
        HttpClient httpClient = getHttpClient(orderUrl);
        HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
        String stringContent = execute.getStringContent();
        ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
        return resultVo;
    }
 
    private static UrlEncodedFormEntity assemblyFormEntity(Map<String, String> parameters, String charset) {
        List<NameValuePair> formParameters = assemblyParameter(parameters);
        UrlEncodedFormEntity formEntity = null;
        try {
            if (charset != null) {
                formEntity = new UrlEncodedFormEntity(formParameters, charset);
            } else {
                formEntity = new UrlEncodedFormEntity(formParameters);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return formEntity;
    }
 
    private static List<NameValuePair> assemblyParameter(Map<String, String> parameters) {
        List<NameValuePair> allParameter = new ArrayList<>();
        if (parameters != null && !parameters.isEmpty()) {
            for (String name : parameters.keySet()) {
                NameValuePair parameter = new BasicNameValuePair(name, parameters.get(name));
                allParameter.add(parameter);
            }
        }
        return allParameter;
    }
 
    public static HttpClient getHttpClient(String url) {
        // 判断https访问
        if (url.startsWith("https")) {
            return sslClient();
        }
        return HttpClients.createDefault();
 
    }
 
    private static HttpClient sslClient() {
        try {
            // 在调用SSL之前需要重写验证方法,取消检测SSL
            X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
 
                @Override
                public void checkClientTrusted(X509Certificate[] xcs, String str) {
                }
 
                @Override
                public void checkServerTrusted(X509Certificate[] xcs, String str) {
                }
            };
            SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
            ctx.init(null, new TrustManager[]{trustManager}, null);
            SSLConnectionSocketFactory socketFactory =
                    new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
            // 创建Registry
            RequestConfig requestConfig =
                    RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).setExpectContinueEnabled(Boolean.TRUE)
                            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                            .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
            // 创建ConnectionManager,添加Connection配置信息
            PoolingHttpClientConnectionManager connectionManager =
                    new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            CloseableHttpClient closeableHttpClient = HttpClients.custom().setConnectionManager(connectionManager)
                    .setDefaultRequestConfig(requestConfig).build();
            return closeableHttpClient;
        } catch (KeyManagementException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        }
 
    }
 
    private static ResponseHandler<HttpResultVo> getResponseHandler() {
        ResponseHandler<HttpResultVo> responseHandler = new ResponseHandler<HttpResultVo>() {
            @Override
            public HttpResultVo handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
                if (httpResponse == null) {
                    throw new ClientProtocolException("HttpResponse is null");
                }
 
                StatusLine statusLine = httpResponse.getStatusLine();
                HttpEntity httpEntity = httpResponse.getEntity();
                if (statusLine == null) {
                    throw new ClientProtocolException("HttpResponse contains no StatusLine");
                }
                if (statusLine.getStatusCode() != 200) {
                    throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
                }
                if (httpEntity == null) {
                    throw new ClientProtocolException("HttpResponse contains no HttpEntity");
                }
 
                HttpResultVo httpResult = new HttpResultVo();
                httpResult.setStatusCode(statusLine.getStatusCode());
                ContentType contentType = ContentType.getOrDefault(httpEntity);
                httpResult.setContentType(contentType.toString());
                boolean isTextType = isTextType(contentType);
                httpResult.setTextType(isTextType);
                if (isTextType) {
                    httpResult.setStringContent(EntityUtils.toString(httpEntity));
                } else {
                    httpResult.setByteArrayContent(EntityUtils.toByteArray(httpEntity));
                }
 
                httpResult.setHeaders(httpResponse.getAllHeaders());
 
                return httpResult;
            }
        };
        return responseHandler;
    }
 
    private static boolean isTextType(ContentType contentType) {
        if (contentType == null) {
            throw new RuntimeException("ContentType is null");
        }
        if (contentType.getMimeType().startsWith("text")) {
            return true;
        } else if (contentType.getMimeType().startsWith("image")) {
            return false;
        } else if (contentType.getMimeType().startsWith("application")) {
            if (contentType.getMimeType().contains("json") || contentType.getMimeType().contains("xml")) {
                return true;
            } else {
                return false;
            }
        } else if (contentType.getMimeType().startsWith("multipart")) {
            return false;
        } else {
            return true;
        }
    }
 
 
    //缴费结果通知
    public Map<String, String> receive(JSONObject jsonObject) throws IOException {
 
        Map<String, String> params = new HashMap<>();
        JSONObject reqdata = new JSONObject();
 
        Map<String, String> result = (Map<String, String>) jsonObject.get("reqdata");
        String orderNo = result.get("orderNo");
 
        //确认是否成功
        String notarize = affirmPost(orderNo);
        reqdata.put("orderNo", orderNo);
        reqdata.put("status", notarize);
 
        String req = Base64.getEncoder().encodeToString(reqdata.toJSONString().getBytes());
        String mac = appId + "||" + req;
        mac = DigestUtils.md5Hex(mac.getBytes());
        params.put("appid", appId);
        params.put("reqdata", req);
        params.put("mac", mac);
 
        return params;
    }
 
    //缴费结果确认查询
    public String affirmPost(String orderNo) throws IOException {
 
        HashMap<String, String> param = new HashMap<>();
 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderNo", orderNo);
        String reqdata = Base64.getEncoder().encodeToString(jsonObject.toJSONString().getBytes());
        String mac = appId + "||" + reqdata;
        mac = DigestUtils.md5Hex(mac.getBytes());
        param.put("appid", appId);
        param.put("reqdata", reqdata);
        param.put("mac", mac);
 
        HttpPost httppost = new HttpPost(payNotifyUrl);
        httppost.setEntity(assemblyFormEntity(param, "utf-8"));
        HttpClient httpClient = getHttpClient(payNotifyUrl);
        HttpResultVo execute = httpClient.execute(httppost, getResponseHandler());
        String stringContent = execute.getStringContent();
        ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
        log.info("请求结果为:" + resultVo);
        if (resultVo.getRespcode().equals("BUS0000")) {
            return "success";
        }
        return "fail";
    }
 
    //缴费结果查询
    public JSONObject query(String orderNo) throws IOException {
        String proUrl = "http://finpt.xjcz.gov.cn/fs-service/fs-pay/query.do";
        String testUrl = "http://finpt.xjcz.gov.cn/fs-service-test/fs-pay/query.do";
 
        HashMap<String, String> param = new HashMap<>();
 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderNo", orderNo);
        String reqdata = Base64.getEncoder().encodeToString(jsonObject.toJSONString().getBytes());
        String mac = appId + "||" + reqdata;
        mac = DigestUtils.md5Hex(mac.getBytes());
        param.put("appid", appId);
        param.put("reqdata", reqdata);
        param.put("mac", mac);
 
        HttpPost httppost = new HttpPost(payQueryUrl);
        httppost.setEntity(assemblyFormEntity(param, "utf-8"));
        HttpClient httpClient = getHttpClient(payQueryUrl);
        HttpResultVo execute = httpClient.execute(httppost, getResponseHandler());
        String stringContent = execute.getStringContent();
        JSONObject result = JSONObject.parseObject(stringContent);
        log.info("请求结果json为:" + result);
        return result;
    }
}