微信公眾號支付接口,微信公眾號支付接口代碼
公司做公眾號時需要接入微信支付,需要微信公眾號支付接口.個人根據(jù)網(wǎng)上的demo摸索著完成了公司公眾號的支付和退款功能。小編搜集了一份案例,現(xiàn)也將代碼分享出來,希望對需要朋友有幫助。
一.提交支付的toPay.jsp頁面代碼:
<%
String basePath = request.getScheme() + "://"+ request.getServerName() + request.getContextPath()+ "/";
%>
<html>
<body>
....
<div class="zdx3"><button onclick="pay()">共需支付${sumPrice }元 確認(rèn)支付</button></div>
</body>
</html>
<script>
function pay() {
var url="<%=basePath%>wechat/pay?money=${sumPrice}"; //注意此處的basePath是沒有端口號的域名地址。如果包含:80,在提交給微信時有可能會提示 “redirect_uri參數(shù)錯誤” 。
//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.提供一個微信支付完成后的回調(diào)接口)
第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";// 商戶號對應(yīng)的密鑰
/*------1.獲取參數(shù)信息------- */
//商戶訂單號
String out_trade_no= request.getParameter("state");
//價格
String money = request.getParameter("money");
//金額轉(zhuǎn)化為分為單位
String finalmoney = WeChat.getMoney(money);
//獲取用戶的code
String code = request.getParameter("code");
/*------2.根據(jù)code獲取微信用戶的openId和access_token------- */
//注: 如果后臺程序之前已經(jīng)得到了用戶的openId 可以不需要這一步,直接從存放openId的位置或session中獲取就可以。
//toPay.jsp頁面中提交的url路徑也就不需要再經(jīng)過微信重定向。寫成:http://localhost:8080/項目名/wechat/pay?money=${sumPrice}&state=${orderId}
String openid=null;
try {
List
以上就是微信公眾號支付接口的全部內(nèi)容,希望能夠?qū)π枰呐笥延幸恍椭?,想要了解更多?a href="http://descansotropical.com/ecschool/wsgh/" target="_blank">微信公眾號文章素材,可以訪問Hi商學(xué)院!