UDN-企业互联网技术人气社区

板块导航

浏览  : 929
回复  : 2

[原生js] 微信支付java版本之查询订单

[复制链接]
西北的风的头像 楼主
发表于 2017-1-17 16:43:24 | 显示全部楼层 |阅读模式
  这篇文章主要为大家详细介绍了微信支付java版本之查询订单,为大家分享了微信支付订单的查询接口,感兴趣的小伙伴们可以参考一下

  本文实例为大家分享了微信支付订单的查询接口,供大家参考,具体内容如下

  1.接口简介
 
201608041441224.png
 
201608041441235.png
201608041441236.png



  2.代码实现
  1. package com.zhrd.bussinss.platform.controller.shop;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.security.KeyStore;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;

  7. import javax.net.ssl.SSLContext;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;

  10. import net.sf.json.JSONObject;

  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  15. import org.apache.http.conn.ssl.SSLContexts;
  16. import org.apache.http.entity.StringEntity;
  17. import org.apache.http.impl.client.CloseableHttpClient;
  18. import org.apache.http.impl.client.HttpClients;
  19. import org.dom4j.Document;
  20. import org.dom4j.Element;
  21. import org.dom4j.io.SAXReader;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Controller;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestMethod;
  26. import org.springframework.web.bind.annotation.RequestParam;
  27. import org.springframework.web.bind.annotation.ResponseBody;

  28. import com.zhrd.bussinss.platform.service.WeiXinPayService;
  29. import com.zhrd.bussinss.platform.utils.CustomizedPropertyPlaceholderConfigurer;
  30. import com.zhrd.bussinss.platform.weixinPayUtils.ClientCustomSSL;

  31. @Controller
  32. @RequestMapping("/shop/weiXinPayOrderSearch")
  33. public class WeiXinPayOrderSearchController {


  34. @Autowired
  35. WeiXinPayService weiXinPayService;



  36. /**
  37. * 微信支出订单状态查询
  38. * @param request
  39. * @param response
  40. * @return
  41. */
  42. @RequestMapping(value="/init",method=RequestMethod.GET )
  43. public String init(HttpServletRequest request,HttpServletResponse response){

  44. return "weixinPayOrderSearch";

  45. }


  46. /**
  47. * 微信支出订单状态查询
  48. * @param request
  49. * @param response
  50. * @return
  51. */
  52. @RequestMapping(value="/getWeiXinPayOrder",method=RequestMethod.POST )
  53. @ResponseBody
  54. public Object getWeiXinPayOrder(HttpServletRequest request,HttpServletResponse response,@RequestParam(required=false) String transactionId,@RequestParam(required=false) String outTradeNo){
  55. try{
  56.   
  57.   KeyStore keyStore = KeyStore.getInstance("PKCS12");
  58.   FileInputStream instream = new FileInputStream(new File(
  59.    CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));
  60.   try {
  61.   keyStore.load(instream, "见邮件".toCharArray());
  62.   }finally {
  63.   instream.close();
  64.   }

  65.   // Trust own CA and all self-signed certs
  66.   SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,
  67.    "10061401".toCharArray()).build();
  68.   // Allow TLSv1 protocol only
  69.   SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  70.    sslcontext, new String[] { "TLSv1" }, null,
  71.    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  72.   CloseableHttpClient httpclient = HttpClients.custom()
  73.    .setSSLSocketFactory(sslsf).build();
  74.   // HttpGet httpget = new
  75.   // HttpGet("https://API.mch.weixin.qq.com/secapi/pay/refund");
  76.   HttpPost httppost = new HttpPost(
  77.    "https://api.mch.weixin.qq.com/pay/orderquery");
  78.   Date dt = new Date();
  79.   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  80.   String nonceStr = sdf.format(dt).toString();<pre name="code" class="java">
  81.   String xml = ClientCustomSSL.SearchNativePackage(transactionId.trim(),"", nonceStr);//此处为微信端id(本地id和服务器端id均可查询)
  82.   try {
  83.   
  84.   StringEntity se = new StringEntity(xml);
  85.   
  86.   httppost.setEntity(se);

  87.   System.out.println("executing request" + httppost.getRequestLine());

  88.   CloseableHttpResponse responseEntry = httpclient.execute(httppost);
  89.   try {
  90.    HttpEntity entity = responseEntry.getEntity();

  91.    System.out.println("----------------------------------------");
  92.    System.out.println(responseEntry.getStatusLine());
  93.    if (entity != null) {
  94.    System.out.println("Response content length: "
  95.     + entity.getContentLength());
  96.    /*BufferedReader bufferedReader = new BufferedReader(
  97.     new InputStreamReader(entity.getContent()));
  98.    String text;
  99.    while ((text = bufferedReader.readLine()) != null) {
  100.     System.out.println("======="+text);
  101.    }*/
  102.    
  103.    
  104.    SAXReader saxReader = new SAXReader();
  105.    Document document = saxReader.read(entity.getContent());
  106.    Element rootElt = document.getRootElement();
  107.    System.out.println("根节点:" + rootElt.getName());
  108.    System.out.println("==="+rootElt.elementText("result_code"));
  109.    System.out.println("==="+rootElt.elementText("return_msg"));
  110.    String resultCode = rootElt.elementText("result_code");
  111.    String returnCode = rootElt.elementText("return_code");
  112.    JSONObject result = new JSONObject();
  113.    System.out.println("resultCode====="+resultCode);
  114.    System.out.println("returnCode====="+returnCode);
  115.    System.out.println("trade_state====="+rootElt.elementText("trade_state"));
  116.    System.out.println("transaction_id====="+rootElt.elementText("transaction_id"));
  117.    System.out.println("out_trade_no====="+ rootElt.elementText("out_trade_no"));
  118.    System.out.println("total_fee====="+rootElt.elementText("total_fee"));
  119.    
  120.    
  121.    if(resultCode != null && resultCode.equals("SUCCESS") && returnCode.equals("SUCCESS")){
  122.     result.put("tradeState", rootElt.elementText("trade_state"));
  123.     result.put("transactionId", rootElt.elementText("transaction_id"));
  124.     result.put("outTradeNo", rootElt.elementText("out_trade_no"));
  125.     result.put("totalFee", rootElt.elementText("total_fee"));
  126.     result.put("status","success");
  127.     result.put("msg","success");
  128.    }else if(returnCode.equals("SUCCESS")){
  129.     result.put("errorCheck", "errCode");
  130.     result.put("status","false");
  131.     result.put("errorMsg",rootElt.elementText("err_code_des"));
  132.    }else{
  133.     result.put("errorCheck", "errMsg");
  134.     result.put("status","false");
  135.     result.put("errorMsg", rootElt.elementText("return_msg"));
  136.    }
  137.    return result;

  138.    }
  139. //   EntityUtils.consume(entity);
  140.   }
  141.   finally {
  142.    responseEntry.close();
  143.   }
  144.   }
  145.   finally {
  146.   httpclient.close();
  147.   }
  148.   
  149.   
  150. }catch(Exception e){
  151.   e.printStackTrace();
  152.   JSONObject result = new JSONObject();
  153.   result.put("status","error");
  154.   result.put("msg",e.getMessage());
  155.   return result;
  156. }
  157. return "";


  158. }

  159. }
复制代码


  以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支。

相关帖子

发表于 2017-1-17 16:43:54 来自手机 | 显示全部楼层
OMG!介是啥东东
使用道具 举报

回复

发表于 2017-1-23 19:13:27 | 显示全部楼层
占坑编辑ing
使用道具 举报

回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们
联系我们
  • 电话:010-86393388
  • 邮件:udn@yonyou.com
  • 地址:北京市海淀区北清路68号
移动客户端下载
关注我们
  • 微信公众号:yonyouudn
  • 扫描右侧二维码关注我们
  • 专注企业互联网的技术社区
版权所有:用友网络科技股份有限公司82041 京ICP备05007539号-11 京公网网备安1101080209224 Powered by Discuz!
快速回复 返回列表 返回顶部