puzhibing
2024-08-17 1b43670626f48266efb898ec7cd7deedcab9ba10
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.ruoyi.payment.wx.utils;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.ruoyi.common.core.utils.WebUtils;
import com.ruoyi.payment.wx.model.WxPaymentInfoModel;
import com.ruoyi.payment.wx.model.WxPaymentRefundModel;
import com.ruoyi.payment.wx.resp.NotifyV3PayDecodeRespBody;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import com.wechat.pay.contrib.apache.httpclient.notification.Notification;
import com.wechat.pay.contrib.apache.httpclient.notification.NotificationHandler;
import com.wechat.pay.contrib.apache.httpclient.notification.NotificationRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.*;
 
/**
 * @author xiaochen
 * @ClassName WxWifiV3Pay
 * @Description
 * @date 2021-11-13 21:10
 */
@Slf4j
public abstract class WxAbstractPay {
    /**
     * 请求成功相应码
     */
    private static final int STATUS_CODE = 200;
    /**
     * 请求成功相应码
     */
    private static final int OTHER_STATUS_CODE = 204;
    /**
     * 请求根地址
     */
    private static final String HOST = "https://api.mch.weixin.qq.com";
 
    private static RuntimeException parameterError(String message, Object... args) {
        message = String.format(message, args);
        return new IllegalArgumentException("parameter error: " + message);
    }
 
    /**
     * 封装基础数据
     *
     * @param requestBody
     * @param notifyUrl
     * @return
     */
    protected String buildBaseParam(WxPaymentInfoModel requestBody, String notifyUrl) {
        // 封装基础数据
        requestBody.setNotify_url(notifyUrl);
        String reqBody = WxJsonUtils.toJsonString(requestBody);
        return reqBody;
    }
 
    /**
     * 微信调起支付参数
     * 返回参数如有不理解 请访问微信官方文档
     * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_1_4.shtml
     *
     * @param prepayId 微信下单返回的prepay_id
     * @param appId    应用ID(appid)
     * @return 当前调起支付所需的参数
     * @throws Exception
     */
    protected Map<String, Object> wxTuneUp(PrivateKeySigner privateKeySigner, String appId, String prepayId) {
        String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
        String nonceStr = WxUtils.generateNonceStr();
        String packageStr = "prepay_id=" + prepayId;
        //加载签名
        String signStr = Stream.of(appId, timeStamp, nonceStr, packageStr).collect(Collectors.joining("\n", "", "\n"));
        String packageSign = privateKeySigner.sign(signStr.getBytes(StandardCharsets.UTF_8)).getSign();
        Map<String, Object> map = new HashMap<>(6);
        map.put("appId", appId);
        map.put("timeStamp", timeStamp);
        map.put("nonceStr", nonceStr);
        map.put("package", packageStr);
        map.put("signType", "RSA");
        map.put("paySign", packageSign);
        return map;
    }
 
    /**
     * 构建方法请求
     *
     * @param uri
     * @param socketTimeout
     * @param connectTimeout
     * @return
     */
    protected HttpGet requestGet(String uri, int socketTimeout, int connectTimeout) {
        //请求URL
        HttpGet httpGet = new HttpGet(HOST + uri);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpGet.setConfig(requestConfig);
        httpGet.setHeader("Content-type", "application/json");
        httpGet.setHeader("Accept", "application/json");
        return httpGet;
    }
 
