In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the Java WeChat Pay official account payment, scan code payment example analysis, the article is very detailed, has a certain reference value, interested friends must read it!
First, H5 official account payment
Key points: correctly obtain openId and unified interface for issuing orders, correctly handle payment result notification, and correctly configure payment authorization directory
H5's payment method is widely used. This payment method is mainly used for the web page of the custom menu in Wechat. It relies on the Wechat client installed on the mobile phone, and the high version of Wechat supports WeChat Pay. Follow my process to pay attention to the instructions.
1 write a page for payment, which is a little easier to write because it is for testing
WeChat Pay sample order number: order number:
2 write a servlet to get code through Oauth
Package com.debug.weixin.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.debug.weixin.util.CommonUtil; import com.debug.weixin.util.ServerConfig; public class OauthServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response) } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String orderNo=request.getParameter ("orderNo"); / / call Wechat Oauth3.0 to get openid String redirectURL=ServerConfig.SERVERDOMAIN+ "/ BasicWeixin/payServletForH5?orderNo=" + orderNo; String redirectURI= "; try {redirectURI=CommonUtil.initOpenId (redirectURL);} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / System.out.println (redirectURI) / / RequestDispatcher dis= request.getRequestDispatcher (redirectURI); / / dis.forward (request, response); response.sendRedirect (redirectURI);}}
3 after obtaining the code, obtain the openId through REDIRECTURI, and call the API for unified ordering.
Package com.debug.weixin.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.SortedMap; import java.util.TreeMap; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.debug.weixin.pojo.WeixinOauth3Token; import com.debug.weixin.pojo.WeixinQRCode; import com.debug.weixin.util.AdvancedUtil; import com.debug.weixin.util.CommonUtil Import com.debug.weixin.util.ConfigUtil; import com.debug.weixin.util.PayCommonUtil; public class PayServletForH5 extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String orderNo=request.getParameter ("orderNo"); String code=request.getParameter ("code") / / get AccessToken WeixinOauth3Token token=AdvancedUtil.getOauth3AccessToken (ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code); String openId=token.getOpenId (); / call Wechat unified payment API SortedMap parameters = new TreeMap (); parameters.put ("appid", ConfigUtil.APPID); parameters.put ("mch_id", ConfigUtil.MCH_ID); parameters.put ("device_info", "1000") Parameters.put ("body", "my Test order"); parameters.put ("nonce_str", PayCommonUtil.CreateNoncestr ()); parameters.put ("out_trade_no", orderNo); / / parameters.put ("total_fee", String.valueOf (total)); parameters.put ("total_fee", "1"); parameters.put ("spbill_create_ip", request.getRemoteAddr ()) Parameters.put ("notify_url", ConfigUtil.NOTIFY_URL); parameters.put ("trade_type", "JSAPI"); parameters.put ("openid", openId); String sign = PayCommonUtil.createSign ("UTF-8", parameters); parameters.put ("sign", sign); String requestXML = PayCommonUtil.getRequestXml (parameters); String result = CommonUtil.httpsRequestForStr (ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML) System.out.println ("- -"); System.out.println (result); System.out.println ("- -"); request.setAttribute ("orderNo", orderNo); request.setAttribute ("totalPrice", "0.01") String payJSON= "; try {payJSON=CommonUtil.getH5PayStr (result,request);} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / System.out.println (payJSON); request.setAttribute (" unifiedOrder ", payJSON); RequestDispatcher dis= request.getRequestDispatcher (" h6Pay.jsp "); dis.forward (request, response);}}
When calling Wechat unified order issuing API, you need to pay attention to the signature algorithm. Only when the signature is calculated correctly can the payment be made smoothly.
Public static String getH5PayStr (String result,HttpServletRequest request) throws Exception {Map map = XMLUtil.doXMLParse (result); SortedMap params = new TreeMap (); params.put ("appId", ConfigUtil.APPID); params.put ("timeStamp", Long.toString (new Date (). GetTime ()); params.put ("nonceStr", PayCommonUtil.CreateNoncestr ()); params.put ("package", "prepay_id=" + map.get ("prepay_id")) Params.put ("signType", ConfigUtil.SIGN_TYPE); String paySign = PayCommonUtil.createSign ("UTF-8", params); params.put ("paySign", paySign); / / the generation rules of paySign are consistent with those of Sign String json = JSONObject.fromObject (params). ToString (); return json;}
4 write the final payment interface and call up Wechat H5 payment
Wechat H5 pays function jsApiCall () {WeixinJSBridge.invoke ('getBrandWCPayRequest', function (res) {WeixinJSBridge.log (res.err_msg); / / alert (res.err_code+res.err_desc+res.err_msg); if (res.err_msg = = "get_brand_wcpay_request:ok") {alert ("Congratulations, successful payment!") } else {alert (res.err_code+res.err_desc+res.err_msg);}} function callpay () {if (typeof WeixinJSBridge = = "undefined") {if (document.addEventListener) {document.addEventListener ('WeixinJSBridgeReady', jsApiCall, false);} else if (document.attachEvent) {document.attachEvent (' WeixinJSBridgeReady', jsApiCall); document.attachEvent ('onWeixinJSBridgeReady', jsApiCall) Else {jsApiCall ();}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.