Use payment interface to get user address in Mall
To give a man to fish is better than to teach
Pay to get the user address
Using the Get address information is used in conjunction with the payment application, the payment application is approved and the function is available.
Mall, the use of payment to obtain the user's delivery address, you can omit the user input address information complex process, improve the user experience.
But it may be because of the user's privacy, so in the process of use, users need to actively choose to use this feature, and by clicking on the operation, we can get to the user's delivery address, this is to pay attention to.
The operation flow is as follows:
1. The user opens the shopping cart page, clicks the checkout, jumps to a oauth2 's page, the address is: https://open.weixin.qq.com/connect/oauth2/authorize
The 2.oauth2 page redirect the link to the checkout page, using PHP to obtain the code parameter in the link, which is processed to get to the Accesstoken value. Generate a signature, assemble an array of parameters to pass to the page.
3. The checkout page uses the user click event, combined with the array parameters generated in 2 to complete the function of obtaining the address. Here can have an address to get to use Ajax to record the function of the database, then the next time the customer shopping, there is no trouble.
In detail, the following points need to be noted:
1. Jump to oauth2 This step, in the user's opinion is not much difference, but in the program here there are a lot of things to do. The first is the parameters of the Oauth2 page, where AppID is the address of the Appid,redirect_uri for the Order settlement page after UrlEncode, Response_type is a fixed code,scope for the snsapi_base, The state can be filled out in this place, and there is a #wechat_redirect, then the final look of the link is:
Https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri= Order settlement Address & Response_type=code&scope=snsapi_base&state= Feel free to fill in #wechat_redirect
2. User access to the address, is repositioned to append the code parameter Order settlement address, this page needs to be obtained by the program to Accesstoken, note that the Accesstoken to obtain user information Accesstoken with another and interactive access Token is not the same one.
The accesstoken can be obtained using a GET request, either with curl or file_get_contents. The requested address is:
Https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=app_secret&code =code&grant_type=authorization_code;
Here is a point to note, sometimes convulsions, will be repeated requests for order settlement page, resulting in Accesstoken failure, need special treatment.
Here the signature generation and payment inside the signature is not the same, here is a lot simpler, just encrypt a string, in the format: accesstoken=accesstoken&appid=appid&noncestr=32 bit random string & timestamp= timestamp &url= The URL of the current page and then SHA1 encrypt the string.
In front-end pages need to use a series of parameters to achieve the function of getting the address, respectively, Appid,scope (the default is Jsapi_address), Signtype (the default is SHA1), Addrsign (above SHA1 encrypted string), TimeStamp (with the timestamp above), NONCESTR (the same as the random string above).
3. On the front-end page, use the following JS function to complete the operation of getting the user's address:
function get_addr () {weixinjsbridge.invoke (' editaddress ', {"appId": "
php echo $sign [' appId ']?>" ," Scope ":" Jsapi_address "," Signtype ":" SHA1 "," Addrsign ":" php echo $sign [' addrsign '] ?> php echo $sign [' TimeStamp '] ?> php echo $sign [' Noncestr '] ?>
//store address information in the database//display the address information on the current page
document.getElementById ("Address_info"). Innerhtml= "to :" +res.username+ " " +res.telnumber+ "
Shipping Address: "+res.provicefirststagename+res.addresscitysecondstagename+res.addresscountiesthirdstagename+ Res.addressdetailinfo;
} else{alert ("Get address failed, click again");}); }
At this point, the development using the Get user shared address is complete.
If you have any questions, please contact qq:97695870