Home > Backend Development > PHP Tutorial > PHP development WeChat payment to obtain the user address, PHP development to obtain the user_PHP tutorial

PHP development WeChat payment to obtain the user address, PHP development to obtain the user_PHP tutorial

WBOY
Release: 2016-07-12 09:07:52
Original
647 people have browsed it

php development WeChat payment to obtain user address, php development to obtain user

Using WeChat to obtain address information is applied together with WeChat payment. If 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 to be paid attention to 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&redirect_uri=Order settlement address&response_type=code&scope=snsapi_base&state=Feel free #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&secret=APP_SECRET&code=CODE&grant_type=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:

function get_addr()
{
  WeixinJSBridge.invoke('editAddress',{
  "appId" : "<&#63;php echo $sign['appId']&#63;>",
  "scope" : "jsapi_address",
  "signType" : "sha1",
  "addrSign" : "<&#63;php echo $sign['addrSign']&#63;>",
  "timeStamp" : "<&#63;php echo $sign['timeStamp']&#63;>",
  "nonceStr" : "<&#63;php echo $sign['nonceStr']&#63;>",
  },function(res){
  if(res.err_msg == 'edit_address:ok')
  {
      
      
      //将地址信息存入数据库
      //将地址信息显示在当前页面
      
      document.getElementById("address_info").innerHTML="<b>收件人:"+res.userName+"</b>   <b>"+res.telNumber+"</b><br /> 收货地址:"+res.proviceFirstStageName+res.addressCitySecondStageName+res.addressCountiesThirdStageName+res.addressDetailInfo;
      
 } else{ alert("获取地址失败,请重新点击"); } }); }

Copy after login

At this point, the development of using WeChat to obtain user shared addresses is completed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1058151.htmlTechArticlephp development WeChat payment to obtain the user address, php development to obtain the user's address information using WeChat is applied together with WeChat payment , if the WeChat payment application is passed, you can use this function. ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template