The shipping address sharing function of payment is mainly used to manage users' personal shipping addresses in a unified manner. the shipping address can be applied to all developers who can call the address. The user's shipping address contains a lot of personal information, so this interface must be applied for, the method of application can be viewed on the mp platform. The address information obtained is applied together with the payment. you can use this function after the payment application is approved.
In the mall, using payment to get the user's shipping address can omit the complicated process of user input address information and improve the user experience.
However, it may be because it involves user privacy. during use, you must choose to use this function and click the operation to obtain the user's shipping address, note this.
The procedure is as follows:
1. the user opens the shopping cart page, click settlement, jump to a page of oau22. address: https://open.weixin.qq.com/connect/oauth2/authorize
2. on the oau2page, link redirect to the settlement page, use PHP to get the code parameter in the link, and obtain the accessToken value after processing. Generate a signature and assemble it into an array to pass it to the page.
3. the Settlement page uses the user click event and the array parameters generated in 2 to get the address. Here, we can use ajax to record the obtained addresses to the database, so the customer will not have to worry about the next shopping.
Pay attention to the following points in detail:
1. there is no difference in the steps to jump to oau22. however, there are a lot of things to do here in the program. The first is the parameter of the oau2page, where appid is appid, redirect_uri is the address of the Order Settlement page after urlencode, response_type is fixed code, scope is fixed snsapi_base, the state can be entered at will in this place, and there is another # wechat_redirect, so the link will eventually look like:
Https://open.weixin.qq.com/connect/oauth2/authorize? Appid = APPID & redirect_uri = Order Settlement address & response_type = code & scope = snsapi_base & state = enter at will # wechat_redirect
2. the user accesses this address and is relocated to the order Settlement address appended with the code parameter. on this page, the program needs to obtain the accessToken, note that this accessToken is the accessToken used to obtain user information and is not the same as the access token used to interact with other users.
You can use the GET request to obtain the accessToken. you can use curl or file_get_contents. Request address:
Https://api.weixin.qq.com/sns/oauth2/access_token? Appid = APPID & secret = APP_SECRET & code = CODE & grant_type = authorization_code;
Note that the order Settlement page will be requested multiple times in a row, resulting in invalid accessToken and special processing is required.
The signature generation here is different from the signature in the payment. it is much simpler here, but only encrypts a string in the format: accesstoken = ACCESSTOKEN & appid = APPID & noncestr = 32-bit random string & timestamp = timestamp & url = URL of the current page, and then perform sha1 encryption on the string.
On the front-end page, you need to use a series of parameters to obtain the address, which are appID, scope (jsapi_address by default), and signType (sha1 by default ), addrSign (the sha1-encrypted string above), timeStamp (timeStamp of the same text), and nonceStr (random string of the same text ).
3. on the front-end page, use the following js function to obtain the user address:
Function get_addr () {WeixinJSBridge. invoke ('editaddress', {"appId": "<? Php echo $ sign ['appid ']?> "," Scope ":" jsapi_address "," signType ":" sha1 "," addrSign ":" <? Php echo $ sign ['addrsign']?> "," TimeStamp ":" <? Php echo $ sign ['timestamp']?> "," NonceStr ":" <? Php echo $ sign ['noncestr']?> ",}, Function (res) {if (res. err_msg = 'edit _ address: OK ') {// Save the address information to the database // display the address information on the current page document. getElementById ("address_info "). innerHTML ="Recipient: "+ res. userName +" "+ Res. telNumber +"
Shipping Address: "+ res. proviceFirstStageName + res. addressCitySecondStageName + res. addressCountiesThirdStageName + res. addressDetailInfo;} else {alert ("failed to get the address. please click ");}});}
Now, the development of getting user shared addresses is complete.