近来对支付来点兴趣,搞了一个微信H5的支付demo供大家参考一下。
@RequestMapping(value ="/H5",method = {RequestMethod.POST,RequestMethod.GET})
public String H5(){return "text_jsp/h5";//微信订单的页面
}
/**
* 微信H5 支付
* 注意:必须再web页面中发起支付且域名已添加到开发配置中
* @throws DocumentException
* @throws IOException
*/
@RequestMapping(value ="/H5Pay",method = {RequestMethod.POST,RequestMethod.GET})
public void H5Pay(HttpServletRequest request,HttpServletResponse response,String ip) throws IOException, DocumentException{
String value = String.valueOf(System.currentTimeMillis());//随机生成的微信订单号
SortedMap<String, String> packageParams = new TreeMap<String, String>();
String nonce_str = UUID.randomUUID().toString().replaceAll("-", "");//生成随机32位字符串
String spbill_create_ip = request.getRemoteAddr();//获取客户端的IP,在没有转发的情况下可以用这个获取IP,转发了就不可以了!
String khdIP = getIpAddr(request);//转发获取客户端IP的方法
packageParams.put("appid", appID);// 公众账号ID
packageParams.put("body", "美人");//商品描述
packageParams.put("mch_id", mchID);//商户号
packageParams.put("nonce_str", nonce_str);//随机字符串
packageParams.put("out_trade_no", value);//订单号
packageParams.put("total_fee", "1");//金额
packageParams.put("spbill_create_ip",khdIP);//客户端IP
packageParams.put("notify_url", h5_notify_url);//回调函数
packageParams.put("trade_type", "MWEB"); //支付类型,H5是MWEB类型
packageParams.put("scene_info", "{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"http://g.lotut.com\",\"wap_name\": \"美人\"}}");//场景信息
RequestHandler reqHandler = new RequestHandler(request, response);
reqHandler.init(appID, secret, partnerKey);