微信扫码支付 模式一 (JSAPI)

本文详细介绍了微信扫码支付的静态二维码模式一,包括设置Oauth2验证域名、支付授权地址、获取必要的支付参数,以及各个阶段的页面实现,如jsapi1.jsp到jsapiResult.jsp。开发过程中需要注意细节,如redirect_url的处理、微信浏览器的检测,以及账号配置的准确性,这些是成功实现支付的关键。
摘要由CSDN通过智能技术生成

这个微信支付是静态二维码支付,就是店面贴着一个二维码,让消费者自己扫自己输入金额,自己发起支付的支付方式。

要准备的东西比较麻烦:
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" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值