微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前开发过的微信支付。
一 H5公众号支付
要点:正确获取openId以及统一下单接口,正确处理支付结果通知,正确配置支付授权目录
H5的支付方式是使用较为广泛的方式,这种支付方式主要用于微信内自定义菜单的网页,依赖手机上安装的微信客户端,高版本的微信才支持微信支付,下面按我的流程注意说明
1 编写用于支付的页面,由于是测试用就写的简单了点
[html] view plain copy
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>微信支付样例</title>
-
-
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
-
</head>
-
-
<body>
-
<form action="oauthServlet" method="POST">
-
订单号:<input type="text" name="orderNo" />
-
<input type="submit" value="H5支付"/>
-
</form>
-
</br></br>
-
<form action="scanCodePayServlet?flag=createCode" method="POST">
-
订单号:<input type="text" name="orderNo" />
-
<input type="submit" value="扫码支付"/>
-
</form>
-
</body>
-
</html>
2 编写一个servlet用于通过Oauth获取code
[java] view plain co
-
package com.debug.weixin.servlet;
-
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
-
import javax.servlet.RequestDispatcher;
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import com.debug.weixin.util.CommonUtil;
-
import com.debug.weixin.util.ServerConfig;
-
-
public class OauthServlet extends HttpServlet {
-
-
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
this.doPost(request, response);
-
}
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
String orderNo=request.getParameter("orderNo");
-
//调用微信Oauth2.0获取openid
-
String redirectURL=ServerConfig.SERVERDOMAIN+"/BasicWeixin/payServletForH5?orderNo="+orderNo;
-
String redirectURI="";
-
try {
-
redirectURI=CommonUtil.initOpenId(redirectURL);
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
//System.out.println(redirectURI);
-
//RequestDispatcher dis= request.getRequestDispatcher(redirectURI);
-
//dis.forward(request, response);
-
response.sendRedirect(redirectURI);
-
}
-
-
}
3 获取到code后,通过REDIRECTURI获取openId,调用统一下单接口
[java] view plain cop
-
package com.debug.weixin.servlet;
-
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
import java.util.SortedMap;
-
import java.util.TreeMap;
-
-
import javax.servlet.RequestDispatcher;
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import com.debug.weixin.pojo.WeixinOauth2Token;
-
import com.debug.weixin.pojo.WeixinQRCode;
-
import com.debug.weixin.util.AdvancedUtil;
-
import com.debug.weixin.util.CommonUtil;
-
import com.debug.weixin.util.ConfigUtil;
-
import com.debug.weixin.util.PayCommonUtil;
-
-
public class PayServletForH5 extends HttpServlet {
-
-
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
this.doPost(request, response);
-
}
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
String orderNo=request.getParameter("orderNo");
-
String code=request.getParameter("code");
-
-
//获取AccessToken
-
-
WeixinOauth2Token token=AdvancedUtil.getOauth2AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code);
-
-
String openId=token.getOpenId();
-
-
//调用微信统一支付接口
-
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
-
parameters.put("appid", ConfigUtil.APPID);
-
-
parameters.put("mch_id", ConfigUtil.MCH_ID);
-
parameters.put("device_info", "1000");
-
parameters.put("body", "我的测试订单");
-
parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());
-
-
-
parameters.put("out_trade_no", orderNo);
-
//parameters.put("total_fee", String.valueOf(total));
-
parameters.put("total_fee", "1");
-
parameters.put("spbill_create_ip", request.getRemoteAddr());
-
parameters.put("notify_url", ConfigUtil.NOTIFY_URL);
-
parameters.put("trade_type", "JSAPI");
-
parameters.put("openid", openId);
-
-
String sign = PayCommonUtil.createSign("UTF-8", parameters);
-
parameters.put("sign", sign);
-
-
String requestXML = PayCommonUtil.getRequestXml(parameters);
-
-
String result = CommonUtil.httpsRequestForStr(ConfigUtil.UNIFIED_ORDER_URL,"POST", requestXML);
-
System.out.println("----------------------------------");
-
System.out.println(result);
-
System.out.println("----------------------------------");
-
-
request.setAttribute("orderNo", orderNo);
-
request.setAttribute("totalPrice", "0.01");
-
String payJSON="";
-
try {
-
payJSON=CommonUtil.getH5PayStr(result,request);
-
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
//System.out.println(payJSON);
-
request.setAttribute("unifiedOrder",payJSON);
-
-
RequestDispatcher dis= request.getRequestDispatcher("h5Pay.jsp");
-
dis.forward(request, response);
-
}
-
-
}
调用微信统一下单接口,需要注意签名算法,只有签名计算正确才能顺利支付
最终看下公众号支付和扫码支付的微信配置: