微信退款接口
官方地址 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
具体的退款API调用接口为 https://api.mch.weixin.qq.com/secapi/pay/refund
证书下载
//TODO 9.0 操作支付表,把当前的支付的状态变成 退款状态 state 1 ---> 2
//TODO 10 操作预约表,可以把当前的预约状态取消 已支付--->退款
//TODO 11 操作用户表,如果是充值退款的话,把用户的现金 - 当前退款的money
//TODO 12其他等等的操作,生成记录单号之类的
关于证书的使用
将下载下来的证书放到项目中的路径,然后在发起post请求时,使用这个证书加密发送内容 比如你发送的是ABC 加密后为密文
HttpClient的代码
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
核心的代码
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* This example demonstrates how to create secure connections with a custom SSL
* context.
*/
public class ClientCustomSSL {
public static String SSLCERT_PATH="";//证书的路径
public static String SSLCERT_PASSWORD;//证书的密籍
@SuppressWarnings("deprecation")
public static String doRefund(String url,String data) throws Exception {
//注意PKCS12证书 是从微信商户平台-》账户设置-》 API安全 中下载的
KeyStore keyStore = KeyStore.getInstance("PKCS12");
//指向你的证书的绝对路径,带着证书去访问
FileInputStream instream = new FileInputStream(new File(SSLCERT_PATH));//P12文件目录
try {
//下载证书时的密码、默认密码是你的MCHID mch_id
keyStore.load(instream, SSLCERT_PASSWORD.toCharArray());//这里写密码
} finally {
instream.close();
}
//下载证书时的密码、默认密码是你的MCHID mch_id
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, SSLCERT_PASSWORD.toCharArray())//这里也是写密码的
.build();
// Allow TLSv1 protocol only
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(data, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httpost);
try {
HttpEntity entity = response.getEntity();
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(entity);
return jsonStr;
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}