欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信支付java版本之查询订单

程序员文章站 2024-03-13 17:01:27
本文实例为大家分享了微信支付订单的查询接口,供大家参考,具体内容如下 1.接口简介 2.代码实现   packag...

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

1.接口简介

微信支付java版本之查询订单

微信支付java版本之查询订单

微信支付java版本之查询订单

2.代码实现
 

package com.zhrd.bussinss.platform.controller.shop;

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 javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

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.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.responsebody;

import com.zhrd.bussinss.platform.service.weixinpayservice;
import com.zhrd.bussinss.platform.utils.customizedpropertyplaceholderconfigurer;
import com.zhrd.bussinss.platform.weixinpayutils.clientcustomssl;

@controller
@requestmapping("/shop/weixinpayordersearch")
public class weixinpayordersearchcontroller {
 
 
 @autowired
 weixinpayservice weixinpayservice;
 
 
 
 /**
 * 微信支出订单状态查询
 * @param request
 * @param response
 * @return
 */
 @requestmapping(value="/init",method=requestmethod.get )
 public string init(httpservletrequest request,httpservletresponse response){
 
 return "weixinpayordersearch";
 
 }

 
 /**
 * 微信支出订单状态查询
 * @param request
 * @param response
 * @return
 */
 @requestmapping(value="/getweixinpayorder",method=requestmethod.post )
 @responsebody
 public object getweixinpayorder(httpservletrequest request,httpservletresponse response,@requestparam(required=false) string transactionid,@requestparam(required=false) string outtradeno){
 try{
  
  keystore keystore = keystore.getinstance("pkcs12");
  fileinputstream instream = new fileinputstream(new file(
   customizedpropertyplaceholderconfigurer.getcontextproperty("wx.cert").tostring()));
  try {
  keystore.load(instream, "见邮件".tochararray());
  }finally {
  instream.close();
  }

  // trust own ca and all self-signed certs
  sslcontext sslcontext = sslcontexts.custom().loadkeymaterial(keystore,
   "10061401".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/orderquery");
  date dt = new date();
  simpledateformat sdf = new simpledateformat("yyyymmddhhmmss");
  string noncestr = sdf.format(dt).tostring();<pre name="code" class="java">
  string xml = clientcustomssl.searchnativepackage(transactionid.trim(),"", noncestr);//此处为微信端id(本地id和服务器端id均可查询)
  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());
   /*bufferedreader bufferedreader = new bufferedreader(
    new inputstreamreader(entity.getcontent()));
   string text;
   while ((text = bufferedreader.readline()) != null) {
    system.out.println("======="+text);
   }*/
   
   
   saxreader saxreader = new saxreader();
   document document = saxreader.read(entity.getcontent());
   element rootelt = document.getrootelement();
   system.out.println("根节点:" + rootelt.getname());
   system.out.println("==="+rootelt.elementtext("result_code"));
   system.out.println("==="+rootelt.elementtext("return_msg"));
   string resultcode = rootelt.elementtext("result_code");
   string returncode = rootelt.elementtext("return_code");
   jsonobject result = new jsonobject();
   system.out.println("resultcode====="+resultcode);
   system.out.println("returncode====="+returncode);
   system.out.println("trade_state====="+rootelt.elementtext("trade_state"));
   system.out.println("transaction_id====="+rootelt.elementtext("transaction_id"));
   system.out.println("out_trade_no====="+ rootelt.elementtext("out_trade_no"));
   system.out.println("total_fee====="+rootelt.elementtext("total_fee"));
   
   
   if(resultcode != null && resultcode.equals("success") && returncode.equals("success")){
    result.put("tradestate", rootelt.elementtext("trade_state"));
    result.put("transactionid", rootelt.elementtext("transaction_id"));
    result.put("outtradeno", rootelt.elementtext("out_trade_no"));
    result.put("totalfee", rootelt.elementtext("total_fee"));
    result.put("status","success");
    result.put("msg","success");
   }else if(returncode.equals("success")){
    result.put("errorcheck", "errcode");
    result.put("status","false");
    result.put("errormsg",rootelt.elementtext("err_code_des"));
   }else{
    result.put("errorcheck", "errmsg");
    result.put("status","false");
    result.put("errormsg", rootelt.elementtext("return_msg"));
   }
   return result;

   }
//   entityutils.consume(entity);
  }
  finally {
   responseentry.close();
  }
  }
  finally {
  httpclient.close();
  }
  
  
 }catch(exception e){
  e.printstacktrace();
  jsonobject result = new jsonobject();
  result.put("status","error");
  result.put("msg",e.getmessage());
  return result;
 }
 return "";
 
 
 }

}


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