接上一文 通知 、我们接下来是实现微信支付的 订单查询接口,在文档中是这样的
我们需要传 接口类型、商户号、商户订单号或平台订单号、随机字符串、签名、者五个参数,一下是demo的示例
1 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 2 req.setCharacterEncoding("utf-8"); 3 resp.setCharacterEncoding("utf-8"); 4 SortedMap<String,String> map = XmlUtils.getParameterMap(req); //这里只TreeMap(); 如果没有用request传参的话可以new一个TreeMap()就可以了。 5 System.out.println(XmlUtils.toXml(map)); 6 map.put("mch_id", SwiftpassConfig.mch_id); 7 String key = SwiftpassConfig.key; 8 String res = null; 9 String reqUrl = SwiftpassConfig.req_url; 10 map.put("nonce_str", String.valueOf(new Date().getTime())); 11 Map<String,String> params = SignUtils.paraFilter(map); 12 StringBuilder buf = new StringBuilder((params.size() +1) * 10); 13 SignUtils.buildPayParams(buf,params,false); 14 String preStr = buf.toString(); 15 String sign = MD5.sign(preStr, "&key=" + key, "utf-8"); 16 map.put("sign", sign); 17 18 System.out.println("reqUrl:" + reqUrl); 19 20 CloseableHttpResponse response = null; 21 CloseableHttpClient client = null; 22 try { 23 HttpPost httpPost = new HttpPost(reqUrl); 24 StringEntity entityParams = new StringEntity(XmlUtils.parseXML(map),"utf-8"); 25 httpPost.setEntity(entityParams); 26 httpPost.setHeader("Content-Type", "text/xml;charset=ISO-8859-1"); 27 client = HttpClients.createDefault(); 28 response = client.execute(httpPost); 29 if(response != null && response.getEntity() != null){ 30 Map<String,String> resultMap = XmlUtils.toMap(EntityUtils.toByteArray(response.getEntity()), "utf-8"); 31 res = XmlUtils.toXml(resultMap); 32 System.out.println("请求结果:" + res); 33 34 if(resultMap.containsKey("sign") && !SignUtils.checkParam(resultMap, key)){ 35 res = "验证签名不通过"; 36 } 37 }else{ 38 res = "操作失败!"; 39 } 40 } catch (Exception e) { 41 e.printStackTrace(); 42 res = "操作失败"; 43 } finally { 44 if(response != null){ 45 response.close(); 46 } 47 if(client != null){ 48 client.close(); 49 } 50 } 51 if(res.startsWith("<")){ 52 resp.setHeader("Content-type", "text/xml;charset=UTF-8"); 53 }else{ 54 resp.setHeader("Content-type", "text/html;charset=UTF-8"); 55 } 56 resp.getWriter().write(res); 57 }
在demo中大多数参数都是封装在了request中了、所以我们在本地需要这样
1 public Pair<Boolean, Map<String, String>> queryACcountOrderInfo(Payparams params) throws Exception { 2 boolean result = false; 3 String merId = params.getMerId(); 4 String orderId = params.getOrderId(); 5 HashMap<String, String> rspData = new HashMap<String,String>(); 6 Map<String,String> resultMap = new HashMap<String, String>(); 7 //组织请求报文------------------------------- 8 9 SortedMap<String,String> map = new TreeMap<String, String>(); 10 map.put("service", "unified.trade.query"); //接口类型、必传 11 map.put("version", "2.0"); 12 map.put("charset", "UTF-8"); 13 map.put("sign_type", "MD5"); 14 map.put("body", "测试购买商品"); 15 map.put("attach", "附加信息"); 16 logger.info(XmlUtils.toXml(map)); 17 String key = "***"; 18 String res = null; 19 String reqUrl = "https://pay.swiftpass.cn/pay/gateway"; 20 map.put("mch_id", merId); // 商户号 21 map.put("out_trade_no", orderId); //订单号 22 map.put("transaction_id", ""); //平台订单号 -- 流水号 23 map.put("nonce_str", String.valueOf(new Date().getTime())); 24 25 Map<String,String> params2 = SignUtils.paraFilter(map); 26 StringBuilder buf = new StringBuilder((params2.size() +1) * 10); 27 SignUtils.buildPayParams(buf,params2,false); 28 String preStr = buf.toString(); 29 String sign = MD5.sign(preStr, "&key=" + key, "utf-8"); 30 map.put("sign", sign); 31 32 System.out.println("reqUrl:" + reqUrl); 33 34 CloseableHttpResponse response = null; 35 CloseableHttpClient client = null; 36 try { 37 HttpPost httpPost = new HttpPost(reqUrl); 38 StringEntity entityParams = new StringEntity(XmlUtils.parseXML(map),"utf-8"); 39 httpPost.setEntity(entityParams); 40 httpPost.setHeader("Content-Type", "text/xml;charset=ISO-8859-1"); 41 client = HttpClients.createDefault(); 42 response = client.execute(httpPost); 43 if(response != null && response.getEntity() != null){ 44 resultMap = XmlUtils.toMap(EntityUtils.toByteArray(response.getEntity()), "utf-8"); 45 res = XmlUtils.toXml(resultMap); 46 logger.info("请求结果:" + res); 47 48 if(resultMap.containsKey("sign") && !SignUtils.checkParam(resultMap, key)){ 49 res = "验证签名不通过"; 50 logger.error("验证签名不通过"); 51 } 52 }else{ 53 res = "操作失败!"; 54 logger.error("操作失败!"); 55 } 56 } catch (Exception e) { 57 e.printStackTrace(); 58 res = "操作失败"; 59 logger.error("操作失败!" + e.getMessage()); 60 } finally { 61 if(response != null){ 62 response.close(); 63 } 64 if(client != null){ 65 client.close(); 66 } 67 } 68 69 //转换成json对象 70 JSONObject respJson = JSONObject.fromObject(resultMap); 72 String resultStr = respJson.toString(); 73 logger.info("strRspJsonStr:"+resultStr); 74 75 Assert.notNull(respJson.get("trade_state"), "微信查询返回码为空,查询失败!!!"); 76 String respCode = respJson.getString("trade_state"); 77 Assert.notNull(respJson.get("status"), "微信查询返回码为空,查询失败!!!"); 78 String resultCode = respJson.getString("status"); 79 80 PayRes payRes = PayReturnCode.getPayRes(Contents.ZFB_CODE, respCode); 81 logger.info("查询结果,返回码:" + respCode + ", 查询信息:" + payRes.getMsg()); 82 87 if("SUCCESS".equals(respCode) && "0".equals(resultCode)){ 88 rspData.put("respCode", "05"); 89 }else{ 90 rspData.put("respCode", "?"); 91 }
//日期格式化 92 String payTime = null; 93 if( respJson.getString("time_end") != null){ 94 String date = respJson.getString("time_end").toString(); 95 String reg = "(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})"; 96 payTime = date.replaceAll(reg, "$1-$2-$3 $4:$5:$6"); 97 } 98 99 100 rspData.put("queryId", respJson.getString("transaction_id")); // 平台流水号,类似检索参考号 101 rspData.put("merOrderId", respJson.getString("out_trade_no"));// 商户订单号 102 rspData.put("txnAmt", Double.toString((Double.parseDouble(respJson.getString("total_fee"))/100))); // 支付总金额 103 rspData.put("txnTime", payTime); // 支付时间,格式yyyy-MM-dd HH:mm:ss 104 return new Pair<Boolean, Map<String,String>>(result, rspData); 105 }
如上所示、就是我们组装的查询微信支付订单信信息。我们只需要获取到这些数据后进行处理就可以了。