gitHop地址
- https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.vworld365.common.exception.RequestException;
import com.vworld365.common.exception.ServiceException;
import com.vworld365.zcam.isv.pojo.ao.SplitAmountAO;
import com.vworld365.zcam.isv.wechat.client.WechatHttpClient;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Test {
public Integer get(SplitAmountAO splitAmountAO) throws ServiceException, RequestException, UnsupportedEncodingException {
// String transactionId= this.queryTradeTransactionId(splitAmountAO.getMchId(),splitAmountAO.getTradeId());
String apiV3Key ="key";
//不需要传入微信支付证书了
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials("13119101", new PrivateKeySigner("2C733862E2AFF3C8B837D8", WechatHttpClient.getPrivateKey())),
apiV3Key.getBytes("utf-8"));
//...
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant("mchid", "2FBF2D26CAF94CE2AFF3C8B837D8",WechatHttpClient.getPrivateKey() )
.withValidator(new WechatPay2Validator(verifier));
// ... 接下来,你仍然可以通过builder设置各种参数,来配置你的HttpClient
// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
HttpClient httpClient = builder.build();
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/profitsharing/orders/unfreeze");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type","application/json; charset=utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("sub_mchid",splitAmountAO.getMchId())
.put("transaction_id", transactionId)
.put("out_order_no", splitAmountAO.getOutRequestNo())
.put("description", "解冻全部剩余资金");
try {
objectMapper.writeValue(bos, rootNode);
} catch (IOException e) {
e.printStackTrace();
}
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
CloseableHttpResponse response = null;
try {
response = (CloseableHttpResponse) httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
String bodyAsString = null;
try {
bodyAsString = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(bodyAsString);
Integer status = WechatHttpClient.getStatus(response);
try {
JSONObject resultJson = WechatHttpClient.getData(response);
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
}
public static PrivateKey getPrivateKey(){
PrivateKey merchantPrivateKey = getPrivateKey(wechatConfig.getApiClientKeyPath());
return merchantPrivateKey;
}
根据GitHub上的注释看这些代码 会明白一些