接入微信JSAPI被扫码支付--JAVA

微信公众号的永久二维码,在根据官方提供的文档踩了N个坑后,终于是调试通了,写个随笔文章。

一、纯属个人理解:

1、微信JSAPI是对于微信公众号来接入开发的,需要有微信公众号和商户号。

2、提供生成的二维码,做线下支付。客户通过扫二维码,输入金额,实时生成订单支付。

3、简单点说就实现微信的个人对商户的转账功能。


二、微信JSAPI支付的基本流程

1、用户打开微信客户端扫码二维码

2、打开微信内置浏览器,登陆授权(获取微信服务端返回的参数openid)

3、输入金额,调用统一下单接口,提交生成订单(获取微信服务端返回的参数prepay_id)

4、用户确认订单,发起支付。

5、输入密码,完成支付。


三、代码

1、授权获取openid

授权可以查看官方文档 http://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html ,获取CODE,根据code参数获取openid

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取配置文件中配置的生产参数
        Map map = loadConfiguration.getWechat("wechat");

        //网页授权获取CODE
        String code = request.getParameter("code");
        //获取商户生产参数
        String appid = (String)map.get("appid");
        String appsecret = (String)map.get("appsecret");
        String openId ="";
        String tempValue="";
        String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";

        HttpResponse temp = HttpConnect.getInstance().doGetStr(URL);        
        
        if( temp == null){
            System.out.println("error");
            response.sendRedirect("/pay/error.jsp");
        }else{
            try {
                tempValue = temp.getStringResult();
            } catch (Exception e) {
                e.printStackTrace();
            }
            JSONObject  jsonObj = JSONObject.fromObject(tempValue);
            if(jsonObj.containsKey("errcode")){
                response.sendRedirect("/pay/error.jsp");
            }
            openId = jsonObj.getString("openid");
        }
        
        response.sendRedirect("/pay/payment.jsp?openId="+openId+"");
    }

2、用户输入金额,发起订单支付请求 payment.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String openId = request.getParameter("openId");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
    <title>微信JSAPI支付</title>
  </head>
  
  <body>
    <div>
      <h3 style="font-size: large;">企业微信-线下扫码支付</h3>
    </div>
  	<div>
        <form id=alipayment action="/topayServlet" method="post" target="_blank" οnsubmit="return checkForm()">
        <div>
          
          <div style="margin-right: 5%;margin-left: 5%;">
          <div>
				    <dt style="font-size: larger;">金额:</dt>
            <dt>
  						<input id="amount" name="amount" value="" type="number" placeholder="请输入数字金额,单位元" style="width: 100%;height: 2rem;float: left;background: white;" />
            </dt>
          </div>
          <footer style="text-align: center;background: green;">
            <input type="hidden" id="openId" name="openId" value="<%=openId%>" />
            <input type="submit" id="subBtn" value="提交" style="font-size: inherit;width: 100%; height: 2.5rem; background: green;" />
          </footer>
          </div>
        </div>
		</form>
  	</div>

  <script type="text/javascript">
  function checkForm(){
    var amount = document.getElementById("amount").value;

    if (amount === "") {
      alert("金额不能为空");
      return false;
    }else{
      return true;
    }
  }
  </script>
  </body>
</html>



3、生成预支付订单,获取prepay_id

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	Map map = loadConfiguration.getWechat("wechat");
	//获取商户生产参数
        String appid = (String)map.get("appid");
        String appsecret = (String)map.get("appsecret");
        String mch_id = (String)map.get("partner");
        String partnerkey = (String)map.get("partnerkey");
	String notify_url = (String)map.get("notify_url");
	String trade_type = (String)map.get("trade_type");

	//网页授权后根据填入的金额自动生成订单
	String money = request.getParameter("amount");
        String openid = request.getParameter("openId");
        String prepay_id="";
		
	//金额转化为分为单位
	float sessionmoney = Float.parseFloat(money);
	String finalmoney = String.format("%.2f", sessionmoney);
	finalmoney = finalmoney.replace(".", "");
	int total_fee = Integer.parseInt(finalmoney);
	finalmoney = String.valueOf(total_fee);
	System.out.println(openid+"***"+money+"***"+finalmoney+"***"+total_fee);
		
        //获取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 nonce_str = strTime + strRandom;
	//商品描述根据情况修改
	String body = "";
	//商户订单号
	String out_trade_no = currTime+strRandom;
	//订单生成的机器 IP
	String spbill_create_ip = request.getRemoteAddr();

	SortedMap<String, String> packageParams = new TreeMap<String, String>();
	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);  
	packageParams.put("total_fee", finalmoney);  
	packageParams.put("spbill_create_ip", spbill_create_ip);  
	packageParams.put("notify_url", notify_url);  
	packageParams.put("trade_type", trade_type);  
	packageParams.put("openid", openid);  

	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>"+
		"<total_fee>"+total_fee+"</total_fee>"+
		"<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"+
		"<notify_url>"+notify_url+"</notify_url>"+
		"<trade_type>"+trade_type+"</trade_type>"+
		"<openid>"+openid+"</openid>"+
		"</xml>";
	System.out.println(xml);	
		
	String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
		
	try {
                prepay_id = new GetWxOrderno().getPayNo(createOrderURL, xml);
		if(prepay_id.equals("")){
		response.sendRedirect("/pay/error.jsp");
		return;
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	SortedMap<String, String> finalpackage = new TreeMap<String, String>();

	String timestamp = Sha1Util.getTimeStamp();
	String prepay_ids = "prepay_id="+prepay_id;
	finalpackage.put("appId", appid);  
	finalpackage.put("timeStamp", timestamp);  
	finalpackage.put("nonceStr", nonce_str);  
	finalpackage.put("package", prepay_ids);  
	finalpackage.put("signType", "MD5");
	String finalsign = reqHandler.createSign(finalpackage);
		
	response.sendRedirect("/pay/pay.jsp?appid="+appid+"&timeStamp="+timestamp+"&nonceStr="+nonce_str+"&package="+prepay_ids+"&sign="+finalsign);
	}

4、发起支付请求 pay.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String appId = request.getParameter("appid");
String timeStamp = request.getParameter("timeStamp");
String nonceStr = request.getParameter("nonceStr");
String packageValue = request.getParameter("package");
String paySign = request.getParameter("sign");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>微信支付</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">
  	<script type="text/javascript">
		function jsApiCall(){
			 WeixinJSBridge.invoke('getBrandWCPayRequest',{
	  		 "appId" : "<%=appId%>","timeStamp" : "<%=timeStamp%>", "nonceStr" : "<%=nonceStr%>", "package" : "<%=packageValue%>","signType" : "MD5", "paySign" : "<%=paySign%>" 
	   			},function(res){
					WeixinJSBridge.log(res.err_msg);
		            if(res.err_msg == "get_brand_wcpay_request:ok"){  
		                alert("微信支付成功!");  
		                WeixinJSBridge.call("closeWindow");
		            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){  
		                alert("用户取消支付!");  
		            }else{  
		               alert("支付失败!");  
		            }  
				})
			}
		function callpay(){
           if (typeof WeixinJSBridge == "undefined"){
               if (document.addEventListener){
                   document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
               }else if (document.attachEvent){
                   document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                   document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
               }
           }else{
               jsApiCall();
           }
        }
	   	callpay(); 
	</script>
  </head>
  <body>
  </body>

</html>

完成微信JSAPI支付


四、注意

1、登陆微信公众平台——接口权限——网页服务——网页账号。配置好授权回调页面域名,在你第一步授权时通过回调页面获取CODE参数。

2、登陆微信公众平台——微信支付——开发配置——支付授权目录。配置号支付授权目录,在最终发起支付的界面必须要放在该目录下,一级目录、二级目录都有效。(支付授权目录为http://域名/pay/,那么支付界面就在http://域名/pay/pay.jsp)


五、工具

1、在测试阶段可以下载微信web开发者工具进行授权测试  https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455784140&token=&lang=zh_CN   (需要绑定测试微信账号 :登陆微信公众平台——开发者工具——web开发者工具——绑定开发者微信账号)

2、具体demo可以查看上传的 WePay 文件





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值