


Use WeChat payment interface to obtain user address in WeChat mall, mall_PHP tutorial
Use the WeChat payment interface to obtain the user address in the WeChat mall. Mall
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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

The main difference between an abstract class and an interface is that an abstract class can contain the implementation of a method, while an interface can only define the signature of a method. 1. Abstract class is defined using abstract keyword, which can contain abstract and concrete methods, suitable for providing default implementations and shared code. 2. The interface is defined using the interface keyword, which only contains method signatures, which is suitable for defining behavioral norms and multiple inheritance.

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

This article provides a brief guide to buying and selling of Binance virtual currency updated in 2025, and explains in detail the operation steps of virtual currency transactions on the Binance platform. The guide covers fiat currency purchase USDT, currency transaction purchase of other currencies (such as BTC), and selling operations, including market trading and limit trading. In addition, the guide also specifically reminds key risks such as payment security and network selection for fiat currency transactions, helping users to conduct Binance transactions safely and efficiently. Through this article, you can quickly master the skills of buying and selling virtual currencies on the Binance platform and reduce transaction risks.
