🆕 #1090 增加微信支付分和免押租借相关接口

This commit is contained in:
spvycf
2020-05-19 16:25:18 +08:00
committed by GitHub
parent 0bc2cf9ade
commit 8709a9c5a7
26 changed files with 1762 additions and 12 deletions

View File

@@ -1,15 +1,25 @@
package com.github.binarywang.wxpay.config;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.v3.WechatPayHttpClientBuilder;
import com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier;
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner;
import com.github.binarywang.wxpay.v3.auth.WechatPay2Credentials;
import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
import com.github.binarywang.wxpay.v3.util.PemUtils;
import lombok.Data;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.URL;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
/**
* 微信支付配置
@@ -85,6 +95,39 @@ public class WxPayConfig {
*/
private String keyPath;
/**
* apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径.
*/
private String privateKeyPath;
/**
* apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径.
*/
private String privateCertPath;
/**
* apiV3 秘钥值.
*/
private String apiv3Key;
/**
* apiV3 证书序列号值
*/
private String certSerialNo;
/**
* 微信支付分serviceId
*/
private String serviceId;
/**
* 微信支付分回调地址
*/
private String payScoreNotifyUrl;
private CloseableHttpClient apiv3HttpClient;
/**
* p12证书文件内容的字节数组.
*/
@@ -185,4 +228,78 @@ public class WxPayConfig {
}
}
}
/**
* @Author doger.wang
* @Description 初始化api v3请求头 自动签名验签
* 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
* @Date 2020/5/14 10:10
* @Param []
* @return org.apache.http.impl.client.CloseableHttpClient
**/
public CloseableHttpClient initApiV3HttpClient()throws WxPayException {
String privateKeyPath = this.getPrivateKeyPath();
String privateCertPath = this.getPrivateCertPath();
String certSerialNo = this.getCertSerialNo();
String apiv3Key = this.getApiv3Key();
if (StringUtils.isBlank(privateKeyPath)) {
throw new WxPayException("请确保privateKeyPath已设置");
}
if (StringUtils.isBlank(privateCertPath)) {
throw new WxPayException("请确保privateCertPath已设置");
}
if (StringUtils.isBlank(certSerialNo)) {
throw new WxPayException("请确保certSerialNo证书序列号已设置");
}
if (StringUtils.isBlank(apiv3Key)) {
throw new WxPayException("请确保apiv3Key值已设置");
}
InputStream keyinputStream=null;
InputStream certinputStream=null;
final String prefix = "classpath:";
if (privateKeyPath.startsWith(prefix)) {
String keypath = StringUtils.removeFirst(privateKeyPath, prefix);
if (!keypath.startsWith("/")) {
keypath = "/" + keypath;
}
keyinputStream = WxPayConfig.class.getResourceAsStream(keypath);
if (keyinputStream == null) {
throw new WxPayException("证书文件【" + this.getPrivateKeyPath() + "】不存在,请核实!");
}
}
if (privateCertPath.startsWith(prefix)) {
String certpath = StringUtils.removeFirst(privateCertPath, prefix);
if (!certpath.startsWith("/")) {
certpath = "/" + certpath;
}
certinputStream = WxPayConfig.class.getResourceAsStream(certpath);
if (certinputStream == null) {
throw new WxPayException("证书文件【" + this.getPrivateCertPath() + "】不存在,请核实!");
}
}
CloseableHttpClient httpClient = null;
try {
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create();
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyinputStream);
X509Certificate x509Certificate = PemUtils.loadCertificate(certinputStream);
ArrayList<X509Certificate> certificates = new ArrayList<>();
certificates.add(x509Certificate);
builder.withMerchant(mchId, certSerialNo, merchantPrivateKey);
builder.withWechatpay(certificates);
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
apiv3Key.getBytes("utf-8"));
builder.withValidator(new WechatPay2Validator(verifier));
httpClient = builder.build();
this.apiv3HttpClient =httpClient;
} catch (Exception e) {
throw new WxPayException("v3请求构造异常", e);
}
return httpClient;
}
}