微信退款

1.请登录微信支付商户平台,点击【账户中心】->【账户设置】->【API安全】->【下载证书】中。

安装这个到本地任意路径,安装的时候需要输入密码(密码默认是mch_id);

2.注意参数设置(微信支付与退款的的支付金额退款金额都是以分为单位的)*

3.核心代码

        设置参数

 

        发送请求

   //微信退款
public static String wePayRefund(String url,String xmlInfo) throws Exception {
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance("PKCS12");
} catch (KeyStoreException e) {
e.printStackTrace();
}
FileInputStream instream = new FileInputStream(new File("C:\\Users\\admin\\Desktop\\apiclient_cert.p12"));//P12文件目录
try {
keyStore.load(instream, Constants.JTS_WCHART_MCH_ID.toCharArray());//这里写密码..默认是你的MCHID
} finally {
instream.close();
}
SSLContext sslcontext = null;
try {
sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, Constants.JTS_WCHART_MCH_ID.toCharArray())//这里也是写密码的
.build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
HttpPost httpost = new HttpPost(url); // 设置响应头信息
httpost.addHeader("Connection", "keep-alive");
httpost.addHeader("Accept", "*/*");
httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpost.addHeader("Host", "api.mch.weixin.qq.com");
httpost.addHeader("X-Requested-With", "XMLHttpRequest");
httpost.addHeader("Cache-Control", "max-age=0");
httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
httpost.setEntity(new StringEntity(xmlInfo, "UTF-8"));
CloseableHttpResponse response = null;
response = httpclient.execute(httpost);
try {
HttpEntity entity = response.getEntity();
String jsonStr = null;
jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(entity);
return jsonStr;
} finally {
response.close();
}
} finally {
httpclient.close();
}
}

QQ:1074794431   

 

 

 

posted @ 2017-09-21 13:18  我输汉字可以吗  阅读(173)  评论(0编辑  收藏  举报