java微信扫码支付

java 后台

/**
	 * 微信扫码支付
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/weiXinSaoMaPay",method=RequestMethod.POST)
	@ResponseBody
	public String weiXinSaoMaPay(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//获取单号和金额
		String hunhe = "";
		String appid = "";
		String appsecret = "";
		String mch_id = "";
		String partnerkey = "";
		String wangzhi = "";

		// 获取openId后调用统一支付接口https://api.mch.weixin.qq.com/pay/unifiedorder
		String currTime = TenpayUtil.getCurrTime();
		// 8位日期
		String strTime = currTime.substring(8, currTime.length());
		// 四位随机数
		String strRandom = TenpayUtil.buildRandom(4) + "";
		// 10位序列号,可以自行调整。
		String strReq = strTime + strRandom;
		// System.out.println("商户号:" + mch_id);
		// 随机数
		String nonce_str = strReq;
		// 商品描述根据情况修改
		String body = "批发";
		String[] shunzu = hunhe.split(",");
		// 商户订单号
		String out_trade_no = shunzu[0];
		// 金额转化为分为单位
		float sessionmoney = Float.parseFloat(shunzu[1]);
		String finalmoney = String.format("%.2f", sessionmoney);
		finalmoney = finalmoney.replace(".", "");
		int intMoney = Integer.parseInt(finalmoney);

		// 总金额以分为单位,不带小数点
		int total_fee = intMoney;

		// 订单生成的机器 IP
		String spbill_create_ip = localIp();
		// 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。
		String notify_url = wangzhi + "/getChuliShuJu.do";

		String trade_type = "NATIVE";
		SortedMap<String, Object> packageParams = new TreeMap<String, Object>();
		packageParams.put("appid", appid);
		packageParams.put("mch_id", mch_id);
		packageParams.put("nonce_str", nonce_str);
		packageParams.put("body", body);
		packageParams.put("out_trade_no", out_trade_no);
		// 这里写的金额为1 分到时修改
		packageParams.put("total_fee", "1");
		packageParams.put("spbill_create_ip", spbill_create_ip);
		packageParams.put("notify_url", notify_url);
		packageParams.put("trade_type", trade_type);
		
		RequestHandler reqHandler = new RequestHandler(request, response);
		reqHandler.init(appid, appsecret, partnerkey);

		String sign = reqHandler.createSign(packageParams);
		String xml = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>"
				+ mch_id + "</mch_id>" + "<nonce_str>" + nonce_str
				+ "</nonce_str>" + "<sign>" + sign + "</sign>"
				+ "<body><![CDATA[" + body + "]]></body>"
				+ "<out_trade_no>"
				+ out_trade_no
				+ "</out_trade_no>"
				+
				// 金额,这里写的1 分到时修改
				"<total_fee>" + 1 + "</total_fee>" + "<spbill_create_ip>"
				+ spbill_create_ip + "</spbill_create_ip>" + "<notify_url>"
				+ notify_url + "</notify_url>" + "<trade_type>" + trade_type
				+ "</trade_type>"+"</xml>";
		
		GetWxOrderno gd = new GetWxOrderno();
		//返回的二维码调用链接
		String code_url = gd.getPayNo("https://api.mch.weixin.qq.com/pay/unifiedorder", xml,"code_url");
		return code_url;
	}

获取本机ip

<pre name="code" class="java">/**
     * 获取本机Ip 
     *  
     *  通过 获取系统所有的networkInterface网络接口 然后遍历 每个网络下的InterfaceAddress组。
     *  获得符合 <code>InetAddress instanceof Inet4Address</code> 条件的一个IpV4地址
     * @return
     */
    @SuppressWarnings("rawtypes")
    private String localIp(){
        String ip = null;
        Enumeration allNetInterfaces;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();            
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
                for (InterfaceAddress add : InterfaceAddress) {
                    InetAddress Ip = add.getAddress();
                    if (Ip != null && Ip instanceof Inet4Address) {
                        ip = Ip.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {     
            System.out.println("获取本机Ip失败:异常信息:"+e.getMessage());
        }
        return ip;
    }


 

jsp前端

<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试扫码支付</title>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js2/jquery-1.4.2.min.js"></script>
<script src="../js/qrcode.js"></script>

</head>
<body>
	<div align="center" id="qrcode"></div>
</body>
<script type="text/javascript">
	$.post("${pageContext.request.contextPath}/weiXinSaoMaPay.do",
			function(re) {
				//这个地址是Demo.java生成的code_url,这个很关键
				var url = re;

				//参数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>


 



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值