


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['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') { //将地址信息存入数据库 //将地址信息显示在当前页面 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor
