这个微信支付是静态二维码支付,就是店面贴着一个二维码,让消费者自己扫自己输入金额,自己发起支付的支付方式。
要准备的东西比较麻烦:
1、到微信公众号平台设置Oauth2的网页验证域名(用于获取code,code用于拿到发起支付的openId),格式是www.xxxx.com/file1/file2/,不需要https:// 要精确到发起支付页面的当前路径
2、配置Oauth2网页验证域名的时候,需要下载一个txt,放到发起支付页面的统计目录
3、到微信公众号平台还是商户平台设置支付授权地址(用于发起支付),不会设置可以发邮件到微信工作人员邮箱,申请处理,邮箱要自己挖(这个地址需要有https://)
4、拿到微信号的appid、mch_id、appscret、key、退款证书、sub_mch_id(服务商需要为子商户开这个,而前五个都是用服务商自己的就可以了)
5、对于境内微信商户,Oauth2的网页验证域名和支付授权地址必须是通过了国内的ICP备案,不然不能用,对于境外微信商户则没有这个要求
以上四个东西都拿到做好了 就可以开始了 (开发文档无敌坑)
一、首先写个通过Oauth的验证的页面(注意redirect_url需要urlencode)
jsapi1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.net.*,java.io.*,java.text.*,java.util.*,com.demo.*,com.demo.dao.*,com.demo.utils.*,java.sql.*" %>
<%@ page language="java" import="java.net.URLDecoder" %>
<%@ page language="java" import="org.json.JSONObject" %>
<%
request.setCharacterEncoding("UTF-8");
String currCode = request.getParameter("currCode");
String oauth2_url = "";
String wechat_appid = "数据库读取出来比较好";
String wechat_appsecret = "数据库读取出来比较好";
String currCode = "RMB";
String state = currCode+"|"+wechat_appid+"|end";
if(wechat_appid==""){
System.out.println("no wechat appid");
}else{
System.out.println("WECHATappid:"+wechat_appid);
}
String redirect_url = "https://www.myserver.com/jsapi2.jsp";
redirect_url = URLEncoder.encode(redirect_url, "UTF-8");
oauth2_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+wechat_appid+"&redirect_uri="+redirect_url+"&response_type=code&scope=snsapi_base&state="+state+"#wechat_redirect";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><fmt:message key="pg.eng.payment.payWECHATONL.text.title"/></title>
</head>
<body>
<!-- this page will direct to wechat authorize url and then wechat will redirect to redirect_url with code and state -->
<script language="JavaScript" type="text/javascript">
window.location.href="<%=oauth2_url%>";
</script>
</body>
</html>
二、然后微信会根据上一个页面的redirect_url跳转到对应页面并给出一个openId,在这个页面可以完成输入金额的操作(注意要验证是否微信浏览器打开这个页面,js里面那个is_weixin();方法)
jsapi2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.net.*,java.io.*,java.text.*,java.util.*,com.demo.*,com.demo.dao.*,com.demo.utils.*,java.sql.*" %>
<%@ page language="java" import="java.net.URLDecoder" %>
<%@ page language="java" import="org.json.JSONObject" %>
<%
request.setCharacterEncoding("UTF-8");
String openId = "";
String code = request.getParameter("code");
String state = request.getParameter("state");
System.out.println("code:"+code+",state:"+state);
String[] statespilt = state.split("\\|");
String currCode = statespilt[0];
String wechat_appid = statespilt[1];
String wechat_appsecret = "用wechat_appid查数据库读取出来比较好";
String notify_url = "回调地址";
System.out.println("wechat_appid:"+wechat_appid+",wechat_appsecret:"+wechat_appsecret);
String access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+wechat_appid+"&secret="+wechat_appsecret+"&code="+code+"&grant_type=authorization_code";
System.out.println("access_token_url:"+access_token_url);
String accesscode_rs = "";
accesscode_rs = HttpUtil.postData("",access_token_url);
System.out.println("accesscode_rs"+accesscode_rs);
JSONObject json;
json = new JSONObject(accesscode_rs);
System.out.println("OTTO-------set JSONObject");
openId = json.getString("openid");
System.out.println("OTTO-------openid:"+openId);
%>
<!-- ******************************** input page start *************************************** -->
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<script language="JavaScript" type="text/javascript">
window.onload=function(){
is_weixin();
}
function is_weixin(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger") {
} else {
window.location.href="remind.jsp";
return;
}
}
</script>
<title>
input page
</title>
</head>
<body>
<form action="jsapi3.jsp" method="POST" >
<input type="text" id="amount"