微信公众号支付接口,微信公众号支付接口代码
公司做公众号时需要接入微信支付,需要微信公众号支付接口.个人根据网上的demo摸索着完成了公司公众号的支付和退款功能。小编搜集了一份案例,现也将代码分享出来,希望对需要朋友有帮助。
一.提交支付的toPay.jsp页面代码:
<%
String basePath = request.getScheme() + "://"+ request.getServerName() + request.getContextPath()+ "/";
%>
<html>
<body>
....
<div class="zdx3"><button onclick="pay()">共需支付${sumPrice }元 确认支付</button></div>
</body>
</html>
<script>
function pay() {
var url="<%=basePath%>wechat/pay?money=${sumPrice}"; //注意此处的basePath是没有端口号的域名地址。如果包含:80,在提交给微信时有可能会提示 “redirect_uri参数错误” 。
//money为订单需要支付的金额
//state中存放的为商品订单号
var weixinUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri="+encodeURI(url)+"&response_type=code&scope=snsapi_userinfo&state=${orderId}#wechat_redirect";
window.location.href=encodeURI(weixinUrl);
}
</script>
二.后台处理支付功能代码
(包含两部分:
1.处理支付信息,通过微信接口生成订单号,返回支付页面
2.提供一个微信支付完成后的回调接口)
第1部分代码:
/**
* 用户提交支付,获取微信支付订单接口
*/
@RequestMapping(value="/pay")
public ModelAndView pay(HttpServletRequest request,HttpServletResponse response){
ModelAndView mv = new ModelAndView();
String GZHID = "wxfd7c065eee11112222";// 微信公众号id
String GZHSecret = "b5b3a627f5d1f8888888888888";// 微信公众号密钥id
String SHHID = "111111111";// 财付通商户号
String SHHKEY = "mmmmmmmmmmmmmmm";// 商户号对应的密钥
/*------1.获取参数信息------- */
//商户订单号
String out_trade_no= request.getParameter("state");
//价格
String money = request.getParameter("money");
//金额转化为分为单位
String finalmoney = WeChat.getMoney(money);
//获取用户的code
String code = request.getParameter("code");
/*------2.根据code获取微信用户的openId和access_token------- */
//注: 如果后台程序之前已经得到了用户的openId 可以不需要这一步,直接从存放openId的位置或session中获取就可以。
//toPay.jsp页面中提交的url路径也就不需要再经过微信重定向。写成:http://localhost:8080/项目名/wechat/pay?money=${sumPrice}&state=${orderId}
String openid=null;
try {
List
以上就是微信公众号支付接口的全部内容,希望能够对需要的朋友有一些帮助,想要了解更多的微信公众号文章素材,可以访问Hi商学院!