It is better to teach a man to fish than to teach him to fish
WeChat payment gets user address
Using WeChat to obtain address information is applied together with WeChat payment. Once the WeChat payment application is passed, you can use this function.
In WeChat Mall, using WeChat Pay to obtain the user's delivery address can omit the complicated process of users entering address information and improve user experience.
However, it may be because it involves user privacy, so during the use process, the user needs to actively choose to use this function, and only through a click operation can we obtain the user's delivery address. This should be noted of.
The operation process is as follows:
1. The user opens the shopping cart page, clicks checkout, and jumps to a WeChat oauth2 page at the address: https://open.weixin.qq.com/connect/oauth2/authorize
2.oauth2 page redirects the link to the settlement page, uses PHP to obtain the code parameter in the link, and obtains the accessToken value after processing. Generate a signature, assemble it into an array of parameters and pass it to the page.
3. The settlement page uses user click events and combines the array parameters generated in 2 to complete the function of obtaining the address. There can be a function here to record the obtained address into the database using ajax, so that the customer will not have to worry about it next time he or she makes a purchase.
Let’s talk about the points that need attention in detail:
1. The step of jumping to WeChat oauth2 is not much different from the user's point of view, but there are a lot of things to do in the program. The first is the parameters of the oauth2 page, where appid is the WeChat appid, redirect_uri is the address of the order settlement page after urlencode, response_type is a fixed code, scope is a fixed snsapi_base, state can be filled in at will, and there is also a #wechat_redirect. Then the final look of the link is:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID<span>&redirect_uri</span>=订单结算地址<span>&response_type</span>=code<span>&scope</span>=snsapi_base<span>&state</span>=随意填写#wechat_redirect
2. The user accesses this address and is redirected to the order settlement address with the added code parameter. On this page, the accessToken needs to be obtained by the program. Note that the accessToken is the accessToken for obtaining user information and another access for interacting with WeChat. The tokens are not the same.
You can obtain the accessToken using GET request, you can use curl or file_get_contents. The request address is:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID<span>&secret</span>=APP_SECRET<span>&code</span>=CODE<span>&grant_type</span>=authorization_code;
One thing to note here is that sometimes WeChat has a seizure and requests the order settlement page multiple times in a row, causing the accessToken to become invalid and requires special handling.
The signature generation here is different from the signature in WeChat payment. It is much simpler. It just encrypts a string. The format is: accesstoken=ACCESSTOKEN&appid=APPID&noncestr=32-digit random string×tamp=timestamp&url=current page URL, and then perform sha1 encryption on the string.
A series of parameters need to be used in the front-end page to achieve the function of obtaining the address, namely appID, scope (default is jsapi_address), signType (default is sha1), addrSign (the string encrypted by sha1 above), timeStamp ( timestamp as above), nonceStr (random string as above).
3. On the front-end page, use the following js function to complete the operation of obtaining the user address:
<span>function get_addr() { WeixinJSBridge.invoke('editAddress',{ "appId" : "</span><span><?</span><span>php echo $sign['appId']</span><span>?></span><span>", "scope" : "jsapi_address", "signType" : "sha1", "addrSign" : "</span><span><?</span><span>php echo $sign['addrSign']</span><span>?></span><span>", "timeStamp" : "</span><span><?</span><span>php echo $sign['timeStamp']</span><span>?></span><span>", "nonceStr" : "</span><span><?</span><span>php echo $sign['nonceStr']</span><span>?></span><span>", },function(res){ if(res.err_msg == 'edit_address:ok') { </span><span> <br /> //将地址信息存入数据库 //将地址信息显示在当前页面 </span>
<span> document.getElementById("address_info").innerHTML="<b>收件人:"+res.userName+"</b> <b>"+res.telNumber+"</b><br /><span> 收货地址:"+res.proviceFirstStageName+res.addressCitySecondStageName+res.addressCountiesThirdStageName+res.addressDetailInfo; </span></span>
<span> } else{ alert("获取地址失败,请重新点击"); } }); }</span>
At this point, the development of using WeChat to obtain user shared addresses is completed.
If you have any questions, please contact QQ: 97695870