Android微信支付,通知回调方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jianfengwen/article/details/47783257
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import com.tenpay.ResponseHandler;
import com.tenpay.util.TenpayUtil;



/**
 * 微信回调Servlet
 *
 */
public class WXPayRspServlet extends HttpServlet {

	private static final long serialVersionUID = -3523419191502630036L;
	private final static Logger log = Logger.getLogger(<span style="font-family: Arial, Helvetica, sans-serif;">WXPayRspServlet</span><span style="font-family: Arial, Helvetica, sans-serif;">.class);</span>
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletInputStream in = req.getInputStream();
		ResponseHandler resHandler = new ResponseHandler(req, resp);
		String resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
		try{
			int size = req.getContentLength(); 
			byte[] bdata = new byte[size];
			in.read(bdata);
			String xmlstring = new String(bdata,TenpayUtil.getCharacterEncoding(req, resp));
			log.info("****** back data xml *******\n");
			log.info(xmlstring);
			if(!StringUtils.isEmpty(xmlstring)){
				Map paramMap = parseXmlToMap(xmlstring);
				/*
				 	WxPayResult wpr = new WxPayResult();
					wpr.setAppid(m.get("appid").toString());
					wpr.setBankType(m.get("bank_type").toString());
					wpr.setCashFee(m.get("cash_fee").toString());
					wpr.setFeeType(m.get("fee_type").toString());
					wpr.setIsSubscribe(m.get("is_subscribe").toString());
					wpr.setMchId(m.get("mch_id").toString());
					wpr.setNonceStr(m.get("nonce_str").toString());
					wpr.setOpenid(m.get("openid").toString());
					wpr.setOutTradeNo(m.get("out_trade_no").toString());
					wpr.setResultCode(m.get("result_code").toString());
					wpr.setReturnCode(m.get("return_code").toString());
					wpr.setSign(m.get("sign").toString());
					wpr.setTimeEnd(m.get("time_end").toString());
					wpr.setTotalFee(m.get("total_fee").toString());
					wpr.setTradeType(m.get("trade_type").toString());
					wpr.setTransactionId(m.get("transaction_id").toString());
				 */
				String result_code = paramMap.get("result_code").toString();
				
				if("SUCCESS".equals(result_code.toUpperCase())){
					//根据自身业务编写代码
				}
			}else{
				resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
				log.error("微信返回报文为空");
			}
			
		}catch(Exception e){
			log.error(e.getMessage(), e);
			e.printStackTrace();
		}
		log.info(resXml);
		resHandler.sendToCFT(resXml);
		
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
	private Map parseXmlToMap(String xml) {
		Map retMap = new HashMap();
		try {
			StringReader read = new StringReader(xml);
			// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
			InputSource source = new InputSource(read);
			// 创建一个新的SAXBuilder
			SAXBuilder sb = new SAXBuilder();
			// 通过输入源构造一个Document
			org.jdom.Document doc =  sb.build(source);
			Element root = (Element) doc.getRootElement();// 指向根节点
			List<Element> es = root.getChildren();
			if (es != null && es.size() != 0) {
				for (Element element : es) {
					retMap.put(element.getName(), element.getValue());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retMap;
	}
}


tenpay的方法如下:


	public void sendToCFT(String msg) throws IOException {
		String strHtml = msg;
		PrintWriter out = this.getHttpServletResponse().getWriter();
		out.println(strHtml);
		out.flush();
		out.close();

	}

其中:
<span style="white-space:pre">	</span>WxPayResult wpr = new WxPayResult();

注解部分为微信返回的信息。


sendToCFT方法为响应微信方法,即<pre name="code" class="java"><return_code><![CDATA[SUCCESS]]></return_code>
是告诉微信已经可以了,不用再发通知(微信一般会发4次通知)。


展开阅读全文

没有更多推荐了,返回首页