Php development WeChat Payment get user address, php development get user _ PHP Tutorial

Source: Internet
Author: User
Tags sha1 encryption
Php Development pays for getting user addresses, and php development gets users. Php developer pays to get the user address. php developer obtains the user's address information and applies together with the payment. after the payment application is approved, this function can be used. Php Development pays for getting user addresses, php development gets users

The address information obtained is applied together with the payment. you can use this function after the payment application is approved.

In the mall, using payment to get the user's shipping address can omit the complicated process of user input address information and improve the user experience.

However, it may be because it involves user privacy. during use, you must choose to use this function and click the operation to obtain the user's shipping address, note this.

The procedure is as follows:

1. the user opens the shopping cart page, click settlement, jump to a page of oau22. address: https://open.weixin.qq.com/connect/oauth2/authorize

2. on the oau2page, link redirect to the settlement page, use PHP to get the code parameter in the link, and obtain the accessToken value after processing. Generate a signature and assemble it into an array to pass it to the page.

3. the Settlement page uses the user click event and the array parameters generated in 2 to get the address. Here, we can use ajax to record the obtained addresses to the database, so the customer will not have to worry about the next shopping.

Pay attention to the following points in detail:

1. there is no difference in the steps to jump to oau22. however, there are a lot of things to do here in the program. The first is the parameter of the oau2page, where appid is appid, redirect_uri is the address of the Order Settlement page after urlencode, response_type is fixed code, scope is fixed snsapi_base, the state can be entered at will in this place, and there is another # wechat_redirect, so the link will eventually look like:

Https://open.weixin.qq.com/connect/oauth2/authorize? Appid = APPID & redirect_uri = Order Settlement address & response_type = code & scope = snsapi_base & state = enter at will # wechat_redirect

2. the user accesses this address and is relocated to the order Settlement address appended with the code parameter. on this page, the program needs to obtain the accessToken, note that this accessToken is the accessToken used to obtain user information and is not the same as the access token used to interact with other users.

You can use the GET request to obtain the accessToken. you can use curl or file_get_contents. Request address:

Https://api.weixin.qq.com/sns/oauth2/access_token? Appid = APPID & secret = APP_SECRET & code = CODE & grant_type = authorization_code;

Note that the order Settlement page will be requested multiple times in a row, resulting in invalid accessToken and special processing is required.

The signature generation here is different from the signature in the payment. it is much simpler here, but only encrypts a string in the format: accesstoken = ACCESSTOKEN & appid = APPID & noncestr = 32-bit random string & timestamp = timestamp & url = URL of the current page, and then perform sha1 encryption on the string.

On the front-end page, you need to use a series of parameters to obtain the address, which are appID, scope (jsapi_address by default), and signType (sha1 by default ), addrSign (the sha1-encrypted string above), timeStamp (timeStamp of the same text), and nonceStr (random string of the same text ).

3. on the front-end page, use the following js function to obtain 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 ') {// Save the address information to the database // display the address information on the current page document. getElementById ("address_info "). innerHTML ="Recipient: "+ res. userName +"   "+ Res. telNumber +"
Shipping Address: "+ res. proviceFirstStageName + res. addressCitySecondStageName + res. addressCountiesThirdStageName + res. addressDetailInfo;} else {alert ("failed to get the address. please click ");}});}

Now, the development of getting user shared addresses is complete.

The token used to obtain the address information is applied together with the payment. after the payment application is passed, you can use this function ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.