记那些年特约商户进件踩过的坑
公司要做特约商户进件,折腾了好几天,都快秃了,现在就来说一说那些踩过的坑(想起来就是一把辛酸泪有木有)。
1. 微信平台证书下载
总的来说微信的SDK还是很有用的,比如微信平台证书它就封装了一个挺好用的方法,下载地址https://wechatpay-api.gitbook.io/wechatpay-api-v3/kai-fa-gong-ju
直接按照测试接口调用就行,很好用的,但是配置一定要配置好,尤其是服务商号和商户号,一定要区分开,在这里吃了大亏!
2. 图片上传
这是我踩过最深的坑,头都要秃了,好吧啦,
- 签名错误,原因是没有设置user-agent,一定要设置
//我的头文件
HttpPost httpPost = new HttpPost(uploadUrl);
// httpPost.setHeader("Wechatpay-Serial", serialNo);
httpPost.setHeader("Accept", "application/json");
// httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("user-agent", "application/json");
httpPost.setHeader("Content-Type","multipart/form-data;boundary=boundary");
httpPost.setHeader("Authorization", authorization);
- 签名的内容
POST
/v3/merchant/media/upload
1566987169 //时间戳
12ced2db6f0193dda91ba86224ea1cd8 //随机数
{"filename":" filea.jpg ","sha256":" hjkahkjsjkfsjk78687dhjahdajhk "}
file不参与签名
3. 二进制内容错误
这是因为,我采用的是字符串拼接,使用SDK里面的方法去用,一直不可以,没办法只能换一种方法去写了
//创建MultipartEntityBuilder
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
//设置boundary
multipartEntityBuilder.setBoundary("boundary");
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
//设置meta内容
multipartEntityBuilder.addTextBody("meta","{\"filename\":\""+file.getName()+"\",\"sha256\":\""+DigestUtils.sha256Hex(new FileInputStream(file))+"\"}", ContentType.APPLICATION_JSON);
//设置图片内容
multipartEntityBuilder.addBinaryBody("file", file, ContentType.create("image/jpg"), file.getName());
//放入内容
httpPost.setEntity(multipartEntityBuilder.build());
HttpClientBuilder httpClientBuilder = HttpClients.custom();
CloseableHttpClient httpClient = httpClientBuilder.setSSLContext(SSLContextUtils.getSSLContext(apiclientCertP12,mchId)).build();
CloseableHttpResponse response = httpClient.execute(httpPost);
3. 申请的坑
1.论微信的坑有多深,我感觉是没个三五天爬不出来的那种,好容易走到申请数据,首先数据里面夹杂着数组,就够头疼的,还有各种校验以及判定,总之写的判定快把自己写吐了。
2. 申请的时候的两种证书的问题,微信的文档很笼统,爬了好几天才爬出来,欲哭无泪
X509Certificate certificate = PemUtil.loadCertificate(new FileInputStream(//微信的平台证书路径,线上下载的那个));
ArrayList<X509Certificate> listCertificates = new ArrayList<>();
listCertificates.add(certificate);
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, serialNo, PemUtil.loadPrivateKey(new FileInputStream(//商户证书的私钥文件)))
.withWechatpay(listCertificates);
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type","application/json");
httpPost.addHeader("Wechatpay-Serial",EncryptSensitive.getSerialNo(//微信的平台证书的路径,也可以线上获取,反正和你在API证书哪里看的不一样));
httpPost.setEntity(new StringEntity(requestParams, "UTF-8"));
CloseableHttpClient httpClient = builder.build();
CloseableHttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity());