jiangqs
2023-07-19 3ea9caae29421bcc6f21283e3f1d52c2bf967a4c
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
package com.ruoyi.shop.util;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.shop.util.dto.ContactInfo;
import com.ruoyi.shop.util.dto.IdCardInfo;
import com.ruoyi.shop.util.dto.SubmitInfo;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
 
import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;
 
/**
 * @ClassName WxShopUtils
 * @Description TODO
 * @Author jqs
 * @Date 2023/6/19 11:11
 * @Version 1.0
 */
 
public class WxShopUtils {
 
    private final static String PRIVATE_KEY = "";
 
    private final static String MCH_ID = "";
 
    private final static String MCH_SERIAL_NO = "";
 
    private final static String API_V3_KEY = "";
 
 
    /**
     * @description  创建httpClient
     * @author  jqs
     * @date    2023/6/19 12:50
     * @param
     * @return  CloseableHttpClient
     */
    private static CloseableHttpClient createHttpClient() throws NotFoundException, IOException, GeneralSecurityException, HttpCodeException {
 
        String privateKey = PRIVATE_KEY;
        String mchId = MCH_ID;
        String mchSerialNo = MCH_SERIAL_NO;
        String apiV3Key = API_V3_KEY;
        // 加载商户私钥(privateKey:私钥字符串)
        PrivateKey merchantPrivateKey = PemUtil
                .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
 
        // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),apiV3Key.getBytes("utf-8"));
 
        // 初始化httpClient
        CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, merchantPrivateKey)
                .withValidator(new WechatPay2Validator(verifier)).build();
        return httpClient;
    }
 
    /**
     * @description  提交申请
     * @author  jqs
     * @date    2023/7/19 11:01
     * @param submitInfo
     * @return  void
     */
    public static void ApplymentSubMch(SubmitInfo submitInfo) throws Exception {
 
        // 初始化httpClient
        CloseableHttpClient httpClient = createHttpClient();
        //请求URL
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/ecommerce/applyments/");
        // 请求body参数
        String reqdata = convertToStr(submitInfo);
        StringEntity entity = new StringEntity(reqdata,"utf-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        //完成签名并执行请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) { //处理成功
                System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
            } else if (statusCode == 204) { //处理成功,无返回Body
                System.out.println("success");
            } else {
                System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
                throw new IOException("request failed");
            }
        } finally {
            httpClient.close();
            response.close();
        }
 
    }
 
    /**
     * @description  获取申请状态
     * @author  jqs
     * @date    2023/7/19 11:01
     * @param applymentId
     * @return  void
     */
    public static void QueryApplyments(String applymentId) throws Exception{
 
        // 初始化httpClient
        CloseableHttpClient httpClient = createHttpClient();
        //请求URL
        HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/ecommerce/applyments/out-request-no/"+applymentId);
        httpGet.setHeader("Accept", "application/json");
        //完成签名并执行请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) { //处理成功
                System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
            } else if (statusCode == 204) { //处理成功,无返回Body
                System.out.println("success");
            } else {
                System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
                throw new IOException("request failed");
            }
        } finally {
            httpClient.close();
            response.close();
        }
    }
 
 
    //加密申请信息
    private static String convertToStr(SubmitInfo submitInfo) throws Exception {
        rsaEncryptSubmitInfo(submitInfo);
        return JSONObject.toJSONString(submitInfo);
    }
 
 
    private static void rsaEncryptSubmitInfo(SubmitInfo submitInfo) throws Exception {
        IdCardInfo idCardInfo=submitInfo.getId_card_info();
        if(idCardInfo!=null){
            idCardInfo.setId_card_name(rsaEncryptByCert(idCardInfo.getId_card_name()));
            idCardInfo.setId_card_number(rsaEncryptByCert(idCardInfo.getId_card_number()));
        }
        ContactInfo contactInfo=submitInfo.getContact_info();
        if(contactInfo!=null){
            contactInfo.setContact_name(rsaEncryptByCert(contactInfo.getContact_name()));
            contactInfo.setContact_id_card_number(rsaEncryptByCert(contactInfo.getContact_id_card_number()));
            contactInfo.setMobile_phone(rsaEncryptByCert(contactInfo.getMobile_phone()));
            if(!StringUtils.isEmpty(contactInfo.getContact_email())){
                contactInfo.setContact_email(rsaEncryptByCert(contactInfo.getContact_email()));
            }
        }
    }
 
    private static String rsaEncryptByCert(String content) throws Exception {
        String platPrivateKey = PRIVATE_KEY;
        InputStream inStream=new ByteArrayInputStream(platPrivateKey.getBytes(StandardCharsets.UTF_8));
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate certificate = (X509Certificate)cf.generateCertificate(inStream);
        PublicKey publicKey=certificate.getPublicKey();
        Cipher ci= Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
        ci.init(Cipher.ENCRYPT_MODE,publicKey);
        return Base64.getEncoder().encodeToString(ci.doFinal(content.getBytes(StandardCharsets.UTF_8)));
    }
 
 
}