修改微信支付
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.peanut.modules.pay.weChatPay.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.peanut.modules.pay.weChatPay.config.WechatPayConfig;
|
||||
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
|
||||
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
|
||||
import lombok.Data;
|
||||
@@ -11,6 +12,7 @@ import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
@@ -21,127 +23,116 @@ import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
|
||||
@Component
|
||||
@Data
|
||||
public class WxPayUtil {
|
||||
@Component
|
||||
@Data
|
||||
public class WxPayUtil {
|
||||
|
||||
public static final String mchId = "1612860909"; // 商户号
|
||||
@Autowired
|
||||
WechatPayConfig wechatPayConfig;
|
||||
|
||||
public static final String appId = "wx47134a8f15083734"; // appId
|
||||
private CloseableHttpClient httpClient;
|
||||
|
||||
public static final String apiV3Key = "4aYFklzaULeGlr7oJPZ6rHWKcxjihZUF"; // apiV3秘钥
|
||||
//商户私钥路径
|
||||
//public static final String privateKeyUrl = "/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem";
|
||||
public static final String privateKeyUrl = "C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem";
|
||||
public void setup() {
|
||||
PrivateKey merchantPrivateKey = null;
|
||||
X509Certificate wechatPayCertificate = null;
|
||||
|
||||
//平台证书路径
|
||||
//public static final String wechatPayCertificateUrl = "/usr/local/hs/peanut_book/target/classes/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem";
|
||||
public static final String wechatPayCertificateUrl = "C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem";
|
||||
//第一步申请完证书后,在API证书哪里点击管理证书就能看到
|
||||
public static final String mchSerialNo = "679AECB2F7AC4183033F713828892BA640E4EEE3"; // 商户证书序列号
|
||||
try {
|
||||
merchantPrivateKey = PemUtil.loadPrivateKey(
|
||||
new FileInputStream(wechatPayConfig.getPrivateKeyUrl()));
|
||||
wechatPayCertificate = PemUtil.loadCertificate(
|
||||
new FileInputStream(wechatPayConfig.getWechatPayCertificateUrl()));
|
||||
|
||||
private CloseableHttpClient httpClient;
|
||||
|
||||
public void setup() {
|
||||
PrivateKey merchantPrivateKey = null;
|
||||
X509Certificate wechatPayCertificate = null;
|
||||
|
||||
try {
|
||||
merchantPrivateKey = PemUtil.loadPrivateKey(
|
||||
new FileInputStream(privateKeyUrl));
|
||||
wechatPayCertificate = PemUtil.loadCertificate(
|
||||
new FileInputStream(wechatPayCertificateUrl));
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ArrayList<X509Certificate> listCertificates = new ArrayList<>();
|
||||
listCertificates.add(wechatPayCertificate);
|
||||
|
||||
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
|
||||
.withMerchant(mchId, mchSerialNo, merchantPrivateKey)
|
||||
.withWechatPay(listCertificates);
|
||||
httpClient = builder.build();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
/**
|
||||
* wxMchid商户号
|
||||
* wxCertno证书编号
|
||||
* wxCertPath证书地址
|
||||
* wxPaternerKey v3秘钥
|
||||
* url 下单地址
|
||||
* body 构造好的消息体
|
||||
*/
|
||||
public JSONObject doPostWexinV3(String url, String body) {
|
||||
if (httpClient == null) {
|
||||
setup();
|
||||
}
|
||||
ArrayList<X509Certificate> listCertificates = new ArrayList<>();
|
||||
listCertificates.add(wechatPayCertificate);
|
||||
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.addHeader("Content-Type", "application/json;chartset=utf-8");
|
||||
httpPost.addHeader("Accept", "application/json");
|
||||
try {
|
||||
if (body == null) {
|
||||
throw new IllegalArgumentException("data参数不能为空");
|
||||
}
|
||||
StringEntity stringEntity = new StringEntity(body, "utf-8");
|
||||
httpPost.setEntity(stringEntity);
|
||||
// 直接执行execute方法,官方会自动处理签名和验签,并进行证书自动更新
|
||||
HttpResponse httpResponse = httpClient.execute(httpPost);
|
||||
HttpEntity httpEntity = httpResponse.getEntity();
|
||||
|
||||
if (httpResponse.getStatusLine().getStatusCode() == 200) {
|
||||
String jsonResult = EntityUtils.toString(httpEntity);
|
||||
|
||||
return JSONObject.parseObject(jsonResult);
|
||||
} else {
|
||||
System.err.println("微信支付错误信息" + EntityUtils.toString(httpEntity));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
//获取签名
|
||||
public String getSign(String appId, long timestamp, String nonceStr, String pack){
|
||||
String message = buildMessage(appId, timestamp, nonceStr, pack);
|
||||
String paySign= null;
|
||||
try {
|
||||
paySign = sign(message.getBytes("utf-8"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return paySign;
|
||||
}
|
||||
|
||||
private String buildMessage(String appId, long timestamp, String nonceStr, String pack) {
|
||||
return appId + "\n"
|
||||
+ timestamp + "\n"
|
||||
+ nonceStr + "\n"
|
||||
+ pack + "\n";
|
||||
}
|
||||
private String sign(byte[] message) throws Exception{
|
||||
PrivateKey merchantPrivateKey = null;
|
||||
X509Certificate wechatPayCertificate = null;
|
||||
|
||||
try {
|
||||
merchantPrivateKey = PemUtil.loadPrivateKey(
|
||||
new FileInputStream(privateKeyUrl));
|
||||
wechatPayCertificate = PemUtil.loadCertificate(
|
||||
new FileInputStream(wechatPayCertificateUrl));
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Signature sign = Signature.getInstance("SHA256withRSA");
|
||||
|
||||
sign.initSign(merchantPrivateKey);
|
||||
sign.update(message);
|
||||
return Base64.getEncoder().encodeToString(sign.sign());
|
||||
}
|
||||
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
|
||||
.withMerchant(wechatPayConfig.getMchId(), wechatPayConfig.getSerialNo(), merchantPrivateKey)
|
||||
.withWechatPay(listCertificates);
|
||||
httpClient = builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* wxMchid商户号
|
||||
* wxCertno证书编号
|
||||
* wxCertPath证书地址
|
||||
* wxPaternerKey v3秘钥
|
||||
* url 下单地址
|
||||
* body 构造好的消息体
|
||||
*/
|
||||
public JSONObject doPostWexinV3(String url, String body) {
|
||||
if (httpClient == null) {
|
||||
setup();
|
||||
}
|
||||
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.addHeader("Content-Type", "application/json;chartset=utf-8");
|
||||
httpPost.addHeader("Accept", "application/json");
|
||||
try {
|
||||
if (body == null) {
|
||||
throw new IllegalArgumentException("data参数不能为空");
|
||||
}
|
||||
StringEntity stringEntity = new StringEntity(body, "utf-8");
|
||||
httpPost.setEntity(stringEntity);
|
||||
// 直接执行execute方法,官方会自动处理签名和验签,并进行证书自动更新
|
||||
HttpResponse httpResponse = httpClient.execute(httpPost);
|
||||
HttpEntity httpEntity = httpResponse.getEntity();
|
||||
|
||||
if (httpResponse.getStatusLine().getStatusCode() == 200) {
|
||||
String jsonResult = EntityUtils.toString(httpEntity);
|
||||
|
||||
return JSONObject.parseObject(jsonResult);
|
||||
} else {
|
||||
System.err.println("微信支付错误信息" + EntityUtils.toString(httpEntity));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
//获取签名
|
||||
public String getSign(String appId, long timestamp, String nonceStr, String pack) {
|
||||
String message = buildMessage(appId, timestamp, nonceStr, pack);
|
||||
String paySign = null;
|
||||
try {
|
||||
paySign = sign(message.getBytes("utf-8"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return paySign;
|
||||
}
|
||||
|
||||
private String buildMessage(String appId, long timestamp, String nonceStr, String pack) {
|
||||
return appId + "\n"
|
||||
+ timestamp + "\n"
|
||||
+ nonceStr + "\n"
|
||||
+ pack + "\n";
|
||||
}
|
||||
|
||||
private String sign(byte[] message) throws Exception {
|
||||
PrivateKey merchantPrivateKey = null;
|
||||
X509Certificate wechatPayCertificate = null;
|
||||
|
||||
try {
|
||||
merchantPrivateKey = PemUtil.loadPrivateKey(
|
||||
new FileInputStream(wechatPayConfig.getPrivateKeyUrl()));
|
||||
wechatPayCertificate = PemUtil.loadCertificate(
|
||||
new FileInputStream(wechatPayConfig.getWechatPayCertificateUrl()));
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Signature sign = Signature.getInstance("SHA256withRSA");
|
||||
|
||||
sign.initSign(merchantPrivateKey);
|
||||
sign.update(message);
|
||||
return Base64.getEncoder().encodeToString(sign.sign());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user