Java支付申请退款
Java⽀付申请退款
⼀、申请退款内容
1.证书使⽤
申请退款是需要证书的。
证书名字:中,说明了Java使⽤的是第⼀种:pkcs12格式的apiclient_cert.p12。
下载证书:-->账户设置-->API安全-->证书下载,操作。
调⽤实例:各版本的调⽤实例请参考⽀付提供的(点击会下载cert⽂件)。
Demo:下载的cert⽂件夹⾥有Java版代码使⽤Demo。
2.实现途经选择
代码⼯具与⽅式----⽤的的插件版:,⽤简单实现。
⼊⼿----,有必填参数说明,有xml举例举例
debug调试----⽤已经⽀付过的确定订单号来测试,查看返回的xml内容
3.申请退款流程
①拼成xml:将参数拼成xml字符串
②提交到URL:POST⽅式提交给申请退款API的URL
③解析返回的xml:成功或者失败及具体信息,⾥⾯有说明
④json格式:也可以拼成json格式,⽅便传递数据。
4.⽤到资料
jar包:中Java版的lib⽂件夹⾥,commons-logging-1.1.3,httpclient-4.3.4,httpcore-4.3.2。
其他内容和代码:请阅读我的
⼆、代码步骤
1.申请退款的Servlet
⽤servlet写的:参数赋值-->拼接参数-->参数提交-->参数返回并解析
public class RefundServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String appid = PayConfigUtil.APP_ID; // appid
String mch_id = PayConfigUtil.MCH_ID; // 商业号
String key = PayConfigUtil.API_KEY; // key
股市几点开盘收盘
String currTime = CurrTime();
String strRandom = PayCommonUtil.buildRandom(4) + "";1升等于多少ml
String nonce_str = currTime + strRandom;//currTime + strRandom; // 随机字符串
String out_trade_no = "520520";//申请退款的商户订单号
String out_refund_no = out_trade_no + "1314";
String total_fee = "1";
String refund_fee = total_fee;
String op_user_id = mch_id; // 操作员默认为商户号
SortedMap<String, String> m = new TreeMap<String, String>();
m.put("appid", appid);
m.put("mch_id", mch_id);
m.put("nonce_str", nonce_str);
m.put("out_trade_no", out_trade_no);
m.put("out_refund_no", out_refund_no);
m.put("total_fee", total_fee);
m.put("refund_fee", refund_fee);
m.put("op_user_id", op_user_id);
//签名算法
String sign = ateSign("UTF-8", m, key);
m.put("sign", sign);
//将集合转换成xml
String requestXML = RequestXml(m);
//带证书的post
String resXml = CertHttpUtil.postData(PayConfigUtil.REFUND_API, requestXML);
//解析xml为集合,请打断点查看resXml详细信息
Map<String, String> map = XMLUtil.doXMLParse(resXml);
//查看申请退款状态
PrintWriter out = Writer();
out.("result_code"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2.带证书的POST提交
要先调⽤证书,然后post提交,证书使⽤基本上是httpclient内容(⽹上抄来的,具体还没研究)
import java.io.File;
import java.io.FileInputStream;
2012思想汇报import java.io.IOException;
import java.security.KeyStore;
import javax.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.fig.RequestConfig;
import org.apache.hods.HttpPost;
import org.ssl.SSLConnectionSocketFactory;
import org.ssl.SSLConnectionSocketFactory; import org.ssl.SSLContexts;
import org.ity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class CertHttpUtil {
private static int socketTimeout = 10000;// 连接超时时间,默认10秒
private static int connectTimeout = 30000;// 传输超时时间,默认30秒 private static RequestConfig requestConfig;// 请求器的配置
private static CloseableHttpClient httpClient;// HTTP请求器
/**
* 通过Https往API post xml数据
* @param url API地址
* @param xmlObj 要提交的XML数据对象
* @return
*/
public static String postData(String url, String xmlObj) {
// 加载证书
try {
initCert();
} catch (Exception e) {
e.printStackTrace();
}
String result = null;
HttpPost httpPost = new HttpPost(url);
// 得指明使⽤UTF-8编码,否则到API服务器XML的中⽂不能被成功识别  StringEntity postEntity = new StringEntity(xmlObj, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(postEntity);
// 根据默认超时限制初始化requestConfig
requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout)
.build();
应用电子技术// 设置请求器的配置
httpPost.setConfig(requestConfig);
try {
HttpResponse response = null;
try {
response = ute(httpPost);
}  catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = Entity();
try {
result = String(entity, "UTF-8");
}  catch (IOException e) {
e.printStackTrace();
}
} finally {
httpPost.abort();
}
return result;
}
/**
* 加载证书
*
*/
private static void initCert() throws Exception {
// 证书密码,默认为商户ID
String key = PayConfigUtil.MCH_ID;
一五一十道来打一成语
String key = PayConfigUtil.MCH_ID;
// 证书的路径
String path = PayConfigUtil.CertPath;
// 指定读取证书格式为PKCS12
KeyStore keyStore = Instance("PKCS12");
// 读取本机存放的PKCS12证书⽂件
FileInputStream instream = new FileInputStream(new File(path));
try {
// 指定PKCS12的密码(商户ID)
keyStore.load(instream, CharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts
.custom()
.loadKeyMaterial(keyStore, CharArray())
.build();作文我的母亲
// 指定TLS版本
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);  // 设置httpclient的SSLSocketFactory
httpClient = HttpClients
.custom()
.setSSLSocketFactory(sslsf)
.build();
}
}

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