Public Account Payment (3): The page calls the payment JS and completes the payment, and the js completes the payment
I. Called JS files
1. First, bind the [JS interface security domain name]. In the "function Settings" section of "public account settings ",
2. Introduce JS files
Note: supports loading using AMD/CMD standard modules.
1 <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
3. Inject the permission verification configuration through the config Interface
1 wx. config ({2 debug: true, // enable the debug mode. The returned values of all called APIs are displayed on the alert client. To view the input parameters, open them on the pc, the parameter information is output through log and printed only on the pc end. 3 appId: '', // required. The unique public ID is 4 timestamp:, // required. The signature timestamp is 5 nonceStr :'', // required. The random string for generating the signature is 6 signature: '', // required. For details, see Appendix 17 jsApiList: ['choosewxpay'] // required, list of JS interfaces to be used. Here, only 8} of the payment is written });
4. Verify the processing by using the ready interface
1 wx. ready (function () {2 wx. hideOptionMenu (); // hide some menus on the right. 3 });
Ii. Signature in wx. config
1. First, you must obtain the access token: globally unique ticket of the public account. Then, get the jsapi_ticket: public account used to call the JS interface. Use jsapi_ticket to obtain the signature.
2. Get access token: Document: url: http://mp.weixin.qq.com/wiki/15/54ce45d8d30b6bf6758f68d2e95bc627.html
1 public static Token getToken (String appid, String appsecret) {2 Token token = null; 3 String requestUrl = Constants. token_url.replace ("APPID", appid ). replace ("APPSECRET", appsecret); 4 // initiate a GET request to obtain the credential 5 JSONObject jsonObject = CommonUtil. httpsRequestToJsonObject (requestUrl, "GET", null); 6 7 if (null! = JsonObject) {8 try {9 token = new Token (); 10 token. setAccessToken (jsonObject. getString ("access_token"); 11 token. setExpiresIn (jsonObject. getInt ("expires_in"); 12} catch (JSONException e) {13 token = null; 14 // failed to get token 15 log. error ("failed to get token" + jsonObject. getInt ("errcode") + "," + jsonObject. getString ("errmsg"); 16} 17} 18 return token; 19}
Some methods are in the first two articles.
3. Get jsapi_ticket
1 public static Ticket getTicket () {2 // Constants. ticket_url = https://api.weixin.qq.com/cgi-bin/ticket/getticket? Access_token = ACCESS_TOKEN & type = jsapi 3 String requestUrl = Constants. ticket_url.replace ("ACCESS_TOKEN", TokenThread. accessToken. getAccessToken (); 4 // initiate a GET request to obtain the credential 5 JSONObject jsonObject = CommonUtil. httpsRequestToJsonObject (requestUrl, "GET", null); 6 Ticket ticket = null; 7 String jsapi_ticket = ""; 8 int expires_in = 0; 9 if (null! = JsonObject) {10 try {11 jsapi_ticket = jsonObject. getString ("ticket"); 12 expires_in = jsonObject. getInt ("expires_in"); 13 ticket = new Ticket (); 14 ticket. setTicket (jsapi_ticket); 15 ticket. setExpiresIn (expires_in); 16} catch (JSONException e) {17 // retrieval failed 18 log. error ("failed to get jsapi_ticket" + jsonObject. getInt ("errcode") + "," + jsonObject. getString ("errmsg"); 19} 20} 21 return ticket; 22}
Note: jsapi_ticket and access token are valid at 7200. I want to get it again after 7200. I put it in an endless loop in the thread.
4. Calculate the signature
1/** 2 * signature entity Class 3 * @ author rory. wu 4*5 */6 public class Signature implements Serializable {7 private static final long serialVersionUID =-7799030247222425708l; 8 9 private String url; 10 private String jsapi_ticket; 11 private String nonceStr; 12 private String timestamp; 13 private String signature; 14 15 // The following is the getset Method 16}
1/** 2 * calculate the Signature 3 * @ param jsapi_ticket 4 * @ param url the address of calling js in the business 5 * @ return 6 */7 public static Signature sign (String jsapi_ticket, string url) {8 String nonce_str = CommonUtil. create_nonce_str (); 9 String timestamp = CommonUtil. create_timestamp (); 10 String string1; 11 String signature = ""; 12 13 // note that all parameter names must be in lower case, the order must be 14 string1 = "jsapi_ticket =" + jsapi_ticket + 15 "& noncestr =" + nonce_str + 16 "& timestamp =" + timestamp + 17 "& url =" + url; 18 try19 {20 MessageDigest crypt = MessageDigest. getInstance ("SHA-1"); 21 crypt. reset (); 22 crypt. update (string1.getBytes ("UTF-8"); 23 signature = CommonUtil. byteToStr (crypt. digest (); 24} 25 catch (NoSuchAlgorithmException e) 26 {27 e. printStackTrace (); 28} 29 catch (UnsupportedEncodingException e) 30 {31 e. printStackTrace (); 32} 33 34 Signature result = new Signature (); 35 result. setUrl (url); 36 result. setJsapi_ticket (jsapi_ticket); 37 result. setNonceStr (nonce_str); 38 result. setTimestamp (timestamp); 39 result. setSignature (signature); 40 41 return result; 42}
Note: Some methods are described in the first two articles.
In this way, the signature is calculated.
5. pass to the front-end page
The random string: nonce_str, timestamp, appId, signature, prepayment ID packaged as prepay_id = prepay_id is passed to the front end.
1 wx. config ({2 appId: '$ {appId}', // required. The unique identifier of the public number is 3 timestamp: $ {timestamp}. // required, signature generation timestamp 4 nonceStr: '$ {nonceStr}', // required. The random string of the generated signature is 5 signature: '$ {signature}', // required, for signature, see Appendix 16 jsApiList: ['choosewxpay'] // required. List of JS interfaces to be used. For a list of all JS interfaces, see Appendix 27 });
6. Now you can use JS.
Iii. Call payment
In the previous article, you have obtained the pre-payment order ID, prepay_id,
1/** 2 * payment object 3 * @ author rory. wu 4*5 */6 public class WxPay implements Serializable {7 private static final long serialVersionUID = 38442523517555525l; 8 private String paySign; 9 private String prepay_id; 10 private String nonce_str; 11 private String timeStamp; 12 13 // get, set Method 14}
1/** 2 * Get the parameters required for weixin payment JS on the page 3 * @ param map 4 * @ return 5 */6 private WxPay getWxPayInfo (String prepay_id) {7 String nonce = CommonUtil. create_nonce_str (). replace ("-", ""); 8 String timeStamp = CommonUtil. create_timestamp (); 9 // calculate the signature 10 String newPrepay_id = "prepay_id =" + prepay_id; 11 String args = "appId =" + Constants. appid12 + "& nonceStr =" + nonce13 + "& package =" + newPrepay_id14 + "& signType = MD5" 15 + "& timeStamp =" + timeStamp16 + "& key =" + key; 17 String paySign = SignUtil. getSign (args, "MD5"); 18 WxPay wxPay = new WxPay (); 19 wxPay. setNonce_str (nonce); 20 wxPay. setPaySign (paySign); 21 wxPay. setPrepay_id (newPrepay_id); 22 wxPay. setTimeStamp (timeStamp); 23 return wxPay; 24} 25
Note: Some methods are described in the first two articles.
The last step of payment: js call
1 wx. chooseWXPay ({2 timestamp: json. timeStamp, 3 nonceStr: json. nonce_str, 4 package: json. prepay_id, 5 signType: 'md5', 6 paySign: json. paySign, 7 success: function (res) {8 alert ("Payment successful"); 9} 10 });
The payment is complete. If you have any questions, please leave a message to me. Only java, 88