    /**
     * 构建方法请求
     *
     * @param uri
     * @param socketTimeout
     * @param connectTimeout
     * @param reqdata
     * @return
     */
    protected HttpPost requestPost(String uri, int socketTimeout, int connectTimeout, String reqdata) {
        //请求URL
        HttpPost httpPost = new HttpPost(HOST + uri);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);
        StringEntity entity = new StringEntity(reqdata, StandardCharsets.UTF_8);
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        return httpPost;
    }
 
    public abstract <T> T verifyNotify(HttpServletRequest request, TypeReference<T> valueTypeRef) throws Exception;
 
    /**
     * 接收回调
     *
     * @param request
     * @return
     * @throws Exception
     */
    public <T> T verifyNotify(HttpServletRequest request, Verifier verifier, String apiKey, TypeReference<T> valueTypeRef) throws Exception {
        String body = WxUtils.streamBodyByReceive(request);
        String requestId = request.getHeader(REQUEST_ID);
        String[] headers = {WECHAT_PAY_SERIAL, WECHAT_PAY_SIGNATURE, WECHAT_PAY_NONCE, WECHAT_PAY_TIMESTAMP};
        String value;
        // 验签必须参数检验
        for (String headerName : headers) {
            value = request.getHeader(headerName);
            if (value == null || "".equals(value)) {
                throw parameterError("empty [%s], request-id=[%s]", headerName, requestId);
            }
        }
        String serial = request.getHeader(WECHAT_PAY_SERIAL);
        String signature = request.getHeader(WECHAT_PAY_SIGNATURE);
        String timestamp = request.getHeader(WECHAT_PAY_TIMESTAMP);
        String nonce = request.getHeader(WECHAT_PAY_NONCE);
        // 构建request,传入必要参数
        NotificationRequest notificationRequest = new NotificationRequest.Builder().withSerialNumber(serial)
                .withNonce(nonce)
                .withTimestamp(timestamp)
                .withSignature(signature)
                .withBody(body)
                .build();
        NotificationHandler handler = new NotificationHandler(verifier, apiKey.getBytes(StandardCharsets.UTF_8));
        // 验签和解析请求体
        Notification notification = handler.parse(notificationRequest);
        assert notification != null;
        T respBody = WxJsonUtils.parseObject(notification.getDecryptData(), valueTypeRef);
        return respBody;
    }
 
    /**
     * 订单查询
     *
     * @param httpClient
     * @param socketTimeout
     * @param connectTimeout
     * @param url
     * @return com.abl.biz.center.payment.wx.v3.NotifyV3PayDecodeRespBody
     * @author xiaochen
     * @date 2021-12-20 17:12
     */
    protected NotifyV3PayDecodeRespBody query(CloseableHttpClient httpClient, int socketTimeout, int connectTimeout, String url) {
        //请求URL
        HttpGet httpGet = requestGet(
                url
                , socketTimeout
                , connectTimeout);
        String repBody = result(httpClient, httpGet);
        NotifyV3PayDecodeRespBody body = WxJsonUtils.parseObject(repBody, NotifyV3PayDecodeRespBody.class);
        return body;
    }
 
    /**
     * 子级实现
     *
     * @param out_trade_no
     * @param mchid
     * @return
     */
    public abstract NotifyV3PayDecodeRespBody query(String out_trade_no, String mchid);
 
 
    /**
     * 订单退款
     *
     * @param refundModel
     * @return
     */
    public abstract Map<String, Object> refund(WxPaymentRefundModel refundModel);
 
    /**
     * 订单退款
     *
     * @param httpClient
     * @param uri
     * @param httpReadTimeoutMs
     * @param httpConnectTimeoutMs
     * @param refundModel
     * @return
     */
    public Map<String, Object> refund(CloseableHttpClient httpClient,
                                      String uri,
                                      int httpReadTimeoutMs,
                                      int httpConnectTimeoutMs,
                                      WxPaymentRefundModel refundModel) {
        String reqBody = WxJsonUtils.toJsonString(refundModel);
        //请求URL
        HttpEntityEnclosingRequestBase httpPost = requestPost(
                uri
                , httpReadTimeoutMs
                , httpConnectTimeoutMs, reqBody);
        String repBody = result(httpClient, httpPost);
        Map<String, Object> body = WxJsonUtils.parseObject(repBody, Map.class);
        return body;
    }
 
 
    /**
     * 请求结果
     *
     * @param request
     * @return
     */
    protected String result(CloseableHttpClient httpClient, HttpRequestBase request) {
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            String respBodyStr = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            if (WxUtils.getLogger().isDebugEnabled()) {
                WxUtils.debug("请求成功:{}", respBodyStr);
            }
            // 成功相应
            if (STATUS_CODE == statusCode || OTHER_STATUS_CODE == statusCode) {
                return respBodyStr;
            } else {
                WxUtils.error("failed,resp code = {},return body = {}", statusCode, respBodyStr);
                throw new RuntimeException(respBodyStr);
            }
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            throw new RuntimeException("接口超时");
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            throw new RuntimeException("读取接口数据超时");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("接口请求失败,请尝试检查网络环境或请求接口是否能正常访问");
        } finally {
            // 关闭响应
            try {
                if (response != null) {
                    //关闭结果集
                    response.getEntity().getContent().close();
                    response.close();
                }
            } catch (IOException e) {
                throw new RuntimeException("关闭流异常");
            }
        }
    }
 
    /**
     * 微信结果确认应答
     * 支付通知http应答码为200或204才会当作正常接收,当回调处理异常时,应答的HTTP状态码应为500,或者4xx。
     *
     * @param
     * @throws IOException
     */
    public void ack() throws IOException {
        ack(true, null);
    }
 
    /**
     * 微信结果确认应答
     * 支付通知http应答码为200或204才会当作正常接收,当回调处理异常时,应答的HTTP状态码应为500,或者4xx。
     *
     * @param
     * @throws IOException
     */
    public void ack(boolean ackSucc, String erroMsg) throws IOException {
        HttpServletResponse response = WebUtils.response();
        PrintWriter writer = response.getWriter();
        if (ackSucc) {
            log.info("响应微信回调成功!");
            response.setStatus(200);
            writer.write("{\"code\": \"SUCCESS\",\"message\": \"成功\"}");
 
        } else {
            log.info("响应微信回调失败:{}!", erroMsg);
            response.setStatus(500);
            writer.write("{\"code\": \"FAIL\",\"message\": " + (StringUtils.hasLength(erroMsg) ? erroMsg : "业务处理失败") + "}");
        }
        // 关闭流
        if (Objects.nonNull(writer)) {
            writer.close();
        }
    }
 
}