|
|
@ -17,7 +17,9 @@ import org.apache.http.entity.ContentType; |
|
|
import org.apache.http.entity.StringEntity; |
|
|
import org.apache.http.entity.StringEntity; |
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
import org.apache.http.util.EntityUtils; |
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.util.Base64Utils; |
|
|
import org.springframework.util.Base64Utils; |
|
|
|
|
|
import sun.misc.BASE64Decoder; |
|
|
|
|
|
|
|
|
import javax.crypto.BadPaddingException; |
|
|
import javax.crypto.BadPaddingException; |
|
|
import javax.crypto.Cipher; |
|
|
import javax.crypto.Cipher; |
|
|
@ -30,6 +32,8 @@ import java.io.IOException; |
|
|
import java.io.UnsupportedEncodingException; |
|
|
import java.io.UnsupportedEncodingException; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.security.*; |
|
|
import java.security.*; |
|
|
|
|
|
import java.security.cert.CertificateException; |
|
|
|
|
|
import java.security.cert.CertificateFactory; |
|
|
import java.security.cert.X509Certificate; |
|
|
import java.security.cert.X509Certificate; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.ZoneOffset; |
|
|
import java.time.ZoneOffset; |
|
|
@ -41,6 +45,7 @@ import java.util.stream.Collectors; |
|
|
* |
|
|
* |
|
|
* @author shuaifengjie.com |
|
|
* @author shuaifengjie.com |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
@Component |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class WxpayUtils { |
|
|
public class WxpayUtils { |
|
|
|
|
|
|
|
|
@ -115,31 +120,25 @@ public class WxpayUtils { |
|
|
public static String response(String data, String url, String mchid, String privateKey, String certificate, String serialNo) { |
|
|
public static String response(String data, String url, String mchid, String privateKey, String certificate, String serialNo) { |
|
|
log.info("====>>> wxpay response, mchid: {}, serialNo: {}, url: {}, privateKey: {}, certificate: {}, data: {}", |
|
|
log.info("====>>> wxpay response, mchid: {}, serialNo: {}, url: {}, privateKey: {}, certificate: {}, data: {}", |
|
|
mchid, serialNo, url, privateKey, certificate, data); |
|
|
mchid, serialNo, url, privateKey, certificate, data); |
|
|
// ...
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
StringEntity reqEntity = new StringEntity(data, ContentType.create("application/json", "utf-8")); |
|
|
StringEntity reqEntity = new StringEntity(data, ContentType.create("application/json", "utf-8")); |
|
|
// ...
|
|
|
|
|
|
httpPost.setEntity(reqEntity); |
|
|
httpPost.setEntity(reqEntity); |
|
|
httpPost.addHeader("Accept", "application/json"); |
|
|
httpPost.addHeader("Accept", "application/json"); |
|
|
// ...
|
|
|
|
|
|
CloseableHttpResponse response = null; |
|
|
CloseableHttpResponse response = null; |
|
|
// 返回
|
|
|
// 返回
|
|
|
String body = null; |
|
|
String body = null; |
|
|
try { |
|
|
try { |
|
|
// 商户私钥
|
|
|
// 商户私钥
|
|
|
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8"))); |
|
|
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8"))); |
|
|
// ...
|
|
|
|
|
|
response = getClient(mchid, serialNo, merchantPrivateKey, certificate).execute(httpPost); |
|
|
response = getClient(mchid, serialNo, merchantPrivateKey, certificate).execute(httpPost); |
|
|
log.info("====>>> wxpay response, result: {}", JSON.toJSONString(response)); |
|
|
log.info("====>>> wxpay response, result: {}", JSON.toJSONString(response)); |
|
|
// response = httpClient.execute(httpPost);
|
|
|
|
|
|
// assertTrue(response.getStatusLine().getStatusCode() != 401);
|
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
|
HttpEntity entity = response.getEntity(); |
|
|
HttpEntity entity = response.getEntity(); |
|
|
body = EntityUtils.toString(entity); |
|
|
body = EntityUtils.toString(entity); |
|
|
log.info("====>>> wxpay response, success: {}, body: {}", statusCode, body); |
|
|
log.info("====>>> wxpay response, success: {}, body: {}", statusCode, body); |
|
|
if (statusCode != 200) { |
|
|
if (statusCode != 200) { |
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
Map<String, String> errorMsg = StringHelper.json2map(body); |
|
|
Map<String, String> errorMsg = StringHelper.json2map(body); |
|
|
|
|
|
log.info("====>>> wxpay response, fail errorMsg: {}", errorMsg); |
|
|
} |
|
|
} |
|
|
// 执行释放
|
|
|
// 执行释放
|
|
|
EntityUtils.consume(entity); |
|
|
EntityUtils.consume(entity); |
|
|
@ -167,18 +166,20 @@ public class WxpayUtils { |
|
|
* @return |
|
|
* @return |
|
|
* @throws UnsupportedEncodingException |
|
|
* @throws UnsupportedEncodingException |
|
|
*/ |
|
|
*/ |
|
|
public static CloseableHttpClient getClient(String mchid, String serialNo, PrivateKey privateKey, String certificate) throws UnsupportedEncodingException { |
|
|
public static CloseableHttpClient getClient(String mchid, String serialNo, PrivateKey privateKey, String certificate) throws CertificateException, IOException { |
|
|
|
|
|
//byte[] bytes = certificate.getBytes(StandardCharsets.UTF_8);
|
|
|
// 使用自动更新的签名验证器,不需要传入证书
|
|
|
ByteArrayInputStream base = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(certificate)); |
|
|
// AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
|
|
|
|
|
|
// new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)), apiV3Key.getBytes("utf-8"));
|
|
|
|
|
|
|
|
|
|
|
|
// builder
|
|
|
|
|
|
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create().withMerchant(mchid, serialNo, privateKey); |
|
|
|
|
|
// .withValidator(new WechatPay2Validator(verifier));
|
|
|
|
|
|
|
|
|
|
|
|
List<X509Certificate> certs = new ArrayList<>(); |
|
|
List<X509Certificate> certs = new ArrayList<>(); |
|
|
certs.add(PemUtil.loadCertificate(new ByteArrayInputStream(certificate.getBytes("utf-8")))); |
|
|
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
|
|
|
|
|
X509Certificate cert = null; |
|
|
|
|
|
try { |
|
|
|
|
|
cert = (X509Certificate) certificateFactory.generateCertificate(base); |
|
|
|
|
|
}catch (CertificateException e){ |
|
|
|
|
|
throw e; |
|
|
|
|
|
} |
|
|
|
|
|
assert cert != null; |
|
|
|
|
|
certs.add(cert); |
|
|
|
|
|
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create().withMerchant(mchid, serialNo, privateKey); |
|
|
builder.withWechatpay(certs); |
|
|
builder.withWechatpay(certs); |
|
|
return builder.build(); |
|
|
return builder.build(); |
|
|
} |
|
|
} |
|
|
|