微信H5支付核心

public class PayH5wechatServlet extends HttpServlet{
    
    /**
     * 
     */
    private static final long serialVersionUID = 2800310217386234064L;
    private Logger logger = Logger.getLogger(PayH5wechatServlet.class);

    public PayH5wechatServlet(){
        super();
    }
    
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Unifiedorder unifiedorder = new Unifiedorder();
        unifiedorder.setAppid("wx55258189d17b307d");
        unifiedorder.setMch_id("1352451102");
        unifiedorder.setSub_mch_id("1352451102");
        unifiedorder.setNonce_str("1add1a30ac87aa2db72f57a2375d8fec");
        unifiedorder.setSign("C380BEC2BF5K8264ILTKCH16CQ2502SI8ZNMTM67VSD727A4B6845133519F3AD6");
        unifiedorder.setBody("购买会员");
        unifiedorder.setOut_trade_no("20150806125346");
        unifiedorder.setTotal_fee(1);
        unifiedorder.setSpbill_create_ip("10.0.68.122");
        unifiedorder.setNotify_url("http://baidu.com");
        unifiedorder.setTrade_type("MWEB");
        unifiedorder.setScene_info("{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"http://www.xxxx.com\",\"wap_name\": \"学易资源分享平台\"}}");

        Map<String, String> msgMap = getUrl(unifiedorder);
        logger.info("msgMap :" + msgMap);
        if (msgMap.get("return_code").equals("SUCCESS") 
                && msgMap.get("result_code").equals("SUCCESS")) {
            logger.info("支付成功");
        }
     } 
    
    /**
     * h5微信支付跳转通过后跳转页面
     */
    public String doH5wechatOrderPay( HttpServletRequest request,HttpServletResponse response) {
            request.setAttribute("OrderId","20150806125346");
            request.setAttribute("ECustName","向腾讯冲QQ会员");
            request.setAttribute("OrderAmout","1");//订单金额 
            return "onlinePay/testOnlinePaygate";  //页面跳转
    }
    
    
    public String xmlH5Info(Unifiedorder unifiedorder){
        if(unifiedorder != null){
            StringBuffer bf = new StringBuffer();
            bf.append("<xml>");
            
            bf.append("<appid><![CDATA[");
            bf.append(unifiedorder.getAppid());
            bf.append("]]></appid>");
            
            bf.append("<mch_id><![CDATA[");
            bf.append(unifiedorder.getMch_id());
            bf.append("]]></mch_id>");
            
            bf.append("<nonce_str><![CDATA[");
            bf.append(unifiedorder.getNonce_str());
            bf.append("]]></nonce_str>");
            
            bf.append("<sign><![CDATA[");
            bf.append(unifiedorder.getSign());
            bf.append("]]></sign>");
            
            bf.append("<body><![CDATA[");
            bf.append(unifiedorder.getBody());
            bf.append("]]></body>");
            
            bf.append("<out_trade_no><![CDATA[");
            bf.append(unifiedorder.getOut_trade_no());
            bf.append("]]></out_trade_no>");
            
            bf.append("<total_fee><![CDATA[");
            bf.append(unifiedorder.getTotal_fee());
            bf.append("]]></total_fee>");
            
            bf.append("<spbill_create_ip><![CDATA[");
            bf.append(unifiedorder.getSpbill_create_ip());
            bf.append("]]></spbill_create_ip>");
            
            bf.append("<notify_url><![CDATA[");
            bf.append(unifiedorder.getNotify_url());
            bf.append("]]></notify_url>");
            
            bf.append("<trade_type><![CDATA[");
            bf.append(unifiedorder.getTrade_type());
            bf.append("]]></trade_type>");
            
            bf.append("<scene_info><![CDATA[");
            bf.append(unifiedorder.getScene_info());
            bf.append("]]></scene_info>");
            
            bf.append("</xml>");
            return bf.toString();
        }
        return "";
        
    }
    
    public String httpsRequest(String requestUrl,String requestMethod,String output) {
        try{
            URL url = new URL(requestUrl);
            HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            
            connection.setRequestMethod(requestMethod);
            if(null != output){
                OutputStream outputStream = connection.getOutputStream();
                outputStream.write(output.getBytes("UTF-8"));
                outputStream.close();
            }
            
            InputStream inputStream  = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while((str = bufferedReader.readLine()) != null){
                buffer.append(str);
            }
            
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            
            inputStream = null ;
            connection.disconnect();    
            return buffer.toString();
            
            }catch(Exception e){
            e.printStackTrace();
        }
        return "";
    }
    
    
    
    public Map<String,String> getUrl(Unifiedorder unifiedorder){
        String xmlInfo = xmlH5Info(unifiedorder);
        String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        String method = "POST";
        
        String weixinPost = httpsRequest(wxUrl,method,xmlInfo);
        logger.info("weixinPost :" + weixinPost);
        //Document doc = StringToXml(weixinPost);
        //ParseXMLUtils.jdomParseXml(weixinPost);
        
//        StringReader reader = new StringReader(weixinPost);
//        InputSource source = new InputSource(reader);
        
        /*Gson gson = new Gson();
        Map<String,String> map = new HashMap<String,String>();
        map = gson.fromJson(weixinPost, map.getClass());
        return map;*/
        Document doc = StringToXml(weixinPost);
        String return_msg_Path = "/xml/return_msg";
        
        String return_msg = getNodeValue(doc,return_msg_Path);
        Map<String,String> map = new HashMap<String,String>();
        map.put("return_msg", return_msg);
        
        return map;
        
        
    }
    
    public Document StringToXml(String str){
        StringBuilder sXml = new StringBuilder();
        sXml.append(str);
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document doc = null;
        
        try{
            InputStream is = new ByteArrayInputStream(sXml.toString().getBytes("utf-8"));
            doc = dbf.newDocumentBuilder().parse(is);
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return doc;
    }
    
    
    public String getNodeValue(Document document,String nodePath){
        XPathFactory xpfactory = XPathFactory.newInstance();
        XPath path =  xpfactory.newXPath();
        String servInitrBrch = "";
        try{
            servInitrBrch = path.evaluate(nodePath, document);
        }catch(XPathExpressionException e){
            e.printStackTrace();
        }
        return servInitrBrch;
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值