WeChat public account payment Development (java), Public java

Source: Internet
Author: User
Tags getnotify openid

Public Account Payment Development (java), Public java

1. There are also many payment methods. Today we are introducing the public account payment method. This is my first development. I may not be able to introduce it in some places. Please forgive me.

2. Prepare before development, we first go to https://pay.weixin.qq.com/wiki/doc/api/jsapi.php? Chapter = 11_1 download some tools and you can write them by yourself, but it may be a little troublesome. We can download them directly. Select

Download the SDK for the Java [payment] API and the tool for calling the example.

3. Generate a hyperlink to go to our recharge page.

4. the foreground calls js to initiate an asynchronous order request.

5. the background receives a request from the foreground, obtains the information sent from the foreground, and encapsulates the information for unified order. The information to be added to the Map is appid (public account ID) mch_id (merchant ID), body (Commodity Description), device_info (device ID), out_trade_no (Merchant Order Number), fee_type (price currency), total_amount (price amount) spbill_create_ip (Terminal IP), notify_url (Notification address), trade_type (transaction type), openid (User ID), nonce_str (random string); you can also add other values. For details, refer: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php? Chapter = 9_1. The request parameters, which are mandatory, are required. Other parameters can be set as needed. Call wxpay. unifiedOrder (map) to generate the order.

6. obtain the values of appId, timeStamp, nonceStr, package (value: prepay_id indicates the order id generated when the order is placed), and signType, and call generateSignature (Map) to generate the signature, store the generated signature to the 5th generated order and return it to the front-end using the json format. At this time, the front-end can obtain the value transmitted from the back-end, and then the front-end calls callpay (); in onBridgeReady (), you need to obtain the following values: appId, timeStamp, nonceStr, package, signType, and paySign (either one of them may fail to return to the password input page ), when the Password Input page appears, we have succeeded in more than half of it.

7. After the payment is successful, process the background callback, that is, the address in notify_url. Note: The callback address for receiving the asynchronous notification is also required:The notification url must be a directly accessible url and cannot contain parameters.. At the same time, we can get the payment result. We use the parameters in to generate a local signature, and compare it with the signature in the result set, we can perform our business processing. After processing is complete, we need to notify a response, otherwise it will request again at intervals.

8. The development is complete.

The Code is as follows:

(1) recharge page

<! DOCTYPE html> 

(2) recharge handling

Protected ModelAndView handleRequestInternal (HttpServletRequest request, HttpServletResponse response) throws Exception {String type = request. getParameter ("type"); String oppendId = request. getParameter ("oppendId"); if (type! = Null) {if (type. equals ("toadd") {return new ModelAndView ("weixin/pay/add "). addObject ("oppendId", oppendId);} else if (type. equals ("add") {/*** place an order with a public account */WXPayConfigImpl config = WXPayConfigImpl. getInstance (); WXPay wxpay = new WXPay (config); String totalFree = request. getParameter ("total_eter"); String out_trade_no = UtilPramKey. getPriamKey ("P"); // Order Number double free = Double. valueOf (totalFree); free = free * 100; totalFree = (int) free + ""; HashMap <String, String> data = new HashMap <String, String> (); data. put ("appid", config. getAppID (); data. put ("mch_id", config. getMchID (); data. put ("body", "product description"); data. put ("device_info", "WEB"); data. put ("out_trade_no", out_trade_no); data. put ("fee_type", "CNY"); data. put ("total_free", totalFree); data. put ("spbill_create_ip", "123.12.12.123"); data. put ("yy_url", "asynchronous notification address"); data. put ("trade_type", "JSAPI"); data. put ("openid", oppendId); data. put ("nonce_str", "random number"); // random number try {Map <String, String> r = wxpay. unifiedOrder (data); JSONObject js222 = new JSONObject (r); Date d = new Date (); if (r. get ("return_code "). equals ("SUCCESS") & r. get ("result_code "). equals ("SUCCESS") {// business processing module point} else {// order Failed} String timestr = d. getTime ()/1000 + ""; r. put ("timeStamp", timestr); String prepay_id = r. get ("prepay_id"); Map <String, String> payMap = new HashMap <String, String> (); payMap. put ("appId", config. getAppID (); payMap. put ("timeStamp", timestr); payMap. put ("nonceStr", r. get ("nonce_str"); payMap. put ("package", "prepay_id =" + prepay_id); payMap. put ("signType", "MD5"); String paySign = WXPayUtil. generateSignature (payMap, config. getKey (); r. put ("sign", paySign); JSONObject js = new JSONObject (r); writeStringToResponse (js. toString (), response);} catch (Exception e) {e. printStackTrace () ;}}} return null ;}

(3) asynchronous callback Processing

Protected ModelAndView handleRequestInternal (HttpServletRequest request, HttpServletResponse response) throws Exception {String re = getNotify (request); values (re, response); return null;} public String getNotify (HttpServletRequest request) {String result = null; String inlength; String policyxml = ""; try {while (inlength = request. getReader (). readLine ())! = Null) {policyxml + = inlength;} catch (IOException e ){
// Get XML error} if (StringUtils. isEmpty (policyxml )){
// Xml is Blank} try {Map <String, String> map = WXPayUtil. xmlToMap (yyxml); String appid = map. get ("appid"); String bank_type = map. get ("bank_type"); String cash_fee = map. get ("cash_fee"); String device_info = map. get ("device_info"); String fee_type = map. get ("fee_type"); String is_subscribe = map. get ("is_subscribe"); String mch_id = map. get ("mch_id"); String nonce_str = map. get ("nonce_str"); String openid = map. get ("o Penid "); String out_trade_no = map. get ("out_trade_no"); String result_code = map. get ("result_code"); String return_code = map. get ("return_code"); String sign = map. get ("sign"); String time_end = map. get ("time_end"); String total_fee = map. get ("total_fee"); String trade_type = map. get ("trade_type"); String transaction_id = map. get ("transaction_id"); Map <String, String> date = new HashMap <> (); date. put ("appid", Ppid); date. put ("bank_type", bank_type); date. put ("cash_fee", cash_fee); date. put ("device_info", device_info); date. put ("fee_type", fee_type); date. put ("is_subscribe", is_subscribe); date. put ("mch_id", mch_id); date. put ("nonce_str", nonce_str); date. put ("openid", openid); date. put ("out_trade_no", out_trade_no); date. put ("result_code", result_code); date. put ("return_code", return_code); date. put ("t Ime_end ", time_end); date. put ("total_fee", total_fee); date. put ("trade_type", trade_type); date. put ("transaction_id", transaction_id); WXPayConfigImpl config = WXPayConfigImpl. getInstance (); String localSign = WXPayUtil. generateSignature (date, config. getKey (); if (localSign. equals (sign) {if (result_code.equals ("SUCCESS") & return_code.equals ("SUCCESS ")) {// processing point of the business module} else {// signature Failed} catch (Ti On e) {// XML Conversion error e. printStackTrace ();} // the correct result must be returned in different ways. You cannot return only one result = "<xml>" + "<return_code> <! [CDATA [SUCCESS]> </return_code> "+" <return_msg> <! [CDATA [OK]> </return_msg> "+" </xml> "; return result;
}

(If any vulnerability exists, please submit it more. Thank you very much)

 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.