PHP Development Micro-credit payment get user address _php Tips

Source: Internet
Author: User
Tags sha1

Using a micro-letter to obtain address information is a micro-letter payment with the application, micro-credit payment application through, you can use this function.

Micro-Credit Mall, the use of micro-letter payment to obtain the user's receiving address, you can omit the user input address information cumbersome process, improve user experience.

But may be because of the user's privacy, so in the use of the process, users need to choose their own active use of this function, and is through the click of the operation, we can get to the user's receiving address, this point is to pay attention to.

The operation process is as follows:

1. The user opens the shopping cart page, clicks the settlement, jumps to a micro-letter oauth2 the page, the address is: https://open.weixin.qq.com/connect/oauth2/authorize

The 2.oauth2 page links redirect to the billing page, uses PHP to get the code parameters in the link, and gets the Accesstoken value by processing. Generate a signature and assemble an array of parameters to pass to the page.

3. The billing page uses the user click event and completes the function of obtaining the address with the array parameters generated in 2. There is a function of using AJAX to log the data to the database, so the next time the customer goes shopping, there is no need to bother.

A few points to note in detail:

1. Jump to the micro-letter Oauth2 This step, the user does not seem to have much difference, but in the program here there are a lot of things to do. The first is the parameters of the Oauth2 page, where AppID is the address of the Appid,redirect_uri for UrlEncode Order settlement page, Response_type for fixed code,scope for fixed snsapi_base, State can fill in this place at will, and there is a #wechat_redirect, then the final appearance 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= fill in #wechat_redirect freely

2. The user accesses the address and is relocated to the additional code parameter order settlement address, which needs to be obtained by the program to Accesstoken, noting that the Accesstoken is accesstoken with another and micro-letter access to access the user's information Token is not the same.

The accesstoken can be obtained using a GET request, either 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;

Here is a point to note, and sometimes the micro-letter will be ventilation, will repeatedly request the Order Settlement page, resulting in Accesstoken failure, need special treatment.

Here the signature generation and micro-letter payment inside the signature is not the same, here is much simpler, just encrypt a string, in the form of: accesstoken=accesstoken&appid=appid&noncestr=32 bit random string & The timestamp= timestamp &url= the URL of the current page, and then SHA1 encrypt the string.

In the front-end page you need to use a series of parameters to achieve the function of getting the address, respectively, Appid,scope (default is jsapi_address), Signtype (default is SHA1), Addrsign (above SHA1 encrypted string), TimeStamp (with the time stamp above), Noncestr (with the above random string).

3. In the front-end page, use the following JS function to get the user address of the operation:

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 ']?> '] C9/>},function (res) {
  if (res.err_msg = = ' Edit_address:ok ')
  {
      
      
      //Send address information to the database
      //display address information on the current page
      document.getElementById ("Address_info"). Innerhtml= "<b> recipient:" +res.username+ "</b> <b > "+res.telnumber+" </b><br/> Receiving address: "+res.provicefirststagename+res.addresscitysecondstagename+ Res.addresscountiesthirdstagename+res.addressdetailinfo;
      
 } else{alert ("Get address failed, click again");}); }  

So far, the development of using micro-letters to obtain user-shared addresses has been completed.

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.