小程序
小游戏
企业微信
微信支付
扫描小程序码分享
本地证书有效期到2024-6-11,但验证错误,调用方法如下:
X509Certificate wechatpayCertificate = PemUtil.loadCertificate( new ByteArrayInputStream(certificate.getBytes("utf-8"))); 方法抛出异常throw new RuntimeException("无效的证书", e);
6 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
String certData = certs.replace("-----BEGIN CERTIFICATE-----", "") .replace("-----END CERTIFICATE-----", "") .replaceAll("\\s+", ""); BASE64Decoder decoder = new BASE64Decoder(); byte[] byteCert = decoder.decodeBuffer(certData); X509Certificate x509Certificate = PemUtil.loadCertificate( new ByteArrayInputStream(byteCert)); //获取提交的pem的串后替换前后缀和换行,别直接getByte 用这种方法 转64字节
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
请问解决了吗?怎么解决的啊
获取openid正常,统一下单都正常,小程序调起支付API,签名,返回证书无效,请问是什么原因呢?
你问题解决了吗? 我也是用这个https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
的例子来运行。就碰到了证书无效的提示。 我的私钥和证书都是微信后台那边下载回来的。
现在我调用的商户开户意愿确认接口,调用时返回401,验签不通过,我按照下面的案例写的
package com.wechat.pay.contrib.apache.httpclient; import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.ArrayList; import org.apache.http.HttpEntity; 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.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; public class HttpClientBuilderTest { private static String mchId = "1900009191"; // 商户号 private static String mchSerialNo = "1DDE55AD98ED71D6EDD4A4A16996DE7B47773A8C"; // 商户证书序列号 private CloseableHttpClient httpClient; private static String reqdata = "{\n" + " \"stock_id\": \"9433645\",\n" + " \"stock_creator_mchid\": \"1900006511\",\n" + " \"out_request_no\": \"20190522_001中文11\",\n" + " \"appid\": \"wxab8acb865bb1637e\"\n" + "}"; // 你的商户私钥 private static String privateKey = "-----BEGIN PRIVATE KEY-----\n" + "-----END PRIVATE KEY-----"; // 你的微信支付平台证书 private static String certificate = "-----BEGIN CERTIFICATE-----\n" + "-----END CERTIFICATE-----"; @Before public void setup() throws IOException { PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey( new ByteArrayInputStream(privateKey.getBytes("utf-8"))); X509Certificate wechatpayCertificate = PemUtil.loadCertificate( new ByteArrayInputStream(certificate.getBytes("utf-8"))); ArrayList<X509Certificate> listCertificates = new ArrayList<>(); listCertificates.add(wechatpayCertificate); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(mchId, mchSerialNo, merchantPrivateKey) .withWechatpay(listCertificates) .build(); } @After public void after() throws IOException { httpClient.close(); } @Test public void getCertificateTest() throws Exception { URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/certificates"); uriBuilder.setParameter("p", "1&2"); uriBuilder.setParameter("q", "你好"); HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.addHeader("Accept", "application/json"); CloseableHttpResponse response1 = httpClient.execute(httpGet); assertEquals(200, response1.getStatusLine().getStatusCode()); try { HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity1); } finally { response1.close(); } } @Test public void getCertificatesWithoutCertTest() throws Exception { PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey( new ByteArrayInputStream(privateKey.getBytes("utf-8"))); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(mchId, mchSerialNo, merchantPrivateKey) .withValidator(response -> true) .build(); getCertificateTest(); } @Test public void postNonRepeatableEntityTest() throws IOException { HttpPost httpPost = new HttpPost( "https://api.mch.weixin.qq.com/v3/marketing/favor/users/oHkLxt_htg84TUEbzvlMwQzVDBqo/coupons"); InputStream stream = new ByteArrayInputStream(reqdata.getBytes("utf-8")); InputStreamEntity reqEntity = new InputStreamEntity(stream); reqEntity.setContentType("application/json"); httpPost.setEntity(reqEntity); httpPost.addHeader("Accept", "application/json"); CloseableHttpResponse response = httpClient.execute(httpPost); assertTrue(response.getStatusLine().getStatusCode() != 401); try { HttpEntity entity2 = response.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response.close(); } } @Test public void postRepeatableEntityTest() throws IOException { HttpPost httpPost = new HttpPost( "https://api.mch.weixin.qq.com/v3/marketing/favor/users/oHkLxt_htg84TUEbzvlMwQzVDBqo/coupons"); // NOTE: 建议指定charset=utf-8。低于4.4.6版本的HttpCore,不能正确的设置字符集,可能导致签名错误 StringEntity reqEntity = new StringEntity( reqdata, ContentType.create("application/json", "utf-8")); httpPost.setEntity(reqEntity); httpPost.addHeader("Accept", "application/json"); CloseableHttpResponse response = httpClient.execute(httpPost); assertTrue(response.getStatusLine().getStatusCode() != 401); try { HttpEntity entity2 = response.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response.close(); } } }
你这是验证的哪个证书?请详细说明。
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
String certData = certs.replace("-----BEGIN CERTIFICATE-----", "") .replace("-----END CERTIFICATE-----", "") .replaceAll("\\s+", ""); BASE64Decoder decoder = new BASE64Decoder(); byte[] byteCert = decoder.decodeBuffer(certData); X509Certificate x509Certificate = PemUtil.loadCertificate( new ByteArrayInputStream(byteCert)); //获取提交的pem的串后替换前后缀和换行,别直接getByte 用这种方法 转64字节
请问解决了吗?怎么解决的啊
获取openid正常,统一下单都正常,小程序调起支付API,签名,返回证书无效,请问是什么原因呢?
你问题解决了吗? 我也是用这个https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
的例子来运行。就碰到了证书无效的提示。 我的私钥和证书都是微信后台那边下载回来的。
现在我调用的商户开户意愿确认接口,调用时返回401,验签不通过,我按照下面的案例写的
package com.wechat.pay.contrib.apache.httpclient; import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.ArrayList; import org.apache.http.HttpEntity; 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.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; public class HttpClientBuilderTest { private static String mchId = "1900009191"; // 商户号 private static String mchSerialNo = "1DDE55AD98ED71D6EDD4A4A16996DE7B47773A8C"; // 商户证书序列号 private CloseableHttpClient httpClient; private static String reqdata = "{\n" + " \"stock_id\": \"9433645\",\n" + " \"stock_creator_mchid\": \"1900006511\",\n" + " \"out_request_no\": \"20190522_001中文11\",\n" + " \"appid\": \"wxab8acb865bb1637e\"\n" + "}"; // 你的商户私钥 private static String privateKey = "-----BEGIN PRIVATE KEY-----\n" + "-----END PRIVATE KEY-----"; // 你的微信支付平台证书 private static String certificate = "-----BEGIN CERTIFICATE-----\n" + "-----END CERTIFICATE-----"; @Before public void setup() throws IOException { PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey( new ByteArrayInputStream(privateKey.getBytes("utf-8"))); X509Certificate wechatpayCertificate = PemUtil.loadCertificate( new ByteArrayInputStream(certificate.getBytes("utf-8"))); ArrayList<X509Certificate> listCertificates = new ArrayList<>(); listCertificates.add(wechatpayCertificate); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(mchId, mchSerialNo, merchantPrivateKey) .withWechatpay(listCertificates) .build(); } @After public void after() throws IOException { httpClient.close(); } @Test public void getCertificateTest() throws Exception { URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/certificates"); uriBuilder.setParameter("p", "1&2"); uriBuilder.setParameter("q", "你好"); HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.addHeader("Accept", "application/json"); CloseableHttpResponse response1 = httpClient.execute(httpGet); assertEquals(200, response1.getStatusLine().getStatusCode()); try { HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity1); } finally { response1.close(); } } @Test public void getCertificatesWithoutCertTest() throws Exception { PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey( new ByteArrayInputStream(privateKey.getBytes("utf-8"))); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(mchId, mchSerialNo, merchantPrivateKey) .withValidator(response -> true) .build(); getCertificateTest(); } @Test public void postNonRepeatableEntityTest() throws IOException { HttpPost httpPost = new HttpPost( "https://api.mch.weixin.qq.com/v3/marketing/favor/users/oHkLxt_htg84TUEbzvlMwQzVDBqo/coupons"); InputStream stream = new ByteArrayInputStream(reqdata.getBytes("utf-8")); InputStreamEntity reqEntity = new InputStreamEntity(stream); reqEntity.setContentType("application/json"); httpPost.setEntity(reqEntity); httpPost.addHeader("Accept", "application/json"); CloseableHttpResponse response = httpClient.execute(httpPost); assertTrue(response.getStatusLine().getStatusCode() != 401); try { HttpEntity entity2 = response.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response.close(); } } @Test public void postRepeatableEntityTest() throws IOException { HttpPost httpPost = new HttpPost( "https://api.mch.weixin.qq.com/v3/marketing/favor/users/oHkLxt_htg84TUEbzvlMwQzVDBqo/coupons"); // NOTE: 建议指定charset=utf-8。低于4.4.6版本的HttpCore,不能正确的设置字符集,可能导致签名错误 StringEntity reqEntity = new StringEntity( reqdata, ContentType.create("application/json", "utf-8")); httpPost.setEntity(reqEntity); httpPost.addHeader("Accept", "application/json"); CloseableHttpResponse response = httpClient.execute(httpPost); assertTrue(response.getStatusLine().getStatusCode() != 401); try { HttpEntity entity2 = response.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response.close(); } } }
你这是验证的哪个证书?请详细说明。