luodangjia
2024-11-05 0c569ab4b38d972213eeaf7e8965f33f8ec650fd
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
package com.ruoyi.admin.utils;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
 
import com.wechat.pay.java.core.cipher.RSASigner;
import com.wechat.pay.java.core.cipher.SignatureResult;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.core.util.NonceUtil;
import com.wechat.pay.java.core.util.PemUtil;
 
/**
 * @desc: 微信工具类
 * @author: shy
 * @date: 2024/4/8 16:10
 */
public class WeChatUtil {
 
    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
    private static final Random RANDOM = new SecureRandom();
 
    /**
     * 生成订单号
     *
     * @param
     * @return String
     * @author shy
     * @date 2024/4/8 16:15
     */
    public static String generateTradeNumber() {
        // 定义订单号前缀
        // 当前年月日
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String currentTimeStr =now.format(formatter);
        // 获取当前时间戳
        long timestamp = System.currentTimeMillis();
        // 构造订单号
        return currentTimeStr + timestamp;
    }
 
    /**
     * 获取随机字符串 Nonce Str
     *
     * @param
     * @return String
     * @author shy
     * @date 2024/4/16 17:07
     */
    public static String generateNonceStr() {
        return NonceUtil.createNonce(32);
    }
 
    /**
     * 获取当前时间戳,单位秒
     * @param
     * @return long
     * @author shy
     * @date 2024/4/16 17:10
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis() / 1000;
    }
 
    public static String getSign(String signatureStr, String privateKeyPath, String merchantSerialNumber) {
        PrivateKey privateKey = PemUtil.loadPrivateKeyFromPath(privateKeyPath);
        RSASigner rsaSigner = new RSASigner(merchantSerialNumber, privateKey);
        SignatureResult signatureResult = rsaSigner.sign(signatureStr);
        return signatureResult.getSign();
    }
 
 
    /**
     * 构造 RequestParam
     *
     * @param request
     * @return RequestParam
     * @author shy
     * @date 2024/4/9 11:16
     */
    public static RequestParam handleNodifyRequestParam(HttpServletRequest request) throws IOException {
        // 请求头Wechatpay-Signature
        String signature = request.getHeader("Wechatpay-Signature");
        // 请求头Wechatpay-nonce
        String nonce = request.getHeader("Wechatpay-Nonce");
        // 请求头Wechatpay-Timestamp
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        // 微信支付证书序列号
        String serial = request.getHeader("Wechatpay-Serial");
        // 签名方式
        String signType = request.getHeader("Wechatpay-Signature-Type");
        // 构造 RequestParam
        return new RequestParam.Builder().serialNumber(serial).nonce(nonce).signature(signature).timestamp(timestamp).signType(signType).body(getRequestBody(request)).build();
 
    }
 
    public static String getRequestBody(HttpServletRequest request) throws IOException {
        ServletInputStream stream;
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            stream = request.getInputStream();
            // 获取响应
            reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new IOException("读取返回支付接口数据流出现异常!");
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        return sb.toString();
    }
}