微信扫码支付 模式二

微信扫码支付应该是最简单的 就只是对生成xml和生成二维码还有http访问这三方面有要求

步骤就是:
1、获得对应的数据 比如appid mch_id之类的必要参数
2、用随机数算法生成一个随机数nonce_str
3、把所有的参数包括随机数都用签名算法算出一个签名字符串
4、把所有参数包括随机数、签名 都放到map里面 把map转换成xml
5、用HttpUtil.postData把xml提交到对应的微信api上
6、微信api会返回一个xml 其中一个是code_url
7、把code_url放到二维码生成函数里面 生成一个二维码
8、大功告成 可以用

wechatpay.java

import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import com.demo.utils.HttpUtil;  //http访问函数
import com.demo.utils.XMLUtil;   //XML生成函数

public class wechatpay {

    public static String weixin_pay(PayDao pd) throws Exception {

        String device_info = pd.getDevice_info();

        String body = pd.getBody();
        String detail = pd.getDetail();
        String attach = pd.getAttach();
        String out_trade_no = pd.getOut_trade_no(); //商户订单号
        String fee_type = pd.getFee_type(); //货币类型
        String total_fee = pd.getTotal_fee();

        String currTime = Method.getCurrTime(); 
        String time_start = currTime.substring(8, currTime.length());
        String strRandom = Method.buildRandom(4) + "";
        String nonce_str = time_start + strRandom; //随机数生成算法

        String appid = "wx1111111111111111";
        String app_secret= "e0111111111111111111111111111111";
        String mch_id = "1381111111";
        String key = "e0711111111111111111111111111111";

        String sign = ""; //签名生成算法
        String spbill_create_ip = Method.localIp();//"192.168.27.45"
        String notify_url= "https://"+Method.localIp()+"/Notify"; //通知回调地址
        String trade_type = "NATIVE";
        String UFDODER_URL= "https://api.mch.weixin.qq.com/pay/unifiedorder";

        Map<Object, String> map = new TreeMap<Object, String>();
        map.put("appid", appid);
        map.put("mch_id", mch_id);
        map.put("device_info", device_info);
        map.put("nonce_str", nonce_str);
        map.put("body", body);
        map.put("detail", detail);
        map.put("attach", attach);
        map.put("out_trade_no", out_trade_no);
        map.put("fee_type", fee_type);
        map.put("total_fee", total_fee);
        map.put("spbill_create_ip", spbill_create_ip);
        map.put("notify_url", notify_url);
        map.put("trade_type", trade_type);
        sign = Method.createSign("UTF-8", map,key);
        map.put("sign", sign);

        String requestXML = Method.getRequestXml(map);

        String urlCode = "no date";
        Map map1 = null;

        try{
            String resXml = HttpUtil.postData(UFDODER_URL, requestXML);
            map1 = XMLUtil.doXMLParse(resXml);
            urlCode = (String) map1.get("code_url");
        }catch (ConnectException ce){
            System.out.println("连接超时:"+ce.getMessage());
        }catch (Exception e){
            System.out.println("https请求异常:"+e.getMessage());
        }

        System.out.println(urlCode);
        System.out.println("requestXML::::"+requestXML);
        System.out.println("resXml::::"+HttpUtil.postData(UFDODER_URL, requestXML));
        System.out.println("map1::::"+map1);

        return urlCode;

    }

}

index.jsp

<%@ page language="java" import="java.util.*,com.demo.*,com.demo.dao.*" pageEncoding="UTF-8"%>
<%
    String currTime = Method.getCurrTime(); 
    String time = currTime.substring(0, currTime.length());
    String strRandom = Method.buildRandom(4) + "";
    String Out_trade_no = time+strRandom;//日期+4位随机数
    PayDao pd = new PayDao();
    pd.setDevice_info("WEB");
    pd.setBody("GOODS");
    pd.setDetail("detail..........");
    pd.setAttach("attach...........");
    pd.setOut_trade_no(Out_trade_no);
    pd.setFee_type("CNY");
    pd.setTotal_fee("1");
%>
<html>
  <head>

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <meta charset="UTF-8">
  </head>

  <body>

    <div align="center" id="qrcode">
        <p >
        扫码支付 模式二
        </p>
    </div>

  </body>

  <script src="qrcode.js"></script>
  <script>
    //这个地址是Demo.java生成的code_url,这个很关键
    var url = "<%=wechatpay.weixin_pay(pd) %>";

    //参数1表示图像大小,取值范围1-10;参数2表示质量,取值范围'L','M','Q','H'
    var qr = qrcode(10, 'M');
    qr.addData(url);
    qr.make();
    var dom=document.createElement('DIV');
    dom.innerHTML = qr.createImgTag();
    var element=document.getElementById("qrcode");
    element.appendChild(dom);
 </script>

</html>

基本就甘啦 生成二维码就用jquery的方法 这样快

附件:http://download.csdn.net/detail/qq_22778717/9649776

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值