Home  >  Article  >  WeChat Applet  >  PHP development WeChat payment method code to obtain user address

PHP development WeChat payment method code to obtain user address

高洛峰
高洛峰Original
2017-03-21 15:39:261399browse

The delivery address sharing function of WeChat Pay is mainly to uniformly manage the personal delivery addresses of WeChat users, and their delivery addresses can be applied to all developers who can call them. The user's delivery address contains a lot of personal information, so this interface must be applied for. The application method can be viewed on the mp platform.

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.

But it may be because it involves user privacy, so during use, the user needs to actively choose to use this function, and only through a click operation can we obtain the user's delivery address. It's something to pay attention to.

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 and assemble it into an array parameter and pass it to the page.

3. The settlement page uses the user click event , combined with 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 the next time he makes a purchase.

Let’s talk about some 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 many in the program. things to do. 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, and state is optional here. Fill in, 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 #wechat_redirect

2. The user accesses this address and is redirected to the order settlement address with the code parameter added. On this page, the accessToken needs to be obtained by the program. Note that the accessToken is used to obtain user information. The accessToken is not the same as another access token used to interact with WeChat.

Use GET request to obtain the accessToken, 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;

There is one thing to note here, Sometimes WeChat has a seizure and requests the order settlement page multiple times in a row, causing the accessToken to become invalid and requiring 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 random characters String×tamp=Timestamp&url=URL of the current page, and then sha1encrypts 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 (time stamp 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" : "<?php echo $sign[&#39;appId&#39;]?>",
  "scope" : "jsapi_address",
  "signType" : "sha1",
  "addrSign" : "<?php echo $sign[&#39;addrSign&#39;]?>",
  "timeStamp" : "<?php echo $sign[&#39;timeStamp&#39;]?>",
  "nonceStr" : "<?php echo $sign[&#39;nonceStr&#39;]?>",
  },function(res){
  if(res.err_msg == &#39;edit_address:ok&#39;)
  {
       
       
      //将地址信息存入数据库
      //将地址信息显示在当前页面
      
      document.getElementById("address_info").innerHTML="<b>收件人:"+res.userName+"</b>   <b>"+res.telNumber+"</b><br /> 收货地址:"+res.proviceFirstStageName+res.addressCitySecondStageName+res.addressCountiesThirdStageName+res.addressDetailInfo;
       
 } else{ alert("获取地址失败,请重新点击"); } }); }

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

The above is the detailed content of PHP development WeChat payment method code to obtain user address. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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