支付V3之投诉回调API封装
⽀付V3之投诉回调API封装
⼤家好,我是梦⾟⼯作室的灵,近期在制作相关软件的过程中,有需要⽤到对商户号的投诉风险合规,第⼀次搞这个还是踩了不少坑,然后为了⽅便使⽤做了以下封装便于⼤家使⽤,其实也不难,主要怪⾃⼰没有去仔细看提供的指导(在 官⽅提供的 wechatpay-apache-httpclient-0.2.2.jar 的基础上继续简化的),下来就讲下怎么写吧
先准备好以下相关资料:
1.商户号 编号
2.商户号 ApiV3 秘钥,需要在 商户号后台设置
3.商户号证书私钥,下载商户号 证书时会有那个 apiclient_key.pem ⽂件就是它
4.商户号证书序列号,这个可以在 商户号证书管理后台查看
准备好以上资料后就可以开始写了
第⼀步:加载证书,获取到httpCilent对象:
public void setup(){
if(!doCheckParam()){
isStep =false;
return;野兔的生活习性
}
try{
privateKey =new (privateKeyFilePath)),"utf-8");
// 加载商户私钥(privateKey:私钥字符串)
merchantPrivateKey = PemUtil.loadPrivateKey(new Bytes("utf-8")));
// 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
AutoUpdateCertificatesVerifier verifier =new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId,new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),
// 初始化httpClient
httpClient = ate().withMerchant(mchId, mchSerialNo, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier)).build();
isStep =true;
}catch(Exception e){
// TODO: handle exception
errorHint = String();
isStep =false;
}
}
第⼆步:发起创建投诉回调通知 ,其他操作类同
/**
* 创建投诉回调通知
*
* @param url
*            回调地址
* @return
* @throws Exception
*/
public String CreateComplaintsNotify(String url)throws Exception {
if(!isStep){
errorHint ="未成功启⽤Step";
雨纷纷 旧故里草木深return null;
}
String result =null;
// 请求URL
HttpPost httpPost =new HttpPost("h.weixin.qq/v3/merchant-service/complaint-notifications");
JSONObject dataJSON =new JSONObject();
dataJSON.put("url", url);
StringEntity entity =new String());
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept","application/json");
// 完成签名并执⾏请求
CloseableHttpResponse response = ute(httpPost);
try{
int statusCode = StatusLine().getStatusCode();
if(statusCode ==200){// 处理成功
result = Entity());
七夕情人节诗句
}else if(statusCode ==204){// 处理成功,⽆返回Body
result ="{'code':204}";
}else{
result = Entity());
}
}finally{
response.close();
}
return result;
}
这⾥需要注意的是:提供的这个 API封装(wechatpay-apache-httpclient-0.2.2.jar)在发起请求时已经⾃带了 Authorization Header,所以不⽤再去添加这个Header,不然会报错,楼主在这⾥被坑了好久,翻了不好⽂档才到
还有就是如果有出现 java.security.InvalidKeyException: Illegal key size WechatPayHttpClientBuild 错误的话,请注意 java版本,如果在 java 8u 162 以下的话,请更换 java 版本 ,必须要在 java 8u 162
以上才可以 , 早期的java 运⾏ 限制了 JCE⽀持的秘钥长度,即默认不⽀持256位的AES,这⾥楼主也被坑了好早,试过好多⽅法才到,换来是java版本低了
下⾯是所有代码⽰例:
package;
import ByteArrayInputStream;
import File;
import FileInputStream;
import IOException;
import InputStream;
import URI;
import Files;
import Paths;
import NoSuchAlgorithmException;
import PrivateKey;
import Base64;
import BadPaddingException;
import Cipher;
import IllegalBlockSizeException;
import NoSuchPaddingException;
import DigestUtils;
import CloseableHttpResponse;
import HttpDelete;
import HttpGet;
import HttpPost;
import HttpPut;
import StringEntity;
import CloseableHttpClient;
import EntityUtils;
import WechatPayHttpClientBuilder;
import WechatPayUploadHttpPost; import AutoUpdateCertificatesVerifier; import PrivateKeySigner;
import WechatPay2Credentials; import WechatPay2Validator; import PemUtil;
import JSONArray;
import JSONObject;
public class WeChatAPIV3 {
CloseableHttpClient httpClient;
String mchId ="商户号";
String mchSerialNo ="证书序列号";
String apiV3Key ="APIV3秘钥";
String privateKeyFilePath ="证书私钥路径";
String privateKey ="";
PrivateKey merchantPrivateKey;
传家宝经验加成
String errorHint ="";
public String getMchId(){
return mchId;
}
public void setMchId(String mchId){
华北}
public String getMchSerialNo(){
return mchSerialNo;
}
public void setMchSerialNo(String mchSerialNo){
}
public String getApiV3Key(){
return apiV3Key;
}
public void setApiV3Key(String apiV3Key){
this.apiV3Key = apiV3Key;
public String getPrivateKeyFilePath(){
return privateKeyFilePath;
}
public void setPrivateKeyFilePath(String privateKeyFilePath){
this.privateKeyFilePath = privateKeyFilePath;
}
private boolean isStep =false;
public WeChatAPIV3(){
}
public WeChatAPIV3(String mchId, String mchSerialNo, String apiV3Key, String privateKeyFilePath){ hId = mchId;
this.apiV3Key = apiV3Key;
this.privateKeyFilePath = privateKeyFilePath;
}
public String rsaDecryptOAEP(String ciphertext)throws BadPaddingException, IOException {
if(!isStep){
errorHint ="未成功启⽤Step";
return null;
}
if(merchantPrivateKey ==null){
errorHint ="未成功加载私钥";
return null;
}
try{
Cipher cipher = Instance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, merchantPrivateKey);
byte[] data = Decoder().decode(ciphertext);
return new String(cipher.doFinal(data),"utf-8");
}catch(NoSuchPaddingException | NoSuchAlgorithmException e){
throw new RuntimeException("当前Java环境不⽀持RSA v1.5/OAEP", e);
}catch(InvalidKeyException e){
throw new IllegalArgumentException("⽆效的私钥", e);
}catch(BadPaddingException | IllegalBlockSizeException e){
throw new BadPaddingException("解密失败");
}
}
public boolean doCheckParam(){
return doCheckValue(mchId, mchSerialNo, apiV3Key, privateKeyFilePath);
}
public boolean item){
for(String each : item){
if(each ==null|| each.length()==0){
errorHint ="缺少必要参数";教育部留学中介排名
return false;
}
return true;
}
public void setup(){
if(!doCheckParam()){
isStep =false;
return;
}
try{
privateKey =new (privateKeyFilePath)),"utf-8");
// 加载商户私钥(privateKey:私钥字符串)
merchantPrivateKey = PemUtil.loadPrivateKey(new Bytes("utf-8")));
// 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
AutoUpdateCertificatesVerifier verifier =new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId,new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),
// 初始化httpClient
httpClient = ate().withMerchant(mchId, mchSerialNo, merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier)).build();
isStep =true;
}catch(Exception e){
// TODO: handle exception
errorHint = String();
isStep =false;
}
}
/**
* 查询投诉详情
*
* @param complaint_id
*            投诉单号
* @return
* @throws Exception
*/
public String GetComplaintsInfo(String complaint_id)throws Exception {
if(!isStep){
errorHint ="未成功启⽤Step";
return null;
}
String result =null;
// 请求URL
HttpGet httpGet =new HttpGet(
"h.weixin.qq/v3/merchant-service/complaints-v2/"+ complaint_id);
httpGet.setHeader("Accept","application/json");
// 完成签名并执⾏请求
CloseableHttpResponse response = ute(httpGet);
try{
int statusCode = StatusLine().getStatusCode();
if(statusCode ==200){// 处理成功

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。