Skip to content

微信支付

liyiorg edited this page Nov 26, 2014 · 1 revision

###生成web js 微信支付请求json 数据

package weixin.popular.example;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import weixin.popular.bean.pay.PayPackage;
import weixin.popular.util.PayUtil;

/**
 * 生成WEB JS 支付请求json
 * @author LiYi
 *
 */
public class PayJsServlet extends HttpServlet{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String appid;			//微信后台获取
	private String partner_id;      //微信支付申请人邮箱获取
	private String partner_key;		//微信支付申请人邮箱获取
	private String pay_sign_key;	//微信支付申请人邮箱获取

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//payPackage 的商品信息,总价可以通过前端传入

		PayPackage payPackage = new PayPackage();
		payPackage.setBank_type("WX");
		payPackage.setBody("商品信息");
		payPackage.setPartner(partner_id);
		payPackage.setOut_trade_no("123456");//订单号
		payPackage.setTotal_fee("1");//单位分
		payPackage.setFee_type("1");
		payPackage.setNotify_url("http://mydomain.com/weixin/notify");//通知回调地址
		payPackage.setSpbill_create_ip(request.getRemoteHost());
		payPackage.setInput_charset("UTF-8");
		String json = PayUtil.generatePayJsRequestJson(payPackage,appid,partner_key,pay_sign_key);

		//将json 传到jsp 页面
		request.setAttribute("json", json);
		//示例jsp
		request.getRequestDispatcher("pay_example.jsp").forward(request,response);
	}


}

###jsp 页面发起js 支付请求

<%@ 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>pay js example</title>
<script type="text/javascript">
//json 数据
var x_json = ${json};
function pay(){
	WeixinJSBridge.invoke('getBrandWCPayRequest',x_json,function(res){
		if(res.err_msg == 'get_brand_wcpay_request:ok'){
			//支付成功,可以做跳转到支付成功的提示页面
		}else{
			//支付失败
			alert(res.err_msg);
		}
	});
}
</script>
</head>
<body>

</body>
</html>

###notify

package weixin.popular.example;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import weixin.popular.bean.pay.PayNotify;
import weixin.popular.util.SignatureUtil;
import weixin.popular.util.XMLConverUtil;

/**
 * 支付回调通知
 * @author LiYi
 *
 */
public class PayNotifyServlet extends HttpServlet{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String pay_sign_key;	//微信支付申请人邮箱获取

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取请求数据
		PayNotify payNotify = XMLConverUtil.convertToObject(PayNotify.class, request.getInputStream());
		//签名验证
		if(!SignatureUtil.validateAppSignature(payNotify, pay_sign_key)){
			response.getOutputStream().write("error".getBytes());
		}else{
			String sign_type = request.getParameter("sign_type");
			String input_charset = request.getParameter("input_charset");
			String sign = request.getParameter("sign");
			String trade_mode = request.getParameter("trade_mode");
			String trade_state = request.getParameter("trade_state");//支付结果 0 成功
			String partner = request.getParameter("partner");
			String bank_type = request.getParameter("bank_type");
			String bank_billno = request.getParameter("bank_billno");
			String total_fee = request.getParameter("total_fee");
			String fee_type = request.getParameter("fee_type");
			String notify_id = request.getParameter("notify_id");
			String transaction_id = request.getParameter("transaction_id");//交易号
			String out_trade_no = request.getParameter("out_trade_no");
			String attach = request.getParameter("attach");
			String time_end = request.getParameter("time_end");
			String transport_fee = request.getParameter("transport_fee");
			String product_fee = request.getParameter("product_fee");
			String discount = request.getParameter("discount");
			response.getOutputStream().write("success".getBytes());
		}
	}


}

API 列表

Clone this wiki locally