1.代码实现
根据支付后的订单编号来关闭对应订单
- package com.zhrd.bussinss.platform.utils;
- import java.io.File;
- import java.io.FileInputStream;
- import java.security.KeyStore;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.net.ssl.SSLContext;
- import net.sf.json.JSONObject;
- 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;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
- import org.springframework.transaction.annotation.Transactional;
- import com.zhrd.bussinss.platform.weixinPayUtils.ClientCustomSSL;
- @Transactional
- public class CloseWeiXinOrderUtils {
- public static Object closeWeiXinOrder(String outTradeNo) {
- try{
- KeyStore keyStore = KeyStore.getInstance("PKCS12");
- FileInputStream instream = new FileInputStream(new File(
- CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));
- try {
- keyStore.load(instream, "10061401".toCharArray());
- }finally {
- instream.close();
- }
- // Trust own CA and all self-signed certs
- SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,
- "见邮件".toCharArray()).build();
- // Allow TLSv1 protocol only
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
- sslcontext, new String[] { "TLSv1" }, null,
- SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- CloseableHttpClient httpclient = HttpClients.custom()
- .setSSLSocketFactory(sslsf).build();
- // HttpGet httpget = new
- // HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund");
- HttpPost httppost = new HttpPost(
- "https://api.mch.weixin.qq.com/pay/closeorder");
- Date dt = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
- String nonceStr = sdf.format(dt).toString();
- String xml = ClientCustomSSL.CreateCloseOrder(outTradeNo,nonceStr);
- try {
- StringEntity se = new StringEntity(xml);
- httppost.setEntity(se);
- System.out.println("executing request" + httppost.getRequestLine());
- CloseableHttpResponse responseEntry = httpclient.execute(httppost);
- try {
- HttpEntity entity = responseEntry.getEntity();
- System.out.println("----------------------------------------");
- System.out.println(responseEntry.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: "
- + entity.getContentLength());
- SAXReader saxReader = new SAXReader();
- Document document = saxReader.read(entity.getContent());
- Element rootElt = document.getRootElement();
- String resultCode = rootElt.elementText("result_code");
- String returnCode = rootElt.elementText("return_code");
- System.out.println("resultCode----"+resultCode);
- System.out.println("returnCode---"+returnCode);
- JSONObject result = new JSONObject();
- if(returnCode.equals("SUCCESS")){
- if(resultCode.equals("SUCCESS")){
- result.put("status","success");
- result.put("msg","success");
- }else{
- result.put("status","false");
- result.put("msg",rootElt.elementText("err_code_des"));
- }
- }else{
- result.put("status","false");
- result.put("msg",rootElt.elementText("return_msg"));
- }
- return result;
- }
- EntityUtils.consume(entity);
- }
- finally {
- responseEntry.close();
- }
- }
- finally {
- httpclient.close();
- }
- return null;
- }catch(Exception e){
- e.printStackTrace();
- JSONObject result = new JSONObject();
- result.put("status","error");
- result.put("msg",e.getMessage());
- return result;
- }
- }
- }