此文针对有自己的账户系统情况,如果只是想接支付直接看最后的代码
开发公众号支付时,在统一下单接口中要求必传用户openid,而获取openid则需要您在公众平台设置获取openid的域名,只有被设置过的域名才是一个有效的获取openid的域名,否则将获取失败。具体界面如图所示
2.在微信公众平台获取AppID和appSecret;
通过这两个参数使用
https://open.weixin.qq.com/connect/oauth2/authorize?appid=AppID&redirect_uri=url&response_type=code&scope=snsapi_base&state=state#wechat_redirect可以获得用户的code,这个地址写在微信公众号底部的菜单栏中让用户点击
在后台使用code获取accessToken
/** * 通过微信用户的code换取网页授权access_token * @return * @throws IOException * @throws */public List
List
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); HttpResponse res = client.execute(post); if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity(); String str = EntityUtils.toString(entity, "utf-8"); ObjectMapper mapper=new ObjectMapper(); @SuppressWarnings("unchecked")
Map
return list;}
try {
List
System.out.println("--openid获取错误");return new ModelAndView("fail");}
从accessToken中获取用户的openid,将openId和公司的用户进行对应存入数据库中(这个看具体需求);
微信商户平台设置:
在微信商户平台(pay.weixin.qq.com)设置您的公众号支付支付目录,设置路径:商户平台-->产品中心-->开发配置,如图4所示。公众号支付在请求支付的时候会校验请求来源是否有在商户平台做了配置,所以必须确保支付目录已经正确的被配置,否则将验证失败,请求支付不成功。
获取api_secret:
/**
* 公众号支付
*/
@RequestMapping(value ="/gzhPay",method = {RequestMethod.POST,RequestMethod.GET})
public ModelAndView webPay(HttpServletRequest request, HttpServletResponse response) {
System.out.println("------------------gzhPay start-------------------");
ModelAndView mv=new ModelAndView();
String total_fee = request.getParameter("total_fee");
//获取用户的code
String code = request.getParameter("code");
String openId=null;
try {
List
openId=list.get(1).toString();
} catch (IOException e) {
System.out.println("--openid获取错误");
}
//我之前已经将用户的openid和公司的账户系统绑定,这里我是从数据库中查找到用户的openId
if (StrKit.isBlank(openId)) {
return mv;
}
if (StrKit.isBlank(total_fee)) {
return mv;
}
String ip = IpKit.getRealIp(request);
if (StrKit.isBlank(ip)) {
ip = "127.0.0.1";
}
WxPayApiConfigKit.putApiConfig(getApiConfig());
Map
.setAttach("IJPay 公众号支付测试 -By Javen")
.setBody("IJPay 公众号支付测试 -By Javen")
.setOpenId(openId)
.setSpbillCreateIp(ip)
.setTotalFee(total_fee)
.setTradeType(WxPayApi.TradeType.JSAPI)
.setNotifyUrl(notify_url)
.setOutTradeNo(String.valueOf(System.currentTimeMillis()))
.build();
String xmlResult = WxPayApi.pushOrder(false,params);
log.info(xmlResult);
Map
String return_code = resultMap.get("return_code");
String return_msg = resultMap.get("return_msg");
if (!PaymentKit.codeIsOK(return_code)) {
return mv;
}
String result_code = resultMap.get("result_code");
if (!PaymentKit.codeIsOK(result_code)) {
return mv;
}
// 以下字段在return_code 和result_code都为SUCCESS的时候有返回
String prepay_id = resultMap.get("prepay_id");
String timeStamp=String.valueOf(System.currentTimeMillis() / 1000);
String nonce_str=String.valueOf(System.currentTimeMillis());
String packages = "prepay_id="+prepay_id;
Map
packageParams.put("appId", appID);
packageParams.put("timeStamp", timeStamp);
packageParams.put("nonceStr", nonce_str);
packageParams.put("package", "prepay_id=" + prepay_id);
packageParams.put("signType", "MD5");
//PaternerKey为商户平台设置的api_secret;
StringpackageSign=PaymentKit.createSign(packageParams,
WxPayApiConfigKit.getWxPayApiConfig().getPaternerKey());
packageParams.put("paySign", packageSign);
//下面可以通过Map传入参数获取json字符串,其形式和上面一样,传到前台可以转为json形式再用,也是一个不错的方式,可以参考IJPay
/*Map
String jsonStr = JSON.toJSONString(packageParams);
mv.addObject("jsonStr",jsonStr);*/
mv.addObject("appid", appID);
mv.addObject("timeStamp", timeStamp);
mv.addObject("nonceStr", nonce_str);
mv.addObject("packageValue", packages);
mv.addObject("paySign", packageSign);
mv.addObject("success","ok");
mv.setViewName("pay");
return mv;
}
前台jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>