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)));
|
}
|
|
|
}
|